Skip to content

Commit 80902f9

Browse files
rascaniclaude
andauthored
Add DecomposeChannelsLastPass for the channels_last CPU fallback (pytorch#20615)
### Summary Provides the channels-first CPU fallback for the dialect: an ExportPass that rewrites channels_last.{convolution,avg_pool2d,permute_copy} into permute_copy + edge.aten op + permute_copy, running on the existing portable kernels with memory-planned intermediates. Intended for the graph portion that no backend claimed; backends instead replace the channels_last ops with their own channels-last kernels. Part of pytorch#20093. ### Test plan ``` pytest backends/transforms/test/test_decompose_channels_last_pass.py ``` Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ae64df0 commit 80902f9

3 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Importing registers the channels_last dialect (and its edge overloads).
8+
import executorch.backends.transforms.channels_last_ops # noqa: F401
9+
from executorch.exir.dialects._ops import ops as exir_ops
10+
from executorch.exir.pass_base import ExportPass
11+
12+
# NCHW <-> NHWC permutations (true, data-moving permutes).
13+
_NHWC_TO_NCHW = [0, 3, 1, 2]
14+
_NCHW_TO_NHWC = [0, 2, 3, 1]
15+
16+
# channels_last op -> the channels-first aten op it wraps.
17+
_DECOMPOSITIONS = {
18+
exir_ops.edge.channels_last.convolution.default: exir_ops.edge.aten.convolution.default,
19+
exir_ops.edge.channels_last.avg_pool2d.default: exir_ops.edge.aten.avg_pool2d.default,
20+
}
21+
22+
23+
class DecomposeChannelsLastPass(ExportPass):
24+
"""Decompose channels_last dialect ops into permute + aten op + permute.
25+
26+
This is the channels-first CPU fallback: a channels_last op operating on
27+
(N, H, W, C) data is rewritten to permute the activation to (N, C, H, W),
28+
run the standard aten op, and permute the result back. Intended to run on
29+
the portion of the graph that no backend claimed; backends instead replace
30+
the channels_last ops with their own channels-last kernels.
31+
"""
32+
33+
def call_operator(self, op, args, kwargs, meta):
34+
aten_op = _DECOMPOSITIONS.get(op)
35+
if aten_op is not None:
36+
nchw_in = super().call_operator(
37+
exir_ops.edge.aten.permute_copy.default,
38+
(args[0], _NHWC_TO_NCHW),
39+
{},
40+
meta,
41+
)
42+
nchw_out = super().call_operator(
43+
aten_op, (nchw_in, *args[1:]), kwargs, meta
44+
)
45+
return super().call_operator(
46+
exir_ops.edge.aten.permute_copy.default,
47+
(nchw_out, _NCHW_TO_NHWC),
48+
{},
49+
meta,
50+
)
51+
if op == exir_ops.edge.channels_last.permute_copy.default:
52+
return super().call_operator(
53+
exir_ops.edge.aten.permute_copy.default, args, kwargs, meta
54+
)
55+
return super().call_operator(op, args, kwargs, meta)

‎backends/transforms/targets.bzl‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,21 @@ def define_common_targets():
204204
],
205205
)
206206

207+
runtime.python_library(
208+
name = "decompose_channels_last_pass",
209+
srcs = [
210+
"decompose_channels_last_pass.py",
211+
],
212+
visibility = [
213+
"//executorch/backends/...",
214+
],
215+
deps = [
216+
"//caffe2:torch",
217+
":channels_last_ops",
218+
"//executorch/exir:pass_base",
219+
],
220+
)
221+
207222
runtime.python_library(
208223
name = "rank_0_to_rank_1",
209224
srcs = [
@@ -295,6 +310,21 @@ def define_common_targets():
295310
],
296311
)
297312

313+
runtime.python_test(
314+
name = "test_decompose_channels_last_pass",
315+
srcs = [
316+
"test/test_decompose_channels_last_pass.py",
317+
],
318+
deps = [
319+
"//caffe2:torch",
320+
":channels_last_ops",
321+
":decompose_channels_last_pass",
322+
"//executorch/exir:lib",
323+
"//executorch/extension/pybindings:portable_lib",
324+
"fbsource//third-party/pypi/pytest:pytest",
325+
],
326+
)
327+
298328
runtime.python_test(
299329
name = "test_rank_0_to_rank_1",
300330
srcs = [
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Importing registers the channels_last dialect.
8+
import executorch.backends.transforms.channels_last_ops # noqa: F401
9+
import pytest
10+
import torch
11+
from executorch.backends.transforms.decompose_channels_last_pass import (
12+
DecomposeChannelsLastPass,
13+
)
14+
from executorch.exir import to_edge
15+
from executorch.exir.dialects._ops import ops as exir_ops
16+
from executorch.extension.pybindings.portable_lib import (
17+
_load_for_executorch_from_buffer,
18+
)
19+
20+
21+
def _count(graph_module: torch.fx.GraphModule, target) -> int:
22+
return sum(
23+
1
24+
for n in graph_module.graph.nodes
25+
if n.op == "call_function" and n.target == target
26+
)
27+
28+
29+
class _ConvModule(torch.nn.Module):
30+
def forward(self, x, w, b):
31+
return torch.ops.channels_last.convolution(
32+
x, w, b, [1, 1], [0, 0], [1, 1], False, [0, 0], 1
33+
)
34+
35+
36+
class _DepthwiseConvModule(torch.nn.Module):
37+
# Depthwise (groups == C) and no bias.
38+
def forward(self, x, w):
39+
return torch.ops.channels_last.convolution(
40+
x, w, None, [1, 1], [0, 0], [1, 1], False, [0, 0], 3
41+
)
42+
43+
44+
class _AvgPoolModule(torch.nn.Module):
45+
def forward(self, x):
46+
return torch.ops.channels_last.avg_pool2d(
47+
x, [2, 2], [2, 2], [0, 0], False, True, None
48+
)
49+
50+
51+
class _PermuteModule(torch.nn.Module):
52+
def forward(self, x):
53+
return torch.ops.channels_last.permute_copy(x, [0, 3, 1, 2])
54+
55+
56+
_CASES = [
57+
(
58+
_ConvModule(),
59+
(torch.randn(2, 8, 8, 3), torch.randn(4, 3, 3, 3), torch.randn(4)),
60+
exir_ops.edge.channels_last.convolution.default,
61+
exir_ops.edge.aten.convolution.default,
62+
2,
63+
),
64+
(
65+
_DepthwiseConvModule(),
66+
(torch.randn(1, 8, 8, 3), torch.randn(3, 1, 3, 3)),
67+
exir_ops.edge.channels_last.convolution.default,
68+
exir_ops.edge.aten.convolution.default,
69+
2,
70+
),
71+
(
72+
_AvgPoolModule(),
73+
(torch.randn(2, 8, 8, 3),),
74+
exir_ops.edge.channels_last.avg_pool2d.default,
75+
exir_ops.edge.aten.avg_pool2d.default,
76+
2,
77+
),
78+
(
79+
_PermuteModule(),
80+
(torch.randn(2, 8, 8, 3),),
81+
exir_ops.edge.channels_last.permute_copy.default,
82+
exir_ops.edge.aten.permute_copy.default,
83+
1,
84+
),
85+
]
86+
87+
88+
@pytest.mark.parametrize("module,args,cl_op,aten_op,n_permutes", _CASES)
89+
def test_decomposes_to_aten_and_permutes(module, args, cl_op, aten_op, n_permutes):
90+
ep = torch.export.export(module.eval(), args, strict=True)
91+
edge = to_edge(ep).transform([DecomposeChannelsLastPass()])
92+
gm = edge.exported_program().graph_module
93+
94+
assert _count(gm, cl_op) == 0
95+
assert _count(gm, aten_op) == 1
96+
assert _count(gm, exir_ops.edge.aten.permute_copy.default) == n_permutes
97+
98+
99+
@pytest.mark.parametrize("module,args,cl_op,aten_op,n_permutes", _CASES)
100+
def test_decomposed_program_runs_and_matches_eager(
101+
module, args, cl_op, aten_op, n_permutes
102+
):
103+
eager = module(*args)
104+
105+
ep = torch.export.export(module.eval(), args, strict=True)
106+
et = to_edge(ep).transform([DecomposeChannelsLastPass()]).to_executorch()
107+
method = _load_for_executorch_from_buffer(et.buffer)
108+
actual = method.forward(list(args))[0]
109+
110+
torch.testing.assert_close(actual, eager, atol=1e-4, rtol=1e-4)

0 commit comments

Comments
 (0)