Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

Fast stickers: run ONNX models on the CPU (the Mac's Neural Engine never finished)

Gabriele Lozupone2b487287fb81
diff --git a/crates/qls-core/src/matting.rs b/crates/qls-core/src/matting.rs
index 54a6c45..d3d9290 100644
--- a/crates/qls-core/src/matting.rs
+++ b/crates/qls-core/src/matting.rs
@@ -10,7 +10,6 @@ use std::sync::Mutex;
use image::{GrayImage, RgbaImage, imageops::FilterType};
use ort::session::Session;
-use ort::session::builder::GraphOptimizationLevel;
use ort::value::Tensor;
use crate::catalog::{Download, FileKind, join_rel};
@@ -124,14 +123,7 @@ pub fn matte(sdcpp_dir: &Path, img: &RgbaImage) -> AppResult<GrayImage> {
if guard.as_ref().is_none_or(|(p, _, _)| p != &path) {
let threads = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(4);
let env = crate::onnx::environment().map_err(ort_err)?;
- let s = Session::builder(&env)
- .map_err(ort_err)?
- .with_optimization_level(GraphOptimizationLevel::Level3)
- .map_err(ort_err)?
- .with_intra_threads(threads)
- .map_err(ort_err)?
- .commit_from_file(&path)
- .map_err(ort_err)?;
+ let s = crate::onnx::cpu_session(&env, &path, threads).map_err(ort_err)?;
*guard = Some((path.clone(), env, s));
}
let (_, _, session) = guard.as_mut().unwrap();
@@ -224,7 +216,11 @@ mod shared_runtime_tests {
let dir = std::path::PathBuf::from(dir);
let img = image::RgbaImage::from_fn(400, 300, |x, y| image::Rgba([(x / 2) as u8, (y / 2) as u8, if (150..250).contains(&x) { 230 } else { 40 }, 255]));
let marked = crate::watermark::embed(&dir, &img).unwrap().unwrap();
+ // On the CPU the first matte (session included) takes ~8 s on an M2 Pro; on the Neural
+ // Engine (ort's default device policy, crate::onnx::cpu_session) it never came back.
+ let started = std::time::Instant::now();
let m = super::matte(&dir, &marked).unwrap();
+ assert!(started.elapsed().as_secs() < 90, "first matte took {:?}", started.elapsed());
assert_eq!(m.dimensions(), (400, 300));
assert!(crate::watermark::detect(&dir, &marked).unwrap().unwrap().kokon);
}
diff --git a/crates/qls-core/src/onnx.rs b/crates/qls-core/src/onnx.rs
index 7d90933..a6f37c8 100644
--- a/crates/qls-core/src/onnx.rs
+++ b/crates/qls-core/src/onnx.rs
@@ -3,9 +3,12 @@
//! ran second failed ("only one environment is allowed per process"; seen on the Mac 2026-11-04: a
//! sticker first, then every result unwatermarked). Both take it from here.
+use std::path::Path;
use std::sync::OnceLock;
use ort::environment::Environment;
+use ort::session::Session;
+use ort::session::builder::{AutoDevicePolicy, GraphOptimizationLevel};
static ENV: OnceLock<Result<Environment, String>> = OnceLock::new();
@@ -14,6 +17,26 @@ pub fn environment() -> Result<Environment, String> {
ENV.get_or_init(|| ort::init().with_name("kokon").build().map_err(|e| e.to_string())).clone()
}
+/// A CPU session of the model at `path` on `threads` threads (graph optimised).
+///
+/// The CPU is asked for explicitly. `ort` (rc.14) sets every new session to pick its device "for
+/// efficiency", and the only macOS runtime it downloads has CoreML, so on a Mac the session went to
+/// the Neural Engine: creating BiRefNet's session waited on ANECompilerService for 13+ minutes and
+/// never finished (M2 Pro, 2026-11-04; the quick cut-out "did nothing"). An explicit CPU provider
+/// list did not stop it, only the device policy does. On the CPU: session 1.4 s, matte ~6 s there.
+pub fn cpu_session(env: &Environment, path: &Path, threads: usize) -> Result<Session, String> {
+ Session::builder(env)
+ .map_err(|e| e.to_string())?
+ .with_auto_device(AutoDevicePolicy::PreferCPU)
+ .map_err(|e| e.to_string())?
+ .with_optimization_level(GraphOptimizationLevel::Level3)
+ .map_err(|e| e.to_string())?
+ .with_intra_threads(threads)
+ .map_err(|e| e.to_string())?
+ .commit_from_file(path)
+ .map_err(|e| e.to_string())
+}
+
#[cfg(test)]
mod tests {
#[test]
diff --git a/crates/qls-core/src/watermark/mod.rs b/crates/qls-core/src/watermark/mod.rs
index 858ac56..e316e56 100644
--- a/crates/qls-core/src/watermark/mod.rs
+++ b/crates/qls-core/src/watermark/mod.rs
@@ -19,7 +19,6 @@ use std::sync::Mutex;
use image::{RgbaImage, imageops::FilterType};
use ort::session::Session;
-use ort::session::builder::GraphOptimizationLevel;
use ort::value::Tensor;
use crate::catalog::{Download, join_rel};
@@ -114,14 +113,7 @@ fn with_session<T>(
.map(|n| n.get())
.unwrap_or(4)
.min(8);
- let session = Session::builder(&s._env)
- .map_err(ort_err)?
- .with_optimization_level(GraphOptimizationLevel::Level3)
- .map_err(ort_err)?
- .with_intra_threads(threads)
- .map_err(ort_err)?
- .commit_from_file(&path)
- .map_err(ort_err)?;
+ let session = crate::onnx::cpu_session(&s._env, &path, threads).map_err(ort_err)?;
*slot = Some(session);
}
f(slot.as_mut().unwrap())