Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions python/tvm/contrib/ethosu/cascader/device_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,24 @@ def __init__(self, shape: List[int], layout="NHWC"):
self.width = int(shape[3])
self.depth = int(shape[2]) * int(shape[4])
else:
self.height = int(shape[1])
self.width = int(shape[2])
self.depth = int(shape[3])
# identity layout is NHWC but the shape is not always 4
length = len(shape)
if length == 4:
self.height = int(shape[1])
self.width = int(shape[2])
self.depth = int(shape[3])
elif length == 3:
self.height = int(shape[0])
self.width = int(shape[1])
self.depth = int(shape[2])
elif length == 2:
self.height = int(shape[0])
self.width = int(shape[1])
self.depth = 1
elif length == 1:
self.height = int(shape[0])
self.width = 1
self.depth = 1

def round_up(self, other: "_Shape"):
self.height = _round_up(self.height, other.height)
Expand Down Expand Up @@ -627,18 +642,19 @@ def _get_subkernel_propagator(
stride_w = int(op_attrs.get("stride_w", 1))
transform = ifm_propagator.transform

if input_layout == "NHCWB16":
transform[1][-1] = min(transform[1][-1], self._subkernel_limits[0] - stride_h)
transform[3][-1] = min(transform[3][-1], self._subkernel_limits[1] - stride_w)
else:
transform[1][-1] = min(transform[1][-1], self._subkernel_limits[0] - stride_h)
transform[2][-1] = min(transform[2][-1], self._subkernel_limits[1] - stride_w)

if op_type in ("ethosu_pooling", "ethosu_depthwise_conv2d"):
if output_layout == "NHCWB16" and input_layout == "NHWC":
transform[3][-1] = depth
elif output_layout == "NHCWB16" and input_layout == "NHCWB16":
transform[2][-1] = 1 + ((depth - 1) // 16)
if op_type != "ethosu_identity":
if input_layout == "NHCWB16":
transform[1][-1] = min(transform[1][-1], self._subkernel_limits[0] - stride_h)
transform[3][-1] = min(transform[3][-1], self._subkernel_limits[1] - stride_w)
else:
transform[1][-1] = min(transform[1][-1], self._subkernel_limits[0] - stride_h)
transform[2][-1] = min(transform[2][-1], self._subkernel_limits[1] - stride_w)

if op_type in ("ethosu_pooling", "ethosu_depthwise_conv2d"):
if output_layout == "NHCWB16" and input_layout == "NHWC":
transform[3][-1] = depth
elif output_layout == "NHCWB16" and input_layout == "NHCWB16":
transform[2][-1] = 1 + ((depth - 1) // 16)

return Propagator(transform, ifm_propagator.offset)

Expand Down
86 changes: 83 additions & 3 deletions python/tvm/relay/backend/contrib/ethosu/te/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
# under the License.
# pylint: disable=invalid-name,unused-argument
"""Tensor Expression for identity"""
import numpy as np
from tvm import te
from tvm.contrib.ethosu.cascader import TESubgraph, EthosuPart, Propagator, register_matcher

from .dma import read_compute, write_compute


Expand Down Expand Up @@ -56,7 +59,6 @@ def identity_compute(
-------
te.Tensor
The Output Feature Map tensor.

"""
dmaed_ifm = read_compute(ifm, ifm_zero_point, ifm_scale)
id_attrs = {"op": "ethosu_identity", "activation": activation}
Expand All @@ -76,7 +78,85 @@ def identity_compute(
name="ethosu_identity",
attrs=id_attrs,
)
length = len(ifm.shape)
ifm_matrix = np.identity(length + 1)
offset = np.zeros(length, dtype="int64")
ifm_propagator = Propagator(
ifm_matrix,
offset.tolist(),
)
propagator_attrs = {
"ifm_propagator": ifm_propagator,
}
return write_compute(identity, ofm_zero_point, ofm_scale, attrs=propagator_attrs)


@register_matcher
def match_ethosu_identity(output_tensor, device_config):
"""Match a Tensor Expression corresponding to an NPU identity.

dmaed_ofm = write_compute(identity, ofm_zero_point, ofm_scale)
If the Tensor Expression matches, an EthosuPart will be created that models the
matched Tensor Expression. Otherwise, None will be returned.

return dmaed_ofm
Parameters
----------
output_tensor : tvm.te.Tensor
The tensor to attempt to match with.
device_config : EthosuDeviceConfig
Target device configuration

Returns
-------
Union[None, EthosuPart]
The created EthosuPart if there was a match, otherwise None.
"""
write = output_tensor
if write.op.name != "ethosu_write":
return None
identity = write.op.input_tensors[0]
if identity.op.name != "ethosu_identity":
return None
read = identity.op.input_tensors[0]
if read.op.name != "ethosu_read":
return None

input_tensors = [
read.op.input_tensors[0],
]
subgraph = TESubgraph(input_tensors, output_tensor)
propagators = [
write.op.attrs["ifm_propagator"],
]
ifm_dtype = input_tensors[0].dtype
ofm_dtype = output_tensor.dtype

input_tensors_shape = input_tensors[0].shape
length = len(input_tensors_shape)
channels = int(input_tensors_shape[length - 1]) if length >= 3 else 1

subkernels = len(device_config.get_kernel_steps(identity.op.name, 1, 1, ifm_dtype))

input_layout = output_layout = "NHWC"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should assert that len(input_tensors_shape) <= 4 if we don't support brick layout for identity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, apologies, I should have said that a message would have been helpful alongside the assert as well. Lets take it in a follow up :)

output_quantum = device_config.get_output_quantum(output_layout)

valid_block_configs = device_config.get_valid_block_configs(
propagators[0],
identity.op.attrs,
output_tensor.shape,
channels,
channels,
output_layout,
input_layout,
ifm_dtype,
ofm_dtype,
1,
1,
)

return EthosuPart(
subgraph,
propagators,
output_quantum,
subkernels,
valid_block_configs,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pytest

pytest.importorskip("ethosu.vela")

import numpy as np

from tvm import te
import tvm.contrib.ethosu.cascader as cs
from tvm.relay.backend.contrib.ethosu.te.identity import match_ethosu_identity, identity_compute
from .infra import make_matrices


def test_ethosu_identity_matcher():
ofm_channels = 21
ifm_shape = (1, 12, 15, ofm_channels)
ifm = te.placeholder(ifm_shape, dtype="int8")
lut = te.placeholder((), dtype="uint8")
out = identity_compute(
ifm=ifm,
lut=lut,
ifm_scale=1,
ifm_zero_point=0,
ofm_scale=1,
ofm_zero_point=0,
activation="NONE",
)

length = len(ifm.shape)
ifm_transform = np.identity(length + 1).tolist()
ifm_offset = np.zeros(length, dtype="int64").tolist()

device_config = cs.EthosuDeviceConfig("ethos-u55-256")
part = match_ethosu_identity(out, device_config)

assert isinstance(part, cs.EthosuPart)
assert len(part.propagators) == 1
assert part.propagators[0].transform == ifm_transform
assert part.propagators[0].offset == ifm_offset


if __name__ == "__main__":
pytest.main([__file__])
Loading