blob: 170b31344e6e9a382c9f6e487d08ef5c750708bc [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"
James Ward485a11d2022-08-05 13:48:37 +010017#include "half.hpp"
Eric Kunze2364dcd2021-04-26 11:06:57 -070018
19#include <iostream>
20using namespace tosa;
21
22TosaSerializationTensor::TosaSerializationTensor(const flatbuffers::String* name,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070023 const flatbuffers::Vector<int32_t>* shape,
Eric Kunze2364dcd2021-04-26 11:06:57 -070024 DType dtype,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070025 const flatbuffers::Vector<uint8_t>* data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070026{
27 _dtype = dtype;
28
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070029 std::copy(shape->begin(), shape->end(), std::back_inserter(_shape));
Eric Kunze2364dcd2021-04-26 11:06:57 -070030
31 assert(name);
32 _name = name->str();
33
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070034 if (data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070035 {
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070036 std::copy(data->begin(), data->end(), std::back_inserter(_data));
Eric Kunze2364dcd2021-04-26 11:06:57 -070037 }
38}
39
Kevin Cheng545a5082021-11-11 01:36:33 +000040TosaSerializationTensor::TosaSerializationTensor(const std::string& name,
Eric Kunze2364dcd2021-04-26 11:06:57 -070041 const std::vector<int32_t>& shape,
42 DType dtype,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070043 const std::vector<uint8_t>& data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070044{
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070045 _dtype = dtype;
46 _shape = shape;
47 _name = name;
48 _data = data;
Eric Kunze2364dcd2021-04-26 11:06:57 -070049}
50
51TosaSerializationTensor::TosaSerializationTensor()
52{
53 _dtype = DType_UNKNOWN;
Kevin Cheng545a5082021-11-11 01:36:33 +000054 _name = "UNKNOWN";
Eric Kunze2364dcd2021-04-26 11:06:57 -070055}
56
57TosaSerializationTensor::~TosaSerializationTensor()
58{}
59
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000060void TosaSerializationOperator::InitializeAttribute(Attribute attribute_type, const TosaAttributeBase* attribute)
Eric Kunze2364dcd2021-04-26 11:06:57 -070061{
Eric Kunze2364dcd2021-04-26 11:06:57 -070062 _attribute_type = attribute_type;
63
64 switch (attribute_type)
65 {
66 case Attribute_NONE:
67 _attribute = new TosaNoneAttribute();
68 break;
69#define DEF_ATTRIBUTE(NAME, ...) \
70 case Attribute_##NAME##Attribute: \
71 _attribute = new Tosa##NAME##Attribute(attribute); \
72 break;
73#include "attribute.def"
74#undef DEF_ATTRIBUTE
75 default:
76 printf("TosaSerializationOperator::TosaSerializationOperator(): Attribute %s not implemented yet\n",
77 EnumNamesAttribute()[attribute_type]);
78 assert(0);
79 }
80
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000081 assert(_attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +000082}
Eric Kunze2364dcd2021-04-26 11:06:57 -070083
Kevin Cheng545a5082021-11-11 01:36:33 +000084TosaSerializationOperator::TosaSerializationOperator(Op op,
85 Attribute attribute_type,
86 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +000087 const std::vector<std::string>& input_tensor_names,
88 const std::vector<std::string>& output_tensor_names)
89{
90 _op = op;
Eric Kunze2364dcd2021-04-26 11:06:57 -070091 _input_tensor_names = input_tensor_names;
92 _output_tensor_names = output_tensor_names;
Kevin Cheng545a5082021-11-11 01:36:33 +000093
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000094 InitializeAttribute(attribute_type, attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +000095}
96
97TosaSerializationOperator::TosaSerializationOperator(Op op,
98 Attribute attribute_type,
99 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +0000100 std::vector<std::string>&& input_tensor_names,
101 std::vector<std::string>&& output_tensor_names)
102{
103 _op = op;
104 _input_tensor_names = std::move(input_tensor_names);
105 _output_tensor_names = std::move(output_tensor_names);
106
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000107 InitializeAttribute(attribute_type, attribute);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700108}
109
110TosaSerializationOperator::~TosaSerializationOperator()
111{
112 delete _attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700113}
114
Kevin Cheng545a5082021-11-11 01:36:33 +0000115TosaSerializationBasicBlock::TosaSerializationBasicBlock(const std::string& name,
116 const std::vector<TosaSerializationOperator*>& operators,
117 const std::vector<TosaSerializationTensor*>& tensors,
118 const std::vector<std::string>& inputs,
119 const std::vector<std::string>& outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700120{
Eric Kunze2364dcd2021-04-26 11:06:57 -0700121 _name = name;
122 _operators = operators;
123 _tensors = tensors;
124 _inputs = inputs;
125 _outputs = outputs;
126}
127
Kevin Cheng545a5082021-11-11 01:36:33 +0000128TosaSerializationBasicBlock::TosaSerializationBasicBlock(std::string&& name,
129 std::vector<TosaSerializationOperator*>&& operators,
130 std::vector<TosaSerializationTensor*>&& tensors,
131 std::vector<std::string>&& inputs,
132 std::vector<std::string>&& outputs)
133{
134 _name = std::move(name);
135 _operators = std::move(operators);
136 _tensors = std::move(tensors);
137 _inputs = std::move(inputs);
138 _outputs = std::move(outputs);
139}
140
Eric Kunze2364dcd2021-04-26 11:06:57 -0700141TosaSerializationBasicBlock::~TosaSerializationBasicBlock()
142{
143 // deallocate all operators
144 for (auto op : GetOperators())
145 {
146 delete op; // ~TosaSerializationOperator()
147 }
148
149 // deallocate all tensors
150 for (auto ts : GetTensors())
151 {
152 delete ts; // ~TosaSerializationTensor()
153 }
154}
155
156TosaSerializationHandler::TosaSerializationHandler()
157{
158 _schemaLoaded = false;
Kevin Chenge6563f52021-10-20 12:12:02 -0700159 _version = TosaVersion(TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700160}
161
162TosaSerializationHandler::~TosaSerializationHandler()
163{
164 Clear(); // deallocate all basic blocks
165}
166
Kevin Chenga81a7a12021-11-10 14:07:34 -0800167TosaVersion TosaSerializationHandler::ParseTosaSchemaVersion(std::string schema)
168{
169 // Parse all 4 version fields in schema file
170 static const char* keywords[4] = { "major: int32 = ", "minor: int32 = ", "patch: int32 = ", "draft: bool = " };
171 string keyword_str[4];
172 size_t search_pos = 0;
173 size_t keyword_pos;
174 size_t semicolon_pos;
175 // parse integer field first
176 for (int32_t i = 0; i < 4; i++)
177 {
178 keyword_pos = schema.find(keywords[i], search_pos);
179 if (keyword_pos == std::string::npos)
180 {
181 printf("ERROR: can't find keyword \"%s\" in schema\n", keywords[i]);
182 assert(0);
183 }
184 semicolon_pos = schema.find(';', keyword_pos);
185 if (keyword_pos == std::string::npos)
186 {
187 printf("ERROR: can't find ';' in schema\n");
188 assert(0);
189 }
190 keyword_str[i] =
191 schema.substr(keyword_pos + strlen(keywords[i]), semicolon_pos - keyword_pos - strlen(keywords[i]));
192 search_pos = semicolon_pos;
193 }
194
195 int32_t schema_major = 0;
196 int32_t schema_minor = 0;
197 int32_t schema_patch = 0;
198 bool schema_draft = false;
199 try
200 {
201 schema_major = stoi(keyword_str[0]);
202 schema_minor = stoi(keyword_str[1]);
203 schema_patch = stoi(keyword_str[2]);
204 schema_draft = (keyword_str[3] == "true") ? true : false;
205 }
206 catch (std::invalid_argument& e)
207 {
208 printf("ERROR: fail at stoi(): %s\n", e.what());
209 assert(0);
210 }
211
212 TosaVersion schema_version(schema_major, schema_minor, schema_patch, schema_draft);
213
214 return schema_version;
215}
216
Eric Kunze2364dcd2021-04-26 11:06:57 -0700217tosa_err_t TosaSerializationHandler::LoadFileSchema(const char* schema_filename)
218{
219 std::string schema;
220 bool ok;
221
222 ok = flatbuffers::LoadFile(schema_filename, false, &schema);
223 if (!ok)
224 {
225 printf("Error loading schema file: %s\n", schema_filename);
226 return TOSA_FILE_ERROR;
227 }
228
229 ok = _parser.Parse(schema.c_str());
Kevin Chenga81a7a12021-11-10 14:07:34 -0800230
231 TosaVersion schema_version = ParseTosaSchemaVersion(schema);
232
233 TosaVersion::compat_t is_compat = schema_version.is_compatible(GetVersion());
234 switch (is_compat)
235 {
236 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
237 break;
238 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
239 printf("WARNING: Schema flatbuffer version %s is partially compatible with serializer version %s\n",
240 schema_version.to_string().c_str(), GetVersion().to_string().c_str());
241 break;
242 case TosaVersion::compat_t::NOT_COMPATIBLE:
243 printf("ERROR: Schema flatbuffer version %s is not compatible with serializer version %s\n",
244 schema_version.to_string().c_str(), GetVersion().to_string().c_str());
245 return TOSA_VERSION_MISMATCH;
246 }
247
Eric Kunze2364dcd2021-04-26 11:06:57 -0700248 if (!ok)
249 {
250 printf("Error parsing ISA schema file: %s\n", schema_filename);
251 return TOSA_FILE_ERROR;
252 }
253 _schemaLoaded = true;
254
255 return TOSA_OK;
256}
257
258tosa_err_t TosaSerializationHandler::LoadFileJson(const char* filename)
259{
260 std::string jsonfile;
261 bool ok;
262 tosa_err_t err;
263
264 if (!_schemaLoaded)
265 {
266 return TOSA_SCHEMA_MISSING;
267 }
268
269 ok = flatbuffers::LoadFile(filename, false, &jsonfile);
270 if (!ok)
271 {
272 printf("Error loading json file: %s\n", filename);
273 return TOSA_FILE_ERROR;
274 }
275
276 ok = _parser.Parse(jsonfile.c_str());
277 if (!ok)
278 {
279 printf("Error parsing json file: %s\n", filename);
280 return TOSA_FILE_ERROR;
281 }
282
283 uint8_t* buf = _parser.builder_.GetBufferPointer();
284
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700285 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700286 if (err != TOSA_OK)
287 {
288 return err;
289 }
290
291 return TOSA_OK;
292}
293
294tosa_err_t TosaSerializationHandler::SaveFileJson(const char* filename)
295{
296 std::string jsongen;
297 tosa_err_t err;
298
299 if (!_schemaLoaded)
300 {
301 return TOSA_SCHEMA_MISSING;
302 }
303
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700304 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700305 if (err != TOSA_OK)
306 {
307 return err;
308 }
309
310 uint8_t* buf = _builder.GetBufferPointer();
311
312 if (!GenerateText(_parser, buf, &jsongen))
313 {
314 printf("Couldn't serialize parsed data to JSON!\n");
315 return TOSA_FILE_ERROR;
316 }
317
318 FILE* file = fopen(filename, "wb");
319
320 if (!file)
321 {
322 printf("Couldn't open output file: %s\n", filename);
323 return TOSA_FILE_ERROR;
324 }
325
326 if (fwrite(jsongen.c_str(), sizeof(char), jsongen.size(), file) != jsongen.size())
327 {
328 printf("Error writing to json output file: %s\n", filename);
329 fclose(file);
330 return TOSA_FILE_ERROR;
331 }
332
333 if (file)
334 fclose(file);
335
336 return TOSA_OK;
337}
338
339tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const char* filename)
340{
341 std::string read_buffer;
342 tosa_err_t err;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800343 const uint8_t* buf;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700344 bool ok;
345
346 ok = flatbuffers::LoadFile(filename, false, &read_buffer);
347 if (!ok)
348 {
349 printf("Error loading flatbuffer file: %s\n", filename);
350 return TOSA_FILE_ERROR;
351 }
352
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800353 buf = reinterpret_cast<const uint8_t*>(read_buffer.data());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700354
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700355 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700356 if (err != TOSA_OK)
357 {
358 return err;
359 }
360
361 return TOSA_OK;
362}
363
Aaron DeBattista8b3903a2021-11-18 16:38:11 +0000364tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const void* input, int in_size)
365{
366 tosa_err_t err;
367
368 const uint8_t* buf = (const uint8_t*)input;
369 err = Deserialize(buf);
370 if (err != TOSA_OK)
371 {
372 return err;
373 }
374
375 return TOSA_OK;
376}
377
Eric Kunze2364dcd2021-04-26 11:06:57 -0700378tosa_err_t TosaSerializationHandler::SaveFileTosaFlatbuffer(const char* filename)
379{
380 tosa_err_t err;
381
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700382 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700383 if (err != TOSA_OK)
384 {
385 return err;
386 }
387
388 uint8_t* buf = _builder.GetBufferPointer();
389
390 bool ok = flatbuffers::SaveFile(filename, (const char*)buf, _builder.GetSize(), false);
391 if (!ok)
392 {
393 printf("Error saving floatbuffer file: %s\n", filename);
394 return TOSA_FILE_ERROR;
395 }
396
397 return TOSA_OK;
398}
399
400tosa_err_t TosaSerializationHandler::Clear()
401{
402 // deallocate all basic blocks
403 for (auto bb : GetBlocks())
404 {
405 delete bb;
406 }
407 _blocks.clear();
408
409 return TOSA_OK;
410}
411
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700412tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700413{
Eric Kunzee6596402022-06-09 21:27:36 +0000414 if (!TosaGraphBufferHasIdentifier(buf))
415 {
416 printf("WARNING: TOSA file does not have TOSA file identifier\n");
417 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700418 auto fb_tosa_graph = GetTosaGraph(buf);
419 auto fb_tosa_version = fb_tosa_graph->version();
420 auto fb_tosa_blocks = fb_tosa_graph->blocks();
421
422 std::vector<std::string> operator_inputs_container;
423 std::vector<std::string> operator_outputs_container;
424
425 std::vector<TosaSerializationOperator*> block_operators_container;
426 std::vector<TosaSerializationTensor*> block_tensors_container;
427 std::vector<std::string> block_inputs_container;
428 std::vector<std::string> block_outputs_container;
429
430 TosaAttributeBase* typed_attribute = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700431 TosaSerializationOperator* new_operator = NULL;
432 TosaSerializationBasicBlock* new_block = NULL;
433 TosaSerializationTensor* new_tensor = NULL;
434
435 // erase container
436 Clear();
437
Kevin Chenge6563f52021-10-20 12:12:02 -0700438 TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(),
439 fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700440
Kevin Chenge6563f52021-10-20 12:12:02 -0700441 TosaVersion::compat_t is_compat = read_version.is_compatible(GetVersion());
442 switch (is_compat)
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700443 {
Kevin Chenge6563f52021-10-20 12:12:02 -0700444 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
445 break;
446 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
447 printf("WARNING: Read flatbuffer version %s is partially compatible with serializer version %s\n",
448 read_version.to_string().c_str(), GetVersion().to_string().c_str());
449 break;
450 case TosaVersion::compat_t::NOT_COMPATIBLE:
451 printf("ERROR: Read flatbuffer version %s is not compatible with serializer version %s\n",
452 read_version.to_string().c_str(), GetVersion().to_string().c_str());
453 return TOSA_VERSION_MISMATCH;
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700454 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700455
456 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
457 {
458 auto curr_block = fb_tosa_blocks->Get(i);
459
460 auto block_name = curr_block->name()->str();
461
462 auto fb_tosa_operators = curr_block->operators();
463 block_operators_container.clear();
464 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
465 {
466 auto curr_operator = fb_tosa_operators->Get(j);
467
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000468 auto operator_op = curr_operator->op();
469 auto attribute_type = curr_operator->attribute_type();
470 auto attribute = curr_operator->attribute();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700471
472 // input tensors
473 auto operator_inputs = curr_operator->inputs();
474 operator_inputs_container.clear();
475 if (operator_inputs)
476 {
477 for (size_t k = 0; k < operator_inputs->size(); k++)
478 {
479 auto curr_input = operator_inputs->Get(k);
480 operator_inputs_container.push_back(curr_input->str());
481 }
482 }
483
484 // output tensors
485 auto operator_outputs = curr_operator->outputs();
486 operator_outputs_container.clear();
487 if (operator_outputs)
488 {
489 for (size_t k = 0; k < operator_outputs->size(); k++)
490 {
491 auto curr_output = operator_outputs->Get(k);
492 operator_outputs_container.push_back(curr_output->str());
493 }
494 }
495
496 switch (attribute_type)
497 {
498 case Attribute_NONE:
499 typed_attribute = new TosaNoneAttribute();
500 break;
501#define DEF_ATTRIBUTE(NAME, ...) \
502 case Attribute_##NAME##Attribute: \
503 typed_attribute = new Tosa##NAME##Attribute(attribute); \
504 break;
505#include "attribute.def"
506#undef DEF_ATTRIBUTE
507 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700508 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700509 EnumNamesAttribute()[attribute_type]);
510 return TOSA_INTERNAL_ERROR;
511 }
512
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000513 new_operator = new TosaSerializationOperator(operator_op, attribute_type, typed_attribute,
514 operator_inputs_container, operator_outputs_container);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700515 if (new_operator)
516 {
517 block_operators_container.push_back(new_operator);
518 }
519 else
520 {
521 return TOSA_MEMORY_ERROR;
522 }
523
524 if (typed_attribute)
525 delete typed_attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700526 }
527
528 auto fb_tosa_tensors = curr_block->tensors();
529 block_tensors_container.clear();
530 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
531 {
532 auto curr_tensor = fb_tosa_tensors->Get(j);
533
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700534 auto tensor_name = curr_tensor->name();
535 auto tensor_shape = curr_tensor->shape();
536 auto tensor_type = curr_tensor->type();
537 auto tensor_data = curr_tensor->data();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700538
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700539 new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700540 if (new_tensor)
541 {
542 block_tensors_container.push_back(new_tensor);
543 }
544 else
545 {
546 return TOSA_MEMORY_ERROR;
547 }
548 }
549
550 auto block_inputs = curr_block->inputs();
551 auto block_outputs = curr_block->outputs();
552
553 block_inputs_container.clear();
554 block_outputs_container.clear();
555
556 for (size_t j = 0; j < block_inputs->size(); j++)
557 {
558 auto curr_block_input = block_inputs->Get(j);
559 block_inputs_container.push_back(curr_block_input->str());
560 }
561 for (size_t j = 0; j < block_outputs->size(); j++)
562 {
563 auto curr_block_output = block_outputs->Get(j);
564 block_outputs_container.push_back(curr_block_output->str());
565 }
566
567 new_block = new TosaSerializationBasicBlock(block_name, block_operators_container, block_tensors_container,
568 block_inputs_container, block_outputs_container);
569 if (new_block)
570 {
571 this->GetBlocks().push_back(new_block);
572 }
573 else
574 {
575 return TOSA_MEMORY_ERROR;
576 }
577 }
578
579 return TOSA_OK;
580}
581
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700582tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700583{
584 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
585
586 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
587 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
588 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
589 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
590
591 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
592 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
593
594 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
595 for (auto block : GetBlocks())
596 {
597 fboffset_block_operators.clear();
598 fboffset_block_tensors.clear();
599 fboffset_block_inputs.clear();
600 fboffset_block_outputs.clear();
601
602 auto block_name = _builder.CreateString(block->GetName().c_str());
603
604 for (auto tensor_str : block->GetInputs())
605 {
606 auto tensor_name = _builder.CreateString(tensor_str.c_str());
607 fboffset_block_inputs.push_back(tensor_name);
608 }
609
610 for (auto tensor_str : block->GetOutputs())
611 {
612 auto tensor_name = _builder.CreateString(tensor_str.c_str());
613 fboffset_block_outputs.push_back(tensor_name);
614 }
615
616 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
617 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
618
619 for (auto op : block->GetOperators())
620 {
621 fboffset_operator_inputs.clear();
622 fboffset_operator_outputs.clear();
623
624 auto operator_op = op->GetOp();
625 auto attribute_type = op->GetAttributeType();
626
627 for (auto tensor_str : op->GetInputTensorNames())
628 {
629 auto tensor_name = _builder.CreateString(tensor_str.c_str());
630 fboffset_operator_inputs.push_back(tensor_name);
631 }
632
633 for (auto tensor_str : op->GetOutputTensorNames())
634 {
635 auto tensor_name = _builder.CreateString(tensor_str.c_str());
636 fboffset_operator_outputs.push_back(tensor_name);
637 }
638
639 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
640 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
641
642 flatbuffers::Offset<void> fb_attribute;
643 switch (attribute_type)
644 {
645 case Attribute_NONE:
646 fb_attribute = 0;
647 break;
648
649#define DEF_ARGS_S_STR(NAME, V) , _builder.CreateString(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V().c_str())
650#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
651
652#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
653#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
654#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
655#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Ward485a11d2022-08-05 13:48:37 +0100656#define DEF_ARGS_S_DType(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700657#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
658
659#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
660#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
661
662#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
663#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
664#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
665 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
666#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
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#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
669 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
670 DEF_ARGS_##F4(NAME, T4, V4)
671#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
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)
674#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) \
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#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
678 case Attribute_##NAME##Attribute: \
679 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
680 break;
681
682#include "attribute.def"
683#undef DEF_ATTRIBUTE
684#undef DEF_ARGS_1
685#undef DEF_ARGS_2
686#undef DEF_ARGS_3
687#undef DEF_ARGS_4
688#undef DEF_ARGS_5
689#undef DEF_ARGS_6
690#undef DEF_ARGS_7
691#undef DEF_ARGS_S
692#undef DEF_ARGS_V
693#undef DEF_ARGS_S_int32_t
694#undef DEF_ARGS_S_float
695#undef DEF_ARGS_S_bool
696#undef DEF_ARGS_S_ResizeMode
James Ward485a11d2022-08-05 13:48:37 +0100697#undef DEF_ARGS_S_DType
Eric Kunze2364dcd2021-04-26 11:06:57 -0700698#undef DEF_ARGS_S_string
699#undef DEF_ARGS_S_STR
700#undef DEF_ARGS_S_DEFAULT
701 default:
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700702 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
Eric Kunze2364dcd2021-04-26 11:06:57 -0700703 EnumNamesAttribute()[attribute_type]);
704 return TOSA_INTERNAL_ERROR;
705 }
706
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000707 auto fboffset_operator = CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute,
708 fb_operator_inputs, fb_operator_outputs);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700709 fboffset_block_operators.push_back(fboffset_operator);
710 }
711
712 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
713
714 for (auto tensor : block->GetTensors())
715 {
716
717 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
718 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
719 auto tensor_dtype = tensor->GetDtype();
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700720 auto tensor_data = _builder.CreateVector(tensor->GetData());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700721
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700722 auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700723 fboffset_block_tensors.push_back(fboffset_tensor);
724 }
725
726 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
727
728 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
729 fb_block_inputs, fb_block_outputs);
730 fboffset_blocks.push_back(fboffset_block);
731 }
732
733 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
734
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700735 auto fb_version =
736 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700737
738 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_blocks);
Eric Kunzee6596402022-06-09 21:27:36 +0000739 _builder.Finish(fb_graph, TosaGraphIdentifier());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700740
741 return TOSA_OK;
742}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700743
744void zero_pad(std::vector<uint8_t>& buf)
745{
746 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
747 {
748 buf.push_back(0);
749 }
750}
751
James Ward485a11d2022-08-05 13:48:37 +0100752tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
753{
754 // Note: Converts fp32->fp16 before converting to uint8_t
755 out.clear();
756 for (auto val : in)
757 {
758 half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val);
759 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16);
760 out.push_back(*val_u16 & 0xFF);
761 out.push_back((*val_u16 >> 8) & 0xFF);
762 }
763 zero_pad(out);
764 return TOSA_OK;
765}
766
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700767tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
768{
769 out.clear();
770 for (auto val : in)
771 {
772 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
773 out.push_back(*val_u32 & 0xFF);
774 out.push_back((*val_u32 >> 8) & 0xFF);
775 out.push_back((*val_u32 >> 16) & 0xFF);
776 out.push_back((*val_u32 >> 24) & 0xFF);
777 }
778 zero_pad(out);
779 return TOSA_OK;
780}
781
782tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
783{
784 out.clear();
785 for (auto val : in)
786 {
787 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
788 out.push_back(*val_u64 & 0xFF);
789 out.push_back((*val_u64 >> 8) & 0xFF);
790 out.push_back((*val_u64 >> 16) & 0xFF);
791 out.push_back((*val_u64 >> 24) & 0xFF);
792 out.push_back((*val_u64 >> 32) & 0xFF);
793 out.push_back((*val_u64 >> 40) & 0xFF);
794 }
795 zero_pad(out);
796 return TOSA_OK;
797}
798
799tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
800{
801 out.clear();
802 for (auto val : in)
803 {
804 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
805 out.push_back(*val_u32 & 0xFF);
806 out.push_back((*val_u32 >> 8) & 0xFF);
807 out.push_back((*val_u32 >> 16) & 0xFF);
808 out.push_back((*val_u32 >> 24) & 0xFF);
809 }
810 zero_pad(out);
811 return TOSA_OK;
812}
813
814tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
815{
816 out.clear();
817 for (auto val : in)
818 {
819 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
820 out.push_back(*val_u16 & 0xFF);
821 out.push_back((*val_u16 >> 8) & 0xFF);
822 }
823 zero_pad(out);
824 return TOSA_OK;
825}
826
827tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
828{
829 out.clear();
830 for (auto val : in)
831 {
832 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
833 out.push_back(*val_u8);
834 }
835 zero_pad(out);
836 return TOSA_OK;
837}
838
Kevin Cheng3ce56342021-07-28 13:42:29 -0700839// Two int4 values are packed into one byte out.
840// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
841// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
842tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
843{
844 out.clear();
845 uint32_t in_size = in.size();
846 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800847 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700848 {
849 int8_t val_0 = in[2 * i];
850 int8_t val_1 = 0;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800851 if (2u * i + 1u < in_size)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700852 {
853 val_1 = in[2 * i + 1];
854 }
855 // In TOSA spec, int4 ranges [-7, 7]
856 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
857 {
858 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
859 val_0, val_1);
860 return TOSA_USER_ERROR;
861 }
862 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
863 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
864 out.push_back(val_u8);
865 }
866 zero_pad(out);
867 return TOSA_OK;
868}
869
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700870tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
871{
872 out.clear();
873 for (auto val : in)
874 {
Eric Kunze4417b422022-06-20 07:27:42 -0700875 uint8_t val_u8 = val;
876 out.push_back(val_u8);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700877 }
878 zero_pad(out);
879 return TOSA_OK;
880}
881
882tosa_err_t
James Ward485a11d2022-08-05 13:48:37 +0100883 TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
884{
885 // Note: fp16 values returned in fp32 type
886 out.clear();
887 if (in.size() < out_size * sizeof(int16_t))
888 {
889 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
890 out_size * sizeof(int16_t));
891 return TOSA_USER_ERROR;
892 }
893
894 for (uint32_t i = 0; i < out_size; i++)
895 {
896 uint16_t f16_byte0 = in[i * sizeof(int16_t)];
897 uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1];
898 uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8);
899
900 // Reinterpret u16 byte as fp16 then convert to fp32
901 half_float::half val_f16 = *(half_float::half*)&val_u16;
902 float val_fp32 = half_float::half_cast<float, half_float::half>(val_f16);
903 out.push_back(val_fp32);
904 }
905 return TOSA_OK;
906}
907
908tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700909 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
910{
911 out.clear();
912 if (in.size() < out_size * sizeof(float))
913 {
914 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
915 out_size * sizeof(float));
916 return TOSA_USER_ERROR;
917 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800918 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700919 {
920 uint32_t byte0 = in[i * sizeof(float)];
921 uint32_t byte1 = in[i * sizeof(float) + 1];
922 uint32_t byte2 = in[i * sizeof(float) + 2];
923 uint32_t byte3 = in[i * sizeof(float) + 3];
924 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
925 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
926 out.push_back(*val_fp32);
927 }
928 return TOSA_OK;
929}
930
931tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
932 uint32_t out_size,
933 std::vector<int64_t>& out)
934{
935 out.clear();
936 if (in.size() < out_size * 6 /* sizeof(int48) */)
937 {
938 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
939 out_size * 6);
940 return TOSA_USER_ERROR;
941 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800942 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700943 {
944 uint64_t byte0 = in[i * 6];
945 uint64_t byte1 = in[i * 6 + 1];
946 uint64_t byte2 = in[i * 6 + 2];
947 uint64_t byte3 = in[i * 6 + 3];
948 uint64_t byte4 = in[i * 6 + 4];
949 uint64_t byte5 = in[i * 6 + 5];
950 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
951 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
952 if (sign)
953 {
954 uint64_t sext_mask = (0xFFFFUL << 48);
955 val_u64 |= sext_mask;
956 }
957 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
958 out.push_back(*val_i64);
959 }
960 return TOSA_OK;
961}
962
963tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
964 uint32_t out_size,
965 std::vector<int32_t>& out)
966{
967 out.clear();
968 if (in.size() < out_size * sizeof(int32_t))
969 {
970 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
971 out_size * sizeof(int32_t));
972 return TOSA_USER_ERROR;
973 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800974 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700975 {
976 uint32_t byte0 = in[i * sizeof(int32_t)];
977 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
978 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
979 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
980 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
981 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
982 out.push_back(*val_i32);
983 }
984 return TOSA_OK;
985}
986
987tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
988 uint32_t out_size,
989 std::vector<int16_t>& out)
990{
991 out.clear();
992 if (in.size() < out_size * sizeof(int16_t))
993 {
994 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
995 out_size * sizeof(int16_t));
996 return TOSA_USER_ERROR;
997 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800998 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700999 {
1000 uint16_t byte0 = in[i * sizeof(int16_t)];
1001 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
1002 uint16_t val_u16 = byte0 + (byte1 << 8);
1003 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
1004 out.push_back(*val_i16);
1005 }
1006 return TOSA_OK;
1007}
1008
1009tosa_err_t
1010 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1011{
1012 out.clear();
1013 if (in.size() < out_size * sizeof(int8_t))
1014 {
1015 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -07001016 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001017 return TOSA_USER_ERROR;
1018 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001019 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001020 {
1021 uint8_t val_u8 = in[i];
1022 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
1023 out.push_back(*val_i8);
1024 }
1025 return TOSA_OK;
1026}
1027
1028tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -07001029 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1030{
1031 out.clear();
1032 if (out_size > in.size() * 2)
1033 {
1034 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
1035 out_size, in.size());
1036 return TOSA_USER_ERROR;
1037 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001038 for (size_t i = 0; i < in.size(); i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -07001039 {
1040 uint8_t val_u8 = in[i];
1041 uint8_t val_0_u4 = val_u8 & 0xF;
1042 uint8_t val_1_u4 = val_u8 >> 4;
1043 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1044 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1045 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1046 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1047 // In TOSA spec, int4 ranges [-7, 7]
1048 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1049 {
1050 printf(
1051 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1052 val_0, val_1);
1053 return TOSA_USER_ERROR;
1054 }
1055 out.push_back(val_0);
1056 if (2 * i + 1 < out_size)
1057 out.push_back(val_1);
1058 }
1059 return TOSA_OK;
1060}
1061
1062tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001063 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1064{
1065 out.clear();
1066 if (in.size() < out_size * sizeof(bool))
1067 {
1068 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1069 in.size(), out_size * sizeof(bool));
1070 return TOSA_USER_ERROR;
1071 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001072 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001073 {
1074 uint8_t val_u8 = in[i];
1075 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1076 out.push_back(*val_bool);
1077 }
1078 return TOSA_OK;
1079}