Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 1 | |
| 2 | // Copyright (c) 2020-2021, ARM Limited. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include "tosa_serialization_handler.h" |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 17 | #include "half.hpp" |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 18 | |
| 19 | #include <iostream> |
| 20 | using namespace tosa; |
| 21 | |
| 22 | TosaSerializationTensor::TosaSerializationTensor(const flatbuffers::String* name, |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 23 | const flatbuffers::Vector<int32_t>* shape, |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 24 | DType dtype, |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 25 | const flatbuffers::Vector<uint8_t>* data) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 26 | { |
| 27 | _dtype = dtype; |
| 28 | |
Jerry Ge | b413a95 | 2023-05-08 19:17:22 +0000 | [diff] [blame^] | 29 | if (shape) |
| 30 | { |
| 31 | std::copy(shape->begin(), shape->end(), std::back_inserter(_shape)); |
| 32 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 33 | |
| 34 | assert(name); |
| 35 | _name = name->str(); |
| 36 | |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 37 | if (data) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 38 | { |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 39 | std::copy(data->begin(), data->end(), std::back_inserter(_data)); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 43 | TosaSerializationTensor::TosaSerializationTensor(const std::string& name, |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 44 | const std::vector<int32_t>& shape, |
| 45 | DType dtype, |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 46 | const std::vector<uint8_t>& data) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 47 | { |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 48 | _dtype = dtype; |
| 49 | _shape = shape; |
| 50 | _name = name; |
| 51 | _data = data; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TosaSerializationTensor::TosaSerializationTensor() |
| 55 | { |
| 56 | _dtype = DType_UNKNOWN; |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 57 | _name = "UNKNOWN"; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TosaSerializationTensor::~TosaSerializationTensor() |
| 61 | {} |
| 62 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 63 | void TosaSerializationOperator::InitializeAttribute(Attribute attribute_type, const TosaAttributeBase* attribute) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 64 | { |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 65 | _attribute_type = attribute_type; |
| 66 | |
| 67 | switch (attribute_type) |
| 68 | { |
| 69 | case Attribute_NONE: |
| 70 | _attribute = new TosaNoneAttribute(); |
| 71 | break; |
| 72 | #define DEF_ATTRIBUTE(NAME, ...) \ |
| 73 | case Attribute_##NAME##Attribute: \ |
| 74 | _attribute = new Tosa##NAME##Attribute(attribute); \ |
| 75 | break; |
| 76 | #include "attribute.def" |
| 77 | #undef DEF_ATTRIBUTE |
| 78 | default: |
| 79 | printf("TosaSerializationOperator::TosaSerializationOperator(): Attribute %s not implemented yet\n", |
| 80 | EnumNamesAttribute()[attribute_type]); |
| 81 | assert(0); |
| 82 | } |
| 83 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 84 | assert(_attribute); |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 85 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 86 | |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 87 | TosaSerializationOperator::TosaSerializationOperator(Op op, |
| 88 | Attribute attribute_type, |
| 89 | const TosaAttributeBase* attribute, |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 90 | const std::vector<std::string>& input_tensor_names, |
| 91 | const std::vector<std::string>& output_tensor_names) |
| 92 | { |
| 93 | _op = op; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 94 | _input_tensor_names = input_tensor_names; |
| 95 | _output_tensor_names = output_tensor_names; |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 96 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 97 | InitializeAttribute(attribute_type, attribute); |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | TosaSerializationOperator::TosaSerializationOperator(Op op, |
| 101 | Attribute attribute_type, |
| 102 | const TosaAttributeBase* attribute, |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 103 | std::vector<std::string>&& input_tensor_names, |
| 104 | std::vector<std::string>&& output_tensor_names) |
| 105 | { |
| 106 | _op = op; |
| 107 | _input_tensor_names = std::move(input_tensor_names); |
| 108 | _output_tensor_names = std::move(output_tensor_names); |
| 109 | |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 110 | InitializeAttribute(attribute_type, attribute); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TosaSerializationOperator::~TosaSerializationOperator() |
| 114 | { |
| 115 | delete _attribute; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 118 | TosaSerializationBasicBlock::TosaSerializationBasicBlock(const std::string& name, |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 119 | const std::string& region_name, |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 120 | const std::vector<TosaSerializationOperator*>& operators, |
| 121 | const std::vector<TosaSerializationTensor*>& tensors, |
| 122 | const std::vector<std::string>& inputs, |
| 123 | const std::vector<std::string>& outputs) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 124 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 125 | _name = name; |
| 126 | _region_name = region_name; |
| 127 | _operators = operators; |
| 128 | _tensors = tensors; |
| 129 | _inputs = inputs; |
| 130 | _outputs = outputs; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 133 | TosaSerializationBasicBlock::TosaSerializationBasicBlock(std::string&& name, |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 134 | std::string&& region_name, |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 135 | std::vector<TosaSerializationOperator*>&& operators, |
| 136 | std::vector<TosaSerializationTensor*>&& tensors, |
| 137 | std::vector<std::string>&& inputs, |
| 138 | std::vector<std::string>&& outputs) |
| 139 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 140 | _name = std::move(name); |
| 141 | _region_name = std::move(region_name); |
| 142 | _operators = std::move(operators); |
| 143 | _tensors = std::move(tensors); |
| 144 | _inputs = std::move(inputs); |
| 145 | _outputs = std::move(outputs); |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 148 | TosaSerializationBasicBlock::~TosaSerializationBasicBlock() |
| 149 | { |
| 150 | // deallocate all operators |
| 151 | for (auto op : GetOperators()) |
| 152 | { |
| 153 | delete op; // ~TosaSerializationOperator() |
| 154 | } |
| 155 | |
| 156 | // deallocate all tensors |
| 157 | for (auto ts : GetTensors()) |
| 158 | { |
| 159 | delete ts; // ~TosaSerializationTensor() |
| 160 | } |
| 161 | } |
| 162 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 163 | TosaSerializationRegion::TosaSerializationRegion(const std::string& name, |
| 164 | const std::vector<TosaSerializationBasicBlock*>& blocks) |
| 165 | { |
| 166 | _name = name; |
| 167 | _blocks = blocks; |
| 168 | } |
| 169 | |
| 170 | TosaSerializationRegion::TosaSerializationRegion(const std::string&& name, |
| 171 | const std::vector<TosaSerializationBasicBlock*>&& blocks) |
| 172 | { |
| 173 | _name = std::move(name); |
| 174 | _blocks = std::move(blocks); |
| 175 | } |
| 176 | |
| 177 | TosaSerializationRegion::~TosaSerializationRegion() |
| 178 | { |
| 179 | // deallocate all blocks |
| 180 | for (auto block : GetBlocks()) |
| 181 | { |
| 182 | delete block; // ~TosaSerializationBasicBlock() |
| 183 | } |
| 184 | } |
| 185 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 186 | TosaSerializationHandler::TosaSerializationHandler() |
| 187 | { |
| 188 | _schemaLoaded = false; |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 189 | _version = TosaVersion(TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TosaSerializationHandler::~TosaSerializationHandler() |
| 193 | { |
| 194 | Clear(); // deallocate all basic blocks |
| 195 | } |
| 196 | |
Kevin Cheng | a81a7a1 | 2021-11-10 14:07:34 -0800 | [diff] [blame] | 197 | TosaVersion TosaSerializationHandler::ParseTosaSchemaVersion(std::string schema) |
| 198 | { |
| 199 | // Parse all 4 version fields in schema file |
| 200 | static const char* keywords[4] = { "major: int32 = ", "minor: int32 = ", "patch: int32 = ", "draft: bool = " }; |
| 201 | string keyword_str[4]; |
| 202 | size_t search_pos = 0; |
| 203 | size_t keyword_pos; |
| 204 | size_t semicolon_pos; |
| 205 | // parse integer field first |
| 206 | for (int32_t i = 0; i < 4; i++) |
| 207 | { |
| 208 | keyword_pos = schema.find(keywords[i], search_pos); |
| 209 | if (keyword_pos == std::string::npos) |
| 210 | { |
| 211 | printf("ERROR: can't find keyword \"%s\" in schema\n", keywords[i]); |
| 212 | assert(0); |
| 213 | } |
| 214 | semicolon_pos = schema.find(';', keyword_pos); |
| 215 | if (keyword_pos == std::string::npos) |
| 216 | { |
| 217 | printf("ERROR: can't find ';' in schema\n"); |
| 218 | assert(0); |
| 219 | } |
| 220 | keyword_str[i] = |
| 221 | schema.substr(keyword_pos + strlen(keywords[i]), semicolon_pos - keyword_pos - strlen(keywords[i])); |
| 222 | search_pos = semicolon_pos; |
| 223 | } |
| 224 | |
| 225 | int32_t schema_major = 0; |
| 226 | int32_t schema_minor = 0; |
| 227 | int32_t schema_patch = 0; |
| 228 | bool schema_draft = false; |
| 229 | try |
| 230 | { |
| 231 | schema_major = stoi(keyword_str[0]); |
| 232 | schema_minor = stoi(keyword_str[1]); |
| 233 | schema_patch = stoi(keyword_str[2]); |
| 234 | schema_draft = (keyword_str[3] == "true") ? true : false; |
| 235 | } |
| 236 | catch (std::invalid_argument& e) |
| 237 | { |
| 238 | printf("ERROR: fail at stoi(): %s\n", e.what()); |
| 239 | assert(0); |
| 240 | } |
| 241 | |
| 242 | TosaVersion schema_version(schema_major, schema_minor, schema_patch, schema_draft); |
| 243 | |
| 244 | return schema_version; |
| 245 | } |
| 246 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 247 | tosa_err_t TosaSerializationHandler::LoadFileSchema(const char* schema_filename) |
| 248 | { |
| 249 | std::string schema; |
| 250 | bool ok; |
| 251 | |
| 252 | ok = flatbuffers::LoadFile(schema_filename, false, &schema); |
| 253 | if (!ok) |
| 254 | { |
| 255 | printf("Error loading schema file: %s\n", schema_filename); |
| 256 | return TOSA_FILE_ERROR; |
| 257 | } |
| 258 | |
| 259 | ok = _parser.Parse(schema.c_str()); |
Kevin Cheng | a81a7a1 | 2021-11-10 14:07:34 -0800 | [diff] [blame] | 260 | |
| 261 | TosaVersion schema_version = ParseTosaSchemaVersion(schema); |
| 262 | |
| 263 | TosaVersion::compat_t is_compat = schema_version.is_compatible(GetVersion()); |
| 264 | switch (is_compat) |
| 265 | { |
| 266 | case TosaVersion::compat_t::COMPLETELY_COMPATIBLE: |
| 267 | break; |
| 268 | case TosaVersion::compat_t::PARTIALLY_COMPATIBLE: |
| 269 | printf("WARNING: Schema flatbuffer version %s is partially compatible with serializer version %s\n", |
| 270 | schema_version.to_string().c_str(), GetVersion().to_string().c_str()); |
| 271 | break; |
| 272 | case TosaVersion::compat_t::NOT_COMPATIBLE: |
| 273 | printf("ERROR: Schema flatbuffer version %s is not compatible with serializer version %s\n", |
| 274 | schema_version.to_string().c_str(), GetVersion().to_string().c_str()); |
| 275 | return TOSA_VERSION_MISMATCH; |
| 276 | } |
| 277 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 278 | if (!ok) |
| 279 | { |
| 280 | printf("Error parsing ISA schema file: %s\n", schema_filename); |
| 281 | return TOSA_FILE_ERROR; |
| 282 | } |
| 283 | _schemaLoaded = true; |
| 284 | |
| 285 | return TOSA_OK; |
| 286 | } |
| 287 | |
| 288 | tosa_err_t TosaSerializationHandler::LoadFileJson(const char* filename) |
| 289 | { |
| 290 | std::string jsonfile; |
| 291 | bool ok; |
| 292 | tosa_err_t err; |
| 293 | |
| 294 | if (!_schemaLoaded) |
| 295 | { |
| 296 | return TOSA_SCHEMA_MISSING; |
| 297 | } |
| 298 | |
| 299 | ok = flatbuffers::LoadFile(filename, false, &jsonfile); |
| 300 | if (!ok) |
| 301 | { |
| 302 | printf("Error loading json file: %s\n", filename); |
| 303 | return TOSA_FILE_ERROR; |
| 304 | } |
| 305 | |
| 306 | ok = _parser.Parse(jsonfile.c_str()); |
| 307 | if (!ok) |
| 308 | { |
| 309 | printf("Error parsing json file: %s\n", filename); |
| 310 | return TOSA_FILE_ERROR; |
| 311 | } |
| 312 | |
| 313 | uint8_t* buf = _parser.builder_.GetBufferPointer(); |
| 314 | |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 315 | err = Deserialize(buf); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 316 | if (err != TOSA_OK) |
| 317 | { |
| 318 | return err; |
| 319 | } |
| 320 | |
| 321 | return TOSA_OK; |
| 322 | } |
| 323 | |
| 324 | tosa_err_t TosaSerializationHandler::SaveFileJson(const char* filename) |
| 325 | { |
| 326 | std::string jsongen; |
| 327 | tosa_err_t err; |
| 328 | |
| 329 | if (!_schemaLoaded) |
| 330 | { |
| 331 | return TOSA_SCHEMA_MISSING; |
| 332 | } |
| 333 | |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 334 | err = Serialize(); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 335 | if (err != TOSA_OK) |
| 336 | { |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | uint8_t* buf = _builder.GetBufferPointer(); |
| 341 | |
| 342 | if (!GenerateText(_parser, buf, &jsongen)) |
| 343 | { |
| 344 | printf("Couldn't serialize parsed data to JSON!\n"); |
| 345 | return TOSA_FILE_ERROR; |
| 346 | } |
| 347 | |
| 348 | FILE* file = fopen(filename, "wb"); |
| 349 | |
| 350 | if (!file) |
| 351 | { |
| 352 | printf("Couldn't open output file: %s\n", filename); |
| 353 | return TOSA_FILE_ERROR; |
| 354 | } |
| 355 | |
| 356 | if (fwrite(jsongen.c_str(), sizeof(char), jsongen.size(), file) != jsongen.size()) |
| 357 | { |
| 358 | printf("Error writing to json output file: %s\n", filename); |
| 359 | fclose(file); |
| 360 | return TOSA_FILE_ERROR; |
| 361 | } |
| 362 | |
| 363 | if (file) |
| 364 | fclose(file); |
| 365 | |
| 366 | return TOSA_OK; |
| 367 | } |
| 368 | |
| 369 | tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const char* filename) |
| 370 | { |
| 371 | std::string read_buffer; |
| 372 | tosa_err_t err; |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 373 | const uint8_t* buf; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 374 | bool ok; |
| 375 | |
| 376 | ok = flatbuffers::LoadFile(filename, false, &read_buffer); |
| 377 | if (!ok) |
| 378 | { |
| 379 | printf("Error loading flatbuffer file: %s\n", filename); |
| 380 | return TOSA_FILE_ERROR; |
| 381 | } |
| 382 | |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 383 | buf = reinterpret_cast<const uint8_t*>(read_buffer.data()); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 384 | |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 385 | err = Deserialize(buf); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 386 | if (err != TOSA_OK) |
| 387 | { |
| 388 | return err; |
| 389 | } |
| 390 | |
| 391 | return TOSA_OK; |
| 392 | } |
| 393 | |
Aaron DeBattista | 8b3903a | 2021-11-18 16:38:11 +0000 | [diff] [blame] | 394 | tosa_err_t TosaSerializationHandler::LoadFileTosaFlatbuffer(const void* input, int in_size) |
| 395 | { |
| 396 | tosa_err_t err; |
| 397 | |
| 398 | const uint8_t* buf = (const uint8_t*)input; |
| 399 | err = Deserialize(buf); |
| 400 | if (err != TOSA_OK) |
| 401 | { |
| 402 | return err; |
| 403 | } |
| 404 | |
| 405 | return TOSA_OK; |
| 406 | } |
| 407 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 408 | tosa_err_t TosaSerializationHandler::SaveFileTosaFlatbuffer(const char* filename) |
| 409 | { |
| 410 | tosa_err_t err; |
| 411 | |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 412 | err = Serialize(); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 413 | if (err != TOSA_OK) |
| 414 | { |
| 415 | return err; |
| 416 | } |
| 417 | |
| 418 | uint8_t* buf = _builder.GetBufferPointer(); |
| 419 | |
| 420 | bool ok = flatbuffers::SaveFile(filename, (const char*)buf, _builder.GetSize(), false); |
| 421 | if (!ok) |
| 422 | { |
| 423 | printf("Error saving floatbuffer file: %s\n", filename); |
| 424 | return TOSA_FILE_ERROR; |
| 425 | } |
| 426 | |
| 427 | return TOSA_OK; |
| 428 | } |
| 429 | |
| 430 | tosa_err_t TosaSerializationHandler::Clear() |
| 431 | { |
| 432 | // deallocate all basic blocks |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 433 | for (auto region : GetRegions()) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 434 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 435 | delete region; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 436 | } |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 437 | _regions.clear(); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 438 | |
| 439 | return TOSA_OK; |
| 440 | } |
| 441 | |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 442 | tosa_err_t TosaSerializationHandler::Deserialize(const uint8_t* buf) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 443 | { |
Eric Kunze | e659640 | 2022-06-09 21:27:36 +0000 | [diff] [blame] | 444 | if (!TosaGraphBufferHasIdentifier(buf)) |
| 445 | { |
| 446 | printf("WARNING: TOSA file does not have TOSA file identifier\n"); |
| 447 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 448 | auto fb_tosa_graph = GetTosaGraph(buf); |
| 449 | auto fb_tosa_version = fb_tosa_graph->version(); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 450 | auto fb_tosa_regions = fb_tosa_graph->regions(); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 451 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 452 | TosaAttributeBase* typed_attribute = NULL; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 453 | TosaSerializationOperator* new_operator = NULL; |
| 454 | TosaSerializationBasicBlock* new_block = NULL; |
| 455 | TosaSerializationTensor* new_tensor = NULL; |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 456 | TosaSerializationRegion* new_region = NULL; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 457 | |
| 458 | // erase container |
| 459 | Clear(); |
| 460 | |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 461 | TosaVersion read_version(fb_tosa_version->_major(), fb_tosa_version->_minor(), fb_tosa_version->_patch(), |
| 462 | fb_tosa_version->_draft()); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 463 | |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 464 | TosaVersion::compat_t is_compat = read_version.is_compatible(GetVersion()); |
| 465 | switch (is_compat) |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 466 | { |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 467 | case TosaVersion::compat_t::COMPLETELY_COMPATIBLE: |
| 468 | break; |
| 469 | case TosaVersion::compat_t::PARTIALLY_COMPATIBLE: |
| 470 | printf("WARNING: Read flatbuffer version %s is partially compatible with serializer version %s\n", |
| 471 | read_version.to_string().c_str(), GetVersion().to_string().c_str()); |
| 472 | break; |
| 473 | case TosaVersion::compat_t::NOT_COMPATIBLE: |
| 474 | printf("ERROR: Read flatbuffer version %s is not compatible with serializer version %s\n", |
| 475 | read_version.to_string().c_str(), GetVersion().to_string().c_str()); |
| 476 | return TOSA_VERSION_MISMATCH; |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 477 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 478 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 479 | for (size_t i = 0; i < fb_tosa_regions->size(); i++) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 480 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 481 | auto curr_region = fb_tosa_regions->Get(i); |
| 482 | auto region_name = curr_region->name()->str(); |
| 483 | auto fb_tosa_blocks = curr_region->blocks(); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 484 | |
Tai Ly | cfcb20d | 2023-03-13 21:04:11 +0000 | [diff] [blame] | 485 | new_region = new TosaSerializationRegion(curr_region->name()->str(), {}); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 486 | this->GetRegions().push_back(new_region); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 487 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 488 | for (size_t i = 0; i < fb_tosa_blocks->size(); i++) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 489 | { |
Tai Ly | cfcb20d | 2023-03-13 21:04:11 +0000 | [diff] [blame] | 490 | std::vector<TosaSerializationOperator*> block_operators_container; |
| 491 | std::vector<TosaSerializationTensor*> block_tensors_container; |
| 492 | std::vector<std::string> block_inputs_container; |
| 493 | std::vector<std::string> block_outputs_container; |
| 494 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 495 | auto curr_block = fb_tosa_blocks->Get(i); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 496 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 497 | auto block_name = curr_block->name()->str(); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 498 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 499 | auto fb_tosa_operators = curr_block->operators(); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 500 | for (size_t j = 0; j < fb_tosa_operators->size(); j++) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 501 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 502 | auto curr_operator = fb_tosa_operators->Get(j); |
| 503 | |
| 504 | auto operator_op = curr_operator->op(); |
| 505 | auto attribute_type = curr_operator->attribute_type(); |
| 506 | auto attribute = curr_operator->attribute(); |
| 507 | |
Tai Ly | cfcb20d | 2023-03-13 21:04:11 +0000 | [diff] [blame] | 508 | std::vector<std::string> operator_inputs_container; |
| 509 | std::vector<std::string> operator_outputs_container; |
| 510 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 511 | // input tensors |
| 512 | auto operator_inputs = curr_operator->inputs(); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 513 | if (operator_inputs) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 514 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 515 | for (size_t k = 0; k < operator_inputs->size(); k++) |
| 516 | { |
| 517 | auto curr_input = operator_inputs->Get(k); |
| 518 | operator_inputs_container.push_back(curr_input->str()); |
| 519 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 520 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 521 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 522 | // output tensors |
| 523 | auto operator_outputs = curr_operator->outputs(); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 524 | if (operator_outputs) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 525 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 526 | for (size_t k = 0; k < operator_outputs->size(); k++) |
| 527 | { |
| 528 | auto curr_output = operator_outputs->Get(k); |
| 529 | operator_outputs_container.push_back(curr_output->str()); |
| 530 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 531 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 532 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 533 | switch (attribute_type) |
| 534 | { |
| 535 | case Attribute_NONE: |
| 536 | typed_attribute = new TosaNoneAttribute(); |
| 537 | break; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 538 | #define DEF_ATTRIBUTE(NAME, ...) \ |
| 539 | case Attribute_##NAME##Attribute: \ |
| 540 | typed_attribute = new Tosa##NAME##Attribute(attribute); \ |
| 541 | break; |
| 542 | #include "attribute.def" |
| 543 | #undef DEF_ATTRIBUTE |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 544 | default: |
| 545 | printf("TosaSerializationHandler::Deserialize(): Attribute %s not implemented yet\n", |
| 546 | EnumNamesAttribute()[attribute_type]); |
| 547 | return TOSA_INTERNAL_ERROR; |
| 548 | } |
| 549 | |
| 550 | new_operator = new TosaSerializationOperator(operator_op, attribute_type, typed_attribute, |
| 551 | operator_inputs_container, operator_outputs_container); |
| 552 | if (new_operator) |
| 553 | { |
| 554 | block_operators_container.push_back(new_operator); |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | return TOSA_MEMORY_ERROR; |
| 559 | } |
| 560 | |
| 561 | if (typed_attribute) |
| 562 | delete typed_attribute; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 565 | auto block_inputs = curr_block->inputs(); |
| 566 | auto block_outputs = curr_block->outputs(); |
| 567 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 568 | for (size_t j = 0; j < block_inputs->size(); j++) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 569 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 570 | auto curr_block_input = block_inputs->Get(j); |
| 571 | block_inputs_container.push_back(curr_block_input->str()); |
| 572 | } |
| 573 | for (size_t j = 0; j < block_outputs->size(); j++) |
| 574 | { |
| 575 | auto curr_block_output = block_outputs->Get(j); |
| 576 | block_outputs_container.push_back(curr_block_output->str()); |
| 577 | } |
| 578 | |
| 579 | auto fb_tosa_tensors = curr_block->tensors(); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 580 | for (size_t j = 0; j < fb_tosa_tensors->size(); j++) |
| 581 | { |
| 582 | auto curr_tensor = fb_tosa_tensors->Get(j); |
| 583 | |
| 584 | auto tensor_name = curr_tensor->name(); |
| 585 | auto tensor_shape = curr_tensor->shape(); |
| 586 | auto tensor_type = curr_tensor->type(); |
| 587 | auto tensor_data = curr_tensor->data(); |
| 588 | |
| 589 | new_tensor = new TosaSerializationTensor(tensor_name, tensor_shape, tensor_type, tensor_data); |
| 590 | if (new_tensor) |
| 591 | { |
| 592 | block_tensors_container.push_back(new_tensor); |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | return TOSA_MEMORY_ERROR; |
| 597 | } |
| 598 | } |
| 599 | new_block = new TosaSerializationBasicBlock(block_name, region_name, block_operators_container, |
| 600 | block_tensors_container, block_inputs_container, |
| 601 | block_outputs_container); |
| 602 | if (new_block) |
| 603 | { |
Tai Ly | cfcb20d | 2023-03-13 21:04:11 +0000 | [diff] [blame] | 604 | new_region->GetBlocks().push_back(new_block); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 605 | } |
| 606 | else |
| 607 | { |
| 608 | return TOSA_MEMORY_ERROR; |
| 609 | } |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 610 | } // end block for_loop |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | return TOSA_OK; |
| 614 | } |
| 615 | |
James Ward | 80905bb | 2023-01-25 15:51:27 +0000 | [diff] [blame] | 616 | std::vector<uint8_t> float_to_u8_helper(float f_in) |
James Ward | c15f7d5 | 2022-12-07 15:38:01 +0000 | [diff] [blame] | 617 | { |
James Ward | 80905bb | 2023-01-25 15:51:27 +0000 | [diff] [blame] | 618 | // Push back a single float value to the buffer with *NO PADDING* |
| 619 | // Therefore ConvertF32toU8 function not used |
James Ward | c15f7d5 | 2022-12-07 15:38:01 +0000 | [diff] [blame] | 620 | std::vector<uint8_t> u8_out; |
James Ward | 80905bb | 2023-01-25 15:51:27 +0000 | [diff] [blame] | 621 | uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&f_in); |
| 622 | u8_out.push_back(*val_u32 & 0xFF); |
| 623 | u8_out.push_back((*val_u32 >> 8) & 0xFF); |
| 624 | u8_out.push_back((*val_u32 >> 16) & 0xFF); |
| 625 | u8_out.push_back((*val_u32 >> 24) & 0xFF); |
James Ward | c15f7d5 | 2022-12-07 15:38:01 +0000 | [diff] [blame] | 626 | return u8_out; |
| 627 | } |
| 628 | |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 629 | tosa_err_t TosaSerializationHandler::Serialize() |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 630 | { |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 631 | // regions |
| 632 | std::vector<flatbuffers::Offset<TosaRegion>> fboffset_regions; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 633 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 634 | // translate TosaFlatbufferOperator to flatbuffers::Offset<TosaOperator> |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 635 | for (auto region : GetRegions()) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 636 | { |
Tai Ly | cfcb20d | 2023-03-13 21:04:11 +0000 | [diff] [blame] | 637 | std::vector<flatbuffers::Offset<TosaBasicBlock>> fboffset_blocks; |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 638 | for (auto block : region->GetBlocks()) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 639 | { |
Tai Ly | cfcb20d | 2023-03-13 21:04:11 +0000 | [diff] [blame] | 640 | std::vector<flatbuffers::Offset<TosaOperator>> fboffset_block_operators; |
| 641 | std::vector<flatbuffers::Offset<TosaTensor>> fboffset_block_tensors; |
| 642 | std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_inputs; |
| 643 | std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_block_outputs; |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 644 | auto block_name = _builder.CreateString(block->GetName().c_str()); |
| 645 | for (auto tensor_str : block->GetInputs()) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 646 | { |
| 647 | auto tensor_name = _builder.CreateString(tensor_str.c_str()); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 648 | fboffset_block_inputs.push_back(tensor_name); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 649 | } |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 650 | for (auto tensor_str : block->GetOutputs()) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 651 | { |
| 652 | auto tensor_name = _builder.CreateString(tensor_str.c_str()); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 653 | fboffset_block_outputs.push_back(tensor_name); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 654 | } |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 655 | auto fb_block_inputs = _builder.CreateVector(fboffset_block_inputs); |
| 656 | auto fb_block_outputs = _builder.CreateVector(fboffset_block_outputs); |
| 657 | for (auto op : block->GetOperators()) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 658 | { |
Tai Ly | cfcb20d | 2023-03-13 21:04:11 +0000 | [diff] [blame] | 659 | std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_inputs; |
| 660 | std::vector<flatbuffers::Offset<flatbuffers::String>> fboffset_operator_outputs; |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 661 | auto operator_op = op->GetOp(); |
| 662 | auto attribute_type = op->GetAttributeType(); |
| 663 | for (auto tensor_str : op->GetInputTensorNames()) |
| 664 | { |
| 665 | auto tensor_name = _builder.CreateString(tensor_str.c_str()); |
| 666 | fboffset_operator_inputs.push_back(tensor_name); |
| 667 | } |
| 668 | for (auto tensor_str : op->GetOutputTensorNames()) |
| 669 | { |
| 670 | auto tensor_name = _builder.CreateString(tensor_str.c_str()); |
| 671 | fboffset_operator_outputs.push_back(tensor_name); |
| 672 | } |
| 673 | auto fb_operator_inputs = _builder.CreateVector(fboffset_operator_inputs); |
| 674 | auto fb_operator_outputs = _builder.CreateVector(fboffset_operator_outputs); |
| 675 | flatbuffers::Offset<void> fb_attribute; |
| 676 | switch (attribute_type) |
| 677 | { |
| 678 | case Attribute_NONE: |
| 679 | fb_attribute = 0; |
| 680 | break; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 681 | #define DEF_ARGS_S_STR(NAME, V) , _builder.CreateString(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V().c_str()) |
James Ward | c15f7d5 | 2022-12-07 15:38:01 +0000 | [diff] [blame] | 682 | #define DEF_ARGS_S_FP_as_U8(NAME, V) \ |
James Ward | 80905bb | 2023-01-25 15:51:27 +0000 | [diff] [blame] | 683 | , _builder.CreateVector<uint8_t>(float_to_u8_helper(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V())) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 684 | #define DEF_ARGS_S_DEFAULT(NAME, V) , reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V() |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 685 | #define DEF_ARGS_S_int32_t(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V) |
James Ward | c15f7d5 | 2022-12-07 15:38:01 +0000 | [diff] [blame] | 686 | #define DEF_ARGS_S_float(NAME, V) DEF_ARGS_S_FP_as_U8(NAME, V) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 687 | #define DEF_ARGS_S_bool(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V) |
| 688 | #define DEF_ARGS_S_ResizeMode(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V) |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 689 | #define DEF_ARGS_S_DType(NAME, V) DEF_ARGS_S_DEFAULT(NAME, V) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 690 | #define DEF_ARGS_S_string(NAME, V) DEF_ARGS_S_STR(NAME, V) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 691 | #define DEF_ARGS_S(NAME, T, V) DEF_ARGS_S_##T(NAME, V) |
| 692 | #define DEF_ARGS_V(NAME, T, V) , _builder.CreateVector<T>(reinterpret_cast<Tosa##NAME*>(op->GetAttribute())->V()) |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 693 | #define DEF_ARGS_1(NAME, T0, F0, V0) DEF_ARGS_##F0(NAME, T0, V0) |
| 694 | #define DEF_ARGS_2(NAME, T0, F0, V0, T1, F1, V1) DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) |
| 695 | #define DEF_ARGS_3(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2) \ |
| 696 | DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) |
| 697 | #define DEF_ARGS_4(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \ |
| 698 | DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) |
| 699 | #define DEF_ARGS_5(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \ |
| 700 | DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \ |
| 701 | DEF_ARGS_##F4(NAME, T4, V4) |
| 702 | #define DEF_ARGS_6(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \ |
| 703 | DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \ |
| 704 | DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) |
| 705 | #define DEF_ARGS_7(NAME, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6) \ |
| 706 | DEF_ARGS_##F0(NAME, T0, V0) DEF_ARGS_##F1(NAME, T1, V1) DEF_ARGS_##F2(NAME, T2, V2) DEF_ARGS_##F3(NAME, T3, V3) \ |
| 707 | DEF_ARGS_##F4(NAME, T4, V4) DEF_ARGS_##F5(NAME, T5, V5) DEF_ARGS_##F6(NAME, T6, V6) |
| 708 | #define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \ |
| 709 | case Attribute_##NAME##Attribute: \ |
| 710 | fb_attribute = Create##NAME##Attribute(_builder DEF_ARGS_##NUM_ARGS(NAME##Attribute, __VA_ARGS__)).Union(); \ |
| 711 | break; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 712 | #include "attribute.def" |
| 713 | #undef DEF_ATTRIBUTE |
| 714 | #undef DEF_ARGS_1 |
| 715 | #undef DEF_ARGS_2 |
| 716 | #undef DEF_ARGS_3 |
| 717 | #undef DEF_ARGS_4 |
| 718 | #undef DEF_ARGS_5 |
| 719 | #undef DEF_ARGS_6 |
| 720 | #undef DEF_ARGS_7 |
| 721 | #undef DEF_ARGS_S |
| 722 | #undef DEF_ARGS_V |
| 723 | #undef DEF_ARGS_S_int32_t |
| 724 | #undef DEF_ARGS_S_float |
| 725 | #undef DEF_ARGS_S_bool |
| 726 | #undef DEF_ARGS_S_ResizeMode |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 727 | #undef DEF_ARGS_S_DType |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 728 | #undef DEF_ARGS_S_string |
| 729 | #undef DEF_ARGS_S_STR |
| 730 | #undef DEF_ARGS_S_DEFAULT |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 731 | default: |
| 732 | printf("TosaSerializationHandler::Serialize(): Attribute %s not implemented yet\n", |
| 733 | EnumNamesAttribute()[attribute_type]); |
| 734 | return TOSA_INTERNAL_ERROR; |
| 735 | } |
| 736 | auto fboffset_operator = CreateTosaOperator(_builder, operator_op, attribute_type, fb_attribute, |
| 737 | fb_operator_inputs, fb_operator_outputs); |
| 738 | fboffset_block_operators.push_back(fboffset_operator); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 739 | } |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 740 | auto fb_block_operators = _builder.CreateVector(fboffset_block_operators); |
| 741 | for (auto tensor : block->GetTensors()) |
| 742 | { |
| 743 | auto tensor_name = _builder.CreateString(tensor->GetName().c_str()); |
| 744 | auto tensor_shape = _builder.CreateVector(tensor->GetShape()); |
| 745 | auto tensor_dtype = tensor->GetDtype(); |
| 746 | auto tensor_data = _builder.CreateVector(tensor->GetData()); |
| 747 | auto fboffset_tensor = CreateTosaTensor(_builder, tensor_name, tensor_shape, tensor_dtype, tensor_data); |
| 748 | fboffset_block_tensors.push_back(fboffset_tensor); |
| 749 | } |
| 750 | auto fb_block_tensors = _builder.CreateVector(fboffset_block_tensors); |
| 751 | auto fboffset_block = CreateTosaBasicBlock(_builder, block_name, fb_block_operators, fb_block_tensors, |
| 752 | fb_block_inputs, fb_block_outputs); |
| 753 | fboffset_blocks.push_back(fboffset_block); |
| 754 | } // end block for_loop |
| 755 | auto fb_blocks = _builder.CreateVector(fboffset_blocks); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 756 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 757 | auto region_name = _builder.CreateString(region->GetName().c_str()); |
| 758 | auto fboffset_region = CreateTosaRegion(_builder, region_name, fb_blocks); |
| 759 | fboffset_regions.push_back(fboffset_region); |
| 760 | } // end region for_loop |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 761 | |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 762 | auto fb_regions = _builder.CreateVector(fboffset_regions); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 763 | |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 764 | auto fb_version = |
| 765 | CreateVersion(_builder, TOSA_VERSION_MAJOR, TOSA_VERSION_MINOR, TOSA_VERSION_PATCH, TOSA_VERSION_DRAFT); |
Jerry Ge | 13c78a6 | 2022-10-04 20:32:39 -0700 | [diff] [blame] | 766 | auto fb_graph = CreateTosaGraph(_builder, fb_version, fb_regions); |
Eric Kunze | e659640 | 2022-06-09 21:27:36 +0000 | [diff] [blame] | 767 | _builder.Finish(fb_graph, TosaGraphIdentifier()); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 768 | |
| 769 | return TOSA_OK; |
| 770 | } |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 771 | |
| 772 | void zero_pad(std::vector<uint8_t>& buf) |
| 773 | { |
| 774 | while ((buf.size() % TENSOR_BUFFER_FORCE_ALIGNMENT) != 0) |
| 775 | { |
| 776 | buf.push_back(0); |
| 777 | } |
| 778 | } |
| 779 | |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 780 | tosa_err_t TosaSerializationHandler::ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out) |
| 781 | { |
| 782 | // Note: Converts fp32->fp16 before converting to uint8_t |
| 783 | out.clear(); |
| 784 | for (auto val : in) |
| 785 | { |
| 786 | half_float::half val_f16 = half_float::half_cast<half_float::half, float>(val); |
| 787 | uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val_f16); |
| 788 | out.push_back(*val_u16 & 0xFF); |
| 789 | out.push_back((*val_u16 >> 8) & 0xFF); |
| 790 | } |
| 791 | zero_pad(out); |
| 792 | return TOSA_OK; |
| 793 | } |
| 794 | |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 795 | tosa_err_t TosaSerializationHandler::ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out) |
| 796 | { |
| 797 | out.clear(); |
| 798 | for (auto val : in) |
| 799 | { |
| 800 | uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val); |
| 801 | out.push_back(*val_u32 & 0xFF); |
| 802 | out.push_back((*val_u32 >> 8) & 0xFF); |
| 803 | out.push_back((*val_u32 >> 16) & 0xFF); |
| 804 | out.push_back((*val_u32 >> 24) & 0xFF); |
| 805 | } |
| 806 | zero_pad(out); |
| 807 | return TOSA_OK; |
| 808 | } |
| 809 | |
| 810 | tosa_err_t TosaSerializationHandler::ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out) |
| 811 | { |
| 812 | out.clear(); |
| 813 | for (auto val : in) |
| 814 | { |
| 815 | uint64_t* val_u64 = reinterpret_cast<uint64_t*>(&val); |
| 816 | out.push_back(*val_u64 & 0xFF); |
| 817 | out.push_back((*val_u64 >> 8) & 0xFF); |
| 818 | out.push_back((*val_u64 >> 16) & 0xFF); |
| 819 | out.push_back((*val_u64 >> 24) & 0xFF); |
| 820 | out.push_back((*val_u64 >> 32) & 0xFF); |
| 821 | out.push_back((*val_u64 >> 40) & 0xFF); |
| 822 | } |
| 823 | zero_pad(out); |
| 824 | return TOSA_OK; |
| 825 | } |
| 826 | |
| 827 | tosa_err_t TosaSerializationHandler::ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out) |
| 828 | { |
| 829 | out.clear(); |
| 830 | for (auto val : in) |
| 831 | { |
| 832 | uint32_t* val_u32 = reinterpret_cast<uint32_t*>(&val); |
| 833 | out.push_back(*val_u32 & 0xFF); |
| 834 | out.push_back((*val_u32 >> 8) & 0xFF); |
| 835 | out.push_back((*val_u32 >> 16) & 0xFF); |
| 836 | out.push_back((*val_u32 >> 24) & 0xFF); |
| 837 | } |
| 838 | zero_pad(out); |
| 839 | return TOSA_OK; |
| 840 | } |
| 841 | |
| 842 | tosa_err_t TosaSerializationHandler::ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out) |
| 843 | { |
| 844 | out.clear(); |
| 845 | for (auto val : in) |
| 846 | { |
| 847 | uint16_t* val_u16 = reinterpret_cast<uint16_t*>(&val); |
| 848 | out.push_back(*val_u16 & 0xFF); |
| 849 | out.push_back((*val_u16 >> 8) & 0xFF); |
| 850 | } |
| 851 | zero_pad(out); |
| 852 | return TOSA_OK; |
| 853 | } |
| 854 | |
| 855 | tosa_err_t TosaSerializationHandler::ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out) |
| 856 | { |
| 857 | out.clear(); |
| 858 | for (auto val : in) |
| 859 | { |
| 860 | uint8_t* val_u8 = reinterpret_cast<uint8_t*>(&val); |
| 861 | out.push_back(*val_u8); |
| 862 | } |
| 863 | zero_pad(out); |
| 864 | return TOSA_OK; |
| 865 | } |
| 866 | |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 867 | // Two int4 values are packed into one byte out. |
| 868 | // For given input value val_0 = in[2*i], and val_1 = in[2*i+1], |
| 869 | // they'll be packed as out[3:0] = val_0, and out[7:4] = val_1 |
| 870 | tosa_err_t TosaSerializationHandler::ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out) |
| 871 | { |
| 872 | out.clear(); |
| 873 | uint32_t in_size = in.size(); |
| 874 | uint32_t out_size = (in_size % 2 == 0) ? (in_size / 2) : ((in_size + 1) / 2); |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 875 | for (uint32_t i = 0; i < out_size; i++) |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 876 | { |
| 877 | int8_t val_0 = in[2 * i]; |
| 878 | int8_t val_1 = 0; |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 879 | if (2u * i + 1u < in_size) |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 880 | { |
| 881 | val_1 = in[2 * i + 1]; |
| 882 | } |
| 883 | // In TOSA spec, int4 ranges [-7, 7] |
| 884 | if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7) |
| 885 | { |
| 886 | printf("TosaSerializationHandler::ConvertI4toU8(): element in input array (%d or %d) exceeds int4 range.\n", |
| 887 | val_0, val_1); |
| 888 | return TOSA_USER_ERROR; |
| 889 | } |
| 890 | int8_t val_packed = (val_0 & 0xF) | ((val_1 & 0xF) << 4); |
| 891 | uint8_t val_u8 = static_cast<uint8_t>(val_packed); |
| 892 | out.push_back(val_u8); |
| 893 | } |
| 894 | zero_pad(out); |
| 895 | return TOSA_OK; |
| 896 | } |
| 897 | |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 898 | tosa_err_t TosaSerializationHandler::ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out) |
| 899 | { |
| 900 | out.clear(); |
| 901 | for (auto val : in) |
| 902 | { |
Eric Kunze | 4417b42 | 2022-06-20 07:27:42 -0700 | [diff] [blame] | 903 | uint8_t val_u8 = val; |
| 904 | out.push_back(val_u8); |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 905 | } |
| 906 | zero_pad(out); |
| 907 | return TOSA_OK; |
| 908 | } |
| 909 | |
| 910 | tosa_err_t |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 911 | TosaSerializationHandler::ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out) |
| 912 | { |
| 913 | // Note: fp16 values returned in fp32 type |
| 914 | out.clear(); |
| 915 | if (in.size() < out_size * sizeof(int16_t)) |
| 916 | { |
| 917 | printf("TosaSerializationHandler::ConvertU8toF16(): uint8 buffer size %ld must >= target size %ld\n", in.size(), |
| 918 | out_size * sizeof(int16_t)); |
| 919 | return TOSA_USER_ERROR; |
| 920 | } |
| 921 | |
| 922 | for (uint32_t i = 0; i < out_size; i++) |
| 923 | { |
| 924 | uint16_t f16_byte0 = in[i * sizeof(int16_t)]; |
| 925 | uint16_t f16_byte1 = in[i * sizeof(int16_t) + 1]; |
| 926 | uint16_t val_u16 = f16_byte0 + (f16_byte1 << 8); |
| 927 | |
| 928 | // Reinterpret u16 byte as fp16 then convert to fp32 |
| 929 | half_float::half val_f16 = *(half_float::half*)&val_u16; |
| 930 | float val_fp32 = half_float::half_cast<float, half_float::half>(val_f16); |
| 931 | out.push_back(val_fp32); |
| 932 | } |
| 933 | return TOSA_OK; |
| 934 | } |
| 935 | |
| 936 | tosa_err_t |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 937 | TosaSerializationHandler::ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out) |
| 938 | { |
| 939 | out.clear(); |
| 940 | if (in.size() < out_size * sizeof(float)) |
| 941 | { |
| 942 | printf("TosaSerializationHandler::ConvertU8toF32(): uint8 buffer size %ld must >= target size %ld\n", in.size(), |
| 943 | out_size * sizeof(float)); |
| 944 | return TOSA_USER_ERROR; |
| 945 | } |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 946 | for (uint32_t i = 0; i < out_size; i++) |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 947 | { |
| 948 | uint32_t byte0 = in[i * sizeof(float)]; |
| 949 | uint32_t byte1 = in[i * sizeof(float) + 1]; |
| 950 | uint32_t byte2 = in[i * sizeof(float) + 2]; |
| 951 | uint32_t byte3 = in[i * sizeof(float) + 3]; |
| 952 | uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24); |
| 953 | float* val_fp32 = reinterpret_cast<float*>(&val_u32); |
| 954 | out.push_back(*val_fp32); |
| 955 | } |
| 956 | return TOSA_OK; |
| 957 | } |
| 958 | |
| 959 | tosa_err_t TosaSerializationHandler::ConvertU8toI48(const std::vector<uint8_t>& in, |
| 960 | uint32_t out_size, |
| 961 | std::vector<int64_t>& out) |
| 962 | { |
| 963 | out.clear(); |
| 964 | if (in.size() < out_size * 6 /* sizeof(int48) */) |
| 965 | { |
| 966 | printf("TosaSerializationHandler::ConvertU8toI48(): uint8 buffer size %ld must >= target size %d\n", in.size(), |
| 967 | out_size * 6); |
| 968 | return TOSA_USER_ERROR; |
| 969 | } |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 970 | for (uint32_t i = 0; i < out_size; i++) |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 971 | { |
| 972 | uint64_t byte0 = in[i * 6]; |
| 973 | uint64_t byte1 = in[i * 6 + 1]; |
| 974 | uint64_t byte2 = in[i * 6 + 2]; |
| 975 | uint64_t byte3 = in[i * 6 + 3]; |
| 976 | uint64_t byte4 = in[i * 6 + 4]; |
| 977 | uint64_t byte5 = in[i * 6 + 5]; |
| 978 | bool sign = ((byte5 >> 7) & 1) == 1 ? true : false; |
| 979 | uint64_t val_u64 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24) + (byte4 << 32) + (byte5 << 40); |
| 980 | if (sign) |
| 981 | { |
| 982 | uint64_t sext_mask = (0xFFFFUL << 48); |
| 983 | val_u64 |= sext_mask; |
| 984 | } |
| 985 | int64_t* val_i64 = reinterpret_cast<int64_t*>(&val_u64); |
| 986 | out.push_back(*val_i64); |
| 987 | } |
| 988 | return TOSA_OK; |
| 989 | } |
| 990 | |
| 991 | tosa_err_t TosaSerializationHandler::ConvertU8toI32(const std::vector<uint8_t>& in, |
| 992 | uint32_t out_size, |
| 993 | std::vector<int32_t>& out) |
| 994 | { |
| 995 | out.clear(); |
| 996 | if (in.size() < out_size * sizeof(int32_t)) |
| 997 | { |
| 998 | printf("TosaSerializationHandler::ConvertU8toI32(): uint8 buffer size %ld must >= target size %ld\n", in.size(), |
| 999 | out_size * sizeof(int32_t)); |
| 1000 | return TOSA_USER_ERROR; |
| 1001 | } |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 1002 | for (uint32_t i = 0; i < out_size; i++) |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 1003 | { |
| 1004 | uint32_t byte0 = in[i * sizeof(int32_t)]; |
| 1005 | uint32_t byte1 = in[i * sizeof(int32_t) + 1]; |
| 1006 | uint32_t byte2 = in[i * sizeof(int32_t) + 2]; |
| 1007 | uint32_t byte3 = in[i * sizeof(int32_t) + 3]; |
| 1008 | uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24); |
| 1009 | int32_t* val_i32 = reinterpret_cast<int32_t*>(&val_u32); |
| 1010 | out.push_back(*val_i32); |
| 1011 | } |
| 1012 | return TOSA_OK; |
| 1013 | } |
| 1014 | |
| 1015 | tosa_err_t TosaSerializationHandler::ConvertU8toI16(const std::vector<uint8_t>& in, |
| 1016 | uint32_t out_size, |
| 1017 | std::vector<int16_t>& out) |
| 1018 | { |
| 1019 | out.clear(); |
| 1020 | if (in.size() < out_size * sizeof(int16_t)) |
| 1021 | { |
| 1022 | printf("TosaSerializationHandler::ConvertU8toI16(): uint8 buffer size %ld must >= target size %ld\n", in.size(), |
| 1023 | out_size * sizeof(int16_t)); |
| 1024 | return TOSA_USER_ERROR; |
| 1025 | } |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 1026 | for (uint32_t i = 0; i < out_size; i++) |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 1027 | { |
| 1028 | uint16_t byte0 = in[i * sizeof(int16_t)]; |
| 1029 | uint16_t byte1 = in[i * sizeof(int16_t) + 1]; |
| 1030 | uint16_t val_u16 = byte0 + (byte1 << 8); |
| 1031 | int16_t* val_i16 = reinterpret_cast<int16_t*>(&val_u16); |
| 1032 | out.push_back(*val_i16); |
| 1033 | } |
| 1034 | return TOSA_OK; |
| 1035 | } |
| 1036 | |
| 1037 | tosa_err_t |
| 1038 | TosaSerializationHandler::ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out) |
| 1039 | { |
| 1040 | out.clear(); |
| 1041 | if (in.size() < out_size * sizeof(int8_t)) |
| 1042 | { |
| 1043 | printf("TosaSerializationHandler::ConvertU8toI8(): uint8 buffer size %ld must >= target size %ld\n", in.size(), |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 1044 | out_size * sizeof(int8_t)); |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 1045 | return TOSA_USER_ERROR; |
| 1046 | } |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 1047 | for (uint32_t i = 0; i < out_size; i++) |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 1048 | { |
| 1049 | uint8_t val_u8 = in[i]; |
| 1050 | int8_t* val_i8 = reinterpret_cast<int8_t*>(&val_u8); |
| 1051 | out.push_back(*val_i8); |
| 1052 | } |
| 1053 | return TOSA_OK; |
| 1054 | } |
| 1055 | |
| 1056 | tosa_err_t |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 1057 | TosaSerializationHandler::ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out) |
| 1058 | { |
| 1059 | out.clear(); |
| 1060 | if (out_size > in.size() * 2) |
| 1061 | { |
| 1062 | printf("TosaSerializationHandler::ConvertU8toI4(): output size %u must <= uint8 buffer size %ld x 2.\n", |
| 1063 | out_size, in.size()); |
| 1064 | return TOSA_USER_ERROR; |
| 1065 | } |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 1066 | for (size_t i = 0; i < in.size(); i++) |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 1067 | { |
| 1068 | uint8_t val_u8 = in[i]; |
| 1069 | uint8_t val_0_u4 = val_u8 & 0xF; |
| 1070 | uint8_t val_1_u4 = val_u8 >> 4; |
| 1071 | uint8_t val_0_u8_sext = (val_0_u4 & 0x08) ? (val_0_u4 | 0xF0) : val_0_u4; |
| 1072 | uint8_t val_1_u8_sext = (val_1_u4 & 0x08) ? (val_1_u4 | 0xF0) : val_1_u4; |
| 1073 | int8_t val_0 = static_cast<int8_t>(val_0_u8_sext); |
| 1074 | int8_t val_1 = static_cast<int8_t>(val_1_u8_sext); |
| 1075 | // In TOSA spec, int4 ranges [-7, 7] |
| 1076 | if (val_0 < -7 || val_0 > 7 || val_1 < -7 || val_1 > 7) |
| 1077 | { |
| 1078 | printf( |
| 1079 | "TosaSerializationHandler::ConvertU8toI4(): element in output array (%d or %d) exceeds int4 range.\n", |
| 1080 | val_0, val_1); |
| 1081 | return TOSA_USER_ERROR; |
| 1082 | } |
| 1083 | out.push_back(val_0); |
| 1084 | if (2 * i + 1 < out_size) |
| 1085 | out.push_back(val_1); |
| 1086 | } |
| 1087 | return TOSA_OK; |
| 1088 | } |
| 1089 | |
| 1090 | tosa_err_t |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 1091 | TosaSerializationHandler::ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out) |
| 1092 | { |
| 1093 | out.clear(); |
| 1094 | if (in.size() < out_size * sizeof(bool)) |
| 1095 | { |
| 1096 | printf("TosaSerializationHandler::ConvertU8toBool(): uint8 buffer size %ld must >= target size %ld\n", |
| 1097 | in.size(), out_size * sizeof(bool)); |
| 1098 | return TOSA_USER_ERROR; |
| 1099 | } |
Eric Kunze | b13fe8f | 2022-02-17 17:14:25 -0800 | [diff] [blame] | 1100 | for (uint32_t i = 0; i < out_size; i++) |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 1101 | { |
| 1102 | uint8_t val_u8 = in[i]; |
| 1103 | bool* val_bool = reinterpret_cast<bool*>(&val_u8); |
| 1104 | out.push_back(*val_bool); |
| 1105 | } |
| 1106 | return TOSA_OK; |
| 1107 | } |