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