7
7
//! This module exports its [`types`] for use throughout the crate and the
8
8
//! [`ML`] object, which exposes [`ML::add_to_linker`]. To implement all of
9
9
//! this, this module proceeds in steps:
10
- //! 1. generate all of the WIT glue code into a `gen ::*` namespace
11
- //! 2. wire up the `gen ::*` glue to the context state, delegating actual
10
+ //! 1. generate all of the WIT glue code into a `generated ::*` namespace
11
+ //! 2. wire up the `generated ::*` glue to the context state, delegating actual
12
12
//! computation to a [`Backend`]
13
13
//! 3. convert some types
14
14
//!
@@ -115,7 +115,7 @@ pub enum ErrorCode {
115
115
}
116
116
117
117
/// Generate the traits and types from the `wasi-nn` WIT specification.
118
- mod gen_ {
118
+ mod generated_ {
119
119
wasmtime:: component:: bindgen!( {
120
120
world: "ml" ,
121
121
path: "wit/wasi-nn.wit" ,
@@ -133,35 +133,35 @@ mod gen_ {
133
133
} ,
134
134
} ) ;
135
135
}
136
- use gen_ :: wasi:: nn:: { self as gen } ; // Shortcut to the module containing the types we need.
136
+ use generated_ :: wasi:: nn:: { self as generated } ; // Shortcut to the module containing the types we need.
137
137
138
138
// Export the `types` used in this crate as well as `ML::add_to_linker`.
139
139
pub mod types {
140
- use super :: gen ;
141
- pub use gen :: errors:: Error ;
142
- pub use gen :: graph:: { ExecutionTarget , Graph , GraphBuilder , GraphEncoding } ;
143
- pub use gen :: inference:: GraphExecutionContext ;
144
- pub use gen :: tensor:: { Tensor , TensorType } ;
140
+ use super :: generated ;
141
+ pub use generated :: errors:: Error ;
142
+ pub use generated :: graph:: { ExecutionTarget , Graph , GraphBuilder , GraphEncoding } ;
143
+ pub use generated :: inference:: GraphExecutionContext ;
144
+ pub use generated :: tensor:: { Tensor , TensorType } ;
145
145
}
146
- pub use gen :: graph:: { ExecutionTarget , Graph , GraphBuilder , GraphEncoding } ;
147
- pub use gen :: inference:: GraphExecutionContext ;
148
- pub use gen :: tensor:: { Tensor , TensorData , TensorDimensions , TensorType } ;
149
- pub use gen_ :: Ml as ML ;
146
+ pub use generated :: graph:: { ExecutionTarget , Graph , GraphBuilder , GraphEncoding } ;
147
+ pub use generated :: inference:: GraphExecutionContext ;
148
+ pub use generated :: tensor:: { Tensor , TensorData , TensorDimensions , TensorType } ;
149
+ pub use generated_ :: Ml as ML ;
150
150
151
151
/// Add the WIT-based version of the `wasi-nn` API to a
152
152
/// [`wasmtime::component::Linker`].
153
153
pub fn add_to_linker < T > (
154
154
l : & mut wasmtime:: component:: Linker < T > ,
155
155
f : impl Fn ( & mut T ) -> WasiNnView < ' _ > + Send + Sync + Copy + ' static ,
156
156
) -> anyhow:: Result < ( ) > {
157
- gen :: graph:: add_to_linker_get_host ( l, f) ?;
158
- gen :: tensor:: add_to_linker_get_host ( l, f) ?;
159
- gen :: inference:: add_to_linker_get_host ( l, f) ?;
160
- gen :: errors:: add_to_linker_get_host ( l, f) ?;
157
+ generated :: graph:: add_to_linker_get_host ( l, f) ?;
158
+ generated :: tensor:: add_to_linker_get_host ( l, f) ?;
159
+ generated :: inference:: add_to_linker_get_host ( l, f) ?;
160
+ generated :: errors:: add_to_linker_get_host ( l, f) ?;
161
161
Ok ( ( ) )
162
162
}
163
163
164
- impl gen :: graph:: Host for WasiNnView < ' _ > {
164
+ impl generated :: graph:: Host for WasiNnView < ' _ > {
165
165
fn load (
166
166
& mut self ,
167
167
builders : Vec < GraphBuilder > ,
@@ -210,7 +210,7 @@ impl gen::graph::Host for WasiNnView<'_> {
210
210
}
211
211
}
212
212
213
- impl gen :: graph:: HostGraph for WasiNnView < ' _ > {
213
+ impl generated :: graph:: HostGraph for WasiNnView < ' _ > {
214
214
fn init_execution_context (
215
215
& mut self ,
216
216
graph : Resource < Graph > ,
@@ -235,7 +235,7 @@ impl gen::graph::HostGraph for WasiNnView<'_> {
235
235
}
236
236
}
237
237
238
- impl gen :: inference:: HostGraphExecutionContext for WasiNnView < ' _ > {
238
+ impl generated :: inference:: HostGraphExecutionContext for WasiNnView < ' _ > {
239
239
fn set_input (
240
240
& mut self ,
241
241
exec_context : Resource < GraphExecutionContext > ,
@@ -291,7 +291,7 @@ impl gen::inference::HostGraphExecutionContext for WasiNnView<'_> {
291
291
}
292
292
}
293
293
294
- impl gen :: tensor:: HostTensor for WasiNnView < ' _ > {
294
+ impl generated :: tensor:: HostTensor for WasiNnView < ' _ > {
295
295
fn new (
296
296
& mut self ,
297
297
dimensions : TensorDimensions ,
@@ -328,17 +328,19 @@ impl gen::tensor::HostTensor for WasiNnView<'_> {
328
328
}
329
329
}
330
330
331
- impl gen :: errors:: HostError for WasiNnView < ' _ > {
332
- fn code ( & mut self , error : Resource < Error > ) -> wasmtime:: Result < gen :: errors:: ErrorCode > {
331
+ impl generated :: errors:: HostError for WasiNnView < ' _ > {
332
+ fn code ( & mut self , error : Resource < Error > ) -> wasmtime:: Result < generated :: errors:: ErrorCode > {
333
333
let error = self . table . get ( & error) ?;
334
334
match error. code {
335
- ErrorCode :: InvalidArgument => Ok ( gen:: errors:: ErrorCode :: InvalidArgument ) ,
336
- ErrorCode :: InvalidEncoding => Ok ( gen:: errors:: ErrorCode :: InvalidEncoding ) ,
337
- ErrorCode :: Timeout => Ok ( gen:: errors:: ErrorCode :: Timeout ) ,
338
- ErrorCode :: RuntimeError => Ok ( gen:: errors:: ErrorCode :: RuntimeError ) ,
339
- ErrorCode :: UnsupportedOperation => Ok ( gen:: errors:: ErrorCode :: UnsupportedOperation ) ,
340
- ErrorCode :: TooLarge => Ok ( gen:: errors:: ErrorCode :: TooLarge ) ,
341
- ErrorCode :: NotFound => Ok ( gen:: errors:: ErrorCode :: NotFound ) ,
335
+ ErrorCode :: InvalidArgument => Ok ( generated:: errors:: ErrorCode :: InvalidArgument ) ,
336
+ ErrorCode :: InvalidEncoding => Ok ( generated:: errors:: ErrorCode :: InvalidEncoding ) ,
337
+ ErrorCode :: Timeout => Ok ( generated:: errors:: ErrorCode :: Timeout ) ,
338
+ ErrorCode :: RuntimeError => Ok ( generated:: errors:: ErrorCode :: RuntimeError ) ,
339
+ ErrorCode :: UnsupportedOperation => {
340
+ Ok ( generated:: errors:: ErrorCode :: UnsupportedOperation )
341
+ }
342
+ ErrorCode :: TooLarge => Ok ( generated:: errors:: ErrorCode :: TooLarge ) ,
343
+ ErrorCode :: NotFound => Ok ( generated:: errors:: ErrorCode :: NotFound ) ,
342
344
ErrorCode :: Trap => Err ( anyhow ! ( error. data. to_string( ) ) ) ,
343
345
}
344
346
}
@@ -354,7 +356,7 @@ impl gen::errors::HostError for WasiNnView<'_> {
354
356
}
355
357
}
356
358
357
- impl gen :: errors:: Host for WasiNnView < ' _ > {
359
+ impl generated :: errors:: Host for WasiNnView < ' _ > {
358
360
fn convert_error ( & mut self , err : Error ) -> wasmtime:: Result < Error > {
359
361
if matches ! ( err. code, ErrorCode :: Trap ) {
360
362
Err ( err. data )
@@ -364,18 +366,18 @@ impl gen::errors::Host for WasiNnView<'_> {
364
366
}
365
367
}
366
368
367
- impl gen :: tensor:: Host for WasiNnView < ' _ > { }
368
- impl gen :: inference:: Host for WasiNnView < ' _ > { }
369
+ impl generated :: tensor:: Host for WasiNnView < ' _ > { }
370
+ impl generated :: inference:: Host for WasiNnView < ' _ > { }
369
371
370
- impl Hash for gen :: graph:: GraphEncoding {
372
+ impl Hash for generated :: graph:: GraphEncoding {
371
373
fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
372
374
self . to_string ( ) . hash ( state)
373
375
}
374
376
}
375
377
376
- impl fmt:: Display for gen :: graph:: GraphEncoding {
378
+ impl fmt:: Display for generated :: graph:: GraphEncoding {
377
379
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
378
- use gen :: graph:: GraphEncoding :: * ;
380
+ use generated :: graph:: GraphEncoding :: * ;
379
381
match self {
380
382
Openvino => write ! ( f, "openvino" ) ,
381
383
Onnx => write ! ( f, "onnx" ) ,
@@ -388,16 +390,16 @@ impl fmt::Display for gen::graph::GraphEncoding {
388
390
}
389
391
}
390
392
391
- impl FromStr for gen :: graph:: GraphEncoding {
393
+ impl FromStr for generated :: graph:: GraphEncoding {
392
394
type Err = GraphEncodingParseError ;
393
395
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
394
396
match s. to_lowercase ( ) . as_str ( ) {
395
- "openvino" => Ok ( gen :: graph:: GraphEncoding :: Openvino ) ,
396
- "onnx" => Ok ( gen :: graph:: GraphEncoding :: Onnx ) ,
397
- "pytorch" => Ok ( gen :: graph:: GraphEncoding :: Pytorch ) ,
398
- "tensorflow" => Ok ( gen :: graph:: GraphEncoding :: Tensorflow ) ,
399
- "tensorflowlite" => Ok ( gen :: graph:: GraphEncoding :: Tensorflowlite ) ,
400
- "autodetect" => Ok ( gen :: graph:: GraphEncoding :: Autodetect ) ,
397
+ "openvino" => Ok ( generated :: graph:: GraphEncoding :: Openvino ) ,
398
+ "onnx" => Ok ( generated :: graph:: GraphEncoding :: Onnx ) ,
399
+ "pytorch" => Ok ( generated :: graph:: GraphEncoding :: Pytorch ) ,
400
+ "tensorflow" => Ok ( generated :: graph:: GraphEncoding :: Tensorflow ) ,
401
+ "tensorflowlite" => Ok ( generated :: graph:: GraphEncoding :: Tensorflowlite ) ,
402
+ "autodetect" => Ok ( generated :: graph:: GraphEncoding :: Autodetect ) ,
401
403
_ => Err ( GraphEncodingParseError ( s. into ( ) ) ) ,
402
404
}
403
405
}
0 commit comments