From 8564b097144ae252bdb03e75e7c10d97bfdf1369 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Thu, 26 Sep 2024 11:18:18 +0530 Subject: [PATCH] feat: Add test case for segfault --- .gitmodules | 2 +- Cargo.toml | 2 +- examples/inspect.rs | 15 ++++++------- flake.lock | 7 ++++--- flake.nix | 2 +- mnn-sys/build.rs | 1 + src/interpreter.rs | 16 +++++++------- src/tensor.rs | 11 +++------- tests/basic.rs | 51 ++++++++++++++++++++++++++++++++++++++------- tests/common.rs | 17 ++++++--------- tests/segfault.rs | 30 ++++++++++++++++++++++++++ 11 files changed, 108 insertions(+), 46 deletions(-) create mode 100644 tests/segfault.rs diff --git a/.gitmodules b/.gitmodules index 22c0691..b52be9d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "mnn-sys/vendor"] path = mnn-sys/vendor - url = git@github.com:alibaba/mnn + url = https://github.com/alibaba/mnn diff --git a/Cargo.toml b/Cargo.toml index ba0cca1..85ec6d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ mnn-threadpool = ["mnn-sys/mnn-threadpool"] tracing = ["dep:tracing"] profile = ["tracing"] -default = ["mnn-threadpool"] +default = ["mnn-threadpool", "opencl"] [dev-dependencies] diff --git a/examples/inspect.rs b/examples/inspect.rs index b447ab9..43384ad 100644 --- a/examples/inspect.rs +++ b/examples/inspect.rs @@ -49,13 +49,14 @@ pub fn main() -> anyhow::Result<()> { println!("{}: {:?}", x.name(), tensor.shape()); tensor.fill(1.0f32); }); - time!(interpreter.run_session_with_callback(&session, |_, name| { - println!("Before Callback: {:?}", name); - true - },|_ , name| { - println!("After Callback: {:?}", name); - true - } , true)?;"run session"); + // time!(interpreter.run_session_with_callback(&session, |_, name| { + // println!("Before Callback: {:?}", name); + // true + // },|_ , name| { + // println!("After Callback: {:?}", name); + // true + // } , true)?;"run session"); + interpreter.run_session(&session)?; let outputs = interpreter.outputs(&session); outputs.iter().for_each(|x| { let tensor = x.tensor::().expect("No tensor"); diff --git a/flake.lock b/flake.lock index 7c040ca..3ccaf4a 100644 --- a/flake.lock +++ b/flake.lock @@ -108,15 +108,16 @@ "mnn-src": { "flake": false, "locked": { - "lastModified": 1725344646, - "narHash": "sha256-cF2CtVMF0va13cgREokyfl5K6e4YKwMITAWn+9EsKOc=", + "lastModified": 1726130630, + "narHash": "sha256-dZNOAKPLjnR8MCkk0iJsOFHII2DMsUcQApfu7OfBRL8=", "owner": "alibaba", "repo": "MNN", - "rev": "a8b7997eb71a341782f4cbe7d1ed9c042517a018", + "rev": "f0e516a4bd26855e21fedb47ed609113d86c5684", "type": "github" }, "original": { "owner": "alibaba", + "ref": "2.9.5", "repo": "MNN", "type": "github" } diff --git a/flake.nix b/flake.nix index f15a525..e51ea24 100644 --- a/flake.nix +++ b/flake.nix @@ -22,7 +22,7 @@ flake = false; }; mnn-src = { - url = "github:alibaba/MNN"; + url = "github:alibaba/MNN/2.9.5"; flake = false; }; }; diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 870df0e..99fda6d 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -43,6 +43,7 @@ fn ensure_vendor_exists(vendor: impl AsRef) -> Result<()> { fn main() -> Result<()> { println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-env-changed=MNN_SRC"); let out_dir = PathBuf::from(std::env::var("OUT_DIR")?); let source = PathBuf::from( std::env::var("MNN_SRC") diff --git a/src/interpreter.rs b/src/interpreter.rs index 95e7055..422088b 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -381,12 +381,12 @@ impl Interpreter { Ok(()) } - /// Wait for all output tensors to be ready after computation - pub fn wait(&self, session: &crate::session::Session) { - self.outputs(session).iter().for_each(|tinfo| { - tinfo.raw_tensor().wait_read(true); - }); - } + // /// Wait for all output tensors to be ready after computation + // pub fn wait(&self, session: &crate::session::Session) { + // self.outputs(session).iter().for_each(|tinfo| { + // tinfo.raw_tensor().wait_read(true); + // }); + // } } #[repr(transparent)] @@ -397,9 +397,11 @@ pub struct TensorInfo<'t, 'tl> { impl core::fmt::Debug for TensorInfo<'_, '_> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + // let tensor = self.raw_tensor(); + // let shape = tensor.shape().clone(); f.debug_struct("TensorInfo") .field("name", &self.name()) - .field("tensor", &self.raw_tensor().shape()) + // .field("tensor", &shape) .finish() } } diff --git a/src/tensor.rs b/src/tensor.rs index cc7a627..986dedf 100644 --- a/src/tensor.rs +++ b/src/tensor.rs @@ -270,6 +270,7 @@ where } pub fn get_dimension_type(&self) -> DimensionType { + debug_assert!(!self.tensor.is_null()); From::from(unsafe { Tensor_getDimensionType(self.tensor) }) } @@ -720,15 +721,9 @@ impl<'r> RawTensor<'r> { self.shape().as_ref().contains(&-1) } - pub fn wait_read(&self, finish: bool) { - unsafe { - Tensor_wait(self.inner, MapType::MAP_TENSOR_READ, finish as i32); - } - } - - pub fn wait_write(&self, finish: bool) { + pub fn wait(&self, map_type: MapType, finish: bool) { unsafe { - Tensor_wait(self.inner, MapType::MAP_TENSOR_WRITE, finish as i32); + Tensor_wait(self.inner, map_type, finish as i32); } } diff --git a/tests/basic.rs b/tests/basic.rs index 89a3a0f..dc79363 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -14,9 +14,46 @@ fn test_basic_metal() { } #[cfg(feature = "opencl")] #[test] -fn test_basic_opencl() { - test_basic(ForwardType::OpenCL).unwrap(); +fn test_basic_opencl() -> Result<(), Box> { + let backend = ForwardType::OpenCL; + let realesr = std::path::Path::new("tests/assets/realesr.mnn"); + use mnn::BackendConfig; + + let mut net = mnn::Interpreter::from_file(realesr)?; + net.set_cache_file(realesr.with_extension("cache"), 128)?; + let mut config = ScheduleConfig::new(); + config.set_type(backend); + let mut session = net.create_session(config)?; + net.update_cache_file(&mut session); + + net.inputs(&session).iter().for_each(|x| { + let mut tensor = x.tensor::().expect("No tensor"); + println!("{}: {:?}", x.name(), tensor.shape()); + tensor.fill(1.0f32); + }); + net.run_session(&session)?; + let outputs = net.outputs(&session); + outputs.iter().for_each(|x| { + let tensor = x.tensor::().expect("No tensor"); + tensor.wait(ffi::MapType::MAP_TENSOR_READ, true); + println!("Waiting for tensor: {}", x.name()); + println!("{}: {:?}", x.name(), tensor.shape()); + // let _ = tensor.create_host_tensor_from_device(true); + }); + + // drop(outputs); + // drop(session); + // drop(net); + Ok(()) } + // net.run_session(&session)?; + // let outputs = net.outputs(&session); + // for output in outputs.iter() { + // println!("output: {:?}", output); + // let tensor = output.tensor::()?; + // let shape = tensor.shape(); + // assert_eq!(shape.as_ref(), [1, 3, 2048, 2048]); + // } #[cfg(feature = "coreml")] #[test] fn test_basic_coreml() { @@ -34,8 +71,8 @@ fn test_multi_path_cpu_cpu() { test_multipath_session(ForwardType::CPU, ForwardType::CPU).unwrap(); } -#[cfg(feature = "opencl")] -#[test] -fn test_multi_path_opencl_cpu() { - test_multipath_session(ForwardType::OpenCL, ForwardType::CPU).unwrap(); -} +// #[cfg(feature = "opencl")] +// #[test] +// fn test_multi_path_opencl_cpu() { +// test_multipath_session(ForwardType::OpenCL, ForwardType::CPU).unwrap(); +// } diff --git a/tests/common.rs b/tests/common.rs index 32ef746..25f5f27 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -28,20 +28,15 @@ impl AsRef<[u8]> for Model { pub fn test_basic(backend: ForwardType) -> Result<()> { use mnn::BackendConfig; - let mut net = mnn::Interpreter::from_bytes(Model::new())?; + let mut net = mnn::Interpreter::from_file("tests/assets/realesr.mnn")?; let mut config = ScheduleConfig::new(); config.set_type(backend); - config.set_backup_type(backend); - let mut bc = BackendConfig::new(); - bc.set_memory_mode(mnn::MemoryMode::High); - bc.set_precision_mode(mnn::PrecisionMode::High); - bc.set_power_mode(mnn::PowerMode::High); - config.set_backend_config(bc); let session = net.create_session(config)?; - for input in &net.inputs(&session) { - println!("input: {:?}", input); - input.tensor::()?.fill(0.0); - } + net.inputs(&session).iter().for_each(|x| { + let mut tensor = x.tensor::().expect("No tensor"); + println!("{}: {:?}", x.name(), tensor.shape()); + tensor.fill(1.0f32); + }); net.run_session(&session)?; let outputs = net.outputs(&session); for output in outputs.iter() { diff --git a/tests/segfault.rs b/tests/segfault.rs new file mode 100644 index 0000000..ea017ca --- /dev/null +++ b/tests/segfault.rs @@ -0,0 +1,30 @@ +use mnn::*; + +/// This segfault on OpenCL backend if we print the tensorinfo +#[cfg(feature = "opencl")] +#[test] +fn test_segfault_case_1_() -> Result<(), Box> { + let backend = ForwardType::OpenCL; + let realesr = std::path::Path::new("tests/assets/realesr.mnn"); + use mnn::BackendConfig; + + let mut net = mnn::Interpreter::from_file(realesr)?; + net.set_cache_file(realesr.with_extension("cache"), 128)?; + let mut config = ScheduleConfig::new(); + config.set_type(backend); + let mut session = net.create_session(config)?; + net.update_cache_file(&mut session); + + net.inputs(&session).iter().for_each(|x| { + let mut tensor = x.tensor::().expect("No tensor"); + // println!("{}: {:?}", x.name(), tensor.shape()); + println!("{:?}", x); + tensor.fill(1.0f32); + }); + net.run_session(&session)?; + let outputs = net.outputs(&session); + drop(outputs); + drop(session); + drop(net); + Ok(()) +}