Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions exir/backend/test/test_backends_lifted.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,17 +1012,19 @@ def false_fn(x, y):
x = x - y
return x

def f(x, y):
x = x + y
x = control_flow.cond(x[0][0] == 1, true_fn, false_fn, [x, y])
x = x - y
return x
class Module(torch.nn.Module):
def forward(self, x, y):
x = x + y
x = control_flow.cond(x[0][0] == 1, true_fn, false_fn, [x, y])
x = x - y
return x

f = Module()
inputs = (torch.ones(2, 2), torch.ones(2, 2))
orig_res = f(*inputs)
orig = to_edge(
export(
torch.export.WrapperModule(f),
f,
inputs,
)
)
Expand Down Expand Up @@ -1066,15 +1068,17 @@ def map_fn(x, y):
x = x + y
return x

def f(xs, y):
y = torch.mm(y, y)
return control_flow.map(map_fn, xs, y)
class Module(torch.nn.Module):
def forward(self, xs, y):
y = torch.mm(y, y)
return control_flow.map(map_fn, xs, y)

f = Module()
inputs = (torch.ones(2, 2), torch.ones(2, 2))
orig_res = f(*inputs)
orig = to_edge(
export(
torch.export.WrapperModule(f),
f,
inputs,
)
)
Expand Down Expand Up @@ -1132,9 +1136,10 @@ def map_fn(x, pred1, pred2, y):
x = x + y
return x.sin()

def f(xs, pred1, pred2, y):
y = torch.mm(y, y)
return control_flow.map(map_fn, xs, pred1, pred2, y)
class Module(torch.nn.Module):
def forward(self, xs, pred1, pred2, y):
y = torch.mm(y, y)
return control_flow.map(map_fn, xs, pred1, pred2, y)

inputs = (
torch.ones(2, 2),
Expand All @@ -1143,10 +1148,11 @@ def f(xs, pred1, pred2, y):
torch.ones(2, 2),
)

f = Module()
orig_res = f(*inputs)
orig = to_edge(
export(
torch.export.WrapperModule(f),
f,
inputs,
)
)
Expand Down Expand Up @@ -1205,12 +1211,14 @@ def f(xs, pred1, pred2, y):
)

def test_list_input(self):
def f(x: List[torch.Tensor]):
y = x[0] + x[1]
return y
class Module(torch.nn.Module):
def forward(self, x: List[torch.Tensor]):
y = x[0] + x[1]
return y

f = Module()
inputs = ([torch.randn(2, 2), torch.randn(2, 2)],)
edge_prog = to_edge(export(torch.export.WrapperModule(f), inputs))
edge_prog = to_edge(export(f, inputs))
lowered_gm = to_backend(
BackendWithCompilerDemo.__name__, edge_prog.exported_program(), []
)
Expand All @@ -1227,12 +1235,14 @@ def forward(self, x: List[torch.Tensor]):
gm.exported_program().module()(*inputs)

def test_dict_input(self):
def f(x: Dict[str, torch.Tensor]):
y = x["a"] + x["b"]
return y
class Module(torch.nn.Module):
def forward(self, x: Dict[str, torch.Tensor]):
y = x["a"] + x["b"]
return y

f = Module()
inputs = ({"a": torch.randn(2, 2), "b": torch.randn(2, 2)},)
edge_prog = to_edge(export(torch.export.WrapperModule(f), inputs))
edge_prog = to_edge(export(f, inputs))
lowered_gm = to_backend(
BackendWithCompilerDemo.__name__, edge_prog.exported_program(), []
)
Expand Down
12 changes: 11 additions & 1 deletion exir/program/test/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@

from torch.library import impl, Library


class WrapperModule(torch.nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn

def forward(self, *args, **kwargs):
return self.fn(*args, **kwargs)


lib = Library("test_op", "DEF")

# Fake a operator for testing.
Expand Down Expand Up @@ -374,7 +384,7 @@ def _test_edge_dialect_verifier(self, callable, validate_ir=True):
two,
)
if not isinstance(callable, torch.nn.Module):
callable = torch.export.WrapperModule(callable)
callable = WrapperModule(callable)

exported_foo = export(callable, inputs)
_ = to_edge(exported_foo, compile_config=edge_compile_config)
Expand Down