• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #ifndef TENSORFLOW_LITE_KERNELS_RESHAPE_TEST_COMMON_H_
16 #define TENSORFLOW_LITE_KERNELS_RESHAPE_TEST_COMMON_H_
17 
18 #include <stdint.h>
19 
20 #include <initializer_list>
21 #include <string>
22 #include <vector>
23 
24 #include "tensorflow/lite/kernels/test_util.h"
25 #include "tensorflow/lite/schema/schema_generated.h"
26 #include "tensorflow/lite/string_type.h"
27 
28 namespace tflite {
29 // There are three ways to specify the output shape of a Reshape
30 // op.
31 enum class ShapeSpecificationType {
32   // The output shape is hardcoded in the ReshapeOptions object.
33   kAsReshapeOption,
34   // The output shape is specified as an input tensor, which is connected to a
35   // Const node, which is guaranteed not to change once inference starts. The
36   // shape is also hardcoded as in kAsReshapeOption.
37   kAsConstantTensor,
38   // The output shape is specified as an input tensor that can change based on
39   // external input. That is, the shape is not know before the inference
40   // starts. The shape is also hardcoded as in kAsReshapeOption.
41   kAsTensor,
42 };
43 
44 template <typename T, typename BASE = SingleOpModel>
45 class ReshapeOpModel : public BASE {
46  public:
ReshapeOpModel(std::initializer_list<int> input_shape,std::initializer_list<int> shape_shape,std::initializer_list<int> shape_data,ShapeSpecificationType shape_type)47   ReshapeOpModel(std::initializer_list<int> input_shape,
48                  std::initializer_list<int> shape_shape,
49                  std::initializer_list<int> shape_data,
50                  ShapeSpecificationType shape_type) {
51     switch (shape_type) {
52       case ShapeSpecificationType::kAsTensor:
53         this->BuildWithTensorShape(input_shape, shape_shape, shape_data);
54         break;
55       case ShapeSpecificationType::kAsConstantTensor:
56         this->BuildWithConstantTensorShape(input_shape, shape_shape,
57                                            shape_data);
58         break;
59       case ShapeSpecificationType::kAsReshapeOption:
60         // In this case the shape of the new shape doesn't matter. It is
61         // always hardcoded as a flat vector.
62         this->BuildWithHardcodedShape(input_shape, shape_data);
63         break;
64     }
65   }
66 
SetInput(std::vector<T> data)67   void SetInput(std::vector<T> data) {
68     this->template PopulateTensor<T>(input_, data);
69   }
70 
SetStringInput(std::initializer_list<string> data)71   void SetStringInput(std::initializer_list<string> data) {
72     this->PopulateStringTensor(input_, data);
73   }
74 
GetOutput()75   std::vector<T> GetOutput() {
76     return this->template ExtractVector<T>(output_);
77   }
GetOutputShape()78   std::vector<int> GetOutputShape() { return this->GetTensorShape(output_); }
79 
80  private:
BuildWithHardcodedShape(std::initializer_list<int> input_shape,std::initializer_list<int> shape_data)81   void BuildWithHardcodedShape(std::initializer_list<int> input_shape,
82                                std::initializer_list<int> shape_data) {
83     input_ = this->AddInput({GetTensorType<T>(), input_shape});
84     output_ = this->AddOutput(GetTensorType<T>());
85     this->SetBuiltinOp(
86         BuiltinOperator_RESHAPE, BuiltinOptions_ReshapeOptions,
87         CreateReshapeOptions(
88             this->builder_,
89             this->builder_.template CreateVector<int>(shape_data))
90             .Union());
91     this->BuildInterpreter({this->GetShape(input_)});
92   }
93 
BuildWithTensorShape(std::initializer_list<int> input_shape,std::initializer_list<int> shape_shape,std::initializer_list<int> shape_data)94   void BuildWithTensorShape(std::initializer_list<int> input_shape,
95                             std::initializer_list<int> shape_shape,
96                             std::initializer_list<int> shape_data) {
97     input_ = this->AddInput({GetTensorType<T>(), input_shape});
98     output_ = this->AddOutput(GetTensorType<T>());
99     int shape_input_tensor = this->AddInput({TensorType_INT32, shape_shape});
100     // Note how shape also appears in ReshapeOptions
101     this->SetBuiltinOp(
102         BuiltinOperator_RESHAPE, BuiltinOptions_ReshapeOptions,
103         CreateReshapeOptions(
104             this->builder_,
105             this->builder_.template CreateVector<int>(shape_data))
106             .Union());
107     this->BuildInterpreter(
108         {this->GetShape(input_), this->GetShape(shape_input_tensor)});
109     if (shape_data.size() != 0) {
110       this->template PopulateTensor<int32_t>(shape_input_tensor, shape_data);
111     }
112   }
113 
BuildWithConstantTensorShape(std::initializer_list<int> input_shape,std::initializer_list<int> shape_shape,std::initializer_list<int> shape_data)114   void BuildWithConstantTensorShape(std::initializer_list<int> input_shape,
115                                     std::initializer_list<int> shape_shape,
116                                     std::initializer_list<int> shape_data) {
117     input_ = this->AddInput({GetTensorType<T>(), input_shape});
118     output_ = this->AddOutput(GetTensorType<T>());
119     this->AddConstInput(TensorType_INT32, shape_data, shape_shape);
120     // Note how the shape also appears in the ReshapeOptions.
121     this->SetBuiltinOp(
122         BuiltinOperator_RESHAPE, BuiltinOptions_ReshapeOptions,
123         CreateReshapeOptions(
124             this->builder_,
125             this->builder_.template CreateVector<int>(shape_data))
126             .Union());
127     this->BuildInterpreter({this->GetShape(input_)});
128   }
129 
130   int input_;
131   int output_;
132 };
133 
134 }  // namespace tflite
135 
136 #endif  // TENSORFLOW_LITE_KERNELS_RESHAPE_TEST_COMMON_H_
137