Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
94 changes: 94 additions & 0 deletions python/tvm/relay/backend/contrib/ethosu/legalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,99 @@ def __call__(self, *args, **kwargs):
pass


class EthosuDepthwiseConv2DRewriter(DFPatternCallback):
"""Convert ethosu.qnn_depthwise_conv2d composite functions to ethosu_depthwise_conv2d
operators"""

def __init__(self):
super().__init__(require_type=True)
self.pattern = (
wildcard().has_attr(
{"Composite": ethosu_patterns.QnnDepthwiseConv2DParams.composite_name}
)
)(wildcard())

def callback(
self, pre: tvm.relay.Expr, post: tvm.relay.Expr, node_map: tvm.ir.container.Map
) -> tvm.relay.Expr:
params = ethosu_patterns.QnnDepthwiseConv2DParams(post.op.body)
params.ifm.tensor = post.args[0]
channels_map = {
"NHWC": 3,
}
if str(params.ofm.layout) not in channels_map.keys():
raise UnsupportedLayout(str(params.ofm.layout))
kernel_shape_map = {
"HWOI": params.weights.shape[0:2],
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth supporting OHWI weights here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIRC, in the Relay that corresponds to depthwise conv2d operator from TFLite, the weights are always in HWOI, that's why other formats are not handled here.

}
if str(params.weights.layout) not in kernel_shape_map.keys():
raise UnsupportedLayout(str(params.weights.layout))

weights_values = params.weights.values
weights_values_ohwi = np.moveaxis(weights_values, [0, 1, 2, 3], [1, 2, 0, 3])

activation = "NONE"
# Activations requiring LUT is currently not supported, so setting it to an empty list
lut = relay.const([], "int8")
clip_min = 0
clip_max = 0
if params.activation:
activation = ethosu_patterns.QnnDepthwiseConv2DParams.activation_map[
params.activation.op.name
]
if activation == "CLIP":
clip_min = int(params.activation.attrs.a_min)
clip_max = int(params.activation.attrs.a_max)
scale_bias = vela_api.pack_biases(
biases=params.biases.tensor.data.asnumpy(),
ifm_scale=params.ifm.q_params.scale_f32,
ifm_dtype=np.dtype(params.ifm.dtype),
weight_scales=params.weights.q_params.scale_f32,
ofm_scale=params.ofm.q_params.scale_f32,
is_activation_tanh_or_sigmoid=activation in ["TANH", "SIGMOID"],
)

ethosu_depthwise_conv2d = ethosu_ops.ethosu_depthwise_conv2d(
post.args[0], # IFM
relay.const(weights_values_ohwi, params.weights.values.dtype),
relay.const(scale_bias, "uint8"),
lut,
float(params.ifm.q_params.scale_f32),
int(params.ifm.q_params.zero_point),
int(params.weights.q_params.zero_point),
float(params.ofm.q_params.scale_f32),
int(params.ofm.q_params.zero_point),
kernel_shape_map[str(params.weights.layout)],
params.ofm.shape[channels_map[str(params.ofm.layout)]],
strides=params.strides,
padding=params.padding,
dilation=params.dilation,
activation=activation,
clip_min=clip_min,
clip_max=clip_max,
upscale="NONE",
ifm_layout=str(params.ifm.layout),
ofm_layout=str(params.ofm.layout),
)
return ethosu_depthwise_conv2d


@ir.transform.module_pass(opt_level=1)
class LegalizeEthosUDepthwiseConv2D:
"""This is the pass that wraps the EthosUDepthwiseConv2DRewriter"""

def transform_module(
self, mod: tvm.ir.IRModule, ctx: tvm.ir.transform.PassContext
) -> tvm.ir.IRModule:
for global_var, func in mod.functions.items():
func = rewrite(EthosuDepthwiseConv2DRewriter(), func)
mod.update_func(global_var, func)
return mod

def __call__(self, *args, **kwargs):
pass


@ir.transform.module_pass(opt_level=1)
class LegalizeEthosU:
"""This is the pass to call graph-rewrites to perform graph transformation
Expand All @@ -220,6 +313,7 @@ def transform_module(
) -> tvm.ir.IRModule:
mod = LegalizeSplit()(mod)
mod = LegalizeEthosUConv2D()(mod)
mod = LegalizeEthosUDepthwiseConv2D()(mod)
return mod

def __call__(self, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions python/tvm/relay/backend/contrib/ethosu/op/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
"Relay operators for the Arm(R) Ethos(TM)-U NPU"

from .convolution import ethosu_conv2d
from .depthwise import ethosu_depthwise_conv2d
205 changes: 205 additions & 0 deletions python/tvm/relay/backend/contrib/ethosu/op/depthwise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# 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.
# pylint: disable=unused-argument
"""Relay operator for depthwise convolution"""
from typing import Tuple

import tvm
from tvm.relay.op import _make
from tvm.topi.generic import schedule_injective
from tvm.relay.op.op import OpStrategy
from tvm.relay.op import strategy as _strategy

from ..te import depthwise_conv2d_compute


def _extract_ethosu_depthwise_conv2d_params(attrs, args):
"""Get the parameters necessary to construct a ethosu_depthwise_conv2d compute TE
from a ethosu_depthwise_conv2d Relay call."""
ifm = args[0]
weight = args[1]
scale_bias = args[2]
lut = args[3]
ifm_scale = attrs.ifm_scale
ifm_zero_point = attrs.ifm_zero_point
weight_zero_point = attrs.weight_zero_point
ofm_scale = attrs.ofm_scale
ofm_zero_point = attrs.ofm_zero_point
strides = attrs.strides
padding = attrs.padding
dilation = attrs.dilation
activation = attrs.activation
clip_min = attrs.clip_min
clip_max = attrs.clip_max
upscale = attrs.upscale
ifm_layout = attrs.ifm_layout
ofm_layout = attrs.ofm_layout

return (
ifm,
weight,
scale_bias,
lut,
ifm_scale,
ifm_zero_point,
weight_zero_point,
ofm_scale,
ofm_zero_point,
strides,
padding,
dilation,
activation,
clip_min,
clip_max,
upscale,
ifm_layout,
ofm_layout,
)


@tvm.ir.register_op_attr("contrib.ethosu.depthwise_conv2d", "FTVMCompute")
def create_ethosu_depthwise_conv2d_compute(attrs, args, out_type):
"""Create an ethosu_depthwise_conv2d compute op."""
params = _extract_ethosu_depthwise_conv2d_params(attrs, args)
op = depthwise_conv2d_compute(*params)
return [op]


@tvm.ir.register_op_attr("contrib.ethosu.depthwise_conv2d", "FTVMStrategy")
def depthwise_conv2d_strategy_ethosu(attrs, inputs, out_type, target):
strategy = OpStrategy()
strategy.add_implementation(
create_ethosu_depthwise_conv2d_compute,
_strategy.wrap_topi_schedule(schedule_injective),
name="ethosu_depthwise_conv2d",
)
return strategy


def ethosu_depthwise_conv2d(
ifm: tvm.relay.Expr,
weight: tvm.relay.Expr,
scale_bias: tvm.relay.Expr,
lut: tvm.relay.Expr,
ifm_scale: float,
ifm_zero_point: int,
weight_zero_point: int,
ofm_scale: float,
ofm_zero_point: int,
kernel_shape: Tuple[int, int],
ofm_channels: int,
strides: Tuple[int, int] = (1, 1),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit : We can use Optional[Tuple[int, int]]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left it as is since Optional is used when the variable can take a value None

padding: Tuple[int, int, int, int] = (0, 0, 0, 0),
dilation: Tuple[int, int] = (1, 1),
activation: str = "NONE",
clip_min: int = 0,
clip_max: int = 0,
upscale: str = "NONE",
ifm_layout: str = "NHWC",
ofm_layout: str = "NHWC",
) -> tvm.relay.Call:
"""This is a quantized 2D depthwise convolution operation as supported
by the NPU. It accepts either NHWC or NHCWB16 format
for the input data and OHWI format for the kernel weights.

Reference: https://developer.arm.com/documentation/102420/0200/

Note that the per-channel weight scale and bias tensor must be
packed together into a combined tensor of uint80s. This is represented
in TVM by a (channels, 10) tensor of type uint8. For more detail,
refer to the Technical Reference Manual linked above.

Parameters
----------
ifm : tvm.relay.Expr
The Input Feature Map tensor (IFM).
weight : tvm.relay.Expr
The weight tensor.
scale_bias : tvm.relay.Expr
The packed per-channel weight scale and bias tensor.
lut : tvm.relay.Expr
The look-up table values to use if activation = "LUT"
ifm_scale : float
The quantization scale for the Input Feature Map tensor.
ifm_zero_point : int
The quantization zero point for the Input Feature Map tensor.
weight_zero_point : int
The quantization zero point for the weight tensor.
ofm_scale : float
The quantization scale for the Output Feature Map tensor.
ofm_zero_point : int
The quantization zero point for the Output Feature Map tensor.
kernel_shape : tuple of int
The 2 dimensional kernel shape as (kernel_height, kernel_width).
ofm_channels : int
The number of OFM channels.
strides : tuple of int, optional
The 2 dimensional strides as (stride_height, stride_width).
padding : tuple of int, optional
The 4 dimensional padding as (pad_top, pad_left, pad_bottom, pad_right).
dilation : tuple of int, optional
The 2 dimensional dilation as (dilation_height, dilation_width).
activation : str, optional
The activation function to use.
"NONE" - no activation function.
"CLIP" - clip the output between clip_min and clip_max.
"TANH" - tanh activation function.
"SIGMOID" - sigmoid activation function.
"LUT" - use a look-up table to perform
the activation function.
clip_min : int, optional
The minimum clipping value if activation = "CLIP"
clip_max : int, optional,
The maximum clipping value if activation = "CLIP"
upscale : str, optional
The 2x2 upscaling mode to apply to the Input Feature Map tensor.
"NONE" - no upscaling.
"NEAREST" - upscale using nearest neighbour.
"ZEROS" - upscale using zeros.
ifm_layout : str, optional
The layout of the Input Feature Map tensor. Can be "NHWC" or "NHCWB16".
ofm_layout : str, optional
The layout of the Output Feature Map tensor. Can be "NHWC" or "NHCWB16".

Returns
-------
out : tvm.relay.Call
A call to the ethosu_depthwise_conv2d op.

"""
return _make.ethosu_depthwise_conv2d(
ifm,
weight,
scale_bias,
lut,
ifm_scale,
ifm_zero_point,
weight_zero_point,
ofm_scale,
ofm_zero_point,
kernel_shape,
ofm_channels,
strides,
padding,
dilation,
activation,
clip_min,
clip_max,
upscale,
ifm_layout,
ofm_layout,
)
1 change: 1 addition & 0 deletions python/tvm/relay/backend/contrib/ethosu/te/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
"""Tensor Expressions for the NPU"""

from .convolution import *
from .depthwise import *
Loading