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