Skip to content

Commit d608b1f

Browse files
NXP backend: Update broadcast condition for broadcastable ops
1 parent 252f57c commit d608b1f

7 files changed

Lines changed: 23 additions & 9 deletions

File tree

backends/nxp/backend/ir/converter/node_converter.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import operator
77
from abc import ABC, abstractmethod
8+
from math import prod
89
from typing import Callable
910

1011
import torch
@@ -411,30 +412,31 @@ def uses_shape_broadcasting(node: Node) -> bool:
411412
)
412413

413414
@staticmethod
414-
def at_least_one_input_shape_matches_the_output_shape(node: Node) -> bool:
415-
"""Determine if given PyTorch fx Node uses at least one input shape broadcasting for it's input nodes or not.
415+
def inputs_satisfy_broadcast_condition(node: Node) -> bool:
416+
"""Determine if given PyTorch fx Node has inputs that satisfy broadcasting conditions for Neutron or not.
416417
417418
:param node: PyTorch fx Node with 'all_input_nodes' initialized.
418-
:return: True, if at least one input has the same shape as the output node.
419+
:return: True, if at least one input has the same number of elements as the output node.
419420
False otherwise.
420421
"""
421422

422423
if node.all_input_nodes is None:
423424
logger.e(
424425
logger.Code.INTERNAL_ERROR,
425-
"node_converter.at_least_one_input_shape_matches_the_output_shape(): 'all_input_nodes' are None!",
426+
"node_converter.inputs_satisfy_broadcast_condition(): 'all_input_nodes' are None!",
426427
)
427428

428429
if len(node.all_input_nodes) == 0:
429430
logger.e(
430431
logger.Code.INTERNAL_ERROR,
431-
"node_converter.at_least_one_input_shape_matches_the_output_shape(): Operator has no inputs!",
432+
"node_converter.inputs_satisfy_broadcast_condition(): Operator has no inputs!",
432433
)
433434

434435
output_shape = node.meta["val"].shape
436+
num_elements = prod(output_shape)
435437

436438
return any(
437-
input_tensor.meta["val"].shape == output_shape
439+
prod(input_tensor.meta["val"].shape) == num_elements
438440
for input_tensor in node.all_input_nodes
439441
)
440442

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _is_supported_on_target(
2626
parameters_mapping: dict[str, Parameter],
2727
custom_delegation_options: CustomDelegationOptions,
2828
) -> bool:
29-
if not NodeConverter.at_least_one_input_shape_matches_the_output_shape(node):
29+
if not NodeConverter.inputs_satisfy_broadcast_condition(node):
3030
return False
3131

3232
# If one input is in channel first and ranks of input tensors are not equal, we need to add Transposes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _is_supported_on_target(
2525
parameters_mapping: dict[str, Parameter],
2626
custom_delegation_options: CustomDelegationOptions,
2727
) -> bool:
28-
if not NodeConverter.at_least_one_input_shape_matches_the_output_shape(node):
28+
if not NodeConverter.inputs_satisfy_broadcast_condition(node):
2929
return False
3030

3131
# If one input is in channel first and ranks of input tensors are not equal, we need to add Transposes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _is_supported_on_target(
2626
parameters_mapping: dict[str, Parameter],
2727
custom_delegation_options: CustomDelegationOptions,
2828
) -> bool:
29-
if not NodeConverter.at_least_one_input_shape_matches_the_output_shape(node):
29+
if not NodeConverter.inputs_satisfy_broadcast_condition(node):
3030
return False
3131

3232
# If one input is in channel first and ranks of input tensors are not equal, we need to add Transposes

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def test__basic_nsys_inference_qat(self, mocker, request):
104104
pytest.param(
105105
[ModelInputSpec((4,)), ModelInputSpec((4, 4))], id="2 inputs 1D + 2D."
106106
),
107+
pytest.param(
108+
[ModelInputSpec((10,)), ModelInputSpec((1, 1))],
109+
id="2 inputs 2D, num_elems of input == num_elems of output",
110+
),
107111
],
108112
)
109113
def test__broadcast(self, mocker, request, input_spec):

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ def test__basic_nsys_inference_qat(self, mocker, request, x_input_shape):
9090
pytest.param(
9191
[ModelInputSpec((4,)), ModelInputSpec((4, 4))], id="2 inputs 1D+2D."
9292
),
93+
pytest.param(
94+
[ModelInputSpec((10,)), ModelInputSpec((1, 1))],
95+
id="2 inputs 2D, num_elems of input == num_elems of output",
96+
),
9397
],
9498
)
9599
def test__correct_broadcast(self, input_spec, mocker, request):

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def test__basic_nsys_inference_qat(self, mocker, request):
104104
[ModelInputSpec((5, 3, 4)), ModelInputSpec((1, 3, 1))],
105105
id="2 inputs 3D.",
106106
),
107+
pytest.param(
108+
[ModelInputSpec((10,)), ModelInputSpec((1, 1))],
109+
id="2 inputs 2D, num_elems of input == num_elems of output",
110+
),
107111
],
108112
)
109113
def test__broadcast(self, mocker, request, input_spec):

0 commit comments

Comments
 (0)