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