Skip to content

Commit 068c5e8

Browse files
NXP backend: Fix default params functionality for Amin, Sum
1 parent d65ac60 commit 068c5e8

7 files changed

Lines changed: 64 additions & 5 deletions

File tree

backends/nxp/backend/ir/converter/node_converters/ops_converters/amin_converter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ def convert(self, node: Node):
7070
t_op.builtin_options = reduce_min_options.ReduceMin(keepdim)
7171

7272
ops = OpsList(middle_op=t_op)
73-
dim = get_dim_and_handle_io_formats(self.builder, ops, dim, keepdim)
73+
# dim default value is None, it that case no changes to dim or io_formats are needed and all dims are reduced
74+
dim = (
75+
get_dim_and_handle_io_formats(self.builder, ops, dim, keepdim)
76+
if dim is not None
77+
else None
78+
)
7479

7580
convert_axes_from_attribute(t_op, self.builder, dim)
7681
self.builder.append_operators(ops.flatten())

backends/nxp/backend/ir/converter/node_converters/ops_converters/sum_dim_int_list_converter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ def convert(self, node: Node):
7272
t_op.builtin_options = sum_options.Sum(keepdim)
7373

7474
ops = OpsList(middle_op=t_op)
75-
dim = get_dim_and_handle_io_formats(self.builder, ops, dim, keepdim)
75+
# dim default value is None, it that case no changes to dim or io_formats are needed and all dims are reduced
76+
dim = (
77+
get_dim_and_handle_io_formats(self.builder, ops, dim, keepdim)
78+
if dim is not None and dim != []
79+
else None
80+
)
7681

7782
convert_axes_from_attribute(t_op, self.builder, dim)
7883
self.builder.append_operators(ops.flatten())

backends/nxp/quantizer/neutron_quantizer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
SqueezePattern,
5959
SubTensorPattern,
6060
SumDimIntListPattern,
61+
SumPattern,
6162
TanhInPlacePattern,
6263
TanhPattern,
6364
TransposeIntPattern,
@@ -304,6 +305,7 @@ def __init__(self, neutron_target_spec: NeutronTargetSpec, is_qat: bool = False)
304305
OpQuantizer(SqueezePattern(is_qat=is_qat), static_qconfig),
305306
OpQuantizer(SubTensorPattern(is_qat=is_qat), static_qconfig),
306307
OpQuantizer(SumDimIntListPattern(is_qat=is_qat), static_qconfig),
308+
OpQuantizer(SumPattern(is_qat=is_qat), static_qconfig),
307309
OpQuantizer(TanhPattern(is_qat=is_qat), static_qconfig),
308310
OpQuantizer(TanhInPlacePattern(is_qat=is_qat), static_qconfig),
309311
OpQuantizer(TransposeIntPattern(is_qat=is_qat), static_qconfig),

backends/nxp/quantizer/patterns.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,15 @@ def partition_types(self):
11501150
return [torch.ops.aten.squeeze.dims]
11511151

11521152

1153+
class SumPattern(SharedSpecPattern):
1154+
"""
1155+
Quantizer for the `aten.sum.default` operator.
1156+
"""
1157+
1158+
def partition_types(self):
1159+
return [torch.ops.aten.sum.default]
1160+
1161+
11531162
class SumDimIntListPattern(SharedSpecPattern):
11541163
"""
11551164
Quantizer for the `aten.sum.dim_IntList` operator.

backends/nxp/tests/ir/converter/node_converter/test_amin_converter.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def forward(self, x):
5757
return torch.amin(x, dim=self.dim, keepdim=self.keepdim)
5858

5959

60+
class AminDefaultParamsModule(torch.nn.Module):
61+
@staticmethod
62+
def forward(x):
63+
return torch.amin(x)
64+
65+
6066
class AminAddModule(AminModule):
6167
def forward(self, x):
6268
x = super().forward(x)
@@ -172,6 +178,19 @@ def test__tuple_dims(self, mocker, request, input_shape, dim, keep_dim):
172178
model = AminModule(dim, keep_dim)
173179
assert_delegated(model, input_shape, mocker, request)
174180

181+
@pytest.mark.parametrize(
182+
"input_shape",
183+
[
184+
pytest.param((4, 2), id="2D."),
185+
pytest.param((2, 3, 4), id="3D."),
186+
pytest.param((1, 3, 3, 7), id="4D."),
187+
pytest.param((3, 1, 4, 1, 5), id="5D."),
188+
],
189+
)
190+
def test__default_params(self, mocker, request, input_shape):
191+
model = AminDefaultParamsModule()
192+
assert_delegated(model, input_shape, mocker, request)
193+
175194
@pytest.mark.parametrize(
176195
"input_shape, dim",
177196
[
@@ -310,7 +329,7 @@ def test__channels_first_input__reducing_all_spatial_dims(
310329
self, mocker, request, dim
311330
):
312331
# If the spatial dimensions are reduced (removed), the `amin` output will always be equal in channels
313-
# first and channels last, so no `Transpose` ops are added.
332+
# first and channels last, so no `Transpose` ops before `ReduceMin` are added.
314333
input_shape = (1, 7, 3, 3)
315334
model = MaxPoolAminModule(dim, False)
316335

backends/nxp/tests/ir/converter/node_converter/test_mean_dim_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def test__channels_first_input__reducing_all_spatial_dims(
306306
self, mocker, request, dim
307307
):
308308
# If the spatial dimensions are reduced (removed), the `mean` output will always be equal in channels
309-
# first and channels last, so no `Transpose` ops are added.
309+
# first and channels last, so no `Transpose` ops before `Mean` are added.
310310
input_shape = (1, 7, 3, 3)
311311
model = MaxPoolMeanDimModule(dim, False)
312312

backends/nxp/tests/ir/converter/node_converter/test_sum_dim_int_list_converter.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def forward(self, x):
5757
return torch.sum(x, dim=self.dim, keepdim=self.keepdim)
5858

5959

60+
class SumDefaultParamsModule(torch.nn.Module):
61+
@staticmethod
62+
def forward(x):
63+
return torch.sum(x)
64+
65+
6066
class SumAddModule(SumModule):
6167
def forward(self, x):
6268
x = super().forward(x)
@@ -172,6 +178,19 @@ def test__tuple_dims(self, mocker, request, input_shape, dim, keep_dim):
172178
model = SumModule(dim, keep_dim)
173179
assert_delegated(model, input_shape, mocker, request)
174180

181+
@pytest.mark.parametrize(
182+
"input_shape",
183+
[
184+
pytest.param((4, 2), id="2D."),
185+
pytest.param((2, 3, 4), id="3D."),
186+
pytest.param((1, 3, 3, 7), id="4D."),
187+
pytest.param((3, 1, 4, 1, 5), id="5D."),
188+
],
189+
)
190+
def test__default_params(self, mocker, request, input_shape):
191+
model = SumDefaultParamsModule()
192+
assert_delegated(model, input_shape, mocker, request)
193+
175194
@pytest.mark.parametrize(
176195
"input_shape, dim",
177196
[
@@ -314,7 +333,7 @@ def test__channels_first_input__reducing_all_spatial_dims(
314333
self, mocker, request, dim
315334
):
316335
# If the spatial dimensions are reduced (removed), the `sum` output will always be equal in channels
317-
# first and channels last, so no `Transpose` ops are added.
336+
# first and channels last, so no `Transpose` ops before `Sum` are added.
318337
input_shape = (1, 7, 3, 3)
319338
model = MaxPoolSumModule(dim, False)
320339

0 commit comments

Comments
 (0)