Completely reproducible results are not guaranteed across PyTorch releases,
individual commits, or different platforms. Furthermore, results may not be
reproducible between CPU and GPU executions, even when using identical seeds.
However, there are some steps you can take to limit the number of sources of
nondeterministic behavior for a specific platform, device, and PyTorch release.
First, you can control sources of randomness that can cause multiple executions
of your application to behave differently. Second, you can configure PyTorch
to avoid using nondeterministic algorithms for some operations, so that multiple
calls to those operations, given the same inputs, will produce the same result.
Controlling sources of randomness
PyTorch random number generator
You can use torch.manual_seed() to seed the RNG for all devices (both
CPU and CUDA):
importtorchtorch.manual_seed(0)
Some PyTorch operations may use random numbers internally.
torch.svd_lowrank() does this, for instance. Consequently, calling it
multiple times back-to-back with the same input arguments may give different
results. However, as long as torch.manual_seed() is set to a constant
at the beginning of an application and all other sources of nondeterminism have
been eliminated, the same series of random numbers will be generated each time
the application is run in the same environment.
It is also possible to obtain identical results from an operation that uses
random numbers by setting torch.manual_seed() to the same value between
subsequent calls.
Python
For custom operators, you might need to set python seed as well:
importrandomrandom.seed(0)
Random number generators in other libraries
If you or any of the libraries you are using rely on NumPy, you can seed the global
NumPy RNG with:
If you are using any other libraries that use random number generators, refer to
the documentation for those libraries to see how to set consistent seeds for them.
CUDA convolution benchmarking
The cuDNN library, used by CUDA convolution operations, can be a source of nondeterminism
across multiple executions of an application. When a cuDNN convolution is called with a
new set of size parameters, an optional feature can run multiple convolution algorithms,
benchmarking them to find the fastest one. Then, the fastest algorithm will be used
consistently during the rest of the process for the corresponding set of size parameters.
Due to benchmarking noise and different hardware, the benchmark may select different
algorithms on subsequent runs, even on the same machine.
Disabling the benchmarking feature with torch.backends.cudnn.benchmark=False
causes cuDNN to deterministically select an algorithm, possibly at the cost of reduced
performance.
However, if you do not need reproducibility across multiple executions of your application,
then performance might improve if the benchmarking feature is enabled with
torch.backends.cudnn.benchmark=True.
Note that this setting is different from the torch.backends.cudnn.deterministic
setting discussed below.
Avoiding nondeterministic algorithms
torch.use_deterministic_algorithms() lets you configure PyTorch to use
deterministic algorithms instead of nondeterministic ones where available, and
to throw an error if an operation is known to be nondeterministic (and without
a deterministic alternative).
When torch.bmm() is called with sparse-dense CUDA tensors it typically uses a
nondeterministic algorithm, but when the deterministic flag is turned on, its alternate
deterministic implementation will be used:
While disabling CUDA convolution benchmarking (discussed above) ensures that
CUDA selects the same algorithm each time an application is run, that algorithm
itself may be nondeterministic, unless either
torch.use_deterministic_algorithms(True) or
torch.backends.cudnn.deterministic=True is set. The latter setting
controls only this behavior, unlike torch.use_deterministic_algorithms()
which will make other PyTorch operations behave deterministically, too.
In some versions of CUDA, RNNs and LSTM networks may have non-deterministic behavior.
See torch.nn.RNN() and torch.nn.LSTM() for details and workarounds.
Filling uninitialized memory
Operations like torch.empty() and torch.Tensor.resize_() can return
tensors with uninitialized memory that contain undefined values. Using such a
tensor as an input to another operation is invalid if determinism is required,
because the output will be nondeterministic. But there is nothing to actually
prevent such invalid code from being run. So for safety,
torch.utils.deterministic.fill_uninitialized_memory is set to True
by default, which will fill the uninitialized memory with a known value if
torch.use_deterministic_algorithms(True) is set. This will prevent the
possibility of this kind of nondeterministic behavior.
However, filling uninitialized memory is detrimental to performance. So if your
program is valid and does not use uninitialized memory as the input to an
operation, then this setting can be turned off for better performance.
DataLoader
DataLoader will reseed workers following the Randomness in multi-process data loading algorithm.
Use worker_init_fn() and generator to preserve reproducibility:
The backward pass uses non-deterministic atomic operations by default.
Setting torch.use_deterministic_algorithms(True,warn_only=False)
enables a deterministic backward implementation.
SDPBackend.EFFICIENT_ATTENTION
Deterministic
Non-deterministic
The backward pass may split work across keys (num_splits_key>1)
for performance, which is non-deterministic. Setting
torch.use_deterministic_algorithms(True,warn_only=False) forces
num_splits_key=1, making the backward pass deterministic.
SDPBackend.CUDNN_ATTENTION
Deterministic
Non-deterministic
cuDNN provides an opt-in deterministic backward for float16
starting in
cuDNN 9.18
(via
set_deterministic_algorithm),
but this has not yet been integrated into PyTorch. This backend is
disabled when
torch.use_deterministic_algorithms(True,warn_only=False) is set,
regardless of whether inputs require gradients or the call is
inside a torch.no_grad() context.
When torch.use_deterministic_algorithms(True,warn_only=True) is set,
the fused backends (Flash, Efficient, and cuDNN) emit a one-time warning but
still use their default non-deterministic code paths. To actually enforce
determinism, pass warn_only=False.
Low-precision dtypes and numerical reproducibility
Bitwise matching numerics across different SDPA backends are not
guaranteed, even for the same inputs and dtype. Each backend performs
floating-point accumulation in a different order, and because floating-point
addition is not associative, the results will differ between backends.
The Math backend by default accumulates in float32 even when given
float16 or bfloat16 inputs. The function
torch.backends.cuda.allow_fp16_bf16_reduction_math_sdp() can enable
reduced-precision accumulation for higher performance at the cost of
different numerical results.
Selecting a specific backend
Use the torch.nn.attention.sdpa_kernel() context manager to restrict
which backends SDPA may use. For example, to guarantee deterministic behavior
regardless of hardware:
importtorchfromtorch.nn.attentionimportsdpa_kernel,SDPBackendtorch.use_deterministic_algorithms(True)# Option 1: Use only the Math backend (always deterministic)withsdpa_kernel(SDPBackend.MATH):out=torch.nn.functional.scaled_dot_product_attention(q,k,v)# Option 2: Allow Flash and Efficient (deterministic with the flag above)withsdpa_kernel([SDPBackend.FLASH_ATTENTION,SDPBackend.EFFICIENT_ATTENTION]):out=torch.nn.functional.scaled_dot_product_attention(q,k,v)