Skip to content

[inductor] skip bmm when converting channel last #159459

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions torch/_inductor/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,12 +800,17 @@ def find_nodes_prefer_channels_last(self) -> OrderedSet[Node]:
With rule 2, we makes sure all the tensors in the chain uses channels last layout. So both copies
can be saved.
"""
last_conv = None
nodes_cannot_propagate = [torch.ops.aten.bmm.default]
Copy link
Contributor

Choose a reason for hiding this comment

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

How does the perf compare if we do a copy inside of aten.bmm.default if the dense dimension dim 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current behavior in aten bmm is to add a clone to do the transpose for both cuda and xpu. I don't know where I can add something like require_stride_order to make bmm dim[-1] dense, but this seems to add another transpose.

output_set = OrderedSet[Node]()
for n in reversed(self.module.graph.nodes): # type: ignore[arg-type, union-attr]
if n.target == torch.ops.aten.convolution.default:
output_set.add(n)
if last_conv is None:
last_conv = n
continue
if n.target in nodes_cannot_propagate:
continue

for user in n.users:
if user in output_set:
output_set.add(n)
Expand All @@ -826,8 +831,14 @@ def find_nodes_prefer_channels_last(self) -> OrderedSet[Node]:
# - res2net50_14w_8s
# - sebotnet33ts_256
for n in self.module.graph.nodes: # type: ignore[union-attr]
# layout propagation ends at last conv node, which will benefit vison transformers.
if last_conv is not None and n == last_conv:
break
if n in output_set:
output_set.update(n.users)
for user in n.users:
if user.target in nodes_cannot_propagate:
continue
output_set.add(user)

return output_set

Expand Down
Loading