forked from onnx/onnx-mlir
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
55 lines (45 loc) · 1.73 KB
/
main.c
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
#include <OnnxMlirRuntime.h>
#include <stdio.h>
OMTensorList *run_main_graph(OMTensorList *);
OMTensorList *create_input_list() {
// Shared shape & rank.
int64_t shape[] = {3, 2};
int64_t num_elements = shape[0] * shape[1];
int64_t rank = 2;
// Construct float arrays filled with 1s or 2s.
float *x1Data = (float *)malloc(sizeof(float) * num_elements);
for (int i = 0; i < num_elements; i++)
x1Data[i] = 1.0;
float *x2Data = (float *)malloc(sizeof(float) * num_elements);
for (int i = 0; i < num_elements; i++)
x2Data[i] = 2.0;
// Use omTensorCreateWithOwnership "true" so float arrays are automatically
// freed when the Tensors are destroyed.
OMTensor *x1 = omTensorCreateWithOwnership(x1Data, shape, rank, ONNX_TYPE_FLOAT, true);
OMTensor *x2 = omTensorCreateWithOwnership(x2Data, shape, rank, ONNX_TYPE_FLOAT, true);
// Construct a TensorList using the Tensors
OMTensor *list[2] = {x1, x2};
return omTensorListCreate(list, 2);
}
int main() {
// Generate input TensorList
OMTensorList *input_list = create_input_list();
// Call the compiled onnx model function.
OMTensorList *output_list = run_main_graph(input_list);
if (!output_list) {
// May inspect errno to get info about the error.
return 1;
}
// Get the first tensor from output list.
OMTensor *y = omTensorListGetOmtByIndex(output_list, 0);
float *outputPtr = (float *) omTensorGetDataPtr(y);
// Print its content, should be all 3.
for (int i = 0; i < 6; i++)
printf("%f ", outputPtr[i]);
printf("\n");
// Destory the list and the tensors inside of it.
// Use omTensorListDestroyShallow if only want to destroy the list themselves.
omTensorListDestroy(input_list);
omTensorListDestroy(output_list);
return 0;
}