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