mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-03 12:35:58 -05:00
Part 1 of 5 of #2167. lightcraft-segment gets a BackboneAccel trait (backbone, plus optional necks, DETR encoder and mask decoder, asked only when runs_heads()), WeightReader and Sam3::load_accelerated. Whatever an accelerator returns is checked (length, finite values); an error, a wrong size or a NaN falls back to candle, logged once, with the reason kept for the status. The engine worker places the model (Metal on macOS, the CPU elsewhere, or the CPU when chosen or switched off), reloads it when a GPU switch or the setting changes, and reports where it ran in segment.model.status. Settings > Performance > AI masks gets Run on: Automatic / CPU. #2067's retry is kept: a job that fails on candle's GPU (Metal) runs once more on the CPU, which then serves the session (now a placement with the reason); LIGHTCRAFT_GPU=0 / LIGHTCRAFT_GPU_BACKEND=off keep the model on the CPU on macOS too. #1963's semantic mask comes from the mask decoder as before (on an accelerator: one more row of its mask product). This build has no GPU backbone (GPU_BACKBONE is None), so the GPU path is off: outside macOS candle runs everything on the CPU, as on main. Also on the CPU: the text features of the last 16 prompts are cached per model, and the detector's box relative-position bias is one pass, with the decoder MLPs' bias and ReLU fused (bit-identical outputs). Co-Authored-By: Claude Opus 5.5 <[email protected]>
58 lines
2.4 KiB
Rust
58 lines
2.4 KiB
Rust
//! Times SAM 3 on this machine (a synthetic gradient image; the first run includes compiling
|
|
//! the GPU kernels and loading the heads): `cargo run -p lightcraft-segment --release --example
|
|
//! sam3_bench -- <model dir>`. Each run times the backbone, the click features (tracker neck),
|
|
//! a first and a further click, and a first and a second description of the same image (the
|
|
//! first one computes the detector's features).
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
fn ms(d: Duration) -> String {
|
|
format!("{:.0} ms", d.as_secs_f64() * 1e3)
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let mut args = std::env::args().skip(1);
|
|
let dir = std::path::PathBuf::from(args.next().ok_or("usage: sam3_bench <model dir> [runs]")?);
|
|
let runs: usize = args.next().map(|s| s.parse()).transpose()?.unwrap_or(3);
|
|
let t = Instant::now();
|
|
let mut model = lightcraft_segment::Sam3::load(&dir)?;
|
|
eprintln!("load: {} on {}", ms(t.elapsed()), model.backbone_device());
|
|
let (w, h) = (1200, 800);
|
|
let rgb: Vec<u8> = (0..w * h * 3).map(|i| ((i / 3 % w) * 255 / w) as u8).collect();
|
|
let click = |x, y| lightcraft_segment::Click { x, y, positive: true };
|
|
for run in 0..runs {
|
|
let t = Instant::now();
|
|
let mut enc = model.encode(&rgb, w, h)?;
|
|
lightcraft_segment::sync(&model)?;
|
|
let encode = t.elapsed();
|
|
let t = Instant::now();
|
|
model.prepare_clicks(&mut enc)?;
|
|
lightcraft_segment::sync(&model)?;
|
|
let features = t.elapsed();
|
|
let t = Instant::now();
|
|
let p = model.segment_clicks(&mut enc, &[click(0.5, 0.5)])?;
|
|
let click1 = t.elapsed();
|
|
let t = Instant::now();
|
|
model.segment_clicks(&mut enc, &[click(0.5, 0.5), click(0.3, 0.6)])?;
|
|
let click2 = t.elapsed();
|
|
let t = Instant::now();
|
|
let d = model.segment_text(&mut enc, "sky", 0.5)?;
|
|
let text1 = t.elapsed();
|
|
let t = Instant::now();
|
|
model.segment_text(&mut enc, "the red car", 0.5)?;
|
|
let text2 = t.elapsed();
|
|
eprintln!(
|
|
"run {run}: encode {}, click features {}, click {} (score {:.2}), further click {}, first describe {} ({}), second describe {}",
|
|
ms(encode),
|
|
ms(features),
|
|
ms(click1),
|
|
p.score,
|
|
ms(click2),
|
|
ms(text1),
|
|
d.is_some(),
|
|
ms(text2)
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|