blob: d3ee7f4dabeb1ed41eaa7da9f97350c09594ffb6 [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 Kunze9e2e0bb2022-12-05 23:23:54 +000030#define TOSA_VERSION_MINOR 50
Kevin Chengb97cb1d2021-10-14 11:53:39 -070031#define TOSA_VERSION_PATCH 0
Eric Kunze9e2e0bb2022-12-05 23:23:54 +000032#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.
James Ward485a11d2022-08-05 13:48:37 +0100297 static tosa_err_t ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700298 static tosa_err_t ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out);
299 static tosa_err_t ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out);
300 static tosa_err_t ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out);
301 static tosa_err_t ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out);
302 static tosa_err_t ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out);
Kevin Cheng3ce56342021-07-28 13:42:29 -0700303 static tosa_err_t ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700304 static tosa_err_t ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out);
305
James Ward485a11d2022-08-05 13:48:37 +0100306 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 -0700307 static tosa_err_t ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out);
308 static tosa_err_t ConvertU8toI48(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int64_t>& out);
309 static tosa_err_t ConvertU8toI32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int32_t>& out);
310 static tosa_err_t ConvertU8toI16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int16_t>& out);
311 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 -0700312 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 -0700313 static tosa_err_t ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out);
314
Eric Kunze2364dcd2021-04-26 11:06:57 -0700315 // version
Kevin Chenge6563f52021-10-20 12:12:02 -0700316 const TosaVersion& GetVersion()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700317 {
318 return _version;
319 }
320
321 // accessor
322 std::vector<TosaSerializationBasicBlock*>& GetBlocks()
323 {
324 return _blocks;
325 }
326
327 TosaSerializationBasicBlock* GetBlockByName(std::string name)
328 {
329 TosaSerializationBasicBlock* result = nullptr;
330 for (auto block : GetBlocks())
331 {
332 if (block->GetName() == name)
333 {
334 result = block;
335 break;
336 }
337 }
338 return result;
339 }
340 TosaSerializationBasicBlock* GetMainBlock()
341 {
342 TosaSerializationBasicBlock* main_block = GetBlockByName(std::string("main"));
343 assert(main_block);
344 return main_block;
345 }
346
347 std::vector<std::string>& GetInputs()
348 {
349 return GetMainBlock()->GetInputs();
350 }
351 std::vector<std::string>& GetOutputs()
352 {
353 return GetMainBlock()->GetOutputs();
354 }
355
356 bool GetSchemaLoaded() const
357 {
358 return _schemaLoaded;
359 }
360
361protected:
362 tosa_err_t Clear();
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700363 tosa_err_t Deserialize(const uint8_t* buf);
364 tosa_err_t Serialize();
Kevin Chenga81a7a12021-11-10 14:07:34 -0800365 TosaVersion ParseTosaSchemaVersion(std::string schema);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700366
367private:
Kevin Chenge6563f52021-10-20 12:12:02 -0700368 TosaVersion _version; /* version struct */
Eric Kunze2364dcd2021-04-26 11:06:57 -0700369 flatbuffers::FlatBufferBuilder _builder; /* flatbuffer builder */
370 flatbuffers::Parser _parser; /* flatbuffer parser, used for json parsing */
371 std::vector<TosaSerializationBasicBlock*> _blocks; /* array structure to store all TosaSerializationBasicBlock */
372 bool _schemaLoaded; /* is the schema properly loaded? */
373};
374
375} // namespace tosa
376
377#endif // _TOSA_SERIALIZATION_HANDLER_H