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 <tuple>
16 #include <vector>
17 
18 #include <gtest/gtest.h>
19 #include "absl/memory/memory.h"
20 #include "tensorflow/lite/toco/graph_transformations/graph_transformations.h"
21 #include "tensorflow/lite/toco/model.h"
22 #include "tensorflow/lite/toco/tooling_util.h"
23 
24 namespace toco {
25 
26 namespace {
27 
RunIdentifyL2Pool(const std::vector<float> & input,const std::vector<int> & input_shape,const std::vector<int> & output_shape)28 void RunIdentifyL2Pool(const std::vector<float>& input,
29                        const std::vector<int>& input_shape,
30                        const std::vector<int>& output_shape) {
31   Model model;
32   Array& input0 = model.GetOrCreateArray("input0");
33   Array& output = model.GetOrCreateArray("output");
34 
35   *input0.mutable_shape()->mutable_dims() = input_shape;
36   input0.data_type = ArrayDataType::kFloat;
37   input0.GetMutableBuffer<ArrayDataType::kFloat>().data = input;
38 
39   *output.mutable_shape()->mutable_dims() = output_shape;
40 
41   auto sq_op = new TensorFlowSquareOperator;
42   sq_op->inputs = {"input0"};
43   sq_op->outputs = {"output"};
44 
45   Array& avgpooloutput = model.GetOrCreateArray("Avgpooloutput");
46   *avgpooloutput.mutable_shape()->mutable_dims() = output_shape;
47 
48   auto avgpool_op = new AveragePoolOperator;
49   avgpool_op->inputs = {sq_op->outputs[0]};
50   avgpool_op->outputs = {"Avgpooloutput"};
51 
52   Array& sqrtoutput = model.GetOrCreateArray("Sqrtoutput");
53   *sqrtoutput.mutable_shape()->mutable_dims() = output_shape;
54 
55   auto sqrt_op = new TensorFlowSqrtOperator;
56   sqrt_op->inputs = {avgpool_op->outputs[0]};
57   sqrt_op->outputs = {"Sqrtoutput"};
58 
59   /*Stack everything with the model*/
60   model.operators.push_back(std::unique_ptr<Operator>(sqrt_op));
61   model.operators.push_back(std::unique_ptr<Operator>(avgpool_op));
62   model.operators.push_back(std::unique_ptr<Operator>(sq_op));
63 
64   bool modified;
65   ASSERT_TRUE(IdentifyL2Pool().Run(&model, 0, &modified).ok());
66   for (auto& op_it : model.operators) {
67     Operator* op = op_it.get();
68     // Since the optimization has kicked in we should not find any
69     // Square, avgpool & Sqrt  operators
70     EXPECT_FALSE(op->type == OperatorType::kSqrt);
71     EXPECT_FALSE(op->type == OperatorType::kAveragePool);
72     EXPECT_FALSE(op->type == OperatorType::kSquare);
73   }
74 }
75 }  // namespace
76 
TEST(IdentifyL2Pool,SimpleTest)77 TEST(IdentifyL2Pool, SimpleTest) {
78   RunIdentifyL2Pool(
79       // Input data
80       {3, 1, 4, 1, -5, 9, -2, 6, 5, 3, 5, 8},
81 
82       // Input shape
83       {3, 4},
84 
85       {3, 4});
86 }
87 
88 }  // namespace toco
89