Skip to content

Commit a361e47

Browse files
committed
refactor(code): improve layout
1 parent 8d19081 commit a361e47

53 files changed

Lines changed: 521 additions & 543 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/assets/src/progress.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ impl ProgressTracker for SmoothedRate {
3030
}
3131

3232
fn tick(&mut self, state: &indicatif::ProgressState, now: Instant) {
33-
if let Some((last, _)) = self.samples.back() {
34-
if now.duration_since(*last) < Duration::from_millis(20) {
35-
return;
36-
}
33+
if let Some((last, _)) = self.samples.back()
34+
&& now.duration_since(*last) < Duration::from_millis(20)
35+
{
36+
return;
3737
}
3838

3939
self.samples.push_back((now, state.pos()));
@@ -51,14 +51,15 @@ impl ProgressTracker for SmoothedRate {
5151
}
5252

5353
fn write(&self, _state: &indicatif::ProgressState, w: &mut dyn std::fmt::Write) {
54-
if let (Some((t0, p0)), Some((t1, p1))) = (self.samples.front(), self.samples.back()) {
55-
if self.samples.len() > 1 && t1 > t0 {
56-
let elapsed = t1.duration_since(*t0).as_millis() as f64 / 1000.0;
57-
let bytes = (p1 - p0) as f64;
58-
let rate = if elapsed > 0.0 { bytes / elapsed } else { 0.0 };
59-
let _ = write!(w, "{}/s", HumanBytes(rate as u64));
60-
return;
61-
}
54+
if let (Some((t0, p0)), Some((t1, p1))) = (self.samples.front(), self.samples.back())
55+
&& self.samples.len() > 1
56+
&& t1 > t0
57+
{
58+
let elapsed = t1.duration_since(*t0).as_millis() as f64 / 1000.0;
59+
let bytes = (p1 - p0) as f64;
60+
let rate = if elapsed > 0.0 { bytes / elapsed } else { 0.0 };
61+
let _ = write!(w, "{}/s", HumanBytes(rate as u64));
62+
return;
6263
}
6364

6465
let _ = write!(w, "-");

crates/assets/src/providers/modelscope.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ fn manifest_cache() -> &'static Mutex<HashMap<String, Arc<Vec<ModelScopeFile>>>>
158158
}
159159

160160
fn manifest_exists(repo_id: &str) -> bool {
161-
if let Some(cache) = MODELSCOPE_MANIFESTS.get() {
162-
if let Ok(guard) = cache.lock() {
163-
return guard.contains_key(repo_id);
164-
}
161+
if let Some(cache) = MODELSCOPE_MANIFESTS.get()
162+
&& let Ok(guard) = cache.lock()
163+
{
164+
return guard.contains_key(repo_id);
165165
}
166166
false
167167
}

crates/cli/src/app.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ use crate::{
3434
},
3535
};
3636

37+
type TokenCallback = Box<dyn Fn(usize, &[i64])>;
38+
3739
#[derive(Default)]
3840
struct StreamProgress {
3941
last_count: usize,
@@ -159,10 +161,11 @@ pub fn run_inference(args: InferArgs) -> Result<()> {
159161
let progress_callback = move |count: usize, ids: &[i64]| {
160162
let mut delta_to_emit = None;
161163

162-
if count > 0 && prefill_duration_for_cb.get().is_none() {
163-
if let Some(start) = start_time_for_cb.get() {
164-
prefill_duration_for_cb.set(Some(start.elapsed()));
165-
}
164+
if count > 0
165+
&& prefill_duration_for_cb.get().is_none()
166+
&& let Some(start) = start_time_for_cb.get()
167+
{
168+
prefill_duration_for_cb.set(Some(start.elapsed()));
166169
}
167170

168171
{
@@ -199,7 +202,7 @@ pub fn run_inference(args: InferArgs) -> Result<()> {
199202
}
200203
};
201204

202-
let mut callback_holder: Option<Box<dyn Fn(usize, &[i64])>> = None;
205+
let mut callback_holder: Option<TokenCallback> = None;
203206
if !quiet {
204207
callback_holder = Some(Box::new(progress_callback));
205208
}

crates/cli/src/args.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22

33
use clap::{Args as ClapArgs, Parser, Subcommand};
4-
use deepseek_ocr_config::{AppConfig, ConfigOverride, ConfigOverrides};
4+
use deepseek_ocr_config::{AppConfig, ConfigOverride, ConfigOverrides, config::InferenceOverride};
55
use deepseek_ocr_core::runtime::{DeviceKind, Precision};
66

77
#[derive(Parser, Debug)]
@@ -164,30 +164,33 @@ pub struct InferArgs {
164164

165165
impl From<&InferArgs> for ConfigOverrides {
166166
fn from(args: &InferArgs) -> Self {
167-
let mut overrides = ConfigOverrides::default();
168-
overrides.config_path = args.config.clone();
169-
overrides.model_id = args.model.clone();
170-
overrides.model_config = args.model_config.clone();
171-
overrides.tokenizer = args.tokenizer.clone();
172-
overrides.weights = args.weights.clone();
173-
overrides.inference.device = args.device;
174-
overrides.inference.precision = args.dtype;
175-
overrides.inference.template = args.template.clone();
176-
overrides.inference.base_size = args.base_size;
177-
overrides.inference.image_size = args.image_size;
178-
overrides.inference.crop_mode = args.crop_mode;
179-
overrides.inference.max_new_tokens = args.max_new_tokens;
180-
if args.no_cache {
181-
overrides.inference.use_cache = Some(false);
167+
let inference = InferenceOverride {
168+
device: args.device,
169+
precision: args.dtype,
170+
template: args.template.clone(),
171+
base_size: args.base_size,
172+
image_size: args.image_size,
173+
crop_mode: args.crop_mode,
174+
max_new_tokens: args.max_new_tokens,
175+
use_cache: args.no_cache.then_some(false),
176+
do_sample: args.do_sample,
177+
temperature: args.temperature,
178+
top_p: args.top_p,
179+
top_k: args.top_k,
180+
repetition_penalty: args.repetition_penalty,
181+
no_repeat_ngram_size: args.no_repeat_ngram_size,
182+
seed: args.seed,
183+
};
184+
185+
ConfigOverrides {
186+
config_path: args.config.clone(),
187+
model_id: args.model.clone(),
188+
model_config: args.model_config.clone(),
189+
tokenizer: args.tokenizer.clone(),
190+
weights: args.weights.clone(),
191+
inference,
192+
..ConfigOverrides::default()
182193
}
183-
overrides.inference.do_sample = args.do_sample;
184-
overrides.inference.temperature = args.temperature;
185-
overrides.inference.top_p = args.top_p;
186-
overrides.inference.top_k = args.top_k;
187-
overrides.inference.repetition_penalty = args.repetition_penalty;
188-
overrides.inference.no_repeat_ngram_size = args.no_repeat_ngram_size;
189-
overrides.inference.seed = args.seed;
190-
overrides
191194
}
192195
}
193196

crates/cli/src/resources.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ pub fn prepare_snapshot_path(
7979
// specific snapshot path regardless of the configured model entry. This
8080
// is primarily intended for local testing of freshly exported `.dsq`
8181
// artifacts.
82-
if let Ok(path_str) = std::env::var("DEEPSEEK_OCR_SNAPSHOT_OVERRIDE") {
83-
if !path_str.trim().is_empty() {
84-
return Ok(Some(PathBuf::from(path_str)));
85-
}
82+
if let Ok(path_str) = std::env::var("DEEPSEEK_OCR_SNAPSHOT_OVERRIDE")
83+
&& !path_str.trim().is_empty()
84+
{
85+
return Ok(Some(PathBuf::from(path_str)));
8686
}
8787

8888
let Some(entry) = snapshot else {

crates/config/src/config.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,13 @@ use crate::fs::{VirtualFileSystem, VirtualPath};
1616

1717
#[derive(Debug, Clone, Serialize, Deserialize)]
1818
#[serde(default)]
19+
#[derive(Default)]
1920
pub struct AppConfig {
2021
pub models: ModelRegistry,
2122
pub inference: InferenceSettings,
2223
pub server: ServerSettings,
2324
}
2425

25-
impl Default for AppConfig {
26-
fn default() -> Self {
27-
Self {
28-
models: ModelRegistry::default(),
29-
inference: InferenceSettings::default(),
30-
server: ServerSettings::default(),
31-
}
32-
}
33-
}
34-
3526
#[derive(Debug, Clone, Serialize, Deserialize)]
3627
#[serde(default)]
3728
pub struct ModelRegistry {
@@ -51,9 +42,7 @@ impl Default for ModelRegistry {
5142
}
5243

5344
fn ensure_default_model_entries(entries: &mut BTreeMap<String, ModelEntry>) {
54-
entries
55-
.entry("deepseek-ocr".to_string())
56-
.or_insert_with(ModelEntry::default);
45+
entries.entry("deepseek-ocr".to_string()).or_default();
5746
entries
5847
.entry("paddleocr-vl".to_string())
5948
.or_insert_with(|| ModelEntry {
@@ -275,10 +264,7 @@ impl AppConfig {
275264
pub fn apply_overrides(&mut self, overrides: &ConfigOverrides) {
276265
if let Some(model_id) = overrides.model_id.as_ref() {
277266
self.models.active = model_id.clone();
278-
self.models
279-
.entries
280-
.entry(model_id.clone())
281-
.or_insert_with(ModelEntry::default);
267+
self.models.entries.entry(model_id.clone()).or_default();
282268
}
283269

284270
if let Some(entry) = self.models.entries.get_mut(&self.models.active) {
@@ -517,7 +503,7 @@ impl ConfigOverride for ConfigOverrides {
517503
}
518504
}
519505

520-
impl<'a> ConfigOverride for &'a ConfigOverrides {
506+
impl ConfigOverride for &ConfigOverrides {
521507
fn apply(self, config: &mut AppConfig) {
522508
config.apply_overrides(self);
523509
}

crates/config/src/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl LocalFileSystem {
124124
&self.app_name
125125
}
126126

127-
fn resolve<'a>(&'a self, path: &VirtualPath) -> Result<PathBuf> {
127+
fn resolve(&self, path: &VirtualPath) -> Result<PathBuf> {
128128
let root = match path.namespace() {
129129
Namespace::Config => &self.config_root,
130130
Namespace::Cache => &self.cache_root,

crates/core/src/conversation/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,14 @@ impl ConversationTemplate {
128128
let seps = [self.sep.as_str(), self.sep2.as_deref().unwrap_or_default()];
129129
let mut buffer = String::new();
130130
for (idx, (_, message)) in self.messages.iter().enumerate() {
131-
match message.as_ref().map(|m| m.trim()).filter(|m| !m.is_empty()) {
132-
Some(content) => {
133-
if idx % 2 == 0 {
134-
buffer.push_str("<image>\n");
135-
buffer.push_str(seps[idx % 2]);
136-
} else {
137-
buffer.push_str(content);
138-
buffer.push_str(seps[idx % 2]);
139-
}
131+
if let Some(content) = message.as_ref().map(|m| m.trim()).filter(|m| !m.is_empty()) {
132+
if idx % 2 == 0 {
133+
buffer.push_str("<image>\n");
134+
buffer.push_str(seps[idx % 2]);
135+
} else {
136+
buffer.push_str(content);
137+
buffer.push_str(seps[idx % 2]);
140138
}
141-
None => {}
142139
}
143140
}
144141
buffer

crates/core/src/inference.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use tokenizers::Tokenizer;
66

77
use crate::{benchmark::Timer, conversation::get_conv_template, sampling::TokenSelectionParams};
88

9+
/// Callback used to stream decoded token pieces.
10+
pub type StreamCallback<'a> = Option<&'a dyn Fn(usize, &[i64])>;
11+
912
/// Vision pre-processing knobs shared across OCR backends.
1013
#[derive(Debug, Clone, Copy)]
1114
pub struct VisionSettings {
@@ -116,7 +119,7 @@ pub trait OcrEngine: Send {
116119
images: &[DynamicImage],
117120
vision: VisionSettings,
118121
params: &DecodeParameters,
119-
stream: Option<&dyn Fn(usize, &[i64])>,
122+
stream: StreamCallback,
120123
) -> Result<DecodeOutcome>;
121124
}
122125

@@ -136,8 +139,6 @@ pub fn render_prompt(template: &str, system_prompt: &str, raw_prompt: &str) -> R
136139
Ok(prompt)
137140
}
138141

139-
/// Normalise decoder output by stripping sentinel tokens and Windows line-endings.
140-
141142
/// Normalise decoder output by stripping sentinel tokens and Windows line-endings.
142143
pub fn normalize_text(s: &str) -> String {
143144
s.replace("\r\n", "\n")

crates/core/src/sampling.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ pub fn select_token_id<P: TokenSelectionParams>(
4747
apply_repetition_penalty(&mut adjusted, context, params.repetition_penalty());
4848

4949
let mut filtered = adjusted.clone();
50-
if let Some(ngram) = params.no_repeat_ngram_size() {
51-
if ngram > 1 {
52-
for token in banned_ngram_tokens(context, ngram) {
53-
if let Ok(index) = usize::try_from(token) {
54-
if index < filtered.len() {
55-
filtered[index] = f32::NEG_INFINITY;
56-
}
57-
}
50+
if let Some(ngram) = params.no_repeat_ngram_size()
51+
&& ngram > 1
52+
{
53+
for token in banned_ngram_tokens(context, ngram) {
54+
if let Ok(index) = usize::try_from(token)
55+
&& index < filtered.len()
56+
{
57+
filtered[index] = f32::NEG_INFINITY;
5858
}
5959
}
6060
}
@@ -67,15 +67,16 @@ pub fn select_token_id<P: TokenSelectionParams>(
6767
.iter()
6868
.map(|&v| (v as f64) / params.temperature())
6969
.collect();
70-
if let Some(k) = params.top_k() {
71-
if k > 0 && k < logits64.len() {
72-
apply_top_k(&mut logits64, k);
73-
}
70+
if let Some(k) = params.top_k()
71+
&& k > 0
72+
&& k < logits64.len()
73+
{
74+
apply_top_k(&mut logits64, k);
7475
}
75-
if let Some(top_p) = params.top_p() {
76-
if (0.0..1.0).contains(&top_p) {
77-
apply_top_p(&mut logits64, top_p);
78-
}
76+
if let Some(top_p) = params.top_p()
77+
&& (0.0..1.0).contains(&top_p)
78+
{
79+
apply_top_p(&mut logits64, top_p);
7980
}
8081
if let Some(sampled) = sample_from_logits(&logits64, rng) {
8182
return Ok(sampled as i64);
@@ -116,14 +117,15 @@ fn apply_repetition_penalty(scores: &mut [f32], context: &[i64], penalty: f32) {
116117
let penalty = penalty.max(f32::MIN_POSITIVE);
117118
let mut seen = HashSet::new();
118119
for &token in context {
119-
if let Ok(index) = usize::try_from(token) {
120-
if index < scores.len() && seen.insert(index) {
121-
let entry = &mut scores[index];
122-
if *entry > 0.0 {
123-
*entry /= penalty;
124-
} else {
125-
*entry *= penalty;
126-
}
120+
if let Ok(index) = usize::try_from(token)
121+
&& index < scores.len()
122+
&& seen.insert(index)
123+
{
124+
let entry = &mut scores[index];
125+
if *entry > 0.0 {
126+
*entry /= penalty;
127+
} else {
128+
*entry *= penalty;
127129
}
128130
}
129131
}

0 commit comments

Comments
 (0)