|
| 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 | +// (c) Meta Platforms, Inc. and affiliates. |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <cassert> |
| 13 | +#include <cmath> |
| 14 | +#include <cstdint> |
| 15 | + |
| 16 | +#include "fast_hadamard_transform_special.h" |
| 17 | + |
| 18 | +namespace executorch { |
| 19 | +namespace internal { |
| 20 | + |
| 21 | +// Square root of 1 << log2_n. |
| 22 | +template <typename T> |
| 23 | +T fast_sqrt_of_power_of_2(int log2_n) { |
| 24 | + // The square root of 2**N is, by definition, 2**(N/2), which is |
| 25 | + // trivial to compute for even N using a left shift. |
| 26 | + // |
| 27 | + // For odd N, 2**(N/2) = 2**(floor(N/2) + 1/2) |
| 28 | + // = 2**(floor(N/2)) * (2 ** (1/2)) |
| 29 | + // = 2**(floor(N/2)) * sqrt(2) |
| 30 | + // which is again fast to compute. |
| 31 | + return T(1 << (log2_n / 2)) * ((log2_n % 2) ? T(std::sqrt(2)) : T(1)); |
| 32 | +} |
| 33 | + |
| 34 | +template <typename T> |
| 35 | +void normalize_after_fht(T* out, int log2_vec_size) { |
| 36 | + const T inv_sqrt = T(1) / fast_sqrt_of_power_of_2<T>(log2_vec_size); |
| 37 | + const int vec_size = 1 << log2_vec_size; |
| 38 | + for (int ii = 0; ii < vec_size; ++ii) { |
| 39 | + out[ii] *= inv_sqrt; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +template <typename T> |
| 44 | +void fast_hadamard_transform_simple_impl(T* vec, int log2_vec_size) { |
| 45 | + if (log2_vec_size == 0) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + int step = 1; |
| 50 | + const auto vec_size = 1 << log2_vec_size; |
| 51 | + while (step < vec_size) { |
| 52 | + for (int ii = 0; ii < vec_size; ii += step * 2) { |
| 53 | + for (int jj = ii; jj < ii + step; ++jj) { |
| 54 | + auto x = vec[jj]; |
| 55 | + auto y = vec[jj + step]; |
| 56 | + vec[jj] = x + y; |
| 57 | + vec[jj + step] = x - y; |
| 58 | + } |
| 59 | + } |
| 60 | + step *= 2; |
| 61 | + } |
| 62 | + |
| 63 | + normalize_after_fht(vec, log2_vec_size); |
| 64 | +} |
| 65 | + |
| 66 | +} // namespace internal |
| 67 | + |
| 68 | +// Compute the fast Walsh-Hadamard transform |
| 69 | +// (https://en.wikipedia.org/wiki/Fast_Walsh%E2%80%93Hadamard_transform) |
| 70 | +// of vec, which must be of length (1 << log2_vec_size). |
| 71 | +template <typename T> |
| 72 | +void fast_hadamard_transform(T* vec, int log2_vec_size) { |
| 73 | + internal::fast_hadamard_transform_simple_impl(vec, log2_vec_size); |
| 74 | +} |
| 75 | + |
| 76 | +// Like fast_hadamard_transform, but vec must be of length 28 * (1 << |
| 77 | +// log2_vec_size) and the transform is computed by interpreting vec as |
| 78 | +// a (28, 1 << log2_vec_size) matrix and performing 28 FHTs, followed |
| 79 | +// by (1 << log2_vec_size) multiplications by a particular Hadamard |
| 80 | +// matrix of size 28x28 (see special_hadamard_code_gen.py for the |
| 81 | +// exact matrix). |
| 82 | +template <typename T> |
| 83 | +void fast_hadamard_transform_28N(T* vec, int log2_vec_size) { |
| 84 | + const int vec_size = (1 << log2_vec_size); |
| 85 | + for (int ii = 0; ii < 28; ++ii) { |
| 86 | + fast_hadamard_transform(&vec[ii * vec_size], log2_vec_size); |
| 87 | + } |
| 88 | + for (int ii = 0; ii < vec_size; ++ii) { |
| 89 | + hadamard_mult_28_strided(&vec[ii], vec_size); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +} // namespace executorch |
0 commit comments