|
| 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 "fast_hadamard_transform.h" |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | + |
| 13 | +namespace executorch { |
| 14 | +void fast_hadamard_transform_symmetric_quantized_s16( |
| 15 | + int16_t* vec, |
| 16 | + int log2_vec_size) { |
| 17 | + if (log2_vec_size == 0) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const int vec_size = 1 << log2_vec_size; |
| 22 | + // We perform log2_vec_size rounds where each round's maximum output |
| 23 | + // is at most double the maximum input, so we can at most multiply |
| 24 | + // the maximum input by vec_size. Performing intermediate arithmetic |
| 25 | + // in 32-bit precision should prevent overflow, since 16 + |
| 26 | + // log2_vec_size should be much less than 32. |
| 27 | + auto tmp = std::make_unique<int32_t[]>(vec_size); |
| 28 | + std::copy(vec, vec + vec_size, tmp.get()); |
| 29 | + |
| 30 | + // Per the function-level comment in the header, we can ignore the |
| 31 | + // quantization scale, so we just delegate to the usual unnormalized |
| 32 | + // implementation. |
| 33 | + // NOTE: if we need this to be fast on CPU, we can use FFHT to |
| 34 | + // generate fht_uint32 similar to fht_float. |
| 35 | + internal::fast_hadamard_transform_unnormalized_simple_impl( |
| 36 | + tmp.get(), log2_vec_size); |
| 37 | + |
| 38 | + // Normalization step: divide by sqrt(1 << log2_vec_size). Similar |
| 39 | + // to fast_sqrt, if N is even, then the maximum-precision way |
| 40 | + // to do this is right-shift by log2_vec_size / 2. If N is odd, we |
| 41 | + // still do the right-shift, and then we have an extra division by |
| 42 | + // sqrt(2) that we perform by making use of a sufficiently accurate |
| 43 | + // rational approximation. (Our initial idea was to divide by sqrt(2) |
| 44 | + // by adjusting the quantization scale, but that would cause this |
| 45 | + // function to tend to increase the magnitude of the elements of |
| 46 | + // vec, which would resulting in clipping and therefore accuracy |
| 47 | + // loss, especially compounded over 30+ transformer layers.) |
| 48 | + const int log2_sqrt_vec_size = log2_vec_size / 2; |
| 49 | + constexpr int32_t qmin = -(1 << 15) + 1; |
| 50 | + constexpr int32_t qmax = -qmin; |
| 51 | + if (log2_vec_size % 2 != 0) { |
| 52 | + // 408 / 577 - 1.0 / sqrt(2) ~= 1.062e-0.6, which should be close enough. |
| 53 | + static const int32_t inv_sqrt_2_numerator = 408; |
| 54 | + static const int32_t inv_sqrt_2_denominator = 577; |
| 55 | + for (int ii = 0; ii < vec_size; ++ii) { |
| 56 | + const auto val_over_sqrt_vec_size = |
| 57 | + (tmp[ii] * inv_sqrt_2_numerator / inv_sqrt_2_denominator) >> |
| 58 | + log2_sqrt_vec_size; |
| 59 | + vec[ii] = std::clamp(val_over_sqrt_vec_size, qmin, qmax); |
| 60 | + } |
| 61 | + } else { |
| 62 | + for (int ii = 0; ii < vec_size; ++ii) { |
| 63 | + vec[ii] = std::clamp(tmp[ii] >> log2_sqrt_vec_size, qmin, qmax); |
| 64 | + } |
| 65 | + } |
| 66 | + return; |
| 67 | +} |
| 68 | +} // namespace executorch |
0 commit comments