forked from bytecodealliance/wasi-nn
/
generated.rs
383 lines (362 loc) · 11.5 KB
/
generated.rs
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// This file is automatically generated, DO NOT EDIT
//
// To regenerate this file run the `crates/witx-bindgen` command
use core::fmt;
use core::mem::MaybeUninit;
pub type BufferSize = u32;
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct NnErrno(u16);
pub const NN_ERRNO_SUCCESS: NnErrno = NnErrno(0);
pub const NN_ERRNO_INVALID_ARGUMENT: NnErrno = NnErrno(1);
pub const NN_ERRNO_INVALID_ENCODING: NnErrno = NnErrno(2);
pub const NN_ERRNO_MISSING_MEMORY: NnErrno = NnErrno(3);
pub const NN_ERRNO_BUSY: NnErrno = NnErrno(4);
pub const NN_ERRNO_RUNTIME_ERROR: NnErrno = NnErrno(5);
pub const NN_ERRNO_UNSUPPORTED_OPERATION: NnErrno = NnErrno(6);
pub const NN_ERRNO_TOO_LARGE: NnErrno = NnErrno(7);
pub const NN_ERRNO_NOT_FOUND: NnErrno = NnErrno(8);
impl NnErrno {
pub const fn raw(&self) -> u16 {
self.0
}
pub fn name(&self) -> &'static str {
match self.0 {
0 => "SUCCESS",
1 => "INVALID_ARGUMENT",
2 => "INVALID_ENCODING",
3 => "MISSING_MEMORY",
4 => "BUSY",
5 => "RUNTIME_ERROR",
6 => "UNSUPPORTED_OPERATION",
7 => "TOO_LARGE",
8 => "NOT_FOUND",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => "",
1 => "",
2 => "",
3 => "",
4 => "",
5 => "",
6 => "",
7 => "",
8 => "",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for NnErrno {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NnErrno")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
impl fmt::Display for NnErrno {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (error {})", self.name(), self.0)
}
}
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl std::error::Error for NnErrno {}
pub type TensorDimensions<'a> = &'a [u32];
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct TensorType(u8);
pub const TENSOR_TYPE_F16: TensorType = TensorType(0);
pub const TENSOR_TYPE_F32: TensorType = TensorType(1);
pub const TENSOR_TYPE_F64: TensorType = TensorType(2);
pub const TENSOR_TYPE_U8: TensorType = TensorType(3);
pub const TENSOR_TYPE_I32: TensorType = TensorType(4);
pub const TENSOR_TYPE_I64: TensorType = TensorType(5);
impl TensorType {
pub const fn raw(&self) -> u8 {
self.0
}
pub fn name(&self) -> &'static str {
match self.0 {
0 => "F16",
1 => "F32",
2 => "F64",
3 => "U8",
4 => "I32",
5 => "I64",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => "",
1 => "",
2 => "",
3 => "",
4 => "",
5 => "",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for TensorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TensorType")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
pub type TensorData<'a> = &'a [u8];
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Tensor<'a> {
pub dimensions: TensorDimensions<'a>,
pub type_: TensorType,
pub data: TensorData<'a>,
}
pub type GraphBuilder<'a> = &'a [u8];
pub type GraphBuilderArray<'a> = &'a [GraphBuilder<'a>];
pub type Graph = u32;
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct GraphEncoding(u8);
pub const GRAPH_ENCODING_OPENVINO: GraphEncoding = GraphEncoding(0);
pub const GRAPH_ENCODING_ONNX: GraphEncoding = GraphEncoding(1);
pub const GRAPH_ENCODING_TENSORFLOW: GraphEncoding = GraphEncoding(2);
pub const GRAPH_ENCODING_PYTORCH: GraphEncoding = GraphEncoding(3);
pub const GRAPH_ENCODING_TENSORFLOWLITE: GraphEncoding = GraphEncoding(4);
pub const GRAPH_ENCODING_AUTODETECT: GraphEncoding = GraphEncoding(5);
impl GraphEncoding {
pub const fn raw(&self) -> u8 {
self.0
}
pub fn name(&self) -> &'static str {
match self.0 {
0 => "OPENVINO",
1 => "ONNX",
2 => "TENSORFLOW",
3 => "PYTORCH",
4 => "TENSORFLOWLITE",
5 => "AUTODETECT",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => "",
1 => "",
2 => "",
3 => "",
4 => "",
5 => "",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for GraphEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GraphEncoding")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct ExecutionTarget(u8);
pub const EXECUTION_TARGET_CPU: ExecutionTarget = ExecutionTarget(0);
pub const EXECUTION_TARGET_GPU: ExecutionTarget = ExecutionTarget(1);
pub const EXECUTION_TARGET_TPU: ExecutionTarget = ExecutionTarget(2);
impl ExecutionTarget {
pub const fn raw(&self) -> u8 {
self.0
}
pub fn name(&self) -> &'static str {
match self.0 {
0 => "CPU",
1 => "GPU",
2 => "TPU",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => "",
1 => "",
2 => "",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for ExecutionTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExecutionTarget")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
pub type GraphExecutionContext = u32;
pub unsafe fn load(
builder: GraphBuilderArray<'_>,
encoding: GraphEncoding,
target: ExecutionTarget,
) -> Result<Graph, NnErrno> {
let mut rp0 = MaybeUninit::<Graph>::uninit();
let ret = wasi_ephemeral_nn::load(
builder.as_ptr() as i32,
builder.len() as i32,
encoding.0 as i32,
target.0 as i32,
rp0.as_mut_ptr() as i32,
);
match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Graph)),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn load_by_name(name: &str) -> Result<Graph, NnErrno> {
let mut rp0 = MaybeUninit::<Graph>::uninit();
let ret = wasi_ephemeral_nn::load_by_name(
name.as_ptr() as i32,
name.len() as i32,
rp0.as_mut_ptr() as i32,
);
match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Graph)),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn load_by_name_with_config(name: &str, config: &str) -> Result<Graph, NnErrno> {
let mut rp0 = MaybeUninit::<Graph>::uninit();
let ret = wasi_ephemeral_nn::load_by_name_with_config(
name.as_ptr() as i32,
name.len() as i32,
config.as_ptr() as i32,
config.len() as i32,
rp0.as_mut_ptr() as i32,
);
match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Graph)),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn init_execution_context(graph: Graph) -> Result<GraphExecutionContext, NnErrno> {
let mut rp0 = MaybeUninit::<GraphExecutionContext>::uninit();
let ret = wasi_ephemeral_nn::init_execution_context(graph as i32, rp0.as_mut_ptr() as i32);
match ret {
0 => Ok(core::ptr::read(
rp0.as_mut_ptr() as i32 as *const GraphExecutionContext
)),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn set_input(
context: GraphExecutionContext,
index: u32,
tensor: Tensor,
) -> Result<(), NnErrno> {
let ret =
wasi_ephemeral_nn::set_input(context as i32, index as i32, &tensor as *const _ as i32);
match ret {
0 => Ok(()),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn get_output(
context: GraphExecutionContext,
index: u32,
out_buffer: *mut u8,
out_buffer_max_size: BufferSize,
) -> Result<BufferSize, NnErrno> {
let mut rp0 = MaybeUninit::<BufferSize>::uninit();
let ret = wasi_ephemeral_nn::get_output(
context as i32,
index as i32,
out_buffer as i32,
out_buffer_max_size as i32,
rp0.as_mut_ptr() as i32,
);
match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const BufferSize)),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn get_output_single(
context: GraphExecutionContext,
index: u32,
out_buffer: *mut u8,
out_buffer_max_size: BufferSize,
) -> Result<BufferSize, NnErrno> {
let mut rp0 = MaybeUninit::<BufferSize>::uninit();
let ret = wasi_ephemeral_nn::get_output_single(
context as i32,
index as i32,
out_buffer as i32,
out_buffer_max_size as i32,
rp0.as_mut_ptr() as i32,
);
match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const BufferSize)),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn compute(context: GraphExecutionContext) -> Result<(), NnErrno> {
let ret = wasi_ephemeral_nn::compute(context as i32);
match ret {
0 => Ok(()),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn compute_single(context: GraphExecutionContext) -> Result<(), NnErrno> {
let ret = wasi_ephemeral_nn::compute_single(context as i32);
match ret {
0 => Ok(()),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn fini_single(context: GraphExecutionContext) -> Result<(), NnErrno> {
let ret = wasi_ephemeral_nn::fini_single(context as i32);
match ret {
0 => Ok(()),
_ => Err(NnErrno(ret as u16)),
}
}
pub unsafe fn unload(graph: Graph) -> Result<(), NnErrno> {
let ret = wasi_ephemeral_nn::unload(graph as i32);
match ret {
0 => Ok(()),
_ => Err(NnErrno(ret as u16)),
}
}
pub mod wasi_ephemeral_nn {
#[link(wasm_import_module = "wasi_ephemeral_nn")]
extern "C" {
pub fn load(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32) -> i32;
pub fn load_by_name(arg0: i32, arg1: i32, arg2: i32) -> i32;
pub fn init_execution_context(arg0: i32, arg1: i32) -> i32;
pub fn set_input(arg0: i32, arg1: i32, arg2: i32) -> i32;
pub fn get_output(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32) -> i32;
pub fn get_output_single(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32) -> i32;
pub fn compute(arg0: i32) -> i32;
pub fn compute_single(arg0: i32) -> i32;
pub fn fini_single(arg0: i32) -> i32;
pub fn load_by_name_with_config(
arg0: i32,
arg1: i32,
arg2: i32,
arg3: i32,
arg4: i32,
) -> i32;
pub fn unload(arg0: i32) -> i32;
}
}