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