Clones the current state of the generator and returns a new generator pointing to this cloned state.
This method is beneficial for preserving a particular state of a generator to restore at a later point.
Retrieves the current state of the generator in a manner that is safe for graph capture.
This method is crucial for ensuring that the generator’s state can be captured in the CUDA graph.
Returns:
A Generator point to the current state of the generator
Sets the state of the generator to the specified state in a manner that is safe for use in graph capture.
This method is crucial for ensuring that the generator’s state can be captured in the CUDA graph.
Parameters:
state (torch.Generator) – A Generator point to the new state for the generator, typically obtained from graphsafe_get_state.
Sets the seed for generating random numbers. Returns a torch.Generator object. Any 32-bit integer is a valid seed.
Parameters:
seed (int) – The desired seed. Value must be within the inclusive range
[-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError
is raised. Negative inputs are remapped to positive values with the formula
0xffff_ffff_ffff_ffff + seed.
Reserves increment values from this generator’s Philox4x32-10 stream and
returns the reserved position as (seed,offset,intragraph_offset), three
1-element int64 tensors. This is the same reservation protocol the
built-in CUDA random kernels use (PhiloxCudaState in C++), so kernels
built on it draw from the same stream as, and compose with, the built-in
random operations. Only Philox-based generators (currently CUDA) support this
method.
What the reservation grants. With effective_offset=(uint64(offset)+uint64(intragraph_offset))%2**64 (both operands
reinterpreted back from int64 to uint64 first; see below), the caller owns
the Philox counter values effective_offset/4 through
effective_offset/4+ceil(increment/4)-1 (the counter advances once
per 4 generated values), at the fixed seed, for every subsequence.
Following the built-in kernels’ convention of one subsequence per thread
(curand_init(seed,subsequence,effective_offset,...), where the counter
occupies counter.x/y and the subsequence ), a thread may
generate up to values rounded up to a multiple of 4. The
generator’s offset advances by that rounded amount, so must be
at least the number of 32-bit values any single thread of the consuming
kernel generates.
int64 reinterpretation. Seed and offset are unsigned 64-bit quantities
returned bit-exactly in int64 tensors (PyTorch tensors have no uint64
arithmetic support); values at or above 2**63 appear negative. Kernels
should reinterpret the bits back to uint64 (e.g. load as int64 and bitcast);
for host-side inspection use .item()&(2**64-1). The offset wraps
modulo 2**64 on advancement.
Graph capture. The two return modes mirror the C++ PhiloxCudaState
protocol. Outside capture (HostState), seed and offset are CPU
tensors holding the current values; .item() is cheap and does not
synchronize, so callers pass the values as scalar kernel arguments. During
capture (DevState, under torch.accelerator.Graph or
torch.cuda.CUDAGraph), seed and are CUDA tensors
aliasing generator state that each replay refills with the values current at
replay time, so kernels must load them from device memory at run time.
is always a CPU tensor: 0 outside capture, and this
reservation’s position within the graph during capture. Callers branch on
capture state (e.g. , or the
device of the returned tensors), exactly like the built-in kernels branch on
via .
Parameters:
increment (int) – Number of Philox outputs to reserve. Must be
non-negative and at most 2**64-1; the offset it advances
wraps modulo 2**64 (under graph capture, where the intragraph
offset starts at 0, the total reserved within one graph must stay
below 2**64).
Returns:
(seed,offset,intragraph_offset),
each a 1-element int64 tensor. seed and hold the
generator’s seed and the reserved stream position as uint64 bits (CPU
tensors outside capture, CUDA tensors aliasing generator state during
capture); is always a CPU tensor holding this
reservation’s position within the capturing graph (0 outside capture).