forked from onnx/onnx-mlir
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mnist.cpp
310 lines (300 loc) · 19 KB
/
mnist.cpp
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
#include <iostream>
#include <vector>
#include "OnnxMlirRuntime.h"
// Declare the inference entry point.
extern "C" OMTensorList *run_main_graph(OMTensorList *);
static float img_data[] = {-0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.029637714847922325f, 0.7467845678329468f,
1.7777715921401978f, 2.796030282974243f, 1.5104787349700928f,
1.5104787349700928f, 0.36493754386901855f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 0.09764464199542999f, 2.5414655208587646f,
2.783302068710327f, 2.796030282974243f, 2.783302068710327f,
2.796030282974243f, 2.783302068710327f, 2.4141831398010254f,
0.6067740321159363f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 0.4922199249267578f, 2.5414655208587646f,
2.808758497238159f, 1.7650433778762817f, -0.4242129623889923f,
-0.4242129623889923f, 2.0323362350463867f, 2.796030282974243f,
2.808758497238159f, 2.2869009971618652f, -0.15692004561424255f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 0.09764464199542999f, 2.796030282974243f,
2.783302068710327f, 1.2431857585906982f, -0.2969306409358978f,
-0.4242129623889923f, -0.4242129623889923f, 0.4794916808605194f,
2.783302068710327f, 2.796030282974243f, 2.783302068710327f,
1.3831963539123535f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.2842023968696594f, 1.7777715921401978f,
2.808758497238159f, 2.5414655208587646f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
0.09764464199542999f, 2.668747901916504f, 2.808758497238159f,
2.796030282974243f, 2.808758497238159f, 1.7650433778762817f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, 0.22492696344852448f,
2.783302068710327f, 2.796030282974243f, 0.466763436794281f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, 2.159618616104126f,
1.4977505207061768f, 2.5287373065948486f, 2.796030282974243f,
2.783302068710327f, 0.6195022463798523f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
1.5104787349700928f, 2.796030282974243f, 2.68147611618042f,
0.08491640537977219f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
0.09764464199542999f, -0.4242129623889923f, 0.6195022463798523f,
2.5541937351226807f, 2.796030282974243f, 2.5541937351226807f,
-0.042365945875644684f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
0.09764464199542999f, 2.5414655208587646f, 2.783302068710327f,
1.6377609968185425f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 1.0013492107391357f, 2.783302068710327f,
2.796030282974243f, 1.7650433778762817f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 0.8740668296813965f, 2.808758497238159f,
2.796030282974243f, 0.6195022463798523f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.15692004561424255f,
2.4141831398010254f, 2.808758497238159f, 2.796030282974243f,
0.36493754386901855f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, 1.3831963539123535f,
2.796030282974243f, 2.783302068710327f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 1.1286314725875854f, 2.796030282974243f,
2.783302068710327f, 0.8740668296813965f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.029637714847922325f,
2.5414655208587646f, 2.808758497238159f, 2.796030282974243f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
2.808758497238159f, 2.796030282974243f, 2.159618616104126f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
0.22492696344852448f, 2.783302068710327f, 2.796030282974243f,
2.783302068710327f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 2.796030282974243f, 2.783302068710327f,
2.159618616104126f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 0.22492696344852448f, 2.796030282974243f,
2.808758497238159f, 2.796030282974243f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.15692004561424255f, 2.808758497238159f,
2.796030282974243f, 1.3831963539123535f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, 0.22492696344852448f,
2.783302068710327f, 2.796030282974243f, 2.783302068710327f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, 0.8740668296813965f,
2.796030282974243f, 2.783302068710327f, 0.8740668296813965f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 1.905053973197937f, 2.808758497238159f,
2.796030282974243f, 1.6504892110824585f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.029637714847922325f,
2.5414655208587646f, 2.808758497238159f, 2.5414655208587646f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, 0.8740668296813965f,
2.796030282974243f, 2.783302068710327f, 2.668747901916504f,
0.08491640537977219f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
1.2559140920639038f, 2.783302068710327f, 2.796030282974243f,
0.466763436794281f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 1.7777715921401978f, 2.796030282974243f,
2.821486711502075f, 2.796030282974243f, 0.6195022463798523f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
0.6195022463798523f, 2.808758497238159f, 2.796030282974243f,
1.1413596868515015f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.2969306409358978f,
1.7650433778762817f, 2.796030282974243f, 2.783302068710327f,
2.668747901916504f, 1.6377609968185425f, 0.09764464199542999f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
1.6504892110824585f, 2.668747901916504f, 2.796030282974243f,
2.274172782897949f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, 1.1413596868515015f,
2.668747901916504f, 2.808758497238159f, 2.796030282974243f,
2.5541937351226807f, 1.5104787349700928f, 1.5232069492340088f,
2.5414655208587646f, 2.808758497238159f, 2.5414655208587646f,
1.1413596868515015f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, 0.08491640537977219f, 1.4977505207061768f,
2.5287373065948486f, 2.796030282974243f, 2.783302068710327f,
2.796030282974243f, 2.783302068710327f, 1.4977505207061768f,
0.21219873428344727f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f, -0.4242129623889923f,
-0.4242129623889923f, -0.4242129623889923f};
int main() {
// Create an input tensor list of 1 tensor.
int inputNum = 1;
OMTensor *inputTensors[inputNum];
// The first input is of tensor<1x1x28x28xf32>.
int64_t rank = 4;
int64_t shape[] = {1, 1, 28, 28};
OMTensor *tensor = omTensorCreate(img_data, shape, rank, ONNX_TYPE_FLOAT);
// Create a tensor list.
inputTensors[0] = tensor;
OMTensorList *tensorListIn = omTensorListCreate(inputTensors, inputNum);
// Compute outputs.
OMTensorList *tensorListOut = run_main_graph(tensorListIn);
// Free the input as it is no longer needed.
omTensorListDestroy(tensorListIn);
// Extract the output. The model defines one output of type tensor<1x10xf32>.
OMTensor *y = omTensorListGetOmtByIndex(tensorListOut, 0);
float *prediction = (float *)omTensorGetDataPtr(y);
// Analyze the output.
int digit = -1;
float prob = 0.;
for (int i = 0; i < 10; i++) {
printf("prediction[%d] = %f\n", i, prediction[i]);
if (prediction[i] > prob) {
digit = i;
prob = prediction[i];
}
}
// Free the output as it is no longer needed.
omTensorListDestroy(tensorListOut);
printf("The digit is %d\n", digit);
return 0;
}