torch.associative_scan is a structured control flow operator that performs an inclusive scan with an
associative combine function. It can logically be seen as implemented as follows:
Because combine_fn is required to be associative, the computation can be parallelized using a
tree-reduction algorithm rather than executed sequentially. This enables efficient GPU implementations
for operations like cumulative sums, products, or other associative accumulations.
Examples
Below is an example that uses associative_scan to compute a cumulative sum:
import torch
from torch._higher_order_ops.associative_scan import associative_scan
defadd(x: torch.Tensor, y: torch.Tensor):return x + y
xs = torch.arange(1,5, dtype=torch.float32)# [1, 2, 3, 4]
cumsum =associative_scan(add, xs, dim=0, combine_mode="generic")print(cumsum)
Here is an example computing a cumulative product:
defmul(x: torch.Tensor, y: torch.Tensor):return x * y
xs = torch.arange(1,5, dtype=torch.float32)# [1, 2, 3, 4]
cumprod =associative_scan(mul, xs, dim=0, combine_mode="generic")print(cumprod)
We can export the model with associative_scan for further transformations and deployment.
This example uses dynamic shapes to allow variable sequence length:
Notice that torch.associative_scan is lowered to torch.ops.higher_order.associative_scan, and the
combine function becomes a sub-graph attribute of the top-level graph module.
Restrictions
combine_fn must be associative: combine_fn(combine_fn(a,b),c)==combine_fn(a,combine_fn(b,c)).
combine_fn must not in-place mutate its inputs.
combine_fn must not reference variables from an outer scope (closures are not supported).
combine_fn’s output cannot alias any of the inputs.
Performs an inclusive scan with an associative combine function.
With combine_mode="pointwise", efficient execution requires runtime code
generation via torch.compile, and codegen is only supported on backends
with scan support (currently CUDA and XPU). On other devices the operator
still runs eagerly via the generic fallback.
Parameters:
combine_fn (Callable) – A binary callable with type (Tensor,Tensor)->Tensor,
or if input is a pytree (pytree,pytree)->pytree.
This function must be pure, i.e., no lifted arguments are supported at the moment,
satisfy the associative property and have no side-effects.
xs (torch.Tensor) – The input tensor, or nested pytree of tensors.
combine_mode (str) – A string indicating whether the combine_fn is pointwise or generic, default pointwise.
If combine_mode=pointwise, combine_fn must be pure and may only contain pointwise
operations; under torch.compilexs must be on a backend with scan codegen support
(CUDA or XPU), otherwise the generic fallback is used.
In all other cases combine_mode=generic should be used.
Note: combine_mode=pointwise is more efficient than combine_mode=generic.
Returns:
A pytree of the same structure and shape as xs. If the scan dimension has size 0,
the output mirrors the (empty) input unchanged. The gradient with respect to xs
is also empty (size 0 along dim), since there are no elements to differentiate through.