Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / CircleGen.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __NNFW_API_TEST_CIRCLE_GEN_H__
18 #define __NNFW_API_TEST_CIRCLE_GEN_H__
19
20 #include <circle_schema_generated.h>
21
22 #include <vector>
23
24 /**
25  * @brief Class for storing flatbuffer buffer
26  *
27  * This is a simple wrapper for a finished FlatBufferBuilder. It owns the buffer and a user can
28  * get the buffer pointer and size.
29  */
30 class CircleBuffer
31 {
32 public:
33   CircleBuffer() = default;
34   explicit CircleBuffer(flatbuffers::FlatBufferBuilder &&fbb) : _fbb{std::move(fbb)}
35   {
36     _fbb.Finished(); // The build must have been finished, so check that here
37   }
38
39   uint8_t *buffer() const { return _fbb.GetBufferPointer(); }
40   size_t size() const { return _fbb.GetSize(); }
41
42 private:
43   flatbuffers::FlatBufferBuilder _fbb;
44 };
45
46 /**
47  * @brief Circle flatbuffer file generator
48  *
49  * This is a helper class for generating circle file.
50  *
51  */
52 class CircleGen
53 {
54 public:
55   struct TensorParams
56   {
57     std::vector<int32_t> shape;
58     circle::TensorType tensor_type = circle::TensorType::TensorType_FLOAT32;
59     uint32_t buffer = 0;
60     std::string name;
61   };
62
63   struct OperatorParams
64   {
65     std::vector<int32_t> inputs;
66     std::vector<int32_t> outputs;
67     int version = 1;
68   };
69
70   struct SubgraphContext
71   {
72     std::vector<int> inputs;
73     std::vector<int> outputs;
74     std::vector<flatbuffers::Offset<circle::Tensor>> tensors;
75     std::vector<flatbuffers::Offset<circle::Operator>> operators;
76   };
77
78 public:
79   CircleGen();
80
81   template <typename T> uint32_t addBuffer(const std::vector<T> &buf_vec)
82   {
83     auto buf = reinterpret_cast<const uint8_t *>(buf_vec.data());
84     auto size = buf_vec.size() * sizeof(T);
85     return addBuffer(buf, size);
86   }
87   uint32_t addBuffer(const uint8_t *buf, size_t size);
88   uint32_t addTensor(const TensorParams &params);
89   void setInputsAndOutputs(const std::vector<int> &inputs, const std::vector<int> &outputs);
90   uint32_t nextSubgraph();
91   CircleBuffer finish();
92
93   // ===== Add Operator methods begin =====
94
95   uint32_t addOperatorAdd(const OperatorParams &params, circle::ActivationFunctionType actfn);
96   uint32_t addOperatorAveragePool2D(const OperatorParams &params, circle::Padding padding,
97                                     int stride_w, int stride_h, int filter_w, int filter_h,
98                                     circle::ActivationFunctionType actfn);
99   uint32_t addOperatorConcatenation(const OperatorParams &params, int axis,
100                                     circle::ActivationFunctionType actfn);
101   uint32_t addOperatorCos(const OperatorParams &params);
102   uint32_t addOperatorL2Normalization(const OperatorParams &params);
103   uint32_t addOperatorLeakyRelu(const OperatorParams &params, float alpha);
104   uint32_t addOperatorLess(const OperatorParams &params);
105   uint32_t addOperatorNeg(const OperatorParams &params);
106   uint32_t addOperatorPad(const OperatorParams &params);
107   uint32_t addOperatorPadV2(const OperatorParams &params);
108   uint32_t addOperatorRank(const OperatorParams &params);
109   uint32_t addOperatorResizeNearestNeighbor(const OperatorParams &params);
110   uint32_t addOperatorWhile(const OperatorParams &params, uint32_t cond_subg, uint32_t body_subg);
111
112   // NOTE Please add addOperator functions ABOVE this lie
113   // ===== Add Operator methods end =====
114
115 private:
116   uint32_t addOperatorWithOptions(const OperatorParams &params, circle::BuiltinOperator opcode,
117                                   circle::BuiltinOptions options_type,
118                                   flatbuffers::Offset<void> options);
119   uint32_t addOperatorCode(circle::BuiltinOperator opcode);
120   flatbuffers::Offset<circle::Buffer> buildBuffer(const uint8_t *buf, size_t size);
121   flatbuffers::Offset<circle::Tensor> buildTensor(const TensorParams &params);
122   flatbuffers::Offset<circle::SubGraph> buildSubGraph(const SubgraphContext &ctx);
123
124   SubgraphContext &curSubgCtx() { return _subgraph_contexts.back(); }
125
126 private:
127   flatbuffers::FlatBufferBuilder _fbb{1024};
128   std::vector<flatbuffers::Offset<circle::Buffer>> _buffers;
129   std::vector<flatbuffers::Offset<circle::OperatorCode>> _opcodes;
130   std::vector<SubgraphContext> _subgraph_contexts;
131 };
132
133 #endif // __NNFW_API_TEST_CIRCLE_GEN_H__