aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/tensor.h
blob: ede42a904849f7efd88a42fd59fbf282fa0afe92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726

// Copyright (c) 2020-2021, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#ifndef TOSA_REFERENCE_TENSOR_H
#define TOSA_REFERENCE_TENSOR_H

#include "model_common.h"
#include "ops/template_types.h"
#include "tosa_generated.h"
#include "tosa_serialization_handler.h"
#include <Eigen/CXX11/Tensor>
#include <list>
#include <vector>

using namespace tosa;

namespace TosaReference
{
class GraphNode;

class Tensor
{
public:
    Tensor(std::string tensorName_, DType tensorDtype__, std::vector<int> shape_);

    virtual ~Tensor();

    int setIsSubgraphInput();
    int setIsSubgraphOutput();

    int getIsSubgraphInput() const
    {
        return isSubgraphInput;
    }

    int getIsSubgraphOutput() const
    {
        return isSubgraphOutput;
    }

    int setProducer(GraphNode* node);
    int addConsumer(GraphNode* node);

    int setIsValid()
    {
        isValid = 1;
        return 0;
    }

    int clearIsValid()
    {
        isValid = 0;
        return 0;
    }

    int getIsValid() const
    {
        return isValid;
    }

    GraphNode* getProducer()
    {
        return producer;
    }

    std::vector<GraphNode*>& getConsumers()
    {
        return consumers;
    }

    const std::string& getName() const
    {
        return tensorName;
    }

    const std::vector<int>& getShape() const
    {
        return shape;
    }

    std::string getShapeAsString() const
    {
        std::string shape_str("[");
        for (auto& dim : shape)
        {
            shape_str += (std::to_string(dim) + ", ");
        }
        shape_str.append("]");
        return shape_str;
    }

    const uint32_t getElementCount() const
    {
        uint32_t elements = 1;
        for (size_t i = 0; i < shape.size(); i++)
            elements *= shape[i];

        return elements;
    }

    // Comparison of rank and type with other tensors
    const int matchRank(const Tensor& ref) const
    {
        return (ref.shape.size() == shape.size()) ? 0 : 1;
    }

    const int matchType(const Tensor& ref) const
    {
        return (ref.tensorDtype == tensorDtype) ? 0 : 1;
    }

    const int matchRankType(const Tensor& ref) const
    {
        return (matchType(ref) || matchRank(ref));
    }

    const int matchRankTypeShape(const Tensor& ref, const bool broadcastOk = false) const
    {
        if (matchRankType(ref))
            return 1;

        for (size_t i = 0; i < shape.size(); i++)
        {
            if (shape[i] != ref.shape[i])
            {
                if (!broadcastOk ||
                    // For broadcasts, the order of *this and ref matters.
                    // *this should be the source tensor.
                    // ref should be the target tensor. In most of the case, ref is expected to be the output tensor.
                    // this->shape must have size 1 if they don't match
                    (broadcastOk && (shape[i] != 1)))
                {
                    return 1;
                }
            }
        }

        return 0;
    }

    const int matchRankShape(const Tensor& ref, const bool broadcastOk = false) const
    {
        if (matchRank(ref))
            return 1;

        for (size_t i = 0; i < shape.size(); i++)
        {
            if (shape[i] != ref.shape[i])
            {
                if (!broadcastOk ||
                    // For broadcasts, the order of *this and ref matters.
                    // *this should be the source tensor.
                    // ref should be the target tensor. In most of the case, ref is expected to be the output tensor.
                    // this->shape must have size 1 if they don't match
                    (broadcastOk && (shape[i] != 1)))
                {
                    return 1;
                }
            }
        }

        return 0;
    }

    // Sometimes we might want to match several semi-compatible types,
    // so just check rank and size here
    const int matchRankSize(const Tensor& ref) const
    {
        if (matchRank(ref))
            return 1;

        for (size_t i = 0; i < shape.size(); i++)
        {
            if (shape[i] != ref.shape[i])
                return 1;
        }

        return 0;
    }

    // Unary check to make sure rank matches
    const int checkRequiredRank(const int exactRank) const
    {
        return (shape.size() == (size_t)exactRank) ? 0 : 1;
    }

    const int checkRequiredRank(const int minRank, const int maxRank) const
    {
        return (shape.size() >= (size_t)minRank && shape.size() <= (size_t)maxRank) ? 0 : 1;
    }

    const int getRank() const
    {
        return shape.size();
    }

    const DType getDtype() const
    {
        return tensorDtype;
    }

    virtual int dumpTensor(FILE* out) const = 0;
    virtual int dumpTensorParams(FILE* out) const;
    virtual int dumpTensorParams(std::ostream& out) const;

    virtual int setTensorValueFloat(const size_t bufLen, const float* vals)   = 0;
    virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals) = 0;
    virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals) = 0;
    virtual int setTensorValueBool(const size_t bufLen, const bool* vals)     = 0;
    virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const   = 0;
    virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const = 0;
    virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const = 0;
    virtual int getTensorValueBool(const size_t bufLen, bool* ibuf) const     = 0;

    virtual int readFromNpyFile(const char* filename);
    virtual int writeToNpyFile(const char* filename) const;
    virtual int copyValueFrom(Tensor* tensor) = 0;

    const char* bool_to_str(bool in) const
    {
        static const char* true_str  = "true";
        static const char* false_str = "false";
        return in ? true_str : false_str;
    }

    virtual int allocate()      = 0;
    virtual int deallocate()    = 0;
    virtual bool is_allocated() = 0;

protected:
    std::string tensorName;
    DType tensorDtype;
    int isValid;
    std::vector<int> shape;
    int isSubgraphInput;
    int isSubgraphOutput;
    bool isAllocated;

    GraphNode* producer;
    std::vector<GraphNode*> consumers;

    // Note: the Eigen::Tensor is not declared in Tensor
    // Instead, the TensorTemplate class keeps the templated tensor
    // declaration so that the graph manipulation tools are isolated
    // from the templated tensor type.
    //
    // Operators need to be aware of the TensorTemplate<EigenTensor<type, rank>> type
    // so that they can operate on the right types.
};

template <class T>
class TensorTemplate : public Tensor
{
public:
    TensorTemplate(std::string tensorName_, DType tensorDtype_, std::vector<int> shape_)
        : Tensor(tensorName_, tensorDtype_, shape_)
    {
        tensor = nullptr;
    }

    virtual ~TensorTemplate()
    {
        deallocate();
    }

    virtual int allocate()
    {
        tensor = new T();
        if (tensor)
            return 0;
        else
            return 1;
    }

    virtual int deallocate()
    {
        if (tensor)
        {
            delete tensor;
        }
        tensor = nullptr;
        return 0;
    }

    virtual bool is_allocated()
    {
        if (tensor)
        {
            return true;
        }
        return false;
    }

    T& getTensor()
    {
        return *tensor;
    }

    virtual int dumpTensor(FILE* out) const;

    virtual int setTensorValueFloat(const size_t bufLen, const float* vals);
    virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals);
    virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals);
    virtual int setTensorValueBool(const size_t bufLen, const bool* vals);
    virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const;
    virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const;
    virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const;
    virtual int getTensorValueBool(const size_t bufLen, bool* bbuf) const;

    virtual int copyValueFrom(Tensor* tensor);

protected:
    T* tensor;
};

// allocate() template specializations to allocate the different tensor sizes
// Let the compiler know here before the factory uses them, but define them in the .cc file.
template <>
int Tensor0<float>::allocate();
template <>
int Tensor1<float>::allocate();
template <>
int Tensor2<float>::allocate();
template <>
int Tensor3<float>::allocate();
template <>
int Tensor4<float>::allocate();
template <>
int Tensor5<float>::allocate();
template <>
int Tensor6<float>::allocate();

template <>
int Tensor0<int32_t>::allocate();
template <>
int Tensor1<int32_t>::allocate();
template <>
int Tensor2<int32_t>::allocate();
template <>
int Tensor3<int32_t>::allocate();
template <>
int Tensor4<int32_t>::allocate();
template <>
int Tensor5<int32_t>::allocate();
template <>
int Tensor6<int32_t>::allocate();

template <>
int Tensor0<int64_t>::allocate();
template <>
int Tensor1<int64_t>::allocate();
template <>
int Tensor2<int64_t>::allocate();
template <>
int Tensor3<int64_t>::allocate();
template <>
int Tensor4<int64_t>::allocate();
template <>
int Tensor5<int64_t>::allocate();
template <>
int Tensor6<int64_t>::allocate();

template <>
int Tensor0<bool>::allocate();
template <>
int Tensor1<bool>::allocate();
template <>
int Tensor2<bool>::allocate();
template <>
int Tensor3<bool>::allocate();
template <>
int Tensor4<bool>::allocate();
template <>
int Tensor5<bool>::allocate();
template <>
int Tensor6<bool>::allocate();

template <>
int Tensor0<float>::copyValueFrom(Tensor* src);
template <>
int Tensor1<float>::copyValueFrom(Tensor* src);
template <>
int Tensor2<float>::copyValueFrom(Tensor* src);
template <>
int Tensor3<float>::copyValueFrom(Tensor* src);
template <>
int Tensor4<float>::copyValueFrom(Tensor* src);
template <>
int Tensor5<float>::copyValueFrom(Tensor* src);
template <>
int Tensor6<float>::copyValueFrom(Tensor* src);

template <>
int Tensor0<int32_t>::copyValueFrom(Tensor* src);
template <>
int Tensor1<int32_t>::copyValueFrom(Tensor* src);
template <>
int Tensor2<int32_t>::copyValueFrom(Tensor* src);
template <>
int Tensor3<int32_t>::copyValueFrom(Tensor* src);
template <>
int Tensor4<int32_t>::copyValueFrom(Tensor* src);
template <>
int Tensor5<int32_t>::copyValueFrom(Tensor* src);
template <>
int Tensor6<int32_t>::copyValueFrom(Tensor* src);

template <>
int Tensor0<int64_t>::copyValueFrom(Tensor* src);
template <>
int Tensor1<int64_t>::copyValueFrom(Tensor* src);
template <>
int Tensor2<int64_t>::copyValueFrom(Tensor* src);
template <>
int Tensor3<int64_t>::copyValueFrom(Tensor* src);
template <>
int Tensor4<int64_t>::copyValueFrom(Tensor* src);
template <>
int Tensor5<int64_t>::copyValueFrom(Tensor* src);
template <>
int Tensor6<int64_t>::copyValueFrom(Tensor* src);

template <>
int Tensor0<bool>::copyValueFrom(Tensor* src);
template <>
int Tensor1<bool>::copyValueFrom(Tensor* src);
template <>
int Tensor2<bool>::copyValueFrom(Tensor* src);
template <>
int Tensor3<bool>::copyValueFrom(Tensor* src);
template <>
int Tensor4<bool>::copyValueFrom(Tensor* src);
template <>
int Tensor5<bool>::copyValueFrom(Tensor* src);
template <>
int Tensor6<bool>::copyValueFrom(Tensor* src);

template <>
int Tensor0<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
int Tensor1<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
int Tensor2<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
int Tensor3<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
int Tensor4<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
int Tensor5<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
int Tensor6<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);

template <>
int Tensor0<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
template <>
int Tensor1<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
template <>
int Tensor2<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
template <>
int Tensor3<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
template <>
int Tensor4<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
template <>
int Tensor5<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
template <>
int Tensor6<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;

template <>
int Tensor0<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals);
template <>
int Tensor1<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals);
template <>
int Tensor2<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals);
template <>
int Tensor3<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals);
template <>
int Tensor4<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals);
template <>
int Tensor5<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals);
template <>
int Tensor6<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals);

template <>
int Tensor0<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const;
template <>
int Tensor1<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const;
template <>
int Tensor2<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const;
template <>
int Tensor3<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const;
template <>
int Tensor4<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const;
template <>
int Tensor5<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const;
template <>
int Tensor6<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const;

template <>
int Tensor0<float>::setTensorValueFloat(const size_t bufLen, const float* vals);
template <>
int Tensor1<float>::setTensorValueFloat(const size_t bufLen, const float* vals);
template <>
int Tensor2<float>::setTensorValueFloat(const size_t bufLen, const float* vals);
template <>
int Tensor3<float>::setTensorValueFloat(const size_t bufLen, const float* vals);
template <>
int Tensor4<float>::setTensorValueFloat(const size_t bufLen, const float* vals);
template <>
int Tensor5<float>::setTensorValueFloat(const size_t bufLen, const float* vals);
template <>
int Tensor6<float>::setTensorValueFloat(const size_t bufLen, const float* vals);

template <>
int Tensor0<float>::getTensorValueFloat(const size_t bufLen, float* vals) const;
template <>
int Tensor1<float>::getTensorValueFloat(const size_t bufLen, float* vals) const;
template <>
int Tensor2<float>::getTensorValueFloat(const size_t bufLen, float* vals) const;
template <>
int Tensor3<float>::getTensorValueFloat(const size_t bufLen, float* vals) const;
template <>
int Tensor4<float>::getTensorValueFloat(const size_t bufLen, float* vals) const;
template <>
int Tensor5<float>::getTensorValueFloat(const size_t bufLen, float* vals) const;
template <>
int Tensor6<float>::getTensorValueFloat(const size_t bufLen, float* vals) const;

template <>
int Tensor0<bool>::setTensorValueBool(const size_t bufLen, const bool* vals);
template <>
int Tensor1<bool>::setTensorValueBool(const size_t bufLen, const bool* vals);
template <>
int Tensor2<bool>::setTensorValueBool(const size_t bufLen, const bool* vals);
template <>
int Tensor3<bool>::setTensorValueBool(const size_t bufLen, const bool* vals);
template <>
int Tensor4<bool>::setTensorValueBool(const size_t bufLen, const bool* vals);
template <>
int Tensor5<bool>::setTensorValueBool(const size_t bufLen, const bool* vals);
template <>
int Tensor6<bool>::setTensorValueBool(const size_t bufLen, const bool* vals);

template <>
int Tensor0<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const;
template <>
int Tensor1<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const;
template <>
int Tensor2<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const;
template <>
int Tensor3<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const;
template <>
int Tensor4<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const;
template <>
int Tensor5<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const;
template <>
int Tensor6<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const;

// assume we only dump float type tensor now
template <>
int Tensor0<float>::dumpTensor(FILE* out) const;
template <>
int Tensor1<float>::dumpTensor(FILE* out) const;
template <>
int Tensor2<float>::dumpTensor(FILE* out) const;
template <>
int Tensor3<float>::dumpTensor(FILE* out) const;
template <>
int Tensor4<float>::dumpTensor(FILE* out) const;
template <>
int Tensor5<float>::dumpTensor(FILE* out) const;
template <>
int Tensor6<float>::dumpTensor(FILE* out) const;
template <>
int Tensor0<int32_t>::dumpTensor(FILE* out) const;
template <>
int Tensor1<int32_t>::dumpTensor(FILE* out) const;
template <>
int Tensor2<int32_t>::dumpTensor(FILE* out) const;
template <>
int Tensor3<int32_t>::dumpTensor(FILE* out) const;
template <>
int Tensor4<int32_t>::dumpTensor(FILE* out) const;
template <>
int Tensor5<int32_t>::dumpTensor(FILE* out) const;
template <>
int Tensor6<int32_t>::dumpTensor(FILE* out) const;
template <>
int Tensor0<int64_t>::dumpTensor(FILE* out) const;
template <>
int Tensor1<int64_t>::dumpTensor(FILE* out) const;
template <>
int Tensor2<int64_t>::dumpTensor(FILE* out) const;
template <>
int Tensor3<int64_t>::dumpTensor(FILE* out) const;
template <>
int Tensor4<int64_t>::dumpTensor(FILE* out) const;
template <>
int Tensor5<int64_t>::dumpTensor(FILE* out) const;
template <>
int Tensor6<int64_t>::dumpTensor(FILE* out) const;
template <>
int Tensor0<bool>::dumpTensor(FILE* out) const;
template <>
int Tensor1<bool>::dumpTensor(FILE* out) const;
template <>
int Tensor2<bool>::dumpTensor(FILE* out) const;
template <>
int Tensor3<bool>::dumpTensor(FILE* out) const;
template <>
int Tensor4<bool>::dumpTensor(FILE* out) const;
template <>
int Tensor5<bool>::dumpTensor(FILE* out) const;
template <>
int Tensor6<bool>::dumpTensor(FILE* out) const;

class TensorFactory
{
public:
    static Tensor* newTensor(std::string tensorName_, DType tensorDtype_, std::vector<int> shape_, const uint32_t rank)
    {
        switch (tensorDtype_)
        {
            case DType_FLOAT:
                switch (rank)
                {
                    case 0:
                        return new Tensor0<float>(tensorName_, tensorDtype_, shape_);
                    case 1:
                        return new Tensor1<float>(tensorName_, tensorDtype_, shape_);
                    case 2:
                        return new Tensor2<float>(tensorName_, tensorDtype_, shape_);
                    case 3:
                        return new Tensor3<float>(tensorName_, tensorDtype_, shape_);
                    case 4:
                        return new Tensor4<float>(tensorName_, tensorDtype_, shape_);
                    case 5:
                        return new Tensor5<float>(tensorName_, tensorDtype_, shape_);
                    case 6:
                        return new Tensor6<float>(tensorName_, tensorDtype_, shape_);
                }
                break;
            case DType_INT32:
            case DType_UINT8:
            case DType_INT4:
            case DType_INT8:
            case DType_INT16:
            case DType_UINT16:
                switch (rank)
                {
                    case 0:
                        return new Tensor0<int32_t>(tensorName_, tensorDtype_, shape_);
                    case 1:
                        return new Tensor1<int32_t>(tensorName_, tensorDtype_, shape_);
                    case 2:
                        return new Tensor2<int32_t>(tensorName_, tensorDtype_, shape_);
                    case 3:
                        return new Tensor3<int32_t>(tensorName_, tensorDtype_, shape_);
                    case 4:
                        return new Tensor4<int32_t>(tensorName_, tensorDtype_, shape_);
                    case 5:
                        return new Tensor5<int32_t>(tensorName_, tensorDtype_, shape_);
                    case 6:
                        return new Tensor6<int32_t>(tensorName_, tensorDtype_, shape_);
                }
                break;
            case DType_INT48:
                switch (rank)
                {
                    case 0:
                        return new Tensor0<int64_t>(tensorName_, tensorDtype_, shape_);
                    case 1:
                        return new Tensor1<int64_t>(tensorName_, tensorDtype_, shape_);
                    case 2:
                        return new Tensor2<int64_t>(tensorName_, tensorDtype_, shape_);
                    case 3:
                        return new Tensor3<int64_t>(tensorName_, tensorDtype_, shape_);
                    case 4:
                        return new Tensor4<int64_t>(tensorName_, tensorDtype_, shape_);
                    case 5:
                        return new Tensor5<int64_t>(tensorName_, tensorDtype_, shape_);
                    case 6:
                        return new Tensor6<int64_t>(tensorName_, tensorDtype_, shape_);
                }
                break;
            case DType_BOOL:
                switch (rank)
                {
                    case 0:
                        return new Tensor0<bool>(tensorName_, tensorDtype_, shape_);
                    case 1:
                        return new Tensor1<bool>(tensorName_, tensorDtype_, shape_);
                    case 2:
                        return new Tensor2<bool>(tensorName_, tensorDtype_, shape_);
                    case 3:
                        return new Tensor3<bool>(tensorName_, tensorDtype_, shape_);
                    case 4:
                        return new Tensor4<bool>(tensorName_, tensorDtype_, shape_);
                    case 5:
                        return new Tensor5<bool>(tensorName_, tensorDtype_, shape_);
                    case 6:
                        return new Tensor6<bool>(tensorName_, tensorDtype_, shape_);
                }
                break;
            default:
                break;
        }
        return nullptr;
    }

    static Tensor* newTensor(DType type, const std::vector<int> shape);
};
};    // namespace TosaReference

#endif