xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/testing/feature_parity/generators/add.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
16 #include "tensorflow/lite/delegates/gpu/common/testing/feature_parity/generators/add.h"
17 
18 #include <stdint.h>
19 
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "flatbuffers/flatbuffers.h"  // from @flatbuffers
25 #include "tensorflow/lite/delegates/gpu/common/testing/feature_parity/utils.h"
26 #include "tensorflow/lite/version.h"
27 
28 namespace tflite {
29 namespace {
30 
31 class AddModelBuilder {
32  public:
Build()33   std::vector<uint8_t> Build() {
34     flatbuffers::FlatBufferBuilder builder;
35     flatbuffers::Offset<OperatorCode> operator_code =
36         CreateOperatorCode(builder, BuiltinOperator_ADD, 0);
37 
38     flatbuffers::Offset<AddOptions> add_options =
39         CreateAddOptions(builder, ActivationFunctionType_NONE);
40 
41     flatbuffers::Offset<Buffer> buffers[1] = {
42         CreateBuffer(builder, builder.CreateVector({})),
43     };
44     std::vector<flatbuffers::Offset<Tensor>> tensors;
45     for (int8_t i = 0; i < input_shapes_.size(); i++) {
46       tensors.push_back(CreateTensor(
47           builder, builder.CreateVector<int32_t>(input_shapes_[i].data(), 4),
48           TensorType_FLOAT32,
49           /*buffer=*/0, builder.CreateString(std::to_string(i))));
50     }
51     tensors.push_back(CreateTensor(
52         builder, builder.CreateVector<int32_t>(output_shape_.data(), 4),
53         TensorType_FLOAT32,
54         /*buffer=*/0,
55         builder.CreateString(std::to_string(input_shapes_.size()))));
56 
57     const int32_t op_inputs[2] = {0, 1};
58     const int32_t op_outputs[1] = {2};
59 
60     flatbuffers::Offset<Operator> op =
61         CreateOperator(builder, /*opcode_index=*/0,
62                        builder.CreateVector<int32_t>(op_inputs, 2),
63                        builder.CreateVector<int32_t>(op_outputs, 1),
64                        BuiltinOptions_AddOptions, add_options.Union());
65 
66     int32_t subgraph_inputs[2] = {0, 1};
67     int32_t subgraph_outputs[1] = {2};
68     flatbuffers::Offset<SubGraph> subgraph =
69         CreateSubGraph(builder, builder.CreateVector(&tensors[0], 3),
70                        builder.CreateVector<int32_t>(subgraph_inputs, 2),
71                        builder.CreateVector<int32_t>(subgraph_outputs, 1),
72                        builder.CreateVector(&op, 1));
73 
74     flatbuffers::Offset<flatbuffers::String> description =
75         builder.CreateString("Add model");
76     flatbuffers::Offset<Model> model_buffer = CreateModel(
77         builder, TFLITE_SCHEMA_VERSION, builder.CreateVector(&operator_code, 1),
78         builder.CreateVector(&subgraph, 1), description,
79         builder.CreateVector(buffers, 1));
80 
81     builder.Finish(model_buffer);
82     return std::vector<uint8_t>(builder.GetBufferPointer(),
83                                 builder.GetBufferPointer() + builder.GetSize());
84   }
85 
SetInputShape(uint32_t input,std::vector<int32_t> && shape)86   void SetInputShape(uint32_t input, std::vector<int32_t>&& shape) {
87     if (input_shapes_.size() <= input) {
88       input_shapes_.resize(input + 1);
89     }
90 
91     input_shapes_[input] = std::move(shape);
92   }
93 
SetOutputShape(std::vector<int32_t> && shape)94   void SetOutputShape(std::vector<int32_t>&& shape) {
95     output_shape_ = std::move(shape);
96   }
97 
98  private:
99   std::vector<std::vector<int32_t>> input_shapes_;
100   std::vector<int32_t> output_shape_;
101 };
102 }  // namespace
103 
Add2SameShapeTensors()104 TestParams Add2SameShapeTensors() {
105   AddModelBuilder builder;
106   builder.SetInputShape(0, {1, 2, 2, 2});
107   builder.SetInputShape(1, {1, 2, 2, 2});
108   builder.SetOutputShape({1, 2, 2, 2});
109   return {"Add2SameShapeTensors", builder.Build()};
110 }
111 
AddBroadcast()112 TestParams AddBroadcast() {
113   AddModelBuilder builder;
114   builder.SetInputShape(0, {1, 2, 2, 2});
115   builder.SetInputShape(1, {1, 1, 1, 2});
116   builder.SetOutputShape({1, 2, 2, 2});
117   return {"AddBroadcast", builder.Build()};
118 }
119 
120 }  // namespace tflite
121