From 7c7b690bf186f21cc2cc40a9162019e771cb268f Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Fri, 8 Nov 2024 19:04:43 +0530 Subject: [PATCH 01/42] feat: Build mnn with cc crate (opencl only) --- .ignore | 1 - Cargo.lock | 121 ++++++++++++++++++++++- Cargo.toml | 4 + mnn-sys/Cargo.toml | 9 +- mnn-sys/build.rs | 241 +++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 361 insertions(+), 15 deletions(-) delete mode 100644 .ignore diff --git a/.ignore b/.ignore deleted file mode 100644 index d88084b..0000000 --- a/.ignore +++ /dev/null @@ -1 +0,0 @@ -mnn-sys/vendor diff --git a/Cargo.lock b/Cargo.lock index 5e4bbc2..545d0bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" @@ -109,6 +109,16 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bstr" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -123,9 +133,9 @@ checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" [[package]] name = "cc" -version = "1.1.21" +version = "1.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" +checksum = "baee610e9452a8f6f0a1b6194ec09ff9e2d85dea54432acdae41aa0761c95d70" dependencies = [ "jobserver", "libc", @@ -219,6 +229,31 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf0a07a401f374238ab8e2f11a104d2851bf9ce711ec69804834de8af45c7af" +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + [[package]] name = "diffy" version = "0.4.0" @@ -320,12 +355,41 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "globset" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", +] + [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "ignore" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata 0.4.7", + "same-file", + "walkdir", + "winapi-util", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -440,6 +504,7 @@ version = "0.2.0" dependencies = [ "anyhow", "bytemuck", + "cc", "clap", "divan", "dunce", @@ -484,9 +549,11 @@ dependencies = [ "diffy", "dunce", "fs_extra", + "ignore", "itertools", "libc", "tap", + "walkdir", ] [[package]] @@ -733,6 +800,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -745,6 +821,26 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" +[[package]] +name = "serde" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -944,6 +1040,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1021,6 +1127,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index c8128ee..4b0a870 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ error-stack.workspace = true oneshot = "0.1" tracing = { version = "0.1.40", optional = true } dunce = "1.0.5" +cc = "1.1.36" [features] metal = ["mnn-sys/metal"] @@ -52,3 +53,6 @@ harness = false [profile.rwd] debug = true inherits = "release" + +[build-dependencies] +cc = "1.1.36" diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index b0cb6ce..968c3e1 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -15,8 +15,10 @@ cmake = { git = "https://github.com/blonteractor/cmake-rs", features = [ diffy = "0.4.0" dunce = "1.0.4" fs_extra = "1.3.0" +ignore = "0.4.23" itertools = "0.13.0" tap = "1.0.1" +walkdir = "2.5.0" [features] vulkan = [] @@ -26,8 +28,13 @@ opencl = [] openmp = [] opengl = [] mnn-threadpool = [] -default = ["mnn-threadpool"] crt_static = [] +mini-build = [] +sparse-compute = [] +arm82 = [] +bf16 = [] + +default = ["mnn-threadpool", "sparse-compute", "opencl"] [dependencies] libc = "0.2.155" diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 2a5e158..cb8d517 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -8,6 +8,12 @@ use std::{ }; const VENDOR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/vendor"); const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR"); +static TARGET_POINTER_WIDTH: LazyLock = LazyLock::new(|| { + std::env::var("CARGO_CFG_TARGET_POINTER_WIDTH") + .expect("CARGO_CFG_TARGET_POINTER_WIDTH not set") + .parse() + .expect("Failed to parse CARGO_CFG_TARGET_POINTER_WIDTH") +}); static TARGET_OS: LazyLock = LazyLock::new(|| std::env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set")); static TARGET_ARCH: LazyLock = LazyLock::new(|| { @@ -102,7 +108,8 @@ fn main() -> Result<()> { if *MNN_COMPILE { let install_dir = out_dir.join("mnn-install"); - build_cmake(&vendor, &install_dir)?; + // build_cmake(&vendor, &install_dir)?; + cc_build::build(&vendor)?; println!( "cargo:rustc-link-search=native={}", install_dir.join("lib").display() @@ -441,6 +448,19 @@ macro_rules! cxx_option_from_feature { CxxOption::from_bool($cxx, cfg!(feature = $feature)) }}; } + +macro_rules! cxx_option_from_features { + ($name:ident=> $feature: literal, $cxx:literal) => { + pub const $name: CxxOption = cxx_option_from_feature!($feature, $cxx); + }; + ($( $name:ident => $feature:literal, $cxx:literal),*) => { + $(cxx_option_from_features!($name=> $feature, $cxx);)* + + pub fn all() -> Vec { + vec![$(Self::$name),*] + } + }; +} impl CxxOption { const fn from_bool(name: &'static str, value: bool) -> Self { Self { @@ -448,15 +468,20 @@ impl CxxOption { value: CxxOptionValue::from_bool(value), } } - pub const VULKAN: CxxOption = cxx_option_from_feature!("vulkan", "MNN_VULKAN"); - pub const METAL: CxxOption = cxx_option_from_feature!("metal", "MNN_METAL"); - pub const COREML: CxxOption = cxx_option_from_feature!("coreml", "MNN_COREML"); - pub const OPENCL: CxxOption = cxx_option_from_feature!("opencl", "MNN_OPENCL"); - pub const OPENMP: CxxOption = cxx_option_from_feature!("openmp", "MNN_OPENMP"); - pub const OPENGL: CxxOption = cxx_option_from_feature!("opengl", "MNN_OPENGL"); - pub const CRT_STATIC: CxxOption = cxx_option_from_feature!("opengl", "MNN_WIN_RUNTIME_MT"); - pub const THREADPOOL: CxxOption = - cxx_option_from_feature!("mnn-threadpool", "MNN_USE_THREAD_POOL"); + cxx_option_from_features! { + VULKAN => "vulkan", "MNN_VULKAN", + METAL => "metal", "MNN_METAL", + COREML => "coreml", "MNN_COREML", + OPENCL => "opencl", "MNN_OPENCL", + OPENMP => "openmp", "MNN_OPENMP", + OPENGL => "opengl", "MNN_OPENGL", + CRT_STATIC => "crt_static", "MNN_WIN_RUNTIME_MT", + SPARSE_COMPUTE => "sparse-compute", "MNN_USE_SPARSE_COMPUTE", + THREADPOOL => "mnn-threadpool", "MNN_USE_THREAD_POOL", + MINI_BUILD => "mini-build", "MNN_BUILD_MINI", + ARM82 => "arm82", "MNN_ARM82", + BF16 => "bf16", "MNN_SUPPORT_BF16" + } pub fn new(name: &'static str, value: impl Into) -> Self { Self { @@ -488,6 +513,14 @@ impl CxxOption { } } + pub fn cc(&self) -> &str { + match &self.value { + CxxOptionValue::On => "1", + CxxOptionValue::Off => "0", + CxxOptionValue::Value(v) => v, + } + } + pub fn cmake_value(&self) -> &'static str { match &self.value { CxxOptionValue::On => "ON", @@ -512,3 +545,191 @@ impl CxxOption { } } } + +mod cc_build { + use std::ffi::OsStr; + + use super::*; + pub fn build(vendor: impl AsRef) -> Result<()> { + let mut build = cc::Build::new(); + let vendor = vendor.as_ref(); + // Get version + build + .include(vendor.join("include/")) + .include(vendor.join("source/")) + .include(vendor.join("express/")) + .include(vendor.join("tools/")) + .include(vendor.join("codegen/")) + .include(vendor.join("schema/current/")) + .include(vendor.join("3rd_party/")) + .include(vendor.join("3rd_party/flatbuffers/include")) + .include(vendor.join("3rd_party/half")) + .include(vendor.join("3rd_party/imageHelper")) + .include(vendor.join("3rd_party/OpenCLHeaders/")) + // .file("source/core/...") + // .file("source/cv/...") + // .file("source/math/...") + // .file("source/shape/...") + // .file("source/geometry/...") + // .file("source/utils/...") + // .file("source/backend/cpu/...") + // .file("express/...") + .cpp(true) + .std("c++11"); + + if cfg!(target_os = "windows") { + build + .flag_if_supported("/wd4267") + .flag_if_supported("/wd4018") + .flag_if_supported("/wd4251") + .flag_if_supported("/wd4996") + .flag_if_supported("/wd4244") + .flag_if_supported("/wd4146") + .flag_if_supported("/wd4129") + .flag_if_supported("/wd4305") + .flag_if_supported("/wd4275") + .flag_if_supported("/wd4101"); + } + + CxxOption::all().iter().for_each(|opt| { + eprintln!("{}: {}", opt.name, opt.enabled()); + if opt.enabled() { + build.define(opt.name, opt.cc()); + } + }); + + let core_files_dir = vendor.join("source").join("core"); + let core_files = ignore::Walk::new(&core_files_dir) + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + build.files(core_files); + + // #[cfg(feature = "cpu")] + { + let cpu_files_dir = vendor.join("source").join("backend").join("cpu"); + let cpu_files = ignore::WalkBuilder::new(&cpu_files_dir) + .add(&cpu_files_dir.join("compute")) + .max_depth(Some(1)) + .add_custom_ignore_filename("CPUImageProcess.hpp") + .add_custom_ignore_filename("CPUImageProcess.cpp") + .build() + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + build.include(cpu_files_dir.join("schema").join("current")); + if CxxOption::ARM82.enabled() { + if TARGET_ARCH.starts_with("armv7") + || TARGET_ARCH.starts_with("aarch64") + || TARGET_ARCH.starts_with("arm64") + { + build.define("ENABLE_ARMV82", None); + build.include(cpu_files_dir.join("arm")); + } + } + + '_arm: { + let arm_source_dir = cpu_files_dir.join("arm"); + + let mut neon_sources: Vec = + vec![arm_source_dir.join("CommonOptFunctionNeon.cpp")]; + if CxxOption::BF16.enabled() { + let path = arm_source_dir.join("CommonNeonBF16.cpp"); + if path.exists() { + neon_sources.push(path); + } + } + + if *TARGET_POINTER_WIDTH == 64 { + let arm64_sources_dir = arm_source_dir.join("arm64"); + let arm64_sources = ignore::Walk::new(&arm64_sources_dir) + .filter_map(Result::ok) + .filter(|e| { + e.path().extension() == Some(OsStr::new("S")) + || e.path().extension() == Some(OsStr::new("s")) + }) + .map(|e| e.into_path()); + + // MNN_LOW_MEMORY + // MNN_CPU_WEIGHT_DEQUANT_GEMM + + build.define("MNN_USE_NEON", None); + build + .files(arm64_sources.chain(neon_sources)) + .include(cpu_files_dir.join("arm")) + .define("__aarch64__", None); + } else if *TARGET_POINTER_WIDTH == 32 { + let arm32_sources_dir = arm_source_dir.join("arm32"); + let arm32_sources = ignore::Walk::new(&arm32_sources_dir) + .filter_map(Result::ok) + .filter(|e| { + e.path().extension() == Some(OsStr::new("S")) + || e.path().extension() == Some(OsStr::new("s")) + }) + .map(|e| e.into_path()); + + // MNN_LOW_MEMORY + // MNN_CPU_WEIGHT_DEQUANT_GEMM + + build.define("MNN_USE_NEON", None); + build + .files(arm32_sources.chain(neon_sources)) + .include(cpu_files_dir.join("arm")) + .define("__arm__", None); + } + // build.objects( + // cc::Build::new() + // .files(arm64_sources.chain(neon_sources)) + // .include(cpu_files_dir.join("arm")) + // .define("__aarch64__", None) + // .define(CxxOption::BF16.name, CxxOption::BF16.cc()) + // .compile_intermediates(), + // ); + } + + build.files(cpu_files); + } + + { + let cv_files_dir = vendor.join("source").join("cv"); + let cv_files = ignore::Walk::new(&cv_files_dir) + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + // build.include(cv_files_dir.join("schema").join("current")); + if *TARGET_OS == "macos" { + build.flag_if_supported("-fno-stack-check"); + } + build.files(cv_files); + } + + { + let extra_files = ignore::WalkBuilder::new(vendor.join("source").join("math")) + .add(vendor.join("source").join("shape")) + .add(vendor.join("source").join("geometry")) + .add(vendor.join("source").join("utils")) + .build() + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + build.files(extra_files); + } + + #[cfg(feature = "opencl")] + { + let opencl_files_dir = vendor.join("source").join("backend").join("opencl"); + let opencl_files = ignore::Walk::new(&opencl_files_dir) + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + build.include(opencl_files_dir.join("schema").join("current")); + build.files(opencl_files); + } + + build.get_files().for_each(|f| { + rerun_if_changed(f); + }); + build.compile("mnn"); + Ok(()) + } +} From 8717d6257bf874e5aabf604889e5417879c5ea4a Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Fri, 8 Nov 2024 19:13:43 +0530 Subject: [PATCH 02/42] feat: Fix build issue with opencl --- mnn-sys/build.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index cb8d517..d57e977 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -723,7 +723,10 @@ mod cc_build { .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); build.include(opencl_files_dir.join("schema").join("current")); - build.files(opencl_files); + build.define("MNN_OPENCL_ENABLED", "1"); + build.files( + opencl_files.chain([opencl_files_dir.join("execution/cl/opencl_program.cc")]), + ); } build.get_files().for_each(|f| { From 808ad7cb32cbfbed019e477a217123c19a85cfdb Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Fri, 8 Nov 2024 19:24:52 +0530 Subject: [PATCH 03/42] feat: Remove useless items --- mnn-sys/build.rs | 374 ++++++++++++++++++++--------------------------- 1 file changed, 162 insertions(+), 212 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index d57e977..ac4e5b7 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -3,6 +3,7 @@ use anyhow::*; #[cfg(unix)] use std::os::unix::fs::PermissionsExt; use std::{ + ffi::OsStr, path::{Path, PathBuf}, sync::LazyLock, }; @@ -108,8 +109,7 @@ fn main() -> Result<()> { if *MNN_COMPILE { let install_dir = out_dir.join("mnn-install"); - // build_cmake(&vendor, &install_dir)?; - cc_build::build(&vendor)?; + build_cpp_build(&vendor)?; println!( "cargo:rustc-link-search=native={}", install_dir.join("lib").display() @@ -308,48 +308,6 @@ pub fn mnn_c_build(path: impl AsRef, vendor: impl AsRef) -> Result<( Ok(()) } -pub fn build_cmake(path: impl AsRef, install: impl AsRef) -> Result<()> { - let threads = std::thread::available_parallelism()?; - cmake::Config::new(path) - .define("CMAKE_CXX_STANDARD", "14") - .parallel(threads.get() as u8) - .define("MNN_BUILD_SHARED_LIBS", "OFF") - .define("MNN_SEP_BUILD", "OFF") - .define("MNN_PORTABLE_BUILD", "ON") - .define("MNN_USE_SYSTEM_LIB", "OFF") - .define("MNN_BUILD_CONVERTER", "OFF") - .define("MNN_BUILD_TOOLS", "OFF") - .define("CMAKE_INSTALL_PREFIX", install.as_ref()) - // https://github.com/rust-lang/rust/issues/39016 - // https://github.com/rust-lang/cc-rs/pull/717 - // .define("CMAKE_BUILD_TYPE", "Release") - .pipe(|config| { - config.define("MNN_WIN_RUNTIME_MT", CxxOption::CRT_STATIC.cmake_value()); - config.define("MNN_USE_THREAD_POOL", CxxOption::THREADPOOL.cmake_value()); - config.define("MNN_OPENMP", CxxOption::OPENMP.cmake_value()); - config.define("MNN_VULKAN", CxxOption::VULKAN.cmake_value()); - config.define("MNN_METAL", CxxOption::METAL.cmake_value()); - config.define("MNN_COREML", CxxOption::COREML.cmake_value()); - config.define("MNN_OPENCL", CxxOption::OPENCL.cmake_value()); - config.define("MNN_OPENGL", CxxOption::OPENGL.cmake_value()); - config.define("CMAKE_CXX_FLAGS", "-O0"); - // #[cfg(windows)] - if *TARGET_OS == "windows" { - config.define("CMAKE_CXX_FLAGS", "-DWIN32=1"); - } - - if is_emscripten() { - config - .define("CMAKE_C_COMPILER", "emcc") - .define("CMAKE_CXX_COMPILER", "em++") - .target("wasm32-unknown-emscripten"); - } - config - }) - .build(); - Ok(()) -} - pub fn try_patch_file(patch: impl AsRef, file: impl AsRef) -> Result<()> { let patch = dunce::canonicalize(patch)?; rerun_if_changed(&patch); @@ -546,193 +504,185 @@ impl CxxOption { } } -mod cc_build { - use std::ffi::OsStr; +pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { + let mut build = cc::Build::new(); + let vendor = vendor.as_ref(); + // Get version + build + .include(vendor.join("include/")) + .include(vendor.join("source/")) + .include(vendor.join("express/")) + .include(vendor.join("tools/")) + .include(vendor.join("codegen/")) + .include(vendor.join("schema/current/")) + .include(vendor.join("3rd_party/")) + .include(vendor.join("3rd_party/flatbuffers/include")) + .include(vendor.join("3rd_party/half")) + .include(vendor.join("3rd_party/imageHelper")) + .include(vendor.join("3rd_party/OpenCLHeaders/")) + // .file("source/core/...") + // .file("source/cv/...") + // .file("source/math/...") + // .file("source/shape/...") + // .file("source/geometry/...") + // .file("source/utils/...") + // .file("source/backend/cpu/...") + // .file("express/...") + .cpp(true) + .std("c++11"); - use super::*; - pub fn build(vendor: impl AsRef) -> Result<()> { - let mut build = cc::Build::new(); - let vendor = vendor.as_ref(); - // Get version + if cfg!(target_os = "windows") { build - .include(vendor.join("include/")) - .include(vendor.join("source/")) - .include(vendor.join("express/")) - .include(vendor.join("tools/")) - .include(vendor.join("codegen/")) - .include(vendor.join("schema/current/")) - .include(vendor.join("3rd_party/")) - .include(vendor.join("3rd_party/flatbuffers/include")) - .include(vendor.join("3rd_party/half")) - .include(vendor.join("3rd_party/imageHelper")) - .include(vendor.join("3rd_party/OpenCLHeaders/")) - // .file("source/core/...") - // .file("source/cv/...") - // .file("source/math/...") - // .file("source/shape/...") - // .file("source/geometry/...") - // .file("source/utils/...") - // .file("source/backend/cpu/...") - // .file("express/...") - .cpp(true) - .std("c++11"); - - if cfg!(target_os = "windows") { - build - .flag_if_supported("/wd4267") - .flag_if_supported("/wd4018") - .flag_if_supported("/wd4251") - .flag_if_supported("/wd4996") - .flag_if_supported("/wd4244") - .flag_if_supported("/wd4146") - .flag_if_supported("/wd4129") - .flag_if_supported("/wd4305") - .flag_if_supported("/wd4275") - .flag_if_supported("/wd4101"); + .flag_if_supported("/wd4267") + .flag_if_supported("/wd4018") + .flag_if_supported("/wd4251") + .flag_if_supported("/wd4996") + .flag_if_supported("/wd4244") + .flag_if_supported("/wd4146") + .flag_if_supported("/wd4129") + .flag_if_supported("/wd4305") + .flag_if_supported("/wd4275") + .flag_if_supported("/wd4101"); + } + + CxxOption::all().iter().for_each(|opt| { + eprintln!("{}: {}", opt.name, opt.enabled()); + if opt.enabled() { + build.define(opt.name, opt.cc()); } + }); - CxxOption::all().iter().for_each(|opt| { - eprintln!("{}: {}", opt.name, opt.enabled()); - if opt.enabled() { - build.define(opt.name, opt.cc()); - } - }); + let core_files_dir = vendor.join("source").join("core"); + let core_files = ignore::Walk::new(&core_files_dir) + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + build.files(core_files); - let core_files_dir = vendor.join("source").join("core"); - let core_files = ignore::Walk::new(&core_files_dir) + // #[cfg(feature = "cpu")] + { + let cpu_files_dir = vendor.join("source").join("backend").join("cpu"); + let cpu_files = ignore::WalkBuilder::new(&cpu_files_dir) + .add(&cpu_files_dir.join("compute")) + .max_depth(Some(1)) + .add_custom_ignore_filename("CPUImageProcess.hpp") + .add_custom_ignore_filename("CPUImageProcess.cpp") + .build() .filter_map(Result::ok) .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); - build.files(core_files); - - // #[cfg(feature = "cpu")] - { - let cpu_files_dir = vendor.join("source").join("backend").join("cpu"); - let cpu_files = ignore::WalkBuilder::new(&cpu_files_dir) - .add(&cpu_files_dir.join("compute")) - .max_depth(Some(1)) - .add_custom_ignore_filename("CPUImageProcess.hpp") - .add_custom_ignore_filename("CPUImageProcess.cpp") - .build() - .filter_map(Result::ok) - .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) - .map(|e| e.into_path()); - build.include(cpu_files_dir.join("schema").join("current")); - if CxxOption::ARM82.enabled() { - if TARGET_ARCH.starts_with("armv7") - || TARGET_ARCH.starts_with("aarch64") - || TARGET_ARCH.starts_with("arm64") - { - build.define("ENABLE_ARMV82", None); - build.include(cpu_files_dir.join("arm")); - } + if CxxOption::ARM82.enabled() { + if TARGET_ARCH.starts_with("armv7") + || TARGET_ARCH.starts_with("aarch64") + || TARGET_ARCH.starts_with("arm64") + { + build.define("ENABLE_ARMV82", None); + build.include(cpu_files_dir.join("arm")); } + } - '_arm: { - let arm_source_dir = cpu_files_dir.join("arm"); - - let mut neon_sources: Vec = - vec![arm_source_dir.join("CommonOptFunctionNeon.cpp")]; - if CxxOption::BF16.enabled() { - let path = arm_source_dir.join("CommonNeonBF16.cpp"); - if path.exists() { - neon_sources.push(path); - } - } + '_arm: { + let arm_source_dir = cpu_files_dir.join("arm"); - if *TARGET_POINTER_WIDTH == 64 { - let arm64_sources_dir = arm_source_dir.join("arm64"); - let arm64_sources = ignore::Walk::new(&arm64_sources_dir) - .filter_map(Result::ok) - .filter(|e| { - e.path().extension() == Some(OsStr::new("S")) - || e.path().extension() == Some(OsStr::new("s")) - }) - .map(|e| e.into_path()); - - // MNN_LOW_MEMORY - // MNN_CPU_WEIGHT_DEQUANT_GEMM - - build.define("MNN_USE_NEON", None); - build - .files(arm64_sources.chain(neon_sources)) - .include(cpu_files_dir.join("arm")) - .define("__aarch64__", None); - } else if *TARGET_POINTER_WIDTH == 32 { - let arm32_sources_dir = arm_source_dir.join("arm32"); - let arm32_sources = ignore::Walk::new(&arm32_sources_dir) - .filter_map(Result::ok) - .filter(|e| { - e.path().extension() == Some(OsStr::new("S")) - || e.path().extension() == Some(OsStr::new("s")) - }) - .map(|e| e.into_path()); - - // MNN_LOW_MEMORY - // MNN_CPU_WEIGHT_DEQUANT_GEMM - - build.define("MNN_USE_NEON", None); - build - .files(arm32_sources.chain(neon_sources)) - .include(cpu_files_dir.join("arm")) - .define("__arm__", None); + let mut neon_sources: Vec = + vec![arm_source_dir.join("CommonOptFunctionNeon.cpp")]; + if CxxOption::BF16.enabled() { + let path = arm_source_dir.join("CommonNeonBF16.cpp"); + if path.exists() { + neon_sources.push(path); } - // build.objects( - // cc::Build::new() - // .files(arm64_sources.chain(neon_sources)) - // .include(cpu_files_dir.join("arm")) - // .define("__aarch64__", None) - // .define(CxxOption::BF16.name, CxxOption::BF16.cc()) - // .compile_intermediates(), - // ); } - build.files(cpu_files); - } - - { - let cv_files_dir = vendor.join("source").join("cv"); - let cv_files = ignore::Walk::new(&cv_files_dir) - .filter_map(Result::ok) - .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) - .map(|e| e.into_path()); - // build.include(cv_files_dir.join("schema").join("current")); - if *TARGET_OS == "macos" { - build.flag_if_supported("-fno-stack-check"); + if *TARGET_POINTER_WIDTH == 64 { + let arm64_sources_dir = arm_source_dir.join("arm64"); + let arm64_sources = ignore::Walk::new(&arm64_sources_dir) + .filter_map(Result::ok) + .filter(|e| { + e.path().extension() == Some(OsStr::new("S")) + || e.path().extension() == Some(OsStr::new("s")) + }) + .map(|e| e.into_path()); + + // MNN_LOW_MEMORY + // MNN_CPU_WEIGHT_DEQUANT_GEMM + + build.define("MNN_USE_NEON", None); + build + .files(arm64_sources.chain(neon_sources)) + .include(cpu_files_dir.join("arm")) + .define("__aarch64__", None); + } else if *TARGET_POINTER_WIDTH == 32 { + let arm32_sources_dir = arm_source_dir.join("arm32"); + let arm32_sources = ignore::Walk::new(&arm32_sources_dir) + .filter_map(Result::ok) + .filter(|e| { + e.path().extension() == Some(OsStr::new("S")) + || e.path().extension() == Some(OsStr::new("s")) + }) + .map(|e| e.into_path()); + + // MNN_LOW_MEMORY + // MNN_CPU_WEIGHT_DEQUANT_GEMM + + build.define("MNN_USE_NEON", None); + build + .files(arm32_sources.chain(neon_sources)) + .include(cpu_files_dir.join("arm")) + .define("__arm__", None); } - build.files(cv_files); + // build.objects( + // cc::Build::new() + // .files(arm64_sources.chain(neon_sources)) + // .include(cpu_files_dir.join("arm")) + // .define("__aarch64__", None) + // .define(CxxOption::BF16.name, CxxOption::BF16.cc()) + // .compile_intermediates(), + // ); } - { - let extra_files = ignore::WalkBuilder::new(vendor.join("source").join("math")) - .add(vendor.join("source").join("shape")) - .add(vendor.join("source").join("geometry")) - .add(vendor.join("source").join("utils")) - .build() - .filter_map(Result::ok) - .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) - .map(|e| e.into_path()); - build.files(extra_files); - } + build.files(cpu_files); + } - #[cfg(feature = "opencl")] - { - let opencl_files_dir = vendor.join("source").join("backend").join("opencl"); - let opencl_files = ignore::Walk::new(&opencl_files_dir) - .filter_map(Result::ok) - .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) - .map(|e| e.into_path()); - build.include(opencl_files_dir.join("schema").join("current")); - build.define("MNN_OPENCL_ENABLED", "1"); - build.files( - opencl_files.chain([opencl_files_dir.join("execution/cl/opencl_program.cc")]), - ); + { + let cv_files_dir = vendor.join("source").join("cv"); + let cv_files = ignore::Walk::new(&cv_files_dir) + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + // build.include(cv_files_dir.join("schema").join("current")); + if *TARGET_OS == "macos" { + build.flag_if_supported("-fno-stack-check"); } + build.files(cv_files); + } + + { + let extra_files = ignore::WalkBuilder::new(vendor.join("source").join("math")) + .add(vendor.join("source").join("shape")) + .add(vendor.join("source").join("geometry")) + .add(vendor.join("source").join("utils")) + .build() + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + build.files(extra_files); + } - build.get_files().for_each(|f| { - rerun_if_changed(f); - }); - build.compile("mnn"); - Ok(()) + #[cfg(feature = "opencl")] + { + let opencl_files_dir = vendor.join("source").join("backend").join("opencl"); + let opencl_files = ignore::Walk::new(&opencl_files_dir) + .filter_map(Result::ok) + .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .map(|e| e.into_path()); + build.include(opencl_files_dir.join("schema").join("current")); + build.define("MNN_OPENCL_ENABLED", "1"); + build.files(opencl_files.chain([opencl_files_dir.join("execution/cl/opencl_program.cc")])); } + + build.get_files().for_each(|f| { + rerun_if_changed(f); + }); + build.compile("mnn"); + Ok(()) } From 187731c5d11f1467ca437fa39f628339db6b1b28 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Fri, 8 Nov 2024 19:53:31 +0530 Subject: [PATCH 04/42] feat: Fixed mnn-audit --- Cargo.lock | 9 --------- flake.lock | 12 ++++++------ flake.nix | 21 ++++++++++++++++++--- mnn-sys/Cargo.toml | 5 +---- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 545d0bf..747e099 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,14 +209,6 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" -[[package]] -name = "cmake" -version = "0.1.50" -source = "git+https://github.com/blonteractor/cmake-rs#b96aab10486f2c1540422dd4e6f38e983745fd5a" -dependencies = [ - "cc", -] - [[package]] name = "colorchoice" version = "1.0.2" @@ -545,7 +537,6 @@ dependencies = [ "anyhow", "bindgen", "cc", - "cmake", "diffy", "dunce", "fs_extra", diff --git a/flake.lock b/flake.lock index 229a00a..1e28b54 100644 --- a/flake.lock +++ b/flake.lock @@ -145,11 +145,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1730531603, - "narHash": "sha256-Dqg6si5CqIzm87sp57j5nTaeBbWhHFaVyG7V6L8k3lY=", + "lastModified": 1730785428, + "narHash": "sha256-Zwl8YgTVJTEum+L+0zVAWvXAGbWAuXHax3KzuejaDyo=", "owner": "nixos", "repo": "nixpkgs", - "rev": "7ffd9ae656aec493492b44d0ddfb28e79a1ea25d", + "rev": "4aa36568d413aca0ea84a1684d2d46f55dbabad7", "type": "github" }, "original": { @@ -178,11 +178,11 @@ ] }, "locked": { - "lastModified": 1730773675, - "narHash": "sha256-pULo7GryzLkqGveWvnNWVz1Kk6EJqvq+HQeSkwvr7DA=", + "lastModified": 1731032894, + "narHash": "sha256-dQSyYPmrQiPr+PGEd+K8038rubFGz7G/dNXVeaGWE0w=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "e19e9d54fac1e53f73411ebe22d19f946b1ba0bd", + "rev": "d52f2a4c103a0acf09ded857b9e2519ae2360e59", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index fcc3871..8ccfe31 100644 --- a/flake.nix +++ b/flake.nix @@ -54,6 +54,17 @@ # enableMetal = true; enableOpencl = true; }; + cargo-audit = pkgs.rustPlatform.buildRustPackage rec { + version = "0.21.0"; + pname = "cargo-audit"; + src = pkgs.fetchCrate { + inherit pname version; + sha256 = "sha256-oMXpJE49If4QKE80ZKhRpMRPh3Bl517a2Ez/1VcaQJQ="; + }; + cargoLock = rec { + lockFile = "${src}/Cargo.lock"; + }; + }; }) ]; }; @@ -113,9 +124,12 @@ }); mnn-fmt = craneLib.cargoFmt {inherit src;}; # Audit dependencies - mnn-audit = craneLib.cargoAudit { - inherit src advisory-db; - }; + mnn-audit = + craneLib.cargoAudit.override { + cargo-audit = pkgs.cargo-audit; + } { + inherit src advisory-db; + }; # Audit licenses mnn-deny = craneLib.cargoDeny { @@ -197,6 +211,7 @@ cargo-nextest cargo-hakari cargo-deny + # cargo-audit cargo-semver-checks rust-bindgen llvm diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index 968c3e1..8cfd4ed 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -8,10 +8,7 @@ license = { workspace = true } [build-dependencies] anyhow = "1.0.86" bindgen = { version = "0.70", features = ["experimental"] } -cc = { version = "1.1.5", features = [] } -cmake = { git = "https://github.com/blonteractor/cmake-rs", features = [ - "parallel", -] } +cc = { version = "1.1.5", features = ["parallel"] } diffy = "0.4.0" dunce = "1.0.4" fs_extra = "1.3.0" From 405ffed549d4e9d7c119f692e0fac165e5bad85f Mon Sep 17 00:00:00 2001 From: Uttarayan Mondal Date: Sun, 10 Nov 2024 18:43:59 +0530 Subject: [PATCH 05/42] feat: Only compile arm files when using arm --- mnn-sys/build.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index ac4e5b7..df60bb9 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -581,7 +581,8 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { } } - '_arm: { + + if TARGET_OS.starts_with("aarch64") || TARGET_OS.starts_with("arm") { let arm_source_dir = cpu_files_dir.join("arm"); let mut neon_sources: Vec = From b9056e373ff94b4d7df462aa1ef06b66b6639e57 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 11 Nov 2024 15:24:29 +0530 Subject: [PATCH 06/42] feat: Added compilation support for sse and avx --- Cargo.toml | 1 - mnn-sync/src/lib.rs | 1 - mnn-sys/build.rs | 318 +++++++++++++++++++++++++++++++++----------- 3 files changed, 243 insertions(+), 77 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4b0a870..35ceae1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ error-stack.workspace = true oneshot = "0.1" tracing = { version = "0.1.40", optional = true } dunce = "1.0.5" -cc = "1.1.36" [features] metal = ["mnn-sys/metal"] diff --git a/mnn-sync/src/lib.rs b/mnn-sync/src/lib.rs index 2ee066b..65b0ff9 100644 --- a/mnn-sync/src/lib.rs +++ b/mnn-sync/src/lib.rs @@ -481,7 +481,6 @@ pub fn test_sync_api() { } #[test] -#[ignore = "This test is not reliable on CI"] pub fn test_sync_api_race() { let interpreter = Interpreter::from_file("../tests/assets/realesr.mnn") .expect("Failed to create interpreter"); diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index df60bb9..fd22505 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -15,6 +15,15 @@ static TARGET_POINTER_WIDTH: LazyLock = LazyLock::new(|| { .parse() .expect("Failed to parse CARGO_CFG_TARGET_POINTER_WIDTH") }); + +static TARGET_FEATURES: LazyLock> = LazyLock::new(|| { + std::env::var("CARGO_CFG_TARGET_FEATURE") + .expect("CARGO_CFG_TARGET_FEATURE not set") + .split(',') + .map(|s| s.to_string()) + .collect() +}); + static TARGET_OS: LazyLock = LazyLock::new(|| std::env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set")); static TARGET_ARCH: LazyLock = LazyLock::new(|| { @@ -438,7 +447,17 @@ impl CxxOption { THREADPOOL => "mnn-threadpool", "MNN_USE_THREAD_POOL", MINI_BUILD => "mini-build", "MNN_BUILD_MINI", ARM82 => "arm82", "MNN_ARM82", - BF16 => "bf16", "MNN_SUPPORT_BF16" + BF16 => "bf16", "MNN_SUPPORT_BF16", + AVX512 => "avx512", "MNN_AVX512", + LOW_MEMORY => "low-memory", "MNN_LOW_MEMORY", + NEON => "neon", "MNN_USE_NEON", + CPU_WEIGHT_DEQUANT_GEMM => "cpu-weight-dequant-gemm", "MNN_CPU_WEIGHT_DEQUANT_GEMM" + } + + pub fn define(&self, build: &mut cc::Build) { + if self.enabled() { + build.define(self.name, self.cc()); + } } pub fn new(name: &'static str, value: impl Into) -> Self { @@ -504,11 +523,32 @@ impl CxxOption { } } +fn is_arm() -> bool { + TARGET_ARCH.starts_with("armv7") + || TARGET_ARCH.starts_with("aarch64") + || TARGET_ARCH.starts_with("arm64") +} + +fn is_x86() -> bool { + TARGET_ARCH.starts_with("x86") + // || TARGET_ARCH.starts_with("i686") + // || TARGET_ARCH.starts_with("i386") +} + +fn read_dir(input: impl AsRef) -> impl Iterator { + ignore::WalkBuilder::new(input) + .max_depth(Some(1)) + .build() + .filter_map(Result::ok) + .map(|e| e.into_path()) +} + pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { let mut build = cc::Build::new(); let vendor = vendor.as_ref(); // Get version build + // .try_flags_from_environment(concat!(env!("CARGO_PKG_NAME"), "_CFLAGS"))? .include(vendor.join("include/")) .include(vendor.join("source/")) .include(vendor.join("express/")) @@ -520,14 +560,6 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { .include(vendor.join("3rd_party/half")) .include(vendor.join("3rd_party/imageHelper")) .include(vendor.join("3rd_party/OpenCLHeaders/")) - // .file("source/core/...") - // .file("source/cv/...") - // .file("source/math/...") - // .file("source/shape/...") - // .file("source/geometry/...") - // .file("source/utils/...") - // .file("source/backend/cpu/...") - // .file("express/...") .cpp(true) .std("c++11"); @@ -563,7 +595,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { { let cpu_files_dir = vendor.join("source").join("backend").join("cpu"); let cpu_files = ignore::WalkBuilder::new(&cpu_files_dir) - .add(&cpu_files_dir.join("compute")) + .add(cpu_files_dir.join("compute")) .max_depth(Some(1)) .add_custom_ignore_filename("CPUImageProcess.hpp") .add_custom_ignore_filename("CPUImageProcess.cpp") @@ -571,74 +603,18 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { .filter_map(Result::ok) .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); - if CxxOption::ARM82.enabled() { - if TARGET_ARCH.starts_with("armv7") - || TARGET_ARCH.starts_with("aarch64") - || TARGET_ARCH.starts_with("arm64") - { - build.define("ENABLE_ARMV82", None); - build.include(cpu_files_dir.join("arm")); - } - } - - - if TARGET_OS.starts_with("aarch64") || TARGET_OS.starts_with("arm") { - let arm_source_dir = cpu_files_dir.join("arm"); - let mut neon_sources: Vec = - vec![arm_source_dir.join("CommonOptFunctionNeon.cpp")]; - if CxxOption::BF16.enabled() { - let path = arm_source_dir.join("CommonNeonBF16.cpp"); - if path.exists() { - neon_sources.push(path); - } - } - - if *TARGET_POINTER_WIDTH == 64 { - let arm64_sources_dir = arm_source_dir.join("arm64"); - let arm64_sources = ignore::Walk::new(&arm64_sources_dir) - .filter_map(Result::ok) - .filter(|e| { - e.path().extension() == Some(OsStr::new("S")) - || e.path().extension() == Some(OsStr::new("s")) - }) - .map(|e| e.into_path()); + if CxxOption::ARM82.enabled() && is_arm() { + build.define("ENABLE_ARMV82", None); + build.include(cpu_files_dir.join("arm")); + } - // MNN_LOW_MEMORY - // MNN_CPU_WEIGHT_DEQUANT_GEMM + if is_arm() { + arm(&mut build, cpu_files_dir.join("arm"))?; + } - build.define("MNN_USE_NEON", None); - build - .files(arm64_sources.chain(neon_sources)) - .include(cpu_files_dir.join("arm")) - .define("__aarch64__", None); - } else if *TARGET_POINTER_WIDTH == 32 { - let arm32_sources_dir = arm_source_dir.join("arm32"); - let arm32_sources = ignore::Walk::new(&arm32_sources_dir) - .filter_map(Result::ok) - .filter(|e| { - e.path().extension() == Some(OsStr::new("S")) - || e.path().extension() == Some(OsStr::new("s")) - }) - .map(|e| e.into_path()); - - // MNN_LOW_MEMORY - // MNN_CPU_WEIGHT_DEQUANT_GEMM - - build.define("MNN_USE_NEON", None); - build - .files(arm32_sources.chain(neon_sources)) - .include(cpu_files_dir.join("arm")) - .define("__arm__", None); - } - // build.objects( - // cc::Build::new() - // .files(arm64_sources.chain(neon_sources)) - // .include(cpu_files_dir.join("arm")) - // .define("__aarch64__", None) - // .define(CxxOption::BF16.name, CxxOption::BF16.cc()) - // .compile_intermediates(), - // ); + if TARGET_FEATURES.contains(&("sse".into())) && is_x86() { + x86_64(&mut build, cpu_files_dir.join("x86_64"))?; } build.files(cpu_files); @@ -687,3 +663,195 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { build.compile("mnn"); Ok(()) } + +fn arm(build: &mut cc::Build, arm_dir: impl AsRef) -> Result<&mut cc::Build> { + let arm_source_dir = arm_dir.as_ref(); + + let mut neon_sources: Vec = vec![arm_source_dir.join("CommonOptFunctionNeon.cpp")]; + if CxxOption::BF16.enabled() { + let path = arm_source_dir.join("CommonNeonBF16.cpp"); + if path.exists() { + neon_sources.push(path); + } + } + + if *TARGET_POINTER_WIDTH == 64 { + let arm64_sources_dir = arm_source_dir.join("arm64"); + let arm64_sources = ignore::Walk::new(&arm64_sources_dir) + .filter_map(Result::ok) + .filter(|e| { + e.path().extension() == Some(OsStr::new("S")) + || e.path().extension() == Some(OsStr::new("s")) + }) + .map(|e| e.into_path()); + + // MNN_LOW_MEMORY + // MNN_CPU_WEIGHT_DEQUANT_GEMM + + build.define("MNN_USE_NEON", None); + build + .files(arm64_sources.chain(neon_sources)) + .include(arm_source_dir) + .define("__aarch64__", None); + } else if *TARGET_POINTER_WIDTH == 32 { + let arm32_sources_dir = arm_source_dir.join("arm32"); + let arm32_sources = ignore::Walk::new(&arm32_sources_dir) + .filter_map(Result::ok) + .filter(|e| { + e.path().extension() == Some(OsStr::new("S")) + || e.path().extension() == Some(OsStr::new("s")) + }) + .map(|e| e.into_path()); + + // MNN_LOW_MEMORY + // MNN_CPU_WEIGHT_DEQUANT_GEMM + + build.define("MNN_USE_NEON", None); + build + .files(arm32_sources.chain(neon_sources)) + .include(arm_source_dir) + .define("__arm__", None); + } + Ok(build) +} + +fn x86_64(build: &mut cc::Build, x86_64_dir: impl AsRef) -> Result<&mut cc::Build> { + let mnn_assembler = std::env::var("MNN_ASSEMBLER").ok(); + let like_msvc = build.get_compiler().is_like_msvc(); + let win_use_asm = like_msvc && *TARGET_POINTER_WIDTH == 64 && mnn_assembler.is_some(); + let has_avx512 = target_has_avx512(); + build.define("MNN_USE_SSE", None); + let x86_src_dir = x86_64_dir.as_ref(); + let mnn_x8664_src = read_dir(&x86_src_dir); + let mnn_avx_src = read_dir(x86_src_dir.join("avx")); + let mnn_avxfma_src = read_dir(x86_src_dir.join("avxfma")); + let mnn_sse_src = read_dir(x86_src_dir.join("sse")); + let mnn_avx512_vnni_src = x86_src_dir.join("avx512/GemmInt8_VNNI.cpp"); + let mnn_avx512_src = read_dir(x86_src_dir.join("avx512")).filter(|p| p != &mnn_avx512_vnni_src); + + if has_avx512 && CxxOption::AVX512.enabled() && (!like_msvc || win_use_asm) { + let mnn_avx512 = cc::Build::new() + .files(mnn_avx512_src) + .define("MNN_USE_SSE", None) + .define("MNN_X86_USE_ASM", None) + .tap_mut(|build| { + if build.get_compiler().is_like_msvc() { + build.flag_if_supported("/arch:AVX512"); + } else { + // target_compile_options(MNNAVX512 PRIVATE -m64 -mavx512f -mavx512dq -mavx512vl -mavx512bw -mfma) + build + .flag_if_supported("-m64") + .flag_if_supported("-mavx512f") + .flag_if_supported("-mavx512dq") + .flag_if_supported("-mavx512vl") + .flag_if_supported("-mavx512bw") + .flag_if_supported("-mfma"); + } + }) + .compile_intermediates(); + build.objects(mnn_avx512); + let mnn_avx512_vnni = true; + if mnn_avx512_vnni { + let mnn_avx512_vnni = cc::Build::new() + .file(mnn_avx512_vnni_src) + .define("MNN_USE_SSE", None) + .define("MNN_X86_USE_ASM", None) + .tap_mut(|build| { + if build.get_compiler().is_like_msvc() { + build.flag_if_supported("/arch:AVX512"); + } else { + // target_compile_options(MNNAVX512_VNNI PRIVATE -m64 -mavx512f -mavx512dq -mavx512vl -mavx512bw -mfma -mavx512vnni) + build + .flag_if_supported("-m64") + .flag_if_supported("-mavx512f") + .flag_if_supported("-mavx512dq") + .flag_if_supported("-mavx512vl") + .flag_if_supported("-mavx512bw") + .flag_if_supported("-mfma") + .flag_if_supported("-mavx512vnni"); + } + }) + .compile_intermediates(); + build.objects(mnn_avx512_vnni); + } + } + + let mnn_sse = cc::Build::new() + .files(mnn_sse_src) + .flag("MNN_USE_SSE") + .tap_mut(|build| { + if !like_msvc { + build.flag_if_supported("-msse4.1"); + } + CxxOption::LOW_MEMORY.define(build); + }) + .compile_intermediates(); + + let mnn_avx = cc::Build::new() + .files(mnn_avx_src) + .flag("MNN_USE_SSE") + .tap_mut(|build| { + if like_msvc { + build.flag_if_supported("/arch:AVX"); + } else { + build + .flag_if_supported("-mavx2") + .define("MNN_X86_USE_ASM", None); + } + CxxOption::LOW_MEMORY.define(build); + }) + .compile_intermediates(); + + let mnn_avxfma = cc::Build::new() + .files(mnn_avxfma_src) + .flag("MNN_USE_SSE") + .tap_mut(|build| { + if like_msvc { + build.flag_if_supported("/arch:AVX2"); + } else { + build + .flag_if_supported("-mavx2") + .flag_if_supported("-mfma") + .define("MNN_X86_USE_ASM", None); + } + CxxOption::LOW_MEMORY.define(build); + CxxOption::BF16.define(build) + }) + .compile_intermediates(); + + let mnn_x8664 = cc::Build::new() + .files(mnn_x8664_src) + .flag("MNN_USE_SSE") + .tap_mut(|build| { + CxxOption::LOW_MEMORY.define(build); + CxxOption::CPU_WEIGHT_DEQUANT_GEMM.define(build); + if has_avx512 && CxxOption::AVX512.enabled() && (!like_msvc || win_use_asm) { + CxxOption::AVX512.define(build); + } + }) + .compile_intermediates(); + + build.objects(mnn_sse); + build.objects(mnn_x8664); + build.objects(mnn_avx); + build.objects(mnn_avxfma); + + has_avx512.then(|| { + CxxOption::AVX512.define(build); + }); + + Ok(build) +} + +fn target_has_avx512() -> bool { + const AVX_PRG: &str = r#" +#ifndef __AVX512F__ +#error "AVX-512 support is required to compile this program." +#endif +int main() {return 0;} "#; + std::fs::write("test.c", AVX_PRG).ok(); + cc::Build::new() + .file("test.c") + .try_compile("avx512") + .is_ok() +} From 58c9ba7c27a1df045ff8839a8cf7e707b07a334c Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 11 Nov 2024 15:25:50 +0530 Subject: [PATCH 07/42] feat: Added some deps --- flake.nix | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index 8ccfe31..961e66b 100644 --- a/flake.nix +++ b/flake.nix @@ -205,9 +205,10 @@ default = pkgs.mkShell { packages = with pkgs; [ - mnn + nushell + git + git-lfs nightlyToolchain - zstd cargo-nextest cargo-hakari cargo-deny @@ -223,8 +224,6 @@ darwin.apple_sdk.frameworks.Metal darwin.apple_sdk.frameworks.CoreML ])); - # RUSTFLAGS = "-Zsanitizer=address"; - # ASAN_OPTIONS = "detect_leaks=1"; }; }; } From ac2fa1d65ca1f01724c527d2e81d6950b6a09f17 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 11 Nov 2024 15:50:02 +0530 Subject: [PATCH 08/42] feat: Add common args to devShell --- flake.nix | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/flake.nix b/flake.nix index 961e66b..73dda9a 100644 --- a/flake.nix +++ b/flake.nix @@ -202,29 +202,30 @@ }; devShells = { - default = pkgs.mkShell { - packages = with pkgs; - [ - nushell - git - git-lfs - nightlyToolchain - cargo-nextest - cargo-hakari - cargo-deny - # cargo-audit - cargo-semver-checks - rust-bindgen - llvm - ] - ++ (lib.optionals pkgs.stdenv.isDarwin [ - darwin.apple_sdk.frameworks.OpenCL + default = pkgs.mkShell (commonArgs + // { + packages = with pkgs; + [ + nushell + git + git-lfs + nightlyToolchain + cargo-nextest + cargo-hakari + cargo-deny + # cargo-audit + cargo-semver-checks + rust-bindgen + llvm ] - ++ (lib.optionals pkgs.stdenv.isAarch64 [ - darwin.apple_sdk.frameworks.Metal - darwin.apple_sdk.frameworks.CoreML - ])); - }; + ++ (lib.optionals pkgs.stdenv.isDarwin [ + darwin.apple_sdk.frameworks.OpenCL + ] + ++ (lib.optionals pkgs.stdenv.isAarch64 [ + darwin.apple_sdk.frameworks.Metal + darwin.apple_sdk.frameworks.CoreML + ])); + }); }; } ) From 43639c90fc3c1e9b87caf8dd5205dd88153e884c Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 11 Nov 2024 15:53:51 +0530 Subject: [PATCH 09/42] feat: Do not emit cargo warnings from compiler warnings --- mnn-sys/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index fd22505..c5171ea 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -549,6 +549,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { // Get version build // .try_flags_from_environment(concat!(env!("CARGO_PKG_NAME"), "_CFLAGS"))? + .cargo_warnings(false) .include(vendor.join("include/")) .include(vendor.join("source/")) .include(vendor.join("express/")) From db9069271a170c098ee3bb5b7039794deb21107a Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 11 Nov 2024 15:56:54 +0530 Subject: [PATCH 10/42] fix: Do not emit link args when compiling with cc crate --- mnn-sys/build.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index c5171ea..e89d1af 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -117,14 +117,10 @@ fn main() -> Result<()> { } if *MNN_COMPILE { - let install_dir = out_dir.join("mnn-install"); build_cpp_build(&vendor)?; - println!( - "cargo:rustc-link-search=native={}", - install_dir.join("lib").display() - ); } else if let core::result::Result::Ok(lib_dir) = std::env::var("MNN_LIB_DIR") { println!("cargo:rustc-link-search=native={}", lib_dir); + println!("cargo:rustc-link-lib=static=MNN"); } else { panic!("MNN_LIB_DIR not set while MNN_COMPILE is false"); } @@ -167,7 +163,6 @@ fn main() -> Result<()> { wasm32_emscripten_libs.display() ); } - println!("cargo:rustc-link-lib=static=MNN"); Ok(()) } From e00e314c183dc0a3c73ebd66850b6af745d01eb2 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 11 Nov 2024 12:52:01 +0100 Subject: [PATCH 11/42] fix: OpenCL issues in linux --- Cargo.lock | 8 +++++++- flake.nix | 6 ++++++ mnn-sys/Cargo.toml | 4 +--- mnn-sys/build.rs | 16 ++++++++++------ src/backend.rs | 2 +- src/interpreter.rs | 30 +++++++++++++++--------------- src/lib.rs | 2 +- 7 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 747e099..9b576f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -542,7 +542,7 @@ dependencies = [ "fs_extra", "ignore", "itertools", - "libc", + "pkg-config", "tap", "walkdir", ] @@ -664,6 +664,12 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + [[package]] name = "portable-atomic" version = "1.8.0" diff --git a/flake.nix b/flake.nix index 73dda9a..4f68eee 100644 --- a/flake.nix +++ b/flake.nix @@ -95,9 +95,14 @@ cmake llvmPackages.libclang.lib clang + pkg-config ]; buildInputs = with pkgs; [] + ++ (lib.optionals pkgs.stdenv.isLinux [ + ocl-icd + opencl-headers + ]) ++ (lib.optionals pkgs.stdenv.isDarwin [ darwin.apple_sdk.frameworks.OpenCL ] @@ -206,6 +211,7 @@ // { packages = with pkgs; [ + clang nushell git git-lfs diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index 8cfd4ed..3ce9d86 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -14,6 +14,7 @@ dunce = "1.0.4" fs_extra = "1.3.0" ignore = "0.4.23" itertools = "0.13.0" +pkg-config = "0.3.31" tap = "1.0.1" walkdir = "2.5.0" @@ -32,6 +33,3 @@ arm82 = [] bf16 = [] default = ["mnn-threadpool", "sparse-compute", "opencl"] - -[dependencies] -libc = "0.2.155" diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index e89d1af..8549f15 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -145,9 +145,13 @@ fn main() -> Result<()> { println!("cargo:rustc-link-lib=framework=OpenCL"); #[cfg(feature = "opengl")] println!("cargo:rustc-link-lib=framework=OpenGL"); - } else { - // #[cfg(feature = "opencl")] - // println!("cargo:rustc-link-lib=static=opencl"); + } else if *TARGET_OS == "linux" { + #[cfg(feature = "opencl")] + { + if !pkg_config::probe_library("OpenCL").is_ok() { + println!("cargo:rustc-link-lib=static=OpenCL"); + }; + } } if is_emscripten() { // println!("cargo:rustc-link-lib=static=stdc++"); @@ -609,9 +613,9 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { arm(&mut build, cpu_files_dir.join("arm"))?; } - if TARGET_FEATURES.contains(&("sse".into())) && is_x86() { - x86_64(&mut build, cpu_files_dir.join("x86_64"))?; - } + // if TARGET_FEATURES.contains(&("sse".into())) && is_x86() { + // x86_64(&mut build, cpu_files_dir.join("x86_64"))?; + // } build.files(cpu_files); } diff --git a/src/backend.rs b/src/backend.rs index b71cb34..618ef7c 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -176,7 +176,7 @@ impl BackendConfig { /// # Safety /// This just binds to the underlying unsafe api and should be used only if you know what you /// are doing - pub unsafe fn set_shared_context(&mut self, shared_context: *mut libc::c_void) { + pub unsafe fn set_shared_context(&mut self, shared_context: *mut core::ffi::c_void) { unsafe { mnn_sys::mnnbc_set_shared_context(self.inner, shared_context); } diff --git a/src/interpreter.rs b/src/interpreter.rs index 80c1a25..f78e10d 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -21,7 +21,7 @@ impl Default for TensorCallback { } impl TensorCallback { - pub fn from_ptr(f: *mut libc::c_void) -> Self { + pub fn from_ptr(f: *mut core::ffi::c_void) -> Self { debug_assert!(!f.is_null()); unsafe { Self { @@ -30,8 +30,8 @@ impl TensorCallback { } } - pub fn into_ptr(self) -> *mut libc::c_void { - Arc::into_raw(self.inner) as *mut libc::c_void + pub fn into_ptr(self) -> *mut core::ffi::c_void { + Arc::into_raw(self.inner) as *mut core::ffi::c_void } pub fn identity() -> impl Fn(&[RawTensor], OperatorInfo) -> bool { @@ -463,7 +463,7 @@ impl Interpreter { end: impl Fn(&[RawTensor], OperatorInfo) -> bool + 'static, sync: bool, ) -> Result<()> { - let sync = sync as libc::c_int; + let sync = sync as core::ffi::c_int; let before = TensorCallback::from(before).into_ptr(); let end = TensorCallback::from(end).into_ptr(); let ret = unsafe { @@ -555,7 +555,7 @@ impl Interpreter { self.inner, session.inner, mnn_sys::cpp::MNN_Interpreter_SessionInfoCode_FLOPS as _, - flop_ptr.cast::(), + flop_ptr.cast::(), ) }; ensure!( @@ -742,15 +742,15 @@ impl<'t, 'tl> Iterator for TensorListIter<'t, 'tl> { // #[no_mangle] // extern "C" fn rust_closure_callback_runner( -// f: *mut libc::c_void, +// f: *mut core::ffi::c_void, // tensors: *const *mut mnn_sys::Tensor, // tensor_count: usize, -// name: *const libc::c_char, -// ) -> libc::c_int { +// name: *const core::ffi::c_char, +// ) -> core::ffi::c_int { // let tensors = unsafe { std::slice::from_raw_parts(tensors.cast(), tensor_count) }; // let name = unsafe { std::ffi::CStr::from_ptr(name) }; // let f: TensorCallback = unsafe { Box::from_raw(f.cast::()) }; -// let ret = f(tensors, name) as libc::c_int; +// let ret = f(tensors, name) as core::ffi::c_int; // core::mem::forget(f); // ret // } @@ -766,24 +766,24 @@ impl<'t, 'tl> Iterator for TensorListIter<'t, 'tl> { // let tensors = [std::ptr::null_mut()]; // let name = std::ffi::CString::new("Test").unwrap(); // let ret = rust_closure_callback_runner(f, tensors.as_ptr(), tensors.len(), name.as_ptr()) -// as libc::c_int; +// as core::ffi::c_int; // assert_eq!(ret, 0); // } #[no_mangle] extern "C" fn rust_closure_callback_runner_op( - f: *mut libc::c_void, + f: *mut core::ffi::c_void, tensors: *const *mut mnn_sys::Tensor, tensor_count: usize, - op: *mut libc::c_void, -) -> libc::c_int { + op: *mut core::ffi::c_void, +) -> core::ffi::c_int { let tensors = unsafe { std::slice::from_raw_parts(tensors.cast(), tensor_count) }; let f: TensorCallback = TensorCallback::from_ptr(f); let op = OperatorInfo { inner: op.cast(), __marker: PhantomData, }; - let ret = f(tensors, op) as libc::c_int; + let ret = f(tensors, op) as core::ffi::c_int; core::mem::forget(f); ret @@ -791,7 +791,7 @@ extern "C" fn rust_closure_callback_runner_op( #[repr(transparent)] pub struct OperatorInfo<'op> { - pub(crate) inner: *mut libc::c_void, + pub(crate) inner: *mut core::ffi::c_void, pub(crate) __marker: PhantomData<&'op ()>, } diff --git a/src/lib.rs b/src/lib.rs index f15942d..dd3cf31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,5 +86,5 @@ pub mod prelude { pub(crate) use crate::profile::profile; pub use core::marker::PhantomData; pub use error_stack::{Report, ResultExt}; - pub use libc::*; + pub use core::ffi::*; } From 33322b71f8d27aefda0ac40f4c06a33bd32f9129 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Tue, 12 Nov 2024 14:58:00 +0530 Subject: [PATCH 12/42] fix: Compilation issue on enabling sse --- Cargo.lock | 30 +++++- flake.lock | 6 +- flake.nix | 2 +- mnn-sys/Cargo.toml | 2 + mnn-sys/build.rs | 226 +++++++++++++++++++++++++++++---------------- 5 files changed, 180 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b576f7..bca2ebf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -504,7 +504,7 @@ dependencies = [ "libc", "mnn-sys", "oneshot", - "thiserror", + "thiserror 1.0.64", "tracing", ] @@ -539,11 +539,13 @@ dependencies = [ "cc", "diffy", "dunce", + "error-stack", "fs_extra", "ignore", "itertools", "pkg-config", "tap", + "thiserror 2.0.3", "walkdir", ] @@ -876,9 +878,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.77" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", @@ -907,7 +909,16 @@ version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.64", +] + +[[package]] +name = "thiserror" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +dependencies = [ + "thiserror-impl 2.0.3", ] [[package]] @@ -921,6 +932,17 @@ dependencies = [ "syn", ] +[[package]] +name = "thiserror-impl" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.1.8" diff --git a/flake.lock b/flake.lock index 1e28b54..145cdaf 100644 --- a/flake.lock +++ b/flake.lock @@ -178,11 +178,11 @@ ] }, "locked": { - "lastModified": 1731032894, - "narHash": "sha256-dQSyYPmrQiPr+PGEd+K8038rubFGz7G/dNXVeaGWE0w=", + "lastModified": 1731292155, + "narHash": "sha256-fYVoUUtSadbOrH0z0epVQDsStBDS/S/fAK//0ECQAAI=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "d52f2a4c103a0acf09ded857b9e2519ae2360e59", + "rev": "7c4cd99ed7604b79e8cb721099ac99c66f656b3a", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 4f68eee..f87f1a2 100644 --- a/flake.nix +++ b/flake.nix @@ -215,7 +215,7 @@ nushell git git-lfs - nightlyToolchain + stableToolchainWithRustAnalyzer cargo-nextest cargo-hakari cargo-deny diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index 3ce9d86..e2dcc79 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -11,11 +11,13 @@ bindgen = { version = "0.70", features = ["experimental"] } cc = { version = "1.1.5", features = ["parallel"] } diffy = "0.4.0" dunce = "1.0.4" +error-stack.workspace = true fs_extra = "1.3.0" ignore = "0.4.23" itertools = "0.13.0" pkg-config = "0.3.31" tap = "1.0.1" +thiserror = "2.0.3" walkdir = "2.5.0" [features] diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 8549f15..85453e5 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -1,5 +1,9 @@ use ::tap::*; -use anyhow::*; +use error_stack::*; +#[derive(Debug, thiserror::Error)] +#[error("Failed to build mnn-sys")] +pub struct Error; +pub type Result> = core::result::Result; #[cfg(unix)] use std::os::unix::fs::PermissionsExt; use std::{ @@ -7,6 +11,7 @@ use std::{ path::{Path, PathBuf}, sync::LazyLock, }; + const VENDOR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/vendor"); const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR"); static TARGET_POINTER_WIDTH: LazyLock = LazyLock::new(|| { @@ -60,20 +65,37 @@ fn ensure_vendor_exists(vendor: impl AsRef) -> Result<()> { if vendor .as_ref() .read_dir() - .with_context(|| format!("Vendor directory missing: {}", vendor.as_ref().display()))? + .change_context(Error) + .attach_printable_lazy(|| { + format!("Vendor directory missing: {}", vendor.as_ref().display()) + })? .flatten() .count() == 0 { - anyhow::bail!("Vendor not found maybe you need to run \"git submodule update --init\"") + return Err(Report::new(Error).attach_printable( + "Vendor not found maybe you need to run \"git submodule update --init\"", + )); } Ok(()) } -fn main() -> Result<()> { +fn main() { + match _main() { + Ok(_) => (), + Err(e) => { + Report::set_color_mode(fmt::ColorMode::Color); + Report::set_charset(fmt::Charset::default()); + eprintln!("{e:?}"); + panic!("Failed to compile mnn-sys") + } + } +} + +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 out_dir = PathBuf::from(std::env::var("OUT_DIR").change_context(Error)?); let source = PathBuf::from( std::env::var("MNN_SRC") .ok() @@ -91,15 +113,17 @@ fn main() -> Result<()> { .overwrite(true) .copy_inside(true), ) - .context("Failed to copy vendor")?; + .change_context(Error) + .attach_printable("Failed to copy vendor")?; let intptr = vendor.join("include").join("MNN").join("HalideRuntime.h"); #[cfg(unix)] - std::fs::set_permissions(&intptr, std::fs::Permissions::from_mode(0o644))?; + std::fs::set_permissions(&intptr, std::fs::Permissions::from_mode(0o644)) + .change_context(Error)?; // try_patch_file("patches/halide_type_t_64.patch", intptr) - // .context("Failed to patch vendor")?; + // .attach_printable("Failed to patch vendor")?; use itertools::Itertools; - let intptr_contents = std::fs::read_to_string(&intptr)?; + let intptr_contents = std::fs::read_to_string(&intptr).change_context(Error)?; let patched = intptr_contents.lines().collect::>(); if let Some((idx, _)) = patched .iter() @@ -112,7 +136,7 @@ fn main() -> Result<()> { .filter(|(c_idx, _)| !(*c_idx == idx - 1 || (idx + 1..=idx + 3).contains(c_idx))) .map(|(_, c)| c) .collect::>(); - std::fs::write(intptr, patched.join("\n"))?; + std::fs::write(intptr, patched.join("\n")).change_context(Error)?; } } @@ -126,9 +150,9 @@ fn main() -> Result<()> { } mnn_c_build(PathBuf::from(MANIFEST_DIR).join("mnn_c"), &vendor) - .with_context(|| "Failed to build mnn_c")?; - mnn_c_bindgen(&vendor, &out_dir).with_context(|| "Failed to generate mnn_c bindings")?; - mnn_cpp_bindgen(&vendor, &out_dir).with_context(|| "Failed to generate mnn_cpp bindings")?; + .attach_printable("Failed to build mnn_c")?; + mnn_c_bindgen(&vendor, &out_dir).attach_printable("Failed to generate mnn_c bindings")?; + mnn_cpp_bindgen(&vendor, &out_dir).attach_printable("Failed to generate mnn_cpp bindings")?; println!("cargo:include={vendor}/include", vendor = vendor.display()); if *TARGET_OS == "macos" { #[cfg(feature = "metal")] @@ -157,9 +181,12 @@ fn main() -> Result<()> { // println!("cargo:rustc-link-lib=static=stdc++"); let emscripten_cache = std::process::Command::new("em-config") .arg("CACHE") - .output()? + .output() + .change_context(Error)? .stdout; - let emscripten_cache = std::str::from_utf8(&emscripten_cache)?.trim(); + let emscripten_cache = std::str::from_utf8(&emscripten_cache) + .change_context(Error)? + .trim(); let wasm32_emscripten_libs = PathBuf::from(emscripten_cache).join("sysroot/lib/wasm32-emscripten"); println!( @@ -173,9 +200,13 @@ fn main() -> Result<()> { pub fn mnn_c_bindgen(vendor: impl AsRef, out: impl AsRef) -> Result<()> { let vendor = vendor.as_ref(); let mnn_c = PathBuf::from(MANIFEST_DIR).join("mnn_c"); - mnn_c.read_dir()?.flatten().for_each(|e| { - rerun_if_changed(e.path()); - }); + mnn_c + .read_dir() + .change_context(Error)? + .flatten() + .for_each(|e| { + rerun_if_changed(e.path()); + }); const HEADERS: &[&str] = &[ "error_code_c.h", "interpreter_c.h", @@ -185,7 +216,6 @@ pub fn mnn_c_bindgen(vendor: impl AsRef, out: impl AsRef) -> Result< ]; let bindings = bindgen::Builder::default() - // .clang_args(["-x", "c++"]) .clang_arg(CxxOption::VULKAN.cxx()) .clang_arg(CxxOption::METAL.cxx()) .clang_arg(CxxOption::COREML.cxx()) @@ -230,8 +260,11 @@ pub fn mnn_c_bindgen(vendor: impl AsRef, out: impl AsRef) -> Result< // // eprintln!("Full bindgen: {}", d.command_line_flags().join(" ")); // std::fs::write("bindgen.txt", d.command_line_flags().join(" ")).ok(); // }) - .generate()?; - bindings.write_to_file(out.as_ref().join("mnn_c.rs"))?; + .generate() + .change_context(Error)?; + bindings + .write_to_file(out.as_ref().join("mnn_c.rs")) + .change_context(Error)?; Ok(()) } @@ -260,17 +293,24 @@ pub fn mnn_cpp_bindgen(vendor: impl AsRef, out: impl AsRef) -> Resul .allowlist_item(".*SessionInfoCode.*"); // let cmd = bindings.command_line_flags().join(" "); // println!("cargo:warn=bindgen: {}", cmd); - let bindings = bindings.generate()?; - bindings.write_to_file(out.as_ref().join("mnn_cpp.rs"))?; + let bindings = bindings.generate().change_context(Error)?; + bindings + .write_to_file(out.as_ref().join("mnn_cpp.rs")) + .change_context(Error)?; Ok(()) } pub fn mnn_c_build(path: impl AsRef, vendor: impl AsRef) -> Result<()> { let mnn_c = path.as_ref(); - let files = mnn_c.read_dir()?.flatten().map(|e| e.path()).filter(|e| { - e.extension() == Some(std::ffi::OsStr::new("cpp")) - || e.extension() == Some(std::ffi::OsStr::new("c")) - }); + let files = mnn_c + .read_dir() + .change_context(Error)? + .flatten() + .map(|e| e.path()) + .filter(|e| { + e.extension() == Some(std::ffi::OsStr::new("cpp")) + || e.extension() == Some(std::ffi::OsStr::new("c")) + }); let vendor = vendor.as_ref(); cc::Build::new() .include(vendor.join("include")) @@ -312,20 +352,8 @@ pub fn mnn_c_build(path: impl AsRef, vendor: impl AsRef) -> Result<( // build // }) .try_compile("mnn_c") - .context("Failed to compile mnn_c library")?; - Ok(()) -} - -pub fn try_patch_file(patch: impl AsRef, file: impl AsRef) -> Result<()> { - let patch = dunce::canonicalize(patch)?; - rerun_if_changed(&patch); - let patch = std::fs::read_to_string(&patch)?; - let patch = diffy::Patch::from_str(&patch)?; - let file_path = file.as_ref(); - let file = std::fs::read_to_string(file_path).context("Failed to read input file")?; - let patched_file = - diffy::apply(&file, &patch).context("Failed to apply patches using diffy")?; - std::fs::write(file_path, patched_file)?; + .change_context(Error) + .attach_printable("Failed to compile mnn_c library")?; Ok(()) } @@ -545,21 +573,24 @@ fn read_dir(input: impl AsRef) -> impl Iterator { pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { let mut build = cc::Build::new(); let vendor = vendor.as_ref(); + let mut includes = vec![ + vendor.join("include/"), + vendor.join("source/"), + vendor.join("express/"), + vendor.join("tools/"), + vendor.join("codegen/"), + vendor.join("schema/current/"), + vendor.join("3rd_party/"), + vendor.join("3rd_party/flatbuffers/include"), + vendor.join("3rd_party/half"), + vendor.join("3rd_party/imageHelper"), + vendor.join("3rd_party/OpenCLHeaders/"), + ]; // Get version build // .try_flags_from_environment(concat!(env!("CARGO_PKG_NAME"), "_CFLAGS"))? .cargo_warnings(false) - .include(vendor.join("include/")) - .include(vendor.join("source/")) - .include(vendor.join("express/")) - .include(vendor.join("tools/")) - .include(vendor.join("codegen/")) - .include(vendor.join("schema/current/")) - .include(vendor.join("3rd_party/")) - .include(vendor.join("3rd_party/flatbuffers/include")) - .include(vendor.join("3rd_party/half")) - .include(vendor.join("3rd_party/imageHelper")) - .include(vendor.join("3rd_party/OpenCLHeaders/")) + .includes(&includes) .cpp(true) .std("c++11"); @@ -607,15 +638,16 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { if CxxOption::ARM82.enabled() && is_arm() { build.define("ENABLE_ARMV82", None); build.include(cpu_files_dir.join("arm")); + includes.push(cpu_files_dir.join("arm")); } if is_arm() { arm(&mut build, cpu_files_dir.join("arm"))?; } - // if TARGET_FEATURES.contains(&("sse".into())) && is_x86() { - // x86_64(&mut build, cpu_files_dir.join("x86_64"))?; - // } + if TARGET_FEATURES.contains(&("sse".into())) && is_x86() { + x86_64(&mut build, &includes, cpu_files_dir.join("x86_x64"))?; + } build.files(cpu_files); } @@ -652,15 +684,17 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { .filter_map(Result::ok) .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); - build.include(opencl_files_dir.join("schema").join("current")); + let ocl_includes = opencl_files_dir.join("schema").join("current"); + build.include(ocl_includes.clone()); + includes.push(ocl_includes); build.define("MNN_OPENCL_ENABLED", "1"); build.files(opencl_files.chain([opencl_files_dir.join("execution/cl/opencl_program.cc")])); } - build.get_files().for_each(|f| { - rerun_if_changed(f); - }); - build.compile("mnn"); + build + .try_compile("mnn") + .change_context(Error) + .attach_printable("Failed to compile mnn")?; Ok(()) } @@ -715,19 +749,25 @@ fn arm(build: &mut cc::Build, arm_dir: impl AsRef) -> Result<&mut cc::Buil Ok(build) } -fn x86_64(build: &mut cc::Build, x86_64_dir: impl AsRef) -> Result<&mut cc::Build> { +fn x86_64<'a>( + build: &'a mut cc::Build, + includes: &'_ [PathBuf], + x86_64_dir: impl AsRef, +) -> Result<&'a mut cc::Build> { let mnn_assembler = std::env::var("MNN_ASSEMBLER").ok(); let like_msvc = build.get_compiler().is_like_msvc(); let win_use_asm = like_msvc && *TARGET_POINTER_WIDTH == 64 && mnn_assembler.is_some(); let has_avx512 = target_has_avx512(); build.define("MNN_USE_SSE", None); let x86_src_dir = x86_64_dir.as_ref(); - let mnn_x8664_src = read_dir(&x86_src_dir); - let mnn_avx_src = read_dir(x86_src_dir.join("avx")); - let mnn_avxfma_src = read_dir(x86_src_dir.join("avxfma")); - let mnn_sse_src = read_dir(x86_src_dir.join("sse")); + let mnn_x8664_src = read_dir(&x86_src_dir).filter(|p| cpp_filter(p)); + let mnn_avx_src = read_dir(x86_src_dir.join("avx")).filter(|p| cpp_filter(p)); + let mnn_avxfma_src = read_dir(x86_src_dir.join("avxfma")).filter(|p| cpp_filter(p)); + let mnn_sse_src = read_dir(x86_src_dir.join("sse")).filter(|p| cpp_filter(p)); let mnn_avx512_vnni_src = x86_src_dir.join("avx512/GemmInt8_VNNI.cpp"); - let mnn_avx512_src = read_dir(x86_src_dir.join("avx512")).filter(|p| p != &mnn_avx512_vnni_src); + let mnn_avx512_src = read_dir(x86_src_dir.join("avx512")) + .filter(|p| cpp_filter(p)) + .filter(|p| p != &mnn_avx512_vnni_src); if has_avx512 && CxxOption::AVX512.enabled() && (!like_msvc || win_use_asm) { let mnn_avx512 = cc::Build::new() @@ -748,7 +788,8 @@ fn x86_64(build: &mut cc::Build, x86_64_dir: impl AsRef) -> Result<&mut cc .flag_if_supported("-mfma"); } }) - .compile_intermediates(); + .try_compile_intermediates() + .change_context(Error)?; build.objects(mnn_avx512); let mnn_avx512_vnni = true; if mnn_avx512_vnni { @@ -771,25 +812,38 @@ fn x86_64(build: &mut cc::Build, x86_64_dir: impl AsRef) -> Result<&mut cc .flag_if_supported("-mavx512vnni"); } }) - .compile_intermediates(); + .try_compile_intermediates() + .change_context(Error)?; build.objects(mnn_avx512_vnni); } } let mnn_sse = cc::Build::new() + .cpp(true) + .std("c++11") + .includes(includes) + // .cargo_warnings(false) .files(mnn_sse_src) - .flag("MNN_USE_SSE") + .define("MNN_USE_SSE", None) .tap_mut(|build| { if !like_msvc { - build.flag_if_supported("-msse4.1"); + build + // .flag_if_supported("-msse") + // .flag_if_supported("-msse2") + .flag("-msse4.1"); } CxxOption::LOW_MEMORY.define(build); }) - .compile_intermediates(); + .try_compile_intermediates() + .change_context(Error) + .attach_printable("Failed to build sse extensions")?; let mnn_avx = cc::Build::new() + .cpp(true) + .std("c++11") + .includes(includes) .files(mnn_avx_src) - .flag("MNN_USE_SSE") + .define("MNN_USE_SSE", None) .tap_mut(|build| { if like_msvc { build.flag_if_supported("/arch:AVX"); @@ -800,11 +854,15 @@ fn x86_64(build: &mut cc::Build, x86_64_dir: impl AsRef) -> Result<&mut cc } CxxOption::LOW_MEMORY.define(build); }) - .compile_intermediates(); + .try_compile_intermediates() + .change_context(Error)?; let mnn_avxfma = cc::Build::new() + .cpp(true) + .std("c++11") + .includes(includes) .files(mnn_avxfma_src) - .flag("MNN_USE_SSE") + .define("MNN_USE_SSE", None) .tap_mut(|build| { if like_msvc { build.flag_if_supported("/arch:AVX2"); @@ -817,11 +875,14 @@ fn x86_64(build: &mut cc::Build, x86_64_dir: impl AsRef) -> Result<&mut cc CxxOption::LOW_MEMORY.define(build); CxxOption::BF16.define(build) }) - .compile_intermediates(); + .try_compile_intermediates() + .change_context(Error)?; let mnn_x8664 = cc::Build::new() + .cpp(true) + .std("c++11") + .includes(includes) .files(mnn_x8664_src) - .flag("MNN_USE_SSE") .tap_mut(|build| { CxxOption::LOW_MEMORY.define(build); CxxOption::CPU_WEIGHT_DEQUANT_GEMM.define(build); @@ -829,7 +890,8 @@ fn x86_64(build: &mut cc::Build, x86_64_dir: impl AsRef) -> Result<&mut cc CxxOption::AVX512.define(build); } }) - .compile_intermediates(); + .try_compile_intermediates() + .change_context(Error)?; build.objects(mnn_sse); build.objects(mnn_x8664); @@ -849,9 +911,17 @@ fn target_has_avx512() -> bool { #error "AVX-512 support is required to compile this program." #endif int main() {return 0;} "#; - std::fs::write("test.c", AVX_PRG).ok(); + let out_dir: PathBuf = std::env::var("OUT_DIR") + .expect("OUT_DIR must be set in build.rs") + .into(); + std::fs::write(out_dir.join("test.c"), AVX_PRG).expect("Failed to write to out_dir"); cc::Build::new() .file("test.c") + .cargo_warnings(false) .try_compile("avx512") .is_ok() } + +fn cpp_filter(path: impl AsRef) -> bool { + path.as_ref().extension() == Some(OsStr::new("cpp")) +} From 827a7f28129f795936c44c92f610ac4c6eb21e7d Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Tue, 12 Nov 2024 15:11:41 +0530 Subject: [PATCH 13/42] chore: Remove some comments --- mnn-sys/build.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 85453e5..074779b 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -827,10 +827,7 @@ fn x86_64<'a>( .define("MNN_USE_SSE", None) .tap_mut(|build| { if !like_msvc { - build - // .flag_if_supported("-msse") - // .flag_if_supported("-msse2") - .flag("-msse4.1"); + build.flag_if_supported("-msse4.1"); } CxxOption::LOW_MEMORY.define(build); }) From 00131e19e18fcadd16a4896f06f368cbb3062b6f Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 11:52:56 +0530 Subject: [PATCH 14/42] feat: Add all features to cargo.toml chore: cargo fmt and clippy fixes chore: Cleanup some extra files --- Cargo.lock | 1 - Cargo.toml | 3 --- flake.nix | 9 ++++++--- mnn-sys/Cargo.toml | 6 ++++++ mnn-sys/build.rs | 3 +-- mnn-sys/mnn_c/interpreter_c.cpp | 2 +- mnn-sys/mnn_c/interpreter_c.h | 2 +- mnn-sys/mnn_c/mnndefine.h | 0 src/lib.rs | 2 +- 9 files changed, 16 insertions(+), 12 deletions(-) delete mode 100644 mnn-sys/mnn_c/mnndefine.h diff --git a/Cargo.lock b/Cargo.lock index bca2ebf..5455c72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -496,7 +496,6 @@ version = "0.2.0" dependencies = [ "anyhow", "bytemuck", - "cc", "clap", "divan", "dunce", diff --git a/Cargo.toml b/Cargo.toml index 35ceae1..c8128ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,3 @@ harness = false [profile.rwd] debug = true inherits = "release" - -[build-dependencies] -cc = "1.1.36" diff --git a/flake.nix b/flake.nix index f87f1a2..1cf54bb 100644 --- a/flake.nix +++ b/flake.nix @@ -77,9 +77,12 @@ stableToolchainWithLLvmTools = pkgs.rust-bin.stable.latest.default.override { extensions = ["rust-src" "llvm-tools"]; }; - stableToolchainWithRustAnalyzer = pkgs.rust-bin.stable.latest.default.override { - extensions = ["rust-src" "rust-analyzer"]; - }; + stableToolchainWithRustAnalyzer = pkgs.rust-bin.stable.latest.default.override ({ + extensions = ["rust-src" "rust-analyzer"]; + } + // (lib.optionalAttrs pkgs.stdenv.isDarwin { + targets = ["aarch64-apple-darwin" "x86_64-apple-darwin"]; + })); craneLib = (crane.mkLib pkgs).overrideToolchain stableToolchain; craneLibLLvmTools = (crane.mkLib pkgs).overrideToolchain stableToolchainWithLLvmTools; diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index e2dcc79..1aa1b51 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -27,11 +27,17 @@ coreml = ["metal"] opencl = [] openmp = [] opengl = [] + mnn-threadpool = [] + crt_static = [] mini-build = [] sparse-compute = [] arm82 = [] bf16 = [] +cpu-weight-dequant-gemm = [] +avx512 = [] +neon = [] +low-memory = [] default = ["mnn-threadpool", "sparse-compute", "opencl"] diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 074779b..460554e 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -172,7 +172,7 @@ fn _main() -> Result<()> { } else if *TARGET_OS == "linux" { #[cfg(feature = "opencl")] { - if !pkg_config::probe_library("OpenCL").is_ok() { + if pkg_config::probe_library("OpenCL").is_err() { println!("cargo:rustc-link-lib=static=OpenCL"); }; } @@ -822,7 +822,6 @@ fn x86_64<'a>( .cpp(true) .std("c++11") .includes(includes) - // .cargo_warnings(false) .files(mnn_sse_src) .define("MNN_USE_SSE", None) .tap_mut(|build| { diff --git a/mnn-sys/mnn_c/interpreter_c.cpp b/mnn-sys/mnn_c/interpreter_c.cpp index 2fe130f..0e8da2e 100644 --- a/mnn-sys/mnn_c/interpreter_c.cpp +++ b/mnn-sys/mnn_c/interpreter_c.cpp @@ -353,7 +353,7 @@ const char *OperatorInfo_name(const void *op) { const char *OperatorInfo_type(const void *op) { return reinterpret_cast(op)->type().c_str(); } -const float OperatorInfo_flops(const void *op) { +float OperatorInfo_flops(const void *op) { return reinterpret_cast(op)->flops(); } } // extern "C" diff --git a/mnn-sys/mnn_c/interpreter_c.h b/mnn-sys/mnn_c/interpreter_c.h index 40c053b..bcd82af 100644 --- a/mnn-sys/mnn_c/interpreter_c.h +++ b/mnn-sys/mnn_c/interpreter_c.h @@ -196,7 +196,7 @@ const char *Interpreter_uuid(const Interpreter *interpreter); const char *OperatorInfo_name(const void *op); const char *OperatorInfo_type(const void *op); -const float OperatorInfo_flops(const void *op); +float OperatorInfo_flops(const void *op); #ifdef __cplusplus } diff --git a/mnn-sys/mnn_c/mnndefine.h b/mnn-sys/mnn_c/mnndefine.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib.rs b/src/lib.rs index dd3cf31..e96abef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,7 +84,7 @@ pub use ffi::MapType; pub mod prelude { pub use crate::error::*; pub(crate) use crate::profile::profile; + pub use core::ffi::*; pub use core::marker::PhantomData; pub use error_stack::{Report, ResultExt}; - pub use core::ffi::*; } From fb1140ba920f784c6934e707a0e8ca5bfbbdc2b0 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 12:09:42 +0530 Subject: [PATCH 15/42] feat: Add .S files as well --- mnn-sys/build.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 460554e..1ae6842 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -761,12 +761,13 @@ fn x86_64<'a>( build.define("MNN_USE_SSE", None); let x86_src_dir = x86_64_dir.as_ref(); let mnn_x8664_src = read_dir(&x86_src_dir).filter(|p| cpp_filter(p)); - let mnn_avx_src = read_dir(x86_src_dir.join("avx")).filter(|p| cpp_filter(p)); - let mnn_avxfma_src = read_dir(x86_src_dir.join("avxfma")).filter(|p| cpp_filter(p)); + let mnn_avx_src = read_dir(x86_src_dir.join("avx")).filter(|p| cpp_filter(p) || asm_filter(p)); + let mnn_avxfma_src = + read_dir(x86_src_dir.join("avxfma")).filter(|p| cpp_filter(p) || asm_filter(p)); let mnn_sse_src = read_dir(x86_src_dir.join("sse")).filter(|p| cpp_filter(p)); let mnn_avx512_vnni_src = x86_src_dir.join("avx512/GemmInt8_VNNI.cpp"); let mnn_avx512_src = read_dir(x86_src_dir.join("avx512")) - .filter(|p| cpp_filter(p)) + .filter(|p| cpp_filter(p) || asm_filter(p)) .filter(|p| p != &mnn_avx512_vnni_src); if has_avx512 && CxxOption::AVX512.enabled() && (!like_msvc || win_use_asm) { @@ -921,3 +922,8 @@ int main() {return 0;} "#; fn cpp_filter(path: impl AsRef) -> bool { path.as_ref().extension() == Some(OsStr::new("cpp")) } + +fn asm_filter(path: impl AsRef) -> bool { + path.as_ref().extension() == Some(OsStr::new("S")) + || path.as_ref().extension() == Some(OsStr::new("s")) +} From 28436e76d8f6687e2bc819dcf49b3b34e4370efe Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 07:46:53 +0100 Subject: [PATCH 16/42] fix: Added cc extensions as c++ files as well --- mnn-sys/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 1ae6842..11409f8 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -921,6 +921,7 @@ int main() {return 0;} "#; fn cpp_filter(path: impl AsRef) -> bool { path.as_ref().extension() == Some(OsStr::new("cpp")) + || path.as_ref().extension() == Some(OsStr::new("cc")) } fn asm_filter(path: impl AsRef) -> bool { From 416ee886fc7978628db49cb1fe64bc644b073bcf Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 14:42:37 +0530 Subject: [PATCH 17/42] add exclude files and use static_flag and static_crt --- Cargo.toml | 1 + mnn-sys/Cargo.toml | 12 ++++++++++++ mnn-sys/build.rs | 43 ++++++++++--------------------------------- 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c8128ee..05766c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [".", "mnn-bridge", "mnn-sync", "mnn-sys"] [workspace.package] license = "Apache-2.0" +repository = "https://github.com/aftershootco/mnn-rs" [package] name = "mnn" diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index 1aa1b51..e474119 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -4,6 +4,18 @@ version = "0.1.0" edition = "2021" links = "mnn" license = { workspace = true } +repository = { workspace = true } +description = "Low-level bindings to MNN" +include = [ + "/src", + "/vendor/source", + "/vendor/3rd_party/flatbuffers", + "/vendor/3rd_party/half", + "/vendor/3rd_party/OpenCLHeaders", + "/vendor/include", + "/vendor/schema", + "/build.rs", +] [build-dependencies] anyhow = "1.0.86" diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 11409f8..5ae9a57 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -338,19 +338,9 @@ pub fn mnn_c_build(path: impl AsRef, vendor: impl AsRef) -> Result<( }) .cpp(true) .static_flag(true) + .static_crt(true) .files(files) .std("c++14") - // .pipe(|build| { - // let c = build.get_compiler(); - // use std::io::Write; - // writeln!( - // std::fs::File::create("./command.txt").unwrap(), - // "{:?}", - // c.to_command() - // ) - // .unwrap(); - // build - // }) .try_compile("mnn_c") .change_context(Error) .attach_printable("Failed to compile mnn_c library")?; @@ -576,40 +566,22 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { let mut includes = vec![ vendor.join("include/"), vendor.join("source/"), - vendor.join("express/"), - vendor.join("tools/"), - vendor.join("codegen/"), vendor.join("schema/current/"), vendor.join("3rd_party/"), vendor.join("3rd_party/flatbuffers/include"), vendor.join("3rd_party/half"), - vendor.join("3rd_party/imageHelper"), vendor.join("3rd_party/OpenCLHeaders/"), ]; - // Get version + build - // .try_flags_from_environment(concat!(env!("CARGO_PKG_NAME"), "_CFLAGS"))? - .cargo_warnings(false) .includes(&includes) .cpp(true) + .static_crt(true) + .static_flag(true) .std("c++11"); - if cfg!(target_os = "windows") { - build - .flag_if_supported("/wd4267") - .flag_if_supported("/wd4018") - .flag_if_supported("/wd4251") - .flag_if_supported("/wd4996") - .flag_if_supported("/wd4244") - .flag_if_supported("/wd4146") - .flag_if_supported("/wd4129") - .flag_if_supported("/wd4305") - .flag_if_supported("/wd4275") - .flag_if_supported("/wd4101"); - } - CxxOption::all().iter().for_each(|opt| { - eprintln!("{}: {}", opt.name, opt.enabled()); + eprintln!("cargo:warn={}: {}", opt.name, opt.enabled()); if opt.enabled() { build.define(opt.name, opt.cc()); } @@ -773,6 +745,7 @@ fn x86_64<'a>( if has_avx512 && CxxOption::AVX512.enabled() && (!like_msvc || win_use_asm) { let mnn_avx512 = cc::Build::new() .files(mnn_avx512_src) + .static_crt(true) .define("MNN_USE_SSE", None) .define("MNN_X86_USE_ASM", None) .tap_mut(|build| { @@ -824,6 +797,7 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_sse_src) + .static_crt(true) .define("MNN_USE_SSE", None) .tap_mut(|build| { if !like_msvc { @@ -840,6 +814,7 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_avx_src) + .static_crt(true) .define("MNN_USE_SSE", None) .tap_mut(|build| { if like_msvc { @@ -859,6 +834,7 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_avxfma_src) + .static_crt(true) .define("MNN_USE_SSE", None) .tap_mut(|build| { if like_msvc { @@ -880,6 +856,7 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_x8664_src) + .static_crt(true) .tap_mut(|build| { CxxOption::LOW_MEMORY.define(build); CxxOption::CPU_WEIGHT_DEQUANT_GEMM.define(build); From 8d712b3eac41ca9633a43da34828b6011befeab3 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 14:47:55 +0530 Subject: [PATCH 18/42] feat: Add crt_static to all items --- mnn-sys/build.rs | 49 ++++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 5ae9a57..7b91441 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -61,6 +61,8 @@ static MNN_COMPILE: LazyLock = LazyLock::new(|| { const HALIDE_SEARCH: &str = r#"HALIDE_ATTRIBUTE_ALIGN(1) halide_type_code_t code; // halide_type_code_t"#; +const STATIC_CRT: bool = cfg!(feature = "crt_static"); + fn ensure_vendor_exists(vendor: impl AsRef) -> Result<()> { if vendor .as_ref() @@ -332,13 +334,11 @@ pub fn mnn_c_build(path: impl AsRef, vendor: impl AsRef) -> Result<( config.target("wasm32-unknown-emscripten"); config.cpp_link_stdlib("c++-noexcept"); } - #[cfg(feature = "crt_static")] - config.static_crt(true); config }) .cpp(true) .static_flag(true) - .static_crt(true) + .static_crt(STATIC_CRT) .files(files) .std("c++14") .try_compile("mnn_c") @@ -351,32 +351,6 @@ pub fn rerun_if_changed(path: impl AsRef) { println!("cargo:rerun-if-changed={}", path.as_ref().display()); } -// pub fn vulkan_includes(vendor: impl AsRef) -> Vec { -// let vendor = vendor.as_ref(); -// let vulkan_dir = vendor.join("source/backend/vulkan"); -// if cfg!(feature = "vulkan") { -// vec![ -// vulkan_dir.clone(), -// vulkan_dir.join("runtime"), -// vulkan_dir.join("component"), -// // IDK If the order is important but the cmake file does it like this -// vulkan_dir.join("buffer/execution"), -// vulkan_dir.join("buffer/backend"), -// vulkan_dir.join("buffer"), -// vulkan_dir.join("buffer/shaders"), -// // vulkan_dir.join("image/execution"), -// // vulkan_dir.join("image/backend"), -// // vulkan_dir.join("image"), -// // vulkan_dir.join("image/shaders"), -// vendor.join("schema/current"), -// vendor.join("3rd_party/flatbuffers/include"), -// vendor.join("source"), -// ] -// } else { -// vec![] -// } -// } - pub fn is_emscripten() -> bool { *TARGET_OS == "emscripten" && *TARGET_ARCH == "wasm32" } @@ -576,7 +550,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { build .includes(&includes) .cpp(true) - .static_crt(true) + .static_crt(STATIC_CRT) .static_flag(true) .std("c++11"); @@ -745,7 +719,8 @@ fn x86_64<'a>( if has_avx512 && CxxOption::AVX512.enabled() && (!like_msvc || win_use_asm) { let mnn_avx512 = cc::Build::new() .files(mnn_avx512_src) - .static_crt(true) + .static_crt(STATIC_CRT) + .static_flag(true) .define("MNN_USE_SSE", None) .define("MNN_X86_USE_ASM", None) .tap_mut(|build| { @@ -797,7 +772,8 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_sse_src) - .static_crt(true) + .static_crt(STATIC_CRT) + .static_flag(true) .define("MNN_USE_SSE", None) .tap_mut(|build| { if !like_msvc { @@ -814,7 +790,8 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_avx_src) - .static_crt(true) + .static_crt(STATIC_CRT) + .static_flag(true) .define("MNN_USE_SSE", None) .tap_mut(|build| { if like_msvc { @@ -834,7 +811,8 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_avxfma_src) - .static_crt(true) + .static_crt(STATIC_CRT) + .static_flag(true) .define("MNN_USE_SSE", None) .tap_mut(|build| { if like_msvc { @@ -856,7 +834,8 @@ fn x86_64<'a>( .std("c++11") .includes(includes) .files(mnn_x8664_src) - .static_crt(true) + .static_crt(STATIC_CRT) + .static_flag(true) .tap_mut(|build| { CxxOption::LOW_MEMORY.define(build); CxxOption::CPU_WEIGHT_DEQUANT_GEMM.define(build); From c2fe0dca67eeacf6a2705306a37dfbf10aea62a4 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 15:36:42 +0530 Subject: [PATCH 19/42] chore: Remove unused deps and features --- Cargo.lock | 54 ++++++---------------------------------------- Cargo.toml | 18 +++++++++------- mnn-sys/Cargo.toml | 16 ++++++-------- mnn-sys/build.rs | 2 +- 4 files changed, 23 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5455c72..5763cad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,15 +246,6 @@ version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" -[[package]] -name = "diffy" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d3041965b7a63e70447ec818a46b1e5297f7fcae3058356d226c02750c4e6cb" -dependencies = [ - "nu-ansi-term 0.50.1", -] - [[package]] name = "divan" version = "0.1.14" @@ -492,7 +483,7 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "mnn" -version = "0.2.0" +version = "0.3.0" dependencies = [ "anyhow", "bytemuck", @@ -503,7 +494,7 @@ dependencies = [ "libc", "mnn-sys", "oneshot", - "thiserror 1.0.64", + "thiserror", "tracing", ] @@ -531,21 +522,17 @@ dependencies = [ [[package]] name = "mnn-sys" -version = "0.1.0" +version = "0.2.0" dependencies = [ - "anyhow", "bindgen", "cc", - "diffy", - "dunce", "error-stack", "fs_extra", "ignore", "itertools", "pkg-config", "tap", - "thiserror 2.0.3", - "walkdir", + "thiserror", ] [[package]] @@ -605,15 +592,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "nu-ansi-term" -version = "0.50.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4a28e057d01f97e61255210fcff094d74ed0466038633e95017f5beb68e4399" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "num-complex" version = "0.4.6" @@ -902,33 +880,13 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "thiserror" -version = "1.0.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" -dependencies = [ - "thiserror-impl 1.0.64", -] - [[package]] name = "thiserror" version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" dependencies = [ - "thiserror-impl 2.0.3", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" -dependencies = [ - "proc-macro2", - "quote", - "syn", + "thiserror-impl", ] [[package]] @@ -1002,7 +960,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ "matchers", - "nu-ansi-term 0.46.0", + "nu-ansi-term", "once_cell", "regex", "sharded-slab", diff --git a/Cargo.toml b/Cargo.toml index 05766c5..9634e38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,32 +6,34 @@ repository = "https://github.com/aftershootco/mnn-rs" [package] name = "mnn" -version = "0.2.0" +version = "0.3.0" edition = "2021" license = { workspace = true } +repository = { workspace = true } +documentation = "https://docs.rs/mnn" +description = "High level MNN to Rust bindings" [workspace.dependencies] -mnn = { version = "0.2.0", path = "." } +mnn = { version = "0.3.0", path = "." } error-stack = { version = "0.5" } [dependencies] libc = "0.2" -mnn-sys = { version = "0.1", path = "mnn-sys", features = [] } -thiserror = "1.0" +mnn-sys = { version = "0.2", path = "mnn-sys", features = [] } +thiserror = "2" error-stack.workspace = true oneshot = "0.1" tracing = { version = "0.1.40", optional = true } dunce = "1.0.5" [features] +opencl = ["mnn-sys/opencl"] + metal = ["mnn-sys/metal"] coreml = ["mnn-sys/coreml"] -vulkan = ["mnn-sys/vulkan"] -opencl = ["mnn-sys/opencl"] -opengl = ["mnn-sys/opengl"] + crt_static = ["mnn-sys/crt_static"] # Disable mnn-threadpool to enable this -openmp = ["mnn-sys/openmp"] mnn-threadpool = ["mnn-sys/mnn-threadpool"] tracing = ["dep:tracing"] profile = ["tracing"] diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index e474119..fbadea4 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mnn-sys" -version = "0.1.0" +version = "0.2.0" edition = "2021" links = "mnn" license = { workspace = true } @@ -18,11 +18,8 @@ include = [ ] [build-dependencies] -anyhow = "1.0.86" bindgen = { version = "0.70", features = ["experimental"] } cc = { version = "1.1.5", features = ["parallel"] } -diffy = "0.4.0" -dunce = "1.0.4" error-stack.workspace = true fs_extra = "1.3.0" ignore = "0.4.23" @@ -30,15 +27,12 @@ itertools = "0.13.0" pkg-config = "0.3.31" tap = "1.0.1" thiserror = "2.0.3" -walkdir = "2.5.0" [features] -vulkan = [] +opencl = [] + metal = [] coreml = ["metal"] -opencl = [] -openmp = [] -opengl = [] mnn-threadpool = [] @@ -48,8 +42,10 @@ sparse-compute = [] arm82 = [] bf16 = [] cpu-weight-dequant-gemm = [] +# Disable if you don't plan to use cpu backend and want quicker compilation +sse = [] avx512 = [] neon = [] low-memory = [] -default = ["mnn-threadpool", "sparse-compute", "opencl"] +default = ["mnn-threadpool", "sparse-compute", "sse", "neon", "opencl"] diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 7b91441..98b0a34 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -591,7 +591,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { arm(&mut build, cpu_files_dir.join("arm"))?; } - if TARGET_FEATURES.contains(&("sse".into())) && is_x86() { + if TARGET_FEATURES.contains(&("sse".into())) && is_x86() && cfg!(feature = "sse") { x86_64(&mut build, &includes, cpu_files_dir.join("x86_x64"))?; } From 07f1dda8307c5d74857d62c28527bfb108e30ffa Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 15:43:16 +0530 Subject: [PATCH 20/42] feat: Disable compilation for vulkan coreml and metal --- mnn-sys/Cargo.toml | 1 + mnn-sys/build.rs | 8 ++------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index fbadea4..396024b 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -33,6 +33,7 @@ opencl = [] metal = [] coreml = ["metal"] +vulkan = [] mnn-threadpool = [] diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 98b0a34..0f6d21a 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -95,6 +95,8 @@ fn main() { } fn _main() -> Result<()> { + #[cfg(any(feature = "vulkan", feature = "metal", feature = "coreml"))] + compile_error!("Vulkan, Metal and CoreML are not supported currently"); 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").change_context(Error)?); @@ -169,8 +171,6 @@ fn _main() -> Result<()> { println!("cargo:rustc-link-lib=framework=CoreVideo"); #[cfg(feature = "opencl")] println!("cargo:rustc-link-lib=framework=OpenCL"); - #[cfg(feature = "opengl")] - println!("cargo:rustc-link-lib=framework=OpenGL"); } else if *TARGET_OS == "linux" { #[cfg(feature = "opencl")] { @@ -320,8 +320,6 @@ pub fn mnn_c_build(path: impl AsRef, vendor: impl AsRef) -> Result<( .pipe(|config| { #[cfg(feature = "vulkan")] config.define("MNN_VULKAN", "1"); - #[cfg(feature = "opengl")] - config.define("MNN_OPENGL", "1"); #[cfg(feature = "metal")] config.define("MNN_METAL", "1"); #[cfg(feature = "coreml")] @@ -431,8 +429,6 @@ impl CxxOption { METAL => "metal", "MNN_METAL", COREML => "coreml", "MNN_COREML", OPENCL => "opencl", "MNN_OPENCL", - OPENMP => "openmp", "MNN_OPENMP", - OPENGL => "opengl", "MNN_OPENGL", CRT_STATIC => "crt_static", "MNN_WIN_RUNTIME_MT", SPARSE_COMPUTE => "sparse-compute", "MNN_USE_SPARSE_COMPUTE", THREADPOOL => "mnn-threadpool", "MNN_USE_THREAD_POOL", From 7f8698adc26fb860090cd085648f2eae67764217 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 16:18:55 +0530 Subject: [PATCH 21/42] feat(API): Don't need &mut anymore to run / create session --- .github/workflows/build.yaml | 6 ++++ Cargo.lock | 2 -- Cargo.toml | 16 ++++++---- mnn-sys/Cargo.toml | 5 ++- mnn-sys/build.rs | 22 +++++++------- src/interpreter.rs | 36 +++++++++++----------- src/schedule.rs | 9 ------ src/session.rs | 8 ++--- tests/basic.rs | 10 ++---- tests/resizing.rs | 4 ++- tests/segfault.rs | 59 +++++++++++++++++------------------- 11 files changed, 86 insertions(+), 91 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index e4ec421..4f6d7ef 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -32,6 +32,9 @@ jobs: matrix: ${{fromJSON(needs.checks-matrix.outputs.matrix)}} steps: - uses: actions/checkout@v4 + with: + lfs: true + submodules: 'recursive' - uses: DeterminateSystems/nix-installer-action@main - uses: DeterminateSystems/magic-nix-cache-action@main - run: nix build -L '.#${{ matrix.attr }}' @@ -44,6 +47,9 @@ jobs: steps: - uses: actions/checkout@v4 + with: + lfs: true + submodules: 'recursive' - uses: DeterminateSystems/nix-installer-action@main - uses: DeterminateSystems/magic-nix-cache-action@main diff --git a/Cargo.lock b/Cargo.lock index 5763cad..dce3756 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -491,9 +491,7 @@ dependencies = [ "divan", "dunce", "error-stack", - "libc", "mnn-sys", - "oneshot", "thiserror", "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 9634e38..10b8b31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,11 +18,12 @@ mnn = { version = "0.3.0", path = "." } error-stack = { version = "0.5" } [dependencies] -libc = "0.2" -mnn-sys = { version = "0.2", path = "mnn-sys", features = [] } +mnn-sys = { version = "0.2", path = "mnn-sys", default-features = false, features = [ + "mnn-threadpool", + "sparse-compute", +] } thiserror = "2" error-stack.workspace = true -oneshot = "0.1" tracing = { version = "0.1.40", optional = true } dunce = "1.0.5" @@ -32,13 +33,16 @@ opencl = ["mnn-sys/opencl"] metal = ["mnn-sys/metal"] coreml = ["mnn-sys/coreml"] +vulkan = [] # This is currently unimplemented + crt_static = ["mnn-sys/crt_static"] -# Disable mnn-threadpool to enable this -mnn-threadpool = ["mnn-sys/mnn-threadpool"] + tracing = ["dep:tracing"] profile = ["tracing"] -default = ["mnn-threadpool"] +simd = ["mnn-sys/simd"] + +default = ["simd"] [dev-dependencies] diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index 396024b..3ab1079 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -43,10 +43,13 @@ sparse-compute = [] arm82 = [] bf16 = [] cpu-weight-dequant-gemm = [] + # Disable if you don't plan to use cpu backend and want quicker compilation sse = [] avx512 = [] neon = [] +simd = ["sse", "avx512", "neon"] + low-memory = [] -default = ["mnn-threadpool", "sparse-compute", "sse", "neon", "opencl"] +default = ["mnn-threadpool", "sparse-compute", "opencl", "simd"] diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 0f6d21a..9f2ac1b 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -31,9 +31,11 @@ static TARGET_FEATURES: LazyLock> = LazyLock::new(|| { static TARGET_OS: LazyLock = LazyLock::new(|| std::env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set")); + static TARGET_ARCH: LazyLock = LazyLock::new(|| { std::env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not found") }); + static EMSCRIPTEN_CACHE: LazyLock = LazyLock::new(|| { let emscripten_cache = std::process::Command::new("em-config") .arg("CACHE") @@ -180,7 +182,6 @@ fn _main() -> Result<()> { } } if is_emscripten() { - // println!("cargo:rustc-link-lib=static=stdc++"); let emscripten_cache = std::process::Command::new("em-config") .arg("CACHE") .output() @@ -526,7 +527,7 @@ fn read_dir(input: impl AsRef) -> impl Iterator { ignore::WalkBuilder::new(input) .max_depth(Some(1)) .build() - .filter_map(Result::ok) + .flatten() .map(|e| e.into_path()) } @@ -559,12 +560,11 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { let core_files_dir = vendor.join("source").join("core"); let core_files = ignore::Walk::new(&core_files_dir) - .filter_map(Result::ok) + .flatten() .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); build.files(core_files); - // #[cfg(feature = "cpu")] { let cpu_files_dir = vendor.join("source").join("backend").join("cpu"); let cpu_files = ignore::WalkBuilder::new(&cpu_files_dir) @@ -573,7 +573,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { .add_custom_ignore_filename("CPUImageProcess.hpp") .add_custom_ignore_filename("CPUImageProcess.cpp") .build() - .filter_map(Result::ok) + .flatten() .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); @@ -597,7 +597,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { { let cv_files_dir = vendor.join("source").join("cv"); let cv_files = ignore::Walk::new(&cv_files_dir) - .filter_map(Result::ok) + .flatten() .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); // build.include(cv_files_dir.join("schema").join("current")); @@ -613,8 +613,8 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { .add(vendor.join("source").join("geometry")) .add(vendor.join("source").join("utils")) .build() - .filter_map(Result::ok) - .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) + .flatten() + .filter(|p| cpp_filter(p.path())) .map(|e| e.into_path()); build.files(extra_files); } @@ -623,7 +623,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { { let opencl_files_dir = vendor.join("source").join("backend").join("opencl"); let opencl_files = ignore::Walk::new(&opencl_files_dir) - .filter_map(Result::ok) + .flatten() .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) .map(|e| e.into_path()); let ocl_includes = opencl_files_dir.join("schema").join("current"); @@ -654,7 +654,7 @@ fn arm(build: &mut cc::Build, arm_dir: impl AsRef) -> Result<&mut cc::Buil if *TARGET_POINTER_WIDTH == 64 { let arm64_sources_dir = arm_source_dir.join("arm64"); let arm64_sources = ignore::Walk::new(&arm64_sources_dir) - .filter_map(Result::ok) + .flatten() .filter(|e| { e.path().extension() == Some(OsStr::new("S")) || e.path().extension() == Some(OsStr::new("s")) @@ -672,7 +672,7 @@ fn arm(build: &mut cc::Build, arm_dir: impl AsRef) -> Result<&mut cc::Buil } else if *TARGET_POINTER_WIDTH == 32 { let arm32_sources_dir = arm_source_dir.join("arm32"); let arm32_sources = ignore::Walk::new(&arm32_sources_dir) - .filter_map(Result::ok) + .flatten() .filter(|e| { e.path().extension() == Some(OsStr::new("S")) || e.path().extension() == Some(OsStr::new("s")) diff --git a/src/interpreter.rs b/src/interpreter.rs index f78e10d..0a8d1c5 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -241,9 +241,9 @@ impl Interpreter { /// /// return: the created session pub fn create_session( - &mut self, + &self, schedule: crate::ScheduleConfig, - ) -> Result { + ) -> Result> { profile!("Creating session"; { let session = unsafe { mnn_sys::Interpreter_createSession(self.inner, schedule.inner) }; assert!(!session.is_null()); @@ -270,7 +270,7 @@ impl Interpreter { /// /// return: the created session pub fn create_multipath_session( - &mut self, + &self, schedule: impl IntoIterator, ) -> Result { profile!("Creating multipath session"; { @@ -436,7 +436,7 @@ impl Interpreter { } /// Run a session - pub fn run_session(&mut self, session: &crate::session::Session) -> Result<()> { + pub fn run_session(&self, session: &crate::session::Session) -> Result<()> { profile!("Running session"; { let ret = unsafe { mnn_sys::Interpreter_runSession(self.inner, session.inner) }; ensure!( @@ -457,7 +457,7 @@ impl Interpreter { /// /// `sync` : synchronously wait for finish of execution or not. pub fn run_session_with_callback( - &mut self, + &self, session: &crate::session::Session, before: impl Fn(&[RawTensor], OperatorInfo) -> bool + 'static, end: impl Fn(&[RawTensor], OperatorInfo) -> bool + 'static, @@ -510,7 +510,7 @@ impl Interpreter { } /// Update cache file - pub fn update_cache_file(&mut self, session: &mut crate::session::Session) -> Result<()> { + pub fn update_cache_file(&self, session: &mut crate::session::Session) -> Result<()> { MNNError::from_error_code(unsafe { mnn_sys::Interpreter_updateCacheFile(self.inner, session.inner) }); @@ -820,7 +820,6 @@ impl OperatorInfo<'_> { } #[test] -#[ignore = "This test doesn't work in CI"] fn test_run_session_with_callback_info_api() { let file = Path::new("tests/assets/realesr.mnn") .canonicalize() @@ -838,7 +837,6 @@ fn test_run_session_with_callback_info_api() { } #[test] -#[ignore = "This test doesn't work in CI"] fn check_whether_sync_actually_works() { let file = Path::new("tests/assets/realesr.mnn") .canonicalize() @@ -868,14 +866,14 @@ fn check_whether_sync_actually_works() { assert!((time - time2) > std::time::Duration::from_millis(50)); } -#[test] -#[ignore = "Fails on CI"] -fn try_to_drop_interpreter_before_session() { - let file = Path::new("tests/assets/realesr.mnn") - .canonicalize() - .unwrap(); - let mut interpreter = Interpreter::from_file(&file).unwrap(); - let session = interpreter.create_session(ScheduleConfig::new()).unwrap(); - drop(interpreter); - drop(session); -} +// Impossible to compile +// #[test] +// fn try_to_drop_interpreter_before_session() { +// let file = Path::new("tests/assets/realesr.mnn") +// .canonicalize() +// .unwrap(); +// let mut interpreter = Interpreter::from_file(&file).unwrap(); +// let session = interpreter.create_session(ScheduleConfig::new()).unwrap(); +// drop(interpreter); +// drop(session); +// } diff --git a/src/schedule.rs b/src/schedule.rs index 875ec67..ebfb21c 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -16,7 +16,6 @@ use crate::{prelude::*, BackendConfig}; /// - `CPU`: Use the CPU for computation. /// - `Metal`: Use the Metal backend for computation (requires the `metal` feature). /// - `OpenCL`: Use the OpenCL backend for computation (requires the `opencl` feature). -/// - `OpenGL`: Use the OpenGL backend for computation (requires the `opengl` feature). /// - `Vulkan`: Use the Vulkan backend for computation (requires the `vulkan` feature). /// - `CoreML`: Use the CoreML backend for computation (requires the `coreml` feature). /// @@ -43,8 +42,6 @@ pub enum ForwardType { Metal, #[cfg(feature = "opencl")] OpenCL, - #[cfg(feature = "opengl")] - OpenGL, #[cfg(feature = "vulkan")] Vulkan, #[cfg(feature = "coreml")] @@ -62,8 +59,6 @@ impl ForwardType { ForwardType::Metal => MNNForwardType::MNN_FORWARD_METAL, #[cfg(feature = "opencl")] ForwardType::OpenCL => MNNForwardType::MNN_FORWARD_OPENCL, - #[cfg(feature = "opengl")] - ForwardType::OpenGL => MNNForwardType::MNN_FORWARD_OPENGL, #[cfg(feature = "vulkan")] ForwardType::Vulkan => MNNForwardType::MNN_FORWARD_VULKAN, #[cfg(feature = "coreml")] @@ -80,8 +75,6 @@ impl ForwardType { "metal", #[cfg(feature = "opencl")] "opencl", - #[cfg(feature = "opengl")] - "opengl", #[cfg(feature = "vulkan")] "vulkan", #[cfg(feature = "coreml")] @@ -102,8 +95,6 @@ impl core::str::FromStr for ForwardType { "metal" => Ok(ForwardType::Metal), #[cfg(feature = "opencl")] "opencl" => Ok(ForwardType::OpenCL), - #[cfg(feature = "opengl")] - "opengl" => Ok(ForwardType::OpenGL), #[cfg(feature = "vulkan")] "vulkan" => Ok(ForwardType::Vulkan), #[cfg(feature = "coreml")] diff --git a/src/session.rs b/src/session.rs index 5be36db..20865a6 100644 --- a/src/session.rs +++ b/src/session.rs @@ -4,7 +4,7 @@ use crate::prelude::*; /// /// Inference unit. multiple sessions could share one net/interpreter. #[derive(Debug)] -pub struct Session { +pub struct Session<'i> { /// Pointer to the underlying MNN session. pub(crate) inner: *mut mnn_sys::Session, /// Pointer to the underlying MNN interpreter @@ -17,7 +17,7 @@ pub struct Session { /// Internal session configurations. pub(crate) __session_internals: crate::SessionInternals, /// Marker to ensure the struct is not Send or Sync. - pub(crate) __marker: PhantomData<()>, + pub(crate) __marker: PhantomData<&'i ()>, } /// Enum representing the internal configurations of a session. @@ -29,7 +29,7 @@ pub enum SessionInternals { MultiSession(crate::ScheduleConfigs), } -impl Session { +impl Session<'_> { // pub unsafe fn from_ptr(session: *mut mnn_sys::Session) -> Self { // Self { // session, @@ -49,7 +49,7 @@ impl Session { } } -impl Drop for Session { +impl Drop for Session<'_> { /// Custom drop implementation to ensure the underlying MNN session is properly destroyed. fn drop(&mut self) { self.destroy(); diff --git a/tests/basic.rs b/tests/basic.rs index 420ab95..bc04727 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -6,15 +6,15 @@ use mnn::ForwardType; fn test_basic_cpu() { test_basic(ForwardType::CPU).unwrap(); } + #[cfg(feature = "metal")] #[test] -#[ignore = "Doesn't work on ci"] fn test_basic_metal() { test_basic(ForwardType::Metal).unwrap(); } + #[cfg(feature = "opencl")] #[test] -#[ignore = "Doesn't work on ci"] fn test_basic_opencl() -> Result<(), Box> { let backend = ForwardType::OpenCL; let realesr = std::path::Path::new("tests/assets/realesr.mnn"); @@ -46,16 +46,12 @@ fn test_basic_opencl() -> Result<(), Box> { // drop(net); Ok(()) } + #[cfg(feature = "coreml")] #[test] fn test_basic_coreml() { test_basic(ForwardType::CoreML).unwrap(); } -#[cfg(feature = "opengl")] -#[test] -fn test_basic_opengl() { - test_basic(ForwardType::OpenGL).unwrap(); -} #[test] #[ignore = "takes too long and unreliable on CI"] diff --git a/tests/resizing.rs b/tests/resizing.rs index f3e4753..69afa31 100644 --- a/tests/resizing.rs +++ b/tests/resizing.rs @@ -6,9 +6,11 @@ pub fn test_resizing() -> Result<()> { let model = std::fs::read("tests/assets/resizing.mnn").expect("No resizing model"); let mut net = Interpreter::from_bytes(&model).unwrap(); net.set_cache_file("resizing.cache", 128)?; - let config = ScheduleConfig::default(); + let mut config = ScheduleConfig::default(); #[cfg(feature = "opencl")] config.set_type(ForwardType::OpenCL); + #[cfg(not(feature = "opencl"))] + config.set_type(ForwardType::CPU); let mut session = net.create_session(config).unwrap(); net.update_cache_file(&mut session)?; diff --git a/tests/segfault.rs b/tests/segfault.rs index d061941..63a2f83 100644 --- a/tests/segfault.rs +++ b/tests/segfault.rs @@ -28,43 +28,40 @@ fn test_segfault_case_1_() -> Result<(), Box> { } #[test] -#[ignore] -pub fn test_resizing() { +pub fn test_segfault_case_2_() { use mnn::*; let model = std::fs::read("tests/assets/resizing.mnn").expect("No resizing model"); let mut net = Interpreter::from_bytes(&model).unwrap(); let config = ScheduleConfig::default(); let mut session = net.create_session(config).unwrap(); - loop { - let inputs = net.inputs(&session); - for tensor_info in inputs.iter() { - let mut tensor = unsafe { tensor_info.tensor_unresized::() }.unwrap(); - let mut shape = tensor.shape().as_ref().to_vec(); - dbg!(&shape); - shape.iter_mut().for_each(|v| { - if *v == -1 { - *v = 3; - } - }); - dbg!(&shape); - net.resize_tensor(&mut tensor, &shape); - } - drop(inputs); + let inputs = net.inputs(&session); + for tensor_info in inputs.iter() { + let mut tensor = unsafe { tensor_info.tensor_unresized::() }.unwrap(); + let mut shape = tensor.shape().as_ref().to_vec(); + dbg!(&shape); + shape.iter_mut().for_each(|v| { + if *v == -1 { + *v = 3; + } + }); + dbg!(&shape); + net.resize_tensor(&mut tensor, &shape); + } + drop(inputs); - net.resize_session(&mut session); - let inputs = net.inputs(&session); - for tensor_info in inputs.iter() { - let tensor = tensor_info.tensor::().unwrap(); - println!( - "{:13}: {:>13}", - tensor_info.name(), - format!("{:?}", tensor.shape()) - ); - let mut host = tensor.create_host_tensor_from_device(false); - host.host_mut().fill(1.0); - } - drop(inputs); - net.run_session(&session).unwrap(); + net.resize_session(&mut session); + let inputs = net.inputs(&session); + for tensor_info in inputs.iter() { + let tensor = tensor_info.tensor::().unwrap(); + println!( + "{:13}: {:>13}", + tensor_info.name(), + format!("{:?}", tensor.shape()) + ); + let mut host = tensor.create_host_tensor_from_device(false); + host.host_mut().fill(1.0); } + drop(inputs); + net.run_session(&session).unwrap(); } From 2b8c4e5e60eee4ce323de8cf0e0bb383e3f52b1f Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 16:24:55 +0530 Subject: [PATCH 22/42] chore: Cargo clippy fixes --- benches/mnn-bench.rs | 4 ++-- examples/inspect.rs | 20 ++++++++++---------- examples/simple.rs | 12 ++++++------ src/interpreter.rs | 10 +++++----- tests/basic.rs | 2 +- tests/common.rs | 4 ++-- tests/resizing.rs | 2 +- tests/segfault.rs | 4 ++-- 8 files changed, 29 insertions(+), 29 deletions(-) diff --git a/benches/mnn-bench.rs b/benches/mnn-bench.rs index 8616f3a..24c5197 100644 --- a/benches/mnn-bench.rs +++ b/benches/mnn-bench.rs @@ -5,7 +5,7 @@ mod mnn_realesr_bench_with_ones { use mnn::*; #[divan::bench] pub fn mnn_benchmark_cpu(bencher: Bencher) { - let mut net = Interpreter::from_file("tests/assets/realesr.mnn").unwrap(); + let net = Interpreter::from_file("tests/assets/realesr.mnn").unwrap(); let mut config = ScheduleConfig::new(); config.set_type(ForwardType::CPU); let session = net.create_session(config).unwrap(); @@ -19,7 +19,7 @@ mod mnn_realesr_bench_with_ones { #[cfg(feature = "opencl")] #[divan::bench] pub fn mnn_benchmark_opencl(bencher: Bencher) { - let mut net = Interpreter::from_file("tests/assets/realesr.mnn").unwrap(); + let net = Interpreter::from_file("tests/assets/realesr.mnn").unwrap(); let mut config = ScheduleConfig::new(); config.set_type(ForwardType::OpenCL); let session = net.create_session(config).unwrap(); diff --git a/examples/inspect.rs b/examples/inspect.rs index f7ffb9c..151e037 100644 --- a/examples/inspect.rs +++ b/examples/inspect.rs @@ -45,25 +45,25 @@ macro_rules! time { pub fn main() -> anyhow::Result<()> { use clap::Parser; let cli = Cli::parse(); - let mut interpreter = Interpreter::from_file(&cli.model)?; - interpreter.set_cache_file(cli.model.with_extension("cache"), 128)?; + let net = Interpreter::from_file(&cli.model)?; + net.set_cache_file(cli.model.with_extension("cache"), 128)?; let mut config = ScheduleConfig::new(); config.set_type(cli.forward); - let mut session = time!(interpreter.create_session(config)?; "create session"); - interpreter.update_cache_file(&mut session)?; + let mut session = time!(net.create_session(config)?; "create session"); + net.update_cache_file(&mut session)?; let mut current = 0; println!("--------------------------------Info--------------------------------"); - let mem = interpreter.memory(&session)?; - let flops = interpreter.flops(&session)?; + let mem = net.memory(&session)?; + let flops = net.flops(&session)?; println!("Memory: {:?}MiB", mem); println!("Flops : {:?}M", flops); - println!("ResizeStatus : {:?}", interpreter.resize_status(&session)?); + println!("ResizeStatus : {:?}", net.resize_status(&session)?); time!(loop { println!("--------------------------------Inputs--------------------------------"); - interpreter.inputs(&session).iter().for_each(|x| { + net.inputs(&session).iter().for_each(|x| { match cli.input_data_type { DataType::F32 => { let mut tensor = x.tensor::().expect("No tensor"); @@ -84,9 +84,9 @@ pub fn main() -> anyhow::Result<()> { }); println!("Running session"); - interpreter.run_session(&session)?; + net.run_session(&session)?; println!("--------------------------------Outputs--------------------------------"); - let outputs = interpreter.outputs(&session); + let outputs = net.outputs(&session); outputs.iter().for_each(|x| { match cli.output_data_type { DataType::F32 => { diff --git a/examples/simple.rs b/examples/simple.rs index 11952b1..b572c4f 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -17,7 +17,7 @@ pub struct Cli { pub fn main() -> anyhow::Result<()> { use clap::Parser; let cli = Cli::parse(); - let mut interpreter = Interpreter::from_file(cli.model)?; + let net = Interpreter::from_file(cli.model)?; let mut config = ScheduleConfig::new(); config.set_type(ForwardType::CPU); @@ -27,10 +27,10 @@ pub fn main() -> anyhow::Result<()> { config.set_backend_config(backend_config); let now = std::time::Instant::now(); - let session = interpreter.create_session(config)?; + let session = net.create_session(config)?; println!("create session time: {:?}", now.elapsed()); - let mut image = interpreter.input(&session, "image")?; - let mut mask = interpreter.input(&session, "mask")?; + let mut image = net.input(&session, "image")?; + let mut mask = net.input(&session, "mask")?; let mut image_tensor = image.create_host_tensor_from_device(false); image_tensor.host_mut().fill(1.0f32); image.copy_from_host_tensor(&image_tensor)?; @@ -40,11 +40,11 @@ pub fn main() -> anyhow::Result<()> { mask.copy_from_host_tensor(&mask_tensor)?; println!("copy time: {:?}", now.elapsed()); - let output = interpreter.output(&session, "output")?; + let output = net.output(&session, "output")?; // image.copy_from_host_tensor(&unit_tensor)?; let now = std::time::Instant::now(); - interpreter.run_session(&session)?; + net.run_session(&session)?; output.wait(ffi::MapType::MAP_TENSOR_READ, true); println!("run time: {:?}", now.elapsed()); diff --git a/src/interpreter.rs b/src/interpreter.rs index 0a8d1c5..5821bb0 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -179,7 +179,7 @@ impl Interpreter { /// /// **Warning:** /// It should be called before create session! - pub fn set_session_mode(&mut self, mode: SessionMode) { + pub fn set_session_mode(&self, mode: SessionMode) { unsafe { mnn_sys::Interpreter_setSessionMode(self.inner, mode.to_mnn_sys()) } } @@ -260,7 +260,7 @@ impl Interpreter { /// # Safety /// This function is marked unsafe since it's not clear what the safety guarantees are right /// now. With a simple test it caused a segfault so it's marked unsafe - pub unsafe fn release_model(&mut self) { + pub unsafe fn release_model(&self) { unsafe { mnn_sys::Interpreter_releaseModel(self.inner) } } @@ -500,7 +500,7 @@ impl Interpreter { /// The API should be called before create session. /// /// Key Depercerate, keeping for future use! - pub fn set_cache_file(&mut self, path: impl AsRef, key_size: usize) -> Result<()> { + pub fn set_cache_file(&self, path: impl AsRef, key_size: usize) -> Result<()> { let path = path.as_ref(); let path = dunce::simplified(path); let path = path.to_str().ok_or_else(|| error!(ErrorKind::AsciiError))?; @@ -824,7 +824,7 @@ fn test_run_session_with_callback_info_api() { let file = Path::new("tests/assets/realesr.mnn") .canonicalize() .unwrap(); - let mut interpreter = Interpreter::from_file(&file).unwrap(); + let interpreter = Interpreter::from_file(&file).unwrap(); let session = interpreter.create_session(ScheduleConfig::new()).unwrap(); interpreter .run_session_with_callback( @@ -841,7 +841,7 @@ fn check_whether_sync_actually_works() { let file = Path::new("tests/assets/realesr.mnn") .canonicalize() .unwrap(); - let mut interpreter = Interpreter::from_file(&file).unwrap(); + let interpreter = Interpreter::from_file(&file).unwrap(); let session = interpreter.create_session(ScheduleConfig::new()).unwrap(); let time = std::time::Instant::now(); interpreter diff --git a/tests/basic.rs b/tests/basic.rs index bc04727..e72cc8b 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -19,7 +19,7 @@ fn test_basic_opencl() -> Result<(), Box> { let backend = ForwardType::OpenCL; let realesr = std::path::Path::new("tests/assets/realesr.mnn"); - let mut net = mnn::Interpreter::from_file(realesr)?; + let net = mnn::Interpreter::from_file(realesr)?; net.set_cache_file(realesr.with_extension("cache"), 128)?; let mut config = ScheduleConfig::new(); config.set_type(backend); diff --git a/tests/common.rs b/tests/common.rs index 57a51cb..8064d09 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -27,7 +27,7 @@ impl AsRef<[u8]> for Model { #[allow(dead_code)] pub fn test_basic(backend: ForwardType) -> Result<()> { - let mut net = mnn::Interpreter::from_file("tests/assets/realesr.mnn")?; + let net = mnn::Interpreter::from_file("tests/assets/realesr.mnn")?; let mut config = ScheduleConfig::new(); config.set_type(backend); let session = net.create_session(config)?; @@ -51,7 +51,7 @@ pub fn test_basic(backend: ForwardType) -> Result<()> { pub fn test_multipath_session(backend: ForwardType, backend2: ForwardType) -> Result<()> { use mnn::BackendConfig; - let mut net = mnn::Interpreter::from_bytes(Model::new())?; + let net = mnn::Interpreter::from_bytes(Model::new())?; let mut config = ScheduleConfig::new(); config.set_type(backend); config.set_backup_type(backend); diff --git a/tests/resizing.rs b/tests/resizing.rs index 69afa31..486bcbf 100644 --- a/tests/resizing.rs +++ b/tests/resizing.rs @@ -4,7 +4,7 @@ use common::*; #[test] pub fn test_resizing() -> Result<()> { let model = std::fs::read("tests/assets/resizing.mnn").expect("No resizing model"); - let mut net = Interpreter::from_bytes(&model).unwrap(); + let net = Interpreter::from_bytes(&model).unwrap(); net.set_cache_file("resizing.cache", 128)?; let mut config = ScheduleConfig::default(); #[cfg(feature = "opencl")] diff --git a/tests/segfault.rs b/tests/segfault.rs index 63a2f83..367ac56 100644 --- a/tests/segfault.rs +++ b/tests/segfault.rs @@ -6,7 +6,7 @@ fn test_segfault_case_1_() -> Result<(), Box> { let backend = ForwardType::OpenCL; let realesr = std::path::Path::new("tests/assets/realesr.mnn"); - let mut net = mnn::Interpreter::from_file(realesr)?; + let net = mnn::Interpreter::from_file(realesr)?; net.set_cache_file(realesr.with_extension("cache"), 128)?; let mut config = ScheduleConfig::new(); config.set_type(backend); @@ -31,7 +31,7 @@ fn test_segfault_case_1_() -> Result<(), Box> { pub fn test_segfault_case_2_() { use mnn::*; let model = std::fs::read("tests/assets/resizing.mnn").expect("No resizing model"); - let mut net = Interpreter::from_bytes(&model).unwrap(); + let net = Interpreter::from_bytes(&model).unwrap(); let config = ScheduleConfig::default(); let mut session = net.create_session(config).unwrap(); From f429533564f7f37fc6e6aab5243c160da173367f Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 16:48:22 +0530 Subject: [PATCH 23/42] feat: Cleanup don't try to compile arm stuff in x86 --- mnn-sys/build.rs | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 9f2ac1b..a0d5566 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -425,6 +425,7 @@ impl CxxOption { value: CxxOptionValue::from_bool(value), } } + cxx_option_from_features! { VULKAN => "vulkan", "MNN_VULKAN", METAL => "metal", "MNN_METAL", @@ -551,12 +552,23 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { .static_flag(true) .std("c++11"); - CxxOption::all().iter().for_each(|opt| { - eprintln!("cargo:warn={}: {}", opt.name, opt.enabled()); - if opt.enabled() { - build.define(opt.name, opt.cc()); - } - }); + // CxxOption::all().iter().for_each(|opt| { + // eprintln!("cargo:warn={}: {}", opt.name, opt.enabled()); + // if opt.enabled() { + // build.define(opt.name, opt.cc()); + // } + // }); + CxxOption::VULKAN.define(&mut build); + CxxOption::METAL.define(&mut build); + CxxOption::COREML.define(&mut build); + CxxOption::OPENCL.define(&mut build); + CxxOption::CRT_STATIC.define(&mut build); + CxxOption::SPARSE_COMPUTE.define(&mut build); + CxxOption::THREADPOOL.define(&mut build); + CxxOption::MINI_BUILD.define(&mut build); + is_arm().then(|| CxxOption::NEON.define(&mut build)); + CxxOption::LOW_MEMORY.define(&mut build); + CxxOption::CPU_WEIGHT_DEQUANT_GEMM.define(&mut build); let core_files_dir = vendor.join("source").join("core"); let core_files = ignore::Walk::new(&core_files_dir) @@ -579,11 +591,11 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { if CxxOption::ARM82.enabled() && is_arm() { build.define("ENABLE_ARMV82", None); - build.include(cpu_files_dir.join("arm")); - includes.push(cpu_files_dir.join("arm")); } if is_arm() { + build.include(cpu_files_dir.join("arm")); + includes.push(cpu_files_dir.join("arm")); arm(&mut build, cpu_files_dir.join("arm"))?; } @@ -643,13 +655,13 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { fn arm(build: &mut cc::Build, arm_dir: impl AsRef) -> Result<&mut cc::Build> { let arm_source_dir = arm_dir.as_ref(); - let mut neon_sources: Vec = vec![arm_source_dir.join("CommonOptFunctionNeon.cpp")]; - if CxxOption::BF16.enabled() { - let path = arm_source_dir.join("CommonNeonBF16.cpp"); - if path.exists() { - neon_sources.push(path); - } - } + let neon_sources: Vec = vec![arm_source_dir.join("CommonOptFunctionNeon.cpp")]; + // if CxxOption::BF16.enabled() { + // let path = arm_source_dir.join("CommonNeonBF16.cpp"); + // if path.exists() { + // neon_sources.push(path); + // } + // } if *TARGET_POINTER_WIDTH == 64 { let arm64_sources_dir = arm_source_dir.join("arm64"); @@ -664,10 +676,8 @@ fn arm(build: &mut cc::Build, arm_dir: impl AsRef) -> Result<&mut cc::Buil // MNN_LOW_MEMORY // MNN_CPU_WEIGHT_DEQUANT_GEMM - build.define("MNN_USE_NEON", None); build .files(arm64_sources.chain(neon_sources)) - .include(arm_source_dir) .define("__aarch64__", None); } else if *TARGET_POINTER_WIDTH == 32 { let arm32_sources_dir = arm_source_dir.join("arm32"); @@ -682,10 +692,8 @@ fn arm(build: &mut cc::Build, arm_dir: impl AsRef) -> Result<&mut cc::Buil // MNN_LOW_MEMORY // MNN_CPU_WEIGHT_DEQUANT_GEMM - build.define("MNN_USE_NEON", None); build .files(arm32_sources.chain(neon_sources)) - .include(arm_source_dir) .define("__arm__", None); } Ok(build) From f19ceb3fa97b6abc89bbee854fa3f7bac8c7634f Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 17:24:35 +0530 Subject: [PATCH 24/42] feat: Added checks for cpu features for sse and neon --- Cargo.lock | 1 - mnn-sys/Cargo.toml | 1 - mnn-sys/build.rs | 18 ++++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dce3756..298720d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -527,7 +527,6 @@ dependencies = [ "error-stack", "fs_extra", "ignore", - "itertools", "pkg-config", "tap", "thiserror", diff --git a/mnn-sys/Cargo.toml b/mnn-sys/Cargo.toml index 3ab1079..901c22c 100644 --- a/mnn-sys/Cargo.toml +++ b/mnn-sys/Cargo.toml @@ -23,7 +23,6 @@ cc = { version = "1.1.5", features = ["parallel"] } error-stack.workspace = true fs_extra = "1.3.0" ignore = "0.4.23" -itertools = "0.13.0" pkg-config = "0.3.31" tap = "1.0.1" thiserror = "2.0.3" diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index a0d5566..b7849b2 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -65,6 +65,11 @@ const HALIDE_SEARCH: &str = const STATIC_CRT: bool = cfg!(feature = "crt_static"); +fn has_cpu_feature(feature: impl AsRef) -> bool { + let feature = feature.as_ref(); + TARGET_FEATURES.iter().find(|f| *f == feature).is_some() +} + fn ensure_vendor_exists(vendor: impl AsRef) -> Result<()> { if vendor .as_ref() @@ -128,13 +133,9 @@ fn _main() -> Result<()> { // try_patch_file("patches/halide_type_t_64.patch", intptr) // .attach_printable("Failed to patch vendor")?; - use itertools::Itertools; let intptr_contents = std::fs::read_to_string(&intptr).change_context(Error)?; let patched = intptr_contents.lines().collect::>(); - if let Some((idx, _)) = patched - .iter() - .find_position(|line| line.contains(HALIDE_SEARCH)) - { + if let Some(idx) = patched.iter().position(|line| line.contains(HALIDE_SEARCH)) { // remove the last line and the next 3 lines let patched = patched .into_iter() @@ -438,6 +439,7 @@ impl CxxOption { ARM82 => "arm82", "MNN_ARM82", BF16 => "bf16", "MNN_SUPPORT_BF16", AVX512 => "avx512", "MNN_AVX512", + SSE => "sse", "MNN_USE_SSE", LOW_MEMORY => "low-memory", "MNN_LOW_MEMORY", NEON => "neon", "MNN_USE_NEON", CPU_WEIGHT_DEQUANT_GEMM => "cpu-weight-dequant-gemm", "MNN_CPU_WEIGHT_DEQUANT_GEMM" @@ -566,7 +568,8 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { CxxOption::SPARSE_COMPUTE.define(&mut build); CxxOption::THREADPOOL.define(&mut build); CxxOption::MINI_BUILD.define(&mut build); - is_arm().then(|| CxxOption::NEON.define(&mut build)); + (is_arm() && has_cpu_feature("neon")).then(|| CxxOption::NEON.define(&mut build)); + (is_x86() && has_cpu_feature("sse")).then(|| CxxOption::SSE.define(&mut build)); CxxOption::LOW_MEMORY.define(&mut build); CxxOption::CPU_WEIGHT_DEQUANT_GEMM.define(&mut build); @@ -599,7 +602,7 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { arm(&mut build, cpu_files_dir.join("arm"))?; } - if TARGET_FEATURES.contains(&("sse".into())) && is_x86() && cfg!(feature = "sse") { + if has_cpu_feature("sse") && is_x86() && CxxOption::SSE.enabled() { x86_64(&mut build, &includes, cpu_files_dir.join("x86_x64"))?; } @@ -708,7 +711,6 @@ fn x86_64<'a>( let like_msvc = build.get_compiler().is_like_msvc(); let win_use_asm = like_msvc && *TARGET_POINTER_WIDTH == 64 && mnn_assembler.is_some(); let has_avx512 = target_has_avx512(); - build.define("MNN_USE_SSE", None); let x86_src_dir = x86_64_dir.as_ref(); let mnn_x8664_src = read_dir(&x86_src_dir).filter(|p| cpp_filter(p)); let mnn_avx_src = read_dir(x86_src_dir.join("avx")).filter(|p| cpp_filter(p) || asm_filter(p)); From 29b56a5b4eaef3053210e5b0422edd1bb13df5ab Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 13:48:16 +0100 Subject: [PATCH 25/42] fix: Fix issue with cargo-llvm-cov --- flake.lock | 24 +++++++------- flake.nix | 94 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 60 insertions(+), 58 deletions(-) diff --git a/flake.lock b/flake.lock index 145cdaf..82b38ae 100644 --- a/flake.lock +++ b/flake.lock @@ -3,11 +3,11 @@ "advisory-db": { "flake": false, "locked": { - "lastModified": 1730464311, - "narHash": "sha256-9xJoP1766XJSO1Qr0Lxg2P6dwPncTr3BJYlFMSXBd/E=", + "lastModified": 1731271136, + "narHash": "sha256-VsrCHM1gP8YqBTQWBQ0TmFNAFv3lBA0PvtWh8/sA9n4=", "owner": "rustsec", "repo": "advisory-db", - "rev": "f3460e5ed91658ab94fa41908cfa44991f9f4f02", + "rev": "509528f6775ad69ab114f1e4b37b4359cae5cef4", "type": "github" }, "original": { @@ -18,11 +18,11 @@ }, "crane": { "locked": { - "lastModified": 1730652660, - "narHash": "sha256-+XVYfmVXAiYA0FZT7ijHf555dxCe+AoAT5A6RU+6vSo=", + "lastModified": 1731098351, + "narHash": "sha256-HQkYvKvaLQqNa10KEFGgWHfMAbWBfFp+4cAgkut+NNE=", "owner": "ipetkov", "repo": "crane", - "rev": "a4ca93905455c07cb7e3aca95d4faf7601cba458", + "rev": "ef80ead953c1b28316cc3f8613904edc2eb90c28", "type": "github" }, "original": { @@ -145,11 +145,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1730785428, - "narHash": "sha256-Zwl8YgTVJTEum+L+0zVAWvXAGbWAuXHax3KzuejaDyo=", + "lastModified": 1731139594, + "narHash": "sha256-IigrKK3vYRpUu+HEjPL/phrfh7Ox881er1UEsZvw9Q4=", "owner": "nixos", "repo": "nixpkgs", - "rev": "4aa36568d413aca0ea84a1684d2d46f55dbabad7", + "rev": "76612b17c0ce71689921ca12d9ffdc9c23ce40b2", "type": "github" }, "original": { @@ -178,11 +178,11 @@ ] }, "locked": { - "lastModified": 1731292155, - "narHash": "sha256-fYVoUUtSadbOrH0z0epVQDsStBDS/S/fAK//0ECQAAI=", + "lastModified": 1731464916, + "narHash": "sha256-WZ5rpjr/wCt7yBOUsvDE2i22hYz9g8W921jlwVktRQ4=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "7c4cd99ed7604b79e8cb721099ac99c66f656b3a", + "rev": "2c19bad6e881b5a154cafb7f9106879b5b356d1f", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 1cf54bb..e3b4457 100644 --- a/flake.nix +++ b/flake.nix @@ -65,58 +65,57 @@ lockFile = "${src}/Cargo.lock"; }; }; + cargo-llvm-cov = prev.cargo-llvm-cov.overrideAttrs (_: { + LLVM_COV = "${final.llvmPackages_19.llvm}/bin/llvm-cov"; + LLVM_PROFDATA = "${final.llvmPackages_19.llvm}/bin/llvm-profdata"; + }); }) ]; }; inherit (pkgs) lib; - stableToolchain = pkgs.rust-bin.stable.latest.default; - nightlyToolchain = pkgs.rust-bin.nightly.latest.default.override { - extensions = ["rust-src" "rust-analyzer"]; - }; - stableToolchainWithLLvmTools = pkgs.rust-bin.stable.latest.default.override { + version = "1.80.0"; + + rustToolchain = pkgs.rust-bin.stable.${version}.default; + rustToolchainWithLLvmTools = pkgs.rust-bin.stable.${version}.default.override { extensions = ["rust-src" "llvm-tools"]; }; - stableToolchainWithRustAnalyzer = pkgs.rust-bin.stable.latest.default.override ({ + rustToolchainWithRustAnalyzer = pkgs.rust-bin.stable.${version}.default.override ({ extensions = ["rust-src" "rust-analyzer"]; } // (lib.optionalAttrs pkgs.stdenv.isDarwin { targets = ["aarch64-apple-darwin" "x86_64-apple-darwin"]; })); - craneLib = (crane.mkLib pkgs).overrideToolchain stableToolchain; - craneLibLLvmTools = (crane.mkLib pkgs).overrideToolchain stableToolchainWithLLvmTools; + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; + craneLibLLvmTools = (crane.mkLib pkgs).overrideToolchain rustToolchainWithLLvmTools; src = lib.sources.sourceFilesBySuffices ./. [".rs" ".toml" ".patch" ".mnn" ".h" ".cpp" ".svg" "lock"]; MNN_SRC = mnn-src; - commonArgs = - { - inherit src MNN_SRC; - pname = "mnn"; - doCheck = false; - LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; - nativeBuildInputs = with pkgs; [ - cmake - llvmPackages.libclang.lib - clang - pkg-config - ]; - buildInputs = with pkgs; - [] - ++ (lib.optionals pkgs.stdenv.isLinux [ - ocl-icd - opencl-headers - ]) - ++ (lib.optionals pkgs.stdenv.isDarwin [ - darwin.apple_sdk.frameworks.OpenCL - ] - ++ (lib.optionals pkgs.stdenv.isAarch64 [ - darwin.apple_sdk.frameworks.Metal - darwin.apple_sdk.frameworks.CoreML - ])); - } - // (lib.optionalAttrs pkgs.stdenv.isLinux { - BINDGEN_EXTRA_CLANG_ARGS = "-I${pkgs.llvmPackages.libclang.lib}/lib/clang/18/include"; - }); + commonArgs = { + inherit src MNN_SRC; + pname = "mnn"; + doCheck = false; + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + nativeBuildInputs = with pkgs; [ + cmake + llvmPackages.libclang.lib + clang + pkg-config + ]; + buildInputs = with pkgs; + [] + ++ (lib.optionals pkgs.stdenv.isLinux [ + ocl-icd + opencl-headers + ]) + ++ (lib.optionals pkgs.stdenv.isDarwin [ + darwin.apple_sdk.frameworks.OpenCL + ] + ++ (lib.optionals pkgs.stdenv.isAarch64 [ + darwin.apple_sdk.frameworks.Metal + darwin.apple_sdk.frameworks.CoreML + ])); + }; cargoArtifacts = craneLib.buildPackage commonArgs; in { checks = { @@ -214,18 +213,18 @@ // { packages = with pkgs; [ + cargo-audit + cargo-deny + cargo-hakari + cargo-nextest + cargo-semver-checks clang - nushell git git-lfs - stableToolchainWithRustAnalyzer - cargo-nextest - cargo-hakari - cargo-deny - # cargo-audit - cargo-semver-checks - rust-bindgen llvm + nushell + rust-bindgen + rustToolchainWithRustAnalyzer ] ++ (lib.optionals pkgs.stdenv.isDarwin [ darwin.apple_sdk.frameworks.OpenCL @@ -233,7 +232,10 @@ ++ (lib.optionals pkgs.stdenv.isAarch64 [ darwin.apple_sdk.frameworks.Metal darwin.apple_sdk.frameworks.CoreML - ])); + ])) + ++ (lib.optionals pkgs.stdenv.isLinux [ + cargo-llvm-cov + ]); }); }; } From c0b708fd184e4e410a081e65fb66982742d578a6 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 13:51:56 +0100 Subject: [PATCH 26/42] feat: use latest rust toolchain --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index e3b4457..6643388 100644 --- a/flake.nix +++ b/flake.nix @@ -74,7 +74,7 @@ }; inherit (pkgs) lib; - version = "1.80.0"; + version = "latest"; rustToolchain = pkgs.rust-bin.stable.${version}.default; rustToolchainWithLLvmTools = pkgs.rust-bin.stable.${version}.default.override { From bdcb0029ea1d213024834ffe26329a265ecbcb5b Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 13 Nov 2024 19:06:54 +0530 Subject: [PATCH 27/42] feat: Added more flags for msvc --- mnn-sys/build.rs | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index b7849b2..dafe8e2 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -67,7 +67,7 @@ const STATIC_CRT: bool = cfg!(feature = "crt_static"); fn has_cpu_feature(feature: impl AsRef) -> bool { let feature = feature.as_ref(); - TARGET_FEATURES.iter().find(|f| *f == feature).is_some() + TARGET_FEATURES.iter().any(|f| *f == feature) } fn ensure_vendor_exists(vendor: impl AsRef) -> Result<()> { @@ -148,7 +148,7 @@ fn _main() -> Result<()> { } if *MNN_COMPILE { - build_cpp_build(&vendor)?; + mnn_cpp_build(&vendor)?; } else if let core::result::Result::Ok(lib_dir) = std::env::var("MNN_LIB_DIR") { println!("cargo:rustc-link-search=native={}", lib_dir); println!("cargo:rustc-link-lib=static=MNN"); @@ -534,7 +534,7 @@ fn read_dir(input: impl AsRef) -> impl Iterator { .map(|e| e.into_path()) } -pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { +pub fn mnn_cpp_build(vendor: impl AsRef) -> Result<()> { let mut build = cc::Build::new(); let vendor = vendor.as_ref(); let mut includes = vec![ @@ -554,12 +554,38 @@ pub fn build_cpp_build(vendor: impl AsRef) -> Result<()> { .static_flag(true) .std("c++11"); - // CxxOption::all().iter().for_each(|opt| { - // eprintln!("cargo:warn={}: {}", opt.name, opt.enabled()); - // if opt.enabled() { - // build.define(opt.name, opt.cc()); - // } - // }); + if cfg!(unix) { + build + .flag_if_supported("-Wno-format") + .flag_if_supported("-Wno-ignored-qualifiers") + .flag_if_supported("-Wno-sign-compare") + .flag_if_supported("-Wno-unused-but-set-variable") + .flag_if_supported("-Wno-unused-function") + .flag_if_supported("-Wno-unused-lambda-capture") + .flag_if_supported("-Wno-unused-local-typedef") + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-private-field") + .flag_if_supported("-Wno-unused-variable"); + } + if cfg!(windows) { + build + .flag_if_supported("/wd4267") + .flag_if_supported("/wd4018") + .flag_if_supported("/wd4251") + .flag_if_supported("/wd4996") + .flag_if_supported("/wd4244") + .flag_if_supported("/wd4146") + .flag_if_supported("/wd4129") + .flag_if_supported("/wd4305") + .flag_if_supported("/wd4275") + .flag_if_supported("/wd4101"); + } + + let like_msvc = build.get_compiler().is_like_msvc(); + if like_msvc { + build.flag_if_supported("/source-charset:utf-8"); + } + CxxOption::VULKAN.define(&mut build); CxxOption::METAL.define(&mut build); CxxOption::COREML.define(&mut build); From bb42fef31be1290cee21fe772c29f6278f53a930 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 18 Nov 2024 15:56:59 +0530 Subject: [PATCH 28/42] feat: Use upstream cargo-llvm-cov and add cachix --- .github/workflows/build.yaml | 6 ++++-- flake.lock | 6 +++--- flake.nix | 5 +---- mnn-sync/src/lib.rs | 6 +++--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 4f6d7ef..925a47c 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -17,7 +17,6 @@ jobs: steps: - uses: actions/checkout@v4 - uses: DeterminateSystems/nix-installer-action@main - - uses: DeterminateSystems/magic-nix-cache-action@main - id: set-matrix name: Generate Nix Matrix run: | @@ -36,7 +35,10 @@ jobs: lfs: true submodules: 'recursive' - uses: DeterminateSystems/nix-installer-action@main - - uses: DeterminateSystems/magic-nix-cache-action@main + - uses: cachix/cachix-action@v14 + with: + name: mnn-rs + authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' - run: nix build -L '.#${{ matrix.attr }}' codecov: diff --git a/flake.lock b/flake.lock index 82b38ae..eec96e3 100644 --- a/flake.lock +++ b/flake.lock @@ -145,11 +145,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1731139594, - "narHash": "sha256-IigrKK3vYRpUu+HEjPL/phrfh7Ox881er1UEsZvw9Q4=", + "lastModified": 1731676054, + "narHash": "sha256-OZiZ3m8SCMfh3B6bfGC/Bm4x3qc1m2SVEAlkV6iY7Yg=", "owner": "nixos", "repo": "nixpkgs", - "rev": "76612b17c0ce71689921ca12d9ffdc9c23ce40b2", + "rev": "5e4fbfb6b3de1aa2872b76d49fafc942626e2add", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 6643388..07b5971 100644 --- a/flake.nix +++ b/flake.nix @@ -65,10 +65,6 @@ lockFile = "${src}/Cargo.lock"; }; }; - cargo-llvm-cov = prev.cargo-llvm-cov.overrideAttrs (_: { - LLVM_COV = "${final.llvmPackages_19.llvm}/bin/llvm-cov"; - LLVM_PROFDATA = "${final.llvmPackages_19.llvm}/bin/llvm-profdata"; - }); }) ]; }; @@ -222,6 +218,7 @@ git git-lfs llvm + mnn nushell rust-bindgen rustToolchainWithRustAnalyzer diff --git a/mnn-sync/src/lib.rs b/mnn-sync/src/lib.rs index 65b0ff9..1957da2 100644 --- a/mnn-sync/src/lib.rs +++ b/mnn-sync/src/lib.rs @@ -127,7 +127,7 @@ impl SessionRunnerState { tracing::info!("Unloading session"); match core::mem::take(self) { Self::Loaded(sr) => { - let net = sr.unload()?; + let net = sr.unload(); *self = Self::Unloaded(net); Ok(()) } @@ -221,11 +221,11 @@ impl SessionRunner { }) } - pub fn unload(self) -> Result { + pub fn unload(self) -> mnn::Interpreter { let session = self.session; let net = self.interpreter; drop(session); - Ok(net) + net } pub fn run_session(&mut self) -> Result<()> { From 14eedf39dc495bfdf7f292c7dbeabbf81d62e063 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 18 Nov 2024 18:05:46 +0530 Subject: [PATCH 29/42] feat: Move cargo-llvm-cov to checks and upload to cachix feat: Added script to upload to cachix easily --- .github/workflows/build.yaml | 7 +- .github/workflows/docs.yaml | 5 +- flake.nix | 156 +++++++++++++++++------------------ tools/cachix/Makefile | 15 ++++ tools/cachix/push.sh | 2 + 5 files changed, 104 insertions(+), 81 deletions(-) create mode 100644 tools/cachix/Makefile create mode 100755 tools/cachix/push.sh diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 925a47c..f98748a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -53,10 +53,13 @@ jobs: lfs: true submodules: 'recursive' - uses: DeterminateSystems/nix-installer-action@main - - uses: DeterminateSystems/magic-nix-cache-action@main + - uses: cachix/cachix-action@v14 + with: + name: mnn-rs + authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' - name: Run codecov - run: nix build .#mnn-llvm-cov + run: nix build .#checks.x86_64-linux.mnn-llvm-cov - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4.0.1 diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index 071fb75..f1e3ef6 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -18,7 +18,10 @@ jobs: steps: - uses: actions/checkout@v4 - uses: DeterminateSystems/nix-installer-action@main - - uses: DeterminateSystems/magic-nix-cache-action@main + - uses: cachix/cachix-action@v14 + with: + name: mnn-rs + authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' - uses: DeterminateSystems/flake-checker-action@main - name: Generate docs diff --git a/flake.nix b/flake.nix index 07b5971..f60d9e3 100644 --- a/flake.nix +++ b/flake.nix @@ -114,95 +114,95 @@ }; cargoArtifacts = craneLib.buildPackage commonArgs; in { - checks = { - mnn-clippy = craneLib.cargoClippy (commonArgs - // { - inherit cargoArtifacts; - cargoClippyExtraArgs = "--all-targets -- --deny warnings"; - }); - mnn-docs = craneLib.cargoDoc (commonArgs - // { - inherit cargoArtifacts; - cargoDocExtraArgs = "-p mnn -p mnn-sys"; - }); - mnn-fmt = craneLib.cargoFmt {inherit src;}; - # Audit dependencies - mnn-audit = - craneLib.cargoAudit.override { - cargo-audit = pkgs.cargo-audit; - } { - inherit src advisory-db; - }; + checks = + { + mnn-clippy = craneLib.cargoClippy (commonArgs + // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + mnn-docs = craneLib.cargoDoc (commonArgs + // { + inherit cargoArtifacts; + cargoDocExtraArgs = "-p mnn -p mnn-sys"; + }); + mnn-fmt = craneLib.cargoFmt {inherit src;}; + # Audit dependencies + mnn-audit = + craneLib.cargoAudit.override { + cargo-audit = pkgs.cargo-audit; + } { + inherit src advisory-db; + }; - # Audit licenses - mnn-deny = craneLib.cargoDeny { - inherit src; - }; - mnn-nextest = craneLib.cargoNextest (commonArgs - // { - inherit cargoArtifacts; - partitions = 1; - partitionType = "count"; - }); - mnn-sys-clippy = craneLib.cargoClippy (commonArgs - // { - inherit cargoArtifacts; - cargoClippyExtraArgs = "-p mnn-sys --all-targets -- --deny warnings"; - }); - mnn-sys-nextest = craneLib.cargoNextest (commonArgs - // { - inherit cargoArtifacts; - partitions = 1; - partitionType = "count"; - cargoExtraArgs = "-p mnn-sys"; - }); - # mnn-asan = let - # rustPlatform = pkgs.makeRustPlatform { - # cargo = nightlyToolchain; - # rustc = nightlyToolchain; - # }; - # in - # rustPlatform.buildRustPackage ( - # commonArgs - # // { - # inherit src; - # name = "mnn-leaks"; - # cargoLock = { - # lockFile = ./Cargo.lock; - # outputHashes = { - # "cmake-0.1.50" = "sha256-GM2D7dpb2i2S6qYVM4HYk5B40TwKCmGQnUPfXksyf0M="; - # }; - # }; - # - # buildPhase = '' - # cargo test --target aarch64-apple-darwin - # ''; - # RUSTFLAGS = "-Zsanitizer=address"; - # ASAN_OPTIONS = "detect_leaks=1"; - # # MNN_COMPILE = "NO"; - # # MNN_LIB_DIR = "${pkgs.mnn}/lib"; - # } - # ); - }; - packages = - rec { - mnn = craneLib.buildPackage (commonArgs + # Audit licenses + mnn-deny = craneLib.cargoDeny { + inherit src; + }; + mnn-nextest = craneLib.cargoNextest (commonArgs + // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + }); + mnn-sys-clippy = craneLib.cargoClippy (commonArgs // { inherit cargoArtifacts; + cargoClippyExtraArgs = "-p mnn-sys --all-targets -- --deny warnings"; }); - inspect = craneLib.buildPackage (commonArgs + mnn-sys-nextest = craneLib.cargoNextest (commonArgs // { inherit cargoArtifacts; - pname = "inspect"; - cargoExtraArgs = - "--example inspect" - + (lib.optionalString pkgs.stdenv.isDarwin " --features opencl" + lib.optionalString pkgs.stdenv.isAarch64 ",metal,coreml"); + partitions = 1; + partitionType = "count"; + cargoExtraArgs = "-p mnn-sys"; }); - default = mnn; + # mnn-asan = let + # rustPlatform = pkgs.makeRustPlatform { + # cargo = nightlyToolchain; + # rustc = nightlyToolchain; + # }; + # in + # rustPlatform.buildRustPackage ( + # commonArgs + # // { + # inherit src; + # name = "mnn-leaks"; + # cargoLock = { + # lockFile = ./Cargo.lock; + # outputHashes = { + # "cmake-0.1.50" = "sha256-GM2D7dpb2i2S6qYVM4HYk5B40TwKCmGQnUPfXksyf0M="; + # }; + # }; + # + # buildPhase = '' + # cargo test --target aarch64-apple-darwin + # ''; + # RUSTFLAGS = "-Zsanitizer=address"; + # ASAN_OPTIONS = "detect_leaks=1"; + # # MNN_COMPILE = "NO"; + # # MNN_LIB_DIR = "${pkgs.mnn}/lib"; + # } + # ); } // lib.optionalAttrs (!pkgs.stdenv.isDarwin) { mnn-llvm-cov = craneLibLLvmTools.cargoLlvmCov (commonArgs // {inherit cargoArtifacts;}); }; + packages = rec { + mnn = craneLib.buildPackage (commonArgs + // { + inherit cargoArtifacts; + }); + inspect = craneLib.buildPackage (commonArgs + // { + inherit cargoArtifacts; + pname = "inspect"; + cargoExtraArgs = + "--example inspect" + + (lib.optionalString pkgs.stdenv.isDarwin " --features opencl" + lib.optionalString pkgs.stdenv.isAarch64 ",metal,coreml"); + }); + default = mnn; + }; devShells = { default = pkgs.mkShell (commonArgs diff --git a/tools/cachix/Makefile b/tools/cachix/Makefile new file mode 100644 index 0000000..21047be --- /dev/null +++ b/tools/cachix/Makefile @@ -0,0 +1,15 @@ +.PHONY: checks push push-all + +all: push-all + +checks: + nix flake check + +push: + cachix watch-exec mnn-rs -- nix flake check + +push-all: + cachix watch-exec mnn-rs -- nix flake check --system x86_64-linux + cachix watch-exec mnn-rs -- nix flake check --system aarch64-darwin + + diff --git a/tools/cachix/push.sh b/tools/cachix/push.sh new file mode 100755 index 0000000..0425b5c --- /dev/null +++ b/tools/cachix/push.sh @@ -0,0 +1,2 @@ +cachix watch-exec mnn-rs -- nix flake check --system x86_64-linux --max-jobs 0 +cachix watch-exec mnn-rs -- nix flake check --system aarch64-darwin From ea12126ccf5db14a6e5f26100a23f7ecbf1f39aa Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 20 Nov 2024 14:26:18 +0530 Subject: [PATCH 30/42] feat: Update to latest unicode license --- deny.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deny.toml b/deny.toml index 1e7ddbc..e6e6c88 100644 --- a/deny.toml +++ b/deny.toml @@ -88,7 +88,7 @@ ignore = [ # List of explicitly allowed licenses # See https://spdx.org/licenses/ for list of possible licenses # [possible values: any SPDX 3.11 short identifier (+ optional exception)]. -allow = ["MIT", "Apache-2.0", "BSD-3-Clause", "ISC", "Unicode-DFS-2016", "Zlib"] +allow = ["MIT", "Apache-2.0", "BSD-3-Clause", "ISC", "Unicode-3.0"] # The confidence threshold for detecting a license from license text. # The higher the value, the more closely the license text must be to the # canonical license text of a valid SPDX license file. From f4210f9dd32b6111a873cf3fc332b74a90997687 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 2 Dec 2024 13:51:43 +0200 Subject: [PATCH 31/42] feat: Don't use MNN_SRC when building without nix --- flake.nix | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/flake.nix b/flake.nix index 648a255..faa156b 100644 --- a/flake.nix +++ b/flake.nix @@ -54,17 +54,6 @@ # enableMetal = true; enableOpencl = true; }; - cargo-audit = pkgs.rustPlatform.buildRustPackage rec { - version = "0.21.0"; - pname = "cargo-audit"; - src = pkgs.fetchCrate { - inherit pname version; - sha256 = "sha256-oMXpJE49If4QKE80ZKhRpMRPh3Bl517a2Ez/1VcaQJQ="; - }; - cargoLock = rec { - lockFile = "${src}/Cargo.lock"; - }; - }; }) ]; }; @@ -213,6 +202,7 @@ devShells = { default = pkgs.mkShell (commonArgs // { + MNN_SRC = null; packages = with pkgs; [ cargo-audit From df8f67c0a2efa80edef32ac97db371c9abaadd13 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 2 Dec 2024 17:38:30 +0530 Subject: [PATCH 32/42] feat: Remove cargo-audit builder from flake.nix and update nixpkgs --- flake.lock | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/flake.lock b/flake.lock index fa6f8b2..a50b426 100644 --- a/flake.lock +++ b/flake.lock @@ -3,11 +3,11 @@ "advisory-db": { "flake": false, "locked": { - "lastModified": 1731271136, - "narHash": "sha256-VsrCHM1gP8YqBTQWBQ0TmFNAFv3lBA0PvtWh8/sA9n4=", + "lastModified": 1732819720, + "narHash": "sha256-6H7mKBKw3VErpGcCGEamBYJsopvqqdFmJhl8slfCtOQ=", "owner": "rustsec", "repo": "advisory-db", - "rev": "509528f6775ad69ab114f1e4b37b4359cae5cef4", + "rev": "9dc4a0bb102451e3c71e1b639068aec5a3e1f5f3", "type": "github" }, "original": { @@ -18,11 +18,11 @@ }, "crane": { "locked": { - "lastModified": 1731098351, - "narHash": "sha256-HQkYvKvaLQqNa10KEFGgWHfMAbWBfFp+4cAgkut+NNE=", + "lastModified": 1733016477, + "narHash": "sha256-Hh0khbqBeCtiNS0SJgqdWrQDem9WlPEc2KF5pAY+st0=", "owner": "ipetkov", "repo": "crane", - "rev": "ef80ead953c1b28316cc3f8613904edc2eb90c28", + "rev": "76d64e779e2fbaf172110038492343a8c4e29b55", "type": "github" }, "original": { @@ -36,11 +36,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1726560853, - "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -130,11 +130,11 @@ ] }, "locked": { - "lastModified": 1729742964, - "narHash": "sha256-B4mzTcQ0FZHdpeWcpDYPERtyjJd/NIuaQ9+BV1h+MpA=", + "lastModified": 1731952509, + "narHash": "sha256-p4gB3Rhw8R6Ak4eMl8pqjCPOLCZRqaehZxdZ/mbFClM=", "owner": "nix-community", "repo": "nix-github-actions", - "rev": "e04df33f62cdcf93d73e9a04142464753a16db67", + "rev": "7b5f051df789b6b20d259924d349a9ba3319b226", "type": "github" }, "original": { @@ -145,11 +145,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1731676054, - "narHash": "sha256-OZiZ3m8SCMfh3B6bfGC/Bm4x3qc1m2SVEAlkV6iY7Yg=", + "lastModified": 1733015953, + "narHash": "sha256-t4BBVpwG9B4hLgc6GUBuj3cjU7lP/PJfpTHuSqE+crk=", "owner": "nixos", "repo": "nixpkgs", - "rev": "5e4fbfb6b3de1aa2872b76d49fafc942626e2add", + "rev": "ac35b104800bff9028425fec3b6e8a41de2bbfff", "type": "github" }, "original": { @@ -178,11 +178,11 @@ ] }, "locked": { - "lastModified": 1731464916, - "narHash": "sha256-WZ5rpjr/wCt7yBOUsvDE2i22hYz9g8W921jlwVktRQ4=", + "lastModified": 1733106880, + "narHash": "sha256-aJmAIjZfWfPSWSExwrYBLRgXVvgF5LP1vaeUGOOIQ98=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "2c19bad6e881b5a154cafb7f9106879b5b356d1f", + "rev": "e66c0d43abf5bdefb664c3583ca8994983c332ae", "type": "github" }, "original": { From 09aa5c4fc5491d88f588407e44f63149a92ba302 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 2 Dec 2024 14:28:29 +0200 Subject: [PATCH 33/42] feat(build): Added SSE define to the x86_x64 root build --- mnn-sys/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 2fd38c6..2e09762 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -899,6 +899,7 @@ fn x86_64<'a>( .static_flag(true) .tap_mut(|build| { CxxOption::LOW_MEMORY.define(build); + CxxOption::SSE.define(build); CxxOption::CPU_WEIGHT_DEQUANT_GEMM.define(build); if has_avx512 && CxxOption::AVX512.enabled() && (!like_msvc || win_use_asm) { CxxOption::AVX512.define(build); From 30c0ff58b06837cb0e936ed24468e361464f87f4 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 2 Dec 2024 18:18:37 +0530 Subject: [PATCH 34/42] chore(clippy): Fix all clippy warnings --- mnn-sys/src/lib.rs | 1 + src/tensor.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/mnn-sys/src/lib.rs b/mnn-sys/src/lib.rs index c2bf3b6..1e87349 100644 --- a/mnn-sys/src/lib.rs +++ b/mnn-sys/src/lib.rs @@ -11,6 +11,7 @@ mod sys { #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] + #![allow(clippy::manual_c_str_literals)] include!(concat!(env!("OUT_DIR"), "/mnn_c.rs")); } pub use sys::*; diff --git a/src/tensor.rs b/src/tensor.rs index 087e3b8..1262089 100644 --- a/src/tensor.rs +++ b/src/tensor.rs @@ -685,7 +685,7 @@ pub struct RawTensor<'r> { // } // } -impl<'r> RawTensor<'r> { +impl RawTensor<'_> { pub fn shape(&self) -> TensorShape { unsafe { mnn_sys::Tensor_shape(self.inner) }.into() } From 550f1a43a39e4cc02c651d937fc42ed8c6253fe2 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 2 Dec 2024 18:29:51 +0530 Subject: [PATCH 35/42] feat(docs): Compile all docs (added mnn-bridge and mnn-sync) --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index faa156b..f469356 100644 --- a/flake.nix +++ b/flake.nix @@ -117,7 +117,7 @@ mnn-docs = craneLib.cargoDoc (commonArgs // { inherit cargoArtifacts; - cargoDocExtraArgs = "-p mnn -p mnn-sys"; + cargoDocExtraArgs = "-p mnn -p mnn-sys -p mnn-bridge -p mnn-sync"; }); mnn-fmt = craneLib.cargoFmt {inherit src;}; # Audit dependencies From 9aaf621de1fd5df5d60118a898a1b258a53770e0 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 2 Dec 2024 19:33:18 +0530 Subject: [PATCH 36/42] fix(mnn-sync): Fixed mnn-sync with new api --- mnn-sync/src/lib.rs | 21 +++++++-------------- mnn-sys/vendor | 2 +- src/interpreter.rs | 2 +- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/mnn-sync/src/lib.rs b/mnn-sync/src/lib.rs index 4fdc880..1f0ca78 100644 --- a/mnn-sync/src/lib.rs +++ b/mnn-sync/src/lib.rs @@ -200,23 +200,16 @@ impl SessionState { #[derive(Debug)] pub struct SessionRunner { pub interpreter: Interpreter, - pub session: Session, + pub session: Session<'static>, } impl SessionRunner { - pub fn new(interpreter: Interpreter, session: Session) -> Self { - Self { - interpreter, - session, - } - } - - pub fn create(mut net: Interpreter, config: ScheduleConfig) -> Result { + pub fn create(net: Interpreter, config: ScheduleConfig) -> Result { #[cfg(feature = "tracing")] tracing::trace!("Creating session"); #[cfg(feature = "tracing")] let now = std::time::Instant::now(); - let mut session = net.create_session(config)?; + let mut session = unsafe { core::mem::transmute(net.create_session(config)?) }; net.update_cache_file(&mut session)?; #[cfg(feature = "tracing")] tracing::trace!("Session created in {:?}", now.elapsed()); @@ -237,9 +230,9 @@ impl SessionRunner { self.interpreter.run_session(&self.session) } - pub fn both_mut(&mut self) -> (&mut Interpreter, &mut Session) { - (&mut self.interpreter, &mut self.session) - } + // pub fn both_mut(&mut self) -> (&mut Interpreter, &mut Session) { + // (&mut self.interpreter, &mut self.session) + // } pub fn resize_session(&mut self) -> Result<()> { self.interpreter.resize_session(&mut self.session); @@ -258,7 +251,7 @@ impl SessionRunner { &self.session } - pub fn session_mut(&mut self) -> &mut Session { + pub fn session_mut(&mut self) -> &mut Session<'static> { &mut self.session } diff --git a/mnn-sys/vendor b/mnn-sys/vendor index a74551b..707b8a4 160000 --- a/mnn-sys/vendor +++ b/mnn-sys/vendor @@ -1 +1 @@ -Subproject commit a74551b4f34b46ce7027c64e800d49fcab497261 +Subproject commit 707b8a41b25e3d0b7c4a39cd81109d7074ca3c28 diff --git a/src/interpreter.rs b/src/interpreter.rs index 5821bb0..9d4de35 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -126,7 +126,7 @@ impl SessionMode { #[derive(Debug)] pub struct Interpreter { pub(crate) inner: *mut mnn_sys::Interpreter, - pub(crate) __marker: PhantomData<()>, + pub(crate) __marker: PhantomData>, } unsafe impl Send for Interpreter {} From 904c8dc65f94f9900599f0274a8018ebfe7041c5 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Tue, 3 Dec 2024 11:22:07 +0530 Subject: [PATCH 37/42] feat: Add src and dst for lifetime transmute of SessionRunner --- Cargo.lock | 2 +- mnn-sync/src/lib.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebef272..70dc0b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" diff --git a/mnn-sync/src/lib.rs b/mnn-sync/src/lib.rs index 1f0ca78..177c872 100644 --- a/mnn-sync/src/lib.rs +++ b/mnn-sync/src/lib.rs @@ -203,13 +203,22 @@ pub struct SessionRunner { pub session: Session<'static>, } +// impl Drop for SessionRunner { +// fn drop(&mut self) { +// drop(self.session); +// drop(self.interpreter); +// } +// } + impl SessionRunner { pub fn create(net: Interpreter, config: ScheduleConfig) -> Result { #[cfg(feature = "tracing")] tracing::trace!("Creating session"); #[cfg(feature = "tracing")] let now = std::time::Instant::now(); - let mut session = unsafe { core::mem::transmute(net.create_session(config)?) }; + let mut session = unsafe { + core::mem::transmute::, Session<'static>>(net.create_session(config)?) + }; net.update_cache_file(&mut session)?; #[cfg(feature = "tracing")] tracing::trace!("Session created in {:?}", now.elapsed()); From 10252bb9de7989ed1a48248bf8532aac2b18be68 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Thu, 19 Dec 2024 12:03:16 +0530 Subject: [PATCH 38/42] feat: Added partial metal compilation support --- mnn-sys/build.rs | 77 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 2e09762..6c311e6 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -12,7 +12,12 @@ use std::{ sync::LazyLock, }; -const VENDOR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/vendor"); +const VENDOR: LazyLock = LazyLock::new(|| { + std::env::var("MNN_SRC") + .map(PathBuf::from) + .ok() + .unwrap_or_else(|| PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/vendor"))) +}); const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR"); static TARGET_POINTER_WIDTH: LazyLock = LazyLock::new(|| { std::env::var("CARGO_CFG_TARGET_POINTER_WIDTH") @@ -126,16 +131,12 @@ fn main() { } fn _main() -> Result<()> { - #[cfg(any(feature = "vulkan", feature = "metal", feature = "coreml"))] + #[cfg(any(feature = "vulkan", feature = "coreml"))] compile_error!("Vulkan, Metal and CoreML are not supported currently"); 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").change_context(Error)?); - let source = PathBuf::from( - std::env::var("MNN_SRC") - .ok() - .unwrap_or_else(|| VENDOR.into()), - ); + let source = (*VENDOR).clone(); ensure_vendor_exists(&source)?; @@ -703,6 +704,9 @@ pub fn mnn_cpp_build(vendor: impl AsRef) -> Result<()> { build.files(opencl_files.chain([opencl_files_dir.join("execution/cl/opencl_program.cc")])); } + #[cfg(feature = "metal")] + metal(&vendor); + build .try_compile("mnn") .change_context(Error) @@ -946,3 +950,62 @@ fn asm_filter(path: impl AsRef) -> bool { path.as_ref().extension() == Some(OsStr::new("S")) || path.as_ref().extension() == Some(OsStr::new("s")) } + +pub trait HasExtension { + fn has_extension>(&self, extensions: impl IntoIterator) -> bool; + fn has_extension_ignore_case>( + &self, + extension: impl IntoIterator, + ) -> bool; +} + +impl> HasExtension for P { + fn has_extension>(&self, extensions: impl IntoIterator) -> bool { + let path_ext = self.as_ref().extension(); + extensions + .into_iter() + .any(|ext| path_ext == Some(ext.as_ref())) + } + fn has_extension_ignore_case>( + &self, + extension: impl IntoIterator, + ) -> bool { + let path_ext = self.as_ref().extension().map(|ext| ext.as_encoded_bytes()); + extension.into_iter().any(|ext| { + let ext = ext.as_ref().as_encoded_bytes(); + path_ext + .map(|p| p.eq_ignore_ascii_case(ext)) + .unwrap_or_default() + }) + } +} + +pub fn metal(source: impl AsRef) { + let metal_source_dir = source.as_ref().join("source/backend/metal"); + let metal_files = ignore::Walk::new(&metal_source_dir) + .flatten() + .filter(|e| e.path().has_extension(["mm", "cpp"])) + .map(ignore::DirEntry::into_path) + .chain(core::iter::once( + metal_source_dir.join("MetalOPRegister.mm"), + )); + + // if cfg!(feature = "support_render") { + // let render_dir = current_list_dir.join("render"); + // for entry in fs::read_dir(render_dir).unwrap() { + // let path = entry.unwrap().path(); + // if let Some(ext) = path.extension() { + // if ext == "mm" || ext == "hpp" || ext == "cpp" { + // metal_files.push(path); + // } + // } + // } + // } + + cc::Build::new() + .files(metal_files) + .include(source.as_ref().join("source")) + .flag("-fobjc-arc") + .flag("-DMNN_METAL_ENABLED=1") + .compile("MNNMetal"); +} From 857fbe0c104b5eb22ca85619db193bdfcadadee0 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 23 Dec 2024 11:50:22 +0530 Subject: [PATCH 39/42] fix: build.rs and tracing patch issues --- examples/inspect.rs | 2 +- mnn-sys/build.rs | 27 +++++++++++++++------------ src/schedule.rs | 4 ---- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/examples/inspect.rs b/examples/inspect.rs index 7a9e4ea..4e1fd12 100644 --- a/examples/inspect.rs +++ b/examples/inspect.rs @@ -47,7 +47,7 @@ macro_rules! time { pub fn main() -> anyhow::Result<()> { use clap::Parser; let cli = Cli::parse(); - let mut net = Interpreter::from_file(&cli.model)?; + let net = Interpreter::from_file(&cli.model)?; if !cli.no_cache { net.set_cache_file(cli.model.with_extension("cache"), 128)?; } diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 6c311e6..a76dfc6 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -12,7 +12,7 @@ use std::{ sync::LazyLock, }; -const VENDOR: LazyLock = LazyLock::new(|| { +static VENDOR: LazyLock = LazyLock::new(|| { std::env::var("MNN_SRC") .map(PathBuf::from) .ok() @@ -132,7 +132,7 @@ fn main() { fn _main() -> Result<()> { #[cfg(any(feature = "vulkan", feature = "coreml"))] - compile_error!("Vulkan, Metal and CoreML are not supported currently"); + compile_error!("Vulkan, CoreML are not supported currently"); 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").change_context(Error)?); @@ -151,14 +151,12 @@ fn _main() -> Result<()> { ) .change_context(Error) .attach_printable("Failed to copy vendor")?; - let intptr = vendor.join("include").join("MNN").join("HalideRuntime.h"); + let halide_runtime_h = vendor.join("include").join("MNN").join("HalideRuntime.h"); #[cfg(unix)] - std::fs::set_permissions(&intptr, std::fs::Permissions::from_mode(0o644)) + std::fs::set_permissions(&halide_runtime_h, std::fs::Permissions::from_mode(0o644)) .change_context(Error)?; - // try_patch_file("patches/halide_type_t_64.patch", intptr) - // .attach_printable("Failed to patch vendor")?; - let intptr_contents = std::fs::read_to_string(&intptr).change_context(Error)?; + let intptr_contents = std::fs::read_to_string(&halide_runtime_h).change_context(Error)?; let patched = intptr_contents.lines().collect::>(); if let Some(idx) = patched.iter().position(|line| line.contains(HALIDE_SEARCH)) { // remove the last line and the next 3 lines @@ -169,12 +167,17 @@ fn _main() -> Result<()> { .map(|(_, c)| c) .collect::>(); - std::fs::write( - intptr, - patched.join("\n").replace(TRACING_SEARCH, TRACING_REPLACE), - ) - .change_context(Error)?; + std::fs::write(halide_runtime_h, patched.join("\n")).change_context(Error)?; } + + let mnn_define = vendor.join("include").join("MNN").join("MNNDefine.h"); + let patched = std::fs::read_to_string(&mnn_define) + .change_context(Error)? + .replace(TRACING_SEARCH, TRACING_REPLACE); + #[cfg(unix)] + std::fs::set_permissions(&mnn_define, std::fs::Permissions::from_mode(0o644)) + .change_context(Error)?; + std::fs::write(mnn_define, patched).change_context(Error)?; } if *MNN_COMPILE { diff --git a/src/schedule.rs b/src/schedule.rs index 5f75a06..f32666c 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -83,8 +83,6 @@ impl ForwardType { MNNForwardType::MNN_FORWARD_METAL => ForwardType::Metal, #[cfg(feature = "opencl")] MNNForwardType::MNN_FORWARD_OPENCL => ForwardType::OpenCL, - #[cfg(feature = "opengl")] - MNNForwardType::MNN_FORWARD_OPENGL => ForwardType::OpenGL, #[cfg(feature = "vulkan")] MNNForwardType::MNN_FORWARD_VULKAN => ForwardType::Vulkan, #[cfg(feature = "coreml")] @@ -120,8 +118,6 @@ impl ForwardType { ForwardType::Metal => "metal", #[cfg(feature = "opencl")] ForwardType::OpenCL => "opencl", - #[cfg(feature = "opengl")] - ForwardType::OpenGL => "opengl", #[cfg(feature = "vulkan")] ForwardType::Vulkan => "vulkan", #[cfg(feature = "coreml")] From 88f85236a85209bdd00832322a31537945213c09 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 23 Dec 2024 12:29:46 +0530 Subject: [PATCH 40/42] feat: Added metal support fully --- mnn-sys/build.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index a76dfc6..71f2353 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -708,7 +708,8 @@ pub fn mnn_cpp_build(vendor: impl AsRef) -> Result<()> { } #[cfg(feature = "metal")] - metal(&vendor); + // metal(&vendor, &includes); + let build = metal_builder(build, vendor).change_context(Error)?; build .try_compile("mnn") @@ -983,7 +984,38 @@ impl> HasExtension for P { } } -pub fn metal(source: impl AsRef) { +pub fn metal>(source: impl AsRef, includes: impl AsRef<[P]>) { + let metal_source_dir = source.as_ref().join("source/backend/metal"); + let metal_files = ignore::Walk::new(&metal_source_dir) + .flatten() + .filter(|e| e.path().has_extension(["cpp"])) + .map(ignore::DirEntry::into_path); + // .chain(core::iter::once( + // metal_source_dir.join("MetalOPRegister.mm"), + // )); + + // if cfg!(feature = "support_render") { + // let render_dir = current_list_dir.join("render"); + // for entry in fs::read_dir(render_dir).unwrap() { + // let path = entry.unwrap().path(); + // if let Some(ext) = path.extension() { + // if ext == "mm" || ext == "hpp" || ext == "cpp" { + // metal_files.push(path); + // } + // } + // } + // } + + cc::Build::new() + .files(metal_files) + .include(source.as_ref().join("source")) + .includes(includes.as_ref()) + .flag("-fobjc-arc") + .flag("-DMNN_METAL_ENABLED=1") + .compile("MNNMetal"); +} + +pub fn metal_builder(mut build: cc::Build, source: impl AsRef) -> Result { let metal_source_dir = source.as_ref().join("source/backend/metal"); let metal_files = ignore::Walk::new(&metal_source_dir) .flatten() @@ -1005,10 +1037,12 @@ pub fn metal(source: impl AsRef) { // } // } - cc::Build::new() + build + .clone() .files(metal_files) .include(source.as_ref().join("source")) .flag("-fobjc-arc") .flag("-DMNN_METAL_ENABLED=1") .compile("MNNMetal"); + Ok(build) } From dcc587e9df8068009308704e250e77e85bcb04dd Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 23 Dec 2024 12:31:14 +0530 Subject: [PATCH 41/42] chore: Cleanup and fix lints --- mnn-sys/build.rs | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 71f2353..3d2a7e7 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -708,8 +708,7 @@ pub fn mnn_cpp_build(vendor: impl AsRef) -> Result<()> { } #[cfg(feature = "metal")] - // metal(&vendor, &includes); - let build = metal_builder(build, vendor).change_context(Error)?; + let build = metal(build, vendor).change_context(Error)?; build .try_compile("mnn") @@ -984,38 +983,7 @@ impl> HasExtension for P { } } -pub fn metal>(source: impl AsRef, includes: impl AsRef<[P]>) { - let metal_source_dir = source.as_ref().join("source/backend/metal"); - let metal_files = ignore::Walk::new(&metal_source_dir) - .flatten() - .filter(|e| e.path().has_extension(["cpp"])) - .map(ignore::DirEntry::into_path); - // .chain(core::iter::once( - // metal_source_dir.join("MetalOPRegister.mm"), - // )); - - // if cfg!(feature = "support_render") { - // let render_dir = current_list_dir.join("render"); - // for entry in fs::read_dir(render_dir).unwrap() { - // let path = entry.unwrap().path(); - // if let Some(ext) = path.extension() { - // if ext == "mm" || ext == "hpp" || ext == "cpp" { - // metal_files.push(path); - // } - // } - // } - // } - - cc::Build::new() - .files(metal_files) - .include(source.as_ref().join("source")) - .includes(includes.as_ref()) - .flag("-fobjc-arc") - .flag("-DMNN_METAL_ENABLED=1") - .compile("MNNMetal"); -} - -pub fn metal_builder(mut build: cc::Build, source: impl AsRef) -> Result { +pub fn metal(build: cc::Build, source: impl AsRef) -> Result { let metal_source_dir = source.as_ref().join("source/backend/metal"); let metal_files = ignore::Walk::new(&metal_source_dir) .flatten() From 7011dd8742f547fe128c30fe9db19355936fa7eb Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Mon, 23 Dec 2024 15:38:47 +0530 Subject: [PATCH 42/42] fix: Fixed compilation issues with cc_build in opencl and metal together --- benches/mnn-bench.rs | 15 +++++++ mnn-sys/build.rs | 91 ++++++++++++++++++++++++--------------- tools/bencher/src/main.rs | 3 +- 3 files changed, 74 insertions(+), 35 deletions(-) diff --git a/benches/mnn-bench.rs b/benches/mnn-bench.rs index 3a0fa5b..f05e438 100644 --- a/benches/mnn-bench.rs +++ b/benches/mnn-bench.rs @@ -30,4 +30,19 @@ mod mnn_realesr_bench_with_ones { net.wait(&session); }); } + + #[cfg(feature = "metal")] + #[divan::bench] + pub fn mnn_realesr_benchmark_metal(bencher: Bencher) { + let net = Interpreter::from_file("tests/assets/realesr.mnn").unwrap(); + let mut config = ScheduleConfig::new(); + config.set_type(ForwardType::Metal); + let session = net.create_session(config).unwrap(); + bencher.bench_local(|| { + let mut input = net.input(&session, "data").unwrap(); + input.fill(1f32); + net.run_session(&session).unwrap(); + net.wait(&session); + }); + } } diff --git a/mnn-sys/build.rs b/mnn-sys/build.rs index 3d2a7e7..5d06bea 100644 --- a/mnn-sys/build.rs +++ b/mnn-sys/build.rs @@ -619,9 +619,9 @@ pub fn mnn_cpp_build(vendor: impl AsRef) -> Result<()> { build.flag_if_supported("/source-charset:utf-8"); } - CxxOption::VULKAN.define(&mut build); + // CxxOption::VULKAN.define(&mut build); + // CxxOption::COREML.define(&mut build); CxxOption::METAL.define(&mut build); - CxxOption::COREML.define(&mut build); CxxOption::OPENCL.define(&mut build); CxxOption::CRT_STATIC.define(&mut build); CxxOption::SPARSE_COMPUTE.define(&mut build); @@ -694,19 +694,7 @@ pub fn mnn_cpp_build(vendor: impl AsRef) -> Result<()> { } #[cfg(feature = "opencl")] - { - let opencl_files_dir = vendor.join("source").join("backend").join("opencl"); - let opencl_files = ignore::Walk::new(&opencl_files_dir) - .flatten() - .filter(|e| e.path().extension() == Some(OsStr::new("cpp"))) - .map(|e| e.into_path()); - let ocl_includes = opencl_files_dir.join("schema").join("current"); - build.include(ocl_includes.clone()); - includes.push(ocl_includes); - build.define("MNN_OPENCL_ENABLED", "1"); - build.files(opencl_files.chain([opencl_files_dir.join("execution/cl/opencl_program.cc")])); - } - + let build = opencl(build, vendor).change_context(Error)?; #[cfg(feature = "metal")] let build = metal(build, vendor).change_context(Error)?; @@ -983,8 +971,8 @@ impl> HasExtension for P { } } -pub fn metal(build: cc::Build, source: impl AsRef) -> Result { - let metal_source_dir = source.as_ref().join("source/backend/metal"); +pub fn metal(mut build: cc::Build, vendor: impl AsRef) -> Result { + let metal_source_dir = vendor.as_ref().join("source/backend/metal"); let metal_files = ignore::Walk::new(&metal_source_dir) .flatten() .filter(|e| e.path().has_extension(["mm", "cpp"])) @@ -993,24 +981,59 @@ pub fn metal(build: cc::Build, source: impl AsRef) -> Result { metal_source_dir.join("MetalOPRegister.mm"), )); - // if cfg!(feature = "support_render") { - // let render_dir = current_list_dir.join("render"); - // for entry in fs::read_dir(render_dir).unwrap() { - // let path = entry.unwrap().path(); - // if let Some(ext) = path.extension() { - // if ext == "mm" || ext == "hpp" || ext == "cpp" { - // metal_files.push(path); - // } - // } - // } - // } - - build - .clone() + cc_builder() + .define(CxxOption::METAL.name, CxxOption::METAL.cc()) .files(metal_files) - .include(source.as_ref().join("source")) + .includes(mnn_includes(vendor)) .flag("-fobjc-arc") - .flag("-DMNN_METAL_ENABLED=1") - .compile("MNNMetal"); + .define("MNN_METAL_ENABLED", "1") + .try_compile("MNNMetal") + .change_context(Error) + .attach_printable("Failed to compile MNNMetal")?; + build.define("MNN_METAL_ENABLED", "1"); + Ok(build) +} + +pub fn opencl(mut build: cc::Build, vendor: impl AsRef) -> Result { + let source = vendor.as_ref().join("source"); + let backend = source.join("backend"); + let opencl_files_dir = backend.join("opencl"); + let opencl_files = ignore::Walk::new(&opencl_files_dir) + .flatten() + .filter(|e| e.path().has_extension(["cpp"])) + .map(|e| e.into_path()); + let ocl_includes = opencl_files_dir.join("schema").join("current"); + cc_builder() + .define(CxxOption::OPENCL.name, CxxOption::OPENCL.cc()) + .includes(mnn_includes(vendor)) + .include(ocl_includes.clone()) + .define("MNN_OPENCL_ENABLED", "1") + .files(opencl_files.chain([opencl_files_dir.join("execution/cl/opencl_program.cc")])) + .try_compile("MNNOpenCL") + .change_context(Error) + .attach_printable("Failed to build MNNOpenCL")?; + build.define("MNN_OPENCL_ENABLED", "1"); Ok(build) } + +pub fn mnn_includes(vendor: impl AsRef) -> Vec { + let vendor = vendor.as_ref(); + vec![ + vendor.join("include/"), + vendor.join("source/"), + vendor.join("schema/current/"), + vendor.join("3rd_party/"), + vendor.join("3rd_party/flatbuffers/include"), + vendor.join("3rd_party/half"), + vendor.join("3rd_party/OpenCLHeaders/"), + ] +} + +pub fn cc_builder() -> cc::Build { + cc::Build::new() + .cpp(true) + .static_crt(STATIC_CRT) + .static_flag(true) + .std("c++11") + .to_owned() +} diff --git a/tools/bencher/src/main.rs b/tools/bencher/src/main.rs index 0ec7af0..87f9f49 100644 --- a/tools/bencher/src/main.rs +++ b/tools/bencher/src/main.rs @@ -306,7 +306,7 @@ pub fn main() -> Result<()> { // let indicatif_layer = IndicatifLayer::new(); tracing_subscriber::registry() .with(cli.verbose.tracing_level_filter()) - // .with(tracing_subscriber::fmt::layer().with_writer(Term::stderr)) + .with(tracing_subscriber::fmt::layer().with_writer(Term::stderr)) .init(); match cli.subcommand { @@ -598,6 +598,7 @@ pub fn bench( bar.set_message(format!("Running inference {c}")); not_terminal.then(|| eprintln!("Running inference {c}")); net.run_session(&session).cc(BenchError)?; + net.wait(&session); } Ok(()) })?;