blob: 453670f0db5e22ad198f7c1b32b76bd95137a115 [file] [log] [blame]
Eric Kunze2364dcd2021-04-26 11:06:57 -07001
Tai Ly5d580fa2023-12-15 20:34:51 +00002// Copyright (c) 2020-2024, ARM Limited.
Eric Kunze2364dcd2021-04-26 11:06:57 -07003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#include "tosa_serialization_handler.h"
James Ward485a11d2022-08-05 13:48:37 +010017#include "half.hpp"
Eric Kunze2364dcd2021-04-26 11:06:57 -070018
19#include <iostream>
20using namespace tosa;
21
22TosaSerializationTensor::TosaSerializationTensor(const flatbuffers::String* name,
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070023 const flatbuffers::Vector<int32_t>* shape,
Eric Kunze2364dcd2021-04-26 11:06:57 -070024 DType dtype,
Jerry Ge442261b2022-09-09 13:38:56 -070025 const flatbuffers::Vector<uint8_t>* data,
Tai Lyc6939a42023-08-21 17:00:29 +000026 const bool variable,
Tai Lyd0520b92023-09-19 21:30:18 +000027 const bool is_unranked,
28 const flatbuffers::String* variable_name)
Eric Kunze2364dcd2021-04-26 11:06:57 -070029{
Jerry Ge442261b2022-09-09 13:38:56 -070030 _dtype = dtype;
31 _variable = variable;
Jerry Geb413a952023-05-08 19:17:22 +000032 if (shape)
33 {
34 std::copy(shape->begin(), shape->end(), std::back_inserter(_shape));
35 }
Eric Kunze2364dcd2021-04-26 11:06:57 -070036
37 assert(name);
38 _name = name->str();
39
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070040 if (data)
Eric Kunze2364dcd2021-04-26 11:06:57 -070041 {
Kevin Cheng3bb1bc12021-06-17 15:57:08 -070042 std::copy(data->begin(), data->end(), std::back_inserter(_data));
Eric Kunze2364dcd2021-04-26 11:06:57 -070043 }
Tai Lyc6939a42023-08-21 17:00:29 +000044 _is_unranked = is_unranked;
Tai Lyd0520b92023-09-19 21:30:18 +000045
46 if (variable_name)
47 {
48 _variable_name = variable_name->str();
49 }
Eric Kunze2364dcd2021-04-26 11:06:57 -070050}
51
Kevin Cheng545a5082021-11-11 01:36:33 +000052TosaSerializationTensor::TosaSerializationTensor(const std::string& name,
Eric Kunze2364dcd2021-04-26 11:06:57 -070053 const std::vector<int32_t>& shape,
54 DType dtype,
Jerry Ge442261b2022-09-09 13:38:56 -070055 const std::vector<uint8_t>& data,
Tai Lyc6939a42023-08-21 17:00:29 +000056 const bool variable,
Tai Lyd0520b92023-09-19 21:30:18 +000057 const bool is_unranked,
58 const std::string& variable_name)
Eric Kunze2364dcd2021-04-26 11:06:57 -070059{
Tai Lyd0520b92023-09-19 21:30:18 +000060 _dtype = dtype;
61 _variable = variable;
62 _shape = shape;
63 _name = name;
64 _data = data;
65 _is_unranked = is_unranked;
66 _variable_name = variable_name;
Eric Kunze2364dcd2021-04-26 11:06:57 -070067}
68
69TosaSerializationTensor::TosaSerializationTensor()
70{
Tai Lyc6939a42023-08-21 17:00:29 +000071 _dtype = DType_UNKNOWN;
72 _variable = false;
73 _name = "UNKNOWN";
74 _is_unranked = false;
Eric Kunze2364dcd2021-04-26 11:06:57 -070075}
76
77TosaSerializationTensor::~TosaSerializationTensor()
78{}
79
Eric Kunzebdcc3fe2022-06-07 05:17:37 +000080void TosaSerializationOperator::InitializeAttribute(Attribute attribute_type, const TosaAttributeBase* attribute)
Eric Kunze2364dcd2021-04-26 11:06:57 -070081{
Eric Kunze2364dcd2021-04-26 11:06:57 -070082 _attribute_type = attribute_type;
83
84 switch (attribute_type)
85 {
86 case Attribute_NONE:
87 _attribute = new TosaNoneAttribute();
88 break;
89#define DEF_ATTRIBUTE(NAME, ...) \
90 case Attribute_##NAME##Attribute: \
91 _attribute = new Tosa##NAME##Attribute(attribute); \
92 break;
93#include "attribute.def"
94#undef DEF_ATTRIBUTE
95 default:
96 printf("TosaSerializationOperator::TosaSerializationOperator(): Attribute %s not implemented yet\n",
97 EnumNamesAttribute()[attribute_type]);
98 assert(0);
99 }
100
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000101 assert(_attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +0000102}
Eric Kunze2364dcd2021-04-26 11:06:57 -0700103
Kevin Cheng545a5082021-11-11 01:36:33 +0000104TosaSerializationOperator::TosaSerializationOperator(Op op,
105 Attribute attribute_type,
106 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +0000107 const std::vector<std::string>& input_tensor_names,
108 const std::vector<std::string>& output_tensor_names)
109{
110 _op = op;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700111 _input_tensor_names = input_tensor_names;
112 _output_tensor_names = output_tensor_names;
Kevin Cheng545a5082021-11-11 01:36:33 +0000113
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000114 InitializeAttribute(attribute_type, attribute);
Kevin Cheng545a5082021-11-11 01:36:33 +0000115}
116
117TosaSerializationOperator::TosaSerializationOperator(Op op,
118 Attribute attribute_type,
119 const TosaAttributeBase* attribute,
Kevin Cheng545a5082021-11-11 01:36:33 +0000120 std::vector<std::string>&& input_tensor_names,
121 std::vector<std::string>&& output_tensor_names)
122{
123 _op = op;
124 _input_tensor_names = std::move(input_tensor_names);
125 _output_tensor_names = std::move(output_tensor_names);
126
Eric Kunzebdcc3fe2022-06-07 05:17:37 +0000127 InitializeAttribute(attribute_type, attribute);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700128}
129
130TosaSerializationOperator::~TosaSerializationOperator()
131{
132 delete _attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700133}
134
Kevin Cheng545a5082021-11-11 01:36:33 +0000135TosaSerializationBasicBlock::TosaSerializationBasicBlock(const std::string& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700136 const std::string& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000137 const std::vector<TosaSerializationOperator*>& operators,
138 const std::vector<TosaSerializationTensor*>& tensors,
139 const std::vector<std::string>& inputs,
140 const std::vector<std::string>& outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700141{
Jerry Ge13c78a62022-10-04 20:32:39 -0700142 _name = name;
143 _region_name = region_name;
144 _operators = operators;
145 _tensors = tensors;
146 _inputs = inputs;
147 _outputs = outputs;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700148}
149
Kevin Cheng545a5082021-11-11 01:36:33 +0000150TosaSerializationBasicBlock::TosaSerializationBasicBlock(std::string&& name,
Jerry Ge13c78a62022-10-04 20:32:39 -0700151 std::string&& region_name,
Kevin Cheng545a5082021-11-11 01:36:33 +0000152 std::vector<TosaSerializationOperator*>&& operators,
153 std::vector<TosaSerializationTensor*>&& tensors,
154 std::vector<std::string>&& inputs,
155 std::vector<std::string>&& outputs)
156{
Jerry Ge13c78a62022-10-04 20:32:39 -0700157 _name = std::move(name);
158 _region_name = std::move(region_name);
159 _operators = std::move(operators);
160 _tensors = std::move(tensors);
161 _inputs = std::move(inputs);
162 _outputs = std::move(outputs);
Kevin Cheng545a5082021-11-11 01:36:33 +0000163}
164
Eric Kunze2364dcd2021-04-26 11:06:57 -0700165TosaSerializationBasicBlock::~TosaSerializationBasicBlock()
166{
167 // deallocate all operators
168 for (auto op : GetOperators())
169 {
170 delete op; // ~TosaSerializationOperator()
171 }
172
173 // deallocate all tensors
174 for (auto ts : GetTensors())
175 {
176 delete ts; // ~TosaSerializationTensor()
177 }
178}
179
Jerry Ge13c78a62022-10-04 20:32:39 -0700180TosaSerializationRegion::TosaSerializationRegion(const std::string& name,
181 const std::vector<TosaSerializationBasicBlock*>& blocks)
182{
183 _name = name;
184 _blocks = blocks;
185}
186
187TosaSerializationRegion::TosaSerializationRegion(const std::string&& name,
188 const std::vector<TosaSerializationBasicBlock*>&& blocks)
189{
190 _name = std::move(name);
191 _blocks = std::move(blocks);
192}
193
194TosaSerializationRegion::~TosaSerializationRegion()
195{
196 // deallocate all blocks
197 for (auto block : GetBlocks())
198 {
199 delete block; // ~TosaSerializationBasicBlock()
200 }
201}
202
Eric Kunze2364dcd2021-04-26 11:06:57 -0700203TosaSerializationHandler::TosaSerializationHandler()
204{
205 _schemaLoaded = false;
Kevin Chenge6563f52021-10-20 12:12:02 -0700206 _version = TosaVersion(TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700207}
208
209TosaSerializationHandler::~TosaSerializationHandler()
210{
211 Clear(); // deallocate all basic blocks
212}
213
Eric Kunze2364dcd2021-04-26 11:06:57 -0700214tosa_err_t TosaSerializationHandler::LoadFileSchema(const char* schema_filename)
215{
216 std::string schema;
217 bool ok;
218
219 ok = flatbuffers::LoadFile(schema_filename, false, &schema);
220 if (!ok)
221 {
222 printf("Error loading schema file: %s\n", schema_filename);
223 return TOSA_FILE_ERROR;
224 }
225
226 ok = _parser.Parse(schema.c_str());
Kevin Chenga81a7a12021-11-10 14:07:34 -0800227
Eric Kunze2364dcd2021-04-26 11:06:57 -0700228 if (!ok)
229 {
230 printf("Error parsing ISA schema file: %s\n", schema_filename);
231 return TOSA_FILE_ERROR;
232 }
233 _schemaLoaded = true;
234
235 return TOSA_OK;
236}
237
238tosa_err_t TosaSerializationHandler::LoadFileJson(const char* filename)
239{
240 std::string jsonfile;
241 bool ok;
242 tosa_err_t err;
243
244 if (!_schemaLoaded)
245 {
246 return TOSA_SCHEMA_MISSING;
247 }
248
249 ok = flatbuffers::LoadFile(filename, false, &jsonfile);
250 if (!ok)
251 {
252 printf("Error loading json file: %s\n", filename);
253 return TOSA_FILE_ERROR;
254 }
255
256 ok = _parser.Parse(jsonfile.c_str());
257 if (!ok)
258 {
259 printf("Error parsing json file: %s\n", filename);
260 return TOSA_FILE_ERROR;
261 }
262
263 uint8_t* buf = _parser.builder_.GetBufferPointer();
264
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700265 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700266 if (err != TOSA_OK)
267 {
268 return err;
269 }
270
271 return TOSA_OK;
272}
273
274tosa_err_t TosaSerializationHandler::SaveFileJson(const char* filename)
275{
276 std::string jsongen;
277 tosa_err_t err;
278
279 if (!_schemaLoaded)
280 {
281 return TOSA_SCHEMA_MISSING;
282 }
283
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700284 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700285 if (err != TOSA_OK)
286 {
287 return err;
288 }
289
290 uint8_t* buf = _builder.GetBufferPointer();
291
Tai Ly89963aa2023-07-03 22:14:05 +0000292 if (GenText(_parser, buf, &jsongen))
Eric Kunze2364dcd2021-04-26 11:06:57 -0700293 {
294 printf("Couldn't serialize parsed data to JSON!\n");
295 return TOSA_FILE_ERROR;
296 }
297
298 FILE* file = fopen(filename, "wb");
299
300 if (!file)
301 {
302 printf("Couldn't open output file: %s\n", filename);
303 return TOSA_FILE_ERROR;
304 }
305
306 if (fwrite(jsongen.c_str(), sizeof(char), jsongen.size(), file) != jsongen.size())
307 {
308 printf("Error writing to json output file: %s\n", filename);
309 fclose(file);
310 return TOSA_FILE_ERROR;
311 }
312
313 if (file)
314 fclose(file);
315
316 return TOSA_OK;
317}
318
319tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const char* filename)
320{
321 std::string read_buffer;
322 tosa_err_t err;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800323 const uint8_t* buf;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700324 bool ok;
325
326 ok = flatbuffers::LoadFile(filename, false, &read_buffer);
327 if (!ok)
328 {
329 printf("Error loading flatbuffer file: %s\n", filename);
330 return TOSA_FILE_ERROR;
331 }
332
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800333 buf = reinterpret_cast<const uint8_t*>(read_buffer.data());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700334
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700335 err = Deserialize(buf);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700336 if (err != TOSA_OK)
337 {
338 return err;
339 }
340
341 return TOSA_OK;
342}
343
Aaron DeBattista8b3903a2021-11-18 16:38:11 +0000344tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const void* input, int in_size)
345{
346 tosa_err_t err;
347
348 const uint8_t* buf = (const uint8_t*)input;
349 err = Deserialize(buf);
350 if (err != TOSA_OK)
351 {
352 return err;
353 }
354
355 return TOSA_OK;
356}
357
Eric Kunze2364dcd2021-04-26 11:06:57 -0700358tosa_err_t TosaSerializationHandler::SaveFileTosaFlatbuffer(const char* filename)
359{
360 tosa_err_t err;
361
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700362 err = Serialize();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700363 if (err != TOSA_OK)
364 {
365 return err;
366 }
367
368 uint8_t* buf = _builder.GetBufferPointer();
369
370 bool ok = flatbuffers::SaveFile(filename, (const char*)buf, _builder.GetSize(), false);
371 if (!ok)
372 {
373 printf("Error saving floatbuffer file: %s\n", filename);
374 return TOSA_FILE_ERROR;
375 }
376
377 return TOSA_OK;
378}
379
380tosa_err_t TosaSerializationHandler::Clear()
381{
382 // deallocate all basic blocks
Jerry Ge13c78a62022-10-04 20:32:39 -0700383 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700384 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700385 delete region;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700386 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700387 _regions.clear();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700388
389 return TOSA_OK;
390}
391
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700392tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700393{
Eric Kunzee6596402022-06-09 21:27:36 +0000394 if (!TosaGraphBufferHasIdentifier(buf))
395 {
396 printf("WARNING: TOSA file does not have TOSA file identifier\n");
397 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700398 auto fb_tosa_graph = GetTosaGraph(buf);
399 auto fb_tosa_version = fb_tosa_graph->version();
Jerry Ge13c78a62022-10-04 20:32:39 -0700400 auto fb_tosa_regions = fb_tosa_graph->regions();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700401
Eric Kunze2364dcd2021-04-26 11:06:57 -0700402 TosaAttributeBase* typed_attribute = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700403 TosaSerializationOperator* new_operator = NULL;
404 TosaSerializationBasicBlock* new_block = NULL;
405 TosaSerializationTensor* new_tensor = NULL;
Jerry Ge13c78a62022-10-04 20:32:39 -0700406 TosaSerializationRegion* new_region = NULL;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700407
408 // erase container
409 Clear();
410
Kevin Chenge6563f52021-10-20 12:12:02 -0700411 TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(),
412 fb_tosa_version->_draft());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700413
Jerry Gec4733b02023-08-02 21:48:39 +0000414 TosaVersion::compat_t is_compat = TosaVersion::is_compatible(read_version, GetVersion());
Kevin Chenge6563f52021-10-20 12:12:02 -0700415 switch (is_compat)
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700416 {
Kevin Chenge6563f52021-10-20 12:12:02 -0700417 case TosaVersion::compat_t::COMPLETELY_COMPATIBLE:
418 break;
Jerry Gec4733b02023-08-02 21:48:39 +0000419 case TosaVersion::compat_t::BACKWARD_COMPATIBLE:
420 printf("WARNING: Different Tosa flatbuffer and serializer versions detected. Read Tosa flatbuffer version "
421 "%s is backward "
422 "compatible with serializer version %s\n",
Kevin Chenge6563f52021-10-20 12:12:02 -0700423 read_version.to_string().c_str(), GetVersion().to_string().c_str());
424 break;
425 case TosaVersion::compat_t::NOT_COMPATIBLE:
Jerry Gec4733b02023-08-02 21:48:39 +0000426 printf("ERROR: Read Tosa flatbuffer version %s is not compatible with serializer version %s\n",
Kevin Chenge6563f52021-10-20 12:12:02 -0700427 read_version.to_string().c_str(), GetVersion().to_string().c_str());
428 return TOSA_VERSION_MISMATCH;
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700429 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700430
Jerry Ge13c78a62022-10-04 20:32:39 -0700431 for (size_t i = 0; i < fb_tosa_regions->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700432 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700433 auto curr_region = fb_tosa_regions->Get(i);
434 auto region_name = curr_region->name()->str();
435 auto fb_tosa_blocks = curr_region->blocks();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700436
Tai Lycfcb20d2023-03-13 21:04:11 +0000437 new_region = new TosaSerializationRegion(curr_region->name()->str(), {});
Jerry Ge13c78a62022-10-04 20:32:39 -0700438 this->GetRegions().push_back(new_region);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700439
Jerry Ge13c78a62022-10-04 20:32:39 -0700440 for (size_t i = 0; i < fb_tosa_blocks->size(); i++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700441 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000442 std::vector<TosaSerializationOperator*> block_operators_container;
443 std::vector<TosaSerializationTensor*> block_tensors_container;
444 std::vector<std::string> block_inputs_container;
445 std::vector<std::string> block_outputs_container;
446
Jerry Ge13c78a62022-10-04 20:32:39 -0700447 auto curr_block = fb_tosa_blocks->Get(i);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700448
Jerry Ge13c78a62022-10-04 20:32:39 -0700449 auto block_name = curr_block->name()->str();
Eric Kunze2364dcd2021-04-26 11:06:57 -0700450
Jerry Ge13c78a62022-10-04 20:32:39 -0700451 auto fb_tosa_operators = curr_block->operators();
Jerry Ge13c78a62022-10-04 20:32:39 -0700452 for (size_t j = 0; j < fb_tosa_operators->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700453 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700454 auto curr_operator = fb_tosa_operators->Get(j);
455
456 auto operator_op = curr_operator->op();
457 auto attribute_type = curr_operator->attribute_type();
458 auto attribute = curr_operator->attribute();
459
Tai Lycfcb20d2023-03-13 21:04:11 +0000460 std::vector<std::string> operator_inputs_container;
461 std::vector<std::string> operator_outputs_container;
462
Jerry Ge13c78a62022-10-04 20:32:39 -0700463 // input tensors
464 auto operator_inputs = curr_operator->inputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700465 if (operator_inputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700466 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700467 for (size_t k = 0; k < operator_inputs->size(); k++)
468 {
469 auto curr_input = operator_inputs->Get(k);
470 operator_inputs_container.push_back(curr_input->str());
471 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700472 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700473
Jerry Ge13c78a62022-10-04 20:32:39 -0700474 // output tensors
475 auto operator_outputs = curr_operator->outputs();
Jerry Ge13c78a62022-10-04 20:32:39 -0700476 if (operator_outputs)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700477 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700478 for (size_t k = 0; k < operator_outputs->size(); k++)
479 {
480 auto curr_output = operator_outputs->Get(k);
481 operator_outputs_container.push_back(curr_output->str());
482 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700483 }
Eric Kunze2364dcd2021-04-26 11:06:57 -0700484
Jerry Ge13c78a62022-10-04 20:32:39 -0700485 switch (attribute_type)
486 {
487 case Attribute_NONE:
488 typed_attribute = new TosaNoneAttribute();
489 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700490#define DEF_ATTRIBUTE(NAME, ...) \
491 case Attribute_##NAME##Attribute: \
492 typed_attribute = new Tosa##NAME##Attribute(attribute); \
493 break;
494#include "attribute.def"
495#undef DEF_ATTRIBUTE
Jerry Ge13c78a62022-10-04 20:32:39 -0700496 default:
497 printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n",
498 EnumNamesAttribute()[attribute_type]);
499 return TOSA_INTERNAL_ERROR;
500 }
501
502 new_operator = new TosaSerializationOperator(operator_op, attribute_type, typed_attribute,
503 operator_inputs_container, operator_outputs_container);
504 if (new_operator)
505 {
506 block_operators_container.push_back(new_operator);
507 }
508 else
509 {
510 return TOSA_MEMORY_ERROR;
511 }
512
513 if (typed_attribute)
514 delete typed_attribute;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700515 }
516
Jerry Ge13c78a62022-10-04 20:32:39 -0700517 auto block_inputs = curr_block->inputs();
518 auto block_outputs = curr_block->outputs();
519
Jerry Ge13c78a62022-10-04 20:32:39 -0700520 for (size_t j = 0; j < block_inputs->size(); j++)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700521 {
Jerry Ge13c78a62022-10-04 20:32:39 -0700522 auto curr_block_input = block_inputs->Get(j);
523 block_inputs_container.push_back(curr_block_input->str());
524 }
525 for (size_t j = 0; j < block_outputs->size(); j++)
526 {
527 auto curr_block_output = block_outputs->Get(j);
528 block_outputs_container.push_back(curr_block_output->str());
529 }
530
531 auto fb_tosa_tensors = curr_block->tensors();
Jerry Ge13c78a62022-10-04 20:32:39 -0700532 for (size_t j = 0; j < fb_tosa_tensors->size(); j++)
533 {
534 auto curr_tensor = fb_tosa_tensors->Get(j);
535
Tai Ly5917fc72023-09-21 19:33:12 +0000536 auto tensor_name = curr_tensor->name();
537 auto tensor_shape = curr_tensor->shape();
538 auto tensor_type = curr_tensor->type();
539 auto tensor_variable = curr_tensor->variable();
540 auto tensor_data = curr_tensor->data();
541 auto tensor_is_unranked = curr_tensor->is_unranked();
542 auto tensor_variable_name = curr_tensor->variable_name();
Jerry Ge13c78a62022-10-04 20:32:39 -0700543
Tai Lyc6939a42023-08-21 17:00:29 +0000544 new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data,
Tai Ly5917fc72023-09-21 19:33:12 +0000545 tensor_variable, tensor_is_unranked, tensor_variable_name);
Jerry Ge13c78a62022-10-04 20:32:39 -0700546 if (new_tensor)
547 {
548 block_tensors_container.push_back(new_tensor);
549 }
550 else
551 {
552 return TOSA_MEMORY_ERROR;
553 }
554 }
555 new_block = new TosaSerializationBasicBlock(block_name, region_name, block_operators_container,
556 block_tensors_container, block_inputs_container,
557 block_outputs_container);
558 if (new_block)
559 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000560 new_region->GetBlocks().push_back(new_block);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700561 }
562 else
563 {
564 return TOSA_MEMORY_ERROR;
565 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700566 } // end block for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700567 }
568
569 return TOSA_OK;
570}
571
James Ward80905bb2023-01-25 15:51:27 +0000572std::vector<uint8_t> float_to_u8_helper(float f_in)
James Wardc15f7d52022-12-07 15:38:01 +0000573{
James Ward80905bb2023-01-25 15:51:27 +0000574 // Push back a single float value to the buffer with *NO PADDING*
575 // Therefore ConvertF32toU8 function not used
James Wardc15f7d52022-12-07 15:38:01 +0000576 std::vector<uint8_t> u8_out;
James Ward80905bb2023-01-25 15:51:27 +0000577 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&f_in);
578 u8_out.push_back(*val_u32 & 0xFF);
579 u8_out.push_back((*val_u32 >> 8) & 0xFF);
580 u8_out.push_back((*val_u32 >> 16) & 0xFF);
581 u8_out.push_back((*val_u32 >> 24) & 0xFF);
James Wardc15f7d52022-12-07 15:38:01 +0000582 return u8_out;
583}
584
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700585tosa_err_t TosaSerializationHandler::Serialize()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700586{
Jerry Ge13c78a62022-10-04 20:32:39 -0700587 // regions
588 std::vector<flatbuffers::Offset<TosaRegion>> fboffset_regions;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700589
Eric Kunze2364dcd2021-04-26 11:06:57 -0700590 // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator>
Jerry Ge13c78a62022-10-04 20:32:39 -0700591 for (auto region : GetRegions())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700592 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000593 std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks;
Jerry Ge13c78a62022-10-04 20:32:39 -0700594 for (auto block : region->GetBlocks())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700595 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000596 std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators;
597 std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors;
598 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs;
599 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700600 auto block_name = _builder.CreateString(block->GetName().c_str());
601 for (auto tensor_str : block->GetInputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700602 {
603 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700604 fboffset_block_inputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700605 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700606 for (auto tensor_str : block->GetOutputs())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700607 {
608 auto tensor_name = _builder.CreateString(tensor_str.c_str());
Jerry Ge13c78a62022-10-04 20:32:39 -0700609 fboffset_block_outputs.push_back(tensor_name);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700610 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700611 auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs);
612 auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs);
613 for (auto op : block->GetOperators())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700614 {
Tai Lycfcb20d2023-03-13 21:04:11 +0000615 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs;
616 std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs;
Jerry Ge13c78a62022-10-04 20:32:39 -0700617 auto operator_op = op->GetOp();
618 auto attribute_type = op->GetAttributeType();
619 for (auto tensor_str : op->GetInputTensorNames())
620 {
621 auto tensor_name = _builder.CreateString(tensor_str.c_str());
622 fboffset_operator_inputs.push_back(tensor_name);
623 }
624 for (auto tensor_str : op->GetOutputTensorNames())
625 {
626 auto tensor_name = _builder.CreateString(tensor_str.c_str());
627 fboffset_operator_outputs.push_back(tensor_name);
628 }
629 auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs);
630 auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs);
631 flatbuffers::Offset<void> fb_attribute;
632 switch (attribute_type)
633 {
634 case Attribute_NONE:
635 fb_attribute = 0;
636 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700637#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 +0000638#define DEF_ARGS_S_FP_as_U8(NAME, V) \
James Ward80905bb2023-01-25 15:51:27 +0000639 , _builder.CreateVector<uint8_t>(float_to_u8_helper(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()))
Eric Kunze2364dcd2021-04-26 11:06:57 -0700640#define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()
Eric Kunze2364dcd2021-04-26 11:06:57 -0700641#define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Wardc15f7d52022-12-07 15:38:01 +0000642#define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_FP_as_U8(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700643#define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
644#define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
James Ward485a11d2022-08-05 13:48:37 +0100645#define DEF_ARGS_S_DType(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700646#define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700647#define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V)
648#define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())
Eric Kunze2364dcd2021-04-26 11:06:57 -0700649#define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0)
650#define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1)
651#define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
652 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2)
653#define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
654 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3)
655#define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
656 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
657 DEF_ARGS_##F4(NAME, T4, V4)
658#define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
659 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
660 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5)
661#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) \
662 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
663 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6)
Eric Kunze9601cbd2023-08-17 20:44:39 +0000664#define DEF_ARGS_8(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6, T7, F7, \
665 V7) \
666 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
667 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
668 DEF_ARGS_##F7(NAME, T7, V7)
669#define DEF_ARGS_9(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6, T7, F7, \
670 V7, T8, F8, V8) \
671 DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \
672 DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) \
673 DEF_ARGS_##F7(NAME, T7, V7) DEF_ARGS_##F8(NAME, T8, V8)
Eric Kunze2364dcd2021-04-26 11:06:57 -0700674#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
675 case Attribute_##NAME##Attribute: \
676 fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \
677 break;
Eric Kunze2364dcd2021-04-26 11:06:57 -0700678#include "attribute.def"
679#undef DEF_ATTRIBUTE
680#undef DEF_ARGS_1
681#undef DEF_ARGS_2
682#undef DEF_ARGS_3
683#undef DEF_ARGS_4
684#undef DEF_ARGS_5
685#undef DEF_ARGS_6
686#undef DEF_ARGS_7
687#undef DEF_ARGS_S
688#undef DEF_ARGS_V
689#undef DEF_ARGS_S_int32_t
690#undef DEF_ARGS_S_float
691#undef DEF_ARGS_S_bool
692#undef DEF_ARGS_S_ResizeMode
James Ward485a11d2022-08-05 13:48:37 +0100693#undef DEF_ARGS_S_DType
Eric Kunze2364dcd2021-04-26 11:06:57 -0700694#undef DEF_ARGS_S_string
695#undef DEF_ARGS_S_STR
696#undef DEF_ARGS_S_DEFAULT
Jerry Ge13c78a62022-10-04 20:32:39 -0700697 default:
698 printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n",
699 EnumNamesAttribute()[attribute_type]);
700 return TOSA_INTERNAL_ERROR;
701 }
702 auto fboffset_operator = CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute,
703 fb_operator_inputs, fb_operator_outputs);
704 fboffset_block_operators.push_back(fboffset_operator);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700705 }
Jerry Ge13c78a62022-10-04 20:32:39 -0700706 auto fb_block_operators = _builder.CreateVector(fboffset_block_operators);
707 for (auto tensor : block->GetTensors())
708 {
Tai Lyd0520b92023-09-19 21:30:18 +0000709 auto tensor_name = _builder.CreateString(tensor->GetName().c_str());
710 auto tensor_shape = _builder.CreateVector(tensor->GetShape());
711 auto tensor_dtype = tensor->GetDtype();
712 bool tensor_variable = tensor->GetVariable();
713 auto tensor_data = _builder.CreateVector(tensor->GetData());
714 auto tensor_is_unranked = tensor->GetIsUnranked();
715 auto tensor_variable_name = _builder.CreateString(tensor->GetVariableName().c_str());
Tai Lyc6939a42023-08-21 17:00:29 +0000716 auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data,
Tai Lyd0520b92023-09-19 21:30:18 +0000717 tensor_variable, tensor_is_unranked, tensor_variable_name);
Jerry Ge13c78a62022-10-04 20:32:39 -0700718 fboffset_block_tensors.push_back(fboffset_tensor);
719 }
720 auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors);
721 auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors,
Won Jeoncb4bbf42023-08-10 08:50:15 +0000722 fb_block_inputs, fb_block_outputs);
Jerry Ge13c78a62022-10-04 20:32:39 -0700723 fboffset_blocks.push_back(fboffset_block);
724 } // end block for_loop
725 auto fb_blocks = _builder.CreateVector(fboffset_blocks);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700726
Jerry Ge13c78a62022-10-04 20:32:39 -0700727 auto region_name = _builder.CreateString(region->GetName().c_str());
728 auto fboffset_region = CreateTosaRegion(_builder, region_name, fb_blocks);
729 fboffset_regions.push_back(fboffset_region);
730 } // end region for_loop
Eric Kunze2364dcd2021-04-26 11:06:57 -0700731
Jerry Ge13c78a62022-10-04 20:32:39 -0700732 auto fb_regions = _builder.CreateVector(fboffset_regions);
Eric Kunze2364dcd2021-04-26 11:06:57 -0700733
Kevin Chengb97cb1d2021-10-14 11:53:39 -0700734 auto fb_version =
735 CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT);
Jerry Ge13c78a62022-10-04 20:32:39 -0700736 auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_regions);
Eric Kunzee6596402022-06-09 21:27:36 +0000737 _builder.Finish(fb_graph, TosaGraphIdentifier());
Eric Kunze2364dcd2021-04-26 11:06:57 -0700738
739 return TOSA_OK;
740}
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700741
Jerry Ge442261b2022-09-09 13:38:56 -0700742void TosaSerializationHandler::ForceAlignTensorData(std::vector<uint8_t>& buf)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700743{
744 while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0)
745 {
746 buf.push_back(0);
747 }
748}
749
James Ward485a11d2022-08-05 13:48:37 +0100750tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
751{
752 // Note: Converts fp32->fp16 before converting to uint8_t
753 out.clear();
754 for (auto val : in)
755 {
756 half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val);
757 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16);
758 out.push_back(*val_u16 & 0xFF);
759 out.push_back((*val_u16 >> 8) & 0xFF);
760 }
Jerry Ge442261b2022-09-09 13:38:56 -0700761 ForceAlignTensorData(out);
James Ward485a11d2022-08-05 13:48:37 +0100762 return TOSA_OK;
763}
764
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700765tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
766{
767 out.clear();
768 for (auto val : in)
769 {
770 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
771 out.push_back(*val_u32 & 0xFF);
772 out.push_back((*val_u32 >> 8) & 0xFF);
773 out.push_back((*val_u32 >> 16) & 0xFF);
774 out.push_back((*val_u32 >> 24) & 0xFF);
775 }
Jerry Ge442261b2022-09-09 13:38:56 -0700776 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700777 return TOSA_OK;
778}
779
Tai Ly5d580fa2023-12-15 20:34:51 +0000780tosa_err_t TosaSerializationHandler::ConvertI64toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
781{
782 out.clear();
783 for (auto val : in)
784 {
785 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
786 out.push_back(*val_u64 & 0xFF);
787 out.push_back((*val_u64 >> 8) & 0xFF);
788 out.push_back((*val_u64 >> 16) & 0xFF);
789 out.push_back((*val_u64 >> 24) & 0xFF);
790 out.push_back((*val_u64 >> 32) & 0xFF);
791 out.push_back((*val_u64 >> 40) & 0xFF);
792 out.push_back((*val_u64 >> 48) & 0xFF);
793 out.push_back((*val_u64 >> 56) & 0xFF);
794 }
795 ForceAlignTensorData(out);
796 return TOSA_OK;
797}
798
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700799tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
800{
801 out.clear();
802 for (auto val : in)
803 {
804 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
805 out.push_back(*val_u64 & 0xFF);
806 out.push_back((*val_u64 >> 8) & 0xFF);
807 out.push_back((*val_u64 >> 16) & 0xFF);
808 out.push_back((*val_u64 >> 24) & 0xFF);
809 out.push_back((*val_u64 >> 32) & 0xFF);
810 out.push_back((*val_u64 >> 40) & 0xFF);
811 }
Jerry Ge442261b2022-09-09 13:38:56 -0700812 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700813 return TOSA_OK;
814}
815
816tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
817{
818 out.clear();
819 for (auto val : in)
820 {
821 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
822 out.push_back(*val_u32 & 0xFF);
823 out.push_back((*val_u32 >> 8) & 0xFF);
824 out.push_back((*val_u32 >> 16) & 0xFF);
825 out.push_back((*val_u32 >> 24) & 0xFF);
826 }
Jerry Ge442261b2022-09-09 13:38:56 -0700827 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700828 return TOSA_OK;
829}
830
831tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
832{
833 out.clear();
834 for (auto val : in)
835 {
836 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
837 out.push_back(*val_u16 & 0xFF);
838 out.push_back((*val_u16 >> 8) & 0xFF);
839 }
Jerry Ge442261b2022-09-09 13:38:56 -0700840 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700841 return TOSA_OK;
842}
843
844tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
845{
846 out.clear();
847 for (auto val : in)
848 {
849 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
850 out.push_back(*val_u8);
851 }
Jerry Ge442261b2022-09-09 13:38:56 -0700852 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700853 return TOSA_OK;
854}
855
Kevin Cheng3ce56342021-07-28 13:42:29 -0700856// Two int4 values are packed into one byte out.
857// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
858// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
859tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
860{
861 out.clear();
862 uint32_t in_size = in.size();
863 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800864 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700865 {
866 int8_t val_0 = in[2 * i];
867 int8_t val_1 = 0;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800868 if (2u * i + 1u < in_size)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700869 {
870 val_1 = in[2 * i + 1];
871 }
872 // In TOSA spec, int4 ranges [-7, 7]
873 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
874 {
875 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
876 val_0, val_1);
877 return TOSA_USER_ERROR;
878 }
879 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
880 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
881 out.push_back(val_u8);
882 }
Jerry Ge442261b2022-09-09 13:38:56 -0700883 ForceAlignTensorData(out);
Kevin Cheng3ce56342021-07-28 13:42:29 -0700884 return TOSA_OK;
885}
886
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700887tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
888{
889 out.clear();
890 for (auto val : in)
891 {
Eric Kunze4417b422022-06-20 07:27:42 -0700892 uint8_t val_u8 = val;
893 out.push_back(val_u8);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700894 }
Jerry Ge442261b2022-09-09 13:38:56 -0700895 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700896 return TOSA_OK;
897}
898
899tosa_err_t
James Ward485a11d2022-08-05 13:48:37 +0100900 TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
901{
902 // Note: fp16 values returned in fp32 type
903 out.clear();
904 if (in.size() < out_size * sizeof(int16_t))
905 {
906 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
907 out_size * sizeof(int16_t));
908 return TOSA_USER_ERROR;
909 }
910
911 for (uint32_t i = 0; i < out_size; i++)
912 {
913 uint16_t f16_byte0 = in[i * sizeof(int16_t)];
914 uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1];
915 uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8);
916
917 // Reinterpret u16 byte as fp16 then convert to fp32
918 half_float::half val_f16 = *(half_float::half*)&val_u16;
919 float val_fp32 = half_float::half_cast<float, half_float::half>(val_f16);
920 out.push_back(val_fp32);
921 }
922 return TOSA_OK;
923}
924
925tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700926 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
927{
928 out.clear();
929 if (in.size() < out_size * sizeof(float))
930 {
931 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
932 out_size * sizeof(float));
933 return TOSA_USER_ERROR;
934 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800935 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700936 {
937 uint32_t byte0 = in[i * sizeof(float)];
938 uint32_t byte1 = in[i * sizeof(float) + 1];
939 uint32_t byte2 = in[i * sizeof(float) + 2];
940 uint32_t byte3 = in[i * sizeof(float) + 3];
941 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
942 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
943 out.push_back(*val_fp32);
944 }
945 return TOSA_OK;
946}
947
Tai Ly5d580fa2023-12-15 20:34:51 +0000948tosa_err_t TosaSerializationHandler::ConvertU8toI64(const std::vector<uint8_t>& in,
949 uint32_t out_size,
950 std::vector<int64_t>& out)
951{
952 out.clear();
953 if (in.size() < out_size * sizeof(int64_t))
954 {
955 printf("TosaSerializationHandler::ConvertU8toI64(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
956 out_size * sizeof(int64_t));
957 return TOSA_USER_ERROR;
958 }
959 for (uint32_t i = 0; i < out_size; i++)
960 {
961 uint64_t byte0 = in[i * sizeof(int64_t)];
962 uint64_t byte1 = in[i * sizeof(int64_t) + 1];
963 uint64_t byte2 = in[i * sizeof(int64_t) + 2];
964 uint64_t byte3 = in[i * sizeof(int64_t) + 3];
965 uint64_t byte4 = in[i * sizeof(int64_t) + 4];
966 uint64_t byte5 = in[i * sizeof(int64_t) + 5];
967 uint64_t byte6 = in[i * sizeof(int64_t) + 6];
968 uint64_t byte7 = in[i * sizeof(int64_t) + 7];
969 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40) +
970 (byte6 << 48) + (byte7 << 56);
971 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
972 out.push_back(*val_i64);
973 }
974 return TOSA_OK;
975}
976
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700977tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
978 uint32_t out_size,
979 std::vector<int64_t>& out)
980{
981 out.clear();
982 if (in.size() < out_size * 6 /* sizeof(int48) */)
983 {
984 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
985 out_size * 6);
986 return TOSA_USER_ERROR;
987 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800988 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700989 {
990 uint64_t byte0 = in[i * 6];
991 uint64_t byte1 = in[i * 6 + 1];
992 uint64_t byte2 = in[i * 6 + 2];
993 uint64_t byte3 = in[i * 6 + 3];
994 uint64_t byte4 = in[i * 6 + 4];
995 uint64_t byte5 = in[i * 6 + 5];
996 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
997 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
998 if (sign)
999 {
1000 uint64_t sext_mask = (0xFFFFUL << 48);
1001 val_u64 |= sext_mask;
1002 }
1003 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
1004 out.push_back(*val_i64);
1005 }
1006 return TOSA_OK;
1007}
1008
1009tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
1010 uint32_t out_size,
1011 std::vector<int32_t>& out)
1012{
1013 out.clear();
1014 if (in.size() < out_size * sizeof(int32_t))
1015 {
1016 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1017 out_size * sizeof(int32_t));
1018 return TOSA_USER_ERROR;
1019 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001020 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001021 {
1022 uint32_t byte0 = in[i * sizeof(int32_t)];
1023 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
1024 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
1025 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
1026 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
1027 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
1028 out.push_back(*val_i32);
1029 }
1030 return TOSA_OK;
1031}
1032
1033tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
1034 uint32_t out_size,
1035 std::vector<int16_t>& out)
1036{
1037 out.clear();
1038 if (in.size() < out_size * sizeof(int16_t))
1039 {
1040 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1041 out_size * sizeof(int16_t));
1042 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 uint16_t byte0 = in[i * sizeof(int16_t)];
1047 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
1048 uint16_t val_u16 = byte0 + (byte1 << 8);
1049 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
1050 out.push_back(*val_i16);
1051 }
1052 return TOSA_OK;
1053}
1054
1055tosa_err_t
1056 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1057{
1058 out.clear();
1059 if (in.size() < out_size * sizeof(int8_t))
1060 {
1061 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -07001062 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001063 return TOSA_USER_ERROR;
1064 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001065 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001066 {
1067 uint8_t val_u8 = in[i];
1068 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
1069 out.push_back(*val_i8);
1070 }
1071 return TOSA_OK;
1072}
1073
1074tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -07001075 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1076{
1077 out.clear();
1078 if (out_size > in.size() * 2)
1079 {
1080 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
1081 out_size, in.size());
1082 return TOSA_USER_ERROR;
1083 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001084 for (size_t i = 0; i < in.size(); i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -07001085 {
1086 uint8_t val_u8 = in[i];
1087 uint8_t val_0_u4 = val_u8 & 0xF;
1088 uint8_t val_1_u4 = val_u8 >> 4;
1089 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1090 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1091 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1092 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1093 // In TOSA spec, int4 ranges [-7, 7]
1094 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1095 {
1096 printf(
1097 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1098 val_0, val_1);
1099 return TOSA_USER_ERROR;
1100 }
1101 out.push_back(val_0);
1102 if (2 * i + 1 < out_size)
1103 out.push_back(val_1);
1104 }
1105 return TOSA_OK;
1106}
1107
1108tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001109 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1110{
1111 out.clear();
1112 if (in.size() < out_size * sizeof(bool))
1113 {
1114 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1115 in.size(), out_size * sizeof(bool));
1116 return TOSA_USER_ERROR;
1117 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001118 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001119 {
1120 uint8_t val_u8 = in[i];
1121 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1122 out.push_back(*val_bool);
1123 }
1124 return TOSA_OK;
1125}