1、随机性来自哪里?
(1)torch算法的随机数种子实现
def set_random_seed(seed: Optional[int] = None, deterministic: bool = False, diff_rank_seed: bool = False) -> int: """Set random seed. Args: seed (int, optional): Seed to be used. deterministic (bool): Whether to set the deterministic option for CUDNN backend, i.e., set `torch.backends.cudnn.deterministic` to True and `torch.backends.cudnn.benchmark` to False. Defaults to False. diff_rank_seed (bool): Whether to add rank number to the random seed to have different random seed in different threads. Defaults to False. """ if seed is None: seed = sync_random_seed() if diff_rank_seed: rank = get_rank() seed += rank random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) # torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # os.environ['PYTHONHASHSEED'] = str(seed) if deterministic: if torch.backends.cudnn.benchmark: print_log( 'torch.backends.cudnn.benchmark is going to be set as ' '`False` to cause cuDNN to deterministically select an ' 'algorithm', logger='current', level=logging.WARNING) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False if digit_version(TORCH_VERSION) >= digit_version('1.10.0'): torch.use_deterministic_algorithms(True) return seed
(2)cuda toolkit的随机数种子实现(即上述代码中的determinstic)
(3)显卡型号不同也可能导致算法回传结果不同((2 封私信 / 45 条消息) 不同机器的随机性 - 搜索结果 - 知乎 (zhihu.com))
(4)产生随机数的代码顺序(关于pytorch无法获得相同结果的另类原因 - 知乎 (zhihu.com))
(5)涉及到浮点数运算的原子操作。
综上,在进行实验时,尽可能地在一台机器上进行实验。
附多卡实验随机性:关于 seed 的一切之如何产生确定性随机 - 知乎 (zhihu.com)
标签:随机性,torch,False,random,rank,deterministic,seed,整理,MMEngine From: https://www.cnblogs.com/lzqdeboke/p/17734144.html