Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame^] | 1 | |
| 2 | // Copyright (c) 2020, 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_REFERENCE_TENSOR_H |
| 17 | #define TOSA_REFERENCE_TENSOR_H |
| 18 | |
| 19 | #include "model_common.h" |
| 20 | #include "ops/template_types.h" |
| 21 | #include "tosa_generated.h" |
| 22 | #include "tosa_serialization_handler.h" |
| 23 | #include <Eigen/CXX11/Tensor> |
| 24 | #include <list> |
| 25 | #include <vector> |
| 26 | |
| 27 | using namespace tosa; |
| 28 | |
| 29 | namespace TosaReference |
| 30 | { |
| 31 | class GraphNode; |
| 32 | |
| 33 | class Tensor |
| 34 | { |
| 35 | public: |
| 36 | Tensor(std::string tensorName_, |
| 37 | DType tensorDtype__, |
| 38 | const std::vector<Usage>& tensorUsage_, |
| 39 | const std::vector<Format>& tensorFormat_, |
| 40 | std::vector<int> shape_, |
| 41 | int isConst_); |
| 42 | |
| 43 | virtual ~Tensor(); |
| 44 | |
| 45 | int setIsSubgraphInput(); |
| 46 | int setIsSubgraphOutput(); |
| 47 | |
| 48 | int getIsSubgraphInput() const |
| 49 | { |
| 50 | return isSubgraphInput; |
| 51 | } |
| 52 | |
| 53 | int getIsSubgraphOutput() const |
| 54 | { |
| 55 | return isSubgraphOutput; |
| 56 | } |
| 57 | |
| 58 | int setProducer(GraphNode* node); |
| 59 | int addConsumer(GraphNode* node); |
| 60 | |
| 61 | int setIsValid() |
| 62 | { |
| 63 | isValid = 1; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int clearIsValid() |
| 68 | { |
| 69 | isValid = 0; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int getIsValid() const |
| 74 | { |
| 75 | return isValid; |
| 76 | } |
| 77 | |
| 78 | int getIsConst() const |
| 79 | { |
| 80 | return isConst; |
| 81 | } |
| 82 | |
| 83 | GraphNode* getProducer() |
| 84 | { |
| 85 | return producer; |
| 86 | } |
| 87 | |
| 88 | std::vector<GraphNode*>& getConsumers() |
| 89 | { |
| 90 | return consumers; |
| 91 | } |
| 92 | |
| 93 | const std::string& getName() const |
| 94 | { |
| 95 | return tensorName; |
| 96 | } |
| 97 | |
| 98 | const std::vector<int>& getShape() const |
| 99 | { |
| 100 | return shape; |
| 101 | } |
| 102 | |
| 103 | std::string getShapeAsString() const |
| 104 | { |
| 105 | std::string shape_str("["); |
| 106 | for (auto& dim : shape) |
| 107 | { |
| 108 | shape_str += (std::to_string(dim) + ", "); |
| 109 | } |
| 110 | shape_str.append("]"); |
| 111 | return shape_str; |
| 112 | } |
| 113 | |
| 114 | const std::vector<Usage>& getUsage() const |
| 115 | { |
| 116 | return tensorUsage; |
| 117 | } |
| 118 | |
| 119 | bool hasUsage(Usage usage) const |
| 120 | { |
| 121 | for (auto& usg : tensorUsage) |
| 122 | { |
| 123 | if (usg == usage) |
| 124 | { |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | std::string getUsageAsString() const |
| 132 | { |
| 133 | std::string usage_str("["); |
| 134 | for (auto& usg : tensorUsage) |
| 135 | { |
| 136 | usage_str += (std::string(EnumNamesUsage()[usg]) + ", "); |
| 137 | } |
| 138 | usage_str.append("]"); |
| 139 | return usage_str; |
| 140 | } |
| 141 | |
| 142 | const std::vector<Format>& getFormat() const |
| 143 | { |
| 144 | return tensorFormat; |
| 145 | } |
| 146 | |
| 147 | bool hasFormat(Format format) const |
| 148 | { |
| 149 | for (auto& fmt : tensorFormat) |
| 150 | { |
| 151 | if (fmt == format) |
| 152 | { |
| 153 | return true; |
| 154 | } |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | std::string getFormatAsString() const |
| 160 | { |
| 161 | std::string format_str("["); |
| 162 | for (auto& fmt : tensorFormat) |
| 163 | { |
| 164 | format_str += (std::string(EnumNamesFormat()[fmt]) + ", "); |
| 165 | } |
| 166 | format_str.append("]"); |
| 167 | return format_str; |
| 168 | } |
| 169 | |
| 170 | const uint32_t getElementCount() const |
| 171 | { |
| 172 | uint32_t elements = 1; |
| 173 | for (size_t i = 0; i < shape.size(); i++) |
| 174 | elements *= shape[i]; |
| 175 | |
| 176 | return elements; |
| 177 | } |
| 178 | |
| 179 | // Comparison of rank and type with other tensors |
| 180 | const int matchRank(const Tensor& ref) const |
| 181 | { |
| 182 | return (ref.shape.size() == shape.size()) ? 0 : 1; |
| 183 | } |
| 184 | |
| 185 | const int matchType(const Tensor& ref) const |
| 186 | { |
| 187 | return (ref.tensorDtype == tensorDtype) ? 0 : 1; |
| 188 | } |
| 189 | |
| 190 | const int matchRankType(const Tensor& ref) const |
| 191 | { |
| 192 | return (matchType(ref) || matchRank(ref)); |
| 193 | } |
| 194 | |
| 195 | const int matchRankTypeShape(const Tensor& ref, const bool broadcastOk = false) const |
| 196 | { |
| 197 | if (matchRankType(ref)) |
| 198 | return 1; |
| 199 | |
| 200 | for (size_t i = 0; i < shape.size(); i++) |
| 201 | { |
| 202 | if (shape[i] != ref.shape[i]) |
| 203 | { |
| 204 | if (!broadcastOk || |
| 205 | // For broadcasts, at least one operand must have size 1 |
| 206 | // if they don't both match |
| 207 | (broadcastOk && (shape[i] != 1 && ref.shape[i] != 1))) |
| 208 | { |
| 209 | return 1; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | // Sometimes we might want to match several semi-compatible types, |
| 218 | // so just check rank and size here |
| 219 | const int matchRankSize(const Tensor& ref) const |
| 220 | { |
| 221 | if (matchRank(ref)) |
| 222 | return 1; |
| 223 | |
| 224 | for (size_t i = 0; i < shape.size(); i++) |
| 225 | { |
| 226 | if (shape[i] != ref.shape[i]) |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | // Unary check to make sure rank matches |
| 234 | const int checkRequiredRank(const int exactRank) const |
| 235 | { |
| 236 | return (shape.size() == (size_t)exactRank) ? 0 : 1; |
| 237 | } |
| 238 | |
| 239 | const int checkRequiredRank(const int minRank, const int maxRank) const |
| 240 | { |
| 241 | return (shape.size() >= (size_t)minRank && shape.size() <= (size_t)maxRank) ? 0 : 1; |
| 242 | } |
| 243 | |
| 244 | const int getRank() const |
| 245 | { |
| 246 | return shape.size(); |
| 247 | } |
| 248 | |
| 249 | const DType getDtype() const |
| 250 | { |
| 251 | return tensorDtype; |
| 252 | } |
| 253 | |
| 254 | virtual int dumpTensor(FILE* out) const = 0; |
| 255 | virtual int dumpTensorParams(FILE* out) const; |
| 256 | virtual int dumpTensorParams(std::ostream& out) const; |
| 257 | |
| 258 | virtual int setTensorValueFloat(const size_t bufLen, const float* vals) = 0; |
| 259 | virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals) = 0; |
| 260 | virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals) = 0; |
| 261 | virtual int setTensorValueBool(const size_t bufLen, const bool* vals) = 0; |
| 262 | virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const = 0; |
| 263 | virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const = 0; |
| 264 | virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const = 0; |
| 265 | virtual int getTensorValueBool(const size_t bufLen, bool* ibuf) const = 0; |
| 266 | |
| 267 | virtual int readFromNpyFile(const char* filename); |
| 268 | virtual int writeToNpyFile(const char* filename) const; |
| 269 | virtual int copyValueFrom(Tensor* tensor) = 0; |
| 270 | |
| 271 | const char* bool_to_str(bool in) const |
| 272 | { |
| 273 | static const char* true_str = "true"; |
| 274 | static const char* false_str = "false"; |
| 275 | return in ? true_str : false_str; |
| 276 | } |
| 277 | |
| 278 | virtual int allocate() = 0; |
| 279 | virtual int deallocate() = 0; |
| 280 | virtual bool is_allocated() = 0; |
| 281 | |
| 282 | protected: |
| 283 | std::string tensorName; |
| 284 | DType tensorDtype; |
| 285 | std::vector<Usage> tensorUsage; |
| 286 | std::vector<Format> tensorFormat; |
| 287 | int isConst; |
| 288 | int isValid; |
| 289 | std::vector<int> shape; |
| 290 | int isSubgraphInput; |
| 291 | int isSubgraphOutput; |
| 292 | bool isAllocated; |
| 293 | |
| 294 | GraphNode* producer; |
| 295 | std::vector<GraphNode*> consumers; |
| 296 | |
| 297 | // Note: the Eigen::Tensor is not declared in Tensor |
| 298 | // Instead, the TensorTemplate class keeps the templated tensor |
| 299 | // declaration so that the graph manipulation tools are isolated |
| 300 | // from the templated tensor type. |
| 301 | // |
| 302 | // Operators need to be aware of the TensorTemplate<EigenTensor<type, rank>> type |
| 303 | // so that they can operate on the right types. |
| 304 | }; |
| 305 | |
| 306 | template <class T> |
| 307 | class TensorTemplate : public Tensor |
| 308 | { |
| 309 | public: |
| 310 | TensorTemplate(std::string tensorName_, |
| 311 | DType tensorDtype_, |
| 312 | const std::vector<Usage>& tensorUsage_, |
| 313 | const std::vector<Format>& tensorFormat_, |
| 314 | std::vector<int> shape_, |
| 315 | int isConst_) |
| 316 | : Tensor(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, isConst_) |
| 317 | { |
| 318 | tensor = nullptr; |
| 319 | } |
| 320 | |
| 321 | virtual ~TensorTemplate() |
| 322 | { |
| 323 | deallocate(); |
| 324 | } |
| 325 | |
| 326 | virtual int allocate() |
| 327 | { |
| 328 | tensor = new T(); |
| 329 | if (tensor) |
| 330 | return 0; |
| 331 | else |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | virtual int deallocate() |
| 336 | { |
| 337 | if (tensor) |
| 338 | { |
| 339 | delete tensor; |
| 340 | } |
| 341 | tensor = nullptr; |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | virtual bool is_allocated() |
| 346 | { |
| 347 | if (tensor) |
| 348 | { |
| 349 | return true; |
| 350 | } |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | T& getTensor() |
| 355 | { |
| 356 | return *tensor; |
| 357 | } |
| 358 | |
| 359 | virtual int dumpTensor(FILE* out) const; |
| 360 | |
| 361 | virtual int setTensorValueFloat(const size_t bufLen, const float* vals); |
| 362 | virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 363 | virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 364 | virtual int setTensorValueBool(const size_t bufLen, const bool* vals); |
| 365 | virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const; |
| 366 | virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const; |
| 367 | virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const; |
| 368 | virtual int getTensorValueBool(const size_t bufLen, bool* bbuf) const; |
| 369 | |
| 370 | virtual int copyValueFrom(Tensor* tensor); |
| 371 | |
| 372 | protected: |
| 373 | T* tensor; |
| 374 | }; |
| 375 | |
| 376 | // allocate() template specializations to allocate the different tensor sizes |
| 377 | // Let the compiler know here before the factory uses them, but define them in the .cc file. |
| 378 | template <> |
| 379 | int Tensor0<float>::allocate(); |
| 380 | template <> |
| 381 | int Tensor1<float>::allocate(); |
| 382 | template <> |
| 383 | int Tensor2<float>::allocate(); |
| 384 | template <> |
| 385 | int Tensor3<float>::allocate(); |
| 386 | template <> |
| 387 | int Tensor4<float>::allocate(); |
| 388 | template <> |
| 389 | int Tensor5<float>::allocate(); |
| 390 | template <> |
| 391 | int Tensor6<float>::allocate(); |
| 392 | |
| 393 | template <> |
| 394 | int Tensor0<int32_t>::allocate(); |
| 395 | template <> |
| 396 | int Tensor1<int32_t>::allocate(); |
| 397 | template <> |
| 398 | int Tensor2<int32_t>::allocate(); |
| 399 | template <> |
| 400 | int Tensor3<int32_t>::allocate(); |
| 401 | template <> |
| 402 | int Tensor4<int32_t>::allocate(); |
| 403 | template <> |
| 404 | int Tensor5<int32_t>::allocate(); |
| 405 | template <> |
| 406 | int Tensor6<int32_t>::allocate(); |
| 407 | |
| 408 | template <> |
| 409 | int Tensor0<int64_t>::allocate(); |
| 410 | template <> |
| 411 | int Tensor1<int64_t>::allocate(); |
| 412 | template <> |
| 413 | int Tensor2<int64_t>::allocate(); |
| 414 | template <> |
| 415 | int Tensor3<int64_t>::allocate(); |
| 416 | template <> |
| 417 | int Tensor4<int64_t>::allocate(); |
| 418 | template <> |
| 419 | int Tensor5<int64_t>::allocate(); |
| 420 | template <> |
| 421 | int Tensor6<int64_t>::allocate(); |
| 422 | |
| 423 | template <> |
| 424 | int Tensor0<bool>::allocate(); |
| 425 | template <> |
| 426 | int Tensor1<bool>::allocate(); |
| 427 | template <> |
| 428 | int Tensor2<bool>::allocate(); |
| 429 | template <> |
| 430 | int Tensor3<bool>::allocate(); |
| 431 | template <> |
| 432 | int Tensor4<bool>::allocate(); |
| 433 | template <> |
| 434 | int Tensor5<bool>::allocate(); |
| 435 | template <> |
| 436 | int Tensor6<bool>::allocate(); |
| 437 | |
| 438 | template <> |
| 439 | int Tensor0<float>::copyValueFrom(Tensor* src); |
| 440 | template <> |
| 441 | int Tensor1<float>::copyValueFrom(Tensor* src); |
| 442 | template <> |
| 443 | int Tensor2<float>::copyValueFrom(Tensor* src); |
| 444 | template <> |
| 445 | int Tensor3<float>::copyValueFrom(Tensor* src); |
| 446 | template <> |
| 447 | int Tensor4<float>::copyValueFrom(Tensor* src); |
| 448 | template <> |
| 449 | int Tensor5<float>::copyValueFrom(Tensor* src); |
| 450 | template <> |
| 451 | int Tensor6<float>::copyValueFrom(Tensor* src); |
| 452 | |
| 453 | template <> |
| 454 | int Tensor0<int32_t>::copyValueFrom(Tensor* src); |
| 455 | template <> |
| 456 | int Tensor1<int32_t>::copyValueFrom(Tensor* src); |
| 457 | template <> |
| 458 | int Tensor2<int32_t>::copyValueFrom(Tensor* src); |
| 459 | template <> |
| 460 | int Tensor3<int32_t>::copyValueFrom(Tensor* src); |
| 461 | template <> |
| 462 | int Tensor4<int32_t>::copyValueFrom(Tensor* src); |
| 463 | template <> |
| 464 | int Tensor5<int32_t>::copyValueFrom(Tensor* src); |
| 465 | template <> |
| 466 | int Tensor6<int32_t>::copyValueFrom(Tensor* src); |
| 467 | |
| 468 | template <> |
| 469 | int Tensor0<int64_t>::copyValueFrom(Tensor* src); |
| 470 | template <> |
| 471 | int Tensor1<int64_t>::copyValueFrom(Tensor* src); |
| 472 | template <> |
| 473 | int Tensor2<int64_t>::copyValueFrom(Tensor* src); |
| 474 | template <> |
| 475 | int Tensor3<int64_t>::copyValueFrom(Tensor* src); |
| 476 | template <> |
| 477 | int Tensor4<int64_t>::copyValueFrom(Tensor* src); |
| 478 | template <> |
| 479 | int Tensor5<int64_t>::copyValueFrom(Tensor* src); |
| 480 | template <> |
| 481 | int Tensor6<int64_t>::copyValueFrom(Tensor* src); |
| 482 | |
| 483 | template <> |
| 484 | int Tensor0<bool>::copyValueFrom(Tensor* src); |
| 485 | template <> |
| 486 | int Tensor1<bool>::copyValueFrom(Tensor* src); |
| 487 | template <> |
| 488 | int Tensor2<bool>::copyValueFrom(Tensor* src); |
| 489 | template <> |
| 490 | int Tensor3<bool>::copyValueFrom(Tensor* src); |
| 491 | template <> |
| 492 | int Tensor4<bool>::copyValueFrom(Tensor* src); |
| 493 | template <> |
| 494 | int Tensor5<bool>::copyValueFrom(Tensor* src); |
| 495 | template <> |
| 496 | int Tensor6<bool>::copyValueFrom(Tensor* src); |
| 497 | |
| 498 | template <> |
| 499 | int Tensor0<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 500 | template <> |
| 501 | int Tensor1<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 502 | template <> |
| 503 | int Tensor2<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 504 | template <> |
| 505 | int Tensor3<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 506 | template <> |
| 507 | int Tensor4<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 508 | template <> |
| 509 | int Tensor5<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 510 | template <> |
| 511 | int Tensor6<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 512 | |
| 513 | template <> |
| 514 | int Tensor0<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 515 | template <> |
| 516 | int Tensor1<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 517 | template <> |
| 518 | int Tensor2<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 519 | template <> |
| 520 | int Tensor3<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 521 | template <> |
| 522 | int Tensor4<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 523 | template <> |
| 524 | int Tensor5<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 525 | template <> |
| 526 | int Tensor6<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 527 | |
| 528 | template <> |
| 529 | int Tensor0<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 530 | template <> |
| 531 | int Tensor1<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 532 | template <> |
| 533 | int Tensor2<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 534 | template <> |
| 535 | int Tensor3<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 536 | template <> |
| 537 | int Tensor4<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 538 | template <> |
| 539 | int Tensor5<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 540 | template <> |
| 541 | int Tensor6<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 542 | |
| 543 | template <> |
| 544 | int Tensor0<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 545 | template <> |
| 546 | int Tensor1<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 547 | template <> |
| 548 | int Tensor2<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 549 | template <> |
| 550 | int Tensor3<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 551 | template <> |
| 552 | int Tensor4<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 553 | template <> |
| 554 | int Tensor5<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 555 | template <> |
| 556 | int Tensor6<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 557 | |
| 558 | template <> |
| 559 | int Tensor0<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 560 | template <> |
| 561 | int Tensor1<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 562 | template <> |
| 563 | int Tensor2<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 564 | template <> |
| 565 | int Tensor3<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 566 | template <> |
| 567 | int Tensor4<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 568 | template <> |
| 569 | int Tensor5<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 570 | template <> |
| 571 | int Tensor6<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 572 | |
| 573 | template <> |
| 574 | int Tensor0<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 575 | template <> |
| 576 | int Tensor1<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 577 | template <> |
| 578 | int Tensor2<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 579 | template <> |
| 580 | int Tensor3<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 581 | template <> |
| 582 | int Tensor4<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 583 | template <> |
| 584 | int Tensor5<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 585 | template <> |
| 586 | int Tensor6<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 587 | |
| 588 | template <> |
| 589 | int Tensor0<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 590 | template <> |
| 591 | int Tensor1<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 592 | template <> |
| 593 | int Tensor2<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 594 | template <> |
| 595 | int Tensor3<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 596 | template <> |
| 597 | int Tensor4<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 598 | template <> |
| 599 | int Tensor5<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 600 | template <> |
| 601 | int Tensor6<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 602 | |
| 603 | template <> |
| 604 | int Tensor0<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 605 | template <> |
| 606 | int Tensor1<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 607 | template <> |
| 608 | int Tensor2<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 609 | template <> |
| 610 | int Tensor3<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 611 | template <> |
| 612 | int Tensor4<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 613 | template <> |
| 614 | int Tensor5<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 615 | template <> |
| 616 | int Tensor6<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 617 | |
| 618 | // assume we only dump float type tensor now |
| 619 | template <> |
| 620 | int Tensor0<float>::dumpTensor(FILE* out) const; |
| 621 | template <> |
| 622 | int Tensor1<float>::dumpTensor(FILE* out) const; |
| 623 | template <> |
| 624 | int Tensor2<float>::dumpTensor(FILE* out) const; |
| 625 | template <> |
| 626 | int Tensor3<float>::dumpTensor(FILE* out) const; |
| 627 | template <> |
| 628 | int Tensor4<float>::dumpTensor(FILE* out) const; |
| 629 | template <> |
| 630 | int Tensor5<float>::dumpTensor(FILE* out) const; |
| 631 | template <> |
| 632 | int Tensor6<float>::dumpTensor(FILE* out) const; |
| 633 | template <> |
| 634 | int Tensor0<int32_t>::dumpTensor(FILE* out) const; |
| 635 | template <> |
| 636 | int Tensor1<int32_t>::dumpTensor(FILE* out) const; |
| 637 | template <> |
| 638 | int Tensor2<int32_t>::dumpTensor(FILE* out) const; |
| 639 | template <> |
| 640 | int Tensor3<int32_t>::dumpTensor(FILE* out) const; |
| 641 | template <> |
| 642 | int Tensor4<int32_t>::dumpTensor(FILE* out) const; |
| 643 | template <> |
| 644 | int Tensor5<int32_t>::dumpTensor(FILE* out) const; |
| 645 | template <> |
| 646 | int Tensor6<int32_t>::dumpTensor(FILE* out) const; |
| 647 | template <> |
| 648 | int Tensor0<int64_t>::dumpTensor(FILE* out) const; |
| 649 | template <> |
| 650 | int Tensor1<int64_t>::dumpTensor(FILE* out) const; |
| 651 | template <> |
| 652 | int Tensor2<int64_t>::dumpTensor(FILE* out) const; |
| 653 | template <> |
| 654 | int Tensor3<int64_t>::dumpTensor(FILE* out) const; |
| 655 | template <> |
| 656 | int Tensor4<int64_t>::dumpTensor(FILE* out) const; |
| 657 | template <> |
| 658 | int Tensor5<int64_t>::dumpTensor(FILE* out) const; |
| 659 | template <> |
| 660 | int Tensor6<int64_t>::dumpTensor(FILE* out) const; |
| 661 | template <> |
| 662 | int Tensor0<bool>::dumpTensor(FILE* out) const; |
| 663 | template <> |
| 664 | int Tensor1<bool>::dumpTensor(FILE* out) const; |
| 665 | template <> |
| 666 | int Tensor2<bool>::dumpTensor(FILE* out) const; |
| 667 | template <> |
| 668 | int Tensor3<bool>::dumpTensor(FILE* out) const; |
| 669 | template <> |
| 670 | int Tensor4<bool>::dumpTensor(FILE* out) const; |
| 671 | template <> |
| 672 | int Tensor5<bool>::dumpTensor(FILE* out) const; |
| 673 | template <> |
| 674 | int Tensor6<bool>::dumpTensor(FILE* out) const; |
| 675 | |
| 676 | class TensorFactory |
| 677 | { |
| 678 | public: |
| 679 | static Tensor* newTensor(std::string tensorName_, |
| 680 | DType tensorDtype_, |
| 681 | const std::vector<Usage>& tensorUsage_, |
| 682 | const std::vector<Format>& tensorFormat_, |
| 683 | std::vector<int> shape_, |
| 684 | int isConst_, |
| 685 | const uint32_t rank) |
| 686 | { |
| 687 | switch (tensorDtype_) |
| 688 | { |
| 689 | case DType_FLOAT: |
| 690 | switch (rank) |
| 691 | { |
| 692 | case 0: |
| 693 | return new Tensor0<float>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 694 | isConst_); |
| 695 | case 1: |
| 696 | return new Tensor1<float>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 697 | isConst_); |
| 698 | case 2: |
| 699 | return new Tensor2<float>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 700 | isConst_); |
| 701 | case 3: |
| 702 | return new Tensor3<float>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 703 | isConst_); |
| 704 | case 4: |
| 705 | return new Tensor4<float>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 706 | isConst_); |
| 707 | case 5: |
| 708 | return new Tensor5<float>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 709 | isConst_); |
| 710 | case 6: |
| 711 | return new Tensor6<float>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 712 | isConst_); |
| 713 | default: |
| 714 | goto done; |
| 715 | } |
| 716 | case DType_INT32: |
| 717 | case DType_AINT8: |
| 718 | case DType_UINT8: |
| 719 | case DType_INT4: |
| 720 | case DType_INT8: |
| 721 | case DType_INT16: |
| 722 | switch (rank) |
| 723 | { |
| 724 | case 0: |
| 725 | return new Tensor0<int32_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 726 | isConst_); |
| 727 | case 1: |
| 728 | return new Tensor1<int32_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 729 | isConst_); |
| 730 | case 2: |
| 731 | return new Tensor2<int32_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 732 | isConst_); |
| 733 | case 3: |
| 734 | return new Tensor3<int32_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 735 | isConst_); |
| 736 | case 4: |
| 737 | return new Tensor4<int32_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 738 | isConst_); |
| 739 | case 5: |
| 740 | return new Tensor5<int32_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 741 | isConst_); |
| 742 | case 6: |
| 743 | return new Tensor6<int32_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 744 | isConst_); |
| 745 | default: |
| 746 | goto done; |
| 747 | } |
| 748 | case DType_INT48: |
| 749 | switch (rank) |
| 750 | { |
| 751 | case 0: |
| 752 | return new Tensor0<int64_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 753 | isConst_); |
| 754 | case 1: |
| 755 | return new Tensor1<int64_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 756 | isConst_); |
| 757 | case 2: |
| 758 | return new Tensor2<int64_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 759 | isConst_); |
| 760 | case 3: |
| 761 | return new Tensor3<int64_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 762 | isConst_); |
| 763 | case 4: |
| 764 | return new Tensor4<int64_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 765 | isConst_); |
| 766 | case 5: |
| 767 | return new Tensor5<int64_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 768 | isConst_); |
| 769 | case 6: |
| 770 | return new Tensor6<int64_t>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 771 | isConst_); |
| 772 | default: |
| 773 | goto done; |
| 774 | } |
| 775 | case DType_BOOL: |
| 776 | switch (rank) |
| 777 | { |
| 778 | case 0: |
| 779 | return new Tensor0<bool>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 780 | isConst_); |
| 781 | case 1: |
| 782 | return new Tensor1<bool>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 783 | isConst_); |
| 784 | case 2: |
| 785 | return new Tensor2<bool>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 786 | isConst_); |
| 787 | case 3: |
| 788 | return new Tensor3<bool>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 789 | isConst_); |
| 790 | case 4: |
| 791 | return new Tensor4<bool>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 792 | isConst_); |
| 793 | case 5: |
| 794 | return new Tensor5<bool>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 795 | isConst_); |
| 796 | case 6: |
| 797 | return new Tensor6<bool>(tensorName_, tensorDtype_, tensorUsage_, tensorFormat_, shape_, |
| 798 | isConst_); |
| 799 | default: |
| 800 | goto done; |
| 801 | } |
| 802 | default: |
| 803 | goto done; |
| 804 | } |
| 805 | |
| 806 | done: |
| 807 | FATAL_ERROR("Unsupported tensor name=%s, type=%s, rank=%d", tensorName_.c_str(), EnumNamesDType()[tensorDtype_], |
| 808 | rank); |
| 809 | } |
| 810 | |
| 811 | static Tensor* newTensor(DType type, const std::vector<int> shape); |
| 812 | }; |
| 813 | }; // namespace TosaReference |
| 814 | |
| 815 | #endif |