blob: 76b21982add687597f3f5e830be27656551d5a6c [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
Won Jeona8141522024-04-29 23:57:27 +0000750tosa_err_t TosaSerializationHandler::ConvertBF16toU8(const std::vector<bf16>& in, std::vector<uint8_t>& out)
Tai Lyce911a22024-03-21 17:01:14 +0000751{
752 // Note: Converts fp32->bf16 by ignoring the least significant 16 bits
753 out.clear();
754 for (auto val : in)
755 {
Won Jeona8141522024-04-29 23:57:27 +0000756 uint8_t bf16_byte0 = val.bits() & 0xFF;
757 uint8_t bf16_byte1 = (val.bits() >> 8) & 0xFF;
758 out.push_back(bf16_byte0);
759 out.push_back(bf16_byte1);
Tai Lyce911a22024-03-21 17:01:14 +0000760 }
761 ForceAlignTensorData(out);
762 return TOSA_OK;
763}
764
Won Jeona8141522024-04-29 23:57:27 +0000765tosa_err_t TosaSerializationHandler::ConvertFP8E4M3toU8(const std::vector<fp8e4m3>& in, std::vector<uint8_t>& out)
Tai Lyce911a22024-03-21 17:01:14 +0000766{
767 // Note: Converts fp32->FP8E4M3 before converting to unint8_t
768 out.clear();
769 for (auto val : in)
770 {
Won Jeona8141522024-04-29 23:57:27 +0000771 uint8_t b8 = val.bits();
Tai Lyce911a22024-03-21 17:01:14 +0000772 out.push_back(b8);
773 }
774 ForceAlignTensorData(out);
775 return TOSA_OK;
776}
777
Won Jeona8141522024-04-29 23:57:27 +0000778tosa_err_t TosaSerializationHandler::ConvertFP8E5M2toU8(const std::vector<fp8e5m2>& in, std::vector<uint8_t>& out)
Tai Lyce911a22024-03-21 17:01:14 +0000779{
780 // Note: Converts fp32->FP8E5M2 before converting to uint8_t
781 out.clear();
782 for (auto val : in)
783 {
Won Jeona8141522024-04-29 23:57:27 +0000784 uint8_t b8 = val.bits();
Tai Lyce911a22024-03-21 17:01:14 +0000785 out.push_back(b8);
786 }
787 ForceAlignTensorData(out);
788 return TOSA_OK;
789}
790
James Ward485a11d2022-08-05 13:48:37 +0100791tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
792{
793 // Note: Converts fp32->fp16 before converting to uint8_t
794 out.clear();
795 for (auto val : in)
796 {
797 half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val);
798 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16);
799 out.push_back(*val_u16 & 0xFF);
800 out.push_back((*val_u16 >> 8) & 0xFF);
801 }
Jerry Ge442261b2022-09-09 13:38:56 -0700802 ForceAlignTensorData(out);
James Ward485a11d2022-08-05 13:48:37 +0100803 return TOSA_OK;
804}
805
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700806tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out)
807{
808 out.clear();
809 for (auto val : in)
810 {
811 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
812 out.push_back(*val_u32 & 0xFF);
813 out.push_back((*val_u32 >> 8) & 0xFF);
814 out.push_back((*val_u32 >> 16) & 0xFF);
815 out.push_back((*val_u32 >> 24) & 0xFF);
816 }
Jerry Ge442261b2022-09-09 13:38:56 -0700817 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700818 return TOSA_OK;
819}
820
Tai Ly5d580fa2023-12-15 20:34:51 +0000821tosa_err_t TosaSerializationHandler::ConvertI64toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
822{
823 out.clear();
824 for (auto val : in)
825 {
826 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
827 out.push_back(*val_u64 & 0xFF);
828 out.push_back((*val_u64 >> 8) & 0xFF);
829 out.push_back((*val_u64 >> 16) & 0xFF);
830 out.push_back((*val_u64 >> 24) & 0xFF);
831 out.push_back((*val_u64 >> 32) & 0xFF);
832 out.push_back((*val_u64 >> 40) & 0xFF);
833 out.push_back((*val_u64 >> 48) & 0xFF);
834 out.push_back((*val_u64 >> 56) & 0xFF);
835 }
836 ForceAlignTensorData(out);
837 return TOSA_OK;
838}
839
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700840tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out)
841{
842 out.clear();
843 for (auto val : in)
844 {
845 uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val);
846 out.push_back(*val_u64 & 0xFF);
847 out.push_back((*val_u64 >> 8) & 0xFF);
848 out.push_back((*val_u64 >> 16) & 0xFF);
849 out.push_back((*val_u64 >> 24) & 0xFF);
850 out.push_back((*val_u64 >> 32) & 0xFF);
851 out.push_back((*val_u64 >> 40) & 0xFF);
852 }
Jerry Ge442261b2022-09-09 13:38:56 -0700853 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700854 return TOSA_OK;
855}
856
857tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out)
858{
859 out.clear();
860 for (auto val : in)
861 {
862 uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val);
863 out.push_back(*val_u32 & 0xFF);
864 out.push_back((*val_u32 >> 8) & 0xFF);
865 out.push_back((*val_u32 >> 16) & 0xFF);
866 out.push_back((*val_u32 >> 24) & 0xFF);
867 }
Jerry Ge442261b2022-09-09 13:38:56 -0700868 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700869 return TOSA_OK;
870}
871
872tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out)
873{
874 out.clear();
875 for (auto val : in)
876 {
877 uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val);
878 out.push_back(*val_u16 & 0xFF);
879 out.push_back((*val_u16 >> 8) & 0xFF);
880 }
Jerry Ge442261b2022-09-09 13:38:56 -0700881 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700882 return TOSA_OK;
883}
884
885tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
886{
887 out.clear();
888 for (auto val : in)
889 {
890 uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val);
891 out.push_back(*val_u8);
892 }
Jerry Ge442261b2022-09-09 13:38:56 -0700893 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700894 return TOSA_OK;
895}
896
Kevin Cheng3ce56342021-07-28 13:42:29 -0700897// Two int4 values are packed into one byte out.
898// For given input value val_0 = in[2*i], and val_1 = in[2*i+1],
899// they'll be packed as out[3:0] = val_0, and out[7:4] = val_1
900tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out)
901{
902 out.clear();
903 uint32_t in_size = in.size();
904 uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2);
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800905 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700906 {
907 int8_t val_0 = in[2 * i];
908 int8_t val_1 = 0;
Eric Kunzeb13fe8f2022-02-17 17:14:25 -0800909 if (2u * i + 1u < in_size)
Kevin Cheng3ce56342021-07-28 13:42:29 -0700910 {
911 val_1 = in[2 * i + 1];
912 }
913 // In TOSA spec, int4 ranges [-7, 7]
914 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
915 {
916 printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n",
917 val_0, val_1);
918 return TOSA_USER_ERROR;
919 }
920 int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4);
921 uint8_t val_u8 = static_cast<uint8_t>(val_packed);
922 out.push_back(val_u8);
923 }
Jerry Ge442261b2022-09-09 13:38:56 -0700924 ForceAlignTensorData(out);
Kevin Cheng3ce56342021-07-28 13:42:29 -0700925 return TOSA_OK;
926}
927
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700928tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out)
929{
930 out.clear();
931 for (auto val : in)
932 {
Eric Kunze4417b422022-06-20 07:27:42 -0700933 uint8_t val_u8 = val;
934 out.push_back(val_u8);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700935 }
Jerry Ge442261b2022-09-09 13:38:56 -0700936 ForceAlignTensorData(out);
Kevin Cheng3bb1bc12021-06-17 15:57:08 -0700937 return TOSA_OK;
938}
939
Won Jeona8141522024-04-29 23:57:27 +0000940tosa_err_t
941 TosaSerializationHandler::ConvertU8toBF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bf16>& out)
Tai Lyce911a22024-03-21 17:01:14 +0000942{
943 // Note: bf16 values returned in fp32 type
944 out.clear();
945 if (in.size() < out_size * sizeof(int16_t))
946 {
947 printf("TosaSerializationHandler::ConvertU8toBF16(): uint8 buffer size %ld must >= target size %ld\n",
948 in.size(), out_size * sizeof(int16_t));
949 return TOSA_USER_ERROR;
950 }
951
952 for (uint32_t i = 0; i < out_size; i++)
953 {
954 uint32_t f32_byte2 = in[i * sizeof(int16_t)];
955 uint32_t f32_byte3 = in[i * sizeof(int16_t) + 1];
956 uint32_t val_u32 = (f32_byte2 << 16) + (f32_byte3 << 24);
957
958 // Reinterpret u32 bytes as fp32
Won Jeona8141522024-04-29 23:57:27 +0000959 float val_f32 = *(float*)&val_u32;
960 float val_bf16 = static_cast<bf16>(val_f32);
961 out.push_back(val_bf16);
Tai Lyce911a22024-03-21 17:01:14 +0000962 }
963 return TOSA_OK;
964}
965
966tosa_err_t TosaSerializationHandler::ConvertU8toFP8E4M3(const std::vector<uint8_t>& in,
967 uint32_t out_size,
Won Jeona8141522024-04-29 23:57:27 +0000968 std::vector<fp8e4m3>& out)
Tai Lyce911a22024-03-21 17:01:14 +0000969{
Tai Lyce911a22024-03-21 17:01:14 +0000970 out.clear();
971 if (in.size() < out_size * sizeof(int8_t))
972 {
973 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
974 out_size * sizeof(int8_t));
975 return TOSA_USER_ERROR;
976 }
977
978 for (uint32_t i = 0; i < out_size; i++)
979 {
Won Jeona8141522024-04-29 23:57:27 +0000980 int8_t bits = static_cast<int8_t>(in[i * sizeof(int8_t)]);
981 auto f8 = fp8e4m3::from_bits(bits);
982 out.push_back(f8);
Tai Lyce911a22024-03-21 17:01:14 +0000983 }
984 return TOSA_OK;
985}
986
987tosa_err_t TosaSerializationHandler::ConvertU8toFP8E5M2(const std::vector<uint8_t>& in,
988 uint32_t out_size,
Won Jeona8141522024-04-29 23:57:27 +0000989 std::vector<fp8e5m2>& out)
Tai Lyce911a22024-03-21 17:01:14 +0000990{
991 // Note: FP8E5M2 values returned in fp32 type
992 out.clear();
993 if (in.size() < out_size * sizeof(int8_t))
994 {
995 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
996 out_size * sizeof(int8_t));
997 return TOSA_USER_ERROR;
998 }
999
1000 for (uint32_t i = 0; i < out_size; i++)
1001 {
Won Jeona8141522024-04-29 23:57:27 +00001002 int8_t bits = static_cast<int8_t>(in[i * sizeof(int8_t)]);
1003 auto f8 = fp8e5m2::from_bits(bits);
1004 out.push_back(f8);
Tai Lyce911a22024-03-21 17:01:14 +00001005 }
1006 return TOSA_OK;
1007}
1008
Jerry Ge758e73e2024-02-26 13:31:22 -08001009tosa_err_t TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in,
1010 uint32_t out_size,
1011 std::vector<half_float::half>& out)
James Ward485a11d2022-08-05 13:48:37 +01001012{
1013 // Note: fp16 values returned in fp32 type
1014 out.clear();
1015 if (in.size() < out_size * sizeof(int16_t))
1016 {
1017 printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1018 out_size * sizeof(int16_t));
1019 return TOSA_USER_ERROR;
1020 }
1021
1022 for (uint32_t i = 0; i < out_size; i++)
1023 {
1024 uint16_t f16_byte0 = in[i * sizeof(int16_t)];
1025 uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1];
1026 uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8);
1027
1028 // Reinterpret u16 byte as fp16 then convert to fp32
1029 half_float::half val_f16 = *(half_float::half*)&val_u16;
Jerry Ge758e73e2024-02-26 13:31:22 -08001030 out.push_back(val_f16);
James Ward485a11d2022-08-05 13:48:37 +01001031 }
1032 return TOSA_OK;
1033}
1034
1035tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001036 TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
1037{
1038 out.clear();
1039 if (in.size() < out_size * sizeof(float))
1040 {
1041 printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1042 out_size * sizeof(float));
1043 return TOSA_USER_ERROR;
1044 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001045 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001046 {
1047 uint32_t byte0 = in[i * sizeof(float)];
1048 uint32_t byte1 = in[i * sizeof(float) + 1];
1049 uint32_t byte2 = in[i * sizeof(float) + 2];
1050 uint32_t byte3 = in[i * sizeof(float) + 3];
1051 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
1052 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
1053 out.push_back(*val_fp32);
1054 }
1055 return TOSA_OK;
1056}
1057
Tai Ly5d580fa2023-12-15 20:34:51 +00001058tosa_err_t TosaSerializationHandler::ConvertU8toI64(const std::vector<uint8_t>& in,
1059 uint32_t out_size,
1060 std::vector<int64_t>& out)
1061{
1062 out.clear();
1063 if (in.size() < out_size * sizeof(int64_t))
1064 {
1065 printf("TosaSerializationHandler::ConvertU8toI64(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1066 out_size * sizeof(int64_t));
1067 return TOSA_USER_ERROR;
1068 }
1069 for (uint32_t i = 0; i < out_size; i++)
1070 {
1071 uint64_t byte0 = in[i * sizeof(int64_t)];
1072 uint64_t byte1 = in[i * sizeof(int64_t) + 1];
1073 uint64_t byte2 = in[i * sizeof(int64_t) + 2];
1074 uint64_t byte3 = in[i * sizeof(int64_t) + 3];
1075 uint64_t byte4 = in[i * sizeof(int64_t) + 4];
1076 uint64_t byte5 = in[i * sizeof(int64_t) + 5];
1077 uint64_t byte6 = in[i * sizeof(int64_t) + 6];
1078 uint64_t byte7 = in[i * sizeof(int64_t) + 7];
1079 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40) +
1080 (byte6 << 48) + (byte7 << 56);
1081 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
1082 out.push_back(*val_i64);
1083 }
1084 return TOSA_OK;
1085}
1086
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001087tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in,
1088 uint32_t out_size,
1089 std::vector<int64_t>& out)
1090{
1091 out.clear();
1092 if (in.size() < out_size * 6 /* sizeof(int48) */)
1093 {
1094 printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(),
1095 out_size * 6);
1096 return TOSA_USER_ERROR;
1097 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001098 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001099 {
1100 uint64_t byte0 = in[i * 6];
1101 uint64_t byte1 = in[i * 6 + 1];
1102 uint64_t byte2 = in[i * 6 + 2];
1103 uint64_t byte3 = in[i * 6 + 3];
1104 uint64_t byte4 = in[i * 6 + 4];
1105 uint64_t byte5 = in[i * 6 + 5];
1106 bool sign = ((byte5 >> 7) & 1) == 1 ? true : false;
1107 uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40);
1108 if (sign)
1109 {
1110 uint64_t sext_mask = (0xFFFFUL << 48);
1111 val_u64 |= sext_mask;
1112 }
1113 int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64);
1114 out.push_back(*val_i64);
1115 }
1116 return TOSA_OK;
1117}
1118
1119tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in,
1120 uint32_t out_size,
1121 std::vector<int32_t>& out)
1122{
1123 out.clear();
1124 if (in.size() < out_size * sizeof(int32_t))
1125 {
1126 printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1127 out_size * sizeof(int32_t));
1128 return TOSA_USER_ERROR;
1129 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001130 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001131 {
1132 uint32_t byte0 = in[i * sizeof(int32_t)];
1133 uint32_t byte1 = in[i * sizeof(int32_t) + 1];
1134 uint32_t byte2 = in[i * sizeof(int32_t) + 2];
1135 uint32_t byte3 = in[i * sizeof(int32_t) + 3];
1136 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
1137 int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32);
1138 out.push_back(*val_i32);
1139 }
1140 return TOSA_OK;
1141}
1142
1143tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in,
1144 uint32_t out_size,
1145 std::vector<int16_t>& out)
1146{
1147 out.clear();
1148 if (in.size() < out_size * sizeof(int16_t))
1149 {
1150 printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
1151 out_size * sizeof(int16_t));
1152 return TOSA_USER_ERROR;
1153 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001154 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001155 {
1156 uint16_t byte0 = in[i * sizeof(int16_t)];
1157 uint16_t byte1 = in[i * sizeof(int16_t) + 1];
1158 uint16_t val_u16 = byte0 + (byte1 << 8);
1159 int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16);
1160 out.push_back(*val_i16);
1161 }
1162 return TOSA_OK;
1163}
1164
1165tosa_err_t
1166 TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1167{
1168 out.clear();
1169 if (in.size() < out_size * sizeof(int8_t))
1170 {
1171 printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(),
Kevin Cheng3ce56342021-07-28 13:42:29 -07001172 out_size * sizeof(int8_t));
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001173 return TOSA_USER_ERROR;
1174 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001175 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001176 {
1177 uint8_t val_u8 = in[i];
1178 int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8);
1179 out.push_back(*val_i8);
1180 }
1181 return TOSA_OK;
1182}
1183
1184tosa_err_t
Kevin Cheng3ce56342021-07-28 13:42:29 -07001185 TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out)
1186{
1187 out.clear();
1188 if (out_size > in.size() * 2)
1189 {
1190 printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n",
1191 out_size, in.size());
1192 return TOSA_USER_ERROR;
1193 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001194 for (size_t i = 0; i < in.size(); i++)
Kevin Cheng3ce56342021-07-28 13:42:29 -07001195 {
1196 uint8_t val_u8 = in[i];
1197 uint8_t val_0_u4 = val_u8 & 0xF;
1198 uint8_t val_1_u4 = val_u8 >> 4;
1199 uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4;
1200 uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4;
1201 int8_t val_0 = static_cast<int8_t>(val_0_u8_sext);
1202 int8_t val_1 = static_cast<int8_t>(val_1_u8_sext);
1203 // In TOSA spec, int4 ranges [-7, 7]
1204 if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7)
1205 {
1206 printf(
1207 "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n",
1208 val_0, val_1);
1209 return TOSA_USER_ERROR;
1210 }
1211 out.push_back(val_0);
1212 if (2 * i + 1 < out_size)
1213 out.push_back(val_1);
1214 }
1215 return TOSA_OK;
1216}
1217
1218tosa_err_t
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001219 TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out)
1220{
1221 out.clear();
1222 if (in.size() < out_size * sizeof(bool))
1223 {
1224 printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n",
1225 in.size(), out_size * sizeof(bool));
1226 return TOSA_USER_ERROR;
1227 }
Eric Kunzeb13fe8f2022-02-17 17:14:25 -08001228 for (uint32_t i = 0; i < out_size; i++)
Kevin Cheng3bb1bc12021-06-17 15:57:08 -07001229 {
1230 uint8_t val_u8 = in[i];
1231 bool* val_bool = reinterpret_cast<bool*>(&val_u8);
1232 out.push_back(*val_bool);
1233 }
1234 return TOSA_OK;
1235}