wasmedge_sdk/
plugin.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Defines plugin related structs.

use crate::{instance::Instance, WasmEdgeResult};
use wasmedge_sys::{self as sys};

#[cfg(feature = "wasi_nn")]
use wasmedge_types::error::WasmEdgeError;

/// Defines low-level types used in Plugin development.
pub mod ffi {
    pub use wasmedge_sys::ffi::{
        WasmEdge_ModuleDescriptor, WasmEdge_ModuleInstanceContext, WasmEdge_PluginDescriptor,
    };
}

/// Preload config for initializing the wasi_nn plug-in.
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
#[derive(Debug)]
pub struct NNPreload {
    /// The alias of the model in the WASI-NN environment.
    alias: String,
    /// The inference backend.
    backend: GraphEncoding,
    /// The execution target, on which the inference runs.
    target: ExecutionTarget,
    /// The path to the model file. Note that the path is the guest path instead of the host path.
    path: std::path::PathBuf,
}
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
impl NNPreload {
    /// Creates a new preload config.
    ///
    /// # Arguments
    ///
    /// * `alias` - The alias of the model in the WASI-NN environment.
    ///
    /// * `backend` - The inference backend.
    ///
    /// * `target` - The execution target, on which the inference runs.
    ///
    /// * `path` - The path to the model file. Note that the path is the guest path instead of the host path.
    ///
    pub fn new(
        alias: impl AsRef<str>,
        backend: GraphEncoding,
        target: ExecutionTarget,
        path: impl AsRef<std::path::Path>,
    ) -> Self {
        Self {
            alias: alias.as_ref().to_owned(),
            backend,
            target,
            path: path.as_ref().to_owned(),
        }
    }
}
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
impl std::fmt::Display for NNPreload {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{alias}:{backend}:{target}:{path}",
            alias = self.alias,
            backend = self.backend,
            target = self.target,
            path = self.path.to_string_lossy().into_owned()
        )
    }
}
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
impl std::str::FromStr for NNPreload {
    type Err = WasmEdgeError;

    fn from_str(preload: &str) -> std::result::Result<Self, Self::Err> {
        let nn_preload: Vec<&str> = preload.split(':').collect();
        if nn_preload.len() != 4 {
            return Err(WasmEdgeError::Operation(format!(
                "Failed to convert to NNPreload value. Invalid preload string: {}. The correct format is: 'alias:backend:target:path'",
                preload
            )));
        }
        let (alias, backend, target, path) = (
            nn_preload[0].to_string(),
            nn_preload[1]
                .parse::<GraphEncoding>()
                .map_err(|err| WasmEdgeError::Operation(err.to_string()))?,
            nn_preload[2]
                .parse::<ExecutionTarget>()
                .map_err(|err| WasmEdgeError::Operation(err.to_string()))?,
            std::path::PathBuf::from(nn_preload[3]),
        );

        Ok(Self::new(alias, backend, target, path))
    }
}

#[cfg(feature = "wasi_nn")]
#[test]
fn test_generate_nnpreload_from_str() {
    use std::str::FromStr;

    // valid preload string
    let preload = "default:GGML:CPU:llama-2-7b-chat.Q5_K_M.gguf";
    let result = NNPreload::from_str(preload);
    assert!(result.is_ok());
    let nnpreload = result.unwrap();
    assert_eq!(nnpreload.alias, "default");
    assert_eq!(nnpreload.backend, GraphEncoding::GGML);
    assert_eq!(nnpreload.target, ExecutionTarget::CPU);
    assert_eq!(
        nnpreload.path,
        std::path::PathBuf::from("llama-2-7b-chat.Q5_K_M.gguf")
    );

    // invalid preload string
    let preload = "default:CPU:GGML:llama-2-7b-chat.Q5_K_M.gguf";
    let result = NNPreload::from_str(preload);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert_eq!(
        WasmEdgeError::Operation(
            "Failed to convert to NNBackend value. Unknown NNBackend type: CPU".to_string()
        ),
        err
    );

    // invalid preload string: unsupported target
    let preload = "default:GGML:NPU:llama-2-7b-chat.Q5_K_M.gguf";
    let result = NNPreload::from_str(preload);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert_eq!(
        WasmEdgeError::Operation(
            "Failed to convert to ExecutionTarget value. Unknown ExecutionTarget type: NPU"
                .to_string()
        ),
        err
    );

    // invalid preload string: invalid format
    let preload = "default:GGML:CPU";
    let result = NNPreload::from_str(preload);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert_eq!(
        WasmEdgeError::Operation(
            "Failed to convert to NNPreload value. Invalid preload string: default:GGML:CPU. The correct format is: 'alias:backend:target:path'"
                .to_string()
        ),
        err
    );
}

/// Describes the encoding of the graph.
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
pub enum GraphEncoding {
    OpenVINO,
    ONNX,
    TensorFlow,
    PyTorch,
    TensorFlowLite,
    Autodetect,
    GGML,
}
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
impl std::fmt::Display for GraphEncoding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GraphEncoding::PyTorch => write!(f, "PyTorch"),
            GraphEncoding::TensorFlowLite => write!(f, "TensorflowLite"),
            GraphEncoding::TensorFlow => write!(f, "Tensorflow"),
            GraphEncoding::OpenVINO => write!(f, "OpenVINO"),
            GraphEncoding::GGML => write!(f, "GGML"),
            GraphEncoding::ONNX => write!(f, "ONNX"),
            GraphEncoding::Autodetect => write!(f, "Autodetect"),
        }
    }
}
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
impl std::str::FromStr for GraphEncoding {
    type Err = WasmEdgeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "openvino" => Ok(GraphEncoding::OpenVINO),
            "onnx" => Ok(GraphEncoding::ONNX),
            "tensorflow" => Ok(GraphEncoding::TensorFlow),
            "pytorch" => Ok(GraphEncoding::PyTorch),
            "tensorflowlite" => Ok(GraphEncoding::TensorFlowLite),
            "autodetect" => Ok(GraphEncoding::Autodetect),
            "ggml" => Ok(GraphEncoding::GGML),
            _ => Err(WasmEdgeError::Operation(format!(
                "Failed to convert to NNBackend value. Unknown NNBackend type: {}",
                s
            ))),
        }
    }
}

/// Define where the graph should be executed.
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[allow(non_camel_case_types)]
pub enum ExecutionTarget {
    CPU,
    GPU,
    TPU,
    AUTO,
}
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
impl std::fmt::Display for ExecutionTarget {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExecutionTarget::CPU => write!(f, "CPU"),
            ExecutionTarget::GPU => write!(f, "GPU"),
            ExecutionTarget::TPU => write!(f, "TPU"),
            ExecutionTarget::AUTO => write!(f, "AUTO"),
        }
    }
}
#[cfg(feature = "wasi_nn")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
impl std::str::FromStr for ExecutionTarget {
    type Err = WasmEdgeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "CPU" => Ok(ExecutionTarget::CPU),
            "GPU" => Ok(ExecutionTarget::GPU),
            "TPU" => Ok(ExecutionTarget::TPU),
            "AUTO" => Ok(ExecutionTarget::AUTO),
            _ => Err(WasmEdgeError::Operation(format!(
                "Failed to convert to ExecutionTarget value. Unknown ExecutionTarget type: {}",
                s
            ))),
        }
    }
}

/// Defines the API to manage plugins.
#[derive(Debug)]
pub struct PluginManager {}
impl PluginManager {
    /// Load plugins from the given path.
    ///
    /// * If the path is not given, then the default plugin paths will be used. The default plugin paths are
    ///
    ///     * The environment variable "WASMEDGE_PLUGIN_PATH".
    ///
    ///     * The `../plugin/` directory related to the WasmEdge installation path.
    ///
    ///     * The `wasmedge/` directory under the library path if the WasmEdge is installed under the "/usr".
    ///
    /// * If the path is given, then
    ///
    ///     * If the path is pointing at a file , then it indicates that a single plugin will be loaded from the file.
    ///
    ///     * If the path is pointing at a directory, then the method will load plugins from the files in the directory.
    ///
    /// # Argument
    ///
    /// * `path` - A path to a plugin file or a directory holding plugin files. If `None`, then the default plugin path will be used.
    pub fn load(path: Option<&std::path::Path>) -> WasmEdgeResult<()> {
        match path {
            Some(p) => sys::plugin::PluginManager::load_plugins(p),
            None => {
                sys::plugin::PluginManager::load_plugins_from_default_paths();
                Ok(())
            }
        }
    }

    /// Initialize the wasi_nn plug-in with the preloads.
    ///
    /// Note that this function is only available after loading the wasi_nn plug-in and before creating, and before creating the module instance from the plug-in.
    ///
    /// # Argument
    ///
    /// * `preloads` - The preload list.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // load wasinn-pytorch-plugin from the default plugin directory: /usr/local/lib/wasmedge
    /// PluginManager::load(None)?;
    /// // preload named model
    /// PluginManager::nn_preload(vec![NNPreload::new(
    ///     "default",
    ///     GraphEncoding::GGML,
    ///     ExecutionTarget::CPU,
    ///     "llama-2-7b-chat.Q5_K_M.gguf",
    /// )]);
    /// ```
    ///
    /// If a preload is string, then use `NNPreload::from_str` to create a `NNPreload` instance:
    ///
    /// ```ignore
    /// use std::str::FromStr;
    ///
    /// // load wasinn-pytorch-plugin from the default plugin directory: /usr/local/lib/wasmedge
    /// PluginManager::load(None)?;
    /// // preload named model
    /// PluginManager::nn_preload(vec![NNPreload::from_str("default:GGML:CPU:llama-2-7b-chat.Q5_K_M.gguf")?]);
    ///
    /// ```
    #[cfg(feature = "wasi_nn")]
    #[cfg_attr(docsrs, doc(cfg(feature = "wasi_nn")))]
    pub fn nn_preload(preloads: Vec<NNPreload>) {
        let mut nn_preloads = Vec::new();
        for preload in preloads {
            nn_preloads.push(preload.to_string());
        }

        let nn_preloads_str: Vec<&str> = nn_preloads.iter().map(|s| s.as_str()).collect();

        sys::plugin::PluginManager::nn_preload(nn_preloads_str);
    }

    /// Returns the count of loaded plugins.
    pub fn count() -> u32 {
        sys::plugin::PluginManager::count()
    }

    /// Returns the names of all loaded plugins.
    pub fn names() -> Vec<String> {
        sys::plugin::PluginManager::names()
    }

    /// Returns the target plugin by its name.
    ///
    /// # Argument
    ///
    /// * `name` - The name of the target plugin.
    ///
    /// # Error
    ///
    /// If failed to return the plugin module instance, then return [PluginError::NotFound](wasmedge_types::error::PluginError::NotFound) error.
    pub fn find(name: impl AsRef<str>) -> WasmEdgeResult<Plugin> {
        sys::plugin::PluginManager::find(name.as_ref()).map(|p| Plugin { inner: p })
    }

    pub fn create_plugin_instance(
        pname: impl AsRef<str>,
        mname: impl AsRef<str>,
    ) -> WasmEdgeResult<PluginInstance> {
        let plugin = sys::plugin::PluginManager::create_plugin_instance(pname, mname)?;
        Ok(plugin)
    }

    /// Initializes the `wasmedge_process` plugin module instance with the parameters.
    ///
    /// # Arguments
    ///
    /// * `allowed_cmds` - A white list of commands.
    ///
    /// * `allowed` - Determines if wasmedge_process is allowed to execute all commands on the white list.
    #[cfg(all(
        target_os = "linux",
        feature = "wasmedge_process",
        not(feature = "static")
    ))]
    #[cfg_attr(
        docsrs,
        doc(cfg(all(
            target_os = "linux",
            feature = "wasmedge_process",
            not(feature = "static")
        )))
    )]
    pub fn init_wasmedge_process(allowed_cmds: Option<Vec<&str>>, allowed: bool) {
        sys::plugin::PluginManager::init_wasmedge_process(allowed_cmds, allowed);
    }

    pub fn auto_detect_plugins() -> WasmEdgeResult<Vec<Instance>> {
        let mut plugin_mods = vec![];
        for plugin_name in PluginManager::names().iter() {
            if let Ok(plugin) = PluginManager::find(plugin_name) {
                for mod_name in plugin.mod_names().iter() {
                    if let Ok(mod_instance) = plugin.mod_instance(mod_name) {
                        plugin_mods.push(mod_instance)
                    }
                }
            }
        }
        Ok(plugin_mods)
    }
}

impl PluginManager {
    pub fn load_plugin_wasi_nn() -> WasmEdgeResult<Instance> {
        Self::create_plugin_instance("wasi_nn", "wasi_nn")
    }

    pub fn load_wasi_crypto_common() -> WasmEdgeResult<Instance> {
        Self::create_plugin_instance("wasi_crypto", "wasi_crypto_common")
    }
    pub fn load_wasi_crypto_asymmetric_common() -> WasmEdgeResult<Instance> {
        Self::create_plugin_instance("wasi_crypto", "wasi_crypto_asymmetric_common")
    }
    pub fn load_wasi_crypto_kx() -> WasmEdgeResult<Instance> {
        Self::create_plugin_instance("wasi_crypto", "wasi_crypto_kx")
    }
    pub fn load_wasi_crypto_signatures() -> WasmEdgeResult<Instance> {
        Self::create_plugin_instance("wasi_crypto", "wasi_crypto_signatures")
    }
    pub fn load_wasi_crypto_symmetric() -> WasmEdgeResult<Instance> {
        Self::create_plugin_instance("wasi_crypto", "wasi_crypto_symmetric")
    }
}

/// Represents a loaded plugin. It provides the APIs for accessing the plugin.
#[derive(Debug)]
pub struct Plugin {
    inner: sys::plugin::Plugin,
}
impl Plugin {
    /// Returns the name of this plugin.
    pub fn name(&self) -> String {
        self.inner.name()
    }

    /// Returns the count of the module instances in this plugin.
    pub fn mod_count(&self) -> u32 {
        self.inner.mod_count()
    }

    /// Returns the names of all module instances in this plugin.
    pub fn mod_names(&self) -> Vec<String> {
        self.inner.mod_names()
    }

    /// Returns a module instance that is generated from the module with the given name in this plugin.
    ///
    /// # Argument
    ///
    /// * `name` - The name of the target module.
    ///
    /// # Error
    ///
    /// If failed to return the plugin module instance, then return [PluginError::Create](wasmedge_types::error::PluginError::Create) error.
    pub fn mod_instance(&self, name: impl AsRef<str>) -> WasmEdgeResult<PluginInstance> {
        self.inner.mod_instance(name.as_ref())
    }
}

pub type PluginInstance = Instance;