Skip to main content

smoke/
smoke.rs

1//! Run one inference with zero-filled inputs.
2
3use std::process::ExitCode;
4
5use mnn_runtime::{Backend, Runtime, RuntimeConfig, Tensor};
6
7fn main() -> ExitCode {
8    let Some(path) = std::env::args_os().nth(1) else {
9        eprintln!("usage: cargo run -p mnn-runtime --example smoke -- <model.mnn>");
10        return ExitCode::FAILURE;
11    };
12
13    let result = (|| {
14        let backend = match std::env::var("MNN_SMOKE_BACKEND").as_deref() {
15            Ok("metal") => Backend::Metal,
16            Ok("coreml") => Backend::CoreMl,
17            Ok("opencl") => Backend::OpenCl,
18            Ok("opengl") => Backend::OpenGl,
19            Ok("vulkan") => Backend::Vulkan,
20            Ok("cuda") => Backend::Cuda,
21            Ok("auto") => Backend::Auto,
22            Ok(_) | Err(_) => Backend::Cpu,
23        };
24        let runtime = Runtime::new(RuntimeConfig::new().with_backend(backend))?;
25        let model = runtime.load_file(path)?;
26        let inputs = model
27            .info()
28            .inputs()
29            .iter()
30            .map(|info| {
31                Tensor::new(
32                    info.name(),
33                    info.concrete_shape()?,
34                    vec![
35                        0.0;
36                        info.element_count().ok_or_else(|| {
37                            mnn_runtime::Error::UnresolvedShape {
38                                name: info.name().to_owned(),
39                            }
40                        })?
41                    ],
42                )
43            })
44            .collect::<mnn_runtime::Result<Vec<_>>>()?;
45        model.run(&inputs)
46    })();
47
48    match result {
49        Ok(outputs) => {
50            println!("MNN {}", Runtime::native_version());
51            for tensor in outputs {
52                let first = tensor.data().first().copied().unwrap_or_default();
53                println!(
54                    "{} {:?}: {} values, first={first}",
55                    tensor.name(),
56                    tensor.shape(),
57                    tensor.data().len()
58                );
59            }
60            ExitCode::SUCCESS
61        }
62        Err(error) => {
63            eprintln!("error: {error}");
64            ExitCode::FAILURE
65        }
66    }
67}