|
| 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 <algorithm> |
| 10 | +#include <cmath> |
| 11 | + |
| 12 | +#include "cortex_m_ops_common.h" |
| 13 | + |
| 14 | +namespace cortex_m { |
| 15 | +namespace native { |
| 16 | +namespace { |
| 17 | + |
| 18 | +template <typename T> |
| 19 | +void quantized_div_typed( |
| 20 | + const Tensor& input1, |
| 21 | + const int32_t zp1, |
| 22 | + const Tensor& input2, |
| 23 | + const int32_t zp2, |
| 24 | + const int32_t out_zp, |
| 25 | + const float effective_scale, |
| 26 | + Tensor& out) { |
| 27 | + const T* input1_ptr = input1.data_ptr<T>(); |
| 28 | + const T* input2_ptr = input2.data_ptr<T>(); |
| 29 | + T* out_ptr = out.mutable_data_ptr<T>(); |
| 30 | + |
| 31 | + // Saturation bounds kept in float: a denominator quantized to a single step |
| 32 | + // off its zero point yields a very large quotient, so rounding and clamping |
| 33 | + // in float avoids overflowing int32 before the saturating cast below. |
| 34 | + constexpr float kActivationMin = |
| 35 | + static_cast<float>(std::numeric_limits<T>::min()); |
| 36 | + constexpr float kActivationMax = |
| 37 | + static_cast<float>(std::numeric_limits<T>::max()); |
| 38 | + |
| 39 | + const int64_t num_elements = out.numel(); |
| 40 | + for (int64_t i = 0; i < num_elements; ++i) { |
| 41 | + const int32_t numerator = static_cast<int32_t>(input1_ptr[i]) - zp1; |
| 42 | + const int32_t denominator = static_cast<int32_t>(input2_ptr[i]) - zp2; |
| 43 | + |
| 44 | + // A zero-point-corrected denominator of 0 has no representable reciprocal; |
| 45 | + // emit a 0 quotient so the op stays total (callers keep divisors off the |
| 46 | + // zero point). |
| 47 | + const float quotient = (denominator != 0) |
| 48 | + ? static_cast<float>(numerator) / static_cast<float>(denominator) |
| 49 | + : 0.0f; |
| 50 | + |
| 51 | + const float scaled = |
| 52 | + std::round(quotient * effective_scale) + static_cast<float>(out_zp); |
| 53 | + const float clamped = |
| 54 | + std::max(kActivationMin, std::min(kActivationMax, scaled)); |
| 55 | + out_ptr[i] = static_cast<T>(clamped); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +} // namespace |
| 60 | + |
| 61 | +using KernelRuntimeContext = torch::executor::KernelRuntimeContext; |
| 62 | + |
| 63 | +// CMSIS-NN has no integer elementwise-division primitive, so the quotient is |
| 64 | +// evaluated in float. Unlike quantized_mul/add there is no fixed-point path to |
| 65 | +// feed, so the effective scale (scale_in1 / (scale_in2 * scale_out)) is |
| 66 | +// computed AoT and carried directly as a float rather than as a |
| 67 | +// multiplier/shift pair. Both int8 and int16 activations are supported. |
| 68 | +// cppcheck-suppress unusedFunction |
| 69 | +Tensor& quantized_div_out( |
| 70 | + KernelRuntimeContext& context, |
| 71 | + const Tensor& input1, |
| 72 | + const int64_t input1_zero_point, |
| 73 | + const Tensor& input2, |
| 74 | + const int64_t input2_zero_point, |
| 75 | + const int64_t output_zero_point, |
| 76 | + const double output_scale, |
| 77 | + Tensor& out) { |
| 78 | + const ScalarType dtype = out.scalar_type(); |
| 79 | + if (dtype != ScalarType::Char && dtype != ScalarType::Short) { |
| 80 | + ET_LOG( |
| 81 | + Error, |
| 82 | + "quantized_div: only int8 and int16 are supported, got %d", |
| 83 | + static_cast<int>(dtype)); |
| 84 | + context.fail(Error::InvalidArgument); |
| 85 | + return out; |
| 86 | + } |
| 87 | + |
| 88 | + // Division is not commutative, so channel broadcasting (which relies on |
| 89 | + // operand swapping in quantized_mul) is unsupported: require equal shapes. |
| 90 | + validate_cmsis_nn_tensor_requirements( |
| 91 | + input1, |
| 92 | + input2, |
| 93 | + out, |
| 94 | + dtype, |
| 95 | + /*require_channels_last=*/false, |
| 96 | + /*require_same_sizes=*/true); |
| 97 | + |
| 98 | + // The rescale is carried entirely by effective_scale (float), so the shared |
| 99 | + // validator only needs to sanity-check the three zero points; pass identity |
| 100 | + // multiplier/shift for each operand. |
| 101 | + const int32_t kIdentityMultiplier(/*value=*/1); |
| 102 | + const int32_t kZeroShift(/*value=*/0); |
| 103 | + validate_quantization_params( |
| 104 | + input1_zero_point, |
| 105 | + kIdentityMultiplier, |
| 106 | + kZeroShift, |
| 107 | + input2_zero_point, |
| 108 | + kIdentityMultiplier, |
| 109 | + kZeroShift, |
| 110 | + output_zero_point, |
| 111 | + kIdentityMultiplier, |
| 112 | + kZeroShift); |
| 113 | + |
| 114 | + const int32_t zp1 = static_cast<int32_t>(input1_zero_point); |
| 115 | + const int32_t zp2 = static_cast<int32_t>(input2_zero_point); |
| 116 | + const int32_t out_zp = static_cast<int32_t>(output_zero_point); |
| 117 | + |
| 118 | + const float effective_scale = static_cast<float>(output_scale); |
| 119 | + |
| 120 | + if (dtype == ScalarType::Char) { |
| 121 | + quantized_div_typed<int8_t>( |
| 122 | + input1, zp1, input2, zp2, out_zp, effective_scale, out); |
| 123 | + } else { |
| 124 | + quantized_div_typed<int16_t>( |
| 125 | + input1, zp1, input2, zp2, out_zp, effective_scale, out); |
| 126 | + } |
| 127 | + |
| 128 | + return out; |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace native |
| 132 | +} // namespace cortex_m |
0 commit comments