onnx-mlir

Logo

Representation and Reference Lowering of ONNX Models in MLIR Compiler Infrastructure

View the Project on GitHub onnx/onnx-mlir

How-Tos

Inference Using Python
Inference Using C/C++
Inference Using Java

References

ONNX Dialect
OMTensor C99 Runtime API
OMTensorList C99 Runtime API
OMTensor Java Runtime API
OMTensorList Java Runtime API
Generate ONNX Dialect
About Documentation

Development

Add an Operation
Testing Guidelines
Error Handling
Command-line Options
Instrumentation
Performance Testing
Constant Propagation
Add an Accelerator

Tools

Tools

RunONNXModel.py
DocCheck

This project is maintained by onnx

Hosted on GitHub Pages — Theme by orderedlist

onnx-mlir: onnx-mlir/include/onnx-mlir/Runtime/OMTensor.h Source File
onnx-mlir
Loading...
Searching...
No Matches
OMTensor.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: Apache-2.0
3 */
4
5//===-------------- OMTensor.h - OMTensor Declaration header --------------===//
6//
7// Copyright 2019-2026 The IBM Research Authors.
8//
9// =============================================================================
10//
11// This file contains declaration of OMTensor data structures and
12// API functions.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef ONNX_MLIR_OMTENSOR_H
17#define ONNX_MLIR_OMTENSOR_H
18
19#ifdef __cplusplus
20#include <algorithm>
21#include <iostream>
22#include <map>
23#include <numeric>
24#include <string>
25#include <vector>
26#else
27#include <stdbool.h>
28#endif // #ifdef __cplusplus
29
30#if defined(__APPLE__) || defined(__MVS__)
31#include <stdlib.h>
32#else
33#include <malloc.h>
34#endif // #ifdef __APPLE__
35
36#include "onnx-mlir/Compiler/OMCompilerMacros.h"
37#include "onnx-mlir/Runtime/OnnxDataType.h"
38
39/* Typically, MemRefs in MLIR context are used as a compile-time constructs.
40 * Information such as element type and rank of the data payload is statically
41 * encoded, meaning that they are determined and fixed at compile-time. This
42 * presents significant burden for any runtime components trying to interact
43 * with the compiled executable.
44 *
45 * Thus a version of MemRef struct that is amenable to runtime manipulation is
46 * provided as a basis for building any runtime-related components providing
47 * user-facing programming interfaces. All information are dynamically encoded
48 * as members of this struct so that they can be accessed and modified easily
49 * during runtime.
50 *
51 * We will refer to it as an OMTensor.
52 */
53
54struct OMTensor;
55
56#ifndef __cplusplus
57typedef struct OMTensor OMTensor;
58#endif
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
92OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreate(
93 void *data_ptr, const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
94
123OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreateWithOwnership(void *data_ptr,
124 const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype, int64_t owning);
125
143OM_EXTERNAL_VISIBILITY OMTensor *omTensorCreateEmpty(
144 const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype);
145
159OM_EXTERNAL_VISIBILITY void omTensorDestroy(OMTensor *tensor);
160
168OM_EXTERNAL_VISIBILITY void *omTensorGetDataPtr(const OMTensor *tensor);
169
183OM_EXTERNAL_VISIBILITY void *omTensorGetAllocatedPtr(const OMTensor *tensor);
184
196OM_EXTERNAL_VISIBILITY const int64_t *omTensorGetShape(const OMTensor *tensor);
197
208OM_EXTERNAL_VISIBILITY OM_DATA_TYPE omTensorGetDataType(const OMTensor *tensor);
209
210/* Helper function to get the ONNX data type size in bytes */
211
212/* Note that this function does really support sub-byte quantities
213 * like int4, unit4 and smaller, as we are returning here byte size
214 * quantities. Eventually, we need to transform this (and the associated
215 * OM_DATA_TYPE_SIZE) to return bit sizes so as to better represent such data
216 * sizes. It also forces all functions in this library to either up-convert
217 * sub-byte size to byte size values to process them.
218 *
219 */
220static inline int64_t getDataTypeSize(OM_DATA_TYPE dataType) {
221 /* Prevent out of bound access to OM_DATA_TYPE_SIZE */
222 if ((int)dataType < 0 || (int)dataType > ONNX_TYPE_LAST)
223 return OM_DATA_TYPE_SIZE[ONNX_TYPE_UNDEFINED];
224 return OM_DATA_TYPE_SIZE[dataType];
225}
226
233OM_EXTERNAL_VISIBILITY int64_t omTensorGetBufferSize(const OMTensor *tensor);
234
241OM_EXTERNAL_VISIBILITY int64_t omTensorGetRank(const OMTensor *tensor);
242
249OM_EXTERNAL_VISIBILITY int64_t omTensorGetNumElems(const OMTensor *tensor);
250
256OM_EXTERNAL_VISIBILITY int64_t omTensorGetOwning(const OMTensor *tensor);
257
261OM_EXTERNAL_VISIBILITY void omTensorSetOwning(OMTensor *tensor, int64_t owning);
262
272OM_EXTERNAL_VISIBILITY void omTensorPrint(
273 const char *msg, const OMTensor *tensor);
274
275#ifdef __cplusplus
276}
277#endif
278
279#endif // ONNX_MLIR_OMTENSOR_H
OM_EXTERNAL_VISIBILITY void * omTensorGetDataPtr(const OMTensor *tensor)
OMTensor data pointer getter.
OM_EXTERNAL_VISIBILITY OMTensor * omTensorCreateEmpty(const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype)
struct OMTensor OMTensor
Definition OMTensor.h:57
OM_EXTERNAL_VISIBILITY void * omTensorGetAllocatedPtr(const OMTensor *tensor)
OMTensor allocated data pointer getter.
OM_EXTERNAL_VISIBILITY int64_t omTensorGetNumElems(const OMTensor *tensor)
OMTensor number of elements getter.
OM_EXTERNAL_VISIBILITY int64_t omTensorGetRank(const OMTensor *tensor)
OMTensor rank getter.
OM_EXTERNAL_VISIBILITY OMTensor * omTensorCreateWithOwnership(void *data_ptr, const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype, int64_t owning)
Create an OMTensor with specified data pointer, shape, rank and element type, manually setting data p...
OM_EXTERNAL_VISIBILITY void omTensorPrint(const char *msg, const OMTensor *tensor)
OM_EXTERNAL_VISIBILITY OMTensor * omTensorCreate(void *data_ptr, const int64_t *shape, int64_t rank, OM_DATA_TYPE dtype)
Create a OMTensor with specified data pointer, shape, rank and element type.
OM_EXTERNAL_VISIBILITY int64_t omTensorGetOwning(const OMTensor *tensor)
OMTensor owning flag getter.
OM_EXTERNAL_VISIBILITY const int64_t * omTensorGetShape(const OMTensor *tensor)
OMTensor data shape getter.
OM_EXTERNAL_VISIBILITY int64_t omTensorGetBufferSize(const OMTensor *tensor)
OMTensor numerical data buffer size getter.
OM_EXTERNAL_VISIBILITY OM_DATA_TYPE omTensorGetDataType(const OMTensor *tensor)
OMTensor data type getter.
OM_EXTERNAL_VISIBILITY void omTensorDestroy(OMTensor *tensor)
Destroy the OMTensor struct.
OM_EXTERNAL_VISIBILITY void omTensorSetOwning(OMTensor *tensor, int64_t owning)
OMTensor owning flag setter.