Skip to content

Commit dba8a83

Browse files
Samsung Exynos AI LiteCore - Lower more ops and add tests to support LLM models (pytorch#22829)
### Summary Implemented lowering for additional ops and added tests to support LLM models. * Add hardsigmoid, hardswish, hardtanh, layer_norm, maximum, prelu, pixel_unshuffle ops and testcase for supporting LLM. and add axis infomation for rms norm. * Replacing torch.randint(...).to(device) and torch.zeros(...).to(device) with creation calls that pass device=device ### Test plan pytest backends/samsung/test/ops/test_hardsigmoid.py \ backends/samsung/test/ops/test_hardswish.py \ backends/samsung/test/ops/test_hardtanh.py \ backends/samsung/test/ops/test_layer_norm.py \ backends/samsung/test/ops/test_maximum.py \ backends/samsung/test/ops/test_prelu.py\ backends/samsung/test/ops/test_pixel_unshuffle.py -v cc @SS-JIA @digantdesai @kimishpatel --------- Signed-off-by: Jiseong oh <jiseong.oh@samsung.com> Signed-off-by: Jiseong Oh <jiseong.oh@samsung.com> Signed-off-by: jiseong.oh <jiseong.oh@samsung.com> Co-authored-by: Xiongzhan Linghu <xz.linghu@samsung.com>
1 parent 11120c8 commit dba8a83

14 files changed

Lines changed: 404 additions & 12 deletions

‎backends/samsung/builders/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
op_pad,
4242
op_permute,
4343
op_pixel_shuffle,
44+
op_pixel_unshuffle,
4445
op_placeholder,
4546
op_pow,
4647
op_prelu,
@@ -106,6 +107,7 @@
106107
"op_mul",
107108
"op_permute",
108109
"op_pixel_shuffle",
110+
"op_pixel_unshuffle",
109111
"op_placeholder",
110112
"op_pow",
111113
"op_prelu",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright (c) 2025 Samsung Electronics Co. LTD
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+
from typing import cast, Dict
7+
8+
import torch
9+
from executorch.backends.samsung.builders.node_visitor import (
10+
NodeVisitor,
11+
register_node_visitor,
12+
)
13+
from executorch.backends.samsung.serialization.enn_graph_schema import EnnGraph
14+
from executorch.backends.transforms import get_shape
15+
16+
17+
@register_node_visitor
18+
class PixelUnshuffleVisitor(NodeVisitor):
19+
target = "aten.pixel_unshuffle.default"
20+
21+
def __init__(self, *args) -> None:
22+
super().__init__(*args)
23+
24+
def define_node(
25+
self,
26+
node: torch.fx.Node,
27+
enn_graph: EnnGraph,
28+
vals_to_ids: Dict[torch.Tensor, int],
29+
) -> bool:
30+
if len(get_shape(node.args[0])) != 4:
31+
return False
32+
33+
input_id = self.define_tensor(node.args[0], enn_graph, vals_to_ids)
34+
35+
downscale_factor = cast(int, node.args[1])
36+
params = {"block_size": downscale_factor, "mode": "CRD"}
37+
38+
output_id = self.define_tensor(node, enn_graph, vals_to_ids)
39+
40+
enn_graph.define_op(
41+
node.name, "SPACE_TO_DEPTH", [input_id], [output_id], params
42+
)
43+
44+
return True

‎backends/samsung/builders/op_rms_norm.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
import logging
78
from typing import cast, Dict, List
89

910
import torch
@@ -13,6 +14,7 @@
1314
)
1415
from executorch.backends.samsung.builders.utils import get_tensor
1516
from executorch.backends.samsung.serialization.enn_graph_schema import EnnGraph
17+
from executorch.backends.transforms import get_shape
1618

1719

1820
@register_node_visitor
@@ -31,6 +33,12 @@ def define_node(
3133

3234
# input2
3335
normalized_shape = cast(List[int], node.args[1])
36+
input_shape = get_shape(input)
37+
if len(normalized_shape) != 1 or normalized_shape[0] != input_shape[-1]:
38+
logging.warning(
39+
"Currently, Enn backend only supports rms norm with last input dimension."
40+
)
41+
return False
3442

3543
gamma_node = node.args[2]
3644
gamma_id = self.define_tensor(gamma_node, enn_graph, vals_to_ids)
@@ -44,6 +52,7 @@ def define_node(
4452
params["normalize_shape"] = normalized_shape
4553
params["param_num"] = 2
4654
params["epsilon"] = epsilon
55+
params["axis"] = [len(input_shape) - 1]
4756

4857
output_id = self.define_tensor(node, enn_graph, vals_to_ids)
4958

‎backends/samsung/partition/enn_partitioner.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def ops_to_not_decompose(
197197
torch.ops.aten.prelu.default,
198198
torch.ops.aten.layer_norm.default,
199199
torch.ops.aten.pixel_shuffle.default,
200+
torch.ops.aten.pixel_unshuffle.default,
200201
torch.ops.aten.hardsigmoid.default,
201202
torch.ops.aten.silu.default,
202203
torch.ops.aten.pad.default,

‎backends/samsung/test/models/test_mobilebert_qat.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ def test_mobilebert_qat_a8w8(self):
9696
example_inputs[1].size(2),
9797
example_inputs[1].size(3),
9898
)
99-
vector_input_ids = torch.randint(0, 256, size_input_ids).to(device)
99+
vector_input_ids = torch.randint(0, 256, size_input_ids, device=device)
100100
vector_attention_mask = torch.zeros(
101-
size_attention_mask, dtype=torch.float32
102-
).to(device)
101+
size_attention_mask, dtype=torch.float32, device=device
102+
)
103103
export_inputs = (
104104
vector_input_ids,
105105
vector_attention_mask,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) Samsung Electronics Co. LTD
2+
# All rights reserved
3+
#
4+
# Licensed under the BSD License (the "License"); you may not use this file
5+
# except in compliance with the License. See the license file in the root
6+
# directory of this source tree for more details.
7+
8+
import unittest
9+
10+
import torch
11+
12+
from executorch.backends.samsung.serialization.compile_options import (
13+
gen_samsung_backend_compile_spec,
14+
)
15+
from executorch.backends.samsung.test.tester import SamsungTester
16+
from executorch.backends.samsung.test.utils.utils import TestConfig
17+
18+
19+
class HardSigmoid(torch.nn.Module):
20+
def __init__(self) -> None:
21+
super().__init__()
22+
self.module = torch.nn.Hardsigmoid()
23+
24+
def forward(self, x: torch.Tensor) -> torch.Tensor:
25+
return self.module(x)
26+
27+
28+
class TestHardSigmoid(unittest.TestCase):
29+
def _test(self, module: torch.nn.Module, inputs):
30+
tester = SamsungTester(
31+
module,
32+
inputs,
33+
[gen_samsung_backend_compile_spec(TestConfig.chipset)],
34+
)
35+
(
36+
tester.export()
37+
.check_count({"torch.ops.aten.hardsigmoid.default": 1})
38+
.to_edge_transform_and_lower()
39+
.check_not(["executorch_exir_dialects_edge__ops_aten_hardsigmoid_default"])
40+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
41+
.to_executorch()
42+
.run_method_and_compare_outputs(inputs=inputs)
43+
)
44+
45+
def test_fp32_hard_sigmoid(self):
46+
inputs = (torch.randn(1, 16, 32, 32),)
47+
self._test(HardSigmoid(), inputs)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) Samsung Electronics Co. LTD
2+
# All rights reserved
3+
#
4+
# Licensed under the BSD License (the "License"); you may not use this file
5+
# except in compliance with the License. See the license file in the root
6+
# directory of this source tree for more details.
7+
8+
import unittest
9+
10+
import torch
11+
12+
from executorch.backends.samsung.serialization.compile_options import (
13+
gen_samsung_backend_compile_spec,
14+
)
15+
from executorch.backends.samsung.test.tester import SamsungTester
16+
from executorch.backends.samsung.test.utils.utils import TestConfig
17+
18+
19+
class HardSwish(torch.nn.Module):
20+
def __init__(self) -> None:
21+
super().__init__()
22+
self.module = torch.nn.Hardswish()
23+
24+
def forward(self, x: torch.Tensor) -> torch.Tensor:
25+
return self.module(x)
26+
27+
28+
class TestHardSwish(unittest.TestCase):
29+
def _test(self, module: torch.nn.Module, inputs):
30+
tester = SamsungTester(
31+
module,
32+
inputs,
33+
[gen_samsung_backend_compile_spec(TestConfig.chipset)],
34+
)
35+
(
36+
tester.export()
37+
.check_count({"torch.ops.aten.hardswish.default": 1})
38+
.to_edge_transform_and_lower()
39+
.check_not(["executorch_exir_dialects_edge__ops_aten_hardswish_default"])
40+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
41+
.to_executorch()
42+
.run_method_and_compare_outputs(inputs=inputs, atol=0.002)
43+
)
44+
45+
def test_fp32_hard_swish(self):
46+
inputs = (torch.randn(1, 16, 32, 32),)
47+
self._test(HardSwish(), inputs)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) Samsung Electronics Co. LTD
2+
# All rights reserved
3+
#
4+
# Licensed under the BSD License (the "License"); you may not use this file
5+
# except in compliance with the License. See the license file in the root
6+
# directory of this source tree for more details.
7+
8+
import unittest
9+
10+
import torch
11+
12+
from executorch.backends.samsung.serialization.compile_options import (
13+
gen_samsung_backend_compile_spec,
14+
)
15+
from executorch.backends.samsung.test.tester import SamsungTester
16+
from executorch.backends.samsung.test.utils.utils import TestConfig
17+
18+
19+
class Hardtanh(torch.nn.Module):
20+
def __init__(self, min_val, max_val) -> None:
21+
super().__init__()
22+
self.module = torch.nn.Hardtanh(min_val=min_val, max_val=max_val)
23+
24+
def forward(self, x: torch.Tensor) -> torch.Tensor:
25+
return self.module(x)
26+
27+
28+
class TestHardtanh(unittest.TestCase):
29+
def _test(self, module: torch.nn.Module, inputs):
30+
tester = SamsungTester(
31+
module,
32+
inputs,
33+
[gen_samsung_backend_compile_spec(TestConfig.chipset)],
34+
)
35+
(
36+
tester.export()
37+
.check_count({"torch.ops.aten.hardtanh.default": 1})
38+
.to_edge_transform_and_lower()
39+
.check_not(["executorch_exir_dialects_edge__ops_aten_hardtanh_default"])
40+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
41+
.to_executorch()
42+
.run_method_and_compare_outputs(inputs=inputs)
43+
)
44+
45+
def test_fp32_hardtanh(self):
46+
inputs = (torch.randn(1, 3, 16, 16),)
47+
self._test(Hardtanh(min_val=0, max_val=1), inputs)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) Samsung Electronics Co. LTD
2+
# All rights reserved
3+
#
4+
# Licensed under the BSD License (the "License"); you may not use this file
5+
# except in compliance with the License. See the license file in the root
6+
# directory of this source tree for more details.
7+
8+
import unittest
9+
10+
import torch
11+
12+
from executorch.backends.samsung.serialization.compile_options import (
13+
gen_samsung_backend_compile_spec,
14+
)
15+
from executorch.backends.samsung.test.tester import SamsungTester
16+
from executorch.backends.samsung.test.utils.utils import TestConfig
17+
18+
19+
class LayerNorm(torch.nn.Module):
20+
def __init__(self) -> None:
21+
super().__init__()
22+
self.module = torch.nn.LayerNorm(10)
23+
24+
def forward(self, x: torch.Tensor) -> torch.Tensor:
25+
return self.module(x)
26+
27+
28+
class TestLayerNorm(unittest.TestCase):
29+
def _test(self, module: torch.nn.Module, inputs):
30+
tester = SamsungTester(
31+
module,
32+
inputs,
33+
[gen_samsung_backend_compile_spec(TestConfig.chipset)],
34+
)
35+
(
36+
tester.export()
37+
.check_count({"torch.ops.aten.layer_norm.default": 1})
38+
.to_edge_transform_and_lower()
39+
.check_not(["executorch_exir_dialects_edge__ops_aten_layer_norm_default"])
40+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
41+
.to_executorch()
42+
.run_method_and_compare_outputs(inputs=inputs)
43+
)
44+
45+
def test_fp32_layer_norm(self):
46+
inputs = (torch.randn(1, 32, 10),)
47+
self._test(LayerNorm(), inputs)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) Samsung Electronics Co. LTD
2+
# All rights reserved
3+
#
4+
# Licensed under the BSD License (the "License"); you may not use this file
5+
# except in compliance with the License. See the license file in the root
6+
# directory of this source tree for more details.
7+
8+
import unittest
9+
10+
import torch
11+
12+
from executorch.backends.samsung.serialization.compile_options import (
13+
gen_samsung_backend_compile_spec,
14+
)
15+
from executorch.backends.samsung.test.tester import SamsungTester
16+
from executorch.backends.samsung.test.utils.utils import TestConfig
17+
18+
19+
class Maximum(torch.nn.Module):
20+
def __init__(self) -> None:
21+
super().__init__()
22+
23+
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
24+
return torch.maximum(x, y)
25+
26+
27+
class TestMaximum(unittest.TestCase):
28+
def _test(self, module: torch.nn.Module, inputs):
29+
tester = SamsungTester(
30+
module,
31+
inputs,
32+
[gen_samsung_backend_compile_spec(TestConfig.chipset)],
33+
)
34+
(
35+
tester.export()
36+
.check_count({"torch.ops.aten.maximum.default": 1})
37+
.to_edge_transform_and_lower()
38+
.check_not(["executorch_exir_dialects_edge__ops_aten_maximum_default"])
39+
.check_count({"torch.ops.higher_order.executorch_call_delegate": 1})
40+
.to_executorch()
41+
.run_method_and_compare_outputs(inputs=inputs)
42+
)
43+
44+
def test_fp32_maximum(self):
45+
inputs = (torch.randn(1, 3, 56, 56), torch.randn(1, 3, 56, 56))
46+
self._test(Maximum(), inputs)

0 commit comments

Comments
 (0)