1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include <gtest/gtest.h>
16 #include "tensorflow/lite/interpreter.h"
17 #include "tensorflow/lite/kernels/register.h"
18 #include "tensorflow/lite/kernels/test_util.h"
19 #include "tensorflow/lite/model.h"
20
21 namespace tflite {
22 namespace {
23
24 using ::testing::ElementsAre;
25 using ::testing::ElementsAreArray;
26
27 template <typename T>
28 class PowOpModel : public SingleOpModel {
29 public:
PowOpModel(const TensorData & input1,const TensorData & input2,const TensorData & output)30 PowOpModel(const TensorData& input1, const TensorData& input2,
31 const TensorData& output) {
32 input1_ = AddInput(input1);
33 input2_ = AddInput(input2);
34 output_ = AddOutput(output);
35 SetBuiltinOp(BuiltinOperator_POW, BuiltinOptions_PowOptions,
36 CreatePowOptions(builder_).Union());
37 BuildInterpreter({GetShape(input1_), GetShape(input2_)});
38 }
39
input1()40 int input1() { return input1_; }
input2()41 int input2() { return input2_; }
42
GetOutput()43 std::vector<T> GetOutput() { return ExtractVector<T>(output_); }
GetOutputShape()44 std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
45
46 private:
47 int input1_;
48 int input2_;
49 int output_;
50 };
51
TEST(PowOpModel,Simple)52 TEST(PowOpModel, Simple) {
53 PowOpModel<int32_t> model({TensorType_INT32, {1, 2, 2, 1}},
54 {TensorType_INT32, {1, 2, 2, 1}},
55 {TensorType_INT32, {}});
56 model.PopulateTensor<int32_t>(model.input1(), {12, 2, 7, 8});
57 model.PopulateTensor<int32_t>(model.input2(), {1, 2, 3, 1});
58 model.Invoke();
59 EXPECT_THAT(model.GetOutputShape(), ElementsAre(1, 2, 2, 1));
60 EXPECT_THAT(model.GetOutput(), ElementsAre(12, 4, 343, 8));
61 }
62
TEST(PowOpModel,NegativeAndZeroValue)63 TEST(PowOpModel, NegativeAndZeroValue) {
64 PowOpModel<int32_t> model({TensorType_INT32, {1, 2, 2, 1}},
65 {TensorType_INT32, {1, 2, 2, 1}},
66 {TensorType_INT32, {}});
67 model.PopulateTensor<int32_t>(model.input1(), {0, 2, -7, 8});
68 model.PopulateTensor<int32_t>(model.input2(), {1, 2, 3, 0});
69 model.Invoke();
70 EXPECT_THAT(model.GetOutputShape(), ElementsAre(1, 2, 2, 1));
71 EXPECT_THAT(model.GetOutput(), ElementsAre(0, 4, -343, 1));
72 }
73
TEST(PowOpModel,Float)74 TEST(PowOpModel, Float) {
75 PowOpModel<float> model({TensorType_FLOAT32, {1, 2, 2, 1}},
76 {TensorType_FLOAT32, {1, 2, 2, 1}},
77 {TensorType_FLOAT32, {}});
78 model.PopulateTensor<float>(model.input1(), {0.3, 0.4, 0.7, 5.8});
79 model.PopulateTensor<float>(model.input2(), {0.5, 2.7, 3.1, 3.2});
80 model.Invoke();
81 EXPECT_THAT(model.GetOutputShape(), ElementsAre(1, 2, 2, 1));
82 EXPECT_THAT(model.GetOutput(),
83 ElementsAreArray(ArrayFloatNear(
84 {0.5477226, 0.08424846, 0.33098164, 277.313}, 1e-3)));
85 }
86
TEST(PowOpModel,NegativeFloatTest)87 TEST(PowOpModel, NegativeFloatTest) {
88 PowOpModel<float> model({TensorType_FLOAT32, {1, 2, 2, 1}},
89 {TensorType_FLOAT32, {1, 2, 2, 1}},
90 {TensorType_FLOAT32, {}});
91 model.PopulateTensor<float>(model.input1(), {0.3, 0.4, 0.7, 5.8});
92 model.PopulateTensor<float>(model.input2(), {0.5, -2.7, 3.1, -3.2});
93 model.Invoke();
94 EXPECT_THAT(model.GetOutputShape(), ElementsAre(1, 2, 2, 1));
95 EXPECT_THAT(model.GetOutput(),
96 ElementsAreArray(ArrayFloatNear(
97 {0.5477226, 11.869653, 0.33098164, 0.003606}, 1e-3)));
98 }
99
TEST(PowOpModel,BroadcastTest)100 TEST(PowOpModel, BroadcastTest) {
101 PowOpModel<int32_t> model({TensorType_INT32, {1, 2, 2, 1}},
102 {TensorType_INT32, {1}}, {TensorType_INT32, {}});
103 model.PopulateTensor<int32_t>(model.input1(), {12, 2, 7, 8});
104 model.PopulateTensor<int32_t>(model.input2(), {4});
105 model.Invoke();
106 EXPECT_THAT(model.GetOutputShape(), ElementsAre(1, 2, 2, 1));
107 EXPECT_THAT(model.GetOutput(), ElementsAre(20736, 16, 2401, 4096));
108 }
109
110 } // namespace
111 } // namespace tflite
112
main(int argc,char ** argv)113 int main(int argc, char** argv) {
114 ::tflite::LogToStderr();
115 ::testing::InitGoogleTest(&argc, argv);
116 return RUN_ALL_TESTS();
117 }
118