onnx-mlir
OMTensor.h
Go to the documentation of this file.
1 //===-------------- OMTensor.h - OMTensor Declaration header --------------===//
2 //
3 // Copyright 2019-2020 The IBM Research Authors.
4 //
5 // =============================================================================
6 //
7 // This file contains declaration of OMTensor data structures and
8 // API functions.
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #ifndef ONNX_MLIR_OMTENSOR_H
13 #define ONNX_MLIR_OMTENSOR_H
14 
15 #ifdef __cplusplus
16 #include <algorithm>
17 #include <iostream>
18 #include <map>
19 #include <numeric>
20 #include <string>
21 #include <vector>
22 #else
23 #include <stdbool.h>
24 #endif // #ifdef __cplusplus
25 
26 #ifdef __APPLE__
27 #include <stdlib.h>
28 #else
29 #include <malloc.h>
30 #endif // #ifdef __APPLE__
31 
33 
34 /* Typically, MemRefs in MLIR context are used as a compile-time constructs.
35  * Information such as element type and rank of the data payload is statically
36  * encoded, meaning that they are determined and fixed at compile-time. This
37  * presents significant burden for any runtime components trying to interact
38  * with the compiled executable.
39  *
40  * Thus a version of MemRef struct that is amenable to runtime manipulation is
41  * provided as a basis for building any runtime-related components providing
42  * user-facing programming interfaces. All information are dynamically encoded
43  * as members of this struct so that they can be accessed and modified easily
44  * during runtime.
45  *
46  * We will refer to it as an OMTensor.
47  */
48 
49 struct OMTensor;
50 
51 #ifndef __cplusplus
52 typedef struct OMTensor OMTensor;
53 #endif
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
79  void *data_ptr, int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
80 
99 OMTensor *omTensorCreateWithOwnership(void *data_ptr, int64_t *shape,
100  int64_t rank, OM_DATA_TYPE dtype, int owning);
101 
118 OMTensor *omTensorCreateEmpty(int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
119 
131 void omTensorDestroy(OMTensor *tensor);
132 
140 void *omTensorGetDataPtr(OMTensor *tensor);
141 
154 int64_t *omTensorGetShape(OMTensor *tensor);
155 
170 void omTensorSetShape(OMTensor *tensor, int64_t *shape);
171 
184 int64_t *omTensorGetStrides(OMTensor *tensor);
185 
200 void omTensorSetStrides(OMTensor *tensor, int64_t *stride);
201 
221  OMTensor *tensor, int64_t *stridesInBytes);
222 
230 
239 void omTensorSetDataType(OMTensor *tensor, OM_DATA_TYPE dataType);
240 
241 /* Helper function to get the ONNX data type size in bytes */
242 static inline int getDataTypeSize(OM_DATA_TYPE dataType) {
243  return OM_DATA_TYPE_SIZE[dataType];
244 }
245 
252 int64_t omTensorGetBufferSize(OMTensor *tensor);
253 
260 int omTensorGetRank(OMTensor *tensor);
261 
268 int64_t omTensorGetNumElems(OMTensor *tensor);
269 
275 int omTensorGetOwning(OMTensor *tensor);
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif // ONNX_MLIR_OMTENSOR_H
int64_t * omTensorGetStrides(OMTensor *tensor)
OMTensor data strides getter.
OMTensor * omTensorCreateWithOwnership(void *data_ptr, int64_t *shape, int64_t rank, OM_DATA_TYPE dtype, int owning)
Create an OMTensor with specified data pointer, shape, rank and element type, manually setting data p...
int64_t omTensorGetBufferSize(OMTensor *tensor)
OMTensor data buffer size getter.
int omTensorGetOwning(OMTensor *tensor)
OMTensor owning flag getter.
void omTensorSetStrides(OMTensor *tensor, int64_t *stride)
OMTensor data strides setter.
struct OMTensor OMTensor
Definition: OMTensor.h:52
OMTensor * omTensorCreateEmpty(int64_t *shape, int64_t rank, OM_DATA_TYPE dtype)
int64_t omTensorGetNumElems(OMTensor *tensor)
OMTensor number of elements getter.
OM_DATA_TYPE omTensorGetDataType(OMTensor *tensor)
OMTensor data type getter.
OMTensor * omTensorCreate(void *data_ptr, int64_t *shape, int64_t rank, OM_DATA_TYPE dtype)
Create a OMTensor with specified data pointer, shape, rank and element type.
void omTensorDestroy(OMTensor *tensor)
Destroy the OMTensor struct.
void omTensorSetDataType(OMTensor *tensor, OM_DATA_TYPE dataType)
OMTensor data type setter.
OM_DATA_TYPE
Definition: OnnxDataType.h:24
void omTensorSetStridesWithPyArrayStrides(OMTensor *tensor, int64_t *stridesInBytes)
OMTensor data strides setter with stride values from PyArray strides.
void * omTensorGetDataPtr(OMTensor *tensor)
OMTensor data pointer getter.
void omTensorSetShape(OMTensor *tensor, int64_t *shape)
OMTensor data shape setter.
int64_t * omTensorGetShape(OMTensor *tensor)
OMTensor data shape getter.
const int OM_DATA_TYPE_SIZE[]
int omTensorGetRank(OMTensor *tensor)
OMTensor rank getter.