|
| 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 | +#include <iostream> |
| 12 | +#include <random> |
| 13 | + |
| 14 | +#include <gtest/gtest.h> |
| 15 | + |
| 16 | +#include <executorch/extension/llm/custom_ops/spinquant/fast_hadamard_transform.h> |
| 17 | +#include <executorch/extension/llm/custom_ops/spinquant/test/fast_hadamard_transform_special_unstrided_cpu.h> |
| 18 | +#include <executorch/extension/llm/custom_ops/spinquant/third-party/FFHT/dumb_fht.h> |
| 19 | + |
| 20 | +namespace { |
| 21 | +void reference_fht_impl(float* buf, int n) { |
| 22 | + dumb_fht(buf, std::log2<int>(n)); |
| 23 | + const auto root_n = std::sqrt(n); |
| 24 | + for (int ii = 0; ii < n; ++ii) { |
| 25 | + buf[ii] /= root_n; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Alternate implementation of fast_hadamard_transform_28N to mutation |
| 30 | +// test against. Benchmarking suggests this one is slower, which is |
| 31 | +// why it's in the test and the strided implementation is in the |
| 32 | +// header. |
| 33 | +template <typename T> |
| 34 | +void fast_hadamard_transform_28N_with_transpose(T* vec, int log2_vec_size) { |
| 35 | + const int vec_size = (1 << log2_vec_size); |
| 36 | + for (int ii = 0; ii < 28; ++ii) { |
| 37 | + executorch::fast_hadamard_transform(&vec[ii * vec_size], log2_vec_size); |
| 38 | + } |
| 39 | + std::unique_ptr<T[]> transposed = std::make_unique<T[]>(28 * vec_size); |
| 40 | + for (int ii = 0; ii < 28; ++ii) { |
| 41 | + for (int jj = 0; jj < vec_size; ++jj) { |
| 42 | + transposed[jj * 28 + ii] = vec[ii * vec_size + jj]; |
| 43 | + } |
| 44 | + } |
| 45 | + for (int ii = 0; ii < vec_size; ++ii) { |
| 46 | + hadamard_mult_28(&transposed[ii * 28]); |
| 47 | + } |
| 48 | + for (int jj = 0; jj < vec_size; ++jj) { |
| 49 | + for (int ii = 0; ii < 28; ++ii) { |
| 50 | + vec[ii * vec_size + jj] = transposed[jj * 28 + ii]; |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +std::vector<float> randomFloats(int howMany) { |
| 56 | + std::random_device rd; |
| 57 | + std::mt19937 gen(rd()); |
| 58 | + std::normal_distribution<float> dist; |
| 59 | + std::vector<float> data(howMany); |
| 60 | + for (int ii = 0; ii < data.size(); ++ii) { |
| 61 | + data[ii] = dist(gen); |
| 62 | + } |
| 63 | + return data; |
| 64 | +} |
| 65 | +} // namespace |
| 66 | + |
| 67 | +TEST(FastHadamardTransformTest, SingleElement) { |
| 68 | + // FHT of a single element is a no-op. |
| 69 | + float data[1] = {42}; |
| 70 | + executorch::fast_hadamard_transform(data, 0); |
| 71 | + EXPECT_EQ(data[0], 42); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(FastHadamardTransformTest, LargerInput) { |
| 75 | + std::vector<float> data = randomFloats(4096); |
| 76 | + |
| 77 | + auto expected = data; |
| 78 | + reference_fht_impl(expected.data(), expected.size()); |
| 79 | + |
| 80 | + auto actual = data; |
| 81 | + executorch::fast_hadamard_transform(actual.data(), 12); |
| 82 | + |
| 83 | + for (int ii = 0; ii < expected.size(); ++ii) { |
| 84 | + EXPECT_FLOAT_EQ(actual[ii], expected[ii]); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +TEST(FastHadamardTransform28NTest, Basic) { |
| 89 | + std::vector<float> data = randomFloats(1024 * 28); |
| 90 | + |
| 91 | + auto expected = data; |
| 92 | + fast_hadamard_transform_28N_with_transpose(expected.data(), 10); |
| 93 | + |
| 94 | + auto actual = data; |
| 95 | + executorch::fast_hadamard_transform_28N(actual.data(), 10); |
| 96 | + |
| 97 | + for (int ii = 0; ii < actual.size(); ++ii) { |
| 98 | + EXPECT_FLOAT_EQ(actual[ii], expected[ii]); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +namespace { |
| 103 | +constexpr int32_t qmin = -(1 << 15) + 1; |
| 104 | +constexpr int32_t qmax = -qmin; |
| 105 | + |
| 106 | +int16_t quantize(float x, float scale) { |
| 107 | + float scaled = x / scale; |
| 108 | + // XXX: Supposed to round ties to even, but this is just test code. |
| 109 | + int32_t scaled_int = |
| 110 | + std::clamp((int32_t)std::lround<int32_t>(scaled), qmin, qmax); |
| 111 | + return static_cast<int16_t>(scaled_int); |
| 112 | +} |
| 113 | + |
| 114 | +template <typename T> |
| 115 | +std::vector<T> quantize(const std::vector<float>& data, float scale) { |
| 116 | + std::vector<T> result; |
| 117 | + result.reserve(data.size()); |
| 118 | + for (const float unquant : data) { |
| 119 | + result.push_back(quantize(unquant, scale)); |
| 120 | + } |
| 121 | + return result; |
| 122 | +} |
| 123 | + |
| 124 | +template <typename T> |
| 125 | +std::pair<std::vector<T>, float> quantize(const std::vector<float>& data) { |
| 126 | + auto [minIt, maxIt] = std::minmax_element(data.begin(), data.end()); |
| 127 | + float scale = (*maxIt - *minIt) / (qmax - qmin); |
| 128 | + return {quantize<T>(data, scale), scale}; |
| 129 | +} |
| 130 | + |
| 131 | +template <typename T> |
| 132 | +float dequantize(T x, float scale) { |
| 133 | + return x * scale; |
| 134 | +} |
| 135 | + |
| 136 | +template <typename T> |
| 137 | +std::vector<float> dequantize(const std::vector<T>& data, float scale) { |
| 138 | + static_assert(!std::is_same_v<T, float>); |
| 139 | + std::vector<float> result; |
| 140 | + result.reserve(data.size()); |
| 141 | + for (const T quant : data) { |
| 142 | + result.push_back(dequantize(quant, scale)); |
| 143 | + } |
| 144 | + return result; |
| 145 | +} |
| 146 | + |
| 147 | +#define EXPECT_CLOSE_IMPL(a, b, atol, rtol) \ |
| 148 | + EXPECT_LE(std::abs(a - b), atol + rtol * std::abs(b)) \ |
| 149 | + << "a: " << a << ", b: " << b |
| 150 | +#define EXPECT_CLOSE(a, b) EXPECT_CLOSE_IMPL(a, b, 2e-4, 1e-4) |
| 151 | + |
| 152 | +void testQuantizedFastHadamardTransform(int logN) { |
| 153 | + std::vector<float> data = randomFloats(1 << logN); |
| 154 | + |
| 155 | + auto [qdata, scale] = quantize<int16_t>(data); |
| 156 | + |
| 157 | + auto expected_unquant = dequantize(qdata, scale); |
| 158 | + reference_fht_impl(expected_unquant.data(), expected_unquant.size()); |
| 159 | + auto expected = quantize<int16_t>(expected_unquant, scale); |
| 160 | + |
| 161 | + auto actual = qdata; |
| 162 | + executorch::fast_hadamard_transform_symmetric_quantized_s16( |
| 163 | + actual.data(), logN); |
| 164 | + |
| 165 | + for (int ii = 0; ii < expected.size(); ++ii) { |
| 166 | + EXPECT_CLOSE( |
| 167 | + dequantize(actual[ii], scale), dequantize(expected[ii], scale)); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +} // namespace |
| 172 | + |
| 173 | +TEST(QuantizedFastHadamardTransformTest, Basic) { |
| 174 | + testQuantizedFastHadamardTransform(12); // 4096 |
| 175 | +} |
| 176 | + |
| 177 | +TEST(QuantizedFastHadamardTransformTest, OddLogN) { |
| 178 | + testQuantizedFastHadamardTransform(11); // 2048 |
| 179 | +} |
| 180 | + |
| 181 | +TEST(QuantizedFastHadamardTransform28NTest, Basic) { |
| 182 | + std::vector<float> data = randomFloats(1024 * 28); |
| 183 | + |
| 184 | + auto [qdata, scale] = quantize<int16_t>(data); |
| 185 | + |
| 186 | + auto expected_unquant = dequantize(qdata, scale); |
| 187 | + fast_hadamard_transform_28N_with_transpose(expected_unquant.data(), 10); |
| 188 | + auto expected = quantize<int16_t>(expected_unquant, scale); |
| 189 | + |
| 190 | + auto actual = qdata; |
| 191 | + executorch::fast_hadamard_transform_symmetric_quantized_s16_28N( |
| 192 | + actual.data(), 10); |
| 193 | + |
| 194 | + for (int ii = 0; ii < expected.size(); ++ii) { |
| 195 | + std::cerr << "element " << ii << ": actual: " << actual[ii] |
| 196 | + << ", expected: " << expected[ii] << std::endl; |
| 197 | + EXPECT_CLOSE( |
| 198 | + dequantize(actual[ii], scale), dequantize(expected[ii], scale)); |
| 199 | + } |
| 200 | +} |
0 commit comments