| 
 | 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 <executorch/extension/llm/custom_ops/op_fast_hadamard_transform.h>  | 
 | 10 | +#include <executorch/extension/llm/custom_ops/spinquant/test/fast_hadamard_transform_test_impl.h>  | 
 | 11 | +#include <executorch/kernels/test/TestUtil.h>  | 
 | 12 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>  | 
 | 13 | + | 
 | 14 | +#include <gtest/gtest.h>  | 
 | 15 | + | 
 | 16 | +#include <cmath>  | 
 | 17 | + | 
 | 18 | +using exec_aten::Tensor;  | 
 | 19 | + | 
 | 20 | +using executorch::runtime::testing::fast_hadamard_transform_28N_with_transpose;  | 
 | 21 | +using executorch::runtime::testing::random_floats;  | 
 | 22 | +using executorch::runtime::testing::reference_fht_impl;  | 
 | 23 | + | 
 | 24 | +namespace {  | 
 | 25 | +Tensor& fast_hadamard_transform_nocontext(const Tensor& vec, Tensor& out) {  | 
 | 26 | +  exec_aten::RuntimeContext context;  | 
 | 27 | +  return torch::executor::native::fast_hadamard_transform_out(  | 
 | 28 | +      context, vec, out);  | 
 | 29 | +}  | 
 | 30 | +} // namespace  | 
 | 31 | + | 
 | 32 | +TEST(OpFastHadamardTransformTest, EmptyInput) {  | 
 | 33 | +  torch::executor::testing::TensorFactory<exec_aten::ScalarType::Float> tfFloat;  | 
 | 34 | +  auto vec = tfFloat.zeros({0});  | 
 | 35 | +  auto out = tfFloat.zeros({0});  | 
 | 36 | +  auto result = fast_hadamard_transform_nocontext(vec, out);  | 
 | 37 | +  EXPECT_EQ(result.numel(), 0);  | 
 | 38 | +}  | 
 | 39 | + | 
 | 40 | +TEST(OpFastHadamardTransformTest, SingleElementInput) {  | 
 | 41 | +  torch::executor::testing::TensorFactory<exec_aten::ScalarType::Float> tfFloat;  | 
 | 42 | +  auto vec = tfFloat.ones({1});  | 
 | 43 | +  auto out = tfFloat.zeros({1});  | 
 | 44 | +  auto result = fast_hadamard_transform_nocontext(vec, out);  | 
 | 45 | +  EXPECT_EQ(result.numel(), 1);  | 
 | 46 | +  // FHT of a single element is a no-op.  | 
 | 47 | +  EXPECT_EQ(result.const_data_ptr<float>()[0], 1);  | 
 | 48 | +}  | 
 | 49 | + | 
 | 50 | +TEST(OpFastHadamardTransformTest, FourKInput) {  | 
 | 51 | +  torch::executor::testing::TensorFactory<exec_aten::ScalarType::Float> tfFloat;  | 
 | 52 | +  std::vector<float> data = random_floats(4096);  | 
 | 53 | +  auto vec = tfFloat.make({4096}, data);  | 
 | 54 | +  auto out = tfFloat.zeros({4096});  | 
 | 55 | +  auto result = fast_hadamard_transform_nocontext(vec, out);  | 
 | 56 | + | 
 | 57 | +  std::vector<float> reference_result = data;  | 
 | 58 | +  reference_fht_impl(reference_result.data(), reference_result.size());  | 
 | 59 | + | 
 | 60 | +  const float* const result_data = result.const_data_ptr<float>();  | 
 | 61 | +  for (int ii = 0; ii < data.size(); ++ii) {  | 
 | 62 | +    EXPECT_FLOAT_EQ(result_data[ii], reference_result[ii]);  | 
 | 63 | +  }  | 
 | 64 | +}  | 
 | 65 | + | 
 | 66 | +TEST(OpFastHadamardTransformTest, MultipleRows) {  | 
 | 67 | +  torch::executor::testing::TensorFactory<exec_aten::ScalarType::Float> tfFloat;  | 
 | 68 | +  std::vector<float> data = random_floats(8 * 8 * 8);  | 
 | 69 | +  auto mat = tfFloat.make({8, 8, 8}, data);  | 
 | 70 | +  auto out = tfFloat.zeros({8, 8, 8});  | 
 | 71 | + | 
 | 72 | +  auto result = fast_hadamard_transform_nocontext(mat, out);  | 
 | 73 | + | 
 | 74 | +  std::vector<float> reference_result = data;  | 
 | 75 | +  for (int ii = 0; ii < 8; ++ii) {  | 
 | 76 | +    for (int jj = 0; jj < 8; ++jj) {  | 
 | 77 | +      reference_fht_impl(&reference_result[ii * 64 + jj * 8], 8);  | 
 | 78 | +    }  | 
 | 79 | +  }  | 
 | 80 | + | 
 | 81 | +  const float* const result_data = result.const_data_ptr<float>();  | 
 | 82 | +  for (int ii = 0; ii < data.size(); ++ii) {  | 
 | 83 | +    EXPECT_FLOAT_EQ(result_data[ii], reference_result[ii]);  | 
 | 84 | +  }  | 
 | 85 | +}  | 
 | 86 | + | 
 | 87 | +TEST(OpFastHadamardTransformTest, Basic28N) {  | 
 | 88 | +  torch::executor::testing::TensorFactory<exec_aten::ScalarType::Float> tfFloat;  | 
 | 89 | +  constexpr int kTestLogSize = 7;  | 
 | 90 | +  constexpr int kTestPowerOfTwoSize = 1 << kTestLogSize;  | 
 | 91 | +  constexpr int kTestTotalSize = kTestPowerOfTwoSize * 28;  | 
 | 92 | +  std::vector<float> data = random_floats(kTestTotalSize);  | 
 | 93 | +  auto vec = tfFloat.make({kTestTotalSize}, data);  | 
 | 94 | +  auto out = tfFloat.zeros({kTestTotalSize});  | 
 | 95 | + | 
 | 96 | +  // The operator is supposed to autodetect 28 * 2**N size and handle  | 
 | 97 | +  // accordingly.  | 
 | 98 | +  auto result = fast_hadamard_transform_nocontext(vec, out);  | 
 | 99 | + | 
 | 100 | +  std::vector<float> reference_result = data;  | 
 | 101 | +  fast_hadamard_transform_28N_with_transpose(  | 
 | 102 | +      reference_result.data(), kTestLogSize);  | 
 | 103 | + | 
 | 104 | +  const float* const result_data = result.const_data_ptr<float>();  | 
 | 105 | +  for (int ii = 0; ii < data.size(); ++ii) {  | 
 | 106 | +    EXPECT_FLOAT_EQ(result_data[ii], reference_result[ii]);  | 
 | 107 | +  }  | 
 | 108 | +}  | 
 | 109 | + | 
 | 110 | +TEST(OpFastHadamardTransformTest, InvalidSize) {  | 
 | 111 | +  torch::executor::testing::TensorFactory<exec_aten::ScalarType::Float> tfFloat;  | 
 | 112 | +  auto mat = tfFloat.zeros({3});  | 
 | 113 | +  auto out = tfFloat.zeros({3});  | 
 | 114 | + | 
 | 115 | +  exec_aten::RuntimeContext context;  | 
 | 116 | +  torch::executor::native::fast_hadamard_transform_out(context, mat, out);  | 
 | 117 | +  EXPECT_NE(context.failure_state(), executorch::runtime::Error::Ok);  | 
 | 118 | +}  | 
0 commit comments