From c55f8a6db48cda9d3a78048338b7f18c4cca62b8 Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Wed, 28 Oct 2020 12:16:55 +0900 Subject: Imported Upstream version 1.10.0 --- .../luci-interpreter/src/kernels/NotEqual.test.cpp | 187 +++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 compiler/luci-interpreter/src/kernels/NotEqual.test.cpp (limited to 'compiler/luci-interpreter/src/kernels/NotEqual.test.cpp') diff --git a/compiler/luci-interpreter/src/kernels/NotEqual.test.cpp b/compiler/luci-interpreter/src/kernels/NotEqual.test.cpp new file mode 100644 index 000000000..8c8712371 --- /dev/null +++ b/compiler/luci-interpreter/src/kernels/NotEqual.test.cpp @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved + * Copyright 2017 The TensorFlow Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "kernels/NotEqual.h" +#include "kernels/TestUtils.h" + +namespace luci_interpreter +{ +namespace kernels +{ +namespace +{ + +using namespace testing; + +TEST(NotEqualTest, FloatSimple) +{ + std::vector x_data{ + 0.5, 0.7, 0.9, // Row 1 + 1, 0, -1, // Row 2 + }; + + std::vector y_data{ + 0.9, 0.7, 0.5, // Row 1 + -1, 0, 1, // Row 2 + }; + + std::vector ref_output_data{ + true, false, true, // Row 1 + true, false, true, // Row 2 + }; + + Tensor x_tensor = makeInputTensor({2, 3}, x_data); + Tensor y_tensor = makeInputTensor({2, 3}, y_data); + Tensor output_tensor = makeOutputTensor(DataType::BOOL); + + NotEqual kernel(&x_tensor, &y_tensor, &output_tensor); + kernel.configure(); + kernel.execute(); + + EXPECT_THAT(extractTensorData(output_tensor), ::testing::ElementsAreArray(ref_output_data)); + EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({2, 3})); +} + +TEST(NotEqualTest, FloatBroardcast) +{ + std::vector x_data{ + 0.5, 0.7, 0.9, // Row 1 + 1, 0, -1, // Row 2 + -1, 0, 1, // Row 3 + 0.9, 0.7, 0.5, // Row 4 + }; + + std::vector y_data{ + 0.9, 0.7, 0.5, // Row 1 + }; + + std::vector ref_output_data{ + true, false, true, // Row 1 + true, true, true, // Row 2 + true, true, true, // Row 3 + false, false, false, // Row 4 + }; + + Tensor x_tensor = makeInputTensor({4, 3}, x_data); + Tensor y_tensor = makeInputTensor({1, 3}, y_data); + Tensor output_tensor = makeOutputTensor(DataType::BOOL); + + NotEqual kernel(&x_tensor, &y_tensor, &output_tensor); + kernel.configure(); + kernel.execute(); + + EXPECT_THAT(extractTensorData(output_tensor), ::testing::ElementsAreArray(ref_output_data)); + EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({4, 3})); +} + +// Choose min / max in such a way that there are exactly 256 units to avoid rounding errors. +const float F_MIN = -128.0 / 128.0; +const float F_MAX = 127.0 / 128.0; + +TEST(NotEqualTest, Uint8Quantized) +{ + std::vector x_data{ + 0.5, 0.5, 0.7, 0.9, // Row 1 + 1, 0, 0.05, -1, // Row 2 + }; + + std::vector y_data{ + 0.9, 0.5, 0.55, 0.5, // Row 1 + -1, 0, 0.05, 1, // Row 2 + }; + + std::vector ref_output_data{ + true, false, true, true, // Row 1 + true, false, false, true, // Row 2 + }; + + std::pair x_quant_param = quantizationParams(F_MIN, F_MAX); + Tensor x_tensor = makeInputTensor({1, 2, 4, 1}, x_quant_param.first, + x_quant_param.second, x_data); + + std::pair y_quant_param = quantizationParams(F_MIN * 2, F_MAX * 2); + Tensor y_tensor = makeInputTensor({1, 2, 4, 1}, y_quant_param.first, + y_quant_param.second, y_data); + + Tensor output_tensor = makeOutputTensor(DataType::BOOL); + + NotEqual kernel(&x_tensor, &y_tensor, &output_tensor); + kernel.configure(); + kernel.execute(); + + EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 2, 4, 1})); + EXPECT_THAT(extractTensorData(output_tensor), ::testing::ElementsAreArray(ref_output_data)); +} + +TEST(NotEqualTest, Uint8QuantizedBroadcast) +{ + std::vector x_data{ + 0.4, -0.8, 0.7, 0.3, // Row 1 + -0.5, 0.1, 0, 0.5, // Row 2 + 1, 0, 0.05, -1, // Row 3 + -1, 0.05, 0, 1, // Row 4 + }; + + std::vector y_data{ + -1, 0.05, 0, 1, // Row 1 + }; + + std::vector ref_output_data{ + true, true, true, true, // Row 1 + true, true, false, true, // Row 2 + true, true, true, true, // Row 3 + false, false, false, false, // Row 4 + }; + + std::pair quant_param = quantizationParams(F_MIN, F_MAX); + Tensor x_tensor = + makeInputTensor({1, 4, 4, 1}, quant_param.first, quant_param.second, x_data); + Tensor y_tensor = + makeInputTensor({1, 1, 4, 1}, quant_param.first, quant_param.second, y_data); + Tensor output_tensor = makeOutputTensor(DataType::BOOL); + + NotEqual kernel(&x_tensor, &y_tensor, &output_tensor); + kernel.configure(); + kernel.execute(); + + EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 4, 4, 1})); + EXPECT_THAT(extractTensorData(output_tensor), ::testing::ElementsAreArray(ref_output_data)); +} + +TEST(NotEqualTest, Input_Type_Mismatch_NEG) +{ + Tensor x_tensor = makeInputTensor({1}, {1.f}); + Tensor y_tensor = makeInputTensor({1}, {1}); + Tensor output_tensor = makeOutputTensor(DataType::BOOL); + + NotEqual kernel(&x_tensor, &y_tensor, &output_tensor); + EXPECT_ANY_THROW(kernel.configure()); +} + +TEST(NotEqualTest, Input_Output_Type_NEG) +{ + Tensor x_tensor = makeInputTensor({1}, {1.f}); + Tensor y_tensor = makeInputTensor({1}, {1.f}); + Tensor output_tensor = makeOutputTensor(DataType::FLOAT32); + + NotEqual kernel(&x_tensor, &y_tensor, &output_tensor); + EXPECT_ANY_THROW(kernel.configure()); +} + +} // namespace +} // namespace kernels +} // namespace luci_interpreter -- cgit v1.2.3