torch.use_deterministic_algorithms#
- torch.use_deterministic_algorithms(mode, *, warn_only=False)[source]#
設定 PyTorch 操作是否必須使用“確定性”演算法。也就是說,在相同輸入、相同軟體和硬體上執行時,總是產生相同輸出的演算法。啟用後,操作將在可用時使用確定性演算法,如果僅有非確定性演算法可用,則在呼叫時會丟擲
RuntimeError。注意
僅憑此設定並不足以使應用程式可重現。有關更多資訊,請參閱 可重現性。
注意
torch.set_deterministic_debug_mode()提供了此功能的替代介面。當
mode=True時,以下通常是非確定性的操作將變為確定性操作:在 CUDA 張量上呼叫的
torch.nn.Conv1d在 CUDA 張量上呼叫的
torch.nn.Conv2d在 CUDA 張量上呼叫的
torch.nn.Conv3d在 CUDA 張量上呼叫的
torch.nn.ConvTranspose1d在 CUDA 張量上呼叫的
torch.nn.ConvTranspose2d在 CUDA 張量上呼叫的
torch.nn.ConvTranspose3d嘗試區分 CUDA 張量時呼叫的
torch.nn.ReplicationPad1d嘗試區分 CUDA 張量時呼叫的
torch.nn.ReplicationPad2d嘗試區分 CUDA 張量時呼叫的
torch.nn.ReplicationPad3d在稀疏-稠密 CUDA 張量上呼叫的
torch.bmm()嘗試區分 CPU 張量且索引是張量列表時呼叫的
torch.Tensor.__getitem__()使用
accumulate=False時的torch.Tensor.index_put()在 CPU 張量上呼叫
accumulate=True時的torch.Tensor.index_put()在 CPU 張量上呼叫
accumulate=True時的torch.Tensor.put_()在 CUDA 張量上呼叫的
torch.Tensor.scatter_add_()在需要梯度的 CUDA 張量上呼叫的
torch.gather()在 CUDA 張量上呼叫的
torch.index_add()嘗試區分 CUDA 張量時呼叫的
torch.index_select()嘗試區分 CUDA 張量時呼叫的
torch.repeat_interleave()在 CPU 或 CUDA 張量上呼叫的
torch.Tensor.index_copy()當 src 型別為 Tensor 且在 CUDA 張量上呼叫時,
torch.Tensor.scatter()當
reduce='sum'或reduce='mean'且在 CUDA 張量上呼叫時,torch.Tensor.scatter_reduce()
當
mode=True時,以下通常是非確定性的操作將丟擲RuntimeError:嘗試區分 CUDA 張量時呼叫的
torch.nn.AvgPool3d嘗試區分 CUDA 張量時呼叫的
torch.nn.AdaptiveAvgPool2d嘗試區分 CUDA 張量時呼叫的
torch.nn.AdaptiveAvgPool3d嘗試區分 CUDA 張量時呼叫的
torch.nn.MaxPool3d嘗試區分 CUDA 張量時呼叫的
torch.nn.AdaptiveMaxPool2d嘗試區分 CUDA 張量時呼叫的
torch.nn.FractionalMaxPool2d嘗試區分 CUDA 張量時呼叫的
torch.nn.FractionalMaxPool3d嘗試區分 CUDA 張量且使用以下模式之一時呼叫的
torch.nn.functional.interpolate():linearbilinearbicubictrilinear
嘗試區分 CUDA 張量時呼叫的
torch.nn.ReflectionPad1d嘗試區分 CUDA 張量時呼叫的
torch.nn.ReflectionPad2d嘗試區分 CUDA 張量時呼叫的
torch.nn.ReflectionPad3d在 CUDA 張量上呼叫的
torch.nn.NLLLoss嘗試區分 CUDA 張量時呼叫的
torch.nn.CTCLoss當
mode='max'且嘗試區分 CUDA 張量時呼叫的torch.nn.EmbeddingBag當
accumulate=False時的torch.Tensor.put_()當
accumulate=True且在 CUDA 張量上呼叫時,torch.Tensor.put_()在 CUDA 張量上呼叫的
torch.histc()當在 CUDA 張量上呼叫且提供了
weights張量時,torch.bincount()在 CUDA 張量上呼叫的
torch.kthvalue()當輸出 indices 且在 CUDA 張量上呼叫時,
torch.median()嘗試區分 CUDA 張量時呼叫的
torch.nn.functional.grid_sample()當在 CUDA 張量上呼叫且 dtype 是浮點數或複數時,
torch.cumsum()當
reduce='prod'且在 CUDA 張量上呼叫時,torch.Tensor.scatter_reduce()使用量化張量時呼叫的
torch.Tensor.resize_()
此外,當啟用此設定並啟用
torch.utils.deterministic.fill_uninitialized_memory時,一些操作會填充未初始化的記憶體。有關更多資訊,請參閱該屬性的文件。一系列 CUDA 操作在 CUDA 版本為 10.2 或更高版本時是非確定性的,除非設定了環境變數
CUBLAS_WORKSPACE_CONFIG=:4096:8或CUBLAS_WORKSPACE_CONFIG=:16:8。有關更多詳細資訊,請參閱 CUDA 文件:https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility 如果未設定其中一個環境變數配置,則在 CUDA 張量上呼叫這些操作時將引發RuntimeError。請注意,確定性操作通常比非確定性操作效能更差。
注意
此標誌不會檢測或阻止由於將就地操作應用於具有內部記憶體重疊的張量,或將此類張量作為操作的
out引數而導致的行為。在這些情況下,多個對同一記憶體位置的不同資料寫入可能會發生,並且寫入順序不確定。- 引數
mode (
bool) – 如果為 True,則使潛在的非確定性操作切換到確定性演算法或丟擲執行時錯誤。如果為 False,則允許非確定性操作。- 關鍵字引數
warn_only (
bool, optional) – 如果為 True,則沒有確定性實現的で操作將丟擲警告而不是錯誤。預設為False。
示例
>>> torch.use_deterministic_algorithms(True) # Forward mode nondeterministic error >>> torch.randn(10, device='cuda').kthvalue(1) ... RuntimeError: kthvalue CUDA does not have a deterministic implementation... # Backward mode nondeterministic error >>> torch.nn.AvgPool3d(1)(torch.randn(3, 4, 5, 6, requires_grad=True).cuda()).sum().backward() ... RuntimeError: avg_pool3d_backward_cuda does not have a deterministic implementation...