tensor.hpp 5.91 KB
Newer Older
PanZezhong's avatar
init  
PanZezhong committed
1
2
3
#ifndef INFER_TENSOR_H
#define INFER_TENSOR_H

thatPepe's avatar
thatPepe committed
4
#include "allocator.hpp"
PanZezhong's avatar
init  
PanZezhong committed
5
6
7
8
9
10
#include "infinicore_infer.h"
#include "utils.hpp"
#include <memory>
#include <string>
#include <vector>

thatPepe's avatar
thatPepe committed
11
class Storage {
wooway777's avatar
wooway777 committed
12
13
private:
    Storage() = default;
PanZezhong's avatar
PanZezhong committed
14
15
16
17
18
    void *_memory;
    size_t _size;
    infiniDevice_t _device_type;
    int _device_id;
    std::shared_ptr<MemoryPool> _memory_pool;
wooway777's avatar
wooway777 committed
19

thatPepe's avatar
thatPepe committed
20
public:
PanZezhong's avatar
init  
PanZezhong committed
21
22
    static std::shared_ptr<Storage> create(size_t size);
    static std::shared_ptr<Storage> createAsync(size_t size, infinirtStream_t stream = nullptr);
thatPepe's avatar
thatPepe committed
23
    static std::shared_ptr<Storage> createFromPool(size_t size, std::shared_ptr<MemoryPool> pool = nullptr);
PanZezhong's avatar
init  
PanZezhong committed
24
25
    static std::shared_ptr<Storage> createHost(size_t size);
    ~Storage();
PanZezhong's avatar
PanZezhong committed
26
27
28
29
30

    void *memory() const { return _memory; }
    size_t size() const { return _size; }
    infiniDevice_t deviceType() const { return _device_type; }
    int deviceId() const { return _device_id; }
PanZezhong's avatar
init  
PanZezhong committed
31
32
33
34
35
36
37
38
};

struct SliceParams {
    size_t dim;
    size_t start;
    size_t len;
};

39
40
41
42
43
44
45
46
47
template <typename... Args>
std::vector<size_t> __shape(Args... args) {
    return std::vector<size_t>{static_cast<size_t>(args)...};
}

template <typename... Args>
std::vector<ptrdiff_t> __strides(Args... args) {
    return std::vector<ptrdiff_t>{static_cast<ptrdiff_t>(args)...};
}
PanZezhong's avatar
init  
PanZezhong committed
48
49
class TensorDesc {
private:
PanZezhong's avatar
PanZezhong committed
50
51
52
    infiniDtype_t _dtype;
    std::vector<size_t> _shape;
    std::vector<ptrdiff_t> _strides;
PanZezhong's avatar
init  
PanZezhong committed
53
54
    infiniopTensorDescriptor_t _desc;

PanZezhong's avatar
PanZezhong committed
55
56
57
58
    TensorDesc(infiniDtype_t dtype, const std::vector<size_t> &shape,
               const std::vector<ptrdiff_t> &strides) : _dtype(dtype), _shape(shape), _strides(strides), _desc(nullptr) {}
    void resetDesc();

PanZezhong's avatar
init  
PanZezhong committed
59
public:
PanZezhong's avatar
PanZezhong committed
60
    ~TensorDesc();
PanZezhong's avatar
init  
PanZezhong committed
61
62
63
    static std::shared_ptr<TensorDesc>
    create(infiniDtype_t dtype, const std::vector<size_t> &shape,
           const std::vector<ptrdiff_t> &strides);
64
65
66
67
68
    static std::shared_ptr<TensorDesc>
    create(infiniDtype_t dtype, const std::vector<size_t> &shape);
    static std::shared_ptr<TensorDesc>
    createWithOrder(infiniDtype_t dtype, const std::vector<size_t> &shape,
                    const std::vector<size_t> &order);
PanZezhong's avatar
PanZezhong committed
69
70
71
72
73
74
75
76
77
78
79
80

    infiniDtype_t dtype() const { return _dtype; }
    const std::vector<size_t> &shape() const { return _shape; }
    const std::vector<ptrdiff_t> &strides() const { return _strides; }
    size_t ndim() const { return _shape.size(); }
    infiniopTensorDescriptor_t desc() const;
    bool isContigous() const;
    std::string info() const;

    void dimMerge(size_t dim_start, size_t dim_end);
    void dimSplit(size_t dim, const std::vector<size_t> &dims);
    void permute(const std::vector<size_t> &order);
81
    void reDesc(const std::vector<size_t> new_shape, const std::vector<ptrdiff_t> new_strides);
PanZezhong's avatar
init  
PanZezhong committed
82
83
84
85
};

class Tensor : public std::enable_shared_from_this<Tensor> {
private:
PanZezhong's avatar
PanZezhong committed
86
    std::shared_ptr<Storage> _storage;
PanZezhong's avatar
PanZezhong committed
87
88
89
    std::shared_ptr<TensorDesc> _desc;

    ptrdiff_t _offset;
PanZezhong's avatar
init  
PanZezhong committed
90

PanZezhong's avatar
PanZezhong committed
91
    void *dataImpl(ptrdiff_t offset) const;
PanZezhong's avatar
init  
PanZezhong committed
92
    std::shared_ptr<Tensor>
PanZezhong's avatar
PanZezhong committed
93
    sliceImpl(const std::vector<SliceParams> &slices) const;
PanZezhong's avatar
init  
PanZezhong committed
94
95
96
97

public:
    static std::shared_ptr<Tensor> buffer(infiniDtype_t dtype,
                                          const std::vector<size_t> &shape,
thatPepe's avatar
thatPepe committed
98
                                          std::shared_ptr<MemoryPool> pool = nullptr);
PanZezhong's avatar
init  
PanZezhong committed
99
100
101
    static std::shared_ptr<Tensor> weight(void *host_data,
                                          infiniDtype_t dtype,
                                          const std::vector<size_t> &shape);
102
103
    std::shared_ptr<Tensor> memShare(const std::vector<size_t> &shape,
                                     infiniDtype_t dtype = INFINI_DTYPE_INVALID) const;
PanZezhong's avatar
init  
PanZezhong committed
104
105
106
107
108
109
    std::shared_ptr<Tensor> slice(size_t dim, size_t start, size_t len);
    std::shared_ptr<Tensor const> slice(size_t dim, size_t start,
                                        size_t len) const;
    std::shared_ptr<Tensor> slice(const std::vector<SliceParams> &slices);
    std::shared_ptr<Tensor const>
    slice(const std::vector<SliceParams> &slices) const;
PanZezhong's avatar
PanZezhong committed
110
111
112
    std::shared_ptr<Tensor> dimMerge(size_t dim_start, size_t dim_end);
    std::shared_ptr<Tensor> dimSplit(size_t dim,
                                     const std::vector<size_t> &dims);
PanZezhong's avatar
init  
PanZezhong committed
113
    std::shared_ptr<Tensor> permute(const std::vector<size_t> &order);
114
    std::shared_ptr<Tensor> reDesc(const std::vector<size_t> new_shape, const std::vector<ptrdiff_t> new_strides);
PanZezhong's avatar
init  
PanZezhong committed
115
116
    void *data(ptrdiff_t offset = 0);
    void const *data(ptrdiff_t offset = 0) const;
PanZezhong's avatar
PanZezhong committed
117
118
    void copyFrom(std::shared_ptr<Tensor const> src, infiniopHandle_t handle,
                  infinirtStream_t stream = nullptr);
PanZezhong's avatar
init  
PanZezhong committed
119
120
121
122
    const std::vector<size_t> &shape() const;
    const std::vector<ptrdiff_t> &strides() const;
    size_t ndim() const;
    infiniDtype_t dtype() const;
PanZezhong's avatar
PanZezhong committed
123
124
    bool isContigous() const;
    infiniopTensorDescriptor_t desc() const;
PanZezhong's avatar
PanZezhong committed
125
126
127
    ptrdiff_t dataOffset() const;
    infiniDevice_t deviceType() const;
    int deviceId() const;
PanZezhong's avatar
init  
PanZezhong committed
128
129
130

    void debug(const std::string &filename) const;
    void debug() const;
PanZezhong's avatar
PanZezhong committed
131
    std::string info() const;
PanZezhong's avatar
init  
PanZezhong committed
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

    ~Tensor();
};

inline size_t dsize(infiniDtype_t dtype) {
    switch (dtype) {
    case INFINI_DTYPE_INVALID:
        return 0;
    case INFINI_DTYPE_BYTE:
        return 1;
    case INFINI_DTYPE_BOOL:
        return 1;
    case INFINI_DTYPE_I8:
        return 1;
    case INFINI_DTYPE_I16:
        return 2;
    case INFINI_DTYPE_I32:
        return 4;
    case INFINI_DTYPE_I64:
        return 8;
    case INFINI_DTYPE_U8:
        return 1;
    case INFINI_DTYPE_U16:
        return 2;
    case INFINI_DTYPE_U32:
        return 4;
    case INFINI_DTYPE_U64:
        return 8;
    case INFINI_DTYPE_F8:
        return 1;
    case INFINI_DTYPE_F16:
        return 2;
    case INFINI_DTYPE_F32:
        return 4;
    case INFINI_DTYPE_F64:
        return 8;
    case INFINI_DTYPE_C16:
        return 2;
    case INFINI_DTYPE_C32:
        return 4;
    case INFINI_DTYPE_C64:
        return 8;
    case INFINI_DTYPE_C128:
        return 16;
    case INFINI_DTYPE_BF16:
        return 2;
    default:
        return 0;
    }
}

#endif