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 |
| 31 | #define TOSA_VERSION_MINOR 23 |
| 32 | #define TOSA_VERSION_PATCH 0 |
Eric Kunze | 5867c9a | 2021-10-29 16:53:32 -0700 | [diff] [blame] | 33 | #define TOSA_VERSION_DRAFT false |
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); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 117 | TosaSerializationTensor(std::string& name, |
| 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 | } |
| 151 | |
| 152 | private: |
| 153 | DType _dtype; /* data type enumeration, see tosa_isa_generated.h */ |
| 154 | std::vector<int32_t> _shape; /* shape of the tensor */ |
| 155 | std::string _name; /* name of the tensor, used for solving dependency */ |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 156 | std::vector<uint8_t> _data; /* data array */ |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | class TosaSerializationOperator |
| 160 | { |
| 161 | public: |
| 162 | // use default copy, void constructor |
| 163 | // constructor and destructor |
| 164 | TosaSerializationOperator(Op op, |
| 165 | Attribute attribute_type, |
| 166 | const TosaAttributeBase* attribute, |
| 167 | QuantInfo qinfo_type, |
| 168 | const TosaQuantInfoBase* qinfo, |
| 169 | std::vector<std::string> input_tensor_names, |
| 170 | std::vector<std::string> output_tensor_names); |
| 171 | ~TosaSerializationOperator(); |
| 172 | |
| 173 | // accessor |
| 174 | Op GetOp() const |
| 175 | { |
| 176 | return _op; |
| 177 | } |
| 178 | Attribute GetAttributeType() const |
| 179 | { |
| 180 | return _attribute_type; |
| 181 | } |
| 182 | TosaAttributeBase* GetAttribute() const |
| 183 | { |
| 184 | return _attribute; |
| 185 | } |
| 186 | QuantInfo GetQInfoType() const |
| 187 | { |
| 188 | return _qinfo_type; |
| 189 | } |
| 190 | TosaQuantInfoBase* GetQInfo() const |
| 191 | { |
| 192 | return _qinfo; |
| 193 | } |
| 194 | std::vector<std::string>& GetInputTensorNames() |
| 195 | { |
| 196 | return _input_tensor_names; |
| 197 | } |
| 198 | std::vector<std::string>& GetOutputTensorNames() |
| 199 | { |
| 200 | return _output_tensor_names; |
| 201 | } |
| 202 | |
| 203 | private: |
| 204 | Op _op; /* operator enum, see tosa_isa_generated.h for enumeration table */ |
| 205 | Attribute _attribute_type; /* operator attribute enum, used for dynamic casting TosaAttributeBase class */ |
| 206 | TosaAttributeBase* _attribute; /* real attribute class goes here */ |
| 207 | QuantInfo _qinfo_type; /* QuantInfo enum */ |
| 208 | TosaQuantInfoBase* _qinfo; /* base class pointer of QuantInfo */ |
| 209 | std::vector<std::string> _input_tensor_names; /* array of input tensor names */ |
| 210 | std::vector<std::string> _output_tensor_names; /* array of output tensor names */ |
| 211 | }; |
| 212 | |
| 213 | class TosaSerializationBasicBlock |
| 214 | { |
| 215 | public: |
| 216 | // constructor and destructor |
| 217 | TosaSerializationBasicBlock(std::string name, |
| 218 | std::vector<TosaSerializationOperator*> operators, |
| 219 | std::vector<TosaSerializationTensor*> tensors, |
| 220 | std::vector<std::string> inputs, |
| 221 | std::vector<std::string> outputs); |
| 222 | ~TosaSerializationBasicBlock(); |
| 223 | |
| 224 | // accessor |
| 225 | std::string GetName() const |
| 226 | { |
| 227 | return _name; |
| 228 | } |
| 229 | std::vector<TosaSerializationOperator*>& GetOperators() |
| 230 | { |
| 231 | return _operators; |
| 232 | } |
| 233 | std::vector<TosaSerializationTensor*>& GetTensors() |
| 234 | { |
| 235 | return _tensors; |
| 236 | } |
| 237 | |
| 238 | TosaSerializationTensor* GetTensorByName(std::string name) |
| 239 | { |
| 240 | TosaSerializationTensor* result = nullptr; |
| 241 | for (auto tensor : GetTensors()) |
| 242 | { |
| 243 | if (tensor->GetName() == name) |
| 244 | { |
| 245 | result = tensor; |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | return result; |
| 250 | } |
| 251 | |
| 252 | std::vector<std::string>& GetInputs() |
| 253 | { |
| 254 | return _inputs; |
| 255 | } |
| 256 | std::vector<std::string>& GetOutputs() |
| 257 | { |
| 258 | return _outputs; |
| 259 | } |
| 260 | |
| 261 | private: |
| 262 | std::string _name; /* name of basic block */ |
| 263 | std::vector<TosaSerializationOperator*> _operators; /* TosaSerializationOperator list */ |
| 264 | std::vector<TosaSerializationTensor*> _tensors; /* TosaSerializationTensor list */ |
| 265 | std::vector<std::string> _inputs; /* array of string to specify block inputs */ |
| 266 | std::vector<std::string> _outputs; /* array of string to specify block outputs */ |
| 267 | }; |
| 268 | |
| 269 | /* |
| 270 | * this is a helper class for writing/reading Tosa ISA |
| 271 | * supported format: .tosa (flatbuffer), .json |
| 272 | * and provide high-level std::vector-like interface |
| 273 | * to access internal data structure |
| 274 | */ |
| 275 | class TosaSerializationHandler |
| 276 | { |
| 277 | public: |
| 278 | // constructor and destructor |
| 279 | TosaSerializationHandler(); |
| 280 | ~TosaSerializationHandler(); |
| 281 | |
| 282 | // file io |
| 283 | tosa_err_t LoadFileJson(const char* filename); |
| 284 | tosa_err_t LoadFileTosaFlatbuffer(const char* filename); |
| 285 | tosa_err_t SaveFileJson(const char* filename); |
| 286 | tosa_err_t SaveFileTosaFlatbuffer(const char* filename); |
| 287 | tosa_err_t LoadFileSchema(const char* schema_filename); |
| 288 | |
Kevin Cheng | 3bb1bc1 | 2021-06-17 15:57:08 -0700 | [diff] [blame] | 289 | // data format conversion. little-endian. |
| 290 | static tosa_err_t ConvertF32toU8(const std::vector<float>& in, std::vector<uint8_t>& out); |
| 291 | static tosa_err_t ConvertI48toU8(const std::vector<int64_t>& in, std::vector<uint8_t>& out); |
| 292 | static tosa_err_t ConvertI32toU8(const std::vector<int32_t>& in, std::vector<uint8_t>& out); |
| 293 | static tosa_err_t ConvertI16toU8(const std::vector<int16_t>& in, std::vector<uint8_t>& out); |
| 294 | 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] | 295 | 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] | 296 | static tosa_err_t ConvertBooltoU8(const std::vector<bool>& in, std::vector<uint8_t>& out); |
| 297 | |
| 298 | static tosa_err_t ConvertU8toF32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out); |
| 299 | static tosa_err_t ConvertU8toI48(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int64_t>& out); |
| 300 | static tosa_err_t ConvertU8toI32(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int32_t>& out); |
| 301 | static tosa_err_t ConvertU8toI16(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<int16_t>& out); |
| 302 | 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] | 303 | 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] | 304 | static tosa_err_t ConvertU8toBool(const std::vector<uint8_t>& in, uint32_t out_size, std::vector<bool>& out); |
| 305 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 306 | // version |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 307 | const TosaVersion& GetVersion() |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 308 | { |
| 309 | return _version; |
| 310 | } |
| 311 | |
| 312 | // accessor |
| 313 | std::vector<TosaSerializationBasicBlock*>& GetBlocks() |
| 314 | { |
| 315 | return _blocks; |
| 316 | } |
| 317 | |
| 318 | TosaSerializationBasicBlock* GetBlockByName(std::string name) |
| 319 | { |
| 320 | TosaSerializationBasicBlock* result = nullptr; |
| 321 | for (auto block : GetBlocks()) |
| 322 | { |
| 323 | if (block->GetName() == name) |
| 324 | { |
| 325 | result = block; |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | return result; |
| 330 | } |
| 331 | TosaSerializationBasicBlock* GetMainBlock() |
| 332 | { |
| 333 | TosaSerializationBasicBlock* main_block = GetBlockByName(std::string("main")); |
| 334 | assert(main_block); |
| 335 | return main_block; |
| 336 | } |
| 337 | |
| 338 | std::vector<std::string>& GetInputs() |
| 339 | { |
| 340 | return GetMainBlock()->GetInputs(); |
| 341 | } |
| 342 | std::vector<std::string>& GetOutputs() |
| 343 | { |
| 344 | return GetMainBlock()->GetOutputs(); |
| 345 | } |
| 346 | |
| 347 | bool GetSchemaLoaded() const |
| 348 | { |
| 349 | return _schemaLoaded; |
| 350 | } |
| 351 | |
| 352 | protected: |
| 353 | tosa_err_t Clear(); |
Kevin Cheng | b97cb1d | 2021-10-14 11:53:39 -0700 | [diff] [blame] | 354 | tosa_err_t Deserialize(const uint8_t* buf); |
| 355 | tosa_err_t Serialize(); |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 356 | |
| 357 | private: |
Kevin Cheng | e6563f5 | 2021-10-20 12:12:02 -0700 | [diff] [blame] | 358 | TosaVersion _version; /* version struct */ |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 359 | flatbuffers::FlatBufferBuilder _builder; /* flatbuffer builder */ |
| 360 | flatbuffers::Parser _parser; /* flatbuffer parser, used for json parsing */ |
| 361 | std::vector<TosaSerializationBasicBlock*> _blocks; /* array structure to store all TosaSerializationBasicBlock */ |
| 362 | bool _schemaLoaded; /* is the schema properly loaded? */ |
| 363 | }; |
| 364 | |
| 365 | } // namespace tosa |
| 366 | |
| 367 | #endif // _TOSA_SERIALIZATION_HANDLER_H |