Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cc crate to build instead of CMake #7

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7c7b690
feat: Build mnn with cc crate (opencl only)
uttarayan21 Nov 8, 2024
8717d62
feat: Fix build issue with opencl
uttarayan21 Nov 8, 2024
808ad7c
feat: Remove useless items
uttarayan21 Nov 8, 2024
187731c
feat: Fixed mnn-audit
uttarayan21 Nov 8, 2024
405ffed
feat: Only compile arm files when using arm
uttarayan21 Nov 10, 2024
b9056e3
feat: Added compilation support for sse and avx
uttarayan21 Nov 11, 2024
58c9ba7
feat: Added some deps
uttarayan21 Nov 11, 2024
ac2fa1d
feat: Add common args to devShell
uttarayan21 Nov 11, 2024
43639c9
feat: Do not emit cargo warnings from compiler warnings
uttarayan21 Nov 11, 2024
db90692
fix: Do not emit link args when compiling with cc crate
uttarayan21 Nov 11, 2024
e00e314
fix: OpenCL issues in linux
uttarayan21 Nov 11, 2024
33322b7
fix: Compilation issue on enabling sse
uttarayan21 Nov 12, 2024
827a7f2
chore: Remove some comments
uttarayan21 Nov 12, 2024
00131e1
feat: Add all features to cargo.toml
uttarayan21 Nov 13, 2024
fb1140b
feat: Add .S files as well
uttarayan21 Nov 13, 2024
28436e7
fix: Added cc extensions as c++ files as well
uttarayan21 Nov 13, 2024
416ee88
add exclude files and use static_flag and static_crt
uttarayan21 Nov 13, 2024
8d712b3
feat: Add crt_static to all items
uttarayan21 Nov 13, 2024
c2fe0dc
chore: Remove unused deps and features
uttarayan21 Nov 13, 2024
07f1dda
feat: Disable compilation for vulkan coreml and metal
uttarayan21 Nov 13, 2024
7f8698a
feat(API): Don't need &mut anymore to run / create session
uttarayan21 Nov 13, 2024
2b8c4e5
chore: Cargo clippy fixes
uttarayan21 Nov 13, 2024
f429533
feat: Cleanup don't try to compile arm stuff in x86
uttarayan21 Nov 13, 2024
f19ceb3
feat: Added checks for cpu features for sse and neon
uttarayan21 Nov 13, 2024
29b56a5
fix: Fix issue with cargo-llvm-cov
uttarayan21 Nov 13, 2024
c0b708f
feat: use latest rust toolchain
uttarayan21 Nov 13, 2024
bdcb002
feat: Added more flags for msvc
uttarayan21 Nov 13, 2024
bb42fef
feat: Use upstream cargo-llvm-cov and add cachix
uttarayan21 Nov 18, 2024
14eedf3
feat: Move cargo-llvm-cov to checks and upload to cachix
uttarayan21 Nov 18, 2024
4b26d68
Merge branch 'master' into cc_build
uttarayan21 Nov 20, 2024
ea12126
feat: Update to latest unicode license
uttarayan21 Nov 20, 2024
61d66d7
Merge branch 'mnn_3' into cc_build
uttarayan21 Dec 2, 2024
f4210f9
feat: Don't use MNN_SRC when building without nix
uttarayan21 Dec 2, 2024
df8f67c
feat: Remove cargo-audit builder from flake.nix and update nixpkgs
uttarayan21 Dec 2, 2024
09aa5c4
feat(build): Added SSE define to the x86_x64 root build
uttarayan21 Dec 2, 2024
30c0ff5
chore(clippy): Fix all clippy warnings
uttarayan21 Dec 2, 2024
550f1a4
feat(docs): Compile all docs (added mnn-bridge and mnn-sync)
uttarayan21 Dec 2, 2024
9aaf621
fix(mnn-sync): Fixed mnn-sync with new api
uttarayan21 Dec 2, 2024
904c8dc
feat: Add src and dst for lifetime transmute of SessionRunner
uttarayan21 Dec 3, 2024
48eb66a
Merge branch 'mnn_3' into cc_build
uttarayan21 Dec 18, 2024
10252bb
feat: Added partial metal compilation support
uttarayan21 Dec 19, 2024
5eed367
Merge branch 'master' into cc_build
uttarayan21 Dec 23, 2024
857fbe0
fix: build.rs and tracing patch issues
uttarayan21 Dec 23, 2024
88f8523
feat: Added metal support fully
uttarayan21 Dec 23, 2024
dcc587e
chore: Cleanup and fix lints
uttarayan21 Dec 23, 2024
7011dd8
fix: Fixed compilation issues with cc_build in opencl and metal together
uttarayan21 Dec 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 159 additions & 118 deletions Cargo.lock

Large diffs are not rendered by default.

33 changes: 20 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,49 @@
members = [".", "mnn-bridge", "mnn-sync", "mnn-sys", "tools/bencher"]
[workspace.package]
license = "Apache-2.0"
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 = "2.0"
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"
serde = { version = "1.0", features = ["derive"], optional = true }

[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"]

vulkan = [] # This is currently unimplemented

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"]
serde = ["dep:serde"]

default = ["mnn-threadpool"]
simd = ["mnn-sys/simd"]

default = ["simd"]


[dev-dependencies]
Expand Down
19 changes: 17 additions & 2 deletions benches/mnn-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod mnn_realesr_bench_with_ones {
use mnn::*;
#[divan::bench]
pub fn mnn_realesr_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();
Expand All @@ -19,7 +19,7 @@ mod mnn_realesr_bench_with_ones {
#[cfg(feature = "opencl")]
#[divan::bench]
pub fn mnn_realesr_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();
Expand All @@ -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);
});
}
}
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions examples/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ macro_rules! time {
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)?;
if !cli.no_cache {
interpreter.set_cache_file(cli.model.with_extension("cache"), 128)?;
net.set_cache_file(cli.model.with_extension("cache"), 128)?;
}

tracing_subscriber::fmt()
Expand All @@ -62,22 +62,22 @@ pub fn main() -> anyhow::Result<()> {

let mut config = ScheduleConfig::new();
config.set_type(cli.forward);
let mut session = time!(interpreter.create_session(config)?; "create session");
let mut session = time!(net.create_session(config)?; "create session");
if !cli.no_cache {
interpreter.update_cache_file(&mut 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::<f32>().expect("No tensor");
Expand All @@ -98,9 +98,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 => {
Expand Down
12 changes: 6 additions & 6 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)?;
Expand All @@ -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());

Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,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
Expand Down Expand Up @@ -226,6 +226,7 @@
cargo-llvm-cov
]
);
# ++ (with packages; [bencher inspect]);
});
};
}
Expand Down
37 changes: 19 additions & 18 deletions mnn-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl SessionRunnerState {
tracing::trace!("Unloading session");
match core::mem::take(self) {
Self::Loaded(sr) => {
let net = sr.unload()?;
let net = sr.unload();
*self = Self::Unloaded(net);
Ok(())
}
Expand Down Expand Up @@ -200,23 +200,25 @@ 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,
}
}
// impl Drop for SessionRunner {
// fn drop(&mut self) {
// drop(self.session);
// drop(self.interpreter);
// }
// }

pub fn create(mut net: Interpreter, config: ScheduleConfig) -> Result<Self> {
impl SessionRunner {
pub fn create(net: Interpreter, config: ScheduleConfig) -> Result<Self> {
#[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::<Session<'_>, Session<'static>>(net.create_session(config)?)
};
net.update_cache_file(&mut session)?;
#[cfg(feature = "tracing")]
tracing::trace!("Session created in {:?}", now.elapsed());
Expand All @@ -226,20 +228,20 @@ impl SessionRunner {
})
}

pub fn unload(self) -> Result<mnn::Interpreter> {
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<()> {
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);
Expand All @@ -258,7 +260,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
}

Expand Down Expand Up @@ -504,7 +506,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");
Expand Down
Loading
Loading