ExportDB is a centralized dataset of supported and unsupported export cases.
It is targeted towards users who want to understand specifically what types of
code are supported, the subtleties of export, and how to modify their existing
code to be compatible with export. Note that this is not an exhaustive set of
everything that is supported by exportdb, but it covers the
most common and confusing use cases that users will run into.
If you have a feature that you think needs a stronger guarantee from us to
support in export please create an issue in the pytorch/pytorch repo with a module:export tag.
# mypy: allow-untyped-defsimporttorchimporttorch._dynamoastorchdynamoclassAssumeConstantResult(torch.nn.Module):""" Applying `assume_constant_result` decorator to burn make non-traceable code as constant. """@torchdynamo.assume_constant_resultdefget_item(self,y):returny.int().item()defforward(self,x,y):returnx[:self.get_item(y)]example_args=(torch.randn(3,2),torch.tensor(4))tags={"torch.escape-hatch"}model=AssumeConstantResult()torch.export.export(model,example_args)
# mypy: allow-untyped-defsimporttorchclassConstrainAsSizeExample(torch.nn.Module):""" If the value is not known at tracing time, you can provide hint so that we
constrain_as_value_example
Original source code:
# mypy: allow-untyped-defsimporttorchclassConstrainAsValueExample(torch.nn.Module):""" If the value is not known at tracing time, you can provide hint so that we
# mypy: allow-untyped-defsimporttorchclassDictionary(torch.nn.Module):""" Dictionary structures are inlined and flattened along tracing. """def
dynamic_shape_assert
Original source code:
# mypy: allow-untyped-defsimporttorchclassDynamicShapeAssert(torch.nn.Module):""" A basic usage of python assertion. """def
dynamic_shape_constructor
Original source code:
# mypy: allow-untyped-defsimporttorchclassDynamicShapeConstructor(torch.nn.Module):""" Tensor constructors should be captured with dynamic shape inputs rather than being baked in with static shape. """
dynamic_shape_if_guard
Original source code:
# mypy: allow-untyped-defsimporttorchclassDynamicShapeIfGuard(torch.nn.Module):""" `if` statement with backed dynamic shape predicate will be specialized into
# mypy: allow-untyped-defsimporttorchclassDynamicShapeSlicing(torch.nn.Module):""" Slices with dynamic shape arguments should be captured into the graph rather than being baked in. """
dynamic_shape_view
Original source code:
# mypy: allow-untyped-defsimporttorchclassDynamicShapeView(torch.nn.Module):""" Dynamic shapes should be propagated to view arguments instead of being baked into the exported graph. """
fn_with_kwargs
Original source code:
# mypy: allow-untyped-defsimporttorchclassFnWithKwargs(torch.nn.Module):""" Keyword arguments are not supported at the moment. """def
# mypy: allow-untyped-defsimporttorchclassListUnpack(torch.nn.Module):""" Lists are treated as static construct, therefore unpacking should be
model_attr_mutation
Original source code:
# mypy: allow-untyped-defsimporttorchclassModelAttrMutation(torch.nn.Module):""" Attribute mutation raises a warning. Covered in the test_export.py test_detect_leak_strict test. """def
nested_function
Original source code:
# mypy: allow-untyped-defsimporttorchclassNestedFunction(torch.nn.Module):""" Nested functions are traced through. Side effects on global captures are not supported though. """
null_context_manager
Original source code:
# mypy: allow-untyped-defsimportcontextlibimporttorchclassNullContextManager(torch.nn.Module):""" Null context manager in Python will be traced out.
# mypy: allow-untyped-defsimporttorchclassStaticForLoop(torch.nn.Module):""" A for loop with constant number of iterations should be unrolled in the exported graph. """def
static_if
Original source code:
# mypy: allow-untyped-defsimporttorchclassStaticIf(torch.nn.Module):""" `if` statement with static predicate value should be traced through with the taken branch. """def
tensor_setattr
Original source code:
# mypy: allow-untyped-defsimporttorchclassTensorSetattr(torch.nn.Module):""" setattr() call onto tensors is not supported. """def
# mypy: allow-untyped-defsimporttorchclassUserInputMutation(torch.nn.Module):""" Directly mutate user input in forward """def
Not Supported Yet
dynamic_shape_round
Original source code:
# mypy: allow-untyped-defsimporttorchfromtorch._export.db.caseimportSupportLevelfromtorch.exportimportDimclassDynamicShapeRound(torch.nn.Module):""" Calling round on dynamic shapes is not supported. """defforward(self,x):returnx[:round(x.shape[0]/2)]x=torch.randn(3,2)dim0_x=Dim("dim0_x")example_args=(x,)tags={"torch.dynamic-shape","python.builtin"}support_level=SupportLevel.NOT_SUPPORTED_YETdynamic_shapes={"x":{0:dim0_x}}model=DynamicShapeRound()torch.export.export(model,example_args,dynamic_shapes=dynamic_shapes)
Result:
RuntimeError: Constraints violated (dim0_x)! For more information, run with TORCH_LOGS="+dynamic".
slice
.
Tensor
(
x
,
0
,
0
,
4
);
x
=
None
return
(
slice_1
,)
Graph
signature
:
# inputs
x
:
USER_INPUT
y
:
USER_INPUT
# outputs
slice_1
:
USER_OUTPUT
Range
constraints
:
{}
):
return
x
.
clone
()
@staticmethod
# pyrefly: ignore [bad-override]
def
backward
(
ctx
,
grad_output
):
return
grad_output
+
1
class
AutogradFunction
(
torch
.
nn
.
Module
):
"""
TorchDynamo does not keep track of backward() on autograd functions. We recommend to
NOTE: If the `pred` is tested on a dim with batch size < 2, it will be specialized.
"""
def
forward
(
self
,
x
,
y
):
def
true_fn
(
x
,
y
):
return
x
+
y
def
false_fn
(
x
,
y
):
return
x
-
y
return
torch
.
cond
(
x
.
shape
[
0
]
>
2
,
true_fn
,
false_fn
,
[
x
,
y
])
example_args
=
(
x
,
y
)
tags
=
{
"torch.cond"
,
"torch.dynamic-shape"
,
}
extra_inputs
=
(
torch
.
randn
(
2
,
2
),
torch
.
randn
(
2
))
dynamic_shapes
=
{
"x"
:
{
0
:
dim0_x
},
"y"
:
None
}
model
=
CondOperands
()
torch
.
export
.
export
(
model
,
example_args
,
dynamic_shapes
=
dynamic_shapes
)
Result:
ExportedProgram:classGraphModule(torch.nn.Module):defforward(self,x:"f32[s6, 2]",y:"f32[2]"):# No stacktrace found for following nodessym_size_int_1:"Sym(s6)"=torch.ops.aten.sym_size.int(x,0)gt:"Sym(s6 > 2)"=sym_size_int_1>2;sym_size_int_1=Nonetrue_graph_0=self.true_graph_0false_graph_0=self.false_graph_0cond=torch.ops.higher_order.cond(gt,true_graph_0,false_graph_0,(x,y));gt=true_graph_0=false_graph_0=x=y=Nonegetitem:"f32[s6, 2]"=cond[0];cond=Nonereturn(getitem,)classtrue_graph_0(torch.nn.Module):defforward(self,x:"f32[s6, 2]",y:"f32[2]"):add:"f32[s6, 2]"=torch.ops.aten.add.Tensor(x,y);x=y=Nonereturn(add,)classfalse_graph_0(torch.nn.Module):defforward(self,x:"f32[s6, 2]",y:"f32[2]"):sub:"f32[s6, 2]"=torch.ops.aten.sub.Tensor(x,y);x=y=Nonereturn(sub,)Graphsignature:# inputsx:USER_INPUTy:USER_INPUT# outputsgetitem:USER_OUTPUTRangeconstraints:{s6:VR[0,int_oo]}
.
Module
):
"""
The conditional statement (aka predicate) passed to cond() must be one of the following:
- torch.Tensor with a single element
- boolean expression
NOTE: If the `pred` is tested on a dim with batch size < 2, it will be specialized.
Returning scalar values from the graph is supported, in addition to Tensor
outputs. Symbolic shapes are captured and rank is specialized.
"""
def
__init__
(
self
)
->
None
:
super
()
.
__init__
()
def
forward
(
self
,
x
):
return
x
.
shape
[
1
]
+
1
example_args
=
(
x
,)
tags
=
{
"torch.dynamic-shape"
}
dynamic_shapes
=
{
"x"
:
{
1
:
dim1_x
}}
model
=
ScalarOutput
()
torch
.
export
.
export
(
model
,
example_args
,
dynamic_shapes
=
dynamic_shapes
)
Result:
ExportedProgram:classGraphModule(torch.nn.Module):defforward(self,x:"f32[3, s11]"):# No stacktrace found for following nodessym_size_int_1:"Sym(s11)"=torch.ops.aten.sym_size.int(x,1);x=Noneadd:"Sym(s11 + 1)"=sym_size_int_1+1;sym_size_int_1=Nonereturn(add,)Graphsignature:# inputsx:USER_INPUT# outputsadd:USER_OUTPUTRangeconstraints:{s11:VR[0,int_oo]}