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