-
Notifications
You must be signed in to change notification settings - Fork 713
Arm backend: Int16 linear support #14258
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc0f134
Arm backend: Handle 16 bit activation for conv2d
per 60033f9
Arm backend: Decompose conv2d with 16 bit activation
per edc1c42
Arm backend: Handle i48 special case for bias tensor
per 2bd09f9
Arm backend: Fix mult and scale calculation for int48_t
per aafcede
Arm backend: Enable linear 16a8w tests
per 18c9985
Arm backend: Add special dtype TOSA handling
per 0bdb35c
Merge branch 'main' into int16_linear_support
digantdesai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
backends/arm/_passes/decompose_int16_activation_conv2d_pass.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # Copyright 2025 Arm Limited and/or its affiliates. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| # pyre-unsafe | ||
|
|
||
| from typing import cast | ||
|
|
||
| import torch | ||
| from executorch.backends.arm._passes.quant_args import QuantArgs | ||
|
|
||
| from executorch.backends.arm.tosa.specification import get_context_spec, Tosa_1_00 | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass | ||
|
|
||
|
|
||
| class DecomposeConv2dWithInt16ActivationPass(ExportPass): | ||
| """ | ||
| This pass decomposes a convolution with input dtype int16 and bias | ||
| into a convolution without bias followed by an addition of the bias | ||
| since the TOSA op requires the bias to be int48 which is hard to represent | ||
| in torch. Instead rescale the int48 output to int16 and add the bias in int16. | ||
| """ | ||
|
|
||
| def call_operator(self, op, args, kwargs, meta): | ||
| if op != exir_ops.edge.aten.convolution.default: | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| tosa_spec = get_context_spec() | ||
| if not tosa_spec.support_integer(): | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| # return if no bias | ||
| if args[2] is None: | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| if args[0].data.dtype == torch.int8: | ||
| return super().call_operator(op, args, kwargs, meta) | ||
| elif args[0].data.dtype == torch.int16: | ||
| if isinstance(tosa_spec, Tosa_1_00) and not tosa_spec.support_extension( | ||
| "int16" | ||
| ): | ||
| raise ValueError( | ||
| "int16 activation for convolution requires TOSA int16 extension" | ||
| ) | ||
| else: | ||
| raise NotImplementedError( | ||
| "Decomposition to conv+add only implemented for activation of int16 type" | ||
| ) | ||
|
|
||
| # convolution with bias and activation is int16 | ||
| # The bias is assumed to be quantized with the same quantization parameters as | ||
| # as the output of the convolution | ||
| bias = args[2] | ||
| assert ( | ||
| meta.data["output_qparams"][0].dtype == bias.data.dtype | ||
| ), "Bias needs to have same type as quantized output type" | ||
| no_bias_args = list(args) | ||
| no_bias_args[2] = None | ||
| # split up to convolution + bias | ||
| convolution = super().call_operator(op, tuple(no_bias_args), kwargs, meta) | ||
|
|
||
| # create a copy of the meta without the qparams, to be used with the new nodes | ||
| new_meta = meta.copy() | ||
| new_meta.data.pop("output_qparams", None) | ||
| new_meta.data.pop("input_qparams", None) | ||
|
|
||
| # reshape the tensor to the same rank as the convolution output to add the bias to the channels | ||
| channel_bias = super().call_operator( | ||
| exir_ops.edge.aten.view_copy.default, | ||
| (bias, [1, len(bias.data), 1, 1]), | ||
| {}, | ||
| new_meta, | ||
| ) | ||
|
|
||
| output_dtype = meta.data["output_qparams"][0].dtype | ||
|
|
||
| if output_dtype == torch.int16: | ||
| # The conv will get the output int48 scaled to int32 in serialization step. | ||
| # To be able to add the bias we need to first scale (cast?) the output to int32. | ||
| # The resulting i32 sum will then need to be scaled back to the output dtype. | ||
|
|
||
| # calculate common rescale factor from convolution output and bias quantization | ||
| output_qparams = cast(QuantArgs, meta.data["output_qparams"][0]) | ||
| conv_output_scale = output_qparams.scale | ||
| bias_qparams = cast(QuantArgs, meta.data["input_qparams"][2]) | ||
| bias_scale = bias_qparams.scale | ||
|
|
||
| common_scale = max(bias_scale, conv_output_scale) | ||
|
|
||
| # calculate how we can rescale bias and conv to a common scale and maximize the output range | ||
| bias_rescale_factor = bias_scale / common_scale | ||
| conv_rescale_factor = conv_output_scale / common_scale | ||
|
|
||
| # Either of conv output or bias now covers the full int16 range and the other one a smaller range. | ||
| # Since we are upscaling to int32 we have 16 additional bits to work with to maximize the output range. | ||
| # Worst case here is that both bias and conv output covers the full int16 range so we leave one bit | ||
| # and then one for the sign bit. | ||
| bits_left_to_shift = 14 | ||
|
|
||
| # update rescale factors | ||
| bias_rescale_factor *= 1 << bits_left_to_shift | ||
| conv_rescale_factor *= 1 << bits_left_to_shift | ||
|
|
||
| conv_output = super().call_operator( | ||
| exir_ops.backend.tosa.RESCALE.default, | ||
| (convolution, torch.int32, conv_rescale_factor, 0, 0), | ||
| {}, | ||
| new_meta, | ||
| ) | ||
|
|
||
| bias_rescaled = super().call_operator( | ||
| exir_ops.backend.tosa.RESCALE.default, | ||
| (channel_bias, torch.int32, bias_rescale_factor, 0, 0), | ||
| {}, | ||
| new_meta, | ||
| ) | ||
|
|
||
| add = super().call_operator( | ||
| exir_ops.edge.aten.add.Tensor, | ||
| (conv_output, bias_rescaled), | ||
| {}, | ||
| new_meta, | ||
| ) | ||
|
|
||
| res_rescale = super().call_operator( | ||
| exir_ops.backend.tosa.RESCALE.default, | ||
| ( | ||
| add, | ||
| output_dtype, | ||
| (common_scale / (conv_output_scale * (1 << bits_left_to_shift))), | ||
| 0, | ||
| 0, | ||
| ), | ||
| {}, | ||
| new_meta, | ||
| ) | ||
|
|
||
| else: | ||
| raise NotImplementedError( | ||
| f"Decomposition to conv+add only implemented for activation of int16 type, not for {output_dtype}" | ||
| ) | ||
|
|
||
| return res_rescale |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Or can it be done by using torch.dtype.int64 instead and then detecting and lowering it as int48 downstream?
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.
I was starting of in that direction, but it interfere a bit with the int64->int32 handling, so rather keep it separate.
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.
yeah given int64 is treated as radioactive :P