I observed that torch.multinomial
behaves deterministically on the CPU device. However, I am unable to reproduce the same results on the GPU. The following script demonstrates the issue I encountered:
import torch
import random
import numpy as np
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
seed = 2147483647
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
w1 = w2 = torch.rand(3065713)
device='cuda'
g = torch.Generator(device=device).manual_seed(2147483647) #############
random_selection1 = torch.multinomial(w1.to(device), 150, replacement=True, generator=g) ##############
print(random_selection1)
g = torch.Generator(device=device).manual_seed(2147483647) #############
random_selection2 = torch.multinomial(w2.to(device), 150, replacement=True, generator=g) ##############
print(random_selection2)
print((random_selection1 != random_selection2).sum())