Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Tai Ly | 8690a08 | 2023-12-18 20:40:24 +0000 | [diff] [blame] | 2 | // Copyright (c) 2020-2024, 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" |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 20 | #include "dtype.h" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 21 | #include "model_common.h" |
| 22 | #include "ops/template_types.h" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 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: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 37 | Tensor(const std::string tensorName_, const DType serializationDtype_, const 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 | |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 45 | bool getIsParentGraphOutput() const |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 46 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 47 | return isParentGraphOutput; |
| 48 | } |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 49 | int setIsVariable(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 50 | |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 51 | bool getIsSubgraphInput() const |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 52 | { |
| 53 | return isSubgraphInput; |
| 54 | } |
| 55 | |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 56 | bool getIsSubgraphOutput() const |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 57 | { |
| 58 | return isSubgraphOutput; |
| 59 | } |
| 60 | |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 61 | bool getIsVariable() const |
| 62 | { |
| 63 | return isVariable; |
| 64 | } |
| 65 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 66 | int setProducer(GraphNode* node); |
| 67 | int addConsumer(GraphNode* node); |
| 68 | |
| 69 | int setIsValid() |
| 70 | { |
| 71 | isValid = 1; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int clearIsValid() |
| 76 | { |
| 77 | isValid = 0; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int getIsValid() const |
| 82 | { |
| 83 | return isValid; |
| 84 | } |
| 85 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 86 | GraphNode* getProducer() |
| 87 | { |
| 88 | return producer; |
| 89 | } |
| 90 | |
| 91 | std::vector<GraphNode*>& getConsumers() |
| 92 | { |
| 93 | return consumers; |
| 94 | } |
| 95 | |
| 96 | const std::string& getName() const |
| 97 | { |
| 98 | return tensorName; |
| 99 | } |
| 100 | |
| 101 | const std::vector<int>& getShape() const |
| 102 | { |
| 103 | return shape; |
| 104 | } |
| 105 | |
Jerry Ge | 264f7fa | 2023-04-21 22:49:57 +0000 | [diff] [blame] | 106 | void setDimSize(size_t dim, uint32_t new_size) |
| 107 | { |
| 108 | this->shape[dim] = new_size; |
| 109 | return; |
| 110 | } |
| 111 | |
Jerry Ge | 12159fc | 2024-04-01 17:05:10 +0000 | [diff] [blame^] | 112 | void setShapeValue(std::vector<int>& shapeValue) |
| 113 | { |
| 114 | for (auto dim : shapeValue) |
| 115 | { |
| 116 | this->shapeValue.push_back(dim); |
| 117 | } |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | int getShapeValueSize() const |
| 122 | { |
| 123 | return this->shapeValue.size(); |
| 124 | } |
| 125 | |
| 126 | std::string getShapeValueAsString() const |
| 127 | { |
| 128 | std::string shape_str("["); |
| 129 | for (auto& dim : shapeValue) |
| 130 | { |
| 131 | shape_str += (std::to_string(dim) + ", "); |
| 132 | } |
| 133 | shape_str.append("]"); |
| 134 | return shape_str; |
| 135 | } |
| 136 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 137 | std::string getShapeAsString() const |
| 138 | { |
| 139 | std::string shape_str("["); |
| 140 | for (auto& dim : shape) |
| 141 | { |
| 142 | shape_str += (std::to_string(dim) + ", "); |
| 143 | } |
| 144 | shape_str.append("]"); |
| 145 | return shape_str; |
| 146 | } |
| 147 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 148 | const uint32_t getElementCount() const |
| 149 | { |
| 150 | uint32_t elements = 1; |
| 151 | for (size_t i = 0; i < shape.size(); i++) |
| 152 | elements *= shape[i]; |
| 153 | |
| 154 | return elements; |
| 155 | } |
| 156 | |
| 157 | // Comparison of rank and type with other tensors |
| 158 | const int matchRank(const Tensor& ref) const |
| 159 | { |
| 160 | return (ref.shape.size() == shape.size()) ? 0 : 1; |
| 161 | } |
| 162 | |
| 163 | const int matchType(const Tensor& ref) const |
| 164 | { |
| 165 | return (ref.tensorDtype == tensorDtype) ? 0 : 1; |
| 166 | } |
| 167 | |
| 168 | const int matchRankType(const Tensor& ref) const |
| 169 | { |
| 170 | return (matchType(ref) || matchRank(ref)); |
| 171 | } |
| 172 | |
| 173 | const int matchRankTypeShape(const Tensor& ref, const bool broadcastOk = false) const |
| 174 | { |
| 175 | if (matchRankType(ref)) |
| 176 | return 1; |
| 177 | |
| 178 | for (size_t i = 0; i < shape.size(); i++) |
| 179 | { |
| 180 | if (shape[i] != ref.shape[i]) |
| 181 | { |
| 182 | if (!broadcastOk || |
Kevin Cheng | 2131a4d | 2021-11-11 19:35:30 +0000 | [diff] [blame] | 183 | // For broadcasts, the order of *this and ref matters. |
| 184 | // *this should be the source tensor. |
| 185 | // ref should be the target tensor. In most of the case, ref is expected to be the output tensor. |
| 186 | // this->shape must have size 1 if they don't match |
| 187 | (broadcastOk && (shape[i] != 1))) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 188 | { |
| 189 | return 1; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 197 | const int matchRankShape(const Tensor& ref, const bool broadcastOk = false) const |
| 198 | { |
| 199 | if (matchRank(ref)) |
| 200 | return 1; |
| 201 | |
| 202 | for (size_t i = 0; i < shape.size(); i++) |
| 203 | { |
| 204 | if (shape[i] != ref.shape[i]) |
| 205 | { |
| 206 | if (!broadcastOk || |
Kevin Cheng | 2131a4d | 2021-11-11 19:35:30 +0000 | [diff] [blame] | 207 | // For broadcasts, the order of *this and ref matters. |
| 208 | // *this should be the source tensor. |
| 209 | // ref should be the target tensor. In most of the case, ref is expected to be the output tensor. |
| 210 | // this->shape must have size 1 if they don't match |
| 211 | (broadcastOk && (shape[i] != 1))) |
Kevin Cheng | 1c3c847 | 2021-11-08 11:19:10 -0800 | [diff] [blame] | 212 | { |
| 213 | return 1; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 221 | // Sometimes we might want to match several semi-compatible types, |
| 222 | // so just check rank and size here |
| 223 | const int matchRankSize(const Tensor& ref) const |
| 224 | { |
| 225 | if (matchRank(ref)) |
| 226 | return 1; |
| 227 | |
| 228 | for (size_t i = 0; i < shape.size(); i++) |
| 229 | { |
| 230 | if (shape[i] != ref.shape[i]) |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | // Unary check to make sure rank matches |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 238 | const int checkRequiredRank(const int minRank) const |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 239 | { |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 240 | return (shape.size() >= (size_t)minRank) ? 0 : 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | const int checkRequiredRank(const int minRank, const int maxRank) const |
| 244 | { |
| 245 | return (shape.size() >= (size_t)minRank && shape.size() <= (size_t)maxRank) ? 0 : 1; |
| 246 | } |
| 247 | |
| 248 | const int getRank() const |
| 249 | { |
| 250 | return shape.size(); |
| 251 | } |
| 252 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 253 | const TOSA_REF_TYPE getDtype() const |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 254 | { |
| 255 | return tensorDtype; |
| 256 | } |
| 257 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 258 | const DType getSerializationDtype() const |
| 259 | { |
| 260 | return serializationDtype; |
| 261 | } |
| 262 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 263 | virtual int dumpTensor(FILE* out) const = 0; |
| 264 | virtual int dumpTensorParams(FILE* out) const; |
| 265 | virtual int dumpTensorParams(std::ostream& out) const; |
| 266 | |
Jerry Ge | 20ab3df | 2024-01-26 16:56:55 +0000 | [diff] [blame] | 267 | virtual int setTensorValueDouble(const size_t bufLen, const double* vals) = 0; |
| 268 | virtual int setTensorValueFloat(const size_t bufLen, const float* vals) = 0; |
| 269 | virtual int setTensorValueUInt8(const size_t bufLen, const uint8_t* vals) = 0; |
| 270 | virtual int setTensorValueInt8(const size_t bufLen, const int8_t* vals) = 0; |
| 271 | virtual int setTensorValueUInt16(const size_t bufLen, const uint16_t* vals) = 0; |
| 272 | virtual int setTensorValueInt16(const size_t bufLen, const int16_t* vals) = 0; |
| 273 | virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals) = 0; |
| 274 | virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals) = 0; |
| 275 | virtual int setTensorValueBool(const size_t bufLen, const bool* vals) = 0; |
| 276 | virtual int getTensorValueDouble(const size_t bufLen, double* fbuf) const = 0; |
| 277 | virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const = 0; |
| 278 | virtual int getTensorValueUInt8(const size_t bufLen, uint8_t* ibuf) const = 0; |
| 279 | virtual int getTensorValueInt8(const size_t bufLen, int8_t* ibuf) const = 0; |
| 280 | virtual int getTensorValueUInt16(const size_t bufLen, uint16_t* ibuf) const = 0; |
| 281 | virtual int getTensorValueInt16(const size_t bufLen, int16_t* ibuf) const = 0; |
| 282 | virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const = 0; |
| 283 | virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const = 0; |
| 284 | virtual int getTensorValueBool(const size_t bufLen, bool* ibuf) const = 0; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 285 | |
| 286 | virtual int readFromNpyFile(const char* filename); |
| 287 | virtual int writeToNpyFile(const char* filename) const; |
| 288 | virtual int copyValueFrom(Tensor* tensor) = 0; |
| 289 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 290 | virtual int readfromVector(const ArrayProxy<double> vals); |
Grant Watson | 64285a1 | 2022-11-16 15:32:39 +0000 | [diff] [blame] | 291 | virtual int readfromVector(const ArrayProxy<float> vals); |
| 292 | virtual int readfromVector(const ArrayProxy<half_float::half> vals); |
Jerry Ge | c529169 | 2024-01-02 22:29:08 +0000 | [diff] [blame] | 293 | virtual int readfromVector(const ArrayProxy<int8_t> vals); |
Jerry Ge | 20ab3df | 2024-01-26 16:56:55 +0000 | [diff] [blame] | 294 | virtual int readfromVector(const ArrayProxy<uint16_t> vals); |
Georgios Pinitas | e905977 | 2023-12-06 18:52:30 +0000 | [diff] [blame] | 295 | virtual int readfromVector(const ArrayProxy<int16_t> vals); |
Grant Watson | 64285a1 | 2022-11-16 15:32:39 +0000 | [diff] [blame] | 296 | virtual int readfromVector(const ArrayProxy<int32_t> vals); |
| 297 | virtual int readfromVector(const ArrayProxy<int64_t> vals); |
| 298 | virtual int readfromVector(const ArrayProxy<unsigned char> vals); |
Matthew Sloyan | ba5fad3 | 2022-09-26 13:31:43 +0100 | [diff] [blame] | 299 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 300 | virtual int writeToVector(ArrayProxy<double> vals); |
Grant Watson | 64285a1 | 2022-11-16 15:32:39 +0000 | [diff] [blame] | 301 | virtual int writeToVector(ArrayProxy<float> vals); |
| 302 | virtual int writeToVector(ArrayProxy<half_float::half> vals); |
Jerry Ge | c529169 | 2024-01-02 22:29:08 +0000 | [diff] [blame] | 303 | virtual int writeToVector(ArrayProxy<int8_t> vals); |
Jerry Ge | 20ab3df | 2024-01-26 16:56:55 +0000 | [diff] [blame] | 304 | virtual int writeToVector(ArrayProxy<uint16_t> vals); |
Georgios Pinitas | e905977 | 2023-12-06 18:52:30 +0000 | [diff] [blame] | 305 | virtual int writeToVector(ArrayProxy<int16_t> vals); |
Grant Watson | 64285a1 | 2022-11-16 15:32:39 +0000 | [diff] [blame] | 306 | virtual int writeToVector(ArrayProxy<int32_t> vals); |
| 307 | virtual int writeToVector(ArrayProxy<int64_t> vals); |
| 308 | virtual int writeToVector(ArrayProxy<unsigned char> vals); |
Matthew Sloyan | ba5fad3 | 2022-09-26 13:31:43 +0100 | [diff] [blame] | 309 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 310 | const char* bool_to_str(bool in) const |
| 311 | { |
| 312 | static const char* true_str = "true"; |
| 313 | static const char* false_str = "false"; |
| 314 | return in ? true_str : false_str; |
| 315 | } |
| 316 | |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 317 | virtual int allocate() = 0; |
| 318 | virtual int deallocate() = 0; |
| 319 | virtual bool is_allocated() const = 0; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 320 | |
| 321 | protected: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 322 | const std::string tensorName; |
| 323 | const DType serializationDtype; |
Jerry Ge | 264f7fa | 2023-04-21 22:49:57 +0000 | [diff] [blame] | 324 | std::vector<int> shape; |
Jerry Ge | 12159fc | 2024-04-01 17:05:10 +0000 | [diff] [blame^] | 325 | std::vector<int> shapeValue; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 326 | const TOSA_REF_TYPE tensorDtype; |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 327 | bool isValid; |
| 328 | bool isSubgraphInput; |
| 329 | bool isSubgraphOutput; |
| 330 | bool isVariable; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 331 | bool isAllocated; |
| 332 | |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 333 | bool isParentGraphOutput; |
| 334 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 335 | GraphNode* producer; |
| 336 | std::vector<GraphNode*> consumers; |
| 337 | |
| 338 | // Note: the Eigen::Tensor is not declared in Tensor |
| 339 | // Instead, the TensorTemplate class keeps the templated tensor |
| 340 | // declaration so that the graph manipulation tools are isolated |
| 341 | // from the templated tensor type. |
| 342 | // |
| 343 | // Operators need to be aware of the TensorTemplate<EigenTensor<type, rank>> type |
| 344 | // so that they can operate on the right types. |
| 345 | }; |
| 346 | |
| 347 | template <class T> |
| 348 | class TensorTemplate : public Tensor |
| 349 | { |
| 350 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 351 | TensorTemplate(const std::string tensorName_, const DType dtype_, const std::vector<int> shape_) |
| 352 | : Tensor(tensorName_, dtype_, shape_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 353 | { |
| 354 | tensor = nullptr; |
| 355 | } |
| 356 | |
| 357 | virtual ~TensorTemplate() |
| 358 | { |
| 359 | deallocate(); |
| 360 | } |
| 361 | |
| 362 | virtual int allocate() |
| 363 | { |
| 364 | tensor = new T(); |
| 365 | if (tensor) |
| 366 | return 0; |
| 367 | else |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | virtual int deallocate() |
| 372 | { |
| 373 | if (tensor) |
| 374 | { |
Eric Kunze | 9a36755 | 2023-07-11 13:27:36 -0700 | [diff] [blame] | 375 | DEBUG_INFO(GT, "Deallocating tensor %s", tensorName.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 376 | delete tensor; |
| 377 | } |
| 378 | tensor = nullptr; |
| 379 | return 0; |
| 380 | } |
| 381 | |
Tai Ly | cf84bc9 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 382 | virtual bool is_allocated() const |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 383 | { |
| 384 | if (tensor) |
| 385 | { |
| 386 | return true; |
| 387 | } |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | T& getTensor() |
| 392 | { |
| 393 | return *tensor; |
| 394 | } |
| 395 | |
| 396 | virtual int dumpTensor(FILE* out) const; |
| 397 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 398 | virtual int setTensorValueDouble(const size_t bufLen, const double* vals); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 399 | virtual int setTensorValueFloat(const size_t bufLen, const float* vals); |
Jerry Ge | c529169 | 2024-01-02 22:29:08 +0000 | [diff] [blame] | 400 | virtual int setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 401 | virtual int setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
Jerry Ge | 20ab3df | 2024-01-26 16:56:55 +0000 | [diff] [blame] | 402 | virtual int setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
Georgios Pinitas | e905977 | 2023-12-06 18:52:30 +0000 | [diff] [blame] | 403 | virtual int setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 404 | virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 405 | virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 406 | virtual int setTensorValueBool(const size_t bufLen, const bool* vals); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 407 | |
| 408 | virtual int getTensorValueDouble(const size_t bufLen, double* fbuf) const; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 409 | virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const; |
Jerry Ge | c529169 | 2024-01-02 22:29:08 +0000 | [diff] [blame] | 410 | virtual int getTensorValueUInt8(const size_t bufLen, uint8_t* ibuf) const; |
| 411 | virtual int getTensorValueInt8(const size_t bufLen, int8_t* ibuf) const; |
Jerry Ge | 20ab3df | 2024-01-26 16:56:55 +0000 | [diff] [blame] | 412 | virtual int getTensorValueUInt16(const size_t bufLen, uint16_t* ibuf) const; |
Georgios Pinitas | e905977 | 2023-12-06 18:52:30 +0000 | [diff] [blame] | 413 | virtual int getTensorValueInt16(const size_t bufLen, int16_t* ibuf) const; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 414 | virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const; |
| 415 | virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const; |
| 416 | virtual int getTensorValueBool(const size_t bufLen, bool* bbuf) const; |
| 417 | |
| 418 | virtual int copyValueFrom(Tensor* tensor); |
| 419 | |
| 420 | protected: |
| 421 | T* tensor; |
| 422 | }; |
| 423 | |
| 424 | // allocate() template specializations to allocate the different tensor sizes |
| 425 | // Let the compiler know here before the factory uses them, but define them in the .cc file. |
| 426 | template <> |
| 427 | int Tensor0<float>::allocate(); |
| 428 | template <> |
| 429 | int Tensor1<float>::allocate(); |
| 430 | template <> |
| 431 | int Tensor2<float>::allocate(); |
| 432 | template <> |
| 433 | int Tensor3<float>::allocate(); |
| 434 | template <> |
| 435 | int Tensor4<float>::allocate(); |
| 436 | template <> |
| 437 | int Tensor5<float>::allocate(); |
| 438 | template <> |
| 439 | int Tensor6<float>::allocate(); |
| 440 | |
| 441 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 442 | int Tensor0<double>::allocate(); |
| 443 | template <> |
| 444 | int Tensor1<double>::allocate(); |
| 445 | template <> |
| 446 | int Tensor2<double>::allocate(); |
| 447 | template <> |
| 448 | int Tensor3<double>::allocate(); |
| 449 | template <> |
| 450 | int Tensor4<double>::allocate(); |
| 451 | template <> |
| 452 | int Tensor5<double>::allocate(); |
| 453 | template <> |
| 454 | int Tensor6<double>::allocate(); |
| 455 | |
| 456 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 457 | int Tensor0<int32_t>::allocate(); |
| 458 | template <> |
| 459 | int Tensor1<int32_t>::allocate(); |
| 460 | template <> |
| 461 | int Tensor2<int32_t>::allocate(); |
| 462 | template <> |
| 463 | int Tensor3<int32_t>::allocate(); |
| 464 | template <> |
| 465 | int Tensor4<int32_t>::allocate(); |
| 466 | template <> |
| 467 | int Tensor5<int32_t>::allocate(); |
| 468 | template <> |
| 469 | int Tensor6<int32_t>::allocate(); |
| 470 | |
| 471 | template <> |
| 472 | int Tensor0<int64_t>::allocate(); |
| 473 | template <> |
| 474 | int Tensor1<int64_t>::allocate(); |
| 475 | template <> |
| 476 | int Tensor2<int64_t>::allocate(); |
| 477 | template <> |
| 478 | int Tensor3<int64_t>::allocate(); |
| 479 | template <> |
| 480 | int Tensor4<int64_t>::allocate(); |
| 481 | template <> |
| 482 | int Tensor5<int64_t>::allocate(); |
| 483 | template <> |
| 484 | int Tensor6<int64_t>::allocate(); |
| 485 | |
| 486 | template <> |
| 487 | int Tensor0<bool>::allocate(); |
| 488 | template <> |
| 489 | int Tensor1<bool>::allocate(); |
| 490 | template <> |
| 491 | int Tensor2<bool>::allocate(); |
| 492 | template <> |
| 493 | int Tensor3<bool>::allocate(); |
| 494 | template <> |
| 495 | int Tensor4<bool>::allocate(); |
| 496 | template <> |
| 497 | int Tensor5<bool>::allocate(); |
| 498 | template <> |
| 499 | int Tensor6<bool>::allocate(); |
| 500 | |
| 501 | template <> |
| 502 | int Tensor0<float>::copyValueFrom(Tensor* src); |
| 503 | template <> |
| 504 | int Tensor1<float>::copyValueFrom(Tensor* src); |
| 505 | template <> |
| 506 | int Tensor2<float>::copyValueFrom(Tensor* src); |
| 507 | template <> |
| 508 | int Tensor3<float>::copyValueFrom(Tensor* src); |
| 509 | template <> |
| 510 | int Tensor4<float>::copyValueFrom(Tensor* src); |
| 511 | template <> |
| 512 | int Tensor5<float>::copyValueFrom(Tensor* src); |
| 513 | template <> |
| 514 | int Tensor6<float>::copyValueFrom(Tensor* src); |
| 515 | |
| 516 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 517 | int Tensor0<double>::copyValueFrom(Tensor* src); |
| 518 | template <> |
| 519 | int Tensor1<double>::copyValueFrom(Tensor* src); |
| 520 | template <> |
| 521 | int Tensor2<double>::copyValueFrom(Tensor* src); |
| 522 | template <> |
| 523 | int Tensor3<double>::copyValueFrom(Tensor* src); |
| 524 | template <> |
| 525 | int Tensor4<double>::copyValueFrom(Tensor* src); |
| 526 | template <> |
| 527 | int Tensor5<double>::copyValueFrom(Tensor* src); |
| 528 | template <> |
| 529 | int Tensor6<double>::copyValueFrom(Tensor* src); |
| 530 | |
| 531 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 532 | int Tensor0<int32_t>::copyValueFrom(Tensor* src); |
| 533 | template <> |
| 534 | int Tensor1<int32_t>::copyValueFrom(Tensor* src); |
| 535 | template <> |
| 536 | int Tensor2<int32_t>::copyValueFrom(Tensor* src); |
| 537 | template <> |
| 538 | int Tensor3<int32_t>::copyValueFrom(Tensor* src); |
| 539 | template <> |
| 540 | int Tensor4<int32_t>::copyValueFrom(Tensor* src); |
| 541 | template <> |
| 542 | int Tensor5<int32_t>::copyValueFrom(Tensor* src); |
| 543 | template <> |
| 544 | int Tensor6<int32_t>::copyValueFrom(Tensor* src); |
| 545 | |
| 546 | template <> |
| 547 | int Tensor0<int64_t>::copyValueFrom(Tensor* src); |
| 548 | template <> |
| 549 | int Tensor1<int64_t>::copyValueFrom(Tensor* src); |
| 550 | template <> |
| 551 | int Tensor2<int64_t>::copyValueFrom(Tensor* src); |
| 552 | template <> |
| 553 | int Tensor3<int64_t>::copyValueFrom(Tensor* src); |
| 554 | template <> |
| 555 | int Tensor4<int64_t>::copyValueFrom(Tensor* src); |
| 556 | template <> |
| 557 | int Tensor5<int64_t>::copyValueFrom(Tensor* src); |
| 558 | template <> |
| 559 | int Tensor6<int64_t>::copyValueFrom(Tensor* src); |
| 560 | |
| 561 | template <> |
| 562 | int Tensor0<bool>::copyValueFrom(Tensor* src); |
| 563 | template <> |
| 564 | int Tensor1<bool>::copyValueFrom(Tensor* src); |
| 565 | template <> |
| 566 | int Tensor2<bool>::copyValueFrom(Tensor* src); |
| 567 | template <> |
| 568 | int Tensor3<bool>::copyValueFrom(Tensor* src); |
| 569 | template <> |
| 570 | int Tensor4<bool>::copyValueFrom(Tensor* src); |
| 571 | template <> |
| 572 | int Tensor5<bool>::copyValueFrom(Tensor* src); |
| 573 | template <> |
| 574 | int Tensor6<bool>::copyValueFrom(Tensor* src); |
| 575 | |
| 576 | template <> |
Jerry Ge | c529169 | 2024-01-02 22:29:08 +0000 | [diff] [blame] | 577 | int Tensor0<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 578 | template <> |
| 579 | int Tensor1<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 580 | template <> |
| 581 | int Tensor2<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 582 | template <> |
| 583 | int Tensor3<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 584 | template <> |
| 585 | int Tensor4<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 586 | template <> |
| 587 | int Tensor5<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 588 | template <> |
| 589 | int Tensor6<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals); |
| 590 | |
| 591 | template <> |
| 592 | int Tensor0<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
| 593 | template <> |
| 594 | int Tensor1<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
| 595 | template <> |
| 596 | int Tensor2<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
| 597 | template <> |
| 598 | int Tensor3<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
| 599 | template <> |
| 600 | int Tensor4<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
| 601 | template <> |
| 602 | int Tensor5<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
| 603 | template <> |
| 604 | int Tensor6<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals); |
| 605 | |
| 606 | template <> |
Jerry Ge | 20ab3df | 2024-01-26 16:56:55 +0000 | [diff] [blame] | 607 | int Tensor0<int32_t>::setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
| 608 | template <> |
| 609 | int Tensor1<int32_t>::setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
| 610 | template <> |
| 611 | int Tensor2<int32_t>::setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
| 612 | template <> |
| 613 | int Tensor3<int32_t>::setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
| 614 | template <> |
| 615 | int Tensor4<int32_t>::setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
| 616 | template <> |
| 617 | int Tensor5<int32_t>::setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
| 618 | template <> |
| 619 | int Tensor6<int32_t>::setTensorValueUInt16(const size_t bufLen, const uint16_t* vals); |
| 620 | |
| 621 | template <> |
Georgios Pinitas | e905977 | 2023-12-06 18:52:30 +0000 | [diff] [blame] | 622 | int Tensor0<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
| 623 | template <> |
| 624 | int Tensor1<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
| 625 | template <> |
| 626 | int Tensor2<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
| 627 | template <> |
| 628 | int Tensor3<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
| 629 | template <> |
| 630 | int Tensor4<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
| 631 | template <> |
| 632 | int Tensor5<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
| 633 | template <> |
| 634 | int Tensor6<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals); |
| 635 | |
| 636 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 637 | int Tensor0<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 638 | template <> |
| 639 | int Tensor1<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 640 | template <> |
| 641 | int Tensor2<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 642 | template <> |
| 643 | int Tensor3<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 644 | template <> |
| 645 | int Tensor4<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 646 | template <> |
| 647 | int Tensor5<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 648 | template <> |
| 649 | int Tensor6<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals); |
| 650 | |
| 651 | template <> |
Jerry Ge | c529169 | 2024-01-02 22:29:08 +0000 | [diff] [blame] | 652 | int Tensor0<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const; |
| 653 | template <> |
| 654 | int Tensor1<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const; |
| 655 | template <> |
| 656 | int Tensor2<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const; |
| 657 | template <> |
| 658 | int Tensor3<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const; |
| 659 | template <> |
| 660 | int Tensor4<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const; |
| 661 | template <> |
| 662 | int Tensor5<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const; |
| 663 | template <> |
| 664 | int Tensor6<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const; |
| 665 | |
| 666 | template <> |
| 667 | int Tensor0<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const; |
| 668 | template <> |
| 669 | int Tensor1<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const; |
| 670 | template <> |
| 671 | int Tensor2<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const; |
| 672 | template <> |
| 673 | int Tensor3<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const; |
| 674 | template <> |
| 675 | int Tensor4<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const; |
| 676 | template <> |
| 677 | int Tensor5<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const; |
| 678 | template <> |
| 679 | int Tensor6<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const; |
| 680 | |
| 681 | template <> |
Jerry Ge | 20ab3df | 2024-01-26 16:56:55 +0000 | [diff] [blame] | 682 | int Tensor0<int32_t>::getTensorValueUInt16(const size_t bufLen, uint16_t* vals) const; |
| 683 | template <> |
| 684 | int Tensor1<int32_t>::getTensorValueUInt16(const size_t bufLen, uint16_t* vals) const; |
| 685 | template <> |
| 686 | int Tensor2<int32_t>::getTensorValueUInt16(const size_t bufLen, uint16_t* vals) const; |
| 687 | template <> |
| 688 | int Tensor3<int32_t>::getTensorValueUInt16(const size_t bufLen, uint16_t* vals) const; |
| 689 | template <> |
| 690 | int Tensor4<int32_t>::getTensorValueUInt16(const size_t bufLen, uint16_t* vals) const; |
| 691 | template <> |
| 692 | int Tensor5<int32_t>::getTensorValueUInt16(const size_t bufLen, uint16_t* vals) const; |
| 693 | template <> |
| 694 | int Tensor6<int32_t>::getTensorValueUInt16(const size_t bufLen, uint16_t* vals) const; |
| 695 | |
| 696 | template <> |
Georgios Pinitas | e905977 | 2023-12-06 18:52:30 +0000 | [diff] [blame] | 697 | int Tensor0<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const; |
| 698 | template <> |
| 699 | int Tensor1<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const; |
| 700 | template <> |
| 701 | int Tensor2<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const; |
| 702 | template <> |
| 703 | int Tensor3<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const; |
| 704 | template <> |
| 705 | int Tensor4<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const; |
| 706 | template <> |
| 707 | int Tensor5<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const; |
| 708 | template <> |
| 709 | int Tensor6<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const; |
| 710 | |
| 711 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 712 | int Tensor0<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 713 | template <> |
| 714 | int Tensor1<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 715 | template <> |
| 716 | int Tensor2<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 717 | template <> |
| 718 | int Tensor3<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 719 | template <> |
| 720 | int Tensor4<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 721 | template <> |
| 722 | int Tensor5<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 723 | template <> |
| 724 | int Tensor6<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const; |
| 725 | |
| 726 | template <> |
| 727 | int Tensor0<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 728 | template <> |
| 729 | int Tensor1<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 730 | template <> |
| 731 | int Tensor2<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 732 | template <> |
| 733 | int Tensor3<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 734 | template <> |
| 735 | int Tensor4<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 736 | template <> |
| 737 | int Tensor5<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 738 | template <> |
| 739 | int Tensor6<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals); |
| 740 | |
| 741 | template <> |
| 742 | int Tensor0<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 743 | template <> |
| 744 | int Tensor1<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 745 | template <> |
| 746 | int Tensor2<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 747 | template <> |
| 748 | int Tensor3<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 749 | template <> |
| 750 | int Tensor4<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 751 | template <> |
| 752 | int Tensor5<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 753 | template <> |
| 754 | int Tensor6<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const; |
| 755 | |
| 756 | template <> |
| 757 | int Tensor0<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 758 | template <> |
| 759 | int Tensor1<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 760 | template <> |
| 761 | int Tensor2<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 762 | template <> |
| 763 | int Tensor3<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 764 | template <> |
| 765 | int Tensor4<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 766 | template <> |
| 767 | int Tensor5<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 768 | template <> |
| 769 | int Tensor6<float>::setTensorValueFloat(const size_t bufLen, const float* vals); |
| 770 | |
| 771 | template <> |
| 772 | int Tensor0<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 773 | template <> |
| 774 | int Tensor1<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 775 | template <> |
| 776 | int Tensor2<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 777 | template <> |
| 778 | int Tensor3<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 779 | template <> |
| 780 | int Tensor4<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 781 | template <> |
| 782 | int Tensor5<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 783 | template <> |
| 784 | int Tensor6<float>::getTensorValueFloat(const size_t bufLen, float* vals) const; |
| 785 | |
| 786 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 787 | int Tensor0<double>::setTensorValueDouble(const size_t bufLen, const double* vals); |
| 788 | template <> |
| 789 | int Tensor1<double>::setTensorValueDouble(const size_t bufLen, const double* vals); |
| 790 | template <> |
| 791 | int Tensor2<double>::setTensorValueDouble(const size_t bufLen, const double* vals); |
| 792 | template <> |
| 793 | int Tensor3<double>::setTensorValueDouble(const size_t bufLen, const double* vals); |
| 794 | template <> |
| 795 | int Tensor4<double>::setTensorValueDouble(const size_t bufLen, const double* vals); |
| 796 | template <> |
| 797 | int Tensor5<double>::setTensorValueDouble(const size_t bufLen, const double* vals); |
| 798 | template <> |
| 799 | int Tensor6<double>::setTensorValueDouble(const size_t bufLen, const double* vals); |
| 800 | |
| 801 | template <> |
| 802 | int Tensor0<double>::getTensorValueDouble(const size_t bufLen, double* vals) const; |
| 803 | template <> |
| 804 | int Tensor1<double>::getTensorValueDouble(const size_t bufLen, double* vals) const; |
| 805 | template <> |
| 806 | int Tensor2<double>::getTensorValueDouble(const size_t bufLen, double* vals) const; |
| 807 | template <> |
| 808 | int Tensor3<double>::getTensorValueDouble(const size_t bufLen, double* vals) const; |
| 809 | template <> |
| 810 | int Tensor4<double>::getTensorValueDouble(const size_t bufLen, double* vals) const; |
| 811 | template <> |
| 812 | int Tensor5<double>::getTensorValueDouble(const size_t bufLen, double* vals) const; |
| 813 | template <> |
| 814 | int Tensor6<double>::getTensorValueDouble(const size_t bufLen, double* vals) const; |
| 815 | |
| 816 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 817 | int Tensor0<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 818 | template <> |
| 819 | int Tensor1<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 820 | template <> |
| 821 | int Tensor2<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 822 | template <> |
| 823 | int Tensor3<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 824 | template <> |
| 825 | int Tensor4<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 826 | template <> |
| 827 | int Tensor5<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 828 | template <> |
| 829 | int Tensor6<bool>::setTensorValueBool(const size_t bufLen, const bool* vals); |
| 830 | |
| 831 | template <> |
| 832 | int Tensor0<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 833 | template <> |
| 834 | int Tensor1<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 835 | template <> |
| 836 | int Tensor2<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 837 | template <> |
| 838 | int Tensor3<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 839 | template <> |
| 840 | int Tensor4<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 841 | template <> |
| 842 | int Tensor5<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 843 | template <> |
| 844 | int Tensor6<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const; |
| 845 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 846 | template <> |
| 847 | int Tensor0<float>::dumpTensor(FILE* out) const; |
| 848 | template <> |
| 849 | int Tensor1<float>::dumpTensor(FILE* out) const; |
| 850 | template <> |
| 851 | int Tensor2<float>::dumpTensor(FILE* out) const; |
| 852 | template <> |
| 853 | int Tensor3<float>::dumpTensor(FILE* out) const; |
| 854 | template <> |
| 855 | int Tensor4<float>::dumpTensor(FILE* out) const; |
| 856 | template <> |
| 857 | int Tensor5<float>::dumpTensor(FILE* out) const; |
| 858 | template <> |
| 859 | int Tensor6<float>::dumpTensor(FILE* out) const; |
| 860 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 861 | int Tensor0<double>::dumpTensor(FILE* out) const; |
| 862 | template <> |
| 863 | int Tensor1<double>::dumpTensor(FILE* out) const; |
| 864 | template <> |
| 865 | int Tensor2<double>::dumpTensor(FILE* out) const; |
| 866 | template <> |
| 867 | int Tensor3<double>::dumpTensor(FILE* out) const; |
| 868 | template <> |
| 869 | int Tensor4<double>::dumpTensor(FILE* out) const; |
| 870 | template <> |
| 871 | int Tensor5<float>::dumpTensor(FILE* out) const; |
| 872 | template <> |
| 873 | int Tensor6<double>::dumpTensor(FILE* out) const; |
| 874 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 875 | int Tensor0<int32_t>::dumpTensor(FILE* out) const; |
| 876 | template <> |
| 877 | int Tensor1<int32_t>::dumpTensor(FILE* out) const; |
| 878 | template <> |
| 879 | int Tensor2<int32_t>::dumpTensor(FILE* out) const; |
| 880 | template <> |
| 881 | int Tensor3<int32_t>::dumpTensor(FILE* out) const; |
| 882 | template <> |
| 883 | int Tensor4<int32_t>::dumpTensor(FILE* out) const; |
| 884 | template <> |
| 885 | int Tensor5<int32_t>::dumpTensor(FILE* out) const; |
| 886 | template <> |
| 887 | int Tensor6<int32_t>::dumpTensor(FILE* out) const; |
| 888 | template <> |
| 889 | int Tensor0<int64_t>::dumpTensor(FILE* out) const; |
| 890 | template <> |
| 891 | int Tensor1<int64_t>::dumpTensor(FILE* out) const; |
| 892 | template <> |
| 893 | int Tensor2<int64_t>::dumpTensor(FILE* out) const; |
| 894 | template <> |
| 895 | int Tensor3<int64_t>::dumpTensor(FILE* out) const; |
| 896 | template <> |
| 897 | int Tensor4<int64_t>::dumpTensor(FILE* out) const; |
| 898 | template <> |
| 899 | int Tensor5<int64_t>::dumpTensor(FILE* out) const; |
| 900 | template <> |
| 901 | int Tensor6<int64_t>::dumpTensor(FILE* out) const; |
| 902 | template <> |
| 903 | int Tensor0<bool>::dumpTensor(FILE* out) const; |
| 904 | template <> |
| 905 | int Tensor1<bool>::dumpTensor(FILE* out) const; |
| 906 | template <> |
| 907 | int Tensor2<bool>::dumpTensor(FILE* out) const; |
| 908 | template <> |
| 909 | int Tensor3<bool>::dumpTensor(FILE* out) const; |
| 910 | template <> |
| 911 | int Tensor4<bool>::dumpTensor(FILE* out) const; |
| 912 | template <> |
| 913 | int Tensor5<bool>::dumpTensor(FILE* out) const; |
| 914 | template <> |
| 915 | int Tensor6<bool>::dumpTensor(FILE* out) const; |
| 916 | |
| 917 | class TensorFactory |
| 918 | { |
| 919 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 920 | static Tensor* newTensor(std::string tensorName_, DType dtype_, std::vector<int> shape_, const uint32_t rank) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 921 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 922 | TOSA_REF_TYPE tensorDtype_ = ConvertDType(dtype_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 923 | switch (tensorDtype_) |
| 924 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 925 | case TOSA_REF_TYPE_FP32: |
| 926 | case TOSA_REF_TYPE_FP16: |
| 927 | case TOSA_REF_TYPE_BF16: |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 928 | case TOSA_REF_TYPE_FP8E4M3: |
| 929 | case TOSA_REF_TYPE_FP8E5M2: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 930 | switch (rank) |
| 931 | { |
| 932 | case 0: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 933 | return new Tensor0<float>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 934 | case 1: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 935 | return new Tensor1<float>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 936 | case 2: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 937 | return new Tensor2<float>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 938 | case 3: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 939 | return new Tensor3<float>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 940 | case 4: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 941 | return new Tensor4<float>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 942 | case 5: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 943 | return new Tensor5<float>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 944 | case 6: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 945 | return new Tensor6<float>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 946 | } |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 947 | break; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 948 | case TOSA_REF_TYPE_INT32: |
| 949 | case TOSA_REF_TYPE_UINT8: |
| 950 | case TOSA_REF_TYPE_INT4: |
| 951 | case TOSA_REF_TYPE_INT8: |
| 952 | case TOSA_REF_TYPE_INT16: |
| 953 | case TOSA_REF_TYPE_UINT16: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 954 | switch (rank) |
| 955 | { |
| 956 | case 0: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 957 | return new Tensor0<int32_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 958 | case 1: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 959 | return new Tensor1<int32_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 960 | case 2: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 961 | return new Tensor2<int32_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 962 | case 3: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 963 | return new Tensor3<int32_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 964 | case 4: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 965 | return new Tensor4<int32_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 966 | case 5: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 967 | return new Tensor5<int32_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 968 | case 6: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 969 | return new Tensor6<int32_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 970 | } |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 971 | break; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 972 | case TOSA_REF_TYPE_INT48: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 973 | switch (rank) |
| 974 | { |
| 975 | case 0: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 976 | return new Tensor0<int64_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 977 | case 1: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 978 | return new Tensor1<int64_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 979 | case 2: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 980 | return new Tensor2<int64_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 981 | case 3: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 982 | return new Tensor3<int64_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 983 | case 4: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 984 | return new Tensor4<int64_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 985 | case 5: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 986 | return new Tensor5<int64_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 987 | case 6: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 988 | return new Tensor6<int64_t>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 989 | } |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 990 | break; |
Won Jeon | a21b2e8 | 2023-08-10 10:33:01 +0000 | [diff] [blame] | 991 | case TOSA_REF_TYPE_SHAPE: |
Tai Ly | 8690a08 | 2023-12-18 20:40:24 +0000 | [diff] [blame] | 992 | switch (rank) |
| 993 | { |
| 994 | case 0: |
| 995 | return new Tensor0<int64_t>(tensorName_, dtype_, shape_); |
| 996 | case 1: |
| 997 | return new Tensor1<int64_t>(tensorName_, dtype_, shape_); |
| 998 | default: |
| 999 | assert(0); // shape tensors must have rank of 0 or 1 |
| 1000 | } |
| 1001 | break; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1002 | case TOSA_REF_TYPE_BOOL: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1003 | switch (rank) |
| 1004 | { |
| 1005 | case 0: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1006 | return new Tensor0<bool>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1007 | case 1: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1008 | return new Tensor1<bool>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1009 | case 2: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1010 | return new Tensor2<bool>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1011 | case 3: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1012 | return new Tensor3<bool>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1013 | case 4: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1014 | return new Tensor4<bool>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1015 | case 5: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1016 | return new Tensor5<bool>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1017 | case 6: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1018 | return new Tensor6<bool>(tensorName_, dtype_, shape_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1019 | } |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1020 | break; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 1021 | case TOSA_REF_TYPE_FP64: |
| 1022 | switch (rank) |
| 1023 | { |
| 1024 | case 0: |
| 1025 | return new Tensor0<double>(tensorName_, dtype_, shape_); |
| 1026 | case 1: |
| 1027 | return new Tensor1<double>(tensorName_, dtype_, shape_); |
| 1028 | case 2: |
| 1029 | return new Tensor2<double>(tensorName_, dtype_, shape_); |
| 1030 | case 3: |
| 1031 | return new Tensor3<double>(tensorName_, dtype_, shape_); |
| 1032 | case 4: |
| 1033 | return new Tensor4<double>(tensorName_, dtype_, shape_); |
| 1034 | case 5: |
| 1035 | return new Tensor5<double>(tensorName_, dtype_, shape_); |
| 1036 | case 6: |
| 1037 | return new Tensor6<double>(tensorName_, dtype_, shape_); |
| 1038 | } |
| 1039 | break; |
| 1040 | case TOSA_REF_TYPE_UNKNOWN: |
| 1041 | assert(0); // tensorDtype_ is uninitialized |
Kevin Cheng | 989cb05 | 2021-04-28 16:29:44 -0700 | [diff] [blame] | 1042 | break; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1043 | } |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 1044 | return nullptr; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1045 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1046 | }; |
| 1047 | }; // namespace TosaReference |
| 1048 | |
| 1049 | #endif |