|
| 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/backends/cadence/hifi/operators/op_quantized_matmul_out.h> |
| 10 | + |
| 11 | +#include <executorch/kernels/test/TestUtil.h> |
| 12 | +#include <executorch/runtime/core/error.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 15 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 16 | +#include <executorch/runtime/platform/runtime.h> |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +namespace impl { |
| 20 | +namespace HiFi { |
| 21 | +namespace native { |
| 22 | +namespace { |
| 23 | + |
| 24 | +using ::executorch::aten::Scalar; |
| 25 | +using ::executorch::aten::ScalarType; |
| 26 | +using ::executorch::aten::Tensor; |
| 27 | +using ::executorch::aten::TensorImpl; |
| 28 | +using ::executorch::runtime::Error; |
| 29 | +using ::executorch::runtime::KernelRuntimeContext; |
| 30 | +using ::executorch::runtime::runtime_init; |
| 31 | +using ::executorch::runtime::testing::TensorFactory; |
| 32 | + |
| 33 | +class HiFiQuantizedMatmulTest : public OperatorTest { |
| 34 | + public: |
| 35 | + protected: |
| 36 | + Tensor& quantized_matmul_out( |
| 37 | + const Tensor& X, |
| 38 | + int64_t X_zero_point, |
| 39 | + const Tensor& Y, |
| 40 | + int64_t Y_zero_point, |
| 41 | + const std::optional<Tensor>& bias, |
| 42 | + int64_t out_multiplier, |
| 43 | + int64_t out_shift, |
| 44 | + int64_t out_zero_point, |
| 45 | + bool transposed, |
| 46 | + Tensor& output) { |
| 47 | + return impl::HiFi::native::quantized_matmul_out( |
| 48 | + context_, |
| 49 | + X, |
| 50 | + X_zero_point, |
| 51 | + Y, |
| 52 | + Y_zero_point, |
| 53 | + bias, |
| 54 | + out_multiplier, |
| 55 | + out_shift, |
| 56 | + out_zero_point, |
| 57 | + transposed, |
| 58 | + output); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +// Test quantized_matmul_out with int16 activations and int8 weights |
| 63 | +TEST_F(HiFiQuantizedMatmulTest, QuantizedMatmulInt16Test) { |
| 64 | + TensorFactory<ScalarType::Short> tf_int16; |
| 65 | + TensorFactory<ScalarType::Int> tf_int32; |
| 66 | + TensorFactory<ScalarType::Char> tf_int8; |
| 67 | + |
| 68 | + // Minimal test case: X [2, 2] x Y [2, 2] = output [2, 2] |
| 69 | + // Small enough to verify by hand calculation |
| 70 | + // |
| 71 | + // X (2x2): Y (2x2): |
| 72 | + // 2 4 1 2 |
| 73 | + // 6 8 1 0 |
| 74 | + // |
| 75 | + // Hand calculation for matmul (before scaling): |
| 76 | + // (0,0): 2*1 + 4*1 = 6 |
| 77 | + // (0,1): 2*2 + 4*0 = 4 |
| 78 | + // (1,0): 6*1 + 8*1 = 14 |
| 79 | + // (1,1): 6*2 + 8*0 = 12 |
| 80 | + // |
| 81 | + // Raw result: [[6, 4], [14, 12]] |
| 82 | + // After 0.5 scaling: [[3, 2], [7, 6]] |
| 83 | + Tensor X = tf_int16.make({2, 2}, {2, 4, 6, 8}); |
| 84 | + Tensor Y = tf_int8.make({2, 2}, {1, 2, 1, 0}); |
| 85 | + Tensor bias = tf_int32.zeros({2}); |
| 86 | + Tensor output = tf_int16.zeros({2, 2}); |
| 87 | + |
| 88 | + int64_t X_zero_point = 0; |
| 89 | + int64_t Y_zero_point = 0; |
| 90 | + int64_t out_multiplier = 1073741824; // 0.5 * 2^31 |
| 91 | + int64_t out_shift = 0; |
| 92 | + int64_t out_zero_point = 0; |
| 93 | + |
| 94 | + quantized_matmul_out( |
| 95 | + X, |
| 96 | + X_zero_point, |
| 97 | + Y, |
| 98 | + Y_zero_point, |
| 99 | + bias, |
| 100 | + out_multiplier, |
| 101 | + out_shift, |
| 102 | + out_zero_point, |
| 103 | + false, // transposed |
| 104 | + output); |
| 105 | + |
| 106 | + Tensor expected = tf_int16.make({2, 2}, {3, 2, 7, 6}); |
| 107 | + EXPECT_TENSOR_EQ(output, expected); |
| 108 | +} |
| 109 | + |
| 110 | +// Test quantized_matmul_out with transposed Y (int16 activations and int8 |
| 111 | +// weights) |
| 112 | +TEST_F(HiFiQuantizedMatmulTest, QuantizedMatmulInt16TransposedTest) { |
| 113 | + TensorFactory<ScalarType::Short> tf_int16; |
| 114 | + TensorFactory<ScalarType::Int> tf_int32; |
| 115 | + TensorFactory<ScalarType::Char> tf_int8; |
| 116 | + |
| 117 | + // Minimal test case with transposed Y: X [2, 2] x Y^T [2, 2] = output [2, 2] |
| 118 | + // Y is stored transposed, so we compute X @ Y^T |
| 119 | + // |
| 120 | + // X (2x2): Y_stored (2x2, which is Y^T): |
| 121 | + // 2 4 1 1 |
| 122 | + // 6 8 2 0 |
| 123 | + // |
| 124 | + // When transposed=true, we compute X @ Y_stored^T = X @ Y |
| 125 | + // Y = Y_stored^T = [[1, 2], [1, 0]] |
| 126 | + // |
| 127 | + // Hand calculation for matmul (before scaling): |
| 128 | + // (0,0): 2*1 + 4*1 = 6 |
| 129 | + // (0,1): 2*2 + 4*0 = 4 |
| 130 | + // (1,0): 6*1 + 8*1 = 14 |
| 131 | + // (1,1): 6*2 + 8*0 = 12 |
| 132 | + // |
| 133 | + // Raw result: [[6, 4], [14, 12]] |
| 134 | + // After 0.5 scaling: [[3, 2], [7, 6]] |
| 135 | + Tensor X = tf_int16.make({2, 2}, {2, 4, 6, 8}); |
| 136 | + Tensor Y = tf_int8.make({2, 2}, {1, 1, 2, 0}); // Stored as Y^T |
| 137 | + Tensor bias = tf_int32.zeros({2}); |
| 138 | + Tensor output = tf_int16.zeros({2, 2}); |
| 139 | + |
| 140 | + int64_t X_zero_point = 0; |
| 141 | + int64_t Y_zero_point = 0; |
| 142 | + int64_t out_multiplier = 1073741824; // 0.5 * 2^31 |
| 143 | + int64_t out_shift = 0; |
| 144 | + int64_t out_zero_point = 0; |
| 145 | + |
| 146 | + quantized_matmul_out( |
| 147 | + X, |
| 148 | + X_zero_point, |
| 149 | + Y, |
| 150 | + Y_zero_point, |
| 151 | + bias, |
| 152 | + out_multiplier, |
| 153 | + out_shift, |
| 154 | + out_zero_point, |
| 155 | + true, // transposed |
| 156 | + output); |
| 157 | + |
| 158 | + Tensor expected = tf_int16.make({2, 2}, {3, 2, 7, 6}); |
| 159 | + EXPECT_TENSOR_EQ(output, expected); |
| 160 | +} |
| 161 | + |
| 162 | +} // namespace |
| 163 | +} // namespace native |
| 164 | +} // namespace HiFi |
| 165 | +} // namespace impl |
0 commit comments