1 /* Copyright 2021 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 <cstdint>
16 #include <vector>
17
18 #include <gtest/gtest.h>
19 #include "tensorflow/lite/interpreter.h"
20 #include "tensorflow/lite/kernels/register.h"
21 #include "tensorflow/lite/kernels/test_util.h"
22 #include "tensorflow/lite/model.h"
23
24 namespace tflite {
25 namespace {
26 using ::testing::ElementsAreArray;
27
28 template <class ShapeType = int32_t>
29 class BroadcastArgsOpModel : public SingleOpModel {
30 public:
BroadcastArgsOpModel(std::initializer_list<ShapeType> input1,std::initializer_list<ShapeType> input2)31 BroadcastArgsOpModel(std::initializer_list<ShapeType> input1,
32 std::initializer_list<ShapeType> input2) {
33 int input1_length = input1.size();
34 int input2_length = input2.size();
35 shape1_ = AddInput({GetTensorType<ShapeType>(), {input1_length}});
36 shape2_ = AddInput({GetTensorType<ShapeType>(), {input2_length}});
37 output_ = AddOutput(GetTensorType<ShapeType>());
38 SetBuiltinOp(BuiltinOperator_BROADCAST_ARGS, BuiltinOptions_NONE, 0);
39 BuildInterpreter({{input1_length}, {input2_length}});
40 if (input1.size() > 0) SetInput1(input1);
41 if (input2.size() > 0) SetInput2(input2);
42 }
43
SetInput1(std::initializer_list<ShapeType> data)44 void SetInput1(std::initializer_list<ShapeType> data) {
45 PopulateTensor(shape1_, data);
46 }
47
SetInput2(std::initializer_list<ShapeType> data)48 void SetInput2(std::initializer_list<ShapeType> data) {
49 PopulateTensor(shape2_, data);
50 }
51
GetOutput()52 std::vector<ShapeType> GetOutput() {
53 return ExtractVector<ShapeType>(output_);
54 }
GetOutputShape()55 std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
56
57 protected:
58 int shape1_;
59 int shape2_;
60 int output_;
61 };
62
63 template <typename T>
64 class BroadcastArgsOpTest : public ::testing::Test {};
65
66 using DataTypes = ::testing::Types<int64_t, int32_t>;
67 TYPED_TEST_SUITE(BroadcastArgsOpTest, DataTypes);
68
69 #ifdef GTEST_HAS_DEATH_TEST
TYPED_TEST(BroadcastArgsOpTest,ShapeNotBroadcastable)70 TYPED_TEST(BroadcastArgsOpTest, ShapeNotBroadcastable) {
71 BroadcastArgsOpModel<TypeParam> m({2, 3, 4, 4}, {2, 2});
72 EXPECT_DEATH(ASSERT_EQ(m.Invoke(), kTfLiteOk), "");
73 }
74 #endif
75
TYPED_TEST(BroadcastArgsOpTest,BroadcastArgsWithScalar)76 TYPED_TEST(BroadcastArgsOpTest, BroadcastArgsWithScalar) {
77 BroadcastArgsOpModel<TypeParam> m({}, {2, 4});
78 ASSERT_EQ(m.Invoke(), kTfLiteOk);
79 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2}));
80 EXPECT_THAT(m.GetOutput(), ElementsAreArray({2, 4}));
81 }
82
TYPED_TEST(BroadcastArgsOpTest,BroadcastArgsDifferentDims)83 TYPED_TEST(BroadcastArgsOpTest, BroadcastArgsDifferentDims) {
84 BroadcastArgsOpModel<TypeParam> m({1}, {2, 4});
85 ASSERT_EQ(m.Invoke(), kTfLiteOk);
86 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2}));
87 EXPECT_THAT(m.GetOutput(), ElementsAreArray({2, 4}));
88 }
89
TYPED_TEST(BroadcastArgsOpTest,BroadcastArgsSameDims)90 TYPED_TEST(BroadcastArgsOpTest, BroadcastArgsSameDims) {
91 BroadcastArgsOpModel<TypeParam> m({1, 4, 6, 3, 1, 5}, {4, 4, 1, 3, 4, 1});
92 ASSERT_EQ(m.Invoke(), kTfLiteOk);
93 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({6}));
94 EXPECT_THAT(m.GetOutput(), ElementsAreArray({4, 4, 6, 3, 4, 5}));
95 }
96
TYPED_TEST(BroadcastArgsOpTest,BroadcastArgsComplex)97 TYPED_TEST(BroadcastArgsOpTest, BroadcastArgsComplex) {
98 BroadcastArgsOpModel<TypeParam> m({6, 3, 1, 5}, {4, 4, 1, 3, 4, 1});
99 ASSERT_EQ(m.Invoke(), kTfLiteOk);
100 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({6}));
101 EXPECT_THAT(m.GetOutput(), ElementsAreArray({4, 4, 6, 3, 4, 5}));
102 }
103
104 } // namespace
105 } // namespace tflite
106