torch.scan is a structured control flow operator that performs an inclusive scan with a combine function.
It is commonly used for cumulative operations like cumsum, cumprod, or more general recurrences.
It can logically be seen as implemented as follows:
Performs an inclusive scan with a combine function.
Parameters:
combine_fn (Callable) – A binary callable with type (Tensor,Tensor)->(Tensor,Tensor),
or if xs is a pytree (pytree,pytree)->(pytree,pytree).
The first input to combine_fn is the previous or initial scan carry
and the second input element to combine_fn is a slice of the input along dim.
The first output element of combine_fn is the next scan carry
and the second output of combine_fn represents a slice of the output.
This function must be pure, i.e., no lifted arguments are supported at the moment
and may not have any side effects.
() – The initial scan carry, a tensor, or nested pytree of tensors.
The is expected to have the same pytree structure as the first output element (i.e. carry)
of .
Restrictions:
The combine_fn shouldn’t have any aliasing between input-input, input-output, and output-output. E.g. return a view
or the same tensor as input is not supported. As a workaround, can clone the output to avoid aliasing.
The combine_fn shouldn’t mutate any inputs. We’ll remove the mutation restriction for inference soon. Please file an issue
if you input mutation support for training is needed.
The combine_fn’s init carry should match the next_carry in pytree structure and in tensor metadata.
Example:
defadd(x:torch.Tensor,y:torch.Tensor):next_carry=y=x+y# clone the output to avoid output-output aliasingreturnnext_carry,y.clone()i0=torch.
zeros_like
(
[
0
]
)
returnscan(combine_fn, init=init, xs=xs)
mod =ScanModule()
inp = torch.randn(5,3)
ep = torch.export.export(mod,(inp,), dynamic_shapes={"xs":{0: torch.export.Dim.DYNAMIC}})
xs (torch.Tensor or pytree with tensor leaves or None) – The input tensor, or nested pytree of tensors.
May be None when length is provided, in which case combine_fn receives None as x
each step (counter-loop mode).
Keyword Arguments:
dim (int) – the dimension to scan over, default 0.
reverse (bool) – A boolean stating if the scan should be reversed with respect to dim, default False.
length (int or None) – Optional number of scan iterations, default None.
When xs has tensor leaves, length is optional; if given it must equal
xs.shape[dim] and serves only as a consistency check (no constraint when
length is None). When xs has no leaves (None or empty pytree),
length drives the number of iterations and combine_fn receives
x=None each step. length=0 with no xs tensors is supported in
eager mode only; it is not supported under torch.compile.
Returns:
final_carry (torch.Tensor or pytree with tensor leaves),
the final carry of the scan operation with same pytree structure as init.
out (torch.Tensor or pytree with tensor leaves),
each tensor leaf is a stacked output along first dim, where each slice is the output of a scan iteration.
If the scan dimension has size 0, final_carry equals init unchanged and each output leaf has
size 0 along dim. The gradient of final_carry with respect to init is the identity
(not zero), since the body is never called and the carry passes through untouched.