Skip to content

Commit

Permalink
[ETHOSN] Add support for concatenate with negative axis (#12686)
Browse files Browse the repository at this point in the history
Supports offloading concatenate with a negative axis to the NPU. In addition, parameterized the concatenate unit tests.
  • Loading branch information
lhutton1 authored Sep 2, 2022
1 parent 445a14f commit 0549a08
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/relay/backend/contrib/ethosn/ethosn_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,12 @@ EthosnError EthosnAPI::LeakyReLU(const Expr& expr, LeakyReLUParams* params) {
EthosnError EthosnAPI::Concatenate(const Expr& expr, ConcatenateParams* params) {
Call call = Downcast<Call>(expr);
const auto& attrs = call->attrs.as<ConcatenateAttrs>();
params->concat_info.m_Axis = attrs->axis;
int axis = attrs->axis;
if (axis < 0) {
int output_dims = Downcast<TensorType>(call->checked_type())->shape.size();
axis = output_dims + axis;
}
params->concat_info.m_Axis = axis;

float output_sc;
int output_zp;
Expand Down
49 changes: 26 additions & 23 deletions tests/python/contrib/test_ethosn/test_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,35 @@ def _get_model(shapes, dtype, axis):

@requires_ethosn
@pytest.mark.parametrize("dtype", ["uint8", "int8"])
def test_concatenate(dtype):
"""Compare Concatenate output with TVM."""

trials = [
@pytest.mark.parametrize(
"shapes,axis",
[
([(1, 4), (1, 6)], 1),
([(1, 16, 4), (1, 16, 4)], 1),
([(1, 25, 4, 16)] * 3, 3),
([(1, 25, 4, 16), (1, 25, 5, 16), (1, 25, 6, 16)], 2),
]

([(1, 4), (1, 6)], -1),
([(1, 16, 4), (1, 16, 4)], -2),
],
)
def test_concatenate(dtype, shapes, axis):
"""Compare Concatenate output with TVM."""
np.random.seed(0)
for shapes, axis in trials:
outputs = []
inputs = _get_inputs(shapes, dtype)
for npu in [False, True]:
model = _get_model(shapes, dtype, axis)
mod = tei.make_module(model, {})
outputs.append(tei.build_and_run(mod, inputs, 1, {}, npu=npu))

outputs = []
inputs = _get_inputs(shapes, dtype)
for npu in [False, True]:
model = _get_model(shapes, dtype, axis)
mod = tei.make_module(model, {})
outputs.append(tei.build_and_run(mod, inputs, 1, {}, npu=npu))

tei.verify(outputs, dtype, 0)


@requires_ethosn
def test_concatenate_failure():
"""Check Concatenate error messages."""

trials = [
@pytest.mark.parametrize(
"shapes,dtype,axis,err_msg",
[
([(1, 4, 4, 4, 4), (1, 4, 4, 4, 4)], "uint8", 1, "dimensions=5, dimensions must be <= 4;"),
(
[(1, 4, 4, 4), (1, 4, 4, 4)],
Expand Down Expand Up @@ -110,9 +112,10 @@ def test_concatenate_failure():
0,
"Concatenation cannot be performed along batch axis (axis 0);",
),
]

for shapes, dtype, axis, err_msg in trials:
model = _get_model(shapes, dtype, axis)
mod = tei.make_ethosn_partition(model)
tei.test_error(mod, {}, err_msg)
],
)
def test_concatenate_failure(shapes, dtype, axis, err_msg):
"""Check Concatenate error messages."""
model = _get_model(shapes, dtype, axis)
mod = tei.make_ethosn_partition(model)
tei.test_error(mod, {}, err_msg)

0 comments on commit 0549a08

Please sign in to comment.