2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "kernels/Tanh.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
22 namespace luci_interpreter
29 using namespace testing;
31 class TanhTest : public ::testing::Test
34 void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
36 std::unique_ptr<IMemoryManager> _memory_manager;
39 TEST_F(TanhTest, Float)
41 Shape input_shape{1, 2, 4, 1};
42 std::vector<float> input_data{
47 makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
48 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
50 Tanh kernel(&input_tensor, &output_tensor);
52 _memory_manager->allocate_memory(output_tensor);
55 std::vector<float> ref_output_data{
56 0, -0.9999877, 0.9640275, 0.999329, //
57 0.99505475, -0.9640275, 1, 0.7615941, //
59 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
62 TEST_F(TanhTest, Uint8)
65 float kMax = 127.f / 128.f;
66 float kTanhTolerance = 2 * (1. / 256);
67 std::pair<float, int32_t> input_quant_param = quantizationParams<uint8_t>(8 * kMin, 8 * kMax);
68 std::pair<float, int32_t> output_quant_param = quantizationParams<uint8_t>(kMin, kMax);
69 std::vector<float> input_data{
84 makeInputTensor<DataType::U8>({2, 6, 4, 1}, input_quant_param.first, input_quant_param.second,
85 input_data, _memory_manager.get());
86 Tensor output_tensor =
87 makeOutputTensor(DataType::U8, output_quant_param.first, output_quant_param.second);
89 Tanh kernel(&input_tensor, &output_tensor);
91 _memory_manager->allocate_memory(output_tensor);
94 std::vector<float> ref_output_data{
95 0.0, -0.999987, 0.964027, 0.999329, //
96 -0.999329, -0.96402, 0.99999, 0.76159, //
97 0.0, -0.999987, 0.964027, 0.999329, //
98 -0.999329, -0.96402, 0.99999, 0.76159, //
99 0.0, -0.999987, 0.964027, 0.999329, //
100 -0.999329, -0.96402, 0.99999, 0.76159, //
101 0.0, -0.999987, 0.964027, 0.999329, //
102 -0.999329, -0.96402, 0.99999, 0.76159, //
103 0.0, -0.999987, 0.964027, 0.999329, //
104 -0.999329, -0.96402, 0.99999, 0.76159, //
105 0.0, -0.999987, 0.964027, 0.999329, //
106 -0.999329, -0.96402, 0.99999, 0.76159, //
108 std::vector<int32_t> ref_output_shape{2, 6, 4, 1};
109 EXPECT_THAT(dequantizeTensorData(output_tensor), FloatArrayNear(ref_output_data, kTanhTolerance));
110 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
113 TEST_F(TanhTest, InputTypeInvalid_NEG)
115 std::vector<int64_t> input_data{
129 Tensor input_tensor =
130 makeInputTensor<DataType::S64>({2, 6, 4, 1}, input_data, _memory_manager.get());
131 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
133 Tanh kernel(&input_tensor, &output_tensor);
134 _memory_manager->allocate_memory(output_tensor);
135 EXPECT_ANY_THROW(kernel.execute());
138 TEST_F(TanhTest, InputOutputMismatch_NEG)
140 std::vector<float> input_data{
154 Tensor input_tensor =
155 makeInputTensor<DataType::FLOAT32>({2, 6, 4, 1}, input_data, _memory_manager.get());
156 Tensor output_tensor = makeOutputTensor(DataType::U8);
158 Tanh kernel(&input_tensor, &output_tensor);
159 EXPECT_ANY_THROW(kernel.configure());
163 } // namespace kernels
164 } // namespace luci_interpreter