blob: 25ac5df9cf742ee80c5688c4da6136daa567c018 [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
Jerry Geb413a952023-05-08 19:17:22 +000029 if (shape)
30 {
31 std::copy(shape->begin(), shape->end(), std::back_inserter(_shape));
32 }
Eric Kunze2364dcd2021-04-26 11:06:57 -070033
34 assert(name);
35 _name = name->str();
36
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070037 if (data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070038 {
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070039 std::copy(data->begin(), data->end(), std::back_inserter(_data));
Eric Kunze2364dcd2021-04-26 11:06:57 -070040 }
41}
42
Kevin Cheng545a5082021-11-11 01:36:33 +000043TosaSerializationTensor::TosaSerializationTensor(const std::string& name,
Eric Kunze2364dcd2021-04-26 11:06:57 -070044 const std::vector<int32_t>& shape,
45 DType dtype,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070046 const std::vector<uint8_t>& data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070047{
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070048 _dtype = dtype;
49 _shape = shape;
50 _name = name;
51 _data = data;
Eric Kunze2364dcd2021-04-26 11:06:57 -070052}
53
54TosaSerializationTensor::TosaSerializationTensor()
55{
56 _dtype = DType_UNKNOWN;
Kevin Cheng545a5082021-11-11 01:36:33 +000057 _name = "UNKNOWN";
Eric Kunze2364dcd2021-04-26 11:06:57 -070058}
59
60TosaSerializationTensor::~TosaSerializationTensor()
61{}
62
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000063void TosaSerializationOperator::InitializeAttribute(Attribute attribute_type, const TosaAttributeBase* attribute)
Eric Kunze2364dcd2021-04-26 11:06:57 -070064{
Eric Kunze2364dcd2021-04-26 11:06:57 -070065 _attribute_type = attribute_type;
66
67 switch (attribute_type)
68 {
69 case Attribute_NONE:
70 _attribute = new TosaNoneAttribute();
71 break;
72#define DEF_ATTRIBUTE(NAME, ...) \
73 case Attribute_##NAME##Attribute: \
74 _attribute = new Tosa##NAME##Attribute(attribute); \
75 break;
76#include "attribute.def"
77#undef DEF_ATTRIBUTE
78 default:
79 printf("TosaSerializationOperator::TosaSerializationOperator(): Attribute %s not implemented yet\n",
80 EnumNamesAttribute()[attribute_type]);
81 assert(0);
82 }
83
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000084 assert(_attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +000085}
Eric Kunze2364dcd2021-04-26 11:06:57 -070086
Kevin Cheng545a5082021-11-11 01:36:33 +000087TosaSerializationOperator::TosaSerializationOperator(Op op,
88 Attribute attribute_type,
89 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +000090 const std::vector<std::string>& input_tensor_names,
91 const std::vector<std::string>& output_tensor_names)
92{
93 _op = op;
Eric Kunze2364dcd2021-04-26 11:06:57 -070094 _input_tensor_names = input_tensor_names;
95 _output_tensor_names = output_tensor_names;
Kevin Cheng545a5082021-11-11 01:36:33 +000096
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000097 InitializeAttribute(attribute_type, attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +000098}
99
100TosaSerializationOperator::TosaSerializationOperator(Op op,
101 Attribute attribute_type,
102 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +0000103 std::vector<std::string>&& input_tensor_names,
104 std::vector<std::string>&& output_tensor_names)
105{
106 _op = op;
107 _input_tensor_names = std::move(input_tensor_names);
108 _output_tensor_names = std::move(output_tensor_names);
109
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000110 InitializeAttribute(attribute_type, attribute);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700111}
112
113TosaSerializationOperator::~TosaSerializationOperator()
114{
115 delete _attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700116}
117
Kevin Cheng545a5082021-11-11 01:36:33 +0000118TosaSerializationBasicBlock::TosaSerializationBasicBlock(const std::string& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700119 const std::string& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000120 const std::vector<TosaSerializationOperator*>& operators,
121 const std::vector<TosaSerializationTensor*>& tensors,
122 const std::vector<std::string>& inputs,
123 const std::vector<std::string>& outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700124{
Jerry Ge13c78a62022-10-04 20:32:39 -0700125 _name = name;
126 _region_name = region_name;
127 _operators = operators;
128 _tensors = tensors;
129 _inputs = inputs;
130 _outputs = outputs;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700131}
132
Kevin Cheng545a5082021-11-11 01:36:33 +0000133TosaSerializationBasicBlock::TosaSerializationBasicBlock(std::string&& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700134 std::string&& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000135 std::vector<TosaSerializationOperator*>&& operators,
136 std::vector<TosaSerializationTensor*>&& tensors,
137 std::vector<std::string>&& inputs,
138 std::vector<std::string>&& outputs)
139{
Jerry Ge13c78a62022-10-04 20:32:39 -0700140 _name = std::move(name);
141 _region_name = std::move(region_name);
142 _operators = std::move(operators);
143 _tensors = std::move(tensors);
144 _inputs = std::move(inputs);
145 _outputs = std::move(outputs);
Kevin Cheng545a5082021-11-11 01:36:33 +0000146}
147
Eric Kunze2364dcd2021-04-26 11:06:57 -0700148TosaSerializationBasicBlock::~TosaSerializationBasicBlock()
149{
150 // deallocate all operators
151 for (auto op : GetOperators())
152 {
153 delete op; // ~TosaSerializationOperator()
154 }
155
156 // deallocate all tensors
157 for (auto ts : GetTensors())
158 {
159 delete ts; // ~TosaSerializationTensor()
160 }
161}
162
Jerry Ge13c78a62022-10-04 20:32:39 -0700163TosaSerializationRegion::TosaSerializationRegion(const std::string& name,
164 const std::vector<TosaSerializationBasicBlock*>& blocks)
165{
166 _name = name;
167 _blocks = blocks;
168}
169
170TosaSerializationRegion::TosaSerializationRegion(const std::string&& name,
171 const std::vector<TosaSerializationBasicBlock*>&& blocks)
172{
173 _name = std::move(name);
174 _blocks = std::move(blocks);
175}
176
177TosaSerializationRegion::~TosaSerializationRegion()
178{
179 // deallocate all blocks
180 for (auto block : GetBlocks())
181 {
182 delete block; // ~TosaSerializationBasicBlock()
183 }
184}
185
Eric Kunze2364dcd2021-04-26 11:06:57 -0700186TosaSerializationHandler::TosaSerializationHandler()
187{
188 _schemaLoaded = false;
Kevin Chenge6563f52021-10-20 12:12:02 -0700189 _version = TosaVersion(TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700190}
191
192TosaSerializationHandler::~TosaSerializationHandler()
193{
194 Clear(); // deallocate all basic blocks
195}
196
Kevin Chenga81a7a12021-11-10 14:07:34 -0800197TosaVersion TosaSerializationHandler::ParseTosaSchemaVersion(std::string schema)
198{
199 // Parse all 4 version fields in schema file
200 static const char* keywords[4] = { "major: int32 = ", "minor: int32 = ", "patch: int32 = ", "draft: bool = " };
201 string keyword_str[4];
202 size_t search_pos = 0;
203 size_t keyword_pos;
204 size_t semicolon_pos;
205 // parse integer field first
206 for (int32_t i = 0; i < 4; i++)
207 {
208 keyword_pos = schema.find(keywords[i], search_pos);
209 if (keyword_pos == std::string::npos)
210 {
211 printf("ERROR: can't find keyword \"%s\" in schema\n", keywords[i]);
212 assert(0);
213 }
214 semicolon_pos = schema.find(';', keyword_pos);
215 if (keyword_pos == std::string::npos)
216 {
217 printf("ERROR: can't find ';' in schema\n");
218 assert(0);
219 }
220 keyword_str[i] =
221 schema.substr(keyword_pos + strlen(keywords[i]), semicolon_pos - keyword_pos - strlen(keywords[i]));
222 search_pos = semicolon_pos;
223 }
224
225 int32_t schema_major = 0;
226 int32_t schema_minor = 0;
227 int32_t schema_patch = 0;
228 bool schema_draft = false;
229 try
230 {
231 schema_major = stoi(keyword_str[0]);
232 schema_minor = stoi(keyword_str[1]);
233 schema_patch = stoi(keyword_str[2]);
234 schema_draft = (keyword_str[3] == "true") ? true : false;
235 }
236 catch (std::invalid_argument& e)
237 {
238 printf("ERROR: fail at stoi(): %s\n", e.what());
239 assert(0);
240 }
241
242 TosaVersion schema_version(schema_major, schema_minor, schema_patch, schema_draft);
243
244 return schema_version;
245}
246
Eric Kunze2364dcd2021-04-26 11:06:57 -0700247tosa_err_t TosaSerializationHandler::LoadFileSchema(const char* schema_filename)
248{
249 std::string schema;
250 bool ok;
251
252 ok = flatbuffers::LoadFile(schema_filename, false, &schema);
253 if (!ok)
254 {
255 printf("Error loading schema file: %s\n", schema_filename);
256 return TOSA_FILE_ERROR;
257 }
258
259 ok = _parser.Parse(schema.c_str());
Kevin Chenga81a7a12021-11-10 14:07:34 -0800260
261 TosaVersion schema_version = ParseTosaSchemaVersion(schema);
262
263 TosaVersion::compat_t is_compat = schema_version.is_compatible(GetVersion());
264 switch (is_compat)
265 {
266 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
267 break;
268 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
269 printf("WARNING: Schema flatbuffer version %s is partially compatible with serializer version %s\n",
270 schema_version.to_string().c_str(), GetVersion().to_string().c_str());
271 break;
272 case TosaVersion::compat_t::NOT_COMPATIBLE:
273 printf("ERROR: Schema flatbuffer version %s is not compatible with serializer version %s\n",
274 schema_version.to_string().c_str(), GetVersion().to_string().c_str());
275 return TOSA_VERSION_MISMATCH;
276 }
277
Eric Kunze2364dcd2021-04-26 11:06:57 -0700278 if (!ok)
279 {
280 printf("Error parsing ISA schema file: %s\n", schema_filename);
281 return TOSA_FILE_ERROR;
282 }
283 _schemaLoaded = true;
284
285 return TOSA_OK;
286}
287
288tosa_err_t TosaSerializationHandler::LoadFileJson(const char* filename)
289{
290 std::string jsonfile;
291 bool ok;
292 tosa_err_t err;
293
294 if (!_schemaLoaded)
295 {
296 return TOSA_SCHEMA_MISSING;
297 }
298
299 ok = flatbuffers::LoadFile(filename, false, &jsonfile);
300 if (!ok)
301 {
302 printf("Error loading json file: %s\n", filename);
303 return TOSA_FILE_ERROR;
304 }
305
306 ok = _parser.Parse(jsonfile.c_str());
307 if (!ok)
308 {
309 printf("Error parsing json file: %s\n", filename);
310 return TOSA_FILE_ERROR;
311 }
312
313 uint8_t* buf = _parser.builder_.GetBufferPointer();
314
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700315 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700316 if (err != TOSA_OK)
317 {
318 return err;
319 }
320
321 return TOSA_OK;
322}
323
324tosa_err_t TosaSerializationHandler::SaveFileJson(const char* filename)
325{
326 std::string jsongen;
327 tosa_err_t err;
328
329 if (!_schemaLoaded)
330 {
331 return TOSA_SCHEMA_MISSING;
332 }
333
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700334 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700335 if (err != TOSA_OK)
336 {
337 return err;
338 }
339
340 uint8_t* buf = _builder.GetBufferPointer();
341
342 if (!GenerateText(_parser, buf, &jsongen))
343 {
344 printf("Couldn't serialize parsed data to JSON!\n");
345 return TOSA_FILE_ERROR;
346 }
347
348 FILE* file = fopen(filename, "wb");
349
350 if (!file)
351 {
352 printf("Couldn't open output file: %s\n", filename);
353 return TOSA_FILE_ERROR;
354 }
355
356 if (fwrite(jsongen.c_str(), sizeof(char), jsongen.size(), file) != jsongen.size())
357 {
358 printf("Error writing to json output file: %s\n", filename);
359 fclose(file);
360 return TOSA_FILE_ERROR;
361 }
362
363 if (file)
364 fclose(file);
365
366 return TOSA_OK;
367}
368
369tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const char* filename)
370{
371 std::string read_buffer;
372 tosa_err_t err;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800373 const uint8_t* buf;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700374 bool ok;
375
376 ok = flatbuffers::LoadFile(filename, false, &read_buffer);
377 if (!ok)
378 {
379 printf("Error loading flatbuffer file: %s\n", filename);
380 return TOSA_FILE_ERROR;
381 }
382
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800383 buf = reinterpret_cast<const uint8_t*>(read_buffer.data());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700384
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700385 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700386 if (err != TOSA_OK)
387 {
388 return err;
389 }
390
391 return TOSA_OK;
392}
393
Aaron DeBattista8b3903a2021-11-18 16:38:11 +0000394tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const void* input, int in_size)
395{
396 tosa_err_t err;
397
398 const uint8_t* buf = (const uint8_t*)input;
399 err = Deserialize(buf);
400 if (err != TOSA_OK)
401 {
402 return err;
403 }
404
405 return TOSA_OK;
406}
407
Eric Kunze2364dcd2021-04-26 11:06:57 -0700408tosa_err_t TosaSerializationHandler::SaveFileTosaFlatbuffer(const char* filename)
409{
410 tosa_err_t err;
411
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700412 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700413 if (err != TOSA_OK)
414 {
415 return err;
416 }
417
418 uint8_t* buf = _builder.GetBufferPointer();
419
420 bool ok = flatbuffers::SaveFile(filename, (const char*)buf, _builder.GetSize(), false);
421 if (!ok)
422 {
423 printf("Error saving floatbuffer file: %s\n", filename);
424 return TOSA_FILE_ERROR;
425 }
426
427 return TOSA_OK;
428}
429
430tosa_err_t TosaSerializationHandler::Clear()
431{
432 // deallocate all basic blocks
Jerry Ge13c78a62022-10-04 20:32:39 -0700433 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700434 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700435 delete region;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700436 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700437 _regions.clear();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700438
439 return TOSA_OK;
440}
441
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700442tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700443{
Eric Kunzee6596402022-06-09 21:27:36 +0000444 if (!TosaGraphBufferHasIdentifier(buf))
445 {
446 printf("WARNING: TOSA file does not have TOSA file identifier\n");
447 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700448 auto fb_tosa_graph = GetTosaGraph(buf);
449 auto fb_tosa_version = fb_tosa_graph->version();
Jerry Ge13c78a62022-10-04 20:32:39 -0700450 auto fb_tosa_regions = fb_tosa_graph->regions();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700451
Eric Kunze2364dcd2021-04-26 11:06:57 -0700452 TosaAttributeBase* typed_attribute = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700453 TosaSerializationOperator* new_operator = NULL;
454 TosaSerializationBasicBlock* new_block = NULL;
455 TosaSerializationTensor* new_tensor = NULL;
Jerry Ge13c78a62022-10-04 20:32:39 -0700456 TosaSerializationRegion* new_region = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700457
458 // erase container
459 Clear();
460
Kevin Chenge6563f52021-10-20 12:12:02 -0700461 TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(),
462 fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700463
Kevin Chenge6563f52021-10-20 12:12:02 -0700464 TosaVersion::compat_t is_compat = read_version.is_compatible(GetVersion());
465 switch (is_compat)
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700466 {
Kevin Chenge6563f52021-10-20 12:12:02 -0700467 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
468 break;
469 case TosaVersion::compat_t::PARTIALLY_COMPATIBLE:
470 printf("WARNING: Read flatbuffer version %s is partially compatible with serializer version %s\n",
471 read_version.to_string().c_str(), GetVersion().to_string().c_str());
472 break;
473 case TosaVersion::compat_t::NOT_COMPATIBLE:
474 printf("ERROR: Read flatbuffer version %s is not compatible with serializer version %s\n",
475 read_version.to_string().c_str(), GetVersion().to_string().c_str());
476 return TOSA_VERSION_MISMATCH;
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700477 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700478
Jerry Ge13c78a62022-10-04 20:32:39 -0700479 for (size_t i = 0; i < fb_tosa_regions->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700480 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700481 auto curr_region = fb_tosa_regions->Get(i);
482 auto region_name = curr_region->name()->str();
483 auto fb_tosa_blocks = curr_region->blocks();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700484
Tai Lycfcb20d2023-03-13 21:04:11 +0000485 new_region = new TosaSerializationRegion(curr_region->name()->str(), {});
Jerry Ge13c78a62022-10-04 20:32:39 -0700486 this->GetRegions().push_back(new_region);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700487
Jerry Ge13c78a62022-10-04 20:32:39 -0700488 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700489 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000490 std::vector<TosaSerializationOperator*> block_operators_container;
491 std::vector<TosaSerializationTensor*> block_tensors_container;
492 std::vector<std::string> block_inputs_container;
493 std::vector<std::string> block_outputs_container;
494
Jerry Ge13c78a62022-10-04 20:32:39 -0700495 auto curr_block = fb_tosa_blocks->Get(i);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700496
Jerry Ge13c78a62022-10-04 20:32:39 -0700497 auto block_name = curr_block->name()->str();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700498
Jerry Ge13c78a62022-10-04 20:32:39 -0700499 auto fb_tosa_operators = curr_block->operators();
Jerry Ge13c78a62022-10-04 20:32:39 -0700500 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700501 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700502 auto curr_operator = fb_tosa_operators->Get(j);
503
504 auto operator_op = curr_operator->op();
505 auto attribute_type = curr_operator->attribute_type();
506 auto attribute = curr_operator->attribute();
507
Tai Lycfcb20d2023-03-13 21:04:11 +0000508 std::vector<std::string> operator_inputs_container;
509 std::vector<std::string> operator_outputs_container;
510
Jerry Ge13c78a62022-10-04 20:32:39 -0700511 // input tensors
512 auto operator_inputs = curr_operator->inputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700513 if (operator_inputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700514 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700515 for (size_t k = 0; k < operator_inputs->size(); k++)
516 {
517 auto curr_input = operator_inputs->Get(k);
518 operator_inputs_container.push_back(curr_input->str());
519 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700520 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700521
Jerry Ge13c78a62022-10-04 20:32:39 -0700522 // output tensors
523 auto operator_outputs = curr_operator->outputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700524 if (operator_outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700525 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700526 for (size_t k = 0; k < operator_outputs->size(); k++)
527 {
528 auto curr_output = operator_outputs->Get(k);
529 operator_outputs_container.push_back(curr_output->str());
530 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700531 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700532
Jerry Ge13c78a62022-10-04 20:32:39 -0700533 switch (attribute_type)
534 {
535 case Attribute_NONE:
536 typed_attribute = new TosaNoneAttribute();
537 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700538#define DEF_ATTRIBUTE(NAME, ...) \
539 case Attribute_##NAME##Attribute: \
540 typed_attribute = new Tosa##NAME##Attribute(attribute); \
541 break;
542#include "attribute.def"
543#undef DEF_ATTRIBUTE
Jerry Ge13c78a62022-10-04 20:32:39 -0700544 default:
545 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
546 EnumNamesAttribute()[attribute_type]);
547 return TOSA_INTERNAL_ERROR;
548 }
549
550 new_operator = new TosaSerializationOperator(operator_op, attribute_type, typed_attribute,
551 operator_inputs_container, operator_outputs_container);
552 if (new_operator)
553 {
554 block_operators_container.push_back(new_operator);
555 }
556 else
557 {
558 return TOSA_MEMORY_ERROR;
559 }
560
561 if (typed_attribute)
562 delete typed_attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700563 }
564
Jerry Ge13c78a62022-10-04 20:32:39 -0700565 auto block_inputs = curr_block->inputs();
566 auto block_outputs = curr_block->outputs();
567
Jerry Ge13c78a62022-10-04 20:32:39 -0700568 for (size_t j = 0; j < block_inputs->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700569 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700570 auto curr_block_input = block_inputs->Get(j);
571 block_inputs_container.push_back(curr_block_input->str());
572 }
573 for (size_t j = 0; j < block_outputs->size(); j++)
574 {
575 auto curr_block_output = block_outputs->Get(j);
576 block_outputs_container.push_back(curr_block_output->str());
577 }
578
579 auto fb_tosa_tensors = curr_block->tensors();
Jerry Ge13c78a62022-10-04 20:32:39 -0700580 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
581 {
582 auto curr_tensor = fb_tosa_tensors->Get(j);
583
584 auto tensor_name = curr_tensor->name();
585 auto tensor_shape = curr_tensor->shape();
586 auto tensor_type = curr_tensor->type();
587 auto tensor_data = curr_tensor->data();
588
589 new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data);
590 if (new_tensor)
591 {
592 block_tensors_container.push_back(new_tensor);
593 }
594 else
595 {
596 return TOSA_MEMORY_ERROR;
597 }
598 }
599 new_block = new TosaSerializationBasicBlock(block_name, region_name, block_operators_container,
600 block_tensors_container, block_inputs_container,
601 block_outputs_container);
602 if (new_block)
603 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000604 new_region->GetBlocks().push_back(new_block);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700605 }
606 else
607 {
608 return TOSA_MEMORY_ERROR;
609 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700610 } // end block for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700611 }
612
613 return TOSA_OK;
614}
615
James Ward80905bb2023-01-25 15:51:27 +0000616std::vector<uint8_t> float_to_u8_helper(float f_in)
James Wardc15f7d52022-12-07 15:38:01 +0000617{
James Ward80905bb2023-01-25 15:51:27 +0000618 // Push back a single float value to the buffer with *NO PADDING*
619 // Therefore ConvertF32toU8 function not used
James Wardc15f7d52022-12-07 15:38:01 +0000620 std::vector<uint8_t> u8_out;
James Ward80905bb2023-01-25 15:51:27 +0000621 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&f_in);
622 u8_out.push_back(*val_u32 & 0xFF);
623 u8_out.push_back((*val_u32 >> 8) & 0xFF);
624 u8_out.push_back((*val_u32 >> 16) & 0xFF);
625 u8_out.push_back((*val_u32 >> 24) & 0xFF);
James Wardc15f7d52022-12-07 15:38:01 +0000626 return u8_out;
627}
628
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700629tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700630{
Jerry Ge13c78a62022-10-04 20:32:39 -0700631 // regions
632 std::vector<flatbuffers::Offset<TosaRegion>> fboffset_regions;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700633
Eric Kunze2364dcd2021-04-26 11:06:57 -0700634 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
Jerry Ge13c78a62022-10-04 20:32:39 -0700635 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700636 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000637 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
Jerry Ge13c78a62022-10-04 20:32:39 -0700638 for (auto block : region->GetBlocks())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700639 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000640 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
641 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
642 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
643 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700644 auto block_name = _builder.CreateString(block->GetName().c_str());
645 for (auto tensor_str : block->GetInputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700646 {
647 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700648 fboffset_block_inputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700649 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700650 for (auto tensor_str : block->GetOutputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700651 {
652 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700653 fboffset_block_outputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700654 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700655 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
656 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
657 for (auto op : block->GetOperators())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700658 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000659 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
660 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700661 auto operator_op = op->GetOp();
662 auto attribute_type = op->GetAttributeType();
663 for (auto tensor_str : op->GetInputTensorNames())
664 {
665 auto tensor_name = _builder.CreateString(tensor_str.c_str());
666 fboffset_operator_inputs.push_back(tensor_name);
667 }
668 for (auto tensor_str : op->GetOutputTensorNames())
669 {
670 auto tensor_name = _builder.CreateString(tensor_str.c_str());
671 fboffset_operator_outputs.push_back(tensor_name);
672 }
673 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
674 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
675 flatbuffers::Offset<void> fb_attribute;
676 switch (attribute_type)
677 {
678 case Attribute_NONE:
679 fb_attribute = 0;
680 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700681#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 +0000682#define DEF_ARGS_S_FP_as_U8(NAME, V) \
James Ward80905bb2023-01-25 15:51:27 +0000683 , _builder.CreateVector<uint8_t>(float_to_u8_helper(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()))
Eric Kunze2364dcd2021-04-26 11:06:57 -0700684#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700685#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Wardc15f7d52022-12-07 15:38:01 +0000686#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_FP_as_U8(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700687#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
688#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Ward485a11d2022-08-05 13:48:37 +0100689#define DEF_ARGS_S_DType(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700690#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700691#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
692#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700693#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
694#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
695#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
696 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
697#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
698 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
699#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
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)
702#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
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)
705#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) \
706 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
707 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
708#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
709 case Attribute_##NAME##Attribute: \
710 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
711 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700712#include "attribute.def"
713#undef DEF_ATTRIBUTE
714#undef DEF_ARGS_1
715#undef DEF_ARGS_2
716#undef DEF_ARGS_3
717#undef DEF_ARGS_4
718#undef DEF_ARGS_5
719#undef DEF_ARGS_6
720#undef DEF_ARGS_7
721#undef DEF_ARGS_S
722#undef DEF_ARGS_V
723#undef DEF_ARGS_S_int32_t
724#undef DEF_ARGS_S_float
725#undef DEF_ARGS_S_bool
726#undef DEF_ARGS_S_ResizeMode
James Ward485a11d2022-08-05 13:48:37 +0100727#undef DEF_ARGS_S_DType
Eric Kunze2364dcd2021-04-26 11:06:57 -0700728#undef DEF_ARGS_S_string
729#undef DEF_ARGS_S_STR
730#undef DEF_ARGS_S_DEFAULT
Jerry Ge13c78a62022-10-04 20:32:39 -0700731 default:
732 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
733 EnumNamesAttribute()[attribute_type]);
734 return TOSA_INTERNAL_ERROR;
735 }
736 auto fboffset_operator = CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute,
737 fb_operator_inputs, fb_operator_outputs);
738 fboffset_block_operators.push_back(fboffset_operator);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700739 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700740 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
741 for (auto tensor : block->GetTensors())
742 {
743 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
744 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
745 auto tensor_dtype = tensor->GetDtype();
746 auto tensor_data = _builder.CreateVector(tensor->GetData());
747 auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data);
748 fboffset_block_tensors.push_back(fboffset_tensor);
749 }
750 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
751 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
752 fb_block_inputs, fb_block_outputs);
753 fboffset_blocks.push_back(fboffset_block);
754 } // end block for_loop
755 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700756
Jerry Ge13c78a62022-10-04 20:32:39 -0700757 auto region_name = _builder.CreateString(region->GetName().c_str());
758 auto fboffset_region = CreateTosaRegion(_builder, region_name, fb_blocks);
759 fboffset_regions.push_back(fboffset_region);
760 } // end region for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700761
Jerry Ge13c78a62022-10-04 20:32:39 -0700762 auto fb_regions = _builder.CreateVector(fboffset_regions);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700763
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700764 auto fb_version =
765 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Jerry Ge13c78a62022-10-04 20:32:39 -0700766 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_regions);
Eric Kunzee6596402022-06-09 21:27:36 +0000767 _builder.Finish(fb_graph, TosaGraphIdentifier());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700768
769 return TOSA_OK;
770}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700771
772void zero_pad(std::vector<uint8_t>& buf)
773{
774 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
775 {
776 buf.push_back(0);
777 }
778}
779
James Ward485a11d2022-08-05 13:48:37 +0100780tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
781{
782 // Note: Converts fp32->fp16 before converting to uint8_t
783 out.clear();
784 for (auto val : in)
785 {
786 half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val);
787 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16);
788 out.push_back(*val_u16 & 0xFF);
789 out.push_back((*val_u16 >> 8) & 0xFF);
790 }
791 zero_pad(out);
792 return TOSA_OK;
793}
794
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700795tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
796{
797 out.clear();
798 for (auto val : in)
799 {
800 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
801 out.push_back(*val_u32 & 0xFF);
802 out.push_back((*val_u32 >> 8) & 0xFF);
803 out.push_back((*val_u32 >> 16) & 0xFF);
804 out.push_back((*val_u32 >> 24) & 0xFF);
805 }
806 zero_pad(out);
807 return TOSA_OK;
808}
809
810tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
811{
812 out.clear();
813 for (auto val : in)
814 {
815 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
816 out.push_back(*val_u64 & 0xFF);
817 out.push_back((*val_u64 >> 8) & 0xFF);
818 out.push_back((*val_u64 >> 16) & 0xFF);
819 out.push_back((*val_u64 >> 24) & 0xFF);
820 out.push_back((*val_u64 >> 32) & 0xFF);
821 out.push_back((*val_u64 >> 40) & 0xFF);
822 }
823 zero_pad(out);
824 return TOSA_OK;
825}
826
827tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
828{
829 out.clear();
830 for (auto val : in)
831 {
832 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
833 out.push_back(*val_u32 & 0xFF);
834 out.push_back((*val_u32 >> 8) & 0xFF);
835 out.push_back((*val_u32 >> 16) & 0xFF);
836 out.push_back((*val_u32 >> 24) & 0xFF);
837 }
838 zero_pad(out);
839 return TOSA_OK;
840}
841
842tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
843{
844 out.clear();
845 for (auto val : in)
846 {
847 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
848 out.push_back(*val_u16 & 0xFF);
849 out.push_back((*val_u16 >> 8) & 0xFF);
850 }
851 zero_pad(out);
852 return TOSA_OK;
853}
854
855tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
856{
857 out.clear();
858 for (auto val : in)
859 {
860 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
861 out.push_back(*val_u8);
862 }
863 zero_pad(out);
864 return TOSA_OK;
865}
866
Kevin Cheng3ce56342021-07-28 13:42:29 -0700867// Two int4 values are packed into one byte out.
868// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
869// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
870tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
871{
872 out.clear();
873 uint32_t in_size = in.size();
874 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800875 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700876 {
877 int8_t val_0 = in[2 * i];
878 int8_t val_1 = 0;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800879 if (2u * i + 1u < in_size)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700880 {
881 val_1 = in[2 * i + 1];
882 }
883 // In TOSA spec, int4 ranges [-7, 7]
884 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
885 {
886 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
887 val_0, val_1);
888 return TOSA_USER_ERROR;
889 }
890 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
891 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
892 out.push_back(val_u8);
893 }
894 zero_pad(out);
895 return TOSA_OK;
896}
897
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700898tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
899{
900 out.clear();
901 for (auto val : in)
902 {
Eric Kunze4417b422022-06-20 07:27:42 -0700903 uint8_t val_u8 = val;
904 out.push_back(val_u8);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700905 }
906 zero_pad(out);
907 return TOSA_OK;
908}
909
910tosa_err_t
James Ward485a11d2022-08-05 13:48:37 +0100911 TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
912{
913 // Note: fp16 values returned in fp32 type
914 out.clear();
915 if (in.size() < out_size * sizeof(int16_t))
916 {
917 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
918 out_size * sizeof(int16_t));
919 return TOSA_USER_ERROR;
920 }
921
922 for (uint32_t i = 0; i < out_size; i++)
923 {
924 uint16_t f16_byte0 = in[i * sizeof(int16_t)];
925 uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1];
926 uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8);
927
928 // Reinterpret u16 byte as fp16 then convert to fp32
929 half_float::half val_f16 = *(half_float::half*)&val_u16;
930 float val_fp32 = half_float::half_cast<float, half_float::half>(val_f16);
931 out.push_back(val_fp32);
932 }
933 return TOSA_OK;
934}
935
936tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700937 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
938{
939 out.clear();
940 if (in.size() < out_size * sizeof(float))
941 {
942 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
943 out_size * sizeof(float));
944 return TOSA_USER_ERROR;
945 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800946 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700947 {
948 uint32_t byte0 = in[i * sizeof(float)];
949 uint32_t byte1 = in[i * sizeof(float) + 1];
950 uint32_t byte2 = in[i * sizeof(float) + 2];
951 uint32_t byte3 = in[i * sizeof(float) + 3];
952 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
953 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
954 out.push_back(*val_fp32);
955 }
956 return TOSA_OK;
957}
958
959tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
960 uint32_t out_size,
961 std::vector<int64_t>& out)
962{
963 out.clear();
964 if (in.size() < out_size * 6 /* sizeof(int48) */)
965 {
966 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
967 out_size * 6);
968 return TOSA_USER_ERROR;
969 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800970 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700971 {
972 uint64_t byte0 = in[i * 6];
973 uint64_t byte1 = in[i * 6 + 1];
974 uint64_t byte2 = in[i * 6 + 2];
975 uint64_t byte3 = in[i * 6 + 3];
976 uint64_t byte4 = in[i * 6 + 4];
977 uint64_t byte5 = in[i * 6 + 5];
978 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
979 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
980 if (sign)
981 {
982 uint64_t sext_mask = (0xFFFFUL << 48);
983 val_u64 |= sext_mask;
984 }
985 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
986 out.push_back(*val_i64);
987 }
988 return TOSA_OK;
989}
990
991tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
992 uint32_t out_size,
993 std::vector<int32_t>& out)
994{
995 out.clear();
996 if (in.size() < out_size * sizeof(int32_t))
997 {
998 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
999 out_size * sizeof(int32_t));
1000 return TOSA_USER_ERROR;
1001 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001002 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001003 {
1004 uint32_t byte0 = in[i * sizeof(int32_t)];
1005 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
1006 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
1007 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
1008 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
1009 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
1010 out.push_back(*val_i32);
1011 }
1012 return TOSA_OK;
1013}
1014
1015tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
1016 uint32_t out_size,
1017 std::vector<int16_t>& out)
1018{
1019 out.clear();
1020 if (in.size() < out_size * sizeof(int16_t))
1021 {
1022 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1023 out_size * sizeof(int16_t));
1024 return TOSA_USER_ERROR;
1025 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001026 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001027 {
1028 uint16_t byte0 = in[i * sizeof(int16_t)];
1029 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
1030 uint16_t val_u16 = byte0 + (byte1 << 8);
1031 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
1032 out.push_back(*val_i16);
1033 }
1034 return TOSA_OK;
1035}
1036
1037tosa_err_t
1038 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1039{
1040 out.clear();
1041 if (in.size() < out_size * sizeof(int8_t))
1042 {
1043 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -07001044 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001045 return TOSA_USER_ERROR;
1046 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001047 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001048 {
1049 uint8_t val_u8 = in[i];
1050 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
1051 out.push_back(*val_i8);
1052 }
1053 return TOSA_OK;
1054}
1055
1056tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -07001057 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1058{
1059 out.clear();
1060 if (out_size > in.size() * 2)
1061 {
1062 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
1063 out_size, in.size());
1064 return TOSA_USER_ERROR;
1065 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001066 for (size_t i = 0; i < in.size(); i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -07001067 {
1068 uint8_t val_u8 = in[i];
1069 uint8_t val_0_u4 = val_u8 & 0xF;
1070 uint8_t val_1_u4 = val_u8 >> 4;
1071 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1072 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1073 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1074 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1075 // In TOSA spec, int4 ranges [-7, 7]
1076 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1077 {
1078 printf(
1079 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1080 val_0, val_1);
1081 return TOSA_USER_ERROR;
1082 }
1083 out.push_back(val_0);
1084 if (2 * i + 1 < out_size)
1085 out.push_back(val_1);
1086 }
1087 return TOSA_OK;
1088}
1089
1090tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001091 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1092{
1093 out.clear();
1094 if (in.size() < out_size * sizeof(bool))
1095 {
1096 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1097 in.size(), out_size * sizeof(bool));
1098 return TOSA_USER_ERROR;
1099 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001100 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001101 {
1102 uint8_t val_u8 = in[i];
1103 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1104 out.push_back(*val_bool);
1105 }
1106 return TOSA_OK;
1107}