blob: d7822139e8f82c691fd7131a48c97f855f5ab6fb [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
Eric Kunze2364dcd2021-04-26 11:06:57 -0700449 TosaAttributeBase* typed_attribute = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700450 TosaSerializationOperator* new_operator = NULL;
451 TosaSerializationBasicBlock* new_block = NULL;
452 TosaSerializationTensor* new_tensor = NULL;
Jerry Ge13c78a62022-10-04 20:32:39 -0700453 TosaSerializationRegion* new_region = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700454
455 // erase container
456 Clear();
457
Kevin Chenge6563f52021-10-20 12:12:02 -0700458 TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(),
459 fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700460
Kevin Chenge6563f52021-10-20 12:12:02 -0700461 TosaVersion::compat_t is_compat = read_version.is_compatible(GetVersion());
462 switch (is_compat)
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700463 {
Kevin Chenge6563f52021-10-20 12:12:02 -0700464 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
465 break;
466 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
467 printf("WARNING: Read flatbuffer version %s is partially compatible with serializer version %s\n",
468 read_version.to_string().c_str(), GetVersion().to_string().c_str());
469 break;
470 case TosaVersion::compat_t::NOT_COMPATIBLE:
471 printf("ERROR: Read flatbuffer version %s is not compatible with serializer version %s\n",
472 read_version.to_string().c_str(), GetVersion().to_string().c_str());
473 return TOSA_VERSION_MISMATCH;
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700474 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700475
Jerry Ge13c78a62022-10-04 20:32:39 -0700476 for (size_t i = 0; i < fb_tosa_regions->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700477 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700478 auto curr_region = fb_tosa_regions->Get(i);
479 auto region_name = curr_region->name()->str();
480 auto fb_tosa_blocks = curr_region->blocks();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700481
Tai Lycfcb20d2023-03-13 21:04:11 +0000482 new_region = new TosaSerializationRegion(curr_region->name()->str(), {});
Jerry Ge13c78a62022-10-04 20:32:39 -0700483 this->GetRegions().push_back(new_region);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700484
Jerry Ge13c78a62022-10-04 20:32:39 -0700485 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700486 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000487 std::vector<TosaSerializationOperator*> block_operators_container;
488 std::vector<TosaSerializationTensor*> block_tensors_container;
489 std::vector<std::string> block_inputs_container;
490 std::vector<std::string> block_outputs_container;
491
Jerry Ge13c78a62022-10-04 20:32:39 -0700492 auto curr_block = fb_tosa_blocks->Get(i);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700493
Jerry Ge13c78a62022-10-04 20:32:39 -0700494 auto block_name = curr_block->name()->str();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700495
Jerry Ge13c78a62022-10-04 20:32:39 -0700496 auto fb_tosa_operators = curr_block->operators();
Jerry Ge13c78a62022-10-04 20:32:39 -0700497 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700498 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700499 auto curr_operator = fb_tosa_operators->Get(j);
500
501 auto operator_op = curr_operator->op();
502 auto attribute_type = curr_operator->attribute_type();
503 auto attribute = curr_operator->attribute();
504
Tai Lycfcb20d2023-03-13 21:04:11 +0000505 std::vector<std::string> operator_inputs_container;
506 std::vector<std::string> operator_outputs_container;
507
Jerry Ge13c78a62022-10-04 20:32:39 -0700508 // input tensors
509 auto operator_inputs = curr_operator->inputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700510 if (operator_inputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700511 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700512 for (size_t k = 0; k < operator_inputs->size(); k++)
513 {
514 auto curr_input = operator_inputs->Get(k);
515 operator_inputs_container.push_back(curr_input->str());
516 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700517 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700518
Jerry Ge13c78a62022-10-04 20:32:39 -0700519 // output tensors
520 auto operator_outputs = curr_operator->outputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700521 if (operator_outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700522 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700523 for (size_t k = 0; k < operator_outputs->size(); k++)
524 {
525 auto curr_output = operator_outputs->Get(k);
526 operator_outputs_container.push_back(curr_output->str());
527 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700528 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700529
Jerry Ge13c78a62022-10-04 20:32:39 -0700530 switch (attribute_type)
531 {
532 case Attribute_NONE:
533 typed_attribute = new TosaNoneAttribute();
534 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700535#define DEF_ATTRIBUTE(NAME, ...) \
536 case Attribute_##NAME##Attribute: \
537 typed_attribute = new Tosa##NAME##Attribute(attribute); \
538 break;
539#include "attribute.def"
540#undef DEF_ATTRIBUTE
Jerry Ge13c78a62022-10-04 20:32:39 -0700541 default:
542 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
543 EnumNamesAttribute()[attribute_type]);
544 return TOSA_INTERNAL_ERROR;
545 }
546
547 new_operator = new TosaSerializationOperator(operator_op, attribute_type, typed_attribute,
548 operator_inputs_container, operator_outputs_container);
549 if (new_operator)
550 {
551 block_operators_container.push_back(new_operator);
552 }
553 else
554 {
555 return TOSA_MEMORY_ERROR;
556 }
557
558 if (typed_attribute)
559 delete typed_attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700560 }
561
Jerry Ge13c78a62022-10-04 20:32:39 -0700562 auto block_inputs = curr_block->inputs();
563 auto block_outputs = curr_block->outputs();
564
Jerry Ge13c78a62022-10-04 20:32:39 -0700565 for (size_t j = 0; j < block_inputs->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700566 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700567 auto curr_block_input = block_inputs->Get(j);
568 block_inputs_container.push_back(curr_block_input->str());
569 }
570 for (size_t j = 0; j < block_outputs->size(); j++)
571 {
572 auto curr_block_output = block_outputs->Get(j);
573 block_outputs_container.push_back(curr_block_output->str());
574 }
575
576 auto fb_tosa_tensors = curr_block->tensors();
Jerry Ge13c78a62022-10-04 20:32:39 -0700577 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
578 {
579 auto curr_tensor = fb_tosa_tensors->Get(j);
580
581 auto tensor_name = curr_tensor->name();
582 auto tensor_shape = curr_tensor->shape();
583 auto tensor_type = curr_tensor->type();
584 auto tensor_data = curr_tensor->data();
585
586 new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data);
587 if (new_tensor)
588 {
589 block_tensors_container.push_back(new_tensor);
590 }
591 else
592 {
593 return TOSA_MEMORY_ERROR;
594 }
595 }
596 new_block = new TosaSerializationBasicBlock(block_name, region_name, block_operators_container,
597 block_tensors_container, block_inputs_container,
598 block_outputs_container);
599 if (new_block)
600 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000601 new_region->GetBlocks().push_back(new_block);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700602 }
603 else
604 {
605 return TOSA_MEMORY_ERROR;
606 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700607 } // end block for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700608 }
609
610 return TOSA_OK;
611}
612
James Ward80905bb2023-01-25 15:51:27 +0000613std::vector<uint8_t> float_to_u8_helper(float f_in)
James Wardc15f7d52022-12-07 15:38:01 +0000614{
James Ward80905bb2023-01-25 15:51:27 +0000615 // Push back a single float value to the buffer with *NO PADDING*
616 // Therefore ConvertF32toU8 function not used
James Wardc15f7d52022-12-07 15:38:01 +0000617 std::vector<uint8_t> u8_out;
James Ward80905bb2023-01-25 15:51:27 +0000618 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&f_in);
619 u8_out.push_back(*val_u32 & 0xFF);
620 u8_out.push_back((*val_u32 >> 8) & 0xFF);
621 u8_out.push_back((*val_u32 >> 16) & 0xFF);
622 u8_out.push_back((*val_u32 >> 24) & 0xFF);
James Wardc15f7d52022-12-07 15:38:01 +0000623 return u8_out;
624}
625
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700626tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700627{
Jerry Ge13c78a62022-10-04 20:32:39 -0700628 // regions
629 std::vector<flatbuffers::Offset<TosaRegion>> fboffset_regions;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700630
Eric Kunze2364dcd2021-04-26 11:06:57 -0700631 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
Jerry Ge13c78a62022-10-04 20:32:39 -0700632 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700633 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000634 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
Jerry Ge13c78a62022-10-04 20:32:39 -0700635 for (auto block : region->GetBlocks())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700636 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000637 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
638 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
639 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
640 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700641 auto block_name = _builder.CreateString(block->GetName().c_str());
642 for (auto tensor_str : block->GetInputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700643 {
644 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700645 fboffset_block_inputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700646 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700647 for (auto tensor_str : block->GetOutputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700648 {
649 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700650 fboffset_block_outputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700651 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700652 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
653 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
654 for (auto op : block->GetOperators())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700655 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000656 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
657 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700658 auto operator_op = op->GetOp();
659 auto attribute_type = op->GetAttributeType();
660 for (auto tensor_str : op->GetInputTensorNames())
661 {
662 auto tensor_name = _builder.CreateString(tensor_str.c_str());
663 fboffset_operator_inputs.push_back(tensor_name);
664 }
665 for (auto tensor_str : op->GetOutputTensorNames())
666 {
667 auto tensor_name = _builder.CreateString(tensor_str.c_str());
668 fboffset_operator_outputs.push_back(tensor_name);
669 }
670 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
671 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
672 flatbuffers::Offset<void> fb_attribute;
673 switch (attribute_type)
674 {
675 case Attribute_NONE:
676 fb_attribute = 0;
677 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700678#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 +0000679#define DEF_ARGS_S_FP_as_U8(NAME, V) \
James Ward80905bb2023-01-25 15:51:27 +0000680 , _builder.CreateVector<uint8_t>(float_to_u8_helper(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()))
Eric Kunze2364dcd2021-04-26 11:06:57 -0700681#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700682#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Wardc15f7d52022-12-07 15:38:01 +0000683#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_FP_as_U8(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700684#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
685#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Ward485a11d2022-08-05 13:48:37 +0100686#define DEF_ARGS_S_DType(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700687#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700688#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
689#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700690#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
691#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
692#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
693 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
694#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
695 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
696#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
697 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
698 DEF_ARGS_##F4(NAME, T4, V4)
699#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
700 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
701 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5)
702#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) \
703 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
704 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
705#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
706 case Attribute_##NAME##Attribute: \
707 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
708 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700709#include "attribute.def"
710#undef DEF_ATTRIBUTE
711#undef DEF_ARGS_1
712#undef DEF_ARGS_2
713#undef DEF_ARGS_3
714#undef DEF_ARGS_4
715#undef DEF_ARGS_5
716#undef DEF_ARGS_6
717#undef DEF_ARGS_7
718#undef DEF_ARGS_S
719#undef DEF_ARGS_V
720#undef DEF_ARGS_S_int32_t
721#undef DEF_ARGS_S_float
722#undef DEF_ARGS_S_bool
723#undef DEF_ARGS_S_ResizeMode
James Ward485a11d2022-08-05 13:48:37 +0100724#undef DEF_ARGS_S_DType
Eric Kunze2364dcd2021-04-26 11:06:57 -0700725#undef DEF_ARGS_S_string
726#undef DEF_ARGS_S_STR
727#undef DEF_ARGS_S_DEFAULT
Jerry Ge13c78a62022-10-04 20:32:39 -0700728 default:
729 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
730 EnumNamesAttribute()[attribute_type]);
731 return TOSA_INTERNAL_ERROR;
732 }
733 auto fboffset_operator = CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute,
734 fb_operator_inputs, fb_operator_outputs);
735 fboffset_block_operators.push_back(fboffset_operator);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700736 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700737 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
738 for (auto tensor : block->GetTensors())
739 {
740 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
741 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
742 auto tensor_dtype = tensor->GetDtype();
743 auto tensor_data = _builder.CreateVector(tensor->GetData());
744 auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data);
745 fboffset_block_tensors.push_back(fboffset_tensor);
746 }
747 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
748 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
749 fb_block_inputs, fb_block_outputs);
750 fboffset_blocks.push_back(fboffset_block);
751 } // end block for_loop
752 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700753
Jerry Ge13c78a62022-10-04 20:32:39 -0700754 auto region_name = _builder.CreateString(region->GetName().c_str());
755 auto fboffset_region = CreateTosaRegion(_builder, region_name, fb_blocks);
756 fboffset_regions.push_back(fboffset_region);
757 } // end region for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700758
Jerry Ge13c78a62022-10-04 20:32:39 -0700759 auto fb_regions = _builder.CreateVector(fboffset_regions);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700760
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700761 auto fb_version =
762 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Jerry Ge13c78a62022-10-04 20:32:39 -0700763 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_regions);
Eric Kunzee6596402022-06-09 21:27:36 +0000764 _builder.Finish(fb_graph, TosaGraphIdentifier());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700765
766 return TOSA_OK;
767}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700768
769void zero_pad(std::vector<uint8_t>& buf)
770{
771 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
772 {
773 buf.push_back(0);
774 }
775}
776
James Ward485a11d2022-08-05 13:48:37 +0100777tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
778{
779 // Note: Converts fp32->fp16 before converting to uint8_t
780 out.clear();
781 for (auto val : in)
782 {
783 half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val);
784 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16);
785 out.push_back(*val_u16 & 0xFF);
786 out.push_back((*val_u16 >> 8) & 0xFF);
787 }
788 zero_pad(out);
789 return TOSA_OK;
790}
791
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700792tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
793{
794 out.clear();
795 for (auto val : in)
796 {
797 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
798 out.push_back(*val_u32 & 0xFF);
799 out.push_back((*val_u32 >> 8) & 0xFF);
800 out.push_back((*val_u32 >> 16) & 0xFF);
801 out.push_back((*val_u32 >> 24) & 0xFF);
802 }
803 zero_pad(out);
804 return TOSA_OK;
805}
806
807tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
808{
809 out.clear();
810 for (auto val : in)
811 {
812 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
813 out.push_back(*val_u64 & 0xFF);
814 out.push_back((*val_u64 >> 8) & 0xFF);
815 out.push_back((*val_u64 >> 16) & 0xFF);
816 out.push_back((*val_u64 >> 24) & 0xFF);
817 out.push_back((*val_u64 >> 32) & 0xFF);
818 out.push_back((*val_u64 >> 40) & 0xFF);
819 }
820 zero_pad(out);
821 return TOSA_OK;
822}
823
824tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
825{
826 out.clear();
827 for (auto val : in)
828 {
829 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
830 out.push_back(*val_u32 & 0xFF);
831 out.push_back((*val_u32 >> 8) & 0xFF);
832 out.push_back((*val_u32 >> 16) & 0xFF);
833 out.push_back((*val_u32 >> 24) & 0xFF);
834 }
835 zero_pad(out);
836 return TOSA_OK;
837}
838
839tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
840{
841 out.clear();
842 for (auto val : in)
843 {
844 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
845 out.push_back(*val_u16 & 0xFF);
846 out.push_back((*val_u16 >> 8) & 0xFF);
847 }
848 zero_pad(out);
849 return TOSA_OK;
850}
851
852tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
853{
854 out.clear();
855 for (auto val : in)
856 {
857 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
858 out.push_back(*val_u8);
859 }
860 zero_pad(out);
861 return TOSA_OK;
862}
863
Kevin Cheng3ce56342021-07-28 13:42:29 -0700864// Two int4 values are packed into one byte out.
865// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
866// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
867tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
868{
869 out.clear();
870 uint32_t in_size = in.size();
871 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800872 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700873 {
874 int8_t val_0 = in[2 * i];
875 int8_t val_1 = 0;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800876 if (2u * i + 1u < in_size)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700877 {
878 val_1 = in[2 * i + 1];
879 }
880 // In TOSA spec, int4 ranges [-7, 7]
881 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
882 {
883 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
884 val_0, val_1);
885 return TOSA_USER_ERROR;
886 }
887 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
888 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
889 out.push_back(val_u8);
890 }
891 zero_pad(out);
892 return TOSA_OK;
893}
894
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700895tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
896{
897 out.clear();
898 for (auto val : in)
899 {
Eric Kunze4417b422022-06-20 07:27:42 -0700900 uint8_t val_u8 = val;
901 out.push_back(val_u8);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700902 }
903 zero_pad(out);
904 return TOSA_OK;
905}
906
907tosa_err_t
James Ward485a11d2022-08-05 13:48:37 +0100908 TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
909{
910 // Note: fp16 values returned in fp32 type
911 out.clear();
912 if (in.size() < out_size * sizeof(int16_t))
913 {
914 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
915 out_size * sizeof(int16_t));
916 return TOSA_USER_ERROR;
917 }
918
919 for (uint32_t i = 0; i < out_size; i++)
920 {
921 uint16_t f16_byte0 = in[i * sizeof(int16_t)];
922 uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1];
923 uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8);
924
925 // Reinterpret u16 byte as fp16 then convert to fp32
926 half_float::half val_f16 = *(half_float::half*)&val_u16;
927 float val_fp32 = half_float::half_cast<float, half_float::half>(val_f16);
928 out.push_back(val_fp32);
929 }
930 return TOSA_OK;
931}
932
933tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700934 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
935{
936 out.clear();
937 if (in.size() < out_size * sizeof(float))
938 {
939 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
940 out_size * sizeof(float));
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(float)];
946 uint32_t byte1 = in[i * sizeof(float) + 1];
947 uint32_t byte2 = in[i * sizeof(float) + 2];
948 uint32_t byte3 = in[i * sizeof(float) + 3];
949 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
950 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
951 out.push_back(*val_fp32);
952 }
953 return TOSA_OK;
954}
955
956tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
957 uint32_t out_size,
958 std::vector<int64_t>& out)
959{
960 out.clear();
961 if (in.size() < out_size * 6 /* sizeof(int48) */)
962 {
963 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
964 out_size * 6);
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 uint64_t byte0 = in[i * 6];
970 uint64_t byte1 = in[i * 6 + 1];
971 uint64_t byte2 = in[i * 6 + 2];
972 uint64_t byte3 = in[i * 6 + 3];
973 uint64_t byte4 = in[i * 6 + 4];
974 uint64_t byte5 = in[i * 6 + 5];
975 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
976 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
977 if (sign)
978 {
979 uint64_t sext_mask = (0xFFFFUL << 48);
980 val_u64 |= sext_mask;
981 }
982 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
983 out.push_back(*val_i64);
984 }
985 return TOSA_OK;
986}
987
988tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
989 uint32_t out_size,
990 std::vector<int32_t>& out)
991{
992 out.clear();
993 if (in.size() < out_size * sizeof(int32_t))
994 {
995 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
996 out_size * sizeof(int32_t));
997 return TOSA_USER_ERROR;
998 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800999 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001000 {
1001 uint32_t byte0 = in[i * sizeof(int32_t)];
1002 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
1003 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
1004 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
1005 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
1006 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
1007 out.push_back(*val_i32);
1008 }
1009 return TOSA_OK;
1010}
1011
1012tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
1013 uint32_t out_size,
1014 std::vector<int16_t>& out)
1015{
1016 out.clear();
1017 if (in.size() < out_size * sizeof(int16_t))
1018 {
1019 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1020 out_size * sizeof(int16_t));
1021 return TOSA_USER_ERROR;
1022 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001023 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001024 {
1025 uint16_t byte0 = in[i * sizeof(int16_t)];
1026 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
1027 uint16_t val_u16 = byte0 + (byte1 << 8);
1028 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
1029 out.push_back(*val_i16);
1030 }
1031 return TOSA_OK;
1032}
1033
1034tosa_err_t
1035 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1036{
1037 out.clear();
1038 if (in.size() < out_size * sizeof(int8_t))
1039 {
1040 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -07001041 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001042 return TOSA_USER_ERROR;
1043 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001044 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001045 {
1046 uint8_t val_u8 = in[i];
1047 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
1048 out.push_back(*val_i8);
1049 }
1050 return TOSA_OK;
1051}
1052
1053tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -07001054 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1055{
1056 out.clear();
1057 if (out_size > in.size() * 2)
1058 {
1059 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
1060 out_size, in.size());
1061 return TOSA_USER_ERROR;
1062 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001063 for (size_t i = 0; i < in.size(); i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -07001064 {
1065 uint8_t val_u8 = in[i];
1066 uint8_t val_0_u4 = val_u8 & 0xF;
1067 uint8_t val_1_u4 = val_u8 >> 4;
1068 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1069 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1070 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1071 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1072 // In TOSA spec, int4 ranges [-7, 7]
1073 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1074 {
1075 printf(
1076 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1077 val_0, val_1);
1078 return TOSA_USER_ERROR;
1079 }
1080 out.push_back(val_0);
1081 if (2 * i + 1 < out_size)
1082 out.push_back(val_1);
1083 }
1084 return TOSA_OK;
1085}
1086
1087tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001088 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1089{
1090 out.clear();
1091 if (in.size() < out_size * sizeof(bool))
1092 {
1093 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1094 in.size(), out_size * sizeof(bool));
1095 return TOSA_USER_ERROR;
1096 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001097 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001098 {
1099 uint8_t val_u8 = in[i];
1100 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1101 out.push_back(*val_bool);
1102 }
1103 return TOSA_OK;
1104}