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 | #ifndef _TOSA_SERIALIZATION_HANDLER_H |
| 17 | #define _TOSA_SERIALIZATION_HANDLER_H |
| 18 | #include "attribute.h" |
| 19 | #include "flatbuffers/idl.h" |
| 20 | #include "flatbuffers/util.h" |
| 21 | #include "numpy_utils.h" |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 22 | #include "tosa_generated.h" |
| 23 | #include <cstdint> |
| 24 | #include <memory> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 28 | // Keep version number in sync with the version default value with schema/tosa.fbs |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 29 | #define TOSA_VERSION_MAJOR 0 |
Eric Kunze | 9e2e0bb | 2022-12-05 23:23:54 +0000 | [diff] [blame^] | 30 | #define TOSA_VERSION_MINOR 50 |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 31 | #define TOSA_VERSION_PATCH 0 |
Eric Kunze | 9e2e0bb | 2022-12-05 23:23:54 +0000 | [diff] [blame^] | 32 | #define TOSA_VERSION_DRAFT false |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 33 | #define TENSOR_BUFFER_FORCE_ALIGNMENT 8 |
| 34 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 35 | namespace tosa |
| 36 | { |
| 37 | |
| 38 | enum tosa_err_t |
| 39 | { |
| 40 | TOSA_OK, |
| 41 | TOSA_USER_ERROR, |
| 42 | TOSA_FILE_ERROR, |
| 43 | TOSA_MEMORY_ERROR, |
| 44 | TOSA_SCHEMA_MISSING, |
| 45 | TOSA_INTERNAL_ERROR, |
| 46 | TOSA_VERSION_MISMATCH, |
| 47 | NUM_TOSA_ERROR |
| 48 | }; |
| 49 | |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 50 | struct TosaVersion |
| 51 | { |
| 52 | int32_t _major; |
| 53 | int32_t _minor; |
| 54 | int32_t _patch; |
| 55 | bool _draft; |
| 56 | |
| 57 | enum class compat_t |
| 58 | { |
| 59 | COMPLETELY_COMPATIBLE, |
| 60 | PARTIALLY_COMPATIBLE, |
| 61 | NOT_COMPATIBLE |
| 62 | }; |
| 63 | |
| 64 | TosaVersion() = default; |
| 65 | TosaVersion(int32_t major, int32_t minor, int32_t patch, bool draft) |
| 66 | { |
| 67 | set_version(major, minor, patch, draft); |
| 68 | } |
| 69 | |
| 70 | void set_version(int32_t major, int32_t minor, int32_t patch, bool draft) |
| 71 | { |
| 72 | _major = major; |
| 73 | _minor = minor; |
| 74 | _patch = patch; |
| 75 | _draft = draft; |
| 76 | } |
| 77 | |
| 78 | std::string to_string() const |
| 79 | { |
| 80 | std::string str; |
| 81 | str += std::to_string(_major) + "."; |
| 82 | str += std::to_string(_minor) + "."; |
| 83 | str += std::to_string(_patch); |
| 84 | if (_draft) |
| 85 | str += "d"; |
| 86 | return str; |
| 87 | } |
| 88 | |
| 89 | compat_t is_compatible(const TosaVersion& rhs) const |
| 90 | { |
| 91 | if (rhs._major == _major && rhs._minor == _minor) |
| 92 | { |
| 93 | if (rhs._patch == _patch && rhs._draft == _draft) |
| 94 | { |
| 95 | return TosaVersion::compat_t::COMPLETELY_COMPATIBLE; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | return TosaVersion::compat_t::PARTIALLY_COMPATIBLE; |
| 100 | } |
| 101 | } |
| 102 | return TosaVersion::compat_t::NOT_COMPATIBLE; |
| 103 | } |
| 104 | }; |
| 105 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 106 | class TosaSerializationHandler; |
| 107 | |
| 108 | class TosaSerializationTensor |
| 109 | { |
| 110 | public: |
| 111 | // constructor and destructor |
| 112 | TosaSerializationTensor(const flatbuffers::String* name, |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 113 | const flatbuffers::Vector<int32_t>* shape, |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 114 | DType dtype, |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 115 | const flatbuffers::Vector<uint8_t>* data); |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 116 | TosaSerializationTensor(const std::string& name, |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 117 | const std::vector<int32_t>& shape, |
| 118 | DType dtype, |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 119 | const std::vector<uint8_t>& data); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 120 | TosaSerializationTensor(); |
| 121 | ~TosaSerializationTensor(); |
| 122 | |
| 123 | // accessor |
| 124 | std::string GetName() const |
| 125 | { |
| 126 | return _name; |
| 127 | } |
| 128 | const std::vector<int32_t>& GetShape() const |
| 129 | { |
| 130 | return _shape; |
| 131 | } |
| 132 | DType GetDtype() |
| 133 | { |
| 134 | return _dtype; |
| 135 | } |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 136 | const std::vector<uint8_t>& GetData() const |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 137 | { |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 138 | return _data; |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // modifier |
| 142 | void SetDtype(DType dtype) |
| 143 | { |
| 144 | _dtype = dtype; |
| 145 | } |
| 146 | void SetName(std::string name) |
| 147 | { |
| 148 | _name = name; |
| 149 | } |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 150 | void SetData(const std::vector<uint8_t>& data) |
| 151 | { |
| 152 | _data = data; |
| 153 | } |
| 154 | void SetData(std::vector<uint8_t>&& data) |
| 155 | { |
| 156 | _data = std::move(data); |
| 157 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 158 | |
| 159 | private: |
| 160 | DType _dtype; /* data type enumeration, see tosa_isa_generated.h */ |
| 161 | std::vector<int32_t> _shape; /* shape of the tensor */ |
| 162 | std::string _name; /* name of the tensor, used for solving dependency */ |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 163 | std::vector<uint8_t> _data; /* data array */ |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | class TosaSerializationOperator |
| 167 | { |
| 168 | public: |
| 169 | // use default copy, void constructor |
| 170 | // constructor and destructor |
| 171 | TosaSerializationOperator(Op op, |
| 172 | Attribute attribute_type, |
| 173 | const TosaAttributeBase* attribute, |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 174 | const std::vector<std::string>& input_tensor_names, |
| 175 | const std::vector<std::string>& output_tensor_names); |
| 176 | TosaSerializationOperator(Op op, |
| 177 | Attribute attribute_type, |
| 178 | const TosaAttributeBase* attribute, |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 179 | std::vector<std::string>&& input_tensor_names, |
| 180 | std::vector<std::string>&& output_tensor_names); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 181 | ~TosaSerializationOperator(); |
| 182 | |
| 183 | // accessor |
| 184 | Op GetOp() const |
| 185 | { |
| 186 | return _op; |
| 187 | } |
| 188 | Attribute GetAttributeType() const |
| 189 | { |
| 190 | return _attribute_type; |
| 191 | } |
| 192 | TosaAttributeBase* GetAttribute() const |
| 193 | { |
| 194 | return _attribute; |
| 195 | } |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 196 | std::vector<std::string>& GetInputTensorNames() |
| 197 | { |
| 198 | return _input_tensor_names; |
| 199 | } |
| 200 | std::vector<std::string>& GetOutputTensorNames() |
| 201 | { |
| 202 | return _output_tensor_names; |
| 203 | } |
| 204 | |
| 205 | private: |
Eric Kunze | bdcc3fe | 2022-06-07 05:17:37 +0000 | [diff] [blame] | 206 | void InitializeAttribute(Attribute attribute_type, const TosaAttributeBase* attribute); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 207 | Op _op; /* operator enum, see tosa_isa_generated.h for enumeration table */ |
| 208 | Attribute _attribute_type; /* operator attribute enum, used for dynamic casting TosaAttributeBase class */ |
| 209 | TosaAttributeBase* _attribute; /* real attribute class goes here */ |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 210 | std::vector<std::string> _input_tensor_names; /* array of input tensor names */ |
| 211 | std::vector<std::string> _output_tensor_names; /* array of output tensor names */ |
| 212 | }; |
| 213 | |
| 214 | class TosaSerializationBasicBlock |
| 215 | { |
| 216 | public: |
| 217 | // constructor and destructor |
Kevin Cheng | 545a508 | 2021-11-11 01:36:33 +0000 | [diff] [blame] | 218 | TosaSerializationBasicBlock(const std::string& name, |
| 219 | const std::vector<TosaSerializationOperator*>& operators, |
| 220 | const std::vector<TosaSerializationTensor*>& tensors, |
| 221 | const std::vector<std::string>& inputs, |
| 222 | const std::vector<std::string>& outputs); |
| 223 | TosaSerializationBasicBlock(std::string&& name, |
| 224 | std::vector<TosaSerializationOperator*>&& operators, |
| 225 | std::vector<TosaSerializationTensor*>&& tensors, |
| 226 | std::vector<std::string>&& inputs, |
| 227 | std::vector<std::string>&& outputs); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 228 | ~TosaSerializationBasicBlock(); |
| 229 | |
| 230 | // accessor |
| 231 | std::string GetName() const |
| 232 | { |
| 233 | return _name; |
| 234 | } |
| 235 | std::vector<TosaSerializationOperator*>& GetOperators() |
| 236 | { |
| 237 | return _operators; |
| 238 | } |
| 239 | std::vector<TosaSerializationTensor*>& GetTensors() |
| 240 | { |
| 241 | return _tensors; |
| 242 | } |
| 243 | |
| 244 | TosaSerializationTensor* GetTensorByName(std::string name) |
| 245 | { |
| 246 | TosaSerializationTensor* result = nullptr; |
| 247 | for (auto tensor : GetTensors()) |
| 248 | { |
| 249 | if (tensor->GetName() == name) |
| 250 | { |
| 251 | result = tensor; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | std::vector<std::string>& GetInputs() |
| 259 | { |
| 260 | return _inputs; |
| 261 | } |
| 262 | std::vector<std::string>& GetOutputs() |
| 263 | { |
| 264 | return _outputs; |
| 265 | } |
| 266 | |
| 267 | private: |
| 268 | std::string _name; /* name of basic block */ |
| 269 | std::vector<TosaSerializationOperator*> _operators; /* TosaSerializationOperator list */ |
| 270 | std::vector<TosaSerializationTensor*> _tensors; /* TosaSerializationTensor list */ |
| 271 | std::vector<std::string> _inputs; /* array of string to specify block inputs */ |
| 272 | std::vector<std::string> _outputs; /* array of string to specify block outputs */ |
| 273 | }; |
| 274 | |
| 275 | /* |
| 276 | * this is a helper class for writing/reading Tosa ISA |
| 277 | * supported format: .tosa (flatbuffer), .json |
| 278 | * and provide high-level std::vector-like interface |
| 279 | * to access internal data structure |
| 280 | */ |
| 281 | class TosaSerializationHandler |
| 282 | { |
| 283 | public: |
| 284 | // constructor and destructor |
| 285 | TosaSerializationHandler(); |
| 286 | ~TosaSerializationHandler(); |
| 287 | |
| 288 | // file io |
| 289 | tosa_err_t LoadFileJson(const char* filename); |
| 290 | tosa_err_t LoadFileTosaFlatbuffer(const char* filename); |
Aaron DeBattista | 8b3903a | 2021-11-18 16:38:11 +0000 | [diff] [blame] | 291 | tosa_err_t LoadFileTosaFlatbuffer(const void* input, int in_size); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 292 | tosa_err_t SaveFileJson(const char* filename); |
| 293 | tosa_err_t SaveFileTosaFlatbuffer(const char* filename); |
| 294 | tosa_err_t LoadFileSchema(const char* schema_filename); |
| 295 | |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 296 | // data format conversion. little-endian. |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 297 | static tosa_err_t ConvertF16toU8(const std::vector<float>& in, std::vector<uint8_t>& out); |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 298 | static tosa_err_t ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out); |
| 299 | static tosa_err_t ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out); |
| 300 | static tosa_err_t ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out); |
| 301 | static tosa_err_t ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out); |
| 302 | static tosa_err_t ConvertI8toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out); |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 303 | static tosa_err_t ConvertI4toU8(const std::vector<int8_t>& in, std::vector<uint8_t>& out); |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 304 | static tosa_err_t ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out); |
| 305 | |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 306 | static tosa_err_t ConvertU8toF16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out); |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 307 | static tosa_err_t ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out); |
| 308 | static tosa_err_t ConvertU8toI48(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int64_t>& out); |
| 309 | static tosa_err_t ConvertU8toI32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int32_t>& out); |
| 310 | static tosa_err_t ConvertU8toI16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int16_t>& out); |
| 311 | static tosa_err_t ConvertU8toI8(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out); |
Kevin Cheng | 3ce5634 | 2021-07-28 13:42:29 -0700 | [diff] [blame] | 312 | static tosa_err_t ConvertU8toI4(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int8_t>& out); |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 313 | static tosa_err_t ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out); |
| 314 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 315 | // version |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 316 | const TosaVersion& GetVersion() |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 317 | { |
| 318 | return _version; |
| 319 | } |
| 320 | |
| 321 | // accessor |
| 322 | std::vector<TosaSerializationBasicBlock*>& GetBlocks() |
| 323 | { |
| 324 | return _blocks; |
| 325 | } |
| 326 | |
| 327 | TosaSerializationBasicBlock* GetBlockByName(std::string name) |
| 328 | { |
| 329 | TosaSerializationBasicBlock* result = nullptr; |
| 330 | for (auto block : GetBlocks()) |
| 331 | { |
| 332 | if (block->GetName() == name) |
| 333 | { |
| 334 | result = block; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | return result; |
| 339 | } |
| 340 | TosaSerializationBasicBlock* GetMainBlock() |
| 341 | { |
| 342 | TosaSerializationBasicBlock* main_block = GetBlockByName(std::string("main")); |
| 343 | assert(main_block); |
| 344 | return main_block; |
| 345 | } |
| 346 | |
| 347 | std::vector<std::string>& GetInputs() |
| 348 | { |
| 349 | return GetMainBlock()->GetInputs(); |
| 350 | } |
| 351 | std::vector<std::string>& GetOutputs() |
| 352 | { |
| 353 | return GetMainBlock()->GetOutputs(); |
| 354 | } |
| 355 | |
| 356 | bool GetSchemaLoaded() const |
| 357 | { |
| 358 | return _schemaLoaded; |
| 359 | } |
| 360 | |
| 361 | protected: |
| 362 | tosa_err_t Clear(); |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 363 | tosa_err_t Deserialize(const uint8_t* buf); |
| 364 | tosa_err_t Serialize(); |
Kevin Cheng | a81a7a1 | 2021-11-10 14:07:34 -0800 | [diff] [blame] | 365 | TosaVersion ParseTosaSchemaVersion(std::string schema); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 366 | |
| 367 | private: |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 368 | TosaVersion _version; /* version struct */ |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 369 | flatbuffers::FlatBufferBuilder _builder; /* flatbuffer builder */ |
| 370 | flatbuffers::Parser _parser; /* flatbuffer parser, used for json parsing */ |
| 371 | std::vector<TosaSerializationBasicBlock*> _blocks; /* array structure to store all TosaSerializationBasicBlock */ |
| 372 | bool _schemaLoaded; /* is the schema properly loaded? */ |
| 373 | }; |
| 374 | |
| 375 | } // namespace tosa |
| 376 | |
| 377 | #endif // _TOSA_SERIALIZATION_HANDLER_H |