diff --git a/.gitignore b/.gitignore index c68b583..5aa0be5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,9 @@ models/ # 打包产物 *.zip +# rusto-rs 源码仓(独立 git 仓库,上游 GitHub byrizki/rusto-rs,不进本仓库; +# 本地 Metal/调优补丁存档见 docs/rusto-rs-metal-patch-20260912.patch) +rusto-rs/ + # macOS .DS_Store diff --git a/README.md b/README.md index ab595c6..9c4488e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,10 @@ 卸载:`bash uninstall.sh`(或双击 `uninstall.command`)。 -> 内置二进制为 **macOS x86_64**。Apple Silicon / Linux x86_64 需自行编译:在 rusto-rs 源码目录 `cargo build --release`,把产物 `target/release/rusto-rs` 覆盖到 `~/rocr/bin/rusto-rs` 即可。 +> 内置二进制为 **macOS x86_64**。Apple Silicon / Linux x86_64 需自行编译:rusto-rs 源码随本仓库位于 `rusto-rs/` +> (独立 git 仓库,上游 https://github.com/byrizki/rusto-rs ,不进本仓库;本地 Metal/调优补丁存档见 +> `docs/rusto-rs-metal-patch-20260912.patch`),在源码目录 `cargo build --release`, +> 把产物 `target/release/rusto-rs` 覆盖到 `~/rocr/bin/rusto-rs` 即可。 ## 使用 @@ -117,8 +120,10 @@ rocr scan.png json # JSON(含坐标框 + 置信度) │ ├── mac-gpu-acceleration-research.md # 独显/开源 GPU 加速方案调研 │ ├── int8-quantize-attempt-20260912.md # INT8 量化尝试与回滚复盘 │ ├── rocr-setup-20260912.md # 本机部署配置记录 +│ ├── rusto-rs-metal-patch-20260912.patch # rusto-rs 本地补丁(后端/线程/精度环境变量 + Metal 链接修复) │ └── bench/ # 基准脚本与原始计时记录 ├── samples/ # 测试样本图 + 生成脚本 +├── rusto-rs/ # 源码仓(独立 git,上游 GitHub,.gitignore 排除,仅保留最终二进制于 target/release/) └── README.md ``` diff --git a/docs/rusto-rs-metal-patch-20260912.patch b/docs/rusto-rs-metal-patch-20260912.patch new file mode 100644 index 0000000..3ba8a82 --- /dev/null +++ b/docs/rusto-rs-metal-patch-20260912.patch @@ -0,0 +1,286 @@ +diff --git a/crates/mnn-sys/build.rs b/crates/mnn-sys/build.rs +index 04d1276..9bce262 100644 +--- a/crates/mnn-sys/build.rs ++++ b/crates/mnn-sys/build.rs +@@ -40,6 +40,25 @@ fn main() { + println!("cargo:rustc-link-lib=stdc++"); + } + ++ // MNN's Metal backend pulls in ___isPlatformVersionAtLeast which lives in ++ // compiler-rt (libclang_rt.osx.a). rustc does not add it implicitly, so ++ // resolve the clang resource dir and link it explicitly. ++ if target_os == "macos" || target_os == "ios" { ++ if let Ok(out) = std::process::Command::new("clang") ++ .arg("-print-resource-dir") ++ .output() ++ { ++ let rd = String::from_utf8_lossy(&out.stdout).trim().to_string(); ++ if !rd.is_empty() { ++ let crt_dir = Path::new(&rd).join("lib/darwin"); ++ if crt_dir.join("libclang_rt.osx.a").exists() { ++ println!("cargo:rustc-link-search=native={}", crt_dir.display()); ++ println!("cargo:rustc-link-lib=static=clang_rt.osx"); ++ } ++ } ++ } ++ } ++ + // Feature-based linking + if cfg!(feature = "metal") { + println!("cargo:rustc-link-lib=framework=Metal"); +diff --git a/src/engine.rs b/src/engine.rs +index 1d87a87..066ee6e 100644 +--- a/src/engine.rs ++++ b/src/engine.rs +@@ -79,11 +79,34 @@ impl MnnSession { + fn ensure_session(&mut self) -> Result<(), EngineError> { + if self.session.is_none() { + let mut config = ScheduleConfig::new(); +- config.set_type(ForwardType::Auto); ++ // 默认保持原行为 (Auto / 4 线程 / High 精度 / Power High); ++ // 以下环境变量可覆盖, 用于性能调优实测。 ++ config.set_type(match std::env::var("ROCR_BACKEND").ok().as_deref() { ++ Some("cpu") => ForwardType::CPU, ++ Some("opencl") => ForwardType::OpenCL, ++ Some("vulkan") => ForwardType::Vulkan, ++ Some("metal") => ForwardType::Metal, ++ _ => ForwardType::Auto, ++ }); ++ if let Some(n) = std::env::var("ROCR_NUM_THREADS") ++ .ok() ++ .and_then(|v| v.parse::().ok()) ++ { ++ config.set_num_thread(n); ++ } + + let mut backend_config = BackendConfig::new(); +- backend_config.set_precision_mode(PrecisionMode::High); +- backend_config.set_power_mode(PowerMode::High); ++ backend_config.set_precision_mode(match std::env::var("ROCR_PRECISION").ok().as_deref() { ++ Some("normal") => PrecisionMode::Normal, ++ Some("low") => PrecisionMode::Low, ++ Some("lowbf16") => PrecisionMode::LowBF16, ++ _ => PrecisionMode::High, ++ }); ++ backend_config.set_power_mode(match std::env::var("ROCR_POWER").ok().as_deref() { ++ Some("normal") => PowerMode::Normal, ++ Some("low") => PowerMode::Low, ++ _ => PowerMode::High, ++ }); + + config.set_backend_config(backend_config); + +diff --git a/src/main.rs b/src/main.rs +index d52a701..d65861e 100644 +--- a/src/main.rs ++++ b/src/main.rs +@@ -1,6 +1,9 @@ + use clap::{Parser, ValueEnum}; + use rusto::{DetectTextResult, ImageSource, InitializeConfig, OcrRunOptions, RustO}; ++use std::io::{BufRead, BufReader, Write}; ++use std::os::unix::net::{UnixListener, UnixStream}; + use std::path::PathBuf; ++use std::time::{Duration, Instant}; + + #[derive(Parser)] + #[command(name = "rusto")] +@@ -18,8 +21,8 @@ struct Cli { + #[arg(long)] + dict: PathBuf, + +- /// Input image path +- image: PathBuf, ++ /// Input image path (one-shot mode) ++ image: Option, + + /// Output format + #[arg(short, long, value_enum, default_value_t = OutputFormat::Json)] +@@ -40,6 +43,19 @@ struct Cli { + /// Path to text unwarping model (uvdoc.mnn) + #[arg(long)] + unwarp_model: Option, ++ ++ /// Run as a persistent daemon: load model once, serve OCR over a Unix socket. ++ /// Each connection sends two lines: "\\n\\n". ++ #[arg(long)] ++ serve: bool, ++ ++ /// Unix socket path for --serve mode ++ #[arg(long)] ++ socket: Option, ++ ++ /// Idle timeout (seconds) for --serve; daemon exits after this long without requests. ++ #[arg(long)] ++ idle: Option, + // Layout detection removed + } + +@@ -60,6 +76,13 @@ enum OutputFormat { + fn main() -> Result<(), Box> { + let cli = Cli::parse(); + ++ if cli.serve { ++ return serve(cli); ++ } ++ ++ // one-shot mode ++ let image = cli.image.ok_or("missing argument (or use --serve)")?; ++ + // Initialize base OCR using the advanced API + let mut ocr = RustO::initialize(InitializeConfig::ppv5(&cli.det_model, &cli.rec_model, &cli.dict))?; + +@@ -72,7 +95,7 @@ fn main() -> Result<(), Box> { + // Run OCR + eprintln!("Processing image..."); + let detected = ocr.detect_text( +- &ImageSource::Path(cli.image.clone()), ++ &ImageSource::Path(image.clone()), + &OcrRunOptions::default(), + )?; + let DetectTextResult::Structured(results) = detected else { +@@ -80,16 +103,126 @@ fn main() -> Result<(), Box> { + }; + eprintln!("✓ OCR completed"); + +- // Output projected text results. +- match cli.format { +- OutputFormat::Json => println!("{}", serde_json::to_string_pretty(&results)?), ++ print_results(cli.format, &results); ++ ++ Ok(()) ++} ++ ++fn serve(cli: Cli) -> Result<(), Box> { ++ let socket = cli.socket.clone().ok_or("serve mode requires --socket ")?; ++ let idle = Duration::from_secs(cli.idle.unwrap_or(600)); ++ let _ = std::fs::remove_file(&socket); ++ let listener = UnixListener::bind(&socket)?; ++ // Non-blocking accept + poll loop, so the idle check runs periodically. ++ listener.set_nonblocking(true)?; ++ eprintln!("rocr daemon listening on {}", socket.display()); ++ ++ let mut ocr = RustO::initialize(InitializeConfig::ppv5(&cli.det_model, &cli.rec_model, &cli.dict))?; ++ eprintln!("model loaded; ready"); ++ ++ let mut last = Instant::now(); ++ loop { ++ match listener.accept() { ++ Ok((mut stream, _)) => { ++ last = Instant::now(); ++ // macOS: 非阻塞 listener accept 出来的 socket 会继承 O_NONBLOCK, ++ // 会让 write_all 写满 ~8KB socket 缓冲后返回 WouldBlock 而截断大响应。 ++ // 强制恢复阻塞模式, 保证大 JSON/TSV 完整写出。 ++ let _ = stream.set_nonblocking(false); ++ if let Err(e) = handle_conn(&mut ocr, &mut stream) { ++ eprintln!("conn error: {e}"); ++ } ++ } ++ Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { ++ if last.elapsed() >= idle { ++ eprintln!("idle timeout {:?}, exiting", idle); ++ break; ++ } ++ std::thread::sleep(Duration::from_millis(500)); ++ } ++ Err(e) => return Err(e.into()), ++ } ++ } ++ let _ = std::fs::remove_file(&socket); ++ Ok(()) ++} ++ ++fn handle_conn(ocr: &mut RustO, stream: &mut UnixStream) -> Result<(), Box> { ++ let mut reader = BufReader::new(stream); ++ let mut fmt_line = String::new(); ++ let mut path_line = String::new(); ++ if reader.read_line(&mut fmt_line)? == 0 { ++ return Ok(()); ++ } ++ if reader.read_line(&mut path_line)? == 0 { ++ return Ok(()); ++ } ++ let fmt = fmt_line.trim(); ++ let path = path_line.trim(); ++ ++ match ocr.detect_text( ++ &ImageSource::Path(PathBuf::from(path)), ++ &OcrRunOptions::default(), ++ ) { ++ Ok(detected) => { ++ let DetectTextResult::Structured(results) = detected else { ++ unreachable!("default OCR output is structured lines"); ++ }; ++ let format = match fmt { ++ "tsv" => OutputFormat::Tsv, ++ "markdown" => OutputFormat::Markdown, ++ "textordered" => OutputFormat::TextOrdered, ++ "json" => OutputFormat::Json, ++ _ => OutputFormat::Text, ++ }; ++ let mut out = String::new(); ++ match format { ++ OutputFormat::Json => out = serde_json::to_string_pretty(&results)?, ++ OutputFormat::Text | OutputFormat::TextOrdered => { ++ for r in &results { ++ out.push_str(&r.text); ++ out.push('\n'); ++ } ++ } ++ OutputFormat::Tsv => { ++ for r in &results { ++ let b = r.box_points; ++ out.push_str(&format!( ++ "{}\t{:.3}\t{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1}\n", ++ r.text, r.score, b[0].0, b[0].1, b[1].0, b[1].1, b[2].0, b[2].1, b[3].0, b[3].1 ++ )); ++ } ++ } ++ OutputFormat::Markdown => { ++ out.push_str("```\n"); ++ for r in &results { ++ out.push_str(&r.text); ++ out.push('\n'); ++ } ++ out.push_str("```\n"); ++ } ++ } ++ reader.get_mut().write_all(out.as_bytes())?; ++ reader.get_mut().flush()?; ++ } ++ Err(e) => { ++ let msg = format!("ERROR: {e}\n"); ++ let _ = reader.get_mut().write_all(msg.as_bytes()); ++ } ++ } ++ Ok(()) ++} ++ ++fn print_results(format: OutputFormat, results: &[rusto::TextResult]) { ++ match format { ++ OutputFormat::Json => println!("{}", serde_json::to_string_pretty(results).unwrap()), + OutputFormat::Text | OutputFormat::TextOrdered => { +- for result in &results { ++ for result in results { + println!("{}", result.text); + } + } + OutputFormat::Tsv => { +- for result in &results { ++ for result in results { + let box_points = result.box_points; + println!( + "{}\t{:.3}\t{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1}", +@@ -108,12 +241,10 @@ fn main() -> Result<(), Box> { + } + OutputFormat::Markdown => { + println!("```"); +- for result in &results { ++ for result in results { + println!("{}", result.text); + } + println!("```"); + } + } +- +- Ok(()) + }