blob: 301adb8894c0195eafb0b437b1074ba3570f2dee [file] [log] [blame]
Eric Kunze2364dcd2021-04-26 11:06:57 -07001
2// Copyright (c) 2020-2021, ARM Limited.
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#ifndef _TOSA_SERIALIZATION_HANDLER_H
17#define _TOSA_SERIALIZATION_HANDLER_H
18#include "attribute.h"
19#include "flatbuffers/idl.h"
20#include "flatbuffers/util.h"
21#include "numpy_utils.h"
Eric Kunze2364dcd2021-04-26 11:06:57 -070022#include "tosa_generated.h"
23#include <cstdint>
24#include <memory>
25#include <string>
26#include <vector>
27
Kevin Chenge6563f52021-10-20 12:12:02 -070028// Keep version number in sync with the version default value with schema/tosa.fbs
Kevin Chengb97cb1d2021-10-14 11:53:39 -070029#define TOSA_VERSION_MAJOR 0
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000030#define TOSA_VERSION_MINOR 30
Kevin Chengb97cb1d2021-10-14 11:53:39 -070031#define TOSA_VERSION_PATCH 0
Eric Kunze8fcef212022-06-16 15:45:39 -070032#define TOSA_VERSION_DRAFT false
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070033#define TENSOR_BUFFER_FORCE_ALIGNMENT 8
34
Eric Kunze2364dcd2021-04-26 11:06:57 -070035namespace tosa
36{
37
38enum tosa_err_t
39{
40 TOSA_OK,
41 TOSA_USER_ERROR,
42 TOSA_FILE_ERROR,
43 TOSA_MEMORY_ERROR,
44 TOSA_SCHEMA_MISSING,
45 TOSA_INTERNAL_ERROR,
46 TOSA_VERSION_MISMATCH,
47 NUM_TOSA_ERROR
48};
49
Kevin Chenge6563f52021-10-20 12:12:02 -070050struct TosaVersion
51{
52 int32_t _major;
53 int32_t _minor;
54 int32_t _patch;
55 bool _draft;
56
57 enum class compat_t
58 {
59 COMPLETELY_COMPATIBLE,
60 PARTIALLY_COMPATIBLE,
61 NOT_COMPATIBLE
62 };
63
64 TosaVersion() = default;
65 TosaVersion(int32_t major, int32_t minor, int32_t patch, bool draft)
66 {
67 set_version(major, minor, patch, draft);
68 }
69
70 void set_version(int32_t major, int32_t minor, int32_t patch, bool draft)
71 {
72 _major = major;
73 _minor = minor;
74 _patch = patch;
75 _draft = draft;
76 }
77
78 std::string to_string() const
79 {
80 std::string str;
81 str += std::to_string(_major) + ".";
82 str += std::to_string(_minor) + ".";
83 str += std::to_string(_patch);
84 if (_draft)
85 str += "d";
86 return str;
87 }
88
89 compat_t is_compatible(const TosaVersion& rhs) const
90 {
91 if (rhs._major == _major && rhs._minor == _minor)
92 {
93 if (rhs._patch == _patch && rhs._draft == _draft)
94 {
95 return TosaVersion::compat_t::COMPLETELY_COMPATIBLE;
96 }
97 else
98 {
99 return TosaVersion::compat_t::PARTIALLY_COMPATIBLE;
100 }
101 }
102 return TosaVersion::compat_t::NOT_COMPATIBLE;
103 }
104};
105
Eric Kunze2364dcd2021-04-26 11:06:57 -0700106class TosaSerializationHandler;
107
108class TosaSerializationTensor
109{
110public:
111 // constructor and destructor
112 TosaSerializationTensor(const flatbuffers::String* name,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700113 const flatbuffers::Vector<int32_t>* shape,
Eric Kunze2364dcd2021-04-26 11:06:57 -0700114 DType dtype,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700115 const flatbuffers::Vector<uint8_t>* data);
Kevin Cheng545a5082021-11-11 01:36:33 +0000116 TosaSerializationTensor(const std::string& name,
Eric Kunze2364dcd2021-04-26 11:06:57 -0700117 const std::vector<int32_t>& shape,
118 DType dtype,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700119 const std::vector<uint8_t>& data);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700120 TosaSerializationTensor();
121 ~TosaSerializationTensor();
122
123 // accessor
124 std::string GetName() const
125 {
126 return _name;
127 }
128 const std::vector<int32_t>& GetShape() const
129 {
130 return _shape;
131 }
132 DType GetDtype()
133 {
134 return _dtype;
135 }
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700136 const std::vector<uint8_t>& GetData() const
Eric Kunze2364dcd2021-04-26 11:06:57 -0700137 {
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700138 return _data;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700139 }
140
141 // modifier
142 void SetDtype(DType dtype)
143 {
144 _dtype = dtype;
145 }
146 void SetName(std::string name)
147 {
148 _name = name;
149 }
Kevin Cheng545a5082021-11-11 01:36:33 +0000150 void SetData(const std::vector<uint8_t>& data)
151 {
152 _data = data;
153 }
154 void SetData(std::vector<uint8_t>&& data)
155 {
156 _data = std::move(data);
157 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700158
159private:
160 DType _dtype; /* data type enumeration, see tosa_isa_generated.h */
161 std::vector<int32_t> _shape; /* shape of the tensor */
162 std::string _name; /* name of the tensor, used for solving dependency */
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700163 std::vector<uint8_t> _data; /* data array */
Eric Kunze2364dcd2021-04-26 11:06:57 -0700164};
165
166class TosaSerializationOperator
167{
168public:
169 // use default copy, void constructor
170 // constructor and destructor
171 TosaSerializationOperator(Op op,
172 Attribute attribute_type,
173 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +0000174 const std::vector<std::string>& input_tensor_names,
175 const std::vector<std::string>& output_tensor_names);
176 TosaSerializationOperator(Op op,
177 Attribute attribute_type,
178 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +0000179 std::vector<std::string>&& input_tensor_names,
180 std::vector<std::string>&& output_tensor_names);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700181 ~TosaSerializationOperator();
182
183 // accessor
184 Op GetOp() const
185 {
186 return _op;
187 }
188 Attribute GetAttributeType() const
189 {
190 return _attribute_type;
191 }
192 TosaAttributeBase* GetAttribute() const
193 {
194 return _attribute;
195 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700196 std::vector<std::string>& GetInputTensorNames()
197 {
198 return _input_tensor_names;
199 }
200 std::vector<std::string>& GetOutputTensorNames()
201 {
202 return _output_tensor_names;
203 }
204
205private:
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000206 void InitializeAttribute(Attribute attribute_type, const TosaAttributeBase* attribute);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700207 Op _op; /* operator enum, see tosa_isa_generated.h for enumeration table */
208 Attribute _attribute_type; /* operator attribute enum, used for dynamic casting TosaAttributeBase class */
209 TosaAttributeBase* _attribute; /* real attribute class goes here */
Eric Kunze2364dcd2021-04-26 11:06:57 -0700210 std::vector<std::string> _input_tensor_names; /* array of input tensor names */
211 std::vector<std::string> _output_tensor_names; /* array of output tensor names */
212};
213
214class TosaSerializationBasicBlock
215{
216public:
217 // constructor and destructor
Kevin Cheng545a5082021-11-11 01:36:33 +0000218 TosaSerializationBasicBlock(const std::string& name,
219 const std::vector<TosaSerializationOperator*>& operators,
220 const std::vector<TosaSerializationTensor*>& tensors,
221 const std::vector<std::string>& inputs,
222 const std::vector<std::string>& outputs);
223 TosaSerializationBasicBlock(std::string&& name,
224 std::vector<TosaSerializationOperator*>&& operators,
225 std::vector<TosaSerializationTensor*>&& tensors,
226 std::vector<std::string>&& inputs,
227 std::vector<std::string>&& outputs);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700228 ~TosaSerializationBasicBlock();
229
230 // accessor
231 std::string GetName() const
232 {
233 return _name;
234 }
235 std::vector<TosaSerializationOperator*>& GetOperators()
236 {
237 return _operators;
238 }
239 std::vector<TosaSerializationTensor*>& GetTensors()
240 {
241 return _tensors;
242 }
243
244 TosaSerializationTensor* GetTensorByName(std::string name)
245 {
246 TosaSerializationTensor* result = nullptr;
247 for (auto tensor : GetTensors())
248 {
249 if (tensor->GetName() == name)
250 {
251 result = tensor;
252 break;
253 }
254 }
255 return result;
256 }
257
258 std::vector<std::string>& GetInputs()
259 {
260 return _inputs;
261 }
262 std::vector<std::string>& GetOutputs()
263 {
264 return _outputs;
265 }
266
267private:
268 std::string _name; /* name of basic block */
269 std::vector<TosaSerializationOperator*> _operators; /* TosaSerializationOperator list */
270 std::vector<TosaSerializationTensor*> _tensors; /* TosaSerializationTensor list */
271 std::vector<std::string> _inputs; /* array of string to specify block inputs */
272 std::vector<std::string> _outputs; /* array of string to specify block outputs */
273};
274
275/*
276 * this is a helper class for writing/reading Tosa ISA
277 * supported format: .tosa (flatbuffer), .json
278 * and provide high-level std::vector-like interface
279 * to access internal data structure
280 */
281class TosaSerializationHandler
282{
283public:
284 // constructor and destructor
285 TosaSerializationHandler();
286 ~TosaSerializationHandler();
287
288 // file io
289 tosa_err_t LoadFileJson(const char* filename);
290 tosa_err_t LoadFileTosaFlatbuffer(const char* filename);
Aaron DeBattista8b3903a2021-11-18 16:38:11 +0000291 tosa_err_t LoadFileTosaFlatbuffer(const void* input, int in_size);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700292 tosa_err_t SaveFileJson(const char* filename);
293 tosa_err_t SaveFileTosaFlatbuffer(const char* filename);
294 tosa_err_t LoadFileSchema(const char* schema_filename);
295
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700296 // data format conversion. little-endian.
297 static tosa_err_t ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out);
298 static tosa_err_t ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out);
299 static tosa_err_t ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out);
300 static tosa_err_t ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out);
301 static tosa_err_t ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out);
Kevin Cheng3ce56342021-07-28 13:42:29 -0700302 static tosa_err_t ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700303 static tosa_err_t ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out);
304
305 static tosa_err_t ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out);
306 static tosa_err_t ConvertU8toI48(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int64_t>& out);
307 static tosa_err_t ConvertU8toI32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int32_t>& out);
308 static tosa_err_t ConvertU8toI16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int16_t>& out);
309 static tosa_err_t ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out);
Kevin Cheng3ce56342021-07-28 13:42:29 -0700310 static tosa_err_t ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700311 static tosa_err_t ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out);
312
Eric Kunze2364dcd2021-04-26 11:06:57 -0700313 // version
Kevin Chenge6563f52021-10-20 12:12:02 -0700314 const TosaVersion& GetVersion()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700315 {
316 return _version;
317 }
318
319 // accessor
320 std::vector<TosaSerializationBasicBlock*>& GetBlocks()
321 {
322 return _blocks;
323 }
324
325 TosaSerializationBasicBlock* GetBlockByName(std::string name)
326 {
327 TosaSerializationBasicBlock* result = nullptr;
328 for (auto block : GetBlocks())
329 {
330 if (block->GetName() == name)
331 {
332 result = block;
333 break;
334 }
335 }
336 return result;
337 }
338 TosaSerializationBasicBlock* GetMainBlock()
339 {
340 TosaSerializationBasicBlock* main_block = GetBlockByName(std::string("main"));
341 assert(main_block);
342 return main_block;
343 }
344
345 std::vector<std::string>& GetInputs()
346 {
347 return GetMainBlock()->GetInputs();
348 }
349 std::vector<std::string>& GetOutputs()
350 {
351 return GetMainBlock()->GetOutputs();
352 }
353
354 bool GetSchemaLoaded() const
355 {
356 return _schemaLoaded;
357 }
358
359protected:
360 tosa_err_t Clear();
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700361 tosa_err_t Deserialize(const uint8_t* buf);
362 tosa_err_t Serialize();
Kevin Chenga81a7a12021-11-10 14:07:34 -0800363 TosaVersion ParseTosaSchemaVersion(std::string schema);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700364
365private:
Kevin Chenge6563f52021-10-20 12:12:02 -0700366 TosaVersion _version; /* version struct */
Eric Kunze2364dcd2021-04-26 11:06:57 -0700367 flatbuffers::FlatBufferBuilder _builder; /* flatbuffer builder */
368 flatbuffers::Parser _parser; /* flatbuffer parser, used for json parsing */
369 std::vector<TosaSerializationBasicBlock*> _blocks; /* array structure to store all TosaSerializationBasicBlock */
370 bool _schemaLoaded; /* is the schema properly loaded? */
371};
372
373} // namespace tosa
374
375#endif // _TOSA_SERIALIZATION_HANDLER_H