2 * Copyright (c) 2021 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/SquaredDifference.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
22 namespace luci_interpreter
29 using namespace testing;
31 TEST(SquaredDifferenceTest, Float)
33 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
35 Shape input_shape{3, 1, 2};
36 std::vector<float> input_data1{1.0, 0.0, -1.0, 11.0, -2.0, -1.44};
37 std::vector<float> input_data2{-1.0, 0.0, 1.0, 12.0, -3.0, -1.43};
38 Tensor input_tensor1 =
39 makeInputTensor<DataType::FLOAT32>(input_shape, input_data1, memory_manager.get());
40 Tensor input_tensor2 =
41 makeInputTensor<DataType::FLOAT32>(input_shape, input_data2, memory_manager.get());
42 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
44 SquaredDifference kernel(&input_tensor1, &input_tensor2, &output_tensor);
46 memory_manager->allocate_memory(output_tensor);
49 std::vector<float> ref_output_data{4.0, 0.0, 4.0, 1.0, 1.0, 0.0001};
50 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
53 TEST(SquaredDifferenceTest, FloatBroadcast)
55 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
57 Shape input_shape1{3, 1, 2};
58 Shape input_shape2{1};
59 std::vector<float> input_data1{1.0, 0.0, -1.0, 11.0, -2.0, -1.44};
60 std::vector<float> input_data2{1.0};
61 Tensor input_tensor1 =
62 makeInputTensor<DataType::FLOAT32>(input_shape1, input_data1, memory_manager.get());
63 Tensor input_tensor2 =
64 makeInputTensor<DataType::FLOAT32>(input_shape2, input_data2, memory_manager.get());
65 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
67 SquaredDifference kernel(&input_tensor1, &input_tensor2, &output_tensor);
69 memory_manager->allocate_memory(output_tensor);
72 std::vector<float> ref_output_data{0.0, 1.0, 4.0, 100.0, 9.0, 5.9536};
73 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
77 } // namespace kernels
78 } // namespace luci_interpreter