-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add quantized_div op (#21294) #21294
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
Open
3l1
wants to merge
1
commit into
main
Choose a base branch
from
export-D113440315
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
|
|
||
| #include "cortex_m_ops_common.h" | ||
|
|
||
| namespace cortex_m { | ||
| namespace native { | ||
| namespace { | ||
|
|
||
| template <typename T> | ||
| void quantized_div_typed( | ||
| const Tensor& input1, | ||
| const int32_t zp1, | ||
| const Tensor& input2, | ||
| const int32_t zp2, | ||
| const int32_t out_zp, | ||
| const float effective_scale, | ||
| Tensor& out) { | ||
| const T* input1_ptr = input1.data_ptr<T>(); | ||
| const T* input2_ptr = input2.data_ptr<T>(); | ||
| T* out_ptr = out.mutable_data_ptr<T>(); | ||
|
|
||
| constexpr int32_t kActivationMin = std::numeric_limits<T>::min(); | ||
| constexpr int32_t kActivationMax = std::numeric_limits<T>::max(); | ||
|
|
||
| const int64_t num_elements = out.numel(); | ||
| for (int64_t i = 0; i < num_elements; ++i) { | ||
| const int32_t numerator = static_cast<int32_t>(input1_ptr[i]) - zp1; | ||
| const int32_t denominator = static_cast<int32_t>(input2_ptr[i]) - zp2; | ||
|
|
||
| const float quotient = (denominator != 0) | ||
| ? static_cast<float>(numerator) / static_cast<float>(denominator) | ||
| : 0.0f; | ||
|
|
||
| int32_t result = | ||
| static_cast<int32_t>(std::round(quotient * effective_scale)) + out_zp; | ||
| result = std::max(kActivationMin, std::min(kActivationMax, result)); | ||
| out_ptr[i] = static_cast<T>(result); | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| using KernelRuntimeContext = torch::executor::KernelRuntimeContext; | ||
|
|
||
| // CMSIS-NN has no integer elementwise-division primitive, so the quotient is | ||
| // evaluated in float. The effective scale (scale_in1 / (scale_in2 * scale_out)) | ||
| // is carried in the AoT-computed output_multiplier/output_shift and | ||
| // reconstructed here, mirroring the softmax kernel's fixed-point-to-float | ||
| // reconstruction. Both int8 and int16 activations are supported. | ||
| // cppcheck-suppress unusedFunction | ||
| Tensor& quantized_div_out( | ||
| KernelRuntimeContext& context, | ||
| const Tensor& input1, | ||
| const int64_t input1_zero_point, | ||
| const Tensor& input2, | ||
| const int64_t input2_zero_point, | ||
| const int64_t output_zero_point, | ||
| const int64_t output_multiplier, | ||
| const int64_t output_shift, | ||
| Tensor& out) { | ||
| const ScalarType dtype = out.scalar_type(); | ||
| if (dtype != ScalarType::Char && dtype != ScalarType::Short) { | ||
| ET_LOG( | ||
| Error, | ||
| "quantized_div: only int8 and int16 are supported, got %d", | ||
| static_cast<int>(dtype)); | ||
| context.fail(Error::InvalidArgument); | ||
| return out; | ||
| } | ||
|
|
||
| // Division is not commutative, so channel broadcasting (which relies on | ||
| // operand swapping in quantized_mul) is unsupported: require equal shapes. | ||
| validate_cmsis_nn_tensor_requirements( | ||
| input1, | ||
| input2, | ||
| out, | ||
| dtype, | ||
| /*require_channels_last=*/false, | ||
| /*require_same_sizes=*/true); | ||
|
|
||
| const int32_t kIdentityMultiplier(/*value=*/1); | ||
| const int32_t kZeroShift(/*value=*/0); | ||
| validate_quantization_params( | ||
| input1_zero_point, | ||
| kIdentityMultiplier, | ||
| kZeroShift, | ||
| input2_zero_point, | ||
| kIdentityMultiplier, | ||
| kZeroShift, | ||
| output_zero_point, | ||
| output_multiplier, | ||
| output_shift); | ||
|
|
||
| const int32_t zp1 = static_cast<int32_t>(input1_zero_point); | ||
| const int32_t zp2 = static_cast<int32_t>(input2_zero_point); | ||
| const int32_t out_zp = static_cast<int32_t>(output_zero_point); | ||
|
|
||
| const float effective_scale = std::ldexp( | ||
| static_cast<float>(output_multiplier) / static_cast<float>(1LL << 31), | ||
| static_cast<int>(output_shift)); | ||
|
|
||
| if (dtype == ScalarType::Char) { | ||
| quantized_div_typed<int8_t>( | ||
| input1, zp1, input2, zp2, out_zp, effective_scale, out); | ||
| } else { | ||
| quantized_div_typed<int16_t>( | ||
| input1, zp1, input2, zp2, out_zp, effective_scale, out); | ||
| } | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| } // namespace native | ||
| } // namespace cortex_m | ||
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
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
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.
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.
If we're not worried about performance here, we might want to keep this and the clamp in float to avoid any risk of exceeding int32 bounds.