blob: a1b12aa25c4cf7a5b0ed4ffee3f1652b56024987 [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 Kunzefb90dd42023-03-08 13:07:07 -080030#define TOSA_VERSION_MINOR 60
Kevin Chengb97cb1d2021-10-14 11:53:39 -070031#define TOSA_VERSION_PATCH 0
Eric Kunzefb90dd42023-03-08 13:07:07 -080032#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,
Jerry Ge13c78a62022-10-04 20:32:39 -0700219 const std::string& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000220 const std::vector<TosaSerializationOperator*>& operators,
221 const std::vector<TosaSerializationTensor*>& tensors,
222 const std::vector<std::string>& inputs,
223 const std::vector<std::string>& outputs);
224 TosaSerializationBasicBlock(std::string&& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700225 std::string&& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000226 std::vector<TosaSerializationOperator*>&& operators,
227 std::vector<TosaSerializationTensor*>&& tensors,
228 std::vector<std::string>&& inputs,
229 std::vector<std::string>&& outputs);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700230 ~TosaSerializationBasicBlock();
231
232 // accessor
233 std::string GetName() const
234 {
235 return _name;
236 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700237 std::string GetRegionName() const
238 {
239 return _region_name;
240 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700241 std::vector<TosaSerializationOperator*>& GetOperators()
242 {
243 return _operators;
244 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700245
Eric Kunze2364dcd2021-04-26 11:06:57 -0700246 std::vector<TosaSerializationTensor*>& GetTensors()
247 {
248 return _tensors;
249 }
250
251 TosaSerializationTensor* GetTensorByName(std::string name)
252 {
253 TosaSerializationTensor* result = nullptr;
254 for (auto tensor : GetTensors())
255 {
256 if (tensor->GetName() == name)
257 {
258 result = tensor;
259 break;
260 }
261 }
262 return result;
263 }
264
265 std::vector<std::string>& GetInputs()
266 {
267 return _inputs;
268 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700269
Eric Kunze2364dcd2021-04-26 11:06:57 -0700270 std::vector<std::string>& GetOutputs()
271 {
272 return _outputs;
273 }
274
275private:
Jerry Ge13c78a62022-10-04 20:32:39 -0700276 std::string _name; /* name of basic block */
277 std::string _region_name;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700278 std::vector<TosaSerializationOperator*> _operators; /* TosaSerializationOperator list */
279 std::vector<TosaSerializationTensor*> _tensors; /* TosaSerializationTensor list */
280 std::vector<std::string> _inputs; /* array of string to specify block inputs */
281 std::vector<std::string> _outputs; /* array of string to specify block outputs */
282};
283
Jerry Ge13c78a62022-10-04 20:32:39 -0700284class TosaSerializationRegion
285{
286public:
287 // constructor and desctructor
288 TosaSerializationRegion(const std::string& name, const std::vector<TosaSerializationBasicBlock*>& blocks);
289 TosaSerializationRegion(const std::string&& name, const std::vector<TosaSerializationBasicBlock*>&& blocks);
290 ~TosaSerializationRegion();
291
292 // accessors
293 std::string GetName() const
294 {
295 return this->_name;
296 }
297
298 std::vector<TosaSerializationBasicBlock*>& GetBlocks()
299 {
300 return this->_blocks;
301 }
302
303 TosaSerializationBasicBlock* GetBlockByName(std::string name)
304 {
305 TosaSerializationBasicBlock* result = nullptr;
306 for (auto block : GetBlocks())
307 {
308 if (block->GetName() == name)
309 {
310 result = block;
311 break;
312 }
313 }
314 return result;
315 }
316
317private:
318 std::string _name; /* name of basic block */
319 std::vector<TosaSerializationBasicBlock*> _blocks; /* TosaSerializationBasicBlock list */
320};
321
Eric Kunze2364dcd2021-04-26 11:06:57 -0700322/*
323 * this is a helper class for writing/reading Tosa ISA
324 * supported format: .tosa (flatbuffer), .json
325 * and provide high-level std::vector-like interface
326 * to access internal data structure
327 */
328class TosaSerializationHandler
329{
330public:
331 // constructor and destructor
332 TosaSerializationHandler();
333 ~TosaSerializationHandler();
334
335 // file io
336 tosa_err_t LoadFileJson(const char* filename);
337 tosa_err_t LoadFileTosaFlatbuffer(const char* filename);
Aaron DeBattista8b3903a2021-11-18 16:38:11 +0000338 tosa_err_t LoadFileTosaFlatbuffer(const void* input, int in_size);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700339 tosa_err_t SaveFileJson(const char* filename);
340 tosa_err_t SaveFileTosaFlatbuffer(const char* filename);
341 tosa_err_t LoadFileSchema(const char* schema_filename);
342
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700343 // data format conversion. little-endian.
James Ward485a11d2022-08-05 13:48:37 +0100344 static tosa_err_t ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700345 static tosa_err_t ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out);
346 static tosa_err_t ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out);
347 static tosa_err_t ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out);
348 static tosa_err_t ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out);
349 static tosa_err_t ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out);
Kevin Cheng3ce56342021-07-28 13:42:29 -0700350 static tosa_err_t ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700351 static tosa_err_t ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out);
352
James Ward485a11d2022-08-05 13:48:37 +0100353 static tosa_err_t ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700354 static tosa_err_t ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out);
355 static tosa_err_t ConvertU8toI48(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int64_t>& out);
356 static tosa_err_t ConvertU8toI32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int32_t>& out);
357 static tosa_err_t ConvertU8toI16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int16_t>& out);
358 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 -0700359 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 -0700360 static tosa_err_t ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out);
361
Eric Kunze2364dcd2021-04-26 11:06:57 -0700362 // version
Kevin Chenge6563f52021-10-20 12:12:02 -0700363 const TosaVersion& GetVersion()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700364 {
365 return _version;
366 }
367
368 // accessor
Jerry Ge13c78a62022-10-04 20:32:39 -0700369 std::vector<TosaSerializationRegion*>& GetRegions()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700370 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700371 return _regions;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700372 }
373
Jerry Ge13c78a62022-10-04 20:32:39 -0700374 TosaSerializationRegion* GetMainRegion()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700375 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700376 return _regions[0];
377 }
378
379 TosaSerializationRegion* GetRegionByName(std::string name)
380 {
381 TosaSerializationRegion* result = nullptr;
382 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700383 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700384 if (region->GetName() == name)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700385 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700386 result = region;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700387 break;
388 }
389 }
390 return result;
391 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700392
393 bool GetSchemaLoaded() const
394 {
395 return _schemaLoaded;
396 }
397
398protected:
399 tosa_err_t Clear();
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700400 tosa_err_t Deserialize(const uint8_t* buf);
401 tosa_err_t Serialize();
Kevin Chenga81a7a12021-11-10 14:07:34 -0800402 TosaVersion ParseTosaSchemaVersion(std::string schema);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700403
404private:
Jerry Ge13c78a62022-10-04 20:32:39 -0700405 TosaVersion _version; /* version struct */
406 flatbuffers::FlatBufferBuilder _builder; /* flatbuffer builder */
407 flatbuffers::Parser _parser; /* flatbuffer parser, used for json parsing */
408 std::vector<TosaSerializationRegion*> _regions; /* array structure to store all TosaSerializationRegion */
409 bool _schemaLoaded; /* is the schema properly loaded? */
Eric Kunze2364dcd2021-04-26 11:06:57 -0700410};
411
412} // namespace tosa
413
414#endif // _TOSA_SERIALIZATION_HANDLER_H