blob: d84e0ab46235dd83b38f11406654f5eabc01ca65 [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,
Jerry Ge13c78a62022-10-04 20:32:39 -0700116 const std::string& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000117 const std::vector<TosaSerializationOperator*>& operators,
118 const std::vector<TosaSerializationTensor*>& tensors,
119 const std::vector<std::string>& inputs,
120 const std::vector<std::string>& outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700121{
Jerry Ge13c78a62022-10-04 20:32:39 -0700122 _name = name;
123 _region_name = region_name;
124 _operators = operators;
125 _tensors = tensors;
126 _inputs = inputs;
127 _outputs = outputs;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700128}
129
Kevin Cheng545a5082021-11-11 01:36:33 +0000130TosaSerializationBasicBlock::TosaSerializationBasicBlock(std::string&& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700131 std::string&& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000132 std::vector<TosaSerializationOperator*>&& operators,
133 std::vector<TosaSerializationTensor*>&& tensors,
134 std::vector<std::string>&& inputs,
135 std::vector<std::string>&& outputs)
136{
Jerry Ge13c78a62022-10-04 20:32:39 -0700137 _name = std::move(name);
138 _region_name = std::move(region_name);
139 _operators = std::move(operators);
140 _tensors = std::move(tensors);
141 _inputs = std::move(inputs);
142 _outputs = std::move(outputs);
Kevin Cheng545a5082021-11-11 01:36:33 +0000143}
144
Eric Kunze2364dcd2021-04-26 11:06:57 -0700145TosaSerializationBasicBlock::~TosaSerializationBasicBlock()
146{
147 // deallocate all operators
148 for (auto op : GetOperators())
149 {
150 delete op; // ~TosaSerializationOperator()
151 }
152
153 // deallocate all tensors
154 for (auto ts : GetTensors())
155 {
156 delete ts; // ~TosaSerializationTensor()
157 }
158}
159
Jerry Ge13c78a62022-10-04 20:32:39 -0700160TosaSerializationRegion::TosaSerializationRegion(const std::string& name,
161 const std::vector<TosaSerializationBasicBlock*>& blocks)
162{
163 _name = name;
164 _blocks = blocks;
165}
166
167TosaSerializationRegion::TosaSerializationRegion(const std::string&& name,
168 const std::vector<TosaSerializationBasicBlock*>&& blocks)
169{
170 _name = std::move(name);
171 _blocks = std::move(blocks);
172}
173
174TosaSerializationRegion::~TosaSerializationRegion()
175{
176 // deallocate all blocks
177 for (auto block : GetBlocks())
178 {
179 delete block; // ~TosaSerializationBasicBlock()
180 }
181}
182
Eric Kunze2364dcd2021-04-26 11:06:57 -0700183TosaSerializationHandler::TosaSerializationHandler()
184{
185 _schemaLoaded = false;
Kevin Chenge6563f52021-10-20 12:12:02 -0700186 _version = TosaVersion(TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700187}
188
189TosaSerializationHandler::~TosaSerializationHandler()
190{
191 Clear(); // deallocate all basic blocks
192}
193
Kevin Chenga81a7a12021-11-10 14:07:34 -0800194TosaVersion TosaSerializationHandler::ParseTosaSchemaVersion(std::string schema)
195{
196 // Parse all 4 version fields in schema file
197 static const char* keywords[4] = { "major: int32 = ", "minor: int32 = ", "patch: int32 = ", "draft: bool = " };
198 string keyword_str[4];
199 size_t search_pos = 0;
200 size_t keyword_pos;
201 size_t semicolon_pos;
202 // parse integer field first
203 for (int32_t i = 0; i < 4; i++)
204 {
205 keyword_pos = schema.find(keywords[i], search_pos);
206 if (keyword_pos == std::string::npos)
207 {
208 printf("ERROR: can't find keyword \"%s\" in schema\n", keywords[i]);
209 assert(0);
210 }
211 semicolon_pos = schema.find(';', keyword_pos);
212 if (keyword_pos == std::string::npos)
213 {
214 printf("ERROR: can't find ';' in schema\n");
215 assert(0);
216 }
217 keyword_str[i] =
218 schema.substr(keyword_pos + strlen(keywords[i]), semicolon_pos - keyword_pos - strlen(keywords[i]));
219 search_pos = semicolon_pos;
220 }
221
222 int32_t schema_major = 0;
223 int32_t schema_minor = 0;
224 int32_t schema_patch = 0;
225 bool schema_draft = false;
226 try
227 {
228 schema_major = stoi(keyword_str[0]);
229 schema_minor = stoi(keyword_str[1]);
230 schema_patch = stoi(keyword_str[2]);
231 schema_draft = (keyword_str[3] == "true") ? true : false;
232 }
233 catch (std::invalid_argument& e)
234 {
235 printf("ERROR: fail at stoi(): %s\n", e.what());
236 assert(0);
237 }
238
239 TosaVersion schema_version(schema_major, schema_minor, schema_patch, schema_draft);
240
241 return schema_version;
242}
243
Eric Kunze2364dcd2021-04-26 11:06:57 -0700244tosa_err_t TosaSerializationHandler::LoadFileSchema(const char* schema_filename)
245{
246 std::string schema;
247 bool ok;
248
249 ok = flatbuffers::LoadFile(schema_filename, false, &schema);
250 if (!ok)
251 {
252 printf("Error loading schema file: %s\n", schema_filename);
253 return TOSA_FILE_ERROR;
254 }
255
256 ok = _parser.Parse(schema.c_str());
Kevin Chenga81a7a12021-11-10 14:07:34 -0800257
258 TosaVersion schema_version = ParseTosaSchemaVersion(schema);
259
260 TosaVersion::compat_t is_compat = schema_version.is_compatible(GetVersion());
261 switch (is_compat)
262 {
263 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
264 break;
265 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
266 printf("WARNING: Schema flatbuffer version %s is partially compatible with serializer version %s\n",
267 schema_version.to_string().c_str(), GetVersion().to_string().c_str());
268 break;
269 case TosaVersion::compat_t::NOT_COMPATIBLE:
270 printf("ERROR: Schema flatbuffer version %s is not compatible with serializer version %s\n",
271 schema_version.to_string().c_str(), GetVersion().to_string().c_str());
272 return TOSA_VERSION_MISMATCH;
273 }
274
Eric Kunze2364dcd2021-04-26 11:06:57 -0700275 if (!ok)
276 {
277 printf("Error parsing ISA schema file: %s\n", schema_filename);
278 return TOSA_FILE_ERROR;
279 }
280 _schemaLoaded = true;
281
282 return TOSA_OK;
283}
284
285tosa_err_t TosaSerializationHandler::LoadFileJson(const char* filename)
286{
287 std::string jsonfile;
288 bool ok;
289 tosa_err_t err;
290
291 if (!_schemaLoaded)
292 {
293 return TOSA_SCHEMA_MISSING;
294 }
295
296 ok = flatbuffers::LoadFile(filename, false, &jsonfile);
297 if (!ok)
298 {
299 printf("Error loading json file: %s\n", filename);
300 return TOSA_FILE_ERROR;
301 }
302
303 ok = _parser.Parse(jsonfile.c_str());
304 if (!ok)
305 {
306 printf("Error parsing json file: %s\n", filename);
307 return TOSA_FILE_ERROR;
308 }
309
310 uint8_t* buf = _parser.builder_.GetBufferPointer();
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::SaveFileJson(const char* filename)
322{
323 std::string jsongen;
324 tosa_err_t err;
325
326 if (!_schemaLoaded)
327 {
328 return TOSA_SCHEMA_MISSING;
329 }
330
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700331 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700332 if (err != TOSA_OK)
333 {
334 return err;
335 }
336
337 uint8_t* buf = _builder.GetBufferPointer();
338
339 if (!GenerateText(_parser, buf, &jsongen))
340 {
341 printf("Couldn't serialize parsed data to JSON!\n");
342 return TOSA_FILE_ERROR;
343 }
344
345 FILE* file = fopen(filename, "wb");
346
347 if (!file)
348 {
349 printf("Couldn't open output file: %s\n", filename);
350 return TOSA_FILE_ERROR;
351 }
352
353 if (fwrite(jsongen.c_str(), sizeof(char), jsongen.size(), file) != jsongen.size())
354 {
355 printf("Error writing to json output file: %s\n", filename);
356 fclose(file);
357 return TOSA_FILE_ERROR;
358 }
359
360 if (file)
361 fclose(file);
362
363 return TOSA_OK;
364}
365
366tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const char* filename)
367{
368 std::string read_buffer;
369 tosa_err_t err;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800370 const uint8_t* buf;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700371 bool ok;
372
373 ok = flatbuffers::LoadFile(filename, false, &read_buffer);
374 if (!ok)
375 {
376 printf("Error loading flatbuffer file: %s\n", filename);
377 return TOSA_FILE_ERROR;
378 }
379
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800380 buf = reinterpret_cast<const uint8_t*>(read_buffer.data());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700381
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700382 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700383 if (err != TOSA_OK)
384 {
385 return err;
386 }
387
388 return TOSA_OK;
389}
390
Aaron DeBattista8b3903a2021-11-18 16:38:11 +0000391tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const void* input, int in_size)
392{
393 tosa_err_t err;
394
395 const uint8_t* buf = (const uint8_t*)input;
396 err = Deserialize(buf);
397 if (err != TOSA_OK)
398 {
399 return err;
400 }
401
402 return TOSA_OK;
403}
404
Eric Kunze2364dcd2021-04-26 11:06:57 -0700405tosa_err_t TosaSerializationHandler::SaveFileTosaFlatbuffer(const char* filename)
406{
407 tosa_err_t err;
408
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700409 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700410 if (err != TOSA_OK)
411 {
412 return err;
413 }
414
415 uint8_t* buf = _builder.GetBufferPointer();
416
417 bool ok = flatbuffers::SaveFile(filename, (const char*)buf, _builder.GetSize(), false);
418 if (!ok)
419 {
420 printf("Error saving floatbuffer file: %s\n", filename);
421 return TOSA_FILE_ERROR;
422 }
423
424 return TOSA_OK;
425}
426
427tosa_err_t TosaSerializationHandler::Clear()
428{
429 // deallocate all basic blocks
Jerry Ge13c78a62022-10-04 20:32:39 -0700430 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700431 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700432 delete region;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700433 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700434 _regions.clear();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700435
436 return TOSA_OK;
437}
438
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700439tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700440{
Eric Kunzee6596402022-06-09 21:27:36 +0000441 if (!TosaGraphBufferHasIdentifier(buf))
442 {
443 printf("WARNING: TOSA file does not have TOSA file identifier\n");
444 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700445 auto fb_tosa_graph = GetTosaGraph(buf);
446 auto fb_tosa_version = fb_tosa_graph->version();
Jerry Ge13c78a62022-10-04 20:32:39 -0700447 auto fb_tosa_regions = fb_tosa_graph->regions();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700448
449 std::vector<std::string> operator_inputs_container;
450 std::vector<std::string> operator_outputs_container;
451
Jerry Ge13c78a62022-10-04 20:32:39 -0700452 std::vector<TosaSerializationBasicBlock*> region_blocks_container;
453
Eric Kunze2364dcd2021-04-26 11:06:57 -0700454 std::vector<TosaSerializationOperator*> block_operators_container;
455 std::vector<TosaSerializationTensor*> block_tensors_container;
456 std::vector<std::string> block_inputs_container;
457 std::vector<std::string> block_outputs_container;
458
459 TosaAttributeBase* typed_attribute = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700460 TosaSerializationOperator* new_operator = NULL;
461 TosaSerializationBasicBlock* new_block = NULL;
462 TosaSerializationTensor* new_tensor = NULL;
Jerry Ge13c78a62022-10-04 20:32:39 -0700463 TosaSerializationRegion* new_region = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700464
465 // erase container
466 Clear();
467
Kevin Chenge6563f52021-10-20 12:12:02 -0700468 TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(),
469 fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700470
Kevin Chenge6563f52021-10-20 12:12:02 -0700471 TosaVersion::compat_t is_compat = read_version.is_compatible(GetVersion());
472 switch (is_compat)
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700473 {
Kevin Chenge6563f52021-10-20 12:12:02 -0700474 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
475 break;
476 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
477 printf("WARNING: Read flatbuffer version %s is partially compatible with serializer version %s\n",
478 read_version.to_string().c_str(), GetVersion().to_string().c_str());
479 break;
480 case TosaVersion::compat_t::NOT_COMPATIBLE:
481 printf("ERROR: Read flatbuffer version %s is not compatible with serializer version %s\n",
482 read_version.to_string().c_str(), GetVersion().to_string().c_str());
483 return TOSA_VERSION_MISMATCH;
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700484 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700485
Jerry Ge13c78a62022-10-04 20:32:39 -0700486 for (size_t i = 0; i < fb_tosa_regions->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700487 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700488 auto curr_region = fb_tosa_regions->Get(i);
489 auto region_name = curr_region->name()->str();
490 auto fb_tosa_blocks = curr_region->blocks();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700491
Jerry Ge13c78a62022-10-04 20:32:39 -0700492 new_region = new TosaSerializationRegion(curr_region->name()->str(), region_blocks_container);
493 this->GetRegions().push_back(new_region);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700494
Jerry Ge13c78a62022-10-04 20:32:39 -0700495 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700496 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700497 auto curr_block = fb_tosa_blocks->Get(i);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700498
Jerry Ge13c78a62022-10-04 20:32:39 -0700499 auto block_name = curr_block->name()->str();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700500
Jerry Ge13c78a62022-10-04 20:32:39 -0700501 auto fb_tosa_operators = curr_block->operators();
502 block_operators_container.clear();
503 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700504 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700505 auto curr_operator = fb_tosa_operators->Get(j);
506
507 auto operator_op = curr_operator->op();
508 auto attribute_type = curr_operator->attribute_type();
509 auto attribute = curr_operator->attribute();
510
511 // input tensors
512 auto operator_inputs = curr_operator->inputs();
513 operator_inputs_container.clear();
514 if (operator_inputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700515 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700516 for (size_t k = 0; k < operator_inputs->size(); k++)
517 {
518 auto curr_input = operator_inputs->Get(k);
519 operator_inputs_container.push_back(curr_input->str());
520 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700521 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700522
Jerry Ge13c78a62022-10-04 20:32:39 -0700523 // output tensors
524 auto operator_outputs = curr_operator->outputs();
525 operator_outputs_container.clear();
526 if (operator_outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700527 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700528 for (size_t k = 0; k < operator_outputs->size(); k++)
529 {
530 auto curr_output = operator_outputs->Get(k);
531 operator_outputs_container.push_back(curr_output->str());
532 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700533 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700534
Jerry Ge13c78a62022-10-04 20:32:39 -0700535 switch (attribute_type)
536 {
537 case Attribute_NONE:
538 typed_attribute = new TosaNoneAttribute();
539 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700540#define DEF_ATTRIBUTE(NAME, ...) \
541 case Attribute_##NAME##Attribute: \
542 typed_attribute = new Tosa##NAME##Attribute(attribute); \
543 break;
544#include "attribute.def"
545#undef DEF_ATTRIBUTE
Jerry Ge13c78a62022-10-04 20:32:39 -0700546 default:
547 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
548 EnumNamesAttribute()[attribute_type]);
549 return TOSA_INTERNAL_ERROR;
550 }
551
552 new_operator = new TosaSerializationOperator(operator_op, attribute_type, typed_attribute,
553 operator_inputs_container, operator_outputs_container);
554 if (new_operator)
555 {
556 block_operators_container.push_back(new_operator);
557 }
558 else
559 {
560 return TOSA_MEMORY_ERROR;
561 }
562
563 if (typed_attribute)
564 delete typed_attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700565 }
566
Jerry Ge13c78a62022-10-04 20:32:39 -0700567 auto block_inputs = curr_block->inputs();
568 auto block_outputs = curr_block->outputs();
569
570 block_inputs_container.clear();
571 block_outputs_container.clear();
572
573 for (size_t j = 0; j < block_inputs->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700574 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700575 auto curr_block_input = block_inputs->Get(j);
576 block_inputs_container.push_back(curr_block_input->str());
577 }
578 for (size_t j = 0; j < block_outputs->size(); j++)
579 {
580 auto curr_block_output = block_outputs->Get(j);
581 block_outputs_container.push_back(curr_block_output->str());
582 }
583
584 auto fb_tosa_tensors = curr_block->tensors();
585 block_tensors_container.clear();
586 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
587 {
588 auto curr_tensor = fb_tosa_tensors->Get(j);
589
590 auto tensor_name = curr_tensor->name();
591 auto tensor_shape = curr_tensor->shape();
592 auto tensor_type = curr_tensor->type();
593 auto tensor_data = curr_tensor->data();
594
595 new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data);
596 if (new_tensor)
597 {
598 block_tensors_container.push_back(new_tensor);
599 }
600 else
601 {
602 return TOSA_MEMORY_ERROR;
603 }
604 }
605 new_block = new TosaSerializationBasicBlock(block_name, region_name, block_operators_container,
606 block_tensors_container, block_inputs_container,
607 block_outputs_container);
608 if (new_block)
609 {
610 this->GetRegions()[0]->GetBlocks().push_back(new_block);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700611 }
612 else
613 {
614 return TOSA_MEMORY_ERROR;
615 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700616 } // end block for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700617 }
618
619 return TOSA_OK;
620}
621
James Ward80905bb2023-01-25 15:51:27 +0000622std::vector<uint8_t> float_to_u8_helper(float f_in)
James Wardc15f7d52022-12-07 15:38:01 +0000623{
James Ward80905bb2023-01-25 15:51:27 +0000624 // Push back a single float value to the buffer with *NO PADDING*
625 // Therefore ConvertF32toU8 function not used
James Wardc15f7d52022-12-07 15:38:01 +0000626 std::vector<uint8_t> u8_out;
James Ward80905bb2023-01-25 15:51:27 +0000627 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&f_in);
628 u8_out.push_back(*val_u32 & 0xFF);
629 u8_out.push_back((*val_u32 >> 8) & 0xFF);
630 u8_out.push_back((*val_u32 >> 16) & 0xFF);
631 u8_out.push_back((*val_u32 >> 24) & 0xFF);
James Wardc15f7d52022-12-07 15:38:01 +0000632 return u8_out;
633}
634
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700635tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700636{
Jerry Ge13c78a62022-10-04 20:32:39 -0700637 // regions
638 std::vector<flatbuffers::Offset<TosaRegion>> fboffset_regions;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700639
Jerry Ge13c78a62022-10-04 20:32:39 -0700640 // blocks
641 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700642 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
643 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
644 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
645 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
646
Jerry Ge13c78a62022-10-04 20:32:39 -0700647 // operators
Eric Kunze2364dcd2021-04-26 11:06:57 -0700648 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
649 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
650
651 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
Jerry Ge13c78a62022-10-04 20:32:39 -0700652 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700653 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700654 for (auto block : region->GetBlocks())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700655 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700656 fboffset_block_operators.clear();
657 fboffset_block_tensors.clear();
658 fboffset_block_inputs.clear();
659 fboffset_block_outputs.clear();
660 auto block_name = _builder.CreateString(block->GetName().c_str());
661 for (auto tensor_str : block->GetInputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700662 {
663 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700664 fboffset_block_inputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700665 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700666 for (auto tensor_str : block->GetOutputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700667 {
668 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700669 fboffset_block_outputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700670 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700671 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
672 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
673 for (auto op : block->GetOperators())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700674 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700675 fboffset_operator_inputs.clear();
676 fboffset_operator_outputs.clear();
677 auto operator_op = op->GetOp();
678 auto attribute_type = op->GetAttributeType();
679 for (auto tensor_str : op->GetInputTensorNames())
680 {
681 auto tensor_name = _builder.CreateString(tensor_str.c_str());
682 fboffset_operator_inputs.push_back(tensor_name);
683 }
684 for (auto tensor_str : op->GetOutputTensorNames())
685 {
686 auto tensor_name = _builder.CreateString(tensor_str.c_str());
687 fboffset_operator_outputs.push_back(tensor_name);
688 }
689 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
690 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
691 flatbuffers::Offset<void> fb_attribute;
692 switch (attribute_type)
693 {
694 case Attribute_NONE:
695 fb_attribute = 0;
696 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700697#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 +0000698#define DEF_ARGS_S_FP_as_U8(NAME, V) \
James Ward80905bb2023-01-25 15:51:27 +0000699 , _builder.CreateVector<uint8_t>(float_to_u8_helper(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()))
Eric Kunze2364dcd2021-04-26 11:06:57 -0700700#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700701#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Wardc15f7d52022-12-07 15:38:01 +0000702#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_FP_as_U8(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700703#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
704#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Ward485a11d2022-08-05 13:48:37 +0100705#define DEF_ARGS_S_DType(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700706#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700707#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
708#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700709#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
710#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
711#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
712 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
713#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
714 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
715#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
716 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
717 DEF_ARGS_##F4(NAME, T4, V4)
718#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
719 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
720 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5)
721#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) \
722 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
723 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
724#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
725 case Attribute_##NAME##Attribute: \
726 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
727 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700728#include "attribute.def"
729#undef DEF_ATTRIBUTE
730#undef DEF_ARGS_1
731#undef DEF_ARGS_2
732#undef DEF_ARGS_3
733#undef DEF_ARGS_4
734#undef DEF_ARGS_5
735#undef DEF_ARGS_6
736#undef DEF_ARGS_7
737#undef DEF_ARGS_S
738#undef DEF_ARGS_V
739#undef DEF_ARGS_S_int32_t
740#undef DEF_ARGS_S_float
741#undef DEF_ARGS_S_bool
742#undef DEF_ARGS_S_ResizeMode
James Ward485a11d2022-08-05 13:48:37 +0100743#undef DEF_ARGS_S_DType
Eric Kunze2364dcd2021-04-26 11:06:57 -0700744#undef DEF_ARGS_S_string
745#undef DEF_ARGS_S_STR
746#undef DEF_ARGS_S_DEFAULT
Jerry Ge13c78a62022-10-04 20:32:39 -0700747 default:
748 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
749 EnumNamesAttribute()[attribute_type]);
750 return TOSA_INTERNAL_ERROR;
751 }
752 auto fboffset_operator = CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute,
753 fb_operator_inputs, fb_operator_outputs);
754 fboffset_block_operators.push_back(fboffset_operator);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700755 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700756 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
757 for (auto tensor : block->GetTensors())
758 {
759 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
760 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
761 auto tensor_dtype = tensor->GetDtype();
762 auto tensor_data = _builder.CreateVector(tensor->GetData());
763 auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data);
764 fboffset_block_tensors.push_back(fboffset_tensor);
765 }
766 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
767 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
768 fb_block_inputs, fb_block_outputs);
769 fboffset_blocks.push_back(fboffset_block);
770 } // end block for_loop
771 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700772
Jerry Ge13c78a62022-10-04 20:32:39 -0700773 auto region_name = _builder.CreateString(region->GetName().c_str());
774 auto fboffset_region = CreateTosaRegion(_builder, region_name, fb_blocks);
775 fboffset_regions.push_back(fboffset_region);
776 } // end region for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700777
Jerry Ge13c78a62022-10-04 20:32:39 -0700778 auto fb_regions = _builder.CreateVector(fboffset_regions);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700779
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700780 auto fb_version =
781 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Jerry Ge13c78a62022-10-04 20:32:39 -0700782 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_regions);
Eric Kunzee6596402022-06-09 21:27:36 +0000783 _builder.Finish(fb_graph, TosaGraphIdentifier());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700784
785 return TOSA_OK;
786}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700787
788void zero_pad(std::vector<uint8_t>& buf)
789{
790 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
791 {
792 buf.push_back(0);
793 }
794}
795
James Ward485a11d2022-08-05 13:48:37 +0100796tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
797{
798 // Note: Converts fp32->fp16 before converting to uint8_t
799 out.clear();
800 for (auto val : in)
801 {
802 half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val);
803 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16);
804 out.push_back(*val_u16 & 0xFF);
805 out.push_back((*val_u16 >> 8) & 0xFF);
806 }
807 zero_pad(out);
808 return TOSA_OK;
809}
810
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700811tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
812{
813 out.clear();
814 for (auto val : in)
815 {
816 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
817 out.push_back(*val_u32 & 0xFF);
818 out.push_back((*val_u32 >> 8) & 0xFF);
819 out.push_back((*val_u32 >> 16) & 0xFF);
820 out.push_back((*val_u32 >> 24) & 0xFF);
821 }
822 zero_pad(out);
823 return TOSA_OK;
824}
825
826tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
827{
828 out.clear();
829 for (auto val : in)
830 {
831 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
832 out.push_back(*val_u64 & 0xFF);
833 out.push_back((*val_u64 >> 8) & 0xFF);
834 out.push_back((*val_u64 >> 16) & 0xFF);
835 out.push_back((*val_u64 >> 24) & 0xFF);
836 out.push_back((*val_u64 >> 32) & 0xFF);
837 out.push_back((*val_u64 >> 40) & 0xFF);
838 }
839 zero_pad(out);
840 return TOSA_OK;
841}
842
843tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
844{
845 out.clear();
846 for (auto val : in)
847 {
848 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
849 out.push_back(*val_u32 & 0xFF);
850 out.push_back((*val_u32 >> 8) & 0xFF);
851 out.push_back((*val_u32 >> 16) & 0xFF);
852 out.push_back((*val_u32 >> 24) & 0xFF);
853 }
854 zero_pad(out);
855 return TOSA_OK;
856}
857
858tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
859{
860 out.clear();
861 for (auto val : in)
862 {
863 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
864 out.push_back(*val_u16 & 0xFF);
865 out.push_back((*val_u16 >> 8) & 0xFF);
866 }
867 zero_pad(out);
868 return TOSA_OK;
869}
870
871tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
872{
873 out.clear();
874 for (auto val : in)
875 {
876 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
877 out.push_back(*val_u8);
878 }
879 zero_pad(out);
880 return TOSA_OK;
881}
882
Kevin Cheng3ce56342021-07-28 13:42:29 -0700883// Two int4 values are packed into one byte out.
884// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
885// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
886tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
887{
888 out.clear();
889 uint32_t in_size = in.size();
890 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800891 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700892 {
893 int8_t val_0 = in[2 * i];
894 int8_t val_1 = 0;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800895 if (2u * i + 1u < in_size)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700896 {
897 val_1 = in[2 * i + 1];
898 }
899 // In TOSA spec, int4 ranges [-7, 7]
900 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
901 {
902 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
903 val_0, val_1);
904 return TOSA_USER_ERROR;
905 }
906 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
907 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
908 out.push_back(val_u8);
909 }
910 zero_pad(out);
911 return TOSA_OK;
912}
913
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700914tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
915{
916 out.clear();
917 for (auto val : in)
918 {
Eric Kunze4417b422022-06-20 07:27:42 -0700919 uint8_t val_u8 = val;
920 out.push_back(val_u8);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700921 }
922 zero_pad(out);
923 return TOSA_OK;
924}
925
926tosa_err_t
James Ward485a11d2022-08-05 13:48:37 +0100927 TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
928{
929 // Note: fp16 values returned in fp32 type
930 out.clear();
931 if (in.size() < out_size * sizeof(int16_t))
932 {
933 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
934 out_size * sizeof(int16_t));
935 return TOSA_USER_ERROR;
936 }
937
938 for (uint32_t i = 0; i < out_size; i++)
939 {
940 uint16_t f16_byte0 = in[i * sizeof(int16_t)];
941 uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1];
942 uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8);
943
944 // Reinterpret u16 byte as fp16 then convert to fp32
945 half_float::half val_f16 = *(half_float::half*)&val_u16;
946 float val_fp32 = half_float::half_cast<float, half_float::half>(val_f16);
947 out.push_back(val_fp32);
948 }
949 return TOSA_OK;
950}
951
952tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700953 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
954{
955 out.clear();
956 if (in.size() < out_size * sizeof(float))
957 {
958 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
959 out_size * sizeof(float));
960 return TOSA_USER_ERROR;
961 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800962 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700963 {
964 uint32_t byte0 = in[i * sizeof(float)];
965 uint32_t byte1 = in[i * sizeof(float) + 1];
966 uint32_t byte2 = in[i * sizeof(float) + 2];
967 uint32_t byte3 = in[i * sizeof(float) + 3];
968 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
969 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
970 out.push_back(*val_fp32);
971 }
972 return TOSA_OK;
973}
974
975tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
976 uint32_t out_size,
977 std::vector<int64_t>& out)
978{
979 out.clear();
980 if (in.size() < out_size * 6 /* sizeof(int48) */)
981 {
982 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
983 out_size * 6);
984 return TOSA_USER_ERROR;
985 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800986 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700987 {
988 uint64_t byte0 = in[i * 6];
989 uint64_t byte1 = in[i * 6 + 1];
990 uint64_t byte2 = in[i * 6 + 2];
991 uint64_t byte3 = in[i * 6 + 3];
992 uint64_t byte4 = in[i * 6 + 4];
993 uint64_t byte5 = in[i * 6 + 5];
994 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
995 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
996 if (sign)
997 {
998 uint64_t sext_mask = (0xFFFFUL << 48);
999 val_u64 |= sext_mask;
1000 }
1001 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
1002 out.push_back(*val_i64);
1003 }
1004 return TOSA_OK;
1005}
1006
1007tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
1008 uint32_t out_size,
1009 std::vector<int32_t>& out)
1010{
1011 out.clear();
1012 if (in.size() < out_size * sizeof(int32_t))
1013 {
1014 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1015 out_size * sizeof(int32_t));
1016 return TOSA_USER_ERROR;
1017 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001018 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001019 {
1020 uint32_t byte0 = in[i * sizeof(int32_t)];
1021 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
1022 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
1023 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
1024 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
1025 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
1026 out.push_back(*val_i32);
1027 }
1028 return TOSA_OK;
1029}
1030
1031tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
1032 uint32_t out_size,
1033 std::vector<int16_t>& out)
1034{
1035 out.clear();
1036 if (in.size() < out_size * sizeof(int16_t))
1037 {
1038 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1039 out_size * sizeof(int16_t));
1040 return TOSA_USER_ERROR;
1041 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001042 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001043 {
1044 uint16_t byte0 = in[i * sizeof(int16_t)];
1045 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
1046 uint16_t val_u16 = byte0 + (byte1 << 8);
1047 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
1048 out.push_back(*val_i16);
1049 }
1050 return TOSA_OK;
1051}
1052
1053tosa_err_t
1054 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1055{
1056 out.clear();
1057 if (in.size() < out_size * sizeof(int8_t))
1058 {
1059 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -07001060 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001061 return TOSA_USER_ERROR;
1062 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001063 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001064 {
1065 uint8_t val_u8 = in[i];
1066 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
1067 out.push_back(*val_i8);
1068 }
1069 return TOSA_OK;
1070}
1071
1072tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -07001073 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1074{
1075 out.clear();
1076 if (out_size > in.size() * 2)
1077 {
1078 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
1079 out_size, in.size());
1080 return TOSA_USER_ERROR;
1081 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001082 for (size_t i = 0; i < in.size(); i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -07001083 {
1084 uint8_t val_u8 = in[i];
1085 uint8_t val_0_u4 = val_u8 & 0xF;
1086 uint8_t val_1_u4 = val_u8 >> 4;
1087 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1088 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1089 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1090 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1091 // In TOSA spec, int4 ranges [-7, 7]
1092 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1093 {
1094 printf(
1095 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1096 val_0, val_1);
1097 return TOSA_USER_ERROR;
1098 }
1099 out.push_back(val_0);
1100 if (2 * i + 1 < out_size)
1101 out.push_back(val_1);
1102 }
1103 return TOSA_OK;
1104}
1105
1106tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001107 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1108{
1109 out.clear();
1110 if (in.size() < out_size * sizeof(bool))
1111 {
1112 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1113 in.size(), out_size * sizeof(bool));
1114 return TOSA_USER_ERROR;
1115 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001116 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001117 {
1118 uint8_t val_u8 = in[i];
1119 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1120 out.push_back(*val_bool);
1121 }
1122 return TOSA_OK;
1123}