Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert aten.alias to no-op #223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions tests/lowering/misc/test_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import torch
import torch_ttnn
import pytest


class AliasModule(torch.nn.Module):
def __init__(self):
super().__init__()

def forward(self, x):
return torch.ops.aten.alias.default(x)


@pytest.mark.parametrize(
"input_shape",
[(4, 4)],
)
def test_alias(device, input_shape):
m = AliasModule()
input = torch.rand(input_shape, dtype=torch.bfloat16)
result_before = m.forward(input)

option = torch_ttnn.TorchTtnnOption(device=device, gen_graphviz=True)
# The compilation is lazy, so we need to run forward once to trigger the compilation
m = torch.compile(m, backend=torch_ttnn.backend, options=option)
result_after = m.forward(input)
option._out_fx_graphs[0].print_tabular()

# Check the graph has be rewritten
nodes = list(option._out_fx_graphs[0].nodes)
# There should be no op
assert [node.op for node in nodes].count("call_function") == 0
# Check inference result
assert torch.allclose(result_before, result_after)
4 changes: 4 additions & 0 deletions torch_ttnn/passes/lowering/to_tt_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ def call_function(self, target, args, kwargs):
return self.call_function_prop_meta(ttnn.squeeze, args, kwargs)
return self.call_function_prop_meta(target, args, kwargs)

if target == torch.ops.aten.alias.default:
# alias is no-op
return args[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing discovery! Since directly returning an argument is a valid conversion, this can lead to several simplifications:

  • We can refactor manual conversions to simple conversions, e.g. aten.arange.*ttnn.arange.
  • Perhaps we can optimize out aten.clone like aten.alias.


return self.call_function_prop_meta(target, args, kwargs)


Expand Down