pub struct TensorInfo { /* private fields */ }Expand description
Model tensor metadata at load time. Nonpositive dimensions are unresolved.
Use returned Tensor shapes for the actual dimensions of a particular run.
Implementations§
Source§impl TensorInfo
impl TensorInfo
Sourcepub fn is_channel_last(&self) -> bool
pub fn is_channel_last(&self) -> bool
Whether dimensions/values use channel-last order (NHWC for rank four).
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Tensor name from the MNN graph.
Examples found in repository?
examples/inspect.rs (line 19)
7fn main() -> ExitCode {
8 let Some(path) = std::env::args_os().nth(1) else {
9 eprintln!("usage: cargo run -p mnn-runtime --example inspect -- <model.mnn>");
10 return ExitCode::FAILURE;
11 };
12
13 let result = Runtime::cpu().and_then(|runtime| runtime.load_file(path));
14 match result {
15 Ok(model) => {
16 println!("MNN {}", Runtime::native_version());
17 println!("inputs:");
18 for tensor in model.info().inputs() {
19 println!(" {} {:?}", tensor.name(), tensor.shape());
20 }
21 println!("outputs:");
22 for tensor in model.info().outputs() {
23 println!(" {} {:?}", tensor.name(), tensor.shape());
24 }
25 ExitCode::SUCCESS
26 }
27 Err(error) => {
28 eprintln!("error: {error}");
29 ExitCode::FAILURE
30 }
31 }
32}More examples
examples/smoke.rs (line 32)
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}Sourcepub fn shape(&self) -> &[i32]
pub fn shape(&self) -> &[i32]
Signed graph dimensions. Nonpositive dimensions require concrete inputs.
An output awaiting its first resize is represented by [-1].
Examples found in repository?
examples/inspect.rs (line 19)
7fn main() -> ExitCode {
8 let Some(path) = std::env::args_os().nth(1) else {
9 eprintln!("usage: cargo run -p mnn-runtime --example inspect -- <model.mnn>");
10 return ExitCode::FAILURE;
11 };
12
13 let result = Runtime::cpu().and_then(|runtime| runtime.load_file(path));
14 match result {
15 Ok(model) => {
16 println!("MNN {}", Runtime::native_version());
17 println!("inputs:");
18 for tensor in model.info().inputs() {
19 println!(" {} {:?}", tensor.name(), tensor.shape());
20 }
21 println!("outputs:");
22 for tensor in model.info().outputs() {
23 println!(" {} {:?}", tensor.name(), tensor.shape());
24 }
25 ExitCode::SUCCESS
26 }
27 Err(error) => {
28 eprintln!("error: {error}");
29 ExitCode::FAILURE
30 }
31 }
32}Sourcepub fn is_dynamic(&self) -> bool
pub fn is_dynamic(&self) -> bool
Whether one or more dimensions are unresolved.
Sourcepub fn concrete_shape(&self) -> Result<Vec<usize>>
pub fn concrete_shape(&self) -> Result<Vec<usize>>
Concrete dimensions, when known.
§Errors
Returns UnresolvedShape if dimensions still depend on the input.
Examples found in repository?
examples/smoke.rs (line 33)
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}Sourcepub fn element_count(&self) -> Option<usize>
pub fn element_count(&self) -> Option<usize>
Number of f32 elements, or None until all dimensions are known.
Examples found in repository?
examples/smoke.rs (line 36)
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}Trait Implementations§
Source§impl Clone for TensorInfo
impl Clone for TensorInfo
Source§impl Debug for TensorInfo
impl Debug for TensorInfo
impl Eq for TensorInfo
Source§impl PartialEq for TensorInfo
impl PartialEq for TensorInfo
impl StructuralPartialEq for TensorInfo
Auto Trait Implementations§
impl Freeze for TensorInfo
impl RefUnwindSafe for TensorInfo
impl Send for TensorInfo
impl Sync for TensorInfo
impl Unpin for TensorInfo
impl UnsafeUnpin for TensorInfo
impl UnwindSafe for TensorInfo
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more