|
| 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 <executorch/runtime/kernel/kernel_includes.h> |
| 10 | + |
| 11 | +#include <vector> |
| 12 | +extern "C" { |
| 13 | +#include "Include/arm_nnfunctions.h" |
| 14 | +} |
| 15 | + |
| 16 | +namespace cortex_m { |
| 17 | +namespace native { |
| 18 | + |
| 19 | +using Tensor = torch::executor::Tensor; |
| 20 | +using ScalarType = executorch::aten::ScalarType; |
| 21 | +using Scalar = torch::executor::Scalar; |
| 22 | +using KernelRuntimeContext = torch::executor::KernelRuntimeContext; |
| 23 | + |
| 24 | +Tensor& quantized_add_out( |
| 25 | + KernelRuntimeContext& context, |
| 26 | + const Tensor& input1_int8, |
| 27 | + const Scalar& input1_zero_point, |
| 28 | + const Scalar& input1_multiplier, |
| 29 | + const Scalar& input1_shift, |
| 30 | + const Tensor& input2_int8, |
| 31 | + const Scalar& input2_zero_point, |
| 32 | + const Scalar& input2_multiplier, |
| 33 | + const Scalar& input2_shift, |
| 34 | + const Scalar& output_zero_point, |
| 35 | + const Scalar& output_multiplier, |
| 36 | + const Scalar& output_shift, |
| 37 | + Tensor& out) { |
| 38 | + |
| 39 | + ET_CHECK_MSG(input1_int8.scalar_type() == ScalarType::Char, "Input1 must be int8"); |
| 40 | + ET_CHECK_MSG(input2_int8.scalar_type() == ScalarType::Char, "Input2 must be int8"); |
| 41 | + ET_CHECK_MSG(out.scalar_type() == ScalarType::Char, "Output must be int8"); |
| 42 | + |
| 43 | + // Check tensor shapes are compatible |
| 44 | + ET_CHECK_SAME_SHAPE_AND_DTYPE2(input1_int8, input2_int8); |
| 45 | + |
| 46 | + ET_LOG(Info, "quantized_add_out: input1_int8.sizes() = %zu", input1_int8.sizes().size()); |
| 47 | + |
| 48 | + // 🔧 FIX: Use template types that ExecutorTorch definitely provides |
| 49 | + // Use to<int64_t>() and to<double>() which are commonly instantiated |
| 50 | + int64_t zp1 = input1_zero_point.to<int64_t>(); |
| 51 | + int32_t input1_mult = static_cast<int32_t>(input1_multiplier.to<int64_t>()); |
| 52 | + int input1_shift_val = static_cast<int>(input1_shift.to<int64_t>()); |
| 53 | + |
| 54 | + int64_t zp2 = input2_zero_point.to<int64_t>(); |
| 55 | + int32_t input2_mult = static_cast<int32_t>(input2_multiplier.to<int64_t>()); |
| 56 | + int input2_shift_val = static_cast<int>(input2_shift.to<int64_t>()); |
| 57 | + |
| 58 | + int64_t out_zp = output_zero_point.to<int64_t>(); |
| 59 | + int32_t output_mult = static_cast<int32_t>(output_multiplier.to<int64_t>()); |
| 60 | + int output_shift_val = static_cast<int>(output_shift.to<int64_t>()); |
| 61 | + |
| 62 | + // Left shift to maximize precision (tune as needed) |
| 63 | + const int32_t left_shift = 20; |
| 64 | + const int32_t activation_min = std::numeric_limits<int8_t>::min(); |
| 65 | + const int32_t activation_max = std::numeric_limits<int8_t>::max(); |
| 66 | + |
| 67 | + // Resize output tensor to match input shape |
| 68 | + auto err = torch::executor::resize_tensor(out, input1_int8.sizes()); |
| 69 | + if (err != executorch::runtime::Error::Ok) { |
| 70 | + ET_LOG(Error, "quantized_add_out: resize_tensor failed with error code [%d]", static_cast<int>(err)); |
| 71 | + std::memset(out.mutable_data_ptr<int8_t>(), 0, out.nbytes()); |
| 72 | + return out; |
| 73 | + } |
| 74 | + |
| 75 | + ET_LOG(Info, "Using AoT-computed parameters: input1[mult=%d, shift=%d], input2[mult=%d, shift=%d], output[mult=%d, shift=%d]", |
| 76 | + input1_mult, input1_shift_val, input2_mult, input2_shift_val, output_mult, output_shift_val); |
| 77 | + |
| 78 | + // Call CMSIS-NN kernel with precomputed parameters |
| 79 | + arm_cmsis_nn_status status = arm_elementwise_add_s8( |
| 80 | + input1_int8.const_data_ptr<int8_t>(), |
| 81 | + input2_int8.const_data_ptr<int8_t>(), |
| 82 | + static_cast<int32_t>(zp1), |
| 83 | + input1_mult, |
| 84 | + input1_shift_val, |
| 85 | + static_cast<int32_t>(zp2), |
| 86 | + input2_mult, |
| 87 | + input2_shift_val, |
| 88 | + left_shift, |
| 89 | + out.mutable_data_ptr<int8_t>(), |
| 90 | + static_cast<int32_t>(out_zp), |
| 91 | + output_mult, |
| 92 | + output_shift_val, |
| 93 | + static_cast<int32_t>(out.numel()), |
| 94 | + activation_min, |
| 95 | + activation_max |
| 96 | + ); |
| 97 | + |
| 98 | + if (status != ARM_CMSIS_NN_SUCCESS) { |
| 99 | + ET_LOG(Error, "quantized_add_out: arm_elementwise_add_s8 failed with status [%d]", status); |
| 100 | + std::memset(out.mutable_data_ptr<int8_t>(), 0, out.nbytes()); |
| 101 | + } else { |
| 102 | + ET_LOG(Info, "quantized_add_out: Successfully completed with AoT-computed parameters! 🎯"); |
| 103 | + } |
| 104 | + |
| 105 | + return out; |
| 106 | +} |
| 107 | + |
| 108 | +// Stub Implementation: Non-out variant for compatibility (functional variant) |
| 109 | +// EXIR/ExecuTorch runs an out-variant pass that converts |
| 110 | +// .default operations to .out variants before memory planning. |
| 111 | +// In the pass we are calling quantized_add's default variant |
| 112 | +// but ExecuTorch's kernel dispatch mechanism will end up calling the out variant. |
| 113 | +// This stub is to make sure that compiler doesn't complain. |
| 114 | +Tensor quantized_add( |
| 115 | + KernelRuntimeContext& context, |
| 116 | + const Tensor& input1_int8, |
| 117 | + const Scalar& input1_zero_point, |
| 118 | + const Scalar& input1_multiplier, |
| 119 | + const Scalar& input1_shift, |
| 120 | + const Tensor& input2_int8, |
| 121 | + const Scalar& input2_zero_point, |
| 122 | + const Scalar& input2_multiplier, |
| 123 | + const Scalar& input2_shift, |
| 124 | + const Scalar& output_zero_point, |
| 125 | + const Scalar& output_multiplier, |
| 126 | + const Scalar& output_shift) { |
| 127 | + ET_LOG(Info, "quantized_add: input1_int8.sizes() = %zu", input1_int8.sizes().size()); |
| 128 | + return const_cast<Tensor&>(input1_int8); // to make compiler happy |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace native |
| 132 | +} // namespace cortex_m |
0 commit comments