aboutsummaryrefslogtreecommitdiff
path: root/python/pyarmnn/src/pyarmnn/swig/modules/armnn_tensor.i
blob: efa9a16352abfe228a56adf66db6a8cc5467b88e (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
%{
#include "armnn/Tensor.hpp"
%}

%include <typemaps/tensor_memory.i>
%include <typemaps/tensor_shape.i>

namespace armnn
{

%feature("docstring",
"
Class for holding the shape information of an Arm NN tensor.

") TensorShape;
class TensorShape
{
public:
    %tensor_shape_typemap(unsigned int numDimensions, const unsigned int* dimensionSizes);
    TensorShape(unsigned int numDimensions, const unsigned int* dimensionSizes);
    %clear_tensor_shape_typemap(unsigned int numDimensions, const unsigned int* dimensionSizes);

    %feature("docstring",
    "
    Returns the number of dimensions in this TensorShape.

    Returns:
        int: The number of dimensions in this TensorShape.

    ") GetNumDimensions;
    unsigned int GetNumDimensions() const;

    %feature("docstring",
    "
    Returns the total number of elements for a tensor with this TensorShape.

    Returns:
        int: The total number of elements for a tensor with this TensorShape.

    ") GetNumElements;
    unsigned int GetNumElements() const;

};

%extend TensorShape {

    unsigned int __getitem__(unsigned int i) const {
        return $self->operator[](i);
    }
    void __setitem__(unsigned int i, unsigned int val) {
         $self->operator[](i) = val;
    }

    std::string __str__() {
        std::string dim = "NumDimensions: " + std::to_string($self->GetNumDimensions());
        std::string elm = "NumElements: " + std::to_string($self->GetNumElements());

        std::string shapeStr = "TensorShape{Shape(";

        auto numDimensions = $self->GetNumDimensions();
        auto sizeDims = $self->GetNumDimensions();
         for (unsigned int i = 0; i < numDimensions; i++) {
            shapeStr += std::to_string($self->operator[](i));

            if (sizeDims - 1 > 0) {
                shapeStr += ", ";
            }
            sizeDims--;
            }
        shapeStr = shapeStr + "), " + dim + ", " + elm + "}";
        return shapeStr;
    }

}


%feature("docstring",
"
Class for holding the tensor information of an Arm NN tensor such as quantization, datatype, shape etc.

") TensorInfo;
class TensorInfo
{
public:
    TensorInfo();

    TensorInfo(const TensorInfo& other);

    TensorInfo(const TensorShape& shape, DataType dataType,
        float quantizationScale = 0.0f, int32_t quantizationOffset = 0);

    %feature("docstring",
    "
    Get the tensor shape.

    Return:
        TensorShape: Current shape of the tensor.

    ") GetShape;
    TensorShape& GetShape();

    %feature("docstring",
    "
    Set the tensor shape. Must have the same number of elements as current tensor.

    Args:
        newShape (TensorShape): New tensor shape to reshape to.

    ") SetShape;
    void SetShape(const TensorShape& newShape);

    %feature("docstring",
    "
    Returns the number of dimensions in this Tensor.

    Returns:
        int: The number of dimensions in this Tensor.

    ") GetNumDimensions;
    unsigned int GetNumDimensions() const;

    %feature("docstring",
    "
    Returns the total number of elements for this Tensor.

    Returns:
        int: The total number of elements for this Tensor.

    ") GetNumElements;
    unsigned int GetNumElements() const;

    %feature("docstring",
    "
    Get the tensor datatype.

    Returns:
        DataType: Current tensor DataType.

    ") GetDataType;
    DataType GetDataType() const;

    %feature("docstring",
    "
    Set the tensor datatype.

    Args:
        type (DataType): DataType to set the tensor to.

    ") SetDataType;
    void SetDataType(DataType type);

    %feature("docstring",
    "
    Get the value of the tensors quantization scale.

    Returns:
        float: Tensor quantization scale value.

    ") GetQuantizationScale;
    float GetQuantizationScale() const;

    %feature("docstring",
    "
    Get the value of the tensors quantization offset.

    Returns:
        int: Tensor quantization offset value.

    ") GetQuantizationOffset;
    int32_t GetQuantizationOffset() const;

    %feature("docstring",
    "
    Set the value of the tensors quantization scale.

    Args:
        scale (float): Scale value to set.

    ") SetQuantizationScale;
    void SetQuantizationScale(float scale);

    %feature("docstring",
    "
    Set the value of the tensors quantization offset.

    Args:
        offset (int): Offset value to set.

    ") SetQuantizationOffset;
    void SetQuantizationOffset(int32_t offset);

    %feature("docstring",
    "
    Returns true if the tensor is a quantized data type.

    Returns:
        bool: True if the tensor is a quantized data type.

    ") IsQuantized;
    bool IsQuantized() const;



    %feature("docstring",
    "
    Check that the types are the same and, if quantize, that the quantization parameters are the same.

    Returns:
        bool: True if matched, else False.

    ") IsTypeSpaceMatch;
    bool IsTypeSpaceMatch(const TensorInfo& other) const;

    %feature("docstring",
    "
    Get the number of bytes needed for this tensor.

    Returns:
        int: Number of bytes consumed by this tensor.

    ") GetNumBytes;
    unsigned int GetNumBytes() const;

};

%extend TensorInfo {

    std::string __str__() {
        const std::string tmp = "TensorInfo{DataType: " + std::to_string(static_cast<int>($self->GetDataType()))
                        + ", IsQuantized: " + std::to_string($self->IsQuantized())
                        + ", QuantizationScale: " + std::to_string( $self->GetQuantizationScale())
                        + ", QuantizationOffset: " + std::to_string($self->GetQuantizationOffset())
                        + ", NumDimensions: " + std::to_string($self->GetNumDimensions())
                        + ", NumElements: " + std::to_string($self->GetNumElements()) + "}";
        return tmp;
    }

}

class Tensor
{
public:
    ~Tensor();
    Tensor();
    Tensor(const Tensor& other);

    %mutable_memory(void* memory);
    Tensor(const TensorInfo& info, void* memory);
    %clear_mutable_memory(void* memory);

    const TensorInfo& GetInfo() const;
    const TensorShape& GetShape() const;

    DataType GetDataType() const;
    unsigned int GetNumDimensions() const;
    unsigned int GetNumBytes() const;
    unsigned int GetNumElements() const;

    /* we want to disable getting the memory area from here - forcing use of get_memory_area() in public api.
     void* GetMemoryArea() const;*/
};

%extend Tensor {

     std::string __str__() {
        const std::string tmp = "Tensor{DataType: " + std::to_string(static_cast<int>($self->GetDataType()))
                        + ", NumBytes: " + std::to_string($self->GetNumBytes())
                        + ", NumDimensions: " + std::to_string( $self->GetNumDimensions())
                        + ", NumElements: " + std::to_string($self->GetNumElements()) + "}";
        return tmp;
    }
}

class ConstTensor
{
public:
    ~ConstTensor();
    ConstTensor();
    ConstTensor(const Tensor& other);
    ConstTensor(const ConstTensor& other);

    %const_memory(const void* memory);
    ConstTensor(const TensorInfo& info, const void* memory);
    %clear_const_memory(const void* memory);

    const TensorInfo& GetInfo() const;
    const TensorShape& GetShape() const;

    DataType GetDataType() const;
    unsigned int GetNumDimensions() const;
    unsigned int GetNumBytes() const;
    unsigned int GetNumElements() const;

    /* we want to disable getting the memory area from here - forcing use of get_memory_area() in public api.
     void* GetMemoryArea() const;*/
};

%extend ConstTensor {

    std::string __str__() {
        const std::string tmp = "ConstTensor{DataType: " + std::to_string(static_cast<int>($self->GetDataType()))
                        + ", NumBytes: " + std::to_string($self->GetNumBytes())
                        + ", NumDimensions: " + std::to_string( $self->GetNumDimensions())
                        + ", NumElements: " + std::to_string($self->GetNumElements()) + "}";
        return tmp;
    }
}

}