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