本文簡要介紹python語言中 torch.use_deterministic_algorithms 的用法。
用法:
torch.use_deterministic_algorithms(mode)mode(
bool) -如果為 True,則使潛在的非確定性操作切換到確定性算法或引發運行時錯誤。如果為 False,則允許非確定性操作。設置PyTorch操作是否必須使用“deterministic”算法。也就是說,給定相同的輸入並且在相同的軟件和硬件上運行時,算法總是產生相同的輸出。啟用後,操作將在可用時使用確定性算法,如果隻有非確定性算法可用,則在調用時將拋出
RuntimeError。以下 normally-nondeterministic 操作將在
mode=True時確定性地執行: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.bmm()在 sparse-dense CUDA 張量上調用時torch.Tensor.__getitem__()嘗試區分 CPU 張量並且索引是張量列表時torch.Tensor.index_put()與accumulate=Falsetorch.Tensor.index_put()和accumulate=True在 CPU 張量上調用時torch.Tensor.put_()和accumulate=True在 CPU 張量上調用時torch.Tensor.scatter_add_()當input維度為一並且在 CUDA 張量上調用torch.gather()當input維度為一並且在需要 grad 的 CUDA 張量上調用torch.index_add()在 CUDA 張量上調用時torch.index_select()嘗試區分 CUDA 張量時torch.repeat_interleave()嘗試區分 CUDA 張量時torch.Tensor.index_copy()在 CPU 或 CUDA 張量上調用時
以下 normally-nondeterministic 操作將在
mode=True時拋出RuntimeError: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()嘗試區分 CUDA 張量並使用以下模式之一時:linearbilinearbicubictrilinear
torch.nn.ReflectionPad1d嘗試區分 CUDA 張量時torch.nn.ReflectionPad2d嘗試區分 CUDA 張量時torch.nn.ReflectionPad3d嘗試區分 CUDA 張量時torch.nn.ReplicationPad1d嘗試區分 CUDA 張量時torch.nn.ReplicationPad2d嘗試區分 CUDA 張量時torch.nn.ReplicationPad3d嘗試區分 CUDA 張量時torch.nn.NLLLoss在 CUDA 張量上調用時torch.nn.CTCLoss嘗試區分 CUDA 張量時torch.nn.EmbeddingBag當mode='max'嘗試微分 CUDA 張量時torch.Tensor.scatter_add_()當input維度大於 1 並在 CUDA 張量上調用torch.gather()當input維度大於 1 並調用需要 grad 的 CUDA 張量torch.Tensor.put_()當accumulate=Falsetorch.Tensor.put_()當accumulate=True並調用 CUDA 張量torch.histc()在 CUDA 張量上調用時torch.bincount()在 CUDA 張量上調用時torch.kthvalue()調用 CUDA 張量torch.median()在 CUDA 張量上調用時帶有索引輸出torch.nn.functional.grid_sample()嘗試區分 CUDA 張量時
如果 CUDA 版本為 10.2 或更高版本,則少數 CUDA 操作是不確定的,除非設置了環境變量
CUBLAS_WORKSPACE_CONFIG=:4096:8或CUBLAS_WORKSPACE_CONFIG=:16:8。有關更多詳細信息,請參閱 CUDA 文檔: https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility 如果未設置這些環境變量配置之一,則在使用 CUDA 張量調用時,將從這些操作中引發RuntimeError:請注意,確定性操作的性能往往比非確定性操作差。
注意
此標誌不會檢測或防止由於在具有內部內存重疊的張量上調用就地操作或通過將此類張量作為操作的
out參數而引起的非確定性行為。在這些情況下,不同數據的多次寫入可能針對單個內存位置,並且無法保證寫入的順序。例子:
>>> torch.use_deterministic_algorithms(True) # Forward mode nondeterministic error >>> torch.randn(10).index_copy(0, torch.tensor([0]), torch.randn(1)) ... RuntimeError: index_copy does not have a deterministic implementation... # Backward mode nondeterministic error >>> torch.randn(10, requires_grad=True, device='cuda').index_select(0, torch.tensor([0], device='cuda')).backward() ... RuntimeError: index_add_cuda_ does not have a deterministic implementation...
參數:
相關用法
- Python PyTorch unicode_csv_reader用法及代碼示例
- Python PyTorch unused用法及代碼示例
- Python PyTorch unique_consecutive用法及代碼示例
- Python PyTorch unbind用法及代碼示例
- Python PyTorch unique用法及代碼示例
- Python PyTorch uniform_partition用法及代碼示例
- Python PyTorch uniform_用法及代碼示例
- Python PyTorch unsqueeze用法及代碼示例
- Python PyTorch frexp用法及代碼示例
- Python PyTorch jvp用法及代碼示例
- Python PyTorch cholesky用法及代碼示例
- Python PyTorch vdot用法及代碼示例
- Python PyTorch ELU用法及代碼示例
- Python PyTorch ScaledDotProduct.__init__用法及代碼示例
- Python PyTorch gumbel_softmax用法及代碼示例
- Python PyTorch get_tokenizer用法及代碼示例
- Python PyTorch saved_tensors_hooks用法及代碼示例
- Python PyTorch positive用法及代碼示例
- Python PyTorch renorm用法及代碼示例
- Python PyTorch AvgPool2d用法及代碼示例
- Python PyTorch MaxUnpool3d用法及代碼示例
- Python PyTorch Bernoulli用法及代碼示例
- Python PyTorch Tensor.unflatten用法及代碼示例
- Python PyTorch Sigmoid用法及代碼示例
- Python PyTorch Tensor.register_hook用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.use_deterministic_algorithms。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
