-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[microNPU] Add the infrastructure for lookup table and TANH #9547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| # pylint: disable=invalid-name, unused-argument, import-outside-toplevel, no-value-for-parameter | ||
| """A set of passes to legalize some of operations for the NPU""" | ||
| from typing import List, Type | ||
| import math | ||
|
|
||
| import numpy as np # type: ignore | ||
|
|
||
|
|
@@ -31,6 +32,7 @@ | |
| from tvm.relay.backend.contrib.ethosu import op as ethosu_ops # type: ignore | ||
| from tvm.relay.backend.contrib.ethosu.errors import UnsupportedLayout # type: ignore | ||
| from tvm.relay.backend.contrib.ethosu import vela_api | ||
| from tvm.relay.backend.contrib.ethosu import util | ||
| from tvm.relay.op.contrib import ethosu as ethosu_patterns # type: ignore | ||
|
|
||
|
|
||
|
|
@@ -123,6 +125,75 @@ def __call__(self, *args, **kwargs): | |
| pass | ||
|
|
||
|
|
||
| def find_tanh_values(ifm_scale, ifm_zp, ofm_scale, ofm_zp): | ||
| """Method to calculate the values of the tanh lookup table""" | ||
| lut_values = list() | ||
| # Only int8 is currently supported | ||
| dtype = np.int8 | ||
| qmin, qmax = np.iinfo(dtype).min, np.iinfo(dtype).max | ||
| for x in range(qmin, qmax + 1): | ||
| x_real = ifm_scale * (x - ifm_zp) | ||
| out_real = math.tanh(x_real) | ||
| lut_result = int(util.round_away_zero(ofm_zp + out_real / ofm_scale)) | ||
| lut_result = min(qmax, max(qmin, lut_result)) | ||
| lut_values.append(lut_result) | ||
|
|
||
| return lut_values | ||
|
|
||
|
|
||
| class TanhRewriter(DFPatternCallback): | ||
| """This pass adds tanh as a LUT to the identity operator""" | ||
|
|
||
| def __init__(self): | ||
| super().__init__(require_type=True, rewrite_once=True) | ||
| self.pattern = ( | ||
| wildcard().has_attr({"Composite": ethosu_patterns.TanhParams.composite_name}) | ||
| )(wildcard()) | ||
|
|
||
| def callback(self, pre, post, node_map): | ||
| id_input = post.args[0] | ||
|
|
||
| quantize_args = post.op.body.args | ||
| output_scale = float(quantize_args[1].data.asnumpy()) | ||
| output_zp = int(quantize_args[2].data.asnumpy()) | ||
|
|
||
| dequantize_args = quantize_args[0].args[0].args | ||
| input_scale = float(dequantize_args[1].data.asnumpy()) | ||
| input_zp = int(dequantize_args[2].data.asnumpy()) | ||
|
|
||
| lut_values = find_tanh_values(input_scale, input_zp, output_scale, output_zp) | ||
| lut = relay.const(lut_values, dtype="uint8") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, can LUT be any other dtype?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, the values are calculated for an
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, glad to see a fix for that issue will improve something else :) |
||
|
|
||
| # We baked the requantization into the LUT, so we don't requantize the identity operator | ||
| identity = ethosu_ops.ethosu_identity( | ||
| ifm=id_input, | ||
| lut=lut, | ||
| ifm_scale=input_scale, | ||
| ifm_zero_point=input_zp, | ||
| ofm_scale=input_scale, | ||
| ofm_zero_point=input_zp, | ||
| activation="TANH", | ||
| ) | ||
|
|
||
| return identity | ||
|
|
||
|
|
||
| @ir.transform.module_pass(opt_level=1) | ||
| class LegalizeTanh: | ||
| """This is the pass that wraps TanhRewriter""" | ||
|
|
||
| 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(TanhRewriter(), func) | ||
| mod.update_func(global_var, func) | ||
| return mod | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| pass | ||
|
|
||
|
|
||
| class Conv2DRewriter(DFPatternCallback): | ||
| """Convert conv2d related composite functions into ethosu_conv2d operators""" | ||
|
|
||
|
|
@@ -915,6 +986,7 @@ def transform_module( | |
| mod = LegalizeMax()(mod) | ||
| mod = LegalizeShl()(mod) | ||
| mod = LegalizeAbs()(mod) | ||
| mod = LegalizeTanh()(mod) | ||
| mod = LegalizeReshape()(mod) | ||
| mod = LegalizeStridedSlice()(mod) | ||
| mod = LegalizeNoOps()(mod) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # 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. | ||
| """The attributes node used for Arm(R) Ethos(TM)-U NPU Relay operators.""" | ||
| from tvm.ir import Attrs | ||
| import tvm._ffi | ||
|
|
||
|
|
||
| @tvm._ffi.register_object("relay.attrs.EthosuConv2DAttrs") | ||
| class EthosuConv2DAttrs(Attrs): | ||
| """Attributes for contrib.ethosu.conv2d.""" | ||
|
|
||
|
|
||
| @tvm._ffi.register_object("relay.attrs.EthosuIdentityAttrs") | ||
| class EthosuIdentityAttrs(Attrs): | ||
| """Attributes for contrib.ethosu.identity.""" | ||
|
|
||
|
|
||
| @tvm._ffi.register_object("relay.attrs.EthosuDepthwiseConv2DAttrs") | ||
| class EthosuDepthwiseConv2DAttrs(Attrs): | ||
| """Attributes for contrib.ethosu.depthwise_conv2d.""" | ||
|
|
||
|
|
||
| @tvm._ffi.register_object("relay.attrs.EthosuPoolingAttrs") | ||
| class EthosuPooling2DAttrs(Attrs): | ||
| """Attributes for contrib.ethosu.pooling.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: missing
Parametersand I think we need spacing for these doc strings?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done