torch.escape-hatch
assume_constant_result
Original source code:
# mypy: allow-untyped-defs
import torch
import torch._dynamo
Original source code:
# mypy: allow-untyped-defs
import torch
import torch._dynamo
Result:
ExportedProgram:
class GraphModule(torch.nn.Module):
def forward(self, x: "f32[3, 2]", y: "i64[]"):
slice_1: "f32[3, 2]" = torch.ops.aten.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: {}
Original source code:
# mypy: allow-untyped-defs
import torch
class ConstrainAsSizeExample(torch.nn.Module):
"""
If the value is not known at tracing time, you can provide hint so that we
can trace further. Please look at torch._check APIs.
"""
def forward(self, x):
a = x.item()
torch._check(a >= 0)
torch._check(a <= 5)
return torch.zeros((a, 5))
example_args = (torch.tensor(4),)
tags = {
"torch.dynamic-value",
"torch.escape-hatch",
}
model = ConstrainAsSizeExample()
torch.export.export(model, example_args)
Result:
ExportedProgram:
class GraphModule(torch.nn.Module):
def forward(self, x: "i64[]"):
item: "Sym(u0)" = torch.ops.aten.item.default(x);
Original source code:
# mypy: allow-untyped-defs
import torch
class ConstrainAsValueExample(torch.nn.Module):
"""
If the value is not known at tracing time, you can provide hint so that we
can trace further. Please look at torch._check API.
"""
def forward(self, x, y):
a = x.item()
torch._check(a >= 0)
torch._check(a <= 5)
if a < 6:
return y.sin()
return y.cos()
example_args = (torch.tensor(4), torch.randn(5, 5))
tags = {
"torch.dynamic-value",
"torch.escape-hatch",
}
model = ConstrainAsValueExample()
torch.export.export(model, example_args)
Result:
ExportedProgram:
class GraphModule(torch.nn.Module):
def forward(self, x: "i64[]", y: "f32[5, 5]"):
item: "Sym(u0)" = torch.ops.aten.item.