blob: cb44f1709d56d71c6589b99243306a03067b9408 [file] [log] [blame]
Eric Kunze2364dcd2021-04-26 11:06:57 -07001
Jerry Ge442261b2022-09-09 13:38:56 -07002// Copyright (c) 2020-2023, ARM Limited.
Eric Kunze2364dcd2021-04-26 11:06:57 -07003//
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,
Jerry Ge442261b2022-09-09 13:38:56 -070025 const flatbuffers::Vector<uint8_t>* data,
26 bool variable)
Eric Kunze2364dcd2021-04-26 11:06:57 -070027{
Jerry Ge442261b2022-09-09 13:38:56 -070028 _dtype = dtype;
29 _variable = variable;
Jerry Geb413a952023-05-08 19:17:22 +000030 if (shape)
31 {
32 std::copy(shape->begin(), shape->end(), std::back_inserter(_shape));
33 }
Eric Kunze2364dcd2021-04-26 11:06:57 -070034
35 assert(name);
36 _name = name->str();
37
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070038 if (data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070039 {
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070040 std::copy(data->begin(), data->end(), std::back_inserter(_data));
Eric Kunze2364dcd2021-04-26 11:06:57 -070041 }
42}
43
Kevin Cheng545a5082021-11-11 01:36:33 +000044TosaSerializationTensor::TosaSerializationTensor(const std::string& name,
Eric Kunze2364dcd2021-04-26 11:06:57 -070045 const std::vector<int32_t>& shape,
46 DType dtype,
Jerry Ge442261b2022-09-09 13:38:56 -070047 const std::vector<uint8_t>& data,
48 bool variable)
Eric Kunze2364dcd2021-04-26 11:06:57 -070049{
Jerry Ge442261b2022-09-09 13:38:56 -070050 _dtype = dtype;
51 _variable = variable;
52 _shape = shape;
53 _name = name;
54 _data = data;
Eric Kunze2364dcd2021-04-26 11:06:57 -070055}
56
57TosaSerializationTensor::TosaSerializationTensor()
58{
Jerry Ge442261b2022-09-09 13:38:56 -070059 _dtype = DType_UNKNOWN;
60 _variable = false;
61 _name = "UNKNOWN";
Eric Kunze2364dcd2021-04-26 11:06:57 -070062}
63
64TosaSerializationTensor::~TosaSerializationTensor()
65{}
66
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000067void TosaSerializationOperator::InitializeAttribute(Attribute attribute_type, const TosaAttributeBase* attribute)
Eric Kunze2364dcd2021-04-26 11:06:57 -070068{
Eric Kunze2364dcd2021-04-26 11:06:57 -070069 _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
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000088 assert(_attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +000089}
Eric Kunze2364dcd2021-04-26 11:06:57 -070090
Kevin Cheng545a5082021-11-11 01:36:33 +000091TosaSerializationOperator::TosaSerializationOperator(Op op,
92 Attribute attribute_type,
93 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +000094 const std::vector<std::string>& input_tensor_names,
95 const std::vector<std::string>& output_tensor_names)
96{
97 _op = op;
Eric Kunze2364dcd2021-04-26 11:06:57 -070098 _input_tensor_names = input_tensor_names;
99 _output_tensor_names = output_tensor_names;
Kevin Cheng545a5082021-11-11 01:36:33 +0000100
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000101 InitializeAttribute(attribute_type, attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +0000102}
103
104TosaSerializationOperator::TosaSerializationOperator(Op op,
105 Attribute attribute_type,
106 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +0000107 std::vector<std::string>&& input_tensor_names,
108 std::vector<std::string>&& output_tensor_names)
109{
110 _op = op;
111 _input_tensor_names = std::move(input_tensor_names);
112 _output_tensor_names = std::move(output_tensor_names);
113
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000114 InitializeAttribute(attribute_type, attribute);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700115}
116
117TosaSerializationOperator::~TosaSerializationOperator()
118{
119 delete _attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700120}
121
Kevin Cheng545a5082021-11-11 01:36:33 +0000122TosaSerializationBasicBlock::TosaSerializationBasicBlock(const std::string& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700123 const std::string& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000124 const std::vector<TosaSerializationOperator*>& operators,
125 const std::vector<TosaSerializationTensor*>& tensors,
126 const std::vector<std::string>& inputs,
127 const std::vector<std::string>& outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700128{
Jerry Ge13c78a62022-10-04 20:32:39 -0700129 _name = name;
130 _region_name = region_name;
131 _operators = operators;
132 _tensors = tensors;
133 _inputs = inputs;
134 _outputs = outputs;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700135}
136
Kevin Cheng545a5082021-11-11 01:36:33 +0000137TosaSerializationBasicBlock::TosaSerializationBasicBlock(std::string&& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700138 std::string&& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000139 std::vector<TosaSerializationOperator*>&& operators,
140 std::vector<TosaSerializationTensor*>&& tensors,
141 std::vector<std::string>&& inputs,
142 std::vector<std::string>&& outputs)
143{
Jerry Ge13c78a62022-10-04 20:32:39 -0700144 _name = std::move(name);
145 _region_name = std::move(region_name);
146 _operators = std::move(operators);
147 _tensors = std::move(tensors);
148 _inputs = std::move(inputs);
149 _outputs = std::move(outputs);
Kevin Cheng545a5082021-11-11 01:36:33 +0000150}
151
Eric Kunze2364dcd2021-04-26 11:06:57 -0700152TosaSerializationBasicBlock::~TosaSerializationBasicBlock()
153{
154 // deallocate all operators
155 for (auto op : GetOperators())
156 {
157 delete op; // ~TosaSerializationOperator()
158 }
159
160 // deallocate all tensors
161 for (auto ts : GetTensors())
162 {
163 delete ts; // ~TosaSerializationTensor()
164 }
165}
166
Jerry Ge13c78a62022-10-04 20:32:39 -0700167TosaSerializationRegion::TosaSerializationRegion(const std::string& name,
168 const std::vector<TosaSerializationBasicBlock*>& blocks)
169{
170 _name = name;
171 _blocks = blocks;
172}
173
174TosaSerializationRegion::TosaSerializationRegion(const std::string&& name,
175 const std::vector<TosaSerializationBasicBlock*>&& blocks)
176{
177 _name = std::move(name);
178 _blocks = std::move(blocks);
179}
180
181TosaSerializationRegion::~TosaSerializationRegion()
182{
183 // deallocate all blocks
184 for (auto block : GetBlocks())
185 {
186 delete block; // ~TosaSerializationBasicBlock()
187 }
188}
189
Eric Kunze2364dcd2021-04-26 11:06:57 -0700190TosaSerializationHandler::TosaSerializationHandler()
191{
192 _schemaLoaded = false;
Kevin Chenge6563f52021-10-20 12:12:02 -0700193 _version = TosaVersion(TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700194}
195
196TosaSerializationHandler::~TosaSerializationHandler()
197{
198 Clear(); // deallocate all basic blocks
199}
200
Eric Kunze2364dcd2021-04-26 11:06:57 -0700201tosa_err_t TosaSerializationHandler::LoadFileSchema(const char* schema_filename)
202{
203 std::string schema;
204 bool ok;
205
206 ok = flatbuffers::LoadFile(schema_filename, false, &schema);
207 if (!ok)
208 {
209 printf("Error loading schema file: %s\n", schema_filename);
210 return TOSA_FILE_ERROR;
211 }
212
213 ok = _parser.Parse(schema.c_str());
Kevin Chenga81a7a12021-11-10 14:07:34 -0800214
Eric Kunze2364dcd2021-04-26 11:06:57 -0700215 if (!ok)
216 {
217 printf("Error parsing ISA schema file: %s\n", schema_filename);
218 return TOSA_FILE_ERROR;
219 }
220 _schemaLoaded = true;
221
222 return TOSA_OK;
223}
224
225tosa_err_t TosaSerializationHandler::LoadFileJson(const char* filename)
226{
227 std::string jsonfile;
228 bool ok;
229 tosa_err_t err;
230
231 if (!_schemaLoaded)
232 {
233 return TOSA_SCHEMA_MISSING;
234 }
235
236 ok = flatbuffers::LoadFile(filename, false, &jsonfile);
237 if (!ok)
238 {
239 printf("Error loading json file: %s\n", filename);
240 return TOSA_FILE_ERROR;
241 }
242
243 ok = _parser.Parse(jsonfile.c_str());
244 if (!ok)
245 {
246 printf("Error parsing json file: %s\n", filename);
247 return TOSA_FILE_ERROR;
248 }
249
250 uint8_t* buf = _parser.builder_.GetBufferPointer();
251
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700252 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700253 if (err != TOSA_OK)
254 {
255 return err;
256 }
257
258 return TOSA_OK;
259}
260
261tosa_err_t TosaSerializationHandler::SaveFileJson(const char* filename)
262{
263 std::string jsongen;
264 tosa_err_t err;
265
266 if (!_schemaLoaded)
267 {
268 return TOSA_SCHEMA_MISSING;
269 }
270
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700271 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700272 if (err != TOSA_OK)
273 {
274 return err;
275 }
276
277 uint8_t* buf = _builder.GetBufferPointer();
278
Tai Ly89963aa2023-07-03 22:14:05 +0000279 if (GenText(_parser, buf, &jsongen))
Eric Kunze2364dcd2021-04-26 11:06:57 -0700280 {
281 printf("Couldn't serialize parsed data to JSON!\n");
282 return TOSA_FILE_ERROR;
283 }
284
285 FILE* file = fopen(filename, "wb");
286
287 if (!file)
288 {
289 printf("Couldn't open output file: %s\n", filename);
290 return TOSA_FILE_ERROR;
291 }
292
293 if (fwrite(jsongen.c_str(), sizeof(char), jsongen.size(), file) != jsongen.size())
294 {
295 printf("Error writing to json output file: %s\n", filename);
296 fclose(file);
297 return TOSA_FILE_ERROR;
298 }
299
300 if (file)
301 fclose(file);
302
303 return TOSA_OK;
304}
305
306tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const char* filename)
307{
308 std::string read_buffer;
309 tosa_err_t err;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800310 const uint8_t* buf;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700311 bool ok;
312
313 ok = flatbuffers::LoadFile(filename, false, &read_buffer);
314 if (!ok)
315 {
316 printf("Error loading flatbuffer file: %s\n", filename);
317 return TOSA_FILE_ERROR;
318 }
319
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800320 buf = reinterpret_cast<const uint8_t*>(read_buffer.data());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700321
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700322 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700323 if (err != TOSA_OK)
324 {
325 return err;
326 }
327
328 return TOSA_OK;
329}
330
Aaron DeBattista8b3903a2021-11-18 16:38:11 +0000331tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const void* input, int in_size)
332{
333 tosa_err_t err;
334
335 const uint8_t* buf = (const uint8_t*)input;
336 err = Deserialize(buf);
337 if (err != TOSA_OK)
338 {
339 return err;
340 }
341
342 return TOSA_OK;
343}
344
Eric Kunze2364dcd2021-04-26 11:06:57 -0700345tosa_err_t TosaSerializationHandler::SaveFileTosaFlatbuffer(const char* filename)
346{
347 tosa_err_t err;
348
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700349 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700350 if (err != TOSA_OK)
351 {
352 return err;
353 }
354
355 uint8_t* buf = _builder.GetBufferPointer();
356
357 bool ok = flatbuffers::SaveFile(filename, (const char*)buf, _builder.GetSize(), false);
358 if (!ok)
359 {
360 printf("Error saving floatbuffer file: %s\n", filename);
361 return TOSA_FILE_ERROR;
362 }
363
364 return TOSA_OK;
365}
366
367tosa_err_t TosaSerializationHandler::Clear()
368{
369 // deallocate all basic blocks
Jerry Ge13c78a62022-10-04 20:32:39 -0700370 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700371 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700372 delete region;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700373 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700374 _regions.clear();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700375
376 return TOSA_OK;
377}
378
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700379tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700380{
Eric Kunzee6596402022-06-09 21:27:36 +0000381 if (!TosaGraphBufferHasIdentifier(buf))
382 {
383 printf("WARNING: TOSA file does not have TOSA file identifier\n");
384 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700385 auto fb_tosa_graph = GetTosaGraph(buf);
386 auto fb_tosa_version = fb_tosa_graph->version();
Jerry Ge13c78a62022-10-04 20:32:39 -0700387 auto fb_tosa_regions = fb_tosa_graph->regions();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700388
Eric Kunze2364dcd2021-04-26 11:06:57 -0700389 TosaAttributeBase* typed_attribute = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700390 TosaSerializationOperator* new_operator = NULL;
391 TosaSerializationBasicBlock* new_block = NULL;
392 TosaSerializationTensor* new_tensor = NULL;
Jerry Ge13c78a62022-10-04 20:32:39 -0700393 TosaSerializationRegion* new_region = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700394
395 // erase container
396 Clear();
397
Kevin Chenge6563f52021-10-20 12:12:02 -0700398 TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(),
399 fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700400
Kevin Chenge6563f52021-10-20 12:12:02 -0700401 TosaVersion::compat_t is_compat = read_version.is_compatible(GetVersion());
402 switch (is_compat)
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700403 {
Kevin Chenge6563f52021-10-20 12:12:02 -0700404 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
405 break;
406 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
407 printf("WARNING: Read flatbuffer version %s is partially compatible with serializer version %s\n",
408 read_version.to_string().c_str(), GetVersion().to_string().c_str());
409 break;
410 case TosaVersion::compat_t::NOT_COMPATIBLE:
411 printf("ERROR: Read flatbuffer version %s is not compatible with serializer version %s\n",
412 read_version.to_string().c_str(), GetVersion().to_string().c_str());
413 return TOSA_VERSION_MISMATCH;
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700414 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700415
Jerry Ge13c78a62022-10-04 20:32:39 -0700416 for (size_t i = 0; i < fb_tosa_regions->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700417 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700418 auto curr_region = fb_tosa_regions->Get(i);
419 auto region_name = curr_region->name()->str();
420 auto fb_tosa_blocks = curr_region->blocks();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700421
Tai Lycfcb20d2023-03-13 21:04:11 +0000422 new_region = new TosaSerializationRegion(curr_region->name()->str(), {});
Jerry Ge13c78a62022-10-04 20:32:39 -0700423 this->GetRegions().push_back(new_region);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700424
Jerry Ge13c78a62022-10-04 20:32:39 -0700425 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700426 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000427 std::vector<TosaSerializationOperator*> block_operators_container;
428 std::vector<TosaSerializationTensor*> block_tensors_container;
429 std::vector<std::string> block_inputs_container;
430 std::vector<std::string> block_outputs_container;
431
Jerry Ge13c78a62022-10-04 20:32:39 -0700432 auto curr_block = fb_tosa_blocks->Get(i);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700433
Jerry Ge13c78a62022-10-04 20:32:39 -0700434 auto block_name = curr_block->name()->str();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700435
Jerry Ge13c78a62022-10-04 20:32:39 -0700436 auto fb_tosa_operators = curr_block->operators();
Jerry Ge13c78a62022-10-04 20:32:39 -0700437 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700438 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700439 auto curr_operator = fb_tosa_operators->Get(j);
440
441 auto operator_op = curr_operator->op();
442 auto attribute_type = curr_operator->attribute_type();
443 auto attribute = curr_operator->attribute();
444
Tai Lycfcb20d2023-03-13 21:04:11 +0000445 std::vector<std::string> operator_inputs_container;
446 std::vector<std::string> operator_outputs_container;
447
Jerry Ge13c78a62022-10-04 20:32:39 -0700448 // input tensors
449 auto operator_inputs = curr_operator->inputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700450 if (operator_inputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700451 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700452 for (size_t k = 0; k < operator_inputs->size(); k++)
453 {
454 auto curr_input = operator_inputs->Get(k);
455 operator_inputs_container.push_back(curr_input->str());
456 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700457 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700458
Jerry Ge13c78a62022-10-04 20:32:39 -0700459 // output tensors
460 auto operator_outputs = curr_operator->outputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700461 if (operator_outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700462 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700463 for (size_t k = 0; k < operator_outputs->size(); k++)
464 {
465 auto curr_output = operator_outputs->Get(k);
466 operator_outputs_container.push_back(curr_output->str());
467 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700468 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700469
Jerry Ge13c78a62022-10-04 20:32:39 -0700470 switch (attribute_type)
471 {
472 case Attribute_NONE:
473 typed_attribute = new TosaNoneAttribute();
474 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700475#define DEF_ATTRIBUTE(NAME, ...) \
476 case Attribute_##NAME##Attribute: \
477 typed_attribute = new Tosa##NAME##Attribute(attribute); \
478 break;
479#include "attribute.def"
480#undef DEF_ATTRIBUTE
Jerry Ge13c78a62022-10-04 20:32:39 -0700481 default:
482 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
483 EnumNamesAttribute()[attribute_type]);
484 return TOSA_INTERNAL_ERROR;
485 }
486
487 new_operator = new TosaSerializationOperator(operator_op, attribute_type, typed_attribute,
488 operator_inputs_container, operator_outputs_container);
489 if (new_operator)
490 {
491 block_operators_container.push_back(new_operator);
492 }
493 else
494 {
495 return TOSA_MEMORY_ERROR;
496 }
497
498 if (typed_attribute)
499 delete typed_attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700500 }
501
Jerry Ge13c78a62022-10-04 20:32:39 -0700502 auto block_inputs = curr_block->inputs();
503 auto block_outputs = curr_block->outputs();
504
Jerry Ge13c78a62022-10-04 20:32:39 -0700505 for (size_t j = 0; j < block_inputs->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700506 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700507 auto curr_block_input = block_inputs->Get(j);
508 block_inputs_container.push_back(curr_block_input->str());
509 }
510 for (size_t j = 0; j < block_outputs->size(); j++)
511 {
512 auto curr_block_output = block_outputs->Get(j);
513 block_outputs_container.push_back(curr_block_output->str());
514 }
515
516 auto fb_tosa_tensors = curr_block->tensors();
Jerry Ge13c78a62022-10-04 20:32:39 -0700517 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
518 {
519 auto curr_tensor = fb_tosa_tensors->Get(j);
520
Jerry Ge442261b2022-09-09 13:38:56 -0700521 auto tensor_name = curr_tensor->name();
522 auto tensor_shape = curr_tensor->shape();
523 auto tensor_type = curr_tensor->type();
524 auto tensor_variable = curr_tensor->variable();
525 auto tensor_data = curr_tensor->data();
Jerry Ge13c78a62022-10-04 20:32:39 -0700526
Jerry Ge442261b2022-09-09 13:38:56 -0700527 new_tensor =
528 new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data, tensor_variable);
Jerry Ge13c78a62022-10-04 20:32:39 -0700529 if (new_tensor)
530 {
531 block_tensors_container.push_back(new_tensor);
532 }
533 else
534 {
535 return TOSA_MEMORY_ERROR;
536 }
537 }
538 new_block = new TosaSerializationBasicBlock(block_name, region_name, block_operators_container,
539 block_tensors_container, block_inputs_container,
540 block_outputs_container);
541 if (new_block)
542 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000543 new_region->GetBlocks().push_back(new_block);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700544 }
545 else
546 {
547 return TOSA_MEMORY_ERROR;
548 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700549 } // end block for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700550 }
551
552 return TOSA_OK;
553}
554
James Ward80905bb2023-01-25 15:51:27 +0000555std::vector<uint8_t> float_to_u8_helper(float f_in)
James Wardc15f7d52022-12-07 15:38:01 +0000556{
James Ward80905bb2023-01-25 15:51:27 +0000557 // Push back a single float value to the buffer with *NO PADDING*
558 // Therefore ConvertF32toU8 function not used
James Wardc15f7d52022-12-07 15:38:01 +0000559 std::vector<uint8_t> u8_out;
James Ward80905bb2023-01-25 15:51:27 +0000560 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&f_in);
561 u8_out.push_back(*val_u32 & 0xFF);
562 u8_out.push_back((*val_u32 >> 8) & 0xFF);
563 u8_out.push_back((*val_u32 >> 16) & 0xFF);
564 u8_out.push_back((*val_u32 >> 24) & 0xFF);
James Wardc15f7d52022-12-07 15:38:01 +0000565 return u8_out;
566}
567
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700568tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700569{
Jerry Ge13c78a62022-10-04 20:32:39 -0700570 // regions
571 std::vector<flatbuffers::Offset<TosaRegion>> fboffset_regions;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700572
Eric Kunze2364dcd2021-04-26 11:06:57 -0700573 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
Jerry Ge13c78a62022-10-04 20:32:39 -0700574 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700575 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000576 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
Jerry Ge13c78a62022-10-04 20:32:39 -0700577 for (auto block : region->GetBlocks())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700578 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000579 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
580 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
581 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
582 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700583 auto block_name = _builder.CreateString(block->GetName().c_str());
584 for (auto tensor_str : block->GetInputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700585 {
586 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700587 fboffset_block_inputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700588 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700589 for (auto tensor_str : block->GetOutputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700590 {
591 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700592 fboffset_block_outputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700593 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700594 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
595 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
596 for (auto op : block->GetOperators())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700597 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000598 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
599 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700600 auto operator_op = op->GetOp();
601 auto attribute_type = op->GetAttributeType();
602 for (auto tensor_str : op->GetInputTensorNames())
603 {
604 auto tensor_name = _builder.CreateString(tensor_str.c_str());
605 fboffset_operator_inputs.push_back(tensor_name);
606 }
607 for (auto tensor_str : op->GetOutputTensorNames())
608 {
609 auto tensor_name = _builder.CreateString(tensor_str.c_str());
610 fboffset_operator_outputs.push_back(tensor_name);
611 }
612 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
613 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
614 flatbuffers::Offset<void> fb_attribute;
615 switch (attribute_type)
616 {
617 case Attribute_NONE:
618 fb_attribute = 0;
619 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700620#define DEF_ARGS_S_STR(NAME, V) , _builder.CreateString(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V().c_str())
James Wardc15f7d52022-12-07 15:38:01 +0000621#define DEF_ARGS_S_FP_as_U8(NAME, V) \
James Ward80905bb2023-01-25 15:51:27 +0000622 , _builder.CreateVector<uint8_t>(float_to_u8_helper(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()))
Eric Kunze2364dcd2021-04-26 11:06:57 -0700623#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700624#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Wardc15f7d52022-12-07 15:38:01 +0000625#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_FP_as_U8(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700626#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
627#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Ward485a11d2022-08-05 13:48:37 +0100628#define DEF_ARGS_S_DType(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700629#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700630#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
631#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700632#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
633#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
634#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
635 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
636#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
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#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
639 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
640 DEF_ARGS_##F4(NAME, T4, V4)
641#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
642 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
643 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5)
644#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) \
645 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
646 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
647#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
648 case Attribute_##NAME##Attribute: \
649 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
650 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700651#include "attribute.def"
652#undef DEF_ATTRIBUTE
653#undef DEF_ARGS_1
654#undef DEF_ARGS_2
655#undef DEF_ARGS_3
656#undef DEF_ARGS_4
657#undef DEF_ARGS_5
658#undef DEF_ARGS_6
659#undef DEF_ARGS_7
660#undef DEF_ARGS_S
661#undef DEF_ARGS_V
662#undef DEF_ARGS_S_int32_t
663#undef DEF_ARGS_S_float
664#undef DEF_ARGS_S_bool
665#undef DEF_ARGS_S_ResizeMode
James Ward485a11d2022-08-05 13:48:37 +0100666#undef DEF_ARGS_S_DType
Eric Kunze2364dcd2021-04-26 11:06:57 -0700667#undef DEF_ARGS_S_string
668#undef DEF_ARGS_S_STR
669#undef DEF_ARGS_S_DEFAULT
Jerry Ge13c78a62022-10-04 20:32:39 -0700670 default:
671 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
672 EnumNamesAttribute()[attribute_type]);
673 return TOSA_INTERNAL_ERROR;
674 }
675 auto fboffset_operator = CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute,
676 fb_operator_inputs, fb_operator_outputs);
677 fboffset_block_operators.push_back(fboffset_operator);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700678 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700679 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
680 for (auto tensor : block->GetTensors())
681 {
682 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
683 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
684 auto tensor_dtype = tensor->GetDtype();
Jerry Ge442261b2022-09-09 13:38:56 -0700685 bool tensor_variable = tensor->GetVariable();
Jerry Ge13c78a62022-10-04 20:32:39 -0700686 auto tensor_data = _builder.CreateVector(tensor->GetData());
Jerry Ge442261b2022-09-09 13:38:56 -0700687 auto fboffset_tensor =
688 CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data, tensor_variable);
Jerry Ge13c78a62022-10-04 20:32:39 -0700689 fboffset_block_tensors.push_back(fboffset_tensor);
690 }
691 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
692 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
Won Jeoncb4bbf42023-08-10 08:50:15 +0000693 fb_block_inputs, fb_block_outputs);
Jerry Ge13c78a62022-10-04 20:32:39 -0700694 fboffset_blocks.push_back(fboffset_block);
695 } // end block for_loop
696 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700697
Jerry Ge13c78a62022-10-04 20:32:39 -0700698 auto region_name = _builder.CreateString(region->GetName().c_str());
699 auto fboffset_region = CreateTosaRegion(_builder, region_name, fb_blocks);
700 fboffset_regions.push_back(fboffset_region);
701 } // end region for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700702
Jerry Ge13c78a62022-10-04 20:32:39 -0700703 auto fb_regions = _builder.CreateVector(fboffset_regions);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700704
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700705 auto fb_version =
706 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Jerry Ge13c78a62022-10-04 20:32:39 -0700707 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_regions);
Eric Kunzee6596402022-06-09 21:27:36 +0000708 _builder.Finish(fb_graph, TosaGraphIdentifier());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700709
710 return TOSA_OK;
711}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700712
Jerry Ge442261b2022-09-09 13:38:56 -0700713void TosaSerializationHandler::ForceAlignTensorData(std::vector<uint8_t>& buf)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700714{
715 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
716 {
717 buf.push_back(0);
718 }
719}
720
James Ward485a11d2022-08-05 13:48:37 +0100721tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
722{
723 // Note: Converts fp32->fp16 before converting to uint8_t
724 out.clear();
725 for (auto val : in)
726 {
727 half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val);
728 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16);
729 out.push_back(*val_u16 & 0xFF);
730 out.push_back((*val_u16 >> 8) & 0xFF);
731 }
Jerry Ge442261b2022-09-09 13:38:56 -0700732 ForceAlignTensorData(out);
James Ward485a11d2022-08-05 13:48:37 +0100733 return TOSA_OK;
734}
735
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700736tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
737{
738 out.clear();
739 for (auto val : in)
740 {
741 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
742 out.push_back(*val_u32 & 0xFF);
743 out.push_back((*val_u32 >> 8) & 0xFF);
744 out.push_back((*val_u32 >> 16) & 0xFF);
745 out.push_back((*val_u32 >> 24) & 0xFF);
746 }
Jerry Ge442261b2022-09-09 13:38:56 -0700747 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700748 return TOSA_OK;
749}
750
751tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
752{
753 out.clear();
754 for (auto val : in)
755 {
756 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
757 out.push_back(*val_u64 & 0xFF);
758 out.push_back((*val_u64 >> 8) & 0xFF);
759 out.push_back((*val_u64 >> 16) & 0xFF);
760 out.push_back((*val_u64 >> 24) & 0xFF);
761 out.push_back((*val_u64 >> 32) & 0xFF);
762 out.push_back((*val_u64 >> 40) & 0xFF);
763 }
Jerry Ge442261b2022-09-09 13:38:56 -0700764 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700765 return TOSA_OK;
766}
767
768tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
769{
770 out.clear();
771 for (auto val : in)
772 {
773 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
774 out.push_back(*val_u32 & 0xFF);
775 out.push_back((*val_u32 >> 8) & 0xFF);
776 out.push_back((*val_u32 >> 16) & 0xFF);
777 out.push_back((*val_u32 >> 24) & 0xFF);
778 }
Jerry Ge442261b2022-09-09 13:38:56 -0700779 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700780 return TOSA_OK;
781}
782
783tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
784{
785 out.clear();
786 for (auto val : in)
787 {
788 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
789 out.push_back(*val_u16 & 0xFF);
790 out.push_back((*val_u16 >> 8) & 0xFF);
791 }
Jerry Ge442261b2022-09-09 13:38:56 -0700792 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700793 return TOSA_OK;
794}
795
796tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
797{
798 out.clear();
799 for (auto val : in)
800 {
801 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
802 out.push_back(*val_u8);
803 }
Jerry Ge442261b2022-09-09 13:38:56 -0700804 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700805 return TOSA_OK;
806}
807
Kevin Cheng3ce56342021-07-28 13:42:29 -0700808// Two int4 values are packed into one byte out.
809// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
810// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
811tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
812{
813 out.clear();
814 uint32_t in_size = in.size();
815 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800816 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700817 {
818 int8_t val_0 = in[2 * i];
819 int8_t val_1 = 0;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800820 if (2u * i + 1u < in_size)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700821 {
822 val_1 = in[2 * i + 1];
823 }
824 // In TOSA spec, int4 ranges [-7, 7]
825 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
826 {
827 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
828 val_0, val_1);
829 return TOSA_USER_ERROR;
830 }
831 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
832 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
833 out.push_back(val_u8);
834 }
Jerry Ge442261b2022-09-09 13:38:56 -0700835 ForceAlignTensorData(out);
Kevin Cheng3ce56342021-07-28 13:42:29 -0700836 return TOSA_OK;
837}
838
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700839tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
840{
841 out.clear();
842 for (auto val : in)
843 {
Eric Kunze4417b422022-06-20 07:27:42 -0700844 uint8_t val_u8 = val;
845 out.push_back(val_u8);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700846 }
Jerry Ge442261b2022-09-09 13:38:56 -0700847 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700848 return TOSA_OK;
849}
850
851tosa_err_t
James Ward485a11d2022-08-05 13:48:37 +0100852 TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
853{
854 // Note: fp16 values returned in fp32 type
855 out.clear();
856 if (in.size() < out_size * sizeof(int16_t))
857 {
858 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
859 out_size * sizeof(int16_t));
860 return TOSA_USER_ERROR;
861 }
862
863 for (uint32_t i = 0; i < out_size; i++)
864 {
865 uint16_t f16_byte0 = in[i * sizeof(int16_t)];
866 uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1];
867 uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8);
868
869 // Reinterpret u16 byte as fp16 then convert to fp32
870 half_float::half val_f16 = *(half_float::half*)&val_u16;
871 float val_fp32 = half_float::half_cast<float, half_float::half>(val_f16);
872 out.push_back(val_fp32);
873 }
874 return TOSA_OK;
875}
876
877tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700878 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
879{
880 out.clear();
881 if (in.size() < out_size * sizeof(float))
882 {
883 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
884 out_size * sizeof(float));
885 return TOSA_USER_ERROR;
886 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800887 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700888 {
889 uint32_t byte0 = in[i * sizeof(float)];
890 uint32_t byte1 = in[i * sizeof(float) + 1];
891 uint32_t byte2 = in[i * sizeof(float) + 2];
892 uint32_t byte3 = in[i * sizeof(float) + 3];
893 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
894 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
895 out.push_back(*val_fp32);
896 }
897 return TOSA_OK;
898}
899
900tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
901 uint32_t out_size,
902 std::vector<int64_t>& out)
903{
904 out.clear();
905 if (in.size() < out_size * 6 /* sizeof(int48) */)
906 {
907 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
908 out_size * 6);
909 return TOSA_USER_ERROR;
910 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800911 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700912 {
913 uint64_t byte0 = in[i * 6];
914 uint64_t byte1 = in[i * 6 + 1];
915 uint64_t byte2 = in[i * 6 + 2];
916 uint64_t byte3 = in[i * 6 + 3];
917 uint64_t byte4 = in[i * 6 + 4];
918 uint64_t byte5 = in[i * 6 + 5];
919 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
920 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
921 if (sign)
922 {
923 uint64_t sext_mask = (0xFFFFUL << 48);
924 val_u64 |= sext_mask;
925 }
926 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
927 out.push_back(*val_i64);
928 }
929 return TOSA_OK;
930}
931
932tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
933 uint32_t out_size,
934 std::vector<int32_t>& out)
935{
936 out.clear();
937 if (in.size() < out_size * sizeof(int32_t))
938 {
939 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
940 out_size * sizeof(int32_t));
941 return TOSA_USER_ERROR;
942 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800943 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700944 {
945 uint32_t byte0 = in[i * sizeof(int32_t)];
946 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
947 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
948 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
949 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
950 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
951 out.push_back(*val_i32);
952 }
953 return TOSA_OK;
954}
955
956tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
957 uint32_t out_size,
958 std::vector<int16_t>& out)
959{
960 out.clear();
961 if (in.size() < out_size * sizeof(int16_t))
962 {
963 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
964 out_size * sizeof(int16_t));
965 return TOSA_USER_ERROR;
966 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800967 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700968 {
969 uint16_t byte0 = in[i * sizeof(int16_t)];
970 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
971 uint16_t val_u16 = byte0 + (byte1 << 8);
972 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
973 out.push_back(*val_i16);
974 }
975 return TOSA_OK;
976}
977
978tosa_err_t
979 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
980{
981 out.clear();
982 if (in.size() < out_size * sizeof(int8_t))
983 {
984 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -0700985 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700986 return TOSA_USER_ERROR;
987 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800988 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700989 {
990 uint8_t val_u8 = in[i];
991 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
992 out.push_back(*val_i8);
993 }
994 return TOSA_OK;
995}
996
997tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -0700998 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
999{
1000 out.clear();
1001 if (out_size > in.size() * 2)
1002 {
1003 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
1004 out_size, in.size());
1005 return TOSA_USER_ERROR;
1006 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001007 for (size_t i = 0; i < in.size(); i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -07001008 {
1009 uint8_t val_u8 = in[i];
1010 uint8_t val_0_u4 = val_u8 & 0xF;
1011 uint8_t val_1_u4 = val_u8 >> 4;
1012 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1013 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1014 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1015 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1016 // In TOSA spec, int4 ranges [-7, 7]
1017 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1018 {
1019 printf(
1020 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1021 val_0, val_1);
1022 return TOSA_USER_ERROR;
1023 }
1024 out.push_back(val_0);
1025 if (2 * i + 1 < out_size)
1026 out.push_back(val_1);
1027 }
1028 return TOSA_OK;
1029}
1030
1031tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001032 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1033{
1034 out.clear();
1035 if (in.size() < out_size * sizeof(bool))
1036 {
1037 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1038 in.size(), out_size * sizeof(bool));
1039 return TOSA_USER_ERROR;
1040 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001041 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001042 {
1043 uint8_t val_u8 = in[i];
1044 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1045 out.push_back(*val_bool);
1046 }
1047 return TOSA_OK;
1048}