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