-
Notifications
You must be signed in to change notification settings - Fork 1k
Cortex-M backend: add quantized_activation op with LUT lowering for sigmoid/tanh/silu #19792
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
Draft
rascani
wants to merge
2
commits into
pytorch:main
Choose a base branch
from
rascani:cortex-m-quantized-activation
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.
Draft
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,131 @@ | ||
| /* | ||
| * 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 "cortex_m_ops_common.h" | ||
|
|
||
| #include <cstring> | ||
|
|
||
| #if defined(__ARM_FEATURE_MVE) && (__ARM_FEATURE_MVE & 1) | ||
| #include <arm_mve.h> | ||
| #define HAS_HELIUM_SIMD 1 | ||
| #endif | ||
|
|
||
| #if defined(ARM_MATH_DSP) && !defined(HAS_HELIUM_SIMD) | ||
| #include <arm_acle.h> | ||
| #define HAS_DSP_PACKED_LUT 1 | ||
| #endif | ||
|
|
||
| namespace cortex_m { | ||
| namespace native { | ||
|
|
||
| #if defined(HAS_DSP_PACKED_LUT) | ||
| // Local 4-byte read/write helpers. We deliberately don't include | ||
| // `arm_nnsupportfunctions.h` for the equivalent CMSIS-NN `arm_nn_read_s8x4_ia` | ||
| // / `arm_nn_write_s8x4_ia` -- the header is public but pulls in the entire | ||
| // CMSIS-NN support surface (~1500 lines) just for two memcpy wrappers. | ||
| static inline uint32_t read_u8x4_ia(const int8_t** in) { | ||
| uint32_t val; | ||
| std::memcpy(&val, *in, 4); | ||
| *in += 4; | ||
| return val; | ||
| } | ||
|
|
||
| static inline void write_u8x4_ia(int8_t** out, uint32_t val) { | ||
| std::memcpy(*out, &val, 4); | ||
| *out += 4; | ||
| } | ||
| #endif | ||
|
|
||
| // cppcheck-suppress unusedFunction | ||
| Tensor& quantized_activation_out( | ||
| KernelRuntimeContext& context, | ||
| const Tensor& input, | ||
| const Tensor& lut, | ||
| Tensor& out) { | ||
| ET_CHECK_MSG( | ||
| input.scalar_type() == ScalarType::Char, | ||
| "quantized_activation: input must be int8"); | ||
| ET_CHECK_MSG( | ||
| out.scalar_type() == ScalarType::Char, | ||
| "quantized_activation: output must be int8"); | ||
| ET_CHECK_MSG( | ||
| lut.scalar_type() == ScalarType::Char, | ||
| "quantized_activation: lut must be int8"); | ||
| ET_CHECK_MSG( | ||
| lut.numel() == 256, | ||
| "quantized_activation: lut must have 256 entries, got %" PRId64, | ||
| static_cast<int64_t>(lut.numel())); | ||
| ET_CHECK_MSG( | ||
| input.numel() == out.numel(), | ||
| "quantized_activation: input and output must have the same numel"); | ||
|
|
||
| const int8_t* in_data = input.const_data_ptr<int8_t>(); | ||
| const int8_t* lut_data = lut.const_data_ptr<int8_t>(); | ||
| int8_t* out_data = out.mutable_data_ptr<int8_t>(); | ||
|
|
||
| // The LUT is precomputed AoT from the input/output qparams and the | ||
| // activation function (sigmoid / tanh / silu / ...), so the kernel does not | ||
| // need to know which activation it is implementing. The signed int8 input | ||
| // is biased by 128 to use it as an unsigned [0, 255] table index. | ||
| const int64_t n = input.numel(); | ||
| int64_t i = 0; | ||
|
|
||
| #if defined(HAS_HELIUM_SIMD) | ||
| // M55/M85: 16 lanes per iteration. Reinterpret the int8 input as uint8 | ||
| // (bit-identical load), add 128 mod 256 to produce a uint8 LUT index, then | ||
| // gather-load the int8 result from the LUT. | ||
| for (; i + 15 < n; i += 16) { | ||
| uint8x16_t in_u8 = | ||
| vldrbq_u8(reinterpret_cast<const uint8_t*>(in_data + i)); | ||
| uint8x16_t idx = vaddq_n_u8(in_u8, 128); | ||
| int8x16_t result = vldrbq_gather_offset_s8(lut_data, idx); | ||
| vstrbq_s8(out_data + i, result); | ||
| } | ||
| #elif defined(HAS_DSP_PACKED_LUT) | ||
| // M4/M7 (DSP, no MVE): process 4 bytes per iteration. The DSP win comes from | ||
| // (a) folding 4 byte-loads into one word-load, (b) batching the +128 bias | ||
| // with `__uadd8`, and (c) folding 4 byte-stores into one word-store. The | ||
| // LUT lookups themselves still hit memory four times per word -- no DSP | ||
| // gather instruction exists on M-class. | ||
| const int8_t* in_ptr = in_data; | ||
| int8_t* out_ptr = out_data; | ||
| const int64_t word_iters = n >> 2; | ||
| for (int64_t w = 0; w < word_iters; ++w) { | ||
| const uint32_t in_word = read_u8x4_ia(&in_ptr); | ||
| const uint32_t idx_word = __uadd8(in_word, 0x80808080u); | ||
| const uint32_t out_word = | ||
| static_cast<uint32_t>(static_cast<uint8_t>(lut_data[idx_word & 0xFFu])) | | ||
| (static_cast<uint32_t>(static_cast<uint8_t>(lut_data[(idx_word >> 8) & 0xFFu])) | ||
| << 8) | | ||
| (static_cast<uint32_t>(static_cast<uint8_t>(lut_data[(idx_word >> 16) & 0xFFu])) | ||
| << 16) | | ||
| (static_cast<uint32_t>(static_cast<uint8_t>(lut_data[(idx_word >> 24) & 0xFFu])) | ||
| << 24); | ||
| write_u8x4_ia(&out_ptr, out_word); | ||
| } | ||
| i = word_iters << 2; | ||
| #endif | ||
|
|
||
| // 4x-unrolled scalar tail. On M-class cores without MVE or DSP the unroll | ||
| // lets the compiler issue independent LUT loads; on the MVE / DSP paths | ||
| // above this only runs for the < 16- (or < 4-) element remainder. | ||
| for (; i + 3 < n; i += 4) { | ||
| out_data[i + 0] = lut_data[static_cast<uint8_t>(in_data[i + 0] + 128)]; | ||
| out_data[i + 1] = lut_data[static_cast<uint8_t>(in_data[i + 1] + 128)]; | ||
| out_data[i + 2] = lut_data[static_cast<uint8_t>(in_data[i + 2] + 128)]; | ||
| out_data[i + 3] = lut_data[static_cast<uint8_t>(in_data[i + 3] + 128)]; | ||
| } | ||
| for (; i < n; ++i) { | ||
| out_data[i] = lut_data[static_cast<uint8_t>(in_data[i] + 128)]; | ||
| } | ||
|
|
||
| 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
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.