Skip to content

[microNPU][ETHOSU] Add fixed point for tanh - #16266

Merged
lhutton1 merged 3 commits into
apache:mainfrom
Aleksei-grovety:ethosu-fixed-point
Mar 21, 2024
Merged

lhutton1 merged 3 commits into
apache:mainfrom
Aleksei-grovety:ethosu-fixed-point

Conversation

@Aleksei-grovety

@Aleksei-grovety Aleksei-grovety commented Dec 20, 2023 •

Copy link
Copy Markdown
Contributor

Add support for calculation tanh with 16 bits fixed point (legalization of non-quantized tanh operation with quantization by fixed point multiplication).

cc @lhutton1, @ekalda, @leandron

@github-actions
github-actions Bot requested a review from lhutton1 December 20, 2023 13:00
ekalda
ekalda previously requested changes Dec 21, 2023

@ekalda ekalda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Aleksei-grovety! I think in the current form of the patch the calculated int16 values are not actually used...

op_map = {
"CLIP": vapi.NpuActivationOp.NONE_OR_RELU,
"TANH": vapi.NpuActivationOp.TABLE_LOOKUP,
"TANH": vapi.NpuActivationOp.TANH,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change would make it always use the NPU's builtin tanh function instead of the calculated lookup table values, making it to not match TFLite reference kernels and turning all tanh LUT value calculation into dead code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These changes have been cancelled and the calculated lookup table values are being used, but first, PR merge is expected

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PR was merged and the code has been updated.

return identity
return identity
elif params.ifm.dtype == "int16":
lut_tanh = relay.const([], "int16")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like this is adding an empty lookup table to the identity operator?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

After the update, the calculated lookup table values for Int16 are used.

Add support for calculation tanh with 16 bits fixed point. Add flag enable_fixed_point to enable fixed point calculation. We get good accuracy with 1 bit to integer part and 15 bits for fractional, with other cases we get worse results.
use fixed_point_multiply to define fraction_size, use LUT for tanh

@lhutton1 lhutton1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me! I'll defer to @ekalda to make sure original concerns were addressed

output_max - output_min
)
table_min = np.iinfo(np.int16).min
table_max = np.iinfo(np.int16).max

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: these results can be used to calculate input_min, input_max etc above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@lhutton1
lhutton1 dismissed ekalda’s stale review March 21, 2024 10:06

Original comments were addressed

@lhutton1
lhutton1 merged commit 62beb02 into apache:main Mar 21, 2024
@lhutton1

Copy link
Copy Markdown
Contributor

Thanks @Aleksei-grovety @ekalda!

thaisacs pushed a commit to thaisacs/tvm that referenced this pull request Apr 3, 2024
Add support for calculation tanh with 16 bits fixed point (legalization of non-quantized tanh operation with quantization by fixed point multiplication).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants