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