blob: a3825427ad204e0ab89b77e69a636ea66a4c7842 [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#include "tosa_serialization_handler.h"
17
18#include <iostream>
19using namespace tosa;
20
21TosaSerializationTensor::TosaSerializationTensor(const flatbuffers::String* name,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070022 const flatbuffers::Vector<int32_t>* shape,
Eric Kunze2364dcd2021-04-26 11:06:57 -070023 DType dtype,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070024 const flatbuffers::Vector<uint8_t>* data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070025{
26 _dtype = dtype;
27
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070028 std::copy(shape->begin(), shape->end(), std::back_inserter(_shape));
Eric Kunze2364dcd2021-04-26 11:06:57 -070029
30 assert(name);
31 _name = name->str();
32
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070033 if (data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070034 {
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070035 std::copy(data->begin(), data->end(), std::back_inserter(_data));
Eric Kunze2364dcd2021-04-26 11:06:57 -070036 }
37}
38
39TosaSerializationTensor::TosaSerializationTensor(std::string& name,
40 const std::vector<int32_t>& shape,
41 DType dtype,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070042 const std::vector<uint8_t>& data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070043{
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070044 _dtype = dtype;
45 _shape = shape;
46 _name = name;
47 _data = data;
Eric Kunze2364dcd2021-04-26 11:06:57 -070048}
49
50TosaSerializationTensor::TosaSerializationTensor()
51{
52 _dtype = DType_UNKNOWN;
53
54 _name = "UNKNOWN";
55}
56
57TosaSerializationTensor::~TosaSerializationTensor()
58{}
59
60TosaSerializationOperator::TosaSerializationOperator(Op op,
61 Attribute attribute_type,
62 const TosaAttributeBase* attribute,
63 QuantInfo qinfo_type,
64 const TosaQuantInfoBase* qinfo,
65 std::vector<std::string> input_tensor_names,
66 std::vector<std::string> output_tensor_names)
67{
68 _op = op;
69 _attribute_type = attribute_type;
70
71 switch (attribute_type)
72 {
73 case Attribute_NONE:
74 _attribute = new TosaNoneAttribute();
75 break;
76#define DEF_ATTRIBUTE(NAME, ...) \
77 case Attribute_##NAME##Attribute: \
78 _attribute = new Tosa##NAME##Attribute(attribute); \
79 break;
80#include "attribute.def"
81#undef DEF_ATTRIBUTE
82 default:
83 printf("TosaSerializationOperator::TosaSerializationOperator(): Attribute %s not implemented yet\n",
84 EnumNamesAttribute()[attribute_type]);
85 assert(0);
86 }
87
88 _qinfo_type = qinfo_type;
89 switch (qinfo_type)
90 {
91 case QuantInfo_NONE:
92 _qinfo = new TosaNoneQuantInfo();
93 break;
94#define DEF_QUANTIZATION_INFO(NAME, ...) \
95 case QuantInfo_##NAME##QuantInfo: \
96 _qinfo = new Tosa##NAME##QuantInfo(qinfo); \
97 break;
98#include "quant_info.def"
99#undef DEF_QUANTIZATION_INFO
100 default:
101 printf("TosaSerializationOperator::TosaSerializationOperator(): QuantInfo %s not implemented yet\n",
102 EnumNamesQuantInfo()[qinfo_type]);
103 assert(0);
104 }
105
106 assert(_attribute && _qinfo);
107
108 _input_tensor_names = input_tensor_names;
109 _output_tensor_names = output_tensor_names;
110}
111
112TosaSerializationOperator::~TosaSerializationOperator()
113{
114 delete _attribute;
115 delete _qinfo;
116 // TosaSerializationTensor should be free'd in TosaSerializationSerializationHandler destructor
117}
118
119TosaSerializationBasicBlock::TosaSerializationBasicBlock(std::string name,
120 std::vector<TosaSerializationOperator*> operators,
121 std::vector<TosaSerializationTensor*> tensors,
122 std::vector<std::string> inputs,
123 std::vector<std::string> outputs)
124{
125
126 _name = name;
127 _operators = operators;
128 _tensors = tensors;
129 _inputs = inputs;
130 _outputs = outputs;
131}
132
133TosaSerializationBasicBlock::~TosaSerializationBasicBlock()
134{
135 // deallocate all operators
136 for (auto op : GetOperators())
137 {
138 delete op; // ~TosaSerializationOperator()
139 }
140
141 // deallocate all tensors
142 for (auto ts : GetTensors())
143 {
144 delete ts; // ~TosaSerializationTensor()
145 }
146}
147
148TosaSerializationHandler::TosaSerializationHandler()
149{
150 _schemaLoaded = false;
Kevin Chenge6563f52021-10-20 12:12:02 -0700151 _version = TosaVersion(TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700152}
153
154TosaSerializationHandler::~TosaSerializationHandler()
155{
156 Clear(); // deallocate all basic blocks
157}
158
Eric Kunze2364dcd2021-04-26 11:06:57 -0700159tosa_err_t TosaSerializationHandler::LoadFileSchema(const char* schema_filename)
160{
161 std::string schema;
162 bool ok;
163
164 ok = flatbuffers::LoadFile(schema_filename, false, &schema);
165 if (!ok)
166 {
167 printf("Error loading schema file: %s\n", schema_filename);
168 return TOSA_FILE_ERROR;
169 }
170
171 ok = _parser.Parse(schema.c_str());
172 if (!ok)
173 {
174 printf("Error parsing ISA schema file: %s\n", schema_filename);
175 return TOSA_FILE_ERROR;
176 }
177 _schemaLoaded = true;
178
179 return TOSA_OK;
180}
181
182tosa_err_t TosaSerializationHandler::LoadFileJson(const char* filename)
183{
184 std::string jsonfile;
185 bool ok;
186 tosa_err_t err;
187
188 if (!_schemaLoaded)
189 {
190 return TOSA_SCHEMA_MISSING;
191 }
192
193 ok = flatbuffers::LoadFile(filename, false, &jsonfile);
194 if (!ok)
195 {
196 printf("Error loading json file: %s\n", filename);
197 return TOSA_FILE_ERROR;
198 }
199
200 ok = _parser.Parse(jsonfile.c_str());
201 if (!ok)
202 {
203 printf("Error parsing json file: %s\n", filename);
204 return TOSA_FILE_ERROR;
205 }
206
207 uint8_t* buf = _parser.builder_.GetBufferPointer();
208
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700209 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700210 if (err != TOSA_OK)
211 {
212 return err;
213 }
214
215 return TOSA_OK;
216}
217
218tosa_err_t TosaSerializationHandler::SaveFileJson(const char* filename)
219{
220 std::string jsongen;
221 tosa_err_t err;
222
223 if (!_schemaLoaded)
224 {
225 return TOSA_SCHEMA_MISSING;
226 }
227
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700228 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700229 if (err != TOSA_OK)
230 {
231 return err;
232 }
233
234 uint8_t* buf = _builder.GetBufferPointer();
235
236 if (!GenerateText(_parser, buf, &jsongen))
237 {
238 printf("Couldn't serialize parsed data to JSON!\n");
239 return TOSA_FILE_ERROR;
240 }
241
242 FILE* file = fopen(filename, "wb");
243
244 if (!file)
245 {
246 printf("Couldn't open output file: %s\n", filename);
247 return TOSA_FILE_ERROR;
248 }
249
250 if (fwrite(jsongen.c_str(), sizeof(char), jsongen.size(), file) != jsongen.size())
251 {
252 printf("Error writing to json output file: %s\n", filename);
253 fclose(file);
254 return TOSA_FILE_ERROR;
255 }
256
257 if (file)
258 fclose(file);
259
260 return TOSA_OK;
261}
262
263tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const char* filename)
264{
265 std::string read_buffer;
266 tosa_err_t err;
267 uint8_t* buf;
268 bool ok;
269
270 ok = flatbuffers::LoadFile(filename, false, &read_buffer);
271 if (!ok)
272 {
273 printf("Error loading flatbuffer file: %s\n", filename);
274 return TOSA_FILE_ERROR;
275 }
276
277 buf = (uint8_t*)read_buffer.data();
278
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700279 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700280 if (err != TOSA_OK)
281 {
282 return err;
283 }
284
285 return TOSA_OK;
286}
287
288tosa_err_t TosaSerializationHandler::SaveFileTosaFlatbuffer(const char* filename)
289{
290 tosa_err_t err;
291
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700292 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700293 if (err != TOSA_OK)
294 {
295 return err;
296 }
297
298 uint8_t* buf = _builder.GetBufferPointer();
299
300 bool ok = flatbuffers::SaveFile(filename, (const char*)buf, _builder.GetSize(), false);
301 if (!ok)
302 {
303 printf("Error saving floatbuffer file: %s\n", filename);
304 return TOSA_FILE_ERROR;
305 }
306
307 return TOSA_OK;
308}
309
310tosa_err_t TosaSerializationHandler::Clear()
311{
312 // deallocate all basic blocks
313 for (auto bb : GetBlocks())
314 {
315 delete bb;
316 }
317 _blocks.clear();
318
319 return TOSA_OK;
320}
321
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700322tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700323{
324 auto fb_tosa_graph = GetTosaGraph(buf);
325 auto fb_tosa_version = fb_tosa_graph->version();
326 auto fb_tosa_blocks = fb_tosa_graph->blocks();
327
328 std::vector<std::string> operator_inputs_container;
329 std::vector<std::string> operator_outputs_container;
330
331 std::vector<TosaSerializationOperator*> block_operators_container;
332 std::vector<TosaSerializationTensor*> block_tensors_container;
333 std::vector<std::string> block_inputs_container;
334 std::vector<std::string> block_outputs_container;
335
336 TosaAttributeBase* typed_attribute = NULL;
337 TosaQuantInfoBase* typed_qinfo = NULL;
338 TosaSerializationOperator* new_operator = NULL;
339 TosaSerializationBasicBlock* new_block = NULL;
340 TosaSerializationTensor* new_tensor = NULL;
341
342 // erase container
343 Clear();
344
Kevin Chenge6563f52021-10-20 12:12:02 -0700345 TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(),
346 fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700347
Kevin Chenge6563f52021-10-20 12:12:02 -0700348 TosaVersion::compat_t is_compat = read_version.is_compatible(GetVersion());
349 switch (is_compat)
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700350 {
Kevin Chenge6563f52021-10-20 12:12:02 -0700351 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
352 break;
353 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
354 printf("WARNING: Read flatbuffer version %s is partially compatible with serializer version %s\n",
355 read_version.to_string().c_str(), GetVersion().to_string().c_str());
356 break;
357 case TosaVersion::compat_t::NOT_COMPATIBLE:
358 printf("ERROR: Read flatbuffer version %s is not compatible with serializer version %s\n",
359 read_version.to_string().c_str(), GetVersion().to_string().c_str());
360 return TOSA_VERSION_MISMATCH;
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700361 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700362
363 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
364 {
365 auto curr_block = fb_tosa_blocks->Get(i);
366
367 auto block_name = curr_block->name()->str();
368
369 auto fb_tosa_operators = curr_block->operators();
370 block_operators_container.clear();
371 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
372 {
373 auto curr_operator = fb_tosa_operators->Get(j);
374
375 auto operator_op = curr_operator->op();
376 auto attribute_type = curr_operator->attribute_type();
377 auto attribute = curr_operator->attribute();
378 auto operator_qinfo_type = curr_operator->quant_info_type();
379 auto operator_qinfo = curr_operator->quant_info();
380
381 // input tensors
382 auto operator_inputs = curr_operator->inputs();
383 operator_inputs_container.clear();
384 if (operator_inputs)
385 {
386 for (size_t k = 0; k < operator_inputs->size(); k++)
387 {
388 auto curr_input = operator_inputs->Get(k);
389 operator_inputs_container.push_back(curr_input->str());
390 }
391 }
392
393 // output tensors
394 auto operator_outputs = curr_operator->outputs();
395 operator_outputs_container.clear();
396 if (operator_outputs)
397 {
398 for (size_t k = 0; k < operator_outputs->size(); k++)
399 {
400 auto curr_output = operator_outputs->Get(k);
401 operator_outputs_container.push_back(curr_output->str());
402 }
403 }
404
405 switch (attribute_type)
406 {
407 case Attribute_NONE:
408 typed_attribute = new TosaNoneAttribute();
409 break;
410#define DEF_ATTRIBUTE(NAME, ...) \
411 case Attribute_##NAME##Attribute: \
412 typed_attribute = new Tosa##NAME##Attribute(attribute); \
413 break;
414#include "attribute.def"
415#undef DEF_ATTRIBUTE
416 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700417 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700418 EnumNamesAttribute()[attribute_type]);
419 return TOSA_INTERNAL_ERROR;
420 }
421
422 switch (operator_qinfo_type)
423 {
424 case QuantInfo_NONE:
425 typed_qinfo = new TosaNoneQuantInfo();
426 break;
427#define DEF_QUANTIZATION_INFO(NAME, ...) \
428 case QuantInfo_##NAME##QuantInfo: \
429 typed_qinfo = new Tosa##NAME##QuantInfo(operator_qinfo); \
430 break;
431
432#include "quant_info.def"
433#undef DEF_QUANTIZATION_INFO
434 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700435 printf("TosaSerializationHandler::Deserialize(): QuantInfo %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700436 EnumNamesQuantInfo()[operator_qinfo_type]);
437 return TOSA_INTERNAL_ERROR;
438 }
439
440 new_operator =
441 new TosaSerializationOperator(operator_op, attribute_type, typed_attribute, operator_qinfo_type,
442 typed_qinfo, operator_inputs_container, operator_outputs_container);
443 if (new_operator)
444 {
445 block_operators_container.push_back(new_operator);
446 }
447 else
448 {
449 return TOSA_MEMORY_ERROR;
450 }
451
452 if (typed_attribute)
453 delete typed_attribute;
454 if (typed_qinfo)
455 delete typed_qinfo;
456 }
457
458 auto fb_tosa_tensors = curr_block->tensors();
459 block_tensors_container.clear();
460 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
461 {
462 auto curr_tensor = fb_tosa_tensors->Get(j);
463
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700464 auto tensor_name = curr_tensor->name();
465 auto tensor_shape = curr_tensor->shape();
466 auto tensor_type = curr_tensor->type();
467 auto tensor_data = curr_tensor->data();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700468
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700469 new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700470 if (new_tensor)
471 {
472 block_tensors_container.push_back(new_tensor);
473 }
474 else
475 {
476 return TOSA_MEMORY_ERROR;
477 }
478 }
479
480 auto block_inputs = curr_block->inputs();
481 auto block_outputs = curr_block->outputs();
482
483 block_inputs_container.clear();
484 block_outputs_container.clear();
485
486 for (size_t j = 0; j < block_inputs->size(); j++)
487 {
488 auto curr_block_input = block_inputs->Get(j);
489 block_inputs_container.push_back(curr_block_input->str());
490 }
491 for (size_t j = 0; j < block_outputs->size(); j++)
492 {
493 auto curr_block_output = block_outputs->Get(j);
494 block_outputs_container.push_back(curr_block_output->str());
495 }
496
497 new_block = new TosaSerializationBasicBlock(block_name, block_operators_container, block_tensors_container,
498 block_inputs_container, block_outputs_container);
499 if (new_block)
500 {
501 this->GetBlocks().push_back(new_block);
502 }
503 else
504 {
505 return TOSA_MEMORY_ERROR;
506 }
507 }
508
509 return TOSA_OK;
510}
511
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700512tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700513{
514 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
515
516 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
517 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
518 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
519 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
520
521 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
522 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
523
524 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
525 for (auto block : GetBlocks())
526 {
527 fboffset_block_operators.clear();
528 fboffset_block_tensors.clear();
529 fboffset_block_inputs.clear();
530 fboffset_block_outputs.clear();
531
532 auto block_name = _builder.CreateString(block->GetName().c_str());
533
534 for (auto tensor_str : block->GetInputs())
535 {
536 auto tensor_name = _builder.CreateString(tensor_str.c_str());
537 fboffset_block_inputs.push_back(tensor_name);
538 }
539
540 for (auto tensor_str : block->GetOutputs())
541 {
542 auto tensor_name = _builder.CreateString(tensor_str.c_str());
543 fboffset_block_outputs.push_back(tensor_name);
544 }
545
546 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
547 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
548
549 for (auto op : block->GetOperators())
550 {
551 fboffset_operator_inputs.clear();
552 fboffset_operator_outputs.clear();
553
554 auto operator_op = op->GetOp();
555 auto attribute_type = op->GetAttributeType();
556
557 for (auto tensor_str : op->GetInputTensorNames())
558 {
559 auto tensor_name = _builder.CreateString(tensor_str.c_str());
560 fboffset_operator_inputs.push_back(tensor_name);
561 }
562
563 for (auto tensor_str : op->GetOutputTensorNames())
564 {
565 auto tensor_name = _builder.CreateString(tensor_str.c_str());
566 fboffset_operator_outputs.push_back(tensor_name);
567 }
568
569 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
570 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
571
572 flatbuffers::Offset<void> fb_attribute;
573 switch (attribute_type)
574 {
575 case Attribute_NONE:
576 fb_attribute = 0;
577 break;
578
579#define DEF_ARGS_S_STR(NAME, V) , _builder.CreateString(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V().c_str())
580#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
581
582#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
583#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
584#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
585#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
586#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
587
588#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
589#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
590
591#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
592#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
593#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
594 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
595#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
596 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
597#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
598 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
599 DEF_ARGS_##F4(NAME, T4, V4)
600#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
601 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
602 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5)
603#define DEF_ARGS_7(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6) \
604 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
605 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
606#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
607 case Attribute_##NAME##Attribute: \
608 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
609 break;
610
611#include "attribute.def"
612#undef DEF_ATTRIBUTE
613#undef DEF_ARGS_1
614#undef DEF_ARGS_2
615#undef DEF_ARGS_3
616#undef DEF_ARGS_4
617#undef DEF_ARGS_5
618#undef DEF_ARGS_6
619#undef DEF_ARGS_7
620#undef DEF_ARGS_S
621#undef DEF_ARGS_V
622#undef DEF_ARGS_S_int32_t
623#undef DEF_ARGS_S_float
624#undef DEF_ARGS_S_bool
625#undef DEF_ARGS_S_ResizeMode
626#undef DEF_ARGS_S_string
627#undef DEF_ARGS_S_STR
628#undef DEF_ARGS_S_DEFAULT
629 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700630 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700631 EnumNamesAttribute()[attribute_type]);
632 return TOSA_INTERNAL_ERROR;
633 }
634
635 auto qinfo_type = op->GetQInfoType();
636 flatbuffers::Offset<void> fb_operator_qinfo;
637 switch (qinfo_type)
638 {
639 case QuantInfo_NONE:
640 fb_operator_qinfo = 0;
641 break;
642#define DEF_ARGS_S(NAME, T, V) , reinterpret_cast<Tosa##NAME*>(op->GetQInfo())->V()
643#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetQInfo())->V())
644
645#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
646#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
647#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
648 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
649#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
650 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
651#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
652 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
653 DEF_ARGS_##F4(NAME, T4, V4)
654#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
655 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
656 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5)
657#define DEF_ARGS_7(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6) \
658 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
659 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
660#define DEF_ARGS_8(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6, T7, F7, \
661 V7) \
662 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
663 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
664 DEF_ARGS_##F7(NAME, T7, V7)
665#define DEF_ARGS_9(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6, T7, F7, \
666 V7, T8, F8, V8) \
667 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
668 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
669 DEF_ARGS_##F7(NAME, T7, V7) DEF_ARGS_##F8(NAME, T8, V8)
670#define DEF_ARGS_10(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6, T7, F7, \
671 V7, T8, F8, V8, T9, F9, V9) \
672 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
673 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
674 DEF_ARGS_##F7(NAME, T7, V7) DEF_ARGS_##F8(NAME, T8, V8) DEF_ARGS_##F9(NAME, T9, V9)
675#define DEF_QUANTIZATION_INFO(NAME, NUM_ARGS, ...) \
676 case QuantInfo_##NAME##QuantInfo: \
677 fb_operator_qinfo = \
678 Create##NAME##QuantInfo(_builder DEF_ARGS_##NUM_ARGS(NAME##QuantInfo, __VA_ARGS__)).Union(); \
679 break;
680
681#include "quant_info.def"
682#undef DEF_QUANTIZATION_INFO
683#undef DEF_ARGS_1
684#undef DEF_ARGS_2
685#undef DEF_ARGS_3
686#undef DEF_ARGS_4
687#undef DEF_ARGS_5
688#undef DEF_ARGS_6
689#undef DEF_ARGS_7
690#undef DEF_ARGS_8
691#undef DEF_ARGS_9
692#undef DEF_ARGS_10
693#undef DEF_ARGS_S
694#undef DEF_ARGS_V
695 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700696 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700697 EnumNamesAttribute()[attribute_type]);
698 return TOSA_INTERNAL_ERROR;
699 }
700
701 auto fboffset_operator =
702 CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute, fb_operator_inputs,
703 fb_operator_outputs, qinfo_type, fb_operator_qinfo);
704 fboffset_block_operators.push_back(fboffset_operator);
705 }
706
707 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
708
709 for (auto tensor : block->GetTensors())
710 {
711
712 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
713 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
714 auto tensor_dtype = tensor->GetDtype();
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700715 auto tensor_data = _builder.CreateVector(tensor->GetData());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700716
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700717 auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700718 fboffset_block_tensors.push_back(fboffset_tensor);
719 }
720
721 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
722
723 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
724 fb_block_inputs, fb_block_outputs);
725 fboffset_blocks.push_back(fboffset_block);
726 }
727
728 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
729
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700730 auto fb_version =
731 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700732
733 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_blocks);
734 _builder.Finish(fb_graph);
735
736 return TOSA_OK;
737}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700738
739void zero_pad(std::vector<uint8_t>& buf)
740{
741 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
742 {
743 buf.push_back(0);
744 }
745}
746
747tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
748{
749 out.clear();
750 for (auto val : in)
751 {
752 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
753 out.push_back(*val_u32 & 0xFF);
754 out.push_back((*val_u32 >> 8) & 0xFF);
755 out.push_back((*val_u32 >> 16) & 0xFF);
756 out.push_back((*val_u32 >> 24) & 0xFF);
757 }
758 zero_pad(out);
759 return TOSA_OK;
760}
761
762tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
763{
764 out.clear();
765 for (auto val : in)
766 {
767 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
768 out.push_back(*val_u64 & 0xFF);
769 out.push_back((*val_u64 >> 8) & 0xFF);
770 out.push_back((*val_u64 >> 16) & 0xFF);
771 out.push_back((*val_u64 >> 24) & 0xFF);
772 out.push_back((*val_u64 >> 32) & 0xFF);
773 out.push_back((*val_u64 >> 40) & 0xFF);
774 }
775 zero_pad(out);
776 return TOSA_OK;
777}
778
779tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
780{
781 out.clear();
782 for (auto val : in)
783 {
784 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
785 out.push_back(*val_u32 & 0xFF);
786 out.push_back((*val_u32 >> 8) & 0xFF);
787 out.push_back((*val_u32 >> 16) & 0xFF);
788 out.push_back((*val_u32 >> 24) & 0xFF);
789 }
790 zero_pad(out);
791 return TOSA_OK;
792}
793
794tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
795{
796 out.clear();
797 for (auto val : in)
798 {
799 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
800 out.push_back(*val_u16 & 0xFF);
801 out.push_back((*val_u16 >> 8) & 0xFF);
802 }
803 zero_pad(out);
804 return TOSA_OK;
805}
806
807tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
808{
809 out.clear();
810 for (auto val : in)
811 {
812 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
813 out.push_back(*val_u8);
814 }
815 zero_pad(out);
816 return TOSA_OK;
817}
818
Kevin Cheng3ce56342021-07-28 13:42:29 -0700819// Two int4 values are packed into one byte out.
820// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
821// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
822tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
823{
824 out.clear();
825 uint32_t in_size = in.size();
826 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
827 for (int i = 0; i < out_size; i++)
828 {
829 int8_t val_0 = in[2 * i];
830 int8_t val_1 = 0;
831 if (2 * i + 1 < in_size)
832 {
833 val_1 = in[2 * i + 1];
834 }
835 // In TOSA spec, int4 ranges [-7, 7]
836 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
837 {
838 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
839 val_0, val_1);
840 return TOSA_USER_ERROR;
841 }
842 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
843 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
844 out.push_back(val_u8);
845 }
846 zero_pad(out);
847 return TOSA_OK;
848}
849
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700850tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
851{
852 out.clear();
853 for (auto val : in)
854 {
855 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
856 out.push_back(*val_u8);
857 }
858 zero_pad(out);
859 return TOSA_OK;
860}
861
862tosa_err_t
863 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
864{
865 out.clear();
866 if (in.size() < out_size * sizeof(float))
867 {
868 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
869 out_size * sizeof(float));
870 return TOSA_USER_ERROR;
871 }
872 for (int i = 0; i < out_size; i++)
873 {
874 uint32_t byte0 = in[i * sizeof(float)];
875 uint32_t byte1 = in[i * sizeof(float) + 1];
876 uint32_t byte2 = in[i * sizeof(float) + 2];
877 uint32_t byte3 = in[i * sizeof(float) + 3];
878 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
879 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
880 out.push_back(*val_fp32);
881 }
882 return TOSA_OK;
883}
884
885tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
886 uint32_t out_size,
887 std::vector<int64_t>& out)
888{
889 out.clear();
890 if (in.size() < out_size * 6 /* sizeof(int48) */)
891 {
892 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
893 out_size * 6);
894 return TOSA_USER_ERROR;
895 }
896 for (int i = 0; i < out_size; i++)
897 {
898 uint64_t byte0 = in[i * 6];
899 uint64_t byte1 = in[i * 6 + 1];
900 uint64_t byte2 = in[i * 6 + 2];
901 uint64_t byte3 = in[i * 6 + 3];
902 uint64_t byte4 = in[i * 6 + 4];
903 uint64_t byte5 = in[i * 6 + 5];
904 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
905 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
906 if (sign)
907 {
908 uint64_t sext_mask = (0xFFFFUL << 48);
909 val_u64 |= sext_mask;
910 }
911 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
912 out.push_back(*val_i64);
913 }
914 return TOSA_OK;
915}
916
917tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
918 uint32_t out_size,
919 std::vector<int32_t>& out)
920{
921 out.clear();
922 if (in.size() < out_size * sizeof(int32_t))
923 {
924 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
925 out_size * sizeof(int32_t));
926 return TOSA_USER_ERROR;
927 }
928 for (int i = 0; i < out_size; i++)
929 {
930 uint32_t byte0 = in[i * sizeof(int32_t)];
931 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
932 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
933 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
934 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
935 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
936 out.push_back(*val_i32);
937 }
938 return TOSA_OK;
939}
940
941tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
942 uint32_t out_size,
943 std::vector<int16_t>& out)
944{
945 out.clear();
946 if (in.size() < out_size * sizeof(int16_t))
947 {
948 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
949 out_size * sizeof(int16_t));
950 return TOSA_USER_ERROR;
951 }
952 for (int i = 0; i < out_size; i++)
953 {
954 uint16_t byte0 = in[i * sizeof(int16_t)];
955 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
956 uint16_t val_u16 = byte0 + (byte1 << 8);
957 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
958 out.push_back(*val_i16);
959 }
960 return TOSA_OK;
961}
962
963tosa_err_t
964 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
965{
966 out.clear();
967 if (in.size() < out_size * sizeof(int8_t))
968 {
969 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -0700970 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700971 return TOSA_USER_ERROR;
972 }
973 for (int i = 0; i < out_size; i++)
974 {
975 uint8_t val_u8 = in[i];
976 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
977 out.push_back(*val_i8);
978 }
979 return TOSA_OK;
980}
981
982tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -0700983 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
984{
985 out.clear();
986 if (out_size > in.size() * 2)
987 {
988 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
989 out_size, in.size());
990 return TOSA_USER_ERROR;
991 }
992 for (int i = 0; i < in.size(); i++)
993 {
994 uint8_t val_u8 = in[i];
995 uint8_t val_0_u4 = val_u8 & 0xF;
996 uint8_t val_1_u4 = val_u8 >> 4;
997 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
998 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
999 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1000 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1001 // In TOSA spec, int4 ranges [-7, 7]
1002 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1003 {
1004 printf(
1005 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1006 val_0, val_1);
1007 return TOSA_USER_ERROR;
1008 }
1009 out.push_back(val_0);
1010 if (2 * i + 1 < out_size)
1011 out.push_back(val_1);
1012 }
1013 return TOSA_OK;
1014}
1015
1016tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001017 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1018{
1019 out.clear();
1020 if (in.size() < out_size * sizeof(bool))
1021 {
1022 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1023 in.size(), out_size * sizeof(bool));
1024 return TOSA_USER_ERROR;
1025 }
1026 for (int i = 0; i < out_size; i++)
1027 {
1028 uint8_t val_u8 = in[i];
1029 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1030 out.push_back(*val_bool);
1031 }
1032 return TOSA_OK;
1033}