Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-nn/src/registry/in_memory.rs
2464 views
1
//! Implement a [`GraphRegistry`] with a hash map.
2
3
use super::{Graph, GraphRegistry};
4
use crate::backend::BackendFromDir;
5
use crate::wit::ExecutionTarget;
6
use anyhow::{anyhow, bail};
7
use std::{collections::HashMap, path::Path};
8
9
pub struct InMemoryRegistry(HashMap<String, Graph>);
10
impl InMemoryRegistry {
11
pub fn new() -> Self {
12
Self(HashMap::new())
13
}
14
15
/// Load a graph from the files contained in the `path` directory.
16
///
17
/// This expects the backend to know how to load graphs (i.e., ML model)
18
/// from a directory. The name used in the registry is the directory's last
19
/// suffix: if the backend can find the files it expects in `/my/model/foo`,
20
/// the registry will contain a new graph named `foo`.
21
pub fn load(&mut self, backend: &mut dyn BackendFromDir, path: &Path) -> anyhow::Result<()> {
22
if !path.is_dir() {
23
bail!(
24
"preload directory is not a valid directory: {}",
25
path.display()
26
);
27
}
28
let name = path
29
.file_name()
30
.map(|s| s.to_string_lossy())
31
.ok_or(anyhow!("no file name in path"))?;
32
33
let graph = backend.load_from_dir(path, ExecutionTarget::Cpu)?;
34
self.0.insert(name.into_owned(), graph);
35
Ok(())
36
}
37
}
38
39
impl GraphRegistry for InMemoryRegistry {
40
fn get(&self, name: &str) -> Option<&Graph> {
41
self.0.get(name)
42
}
43
fn get_mut(&mut self, name: &str) -> Option<&mut Graph> {
44
self.0.get_mut(name)
45
}
46
}
47
48