blob: fced242e1e50633e742575e820e4a373e91a53b4 [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 Chengb97cb1d2021-10-14 11:53:39 -0700151 _version = VersionToStr(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 -0700322std::string TosaSerializationHandler::VersionToStr(int32_t major, int32_t minor, int32_t patch, bool draft)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700323{
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700324 std::string str;
325 str += std::to_string(major) + ".";
326 str += std::to_string(minor) + ".";
327 str += std::to_string(patch);
328 if (draft)
329 str += "d";
330 return str;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700331}
332
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700333tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700334{
335 auto fb_tosa_graph = GetTosaGraph(buf);
336 auto fb_tosa_version = fb_tosa_graph->version();
337 auto fb_tosa_blocks = fb_tosa_graph->blocks();
338
339 std::vector<std::string> operator_inputs_container;
340 std::vector<std::string> operator_outputs_container;
341
342 std::vector<TosaSerializationOperator*> block_operators_container;
343 std::vector<TosaSerializationTensor*> block_tensors_container;
344 std::vector<std::string> block_inputs_container;
345 std::vector<std::string> block_outputs_container;
346
347 TosaAttributeBase* typed_attribute = NULL;
348 TosaQuantInfoBase* typed_qinfo = NULL;
349 TosaSerializationOperator* new_operator = NULL;
350 TosaSerializationBasicBlock* new_block = NULL;
351 TosaSerializationTensor* new_tensor = NULL;
352
353 // erase container
354 Clear();
355
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700356 std::string read_version = VersionToStr(fb_tosa_version->_major(), fb_tosa_version->_minor(),
357 fb_tosa_version->_patch(), fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700358
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700359 if (read_version != GetVersionStr())
360 {
361 printf("Read flatbuffer version %s doesn't match serializer version %s\n", read_version.c_str(),
362 GetVersionStr().c_str());
363 return TOSA_VERSION_MISMATCH;
364 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700365
366 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
367 {
368 auto curr_block = fb_tosa_blocks->Get(i);
369
370 auto block_name = curr_block->name()->str();
371
372 auto fb_tosa_operators = curr_block->operators();
373 block_operators_container.clear();
374 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
375 {
376 auto curr_operator = fb_tosa_operators->Get(j);
377
378 auto operator_op = curr_operator->op();
379 auto attribute_type = curr_operator->attribute_type();
380 auto attribute = curr_operator->attribute();
381 auto operator_qinfo_type = curr_operator->quant_info_type();
382 auto operator_qinfo = curr_operator->quant_info();
383
384 // input tensors
385 auto operator_inputs = curr_operator->inputs();
386 operator_inputs_container.clear();
387 if (operator_inputs)
388 {
389 for (size_t k = 0; k < operator_inputs->size(); k++)
390 {
391 auto curr_input = operator_inputs->Get(k);
392 operator_inputs_container.push_back(curr_input->str());
393 }
394 }
395
396 // output tensors
397 auto operator_outputs = curr_operator->outputs();
398 operator_outputs_container.clear();
399 if (operator_outputs)
400 {
401 for (size_t k = 0; k < operator_outputs->size(); k++)
402 {
403 auto curr_output = operator_outputs->Get(k);
404 operator_outputs_container.push_back(curr_output->str());
405 }
406 }
407
408 switch (attribute_type)
409 {
410 case Attribute_NONE:
411 typed_attribute = new TosaNoneAttribute();
412 break;
413#define DEF_ATTRIBUTE(NAME, ...) \
414 case Attribute_##NAME##Attribute: \
415 typed_attribute = new Tosa##NAME##Attribute(attribute); \
416 break;
417#include "attribute.def"
418#undef DEF_ATTRIBUTE
419 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700420 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700421 EnumNamesAttribute()[attribute_type]);
422 return TOSA_INTERNAL_ERROR;
423 }
424
425 switch (operator_qinfo_type)
426 {
427 case QuantInfo_NONE:
428 typed_qinfo = new TosaNoneQuantInfo();
429 break;
430#define DEF_QUANTIZATION_INFO(NAME, ...) \
431 case QuantInfo_##NAME##QuantInfo: \
432 typed_qinfo = new Tosa##NAME##QuantInfo(operator_qinfo); \
433 break;
434
435#include "quant_info.def"
436#undef DEF_QUANTIZATION_INFO
437 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700438 printf("TosaSerializationHandler::Deserialize(): QuantInfo %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700439 EnumNamesQuantInfo()[operator_qinfo_type]);
440 return TOSA_INTERNAL_ERROR;
441 }
442
443 new_operator =
444 new TosaSerializationOperator(operator_op, attribute_type, typed_attribute, operator_qinfo_type,
445 typed_qinfo, operator_inputs_container, operator_outputs_container);
446 if (new_operator)
447 {
448 block_operators_container.push_back(new_operator);
449 }
450 else
451 {
452 return TOSA_MEMORY_ERROR;
453 }
454
455 if (typed_attribute)
456 delete typed_attribute;
457 if (typed_qinfo)
458 delete typed_qinfo;
459 }
460
461 auto fb_tosa_tensors = curr_block->tensors();
462 block_tensors_container.clear();
463 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
464 {
465 auto curr_tensor = fb_tosa_tensors->Get(j);
466
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700467 auto tensor_name = curr_tensor->name();
468 auto tensor_shape = curr_tensor->shape();
469 auto tensor_type = curr_tensor->type();
470 auto tensor_data = curr_tensor->data();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700471
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700472 new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700473 if (new_tensor)
474 {
475 block_tensors_container.push_back(new_tensor);
476 }
477 else
478 {
479 return TOSA_MEMORY_ERROR;
480 }
481 }
482
483 auto block_inputs = curr_block->inputs();
484 auto block_outputs = curr_block->outputs();
485
486 block_inputs_container.clear();
487 block_outputs_container.clear();
488
489 for (size_t j = 0; j < block_inputs->size(); j++)
490 {
491 auto curr_block_input = block_inputs->Get(j);
492 block_inputs_container.push_back(curr_block_input->str());
493 }
494 for (size_t j = 0; j < block_outputs->size(); j++)
495 {
496 auto curr_block_output = block_outputs->Get(j);
497 block_outputs_container.push_back(curr_block_output->str());
498 }
499
500 new_block = new TosaSerializationBasicBlock(block_name, block_operators_container, block_tensors_container,
501 block_inputs_container, block_outputs_container);
502 if (new_block)
503 {
504 this->GetBlocks().push_back(new_block);
505 }
506 else
507 {
508 return TOSA_MEMORY_ERROR;
509 }
510 }
511
512 return TOSA_OK;
513}
514
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700515tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700516{
517 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
518
519 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
520 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
521 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
522 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
523
524 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
525 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
526
527 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
528 for (auto block : GetBlocks())
529 {
530 fboffset_block_operators.clear();
531 fboffset_block_tensors.clear();
532 fboffset_block_inputs.clear();
533 fboffset_block_outputs.clear();
534
535 auto block_name = _builder.CreateString(block->GetName().c_str());
536
537 for (auto tensor_str : block->GetInputs())
538 {
539 auto tensor_name = _builder.CreateString(tensor_str.c_str());
540 fboffset_block_inputs.push_back(tensor_name);
541 }
542
543 for (auto tensor_str : block->GetOutputs())
544 {
545 auto tensor_name = _builder.CreateString(tensor_str.c_str());
546 fboffset_block_outputs.push_back(tensor_name);
547 }
548
549 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
550 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
551
552 for (auto op : block->GetOperators())
553 {
554 fboffset_operator_inputs.clear();
555 fboffset_operator_outputs.clear();
556
557 auto operator_op = op->GetOp();
558 auto attribute_type = op->GetAttributeType();
559
560 for (auto tensor_str : op->GetInputTensorNames())
561 {
562 auto tensor_name = _builder.CreateString(tensor_str.c_str());
563 fboffset_operator_inputs.push_back(tensor_name);
564 }
565
566 for (auto tensor_str : op->GetOutputTensorNames())
567 {
568 auto tensor_name = _builder.CreateString(tensor_str.c_str());
569 fboffset_operator_outputs.push_back(tensor_name);
570 }
571
572 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
573 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
574
575 flatbuffers::Offset<void> fb_attribute;
576 switch (attribute_type)
577 {
578 case Attribute_NONE:
579 fb_attribute = 0;
580 break;
581
582#define DEF_ARGS_S_STR(NAME, V) , _builder.CreateString(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V().c_str())
583#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
584
585#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
586#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
587#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
588#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
589#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
590
591#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
592#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
593
594#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
595#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
596#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
597 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
598#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
599 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
600#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
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)
603#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
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)
606#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) \
607 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
608 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
609#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
610 case Attribute_##NAME##Attribute: \
611 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
612 break;
613
614#include "attribute.def"
615#undef DEF_ATTRIBUTE
616#undef DEF_ARGS_1
617#undef DEF_ARGS_2
618#undef DEF_ARGS_3
619#undef DEF_ARGS_4
620#undef DEF_ARGS_5
621#undef DEF_ARGS_6
622#undef DEF_ARGS_7
623#undef DEF_ARGS_S
624#undef DEF_ARGS_V
625#undef DEF_ARGS_S_int32_t
626#undef DEF_ARGS_S_float
627#undef DEF_ARGS_S_bool
628#undef DEF_ARGS_S_ResizeMode
629#undef DEF_ARGS_S_string
630#undef DEF_ARGS_S_STR
631#undef DEF_ARGS_S_DEFAULT
632 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700633 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700634 EnumNamesAttribute()[attribute_type]);
635 return TOSA_INTERNAL_ERROR;
636 }
637
638 auto qinfo_type = op->GetQInfoType();
639 flatbuffers::Offset<void> fb_operator_qinfo;
640 switch (qinfo_type)
641 {
642 case QuantInfo_NONE:
643 fb_operator_qinfo = 0;
644 break;
645#define DEF_ARGS_S(NAME, T, V) , reinterpret_cast<Tosa##NAME*>(op->GetQInfo())->V()
646#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetQInfo())->V())
647
648#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
649#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
650#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
651 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
652#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
653 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
654#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
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)
657#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
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)
660#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) \
661 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
662 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
663#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, \
664 V7) \
665 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
666 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
667 DEF_ARGS_##F7(NAME, T7, V7)
668#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, \
669 V7, T8, F8, V8) \
670 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
671 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
672 DEF_ARGS_##F7(NAME, T7, V7) DEF_ARGS_##F8(NAME, T8, V8)
673#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, \
674 V7, T8, F8, V8, T9, F9, V9) \
675 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
676 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
677 DEF_ARGS_##F7(NAME, T7, V7) DEF_ARGS_##F8(NAME, T8, V8) DEF_ARGS_##F9(NAME, T9, V9)
678#define DEF_QUANTIZATION_INFO(NAME, NUM_ARGS, ...) \
679 case QuantInfo_##NAME##QuantInfo: \
680 fb_operator_qinfo = \
681 Create##NAME##QuantInfo(_builder DEF_ARGS_##NUM_ARGS(NAME##QuantInfo, __VA_ARGS__)).Union(); \
682 break;
683
684#include "quant_info.def"
685#undef DEF_QUANTIZATION_INFO
686#undef DEF_ARGS_1
687#undef DEF_ARGS_2
688#undef DEF_ARGS_3
689#undef DEF_ARGS_4
690#undef DEF_ARGS_5
691#undef DEF_ARGS_6
692#undef DEF_ARGS_7
693#undef DEF_ARGS_8
694#undef DEF_ARGS_9
695#undef DEF_ARGS_10
696#undef DEF_ARGS_S
697#undef DEF_ARGS_V
698 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700699 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700700 EnumNamesAttribute()[attribute_type]);
701 return TOSA_INTERNAL_ERROR;
702 }
703
704 auto fboffset_operator =
705 CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute, fb_operator_inputs,
706 fb_operator_outputs, qinfo_type, fb_operator_qinfo);
707 fboffset_block_operators.push_back(fboffset_operator);
708 }
709
710 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
711
712 for (auto tensor : block->GetTensors())
713 {
714
715 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
716 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
717 auto tensor_dtype = tensor->GetDtype();
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700718 auto tensor_data = _builder.CreateVector(tensor->GetData());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700719
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700720 auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700721 fboffset_block_tensors.push_back(fboffset_tensor);
722 }
723
724 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
725
726 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
727 fb_block_inputs, fb_block_outputs);
728 fboffset_blocks.push_back(fboffset_block);
729 }
730
731 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
732
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700733 auto fb_version =
734 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700735
736 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_blocks);
737 _builder.Finish(fb_graph);
738
739 return TOSA_OK;
740}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700741
742void zero_pad(std::vector<uint8_t>& buf)
743{
744 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
745 {
746 buf.push_back(0);
747 }
748}
749
750tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
751{
752 out.clear();
753 for (auto val : in)
754 {
755 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
756 out.push_back(*val_u32 & 0xFF);
757 out.push_back((*val_u32 >> 8) & 0xFF);
758 out.push_back((*val_u32 >> 16) & 0xFF);
759 out.push_back((*val_u32 >> 24) & 0xFF);
760 }
761 zero_pad(out);
762 return TOSA_OK;
763}
764
765tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
766{
767 out.clear();
768 for (auto val : in)
769 {
770 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
771 out.push_back(*val_u64 & 0xFF);
772 out.push_back((*val_u64 >> 8) & 0xFF);
773 out.push_back((*val_u64 >> 16) & 0xFF);
774 out.push_back((*val_u64 >> 24) & 0xFF);
775 out.push_back((*val_u64 >> 32) & 0xFF);
776 out.push_back((*val_u64 >> 40) & 0xFF);
777 }
778 zero_pad(out);
779 return TOSA_OK;
780}
781
782tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
783{
784 out.clear();
785 for (auto val : in)
786 {
787 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
788 out.push_back(*val_u32 & 0xFF);
789 out.push_back((*val_u32 >> 8) & 0xFF);
790 out.push_back((*val_u32 >> 16) & 0xFF);
791 out.push_back((*val_u32 >> 24) & 0xFF);
792 }
793 zero_pad(out);
794 return TOSA_OK;
795}
796
797tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
798{
799 out.clear();
800 for (auto val : in)
801 {
802 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
803 out.push_back(*val_u16 & 0xFF);
804 out.push_back((*val_u16 >> 8) & 0xFF);
805 }
806 zero_pad(out);
807 return TOSA_OK;
808}
809
810tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
811{
812 out.clear();
813 for (auto val : in)
814 {
815 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
816 out.push_back(*val_u8);
817 }
818 zero_pad(out);
819 return TOSA_OK;
820}
821
Kevin Cheng3ce56342021-07-28 13:42:29 -0700822// Two int4 values are packed into one byte out.
823// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
824// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
825tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
826{
827 out.clear();
828 uint32_t in_size = in.size();
829 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
830 for (int i = 0; i < out_size; i++)
831 {
832 int8_t val_0 = in[2 * i];
833 int8_t val_1 = 0;
834 if (2 * i + 1 < in_size)
835 {
836 val_1 = in[2 * i + 1];
837 }
838 // In TOSA spec, int4 ranges [-7, 7]
839 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
840 {
841 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
842 val_0, val_1);
843 return TOSA_USER_ERROR;
844 }
845 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
846 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
847 out.push_back(val_u8);
848 }
849 zero_pad(out);
850 return TOSA_OK;
851}
852
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700853tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
854{
855 out.clear();
856 for (auto val : in)
857 {
858 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
859 out.push_back(*val_u8);
860 }
861 zero_pad(out);
862 return TOSA_OK;
863}
864
865tosa_err_t
866 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
867{
868 out.clear();
869 if (in.size() < out_size * sizeof(float))
870 {
871 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
872 out_size * sizeof(float));
873 return TOSA_USER_ERROR;
874 }
875 for (int i = 0; i < out_size; i++)
876 {
877 uint32_t byte0 = in[i * sizeof(float)];
878 uint32_t byte1 = in[i * sizeof(float) + 1];
879 uint32_t byte2 = in[i * sizeof(float) + 2];
880 uint32_t byte3 = in[i * sizeof(float) + 3];
881 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
882 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
883 out.push_back(*val_fp32);
884 }
885 return TOSA_OK;
886}
887
888tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
889 uint32_t out_size,
890 std::vector<int64_t>& out)
891{
892 out.clear();
893 if (in.size() < out_size * 6 /* sizeof(int48) */)
894 {
895 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
896 out_size * 6);
897 return TOSA_USER_ERROR;
898 }
899 for (int i = 0; i < out_size; i++)
900 {
901 uint64_t byte0 = in[i * 6];
902 uint64_t byte1 = in[i * 6 + 1];
903 uint64_t byte2 = in[i * 6 + 2];
904 uint64_t byte3 = in[i * 6 + 3];
905 uint64_t byte4 = in[i * 6 + 4];
906 uint64_t byte5 = in[i * 6 + 5];
907 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
908 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
909 if (sign)
910 {
911 uint64_t sext_mask = (0xFFFFUL << 48);
912 val_u64 |= sext_mask;
913 }
914 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
915 out.push_back(*val_i64);
916 }
917 return TOSA_OK;
918}
919
920tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
921 uint32_t out_size,
922 std::vector<int32_t>& out)
923{
924 out.clear();
925 if (in.size() < out_size * sizeof(int32_t))
926 {
927 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
928 out_size * sizeof(int32_t));
929 return TOSA_USER_ERROR;
930 }
931 for (int i = 0; i < out_size; i++)
932 {
933 uint32_t byte0 = in[i * sizeof(int32_t)];
934 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
935 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
936 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
937 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
938 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
939 out.push_back(*val_i32);
940 }
941 return TOSA_OK;
942}
943
944tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
945 uint32_t out_size,
946 std::vector<int16_t>& out)
947{
948 out.clear();
949 if (in.size() < out_size * sizeof(int16_t))
950 {
951 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
952 out_size * sizeof(int16_t));
953 return TOSA_USER_ERROR;
954 }
955 for (int i = 0; i < out_size; i++)
956 {
957 uint16_t byte0 = in[i * sizeof(int16_t)];
958 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
959 uint16_t val_u16 = byte0 + (byte1 << 8);
960 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
961 out.push_back(*val_i16);
962 }
963 return TOSA_OK;
964}
965
966tosa_err_t
967 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
968{
969 out.clear();
970 if (in.size() < out_size * sizeof(int8_t))
971 {
972 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -0700973 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700974 return TOSA_USER_ERROR;
975 }
976 for (int i = 0; i < out_size; i++)
977 {
978 uint8_t val_u8 = in[i];
979 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
980 out.push_back(*val_i8);
981 }
982 return TOSA_OK;
983}
984
985tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -0700986 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
987{
988 out.clear();
989 if (out_size > in.size() * 2)
990 {
991 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
992 out_size, in.size());
993 return TOSA_USER_ERROR;
994 }
995 for (int i = 0; i < in.size(); i++)
996 {
997 uint8_t val_u8 = in[i];
998 uint8_t val_0_u4 = val_u8 & 0xF;
999 uint8_t val_1_u4 = val_u8 >> 4;
1000 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1001 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1002 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1003 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1004 // In TOSA spec, int4 ranges [-7, 7]
1005 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1006 {
1007 printf(
1008 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1009 val_0, val_1);
1010 return TOSA_USER_ERROR;
1011 }
1012 out.push_back(val_0);
1013 if (2 * i + 1 < out_size)
1014 out.push_back(val_1);
1015 }
1016 return TOSA_OK;
1017}
1018
1019tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001020 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1021{
1022 out.clear();
1023 if (in.size() < out_size * sizeof(bool))
1024 {
1025 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1026 in.size(), out_size * sizeof(bool));
1027 return TOSA_USER_ERROR;
1028 }
1029 for (int i = 0; i < out_size; i++)
1030 {
1031 uint8_t val_u8 = in[i];
1032 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1033 out.push_back(*val_bool);
1034 }
1035 return TOSA_OK;
1036}