|
| 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 <gtest/gtest.h> |
| 10 | +#include <sys/times.h> |
| 11 | + |
| 12 | +#include <executorch/kernels/test/TestUtil.h> |
| 13 | +#include <executorch/runtime/core/error.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 15 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 16 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 17 | +#include <executorch/runtime/platform/runtime.h> |
| 18 | + |
| 19 | +#include <executorch/backends/cadence/hifi/operators/operators.h> |
| 20 | + |
| 21 | +namespace impl { |
| 22 | +namespace HiFi { |
| 23 | +namespace native { |
| 24 | +namespace { |
| 25 | + |
| 26 | +using ::executorch::aten::Scalar; |
| 27 | +using ::executorch::aten::ScalarType; |
| 28 | +using ::executorch::aten::Tensor; |
| 29 | +using ::executorch::aten::TensorImpl; |
| 30 | +using ::executorch::runtime::Error; |
| 31 | +using ::executorch::runtime::KernelRuntimeContext; |
| 32 | +using ::executorch::runtime::runtime_init; |
| 33 | +using ::executorch::runtime::testing::TensorFactory; |
| 34 | +using std::optional; |
| 35 | +using std::string_view; |
| 36 | + |
| 37 | +class HiFiQuantizedLinearTest : public OperatorTest { |
| 38 | + public: |
| 39 | + protected: |
| 40 | + void quantized_linear_out( |
| 41 | + const Tensor& input, |
| 42 | + const Tensor& weight, |
| 43 | + const Tensor& bias, |
| 44 | + int64_t in_zero_point, |
| 45 | + const Tensor& weight_zero_point, |
| 46 | + const Tensor& out_multiplier, |
| 47 | + const Tensor& out_shift, |
| 48 | + int64_t out_zero_point, |
| 49 | + const optional<Tensor>& offset, |
| 50 | + Tensor& output) { |
| 51 | + return ::impl::HiFi::native::quantized_linear_out( |
| 52 | + context_, |
| 53 | + input, |
| 54 | + weight, |
| 55 | + bias, |
| 56 | + in_zero_point, |
| 57 | + weight_zero_point, |
| 58 | + out_multiplier, |
| 59 | + out_shift, |
| 60 | + out_zero_point, |
| 61 | + offset, |
| 62 | + output); |
| 63 | + } |
| 64 | + |
| 65 | + void quantized_linear_per_tensor_out( |
| 66 | + const Tensor& input, |
| 67 | + const Tensor& weight, |
| 68 | + const Tensor& bias, |
| 69 | + int64_t in_zero_point, |
| 70 | + int64_t weight_zero_point, |
| 71 | + int64_t out_multiplier, |
| 72 | + int64_t out_shift, |
| 73 | + int64_t out_zero_point, |
| 74 | + const optional<Tensor>& offset, |
| 75 | + Tensor& output) { |
| 76 | + return ::impl::HiFi::native::quantized_linear_per_tensor_out( |
| 77 | + context_, |
| 78 | + input, |
| 79 | + weight, |
| 80 | + bias, |
| 81 | + in_zero_point, |
| 82 | + weight_zero_point, |
| 83 | + out_multiplier, |
| 84 | + out_shift, |
| 85 | + out_zero_point, |
| 86 | + offset, |
| 87 | + output); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +// Test quantized_linear_out with int16 activations (asym8s) |
| 92 | +TEST_F(HiFiQuantizedLinearTest, QuantizedLinearInt16Test) { |
| 93 | + TensorFactory<ScalarType::Short> tf_int16; |
| 94 | + TensorFactory<ScalarType::Int> tf_int32; |
| 95 | + TensorFactory<ScalarType::Char> tf_int8; |
| 96 | + |
| 97 | + // Simple 2D case: input [2, 3] x weight [4, 3] = output [2, 4] |
| 98 | + // Values captured from e2e test with |
| 99 | + // CadenceWith16BitLinearActivationsQuantizer |
| 100 | + Tensor input = |
| 101 | + tf_int16.make({2, 3}, {-28170, -26389, -32768, -31474, -32266, -29076}); |
| 102 | + Tensor weight = tf_int8.make( |
| 103 | + {4, 3}, {1, 87, -128, -114, -59, 44, -1, 127, -12, 44, -46, -29}); |
| 104 | + Tensor bias = tf_int32.zeros({4}); |
| 105 | + Tensor output = tf_int16.zeros({2, 4}); |
| 106 | + |
| 107 | + int64_t in_zero_point = -29822; |
| 108 | + Tensor weight_zero_point = tf_int32.make({1}, {2}); |
| 109 | + Tensor out_multiplier = tf_int32.make({1}, {2011373824}); |
| 110 | + Tensor out_shift = tf_int32.make({1}, {-8}); |
| 111 | + int64_t out_zero_point = -30847; |
| 112 | + quantized_linear_out( |
| 113 | + input, |
| 114 | + weight, |
| 115 | + bias, |
| 116 | + in_zero_point, |
| 117 | + weight_zero_point, |
| 118 | + out_multiplier, |
| 119 | + out_shift, |
| 120 | + out_zero_point, |
| 121 | + std::nullopt, |
| 122 | + output); |
| 123 | + // Expected output from e2e test |
| 124 | + Tensor expected_output = tf_int16.make( |
| 125 | + {2, 4}, {-28384, -32767, -29144, -30862, -31956, -29486, -31985, -30756}); |
| 126 | + EXPECT_TENSOR_CLOSE(output, expected_output); |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace |
| 130 | +} // namespace native |
| 131 | +} // namespace HiFi |
| 132 | +} // namespace impl |
0 commit comments