torch.switch is a structured control flow operator for multi-way branching. It can be used to specify
switch-case like control flow and can logically be seen as implemented as follows:
Its unique power lies in its ability to express data-dependent multi-way control flow: it lowers to a
switch operator (torch.ops.higher_order.switch), which preserves the index, all branch functions, and operands.
This enables efficient compilation and deployment of models with N-way branching based on the value or
shape of inputs or intermediate outputs.
Examples
Below is an example that uses switch to select between multiple operations based on an input index:
import torch
from torch._higher_order_ops.switch import switch
defbranch0(x: torch.Tensor):return x.cos()defbranch1(x: torch.Tensor):return x.sin()defbranch2(x: torch.Tensor):return x.tan()classBasicSwitch(torch.nn.Module):"""
A basic usage of switch with multiple branches.
"""def__init__(self):super().__init__()defforward(self, index: torch.Tensor, x: torch.Tensor)-> torch.Tensor:returnswitch(index,[branch0, branch1, branch2],(x,))
switch_mod =BasicSwitch()
We can eagerly run the model and expect the results vary based on the index:
x = torch.randn(3)
idx0 = torch.tensor(0)
idx1 = torch.tensor(1)
idx2 = torch.tensor(2)print(switch_mod(idx0, x),branch0(x))print(switch_mod(idx1, x),branch1(x))
idx2 x x
We can export the model for further transformations and deployment:
x = torch.randn(4,3)
idx = torch.tensor(1)
ep = torch.export.export(BasicSwitch(),(idx, x),
dynamic_shapes={"index":None,"x":{0: torch.export.Dim.DYNAMIC}})printep
Notice that torch.switch is lowered to torch.ops.higher_order.switch, and branch functions become
sub-graph attributes of the top level graph module.
Here is another example showcasing switch with data-dependent index:
defbranch0(x: torch.Tensor):return x *2defbranch1(x: torch.Tensor):return x +10defbranch2(x: torch.Tensor):return x **2classDataDependentSwitch(torch.nn.Module):"""
A usage of switch with data-dependent index.
"""def__init__
x torch torch
index torchx x
index branch0 branch1 branch2 x
x torch
ep torchexport
x
dynamic_shapes torchexportDYNAMIC
ep
Invariants of torch.ops.higher_order.switch
There are several useful invariants for torch.ops.higher_order.switch:
For index:
If the index is a constant (e.g. a Python int), the operator may specialize to a single branch
If the index is a tensor, it must be a single-element tensor
Out-of-range indices are clamped to [0, len(branches)-1]
For branches:
All branches must have the same input and output signature
The input and output signature will be a flattened tuple
They are torch.fx.GraphModule
Closures in original functions become explicit inputs. No closures.
No mutations on inputs or globals are allowed
Branch outputs must be tensors or possibly nested tuples/lists/dicts of tensors. Non-tensor leaves must be int or None. Diverging int values across branches are merged into a SymInt for dynamic shapes; None must match positionally across every branch.
For operands:
It will be a flat tuple of tensors
Nesting of torch.switch in user program becomes nested graph modules
Selects and runs one of N branch functions by index.
Equivalent to: branches[index](*operands) with index in [0,len(branches)).
Parameters:
index (Union[int, torch.Tensor]) – An int or single-element integer tensor
indicating which branch to run. Out-of-range values are clamped into
[0,len(branches)).
branches (Union[tuple[Callable, ...], list[Callable]]) – Non-empty sequence of
callables. Each must accept operands and return the same structure of outputs.
Restrictions:
Each branch must have the same signature as operands and return the same
output structure (shape, dtype, etc.). Constant int and None leaves
are also permitted in branch outputs and are merged across branches (an
unbacked SymInt is introduced when int leaves differ between branches).
Branches cannot have in-place mutations on inputs or global variables.
print
(
switch_mod
(
,
)
,
branch2
(
)
)
(
)
(
self
)
:
super
(
)
.
__init__
(
)
def
forward
(
self
,
:
.
Tensor
)
->
.
Tensor
:
# Select branch based on the sign of the sum
=
.
clamp
(
(
.
sum
(
)
>
0
)
.
long
(
)
+
(
.
sum
(
)
>
5
)
.
long
(
)
,
0
,
2
)
return
switch
(
,
[
,
,
]
,
(
,
)
)
=
.
randn
(
4
,
3
)
=
.
.
export
(
DataDependentSwitch
(
)
,
(
,
)
,
=
{
"x"
:
{
0
:
.
.
Dim
.
}
}
)
print
(
)
operands (Tuple of possibly nested dict/list/tuple of torch.Tensor) – Inputs to
the branch functions. Defaults to ().