|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "cortex_m_ops_common.h" |
| 10 | + |
| 11 | +#include <cstring> |
| 12 | + |
| 13 | +#if defined(__ARM_FEATURE_MVE) && (__ARM_FEATURE_MVE & 1) |
| 14 | +#include <arm_mve.h> |
| 15 | +#define HAS_HELIUM_SIMD 1 |
| 16 | +#endif |
| 17 | + |
| 18 | +#if defined(ARM_MATH_DSP) && !defined(HAS_HELIUM_SIMD) |
| 19 | +#include <arm_acle.h> |
| 20 | +#define HAS_DSP_PACKED_LUT 1 |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace cortex_m { |
| 24 | +namespace native { |
| 25 | + |
| 26 | +#if defined(HAS_DSP_PACKED_LUT) |
| 27 | +// Local 4-byte read/write helpers. We deliberately don't include |
| 28 | +// `arm_nnsupportfunctions.h` for the equivalent CMSIS-NN `arm_nn_read_s8x4_ia` |
| 29 | +// / `arm_nn_write_s8x4_ia` -- the header is public but pulls in the entire |
| 30 | +// CMSIS-NN support surface (~1500 lines) just for two memcpy wrappers. |
| 31 | +static inline uint32_t read_u8x4_ia(const int8_t** in) { |
| 32 | + uint32_t val; |
| 33 | + std::memcpy(&val, *in, 4); |
| 34 | + *in += 4; |
| 35 | + return val; |
| 36 | +} |
| 37 | + |
| 38 | +static inline void write_u8x4_ia(int8_t** out, uint32_t val) { |
| 39 | + std::memcpy(*out, &val, 4); |
| 40 | + *out += 4; |
| 41 | +} |
| 42 | +#endif |
| 43 | + |
| 44 | +// cppcheck-suppress unusedFunction |
| 45 | +Tensor& quantized_activation_out( |
| 46 | + KernelRuntimeContext& /*context*/, |
| 47 | + const Tensor& input, |
| 48 | + const Tensor& lut, |
| 49 | + Tensor& out) { |
| 50 | + ET_CHECK_MSG( |
| 51 | + input.scalar_type() == ScalarType::Char, |
| 52 | + "quantized_activation: input must be int8"); |
| 53 | + ET_CHECK_MSG( |
| 54 | + out.scalar_type() == ScalarType::Char, |
| 55 | + "quantized_activation: output must be int8"); |
| 56 | + ET_CHECK_MSG( |
| 57 | + lut.scalar_type() == ScalarType::Char, |
| 58 | + "quantized_activation: lut must be int8"); |
| 59 | + ET_CHECK_MSG( |
| 60 | + lut.numel() == 256, |
| 61 | + "quantized_activation: lut must have 256 entries, got %" PRId64, |
| 62 | + static_cast<int64_t>(lut.numel())); |
| 63 | + ET_CHECK_MSG( |
| 64 | + input.numel() == out.numel(), |
| 65 | + "quantized_activation: input and output must have the same numel"); |
| 66 | + |
| 67 | + const int8_t* in_data = input.const_data_ptr<int8_t>(); |
| 68 | + const int8_t* lut_data = lut.const_data_ptr<int8_t>(); |
| 69 | + int8_t* out_data = out.mutable_data_ptr<int8_t>(); |
| 70 | + |
| 71 | + // The LUT is precomputed AoT from the input/output qparams and the |
| 72 | + // activation function (sigmoid / tanh / silu / ...), so the kernel does not |
| 73 | + // need to know which activation it is implementing. The signed int8 input |
| 74 | + // is biased by 128 to use it as an unsigned [0, 255] table index. |
| 75 | + const int64_t n = input.numel(); |
| 76 | + int64_t i = 0; |
| 77 | + |
| 78 | +#if defined(HAS_HELIUM_SIMD) |
| 79 | + // M55/M85: 16 lanes per iteration. Reinterpret the int8 input as uint8 |
| 80 | + // (bit-identical load), add 128 mod 256 to produce a uint8 LUT index, then |
| 81 | + // gather-load the int8 result from the LUT. |
| 82 | + for (; i + 15 < n; i += 16) { |
| 83 | + uint8x16_t in_u8 = vldrbq_u8(reinterpret_cast<const uint8_t*>(in_data + i)); |
| 84 | + uint8x16_t idx = vaddq_n_u8(in_u8, 128); |
| 85 | + int8x16_t result = vldrbq_gather_offset_s8(lut_data, idx); |
| 86 | + vstrbq_s8(out_data + i, result); |
| 87 | + } |
| 88 | +#elif defined(HAS_DSP_PACKED_LUT) |
| 89 | + // M4/M7 (DSP, no MVE): process 4 bytes per iteration. The DSP win comes from |
| 90 | + // (a) folding 4 byte-loads into one word-load, (b) batching the +128 bias |
| 91 | + // with `__uadd8`, and (c) folding 4 byte-stores into one word-store. The |
| 92 | + // LUT lookups themselves still hit memory four times per word -- no DSP |
| 93 | + // gather instruction exists on M-class. |
| 94 | + const int8_t* in_ptr = in_data; |
| 95 | + int8_t* out_ptr = out_data; |
| 96 | + const int64_t word_iters = n >> 2; |
| 97 | + for (int64_t w = 0; w < word_iters; ++w) { |
| 98 | + const uint32_t in_word = read_u8x4_ia(&in_ptr); |
| 99 | + const uint32_t idx_word = __uadd8(in_word, 0x80808080u); |
| 100 | + const uint32_t out_word = static_cast<uint32_t>(static_cast<uint8_t>( |
| 101 | + lut_data[idx_word & 0xFFu])) | |
| 102 | + (static_cast<uint32_t>( |
| 103 | + static_cast<uint8_t>(lut_data[(idx_word >> 8) & 0xFFu])) |
| 104 | + << 8) | |
| 105 | + (static_cast<uint32_t>( |
| 106 | + static_cast<uint8_t>(lut_data[(idx_word >> 16) & 0xFFu])) |
| 107 | + << 16) | |
| 108 | + (static_cast<uint32_t>( |
| 109 | + static_cast<uint8_t>(lut_data[(idx_word >> 24) & 0xFFu])) |
| 110 | + << 24); |
| 111 | + write_u8x4_ia(&out_ptr, out_word); |
| 112 | + } |
| 113 | + i = word_iters << 2; |
| 114 | +#endif |
| 115 | + |
| 116 | + // 4x-unrolled scalar tail. On M-class cores without MVE or DSP the unroll |
| 117 | + // lets the compiler issue independent LUT loads; on the MVE / DSP paths |
| 118 | + // above this only runs for the < 16- (or < 4-) element remainder. |
| 119 | + for (; i + 3 < n; i += 4) { |
| 120 | + out_data[i + 0] = lut_data[static_cast<uint8_t>(in_data[i + 0] + 128)]; |
| 121 | + out_data[i + 1] = lut_data[static_cast<uint8_t>(in_data[i + 1] + 128)]; |
| 122 | + out_data[i + 2] = lut_data[static_cast<uint8_t>(in_data[i + 2] + 128)]; |
| 123 | + out_data[i + 3] = lut_data[static_cast<uint8_t>(in_data[i + 3] + 128)]; |
| 124 | + } |
| 125 | + for (; i < n; ++i) { |
| 126 | + out_data[i] = lut_data[static_cast<uint8_t>(in_data[i] + 128)]; |
| 127 | + } |
| 128 | + |
| 129 | + return out; |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace native |
| 133 | +} // namespace cortex_m |
0 commit comments