|
| 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 | + |
| 11 | +#include <executorch/kernels/test/TestUtil.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 15 | + |
| 16 | +#include <executorch/backends/cadence/generic/operators/op_quantized_layer_norm.h> |
| 17 | + |
| 18 | +namespace impl { |
| 19 | +namespace generic { |
| 20 | +namespace native { |
| 21 | +namespace { |
| 22 | + |
| 23 | +using ::executorch::aten::IntArrayRef; |
| 24 | +using ::executorch::aten::ScalarType; |
| 25 | +using ::executorch::aten::Tensor; |
| 26 | +using ::executorch::runtime::testing::TensorFactory; |
| 27 | + |
| 28 | +// Scales are powers of two and inputs are symmetric around their mean, so |
| 29 | +// every row normalizes to exactly +/-1 (eps is negligible). |
| 30 | +constexpr double kEps = 1e-9; |
| 31 | + |
| 32 | +class GenericQuantizedLayerNormTest : public OperatorTest {}; |
| 33 | + |
| 34 | +TEST_F(GenericQuantizedLayerNormTest, Int16LargeMagnitudePerTensor) { |
| 35 | + TensorFactory<ScalarType::Short> tf_short; |
| 36 | + TensorFactory<ScalarType::Float> tf_float; |
| 37 | + const std::vector<int64_t> normalized_shape{8}; |
| 38 | + |
| 39 | + // Per-row sum of squares is 8 * 30000^2, well past int32 range. |
| 40 | + const Tensor input = tf_short.make( |
| 41 | + {2, 8}, |
| 42 | + {30000, -30000, 30000, -30000, 30000, -30000, 30000, -30000, |
| 43 | + -30000, 30000, -30000, 30000, -30000, 30000, -30000, 30000}); |
| 44 | + const Tensor weight = tf_float.ones({8}); |
| 45 | + const Tensor bias = tf_float.zeros({8}); |
| 46 | + Tensor out = tf_short.zeros({2, 8}); |
| 47 | + |
| 48 | + quantized_layer_norm_per_tensor_out( |
| 49 | + context_, |
| 50 | + input, |
| 51 | + /*in_scale=*/1.0 / 1024, |
| 52 | + /*in_zero_point=*/0, |
| 53 | + IntArrayRef(normalized_shape.data(), normalized_shape.size()), |
| 54 | + weight, |
| 55 | + bias, |
| 56 | + kEps, |
| 57 | + /*output_scale=*/1.0 / 16384, |
| 58 | + /*output_zero_point=*/0, |
| 59 | + out); |
| 60 | + |
| 61 | + const Tensor expected = tf_short.make( |
| 62 | + {2, 8}, |
| 63 | + {16384, -16384, 16384, -16384, 16384, -16384, 16384, -16384, |
| 64 | + -16384, 16384, -16384, 16384, -16384, 16384, -16384, 16384}); |
| 65 | + EXPECT_TENSOR_EQ(out, expected); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_F(GenericQuantizedLayerNormTest, Int16LargeMeanSmallSpreadWithZeroPoints) { |
| 69 | + TensorFactory<ScalarType::Short> tf_short; |
| 70 | + TensorFactory<ScalarType::Float> tf_float; |
| 71 | + TensorFactory<ScalarType::Long> tf_long; |
| 72 | + const std::vector<int64_t> normalized_shape{8}; |
| 73 | + |
| 74 | + // Centered values are 21000 +/- 4: the mean dwarfs the spread, which breaks |
| 75 | + // an fp32 E[x^2] - E[x]^2 variance. |
| 76 | + const Tensor input = tf_short.make( |
| 77 | + {1, 8}, {20004, 19996, 20004, 19996, 20004, 19996, 20004, 19996}); |
| 78 | + const Tensor in_scale = tf_float.make({1}, {1.0f / 64}); |
| 79 | + const Tensor in_zero_point = tf_long.make({1}, {-1000}); |
| 80 | + const Tensor weight = tf_float.full({8}, 2.0f); |
| 81 | + const Tensor bias = tf_float.full({8}, 0.5f); |
| 82 | + Tensor out = tf_short.zeros({1, 8}); |
| 83 | + |
| 84 | + quantized_layer_norm_out( |
| 85 | + context_, |
| 86 | + input, |
| 87 | + in_scale, |
| 88 | + in_zero_point, |
| 89 | + IntArrayRef(normalized_shape.data(), normalized_shape.size()), |
| 90 | + weight, |
| 91 | + bias, |
| 92 | + kEps, |
| 93 | + /*output_scale=*/1.0 / 4096, |
| 94 | + /*output_zero_point=*/100, |
| 95 | + out); |
| 96 | + |
| 97 | + // (+1 * 2 + 0.5) * 4096 + 100 and (-1 * 2 + 0.5) * 4096 + 100. |
| 98 | + const Tensor expected = tf_short.make( |
| 99 | + {1, 8}, {10340, -6044, 10340, -6044, 10340, -6044, 10340, -6044}); |
| 100 | + EXPECT_TENSOR_EQ(out, expected); |
| 101 | +} |
| 102 | + |
| 103 | +TEST_F(GenericQuantizedLayerNormTest, Int8WithZeroPoint) { |
| 104 | + TensorFactory<ScalarType::Char> tf_char; |
| 105 | + TensorFactory<ScalarType::Float> tf_float; |
| 106 | + const std::vector<int64_t> normalized_shape{4}; |
| 107 | + |
| 108 | + const Tensor input = tf_char.make({1, 4}, {10, 30, 10, 30}); |
| 109 | + const Tensor weight = tf_float.ones({4}); |
| 110 | + const Tensor bias = tf_float.zeros({4}); |
| 111 | + Tensor out = tf_char.zeros({1, 4}); |
| 112 | + |
| 113 | + quantized_layer_norm_per_tensor_out( |
| 114 | + context_, |
| 115 | + input, |
| 116 | + /*in_scale=*/1.0 / 8, |
| 117 | + /*in_zero_point=*/20, |
| 118 | + IntArrayRef(normalized_shape.data(), normalized_shape.size()), |
| 119 | + weight, |
| 120 | + bias, |
| 121 | + kEps, |
| 122 | + /*output_scale=*/1.0 / 64, |
| 123 | + /*output_zero_point=*/0, |
| 124 | + out); |
| 125 | + |
| 126 | + const Tensor expected = tf_char.make({1, 4}, {-64, 64, -64, 64}); |
| 127 | + EXPECT_TENSOR_EQ(out, expected); |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace |
| 131 | +} // namespace native |
| 132 | +} // namespace generic |
| 133 | +} // namespace impl |
0 commit comments