|
| 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 cadence { |
| 22 | +namespace impl { |
| 23 | +namespace HiFi { |
| 24 | +namespace native { |
| 25 | +namespace { |
| 26 | + |
| 27 | +using ::executorch::aten::Scalar; |
| 28 | +using ::executorch::aten::ScalarType; |
| 29 | +using ::executorch::aten::Tensor; |
| 30 | +using ::executorch::aten::TensorImpl; |
| 31 | +using ::executorch::runtime::Error; |
| 32 | +using ::executorch::runtime::KernelRuntimeContext; |
| 33 | +using ::executorch::runtime::runtime_init; |
| 34 | +using ::executorch::runtime::testing::TensorFactory; |
| 35 | +using std::optional; |
| 36 | +using std::string_view; |
| 37 | + |
| 38 | +class HiFiQuantizedReluTest : public OperatorTest { |
| 39 | + public: |
| 40 | + protected: |
| 41 | + void quantized_relu_out( |
| 42 | + const Tensor& input, |
| 43 | + const Tensor& in_zero_point, |
| 44 | + const int64_t out_zero_point, |
| 45 | + const Tensor& out_multiplier, |
| 46 | + const Tensor& out_shift, |
| 47 | + Tensor& output) { |
| 48 | + return ::cadence::impl::HiFi::native::quantized_relu_out( |
| 49 | + context_, |
| 50 | + input, |
| 51 | + in_zero_point, |
| 52 | + out_zero_point, |
| 53 | + out_multiplier, |
| 54 | + out_shift, |
| 55 | + output); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +TEST_F(HiFiQuantizedReluTest, MultiDimensionalTest) { |
| 60 | + TensorFactory<ScalarType::Char> tf_chars; |
| 61 | + const std::vector<int32_t> sizes{2, 3, 5, 6}; |
| 62 | + Tensor quantized_input = tf_chars.full(sizes, -128); |
| 63 | + Tensor quantized_output = tf_chars.full(sizes, 100); |
| 64 | + Tensor in_zero_point = tf_chars.full({1}, 127); |
| 65 | + int64_t out_zero_point = -128; |
| 66 | + Tensor out_multiplier = |
| 67 | + TensorFactory<ScalarType::Int>().full({1}, 1077952640); |
| 68 | + Tensor out_shift = TensorFactory<ScalarType::Int>().full({1}, 5); |
| 69 | + |
| 70 | + quantized_relu_out( |
| 71 | + quantized_input, |
| 72 | + in_zero_point, |
| 73 | + out_zero_point, |
| 74 | + out_multiplier, |
| 75 | + out_shift, |
| 76 | + quantized_output); |
| 77 | + |
| 78 | + Tensor expected_output = tf_chars.full(sizes, -128); |
| 79 | + EXPECT_TENSOR_EQ(quantized_output, expected_output); |
| 80 | +} |
| 81 | + |
| 82 | +TEST_F(HiFiQuantizedReluTest, OneDimensionalTest) { |
| 83 | + TensorFactory<ScalarType::Char> tf_chars; |
| 84 | + const std::vector<int32_t> sizes{56}; |
| 85 | + Tensor quantized_input = tf_chars.full(sizes, -128); |
| 86 | + Tensor quantized_output = tf_chars.full(sizes, 100); |
| 87 | + Tensor in_zero_point = tf_chars.full({1}, 127); |
| 88 | + int64_t out_zero_point = -128; |
| 89 | + Tensor out_multiplier = |
| 90 | + TensorFactory<ScalarType::Int>().full({1}, 1077952640); |
| 91 | + Tensor out_shift = TensorFactory<ScalarType::Int>().full({1}, 5); |
| 92 | + |
| 93 | + quantized_relu_out( |
| 94 | + quantized_input, |
| 95 | + in_zero_point, |
| 96 | + out_zero_point, |
| 97 | + out_multiplier, |
| 98 | + out_shift, |
| 99 | + quantized_output); |
| 100 | + |
| 101 | + Tensor expected_output = tf_chars.full(sizes, -128); |
| 102 | + EXPECT_TENSOR_EQ(quantized_output, expected_output); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace |
| 106 | +} // namespace native |
| 107 | +} // namespace HiFi |
| 108 | +} // namespace impl |
| 109 | +} // namespace cadence |
0 commit comments