Skip to content

Commit c4d47ce

Browse files
committed
Refactor group size calculation to use checked division in FastViT and Stella v5 models; add clippy allow for explicit counter loops in PaddleOCR-VL and quantized LFM2 examples.
1 parent a3346ea commit c4d47ce

5 files changed

Lines changed: 11 additions & 15 deletions

File tree

candle-core/src/npy.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ impl Header {
117117
match c {
118118
'(' => cnt_parenthesis += 1,
119119
')' => cnt_parenthesis -= 1,
120-
',' => {
121-
if cnt_parenthesis == 0 {
122-
parts.push(header[start_index..index].to_owned());
123-
start_index = index + 1;
124-
}
120+
',' if cnt_parenthesis == 0 => {
121+
parts.push(header[start_index..index].to_owned());
122+
start_index = index + 1;
125123
}
126124
_ => {}
127125
}

candle-examples/examples/quantized-lfm2/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ fn main() -> Result<()> {
297297
let eos_token = guess_eos_id(tos.tokenizer());
298298
let mut sampled = 0;
299299
let start_post_prompt = std::time::Instant::now();
300+
#[allow(clippy::explicit_counter_loop)]
300301
for _ in 0..to_sample {
301302
if let Some(max_ctx) = context_length {
302303
if index_pos + 1 > max_ctx {

candle-transformers/src/models/fastvit.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,7 @@ fn mobileone_block(
197197
use_act: bool,
198198
vb: VarBuilder,
199199
) -> Result<Func<'static>> {
200-
let groups = if group_size == 0 {
201-
1
202-
} else {
203-
in_channels / group_size
204-
};
200+
let groups = in_channels.checked_div(group_size).unwrap_or(1);
205201

206202
let padding = kernel / 2;
207203
let conv2d_cfg = Conv2dConfig {

candle-transformers/src/models/paddleocr_vl/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ impl PaddleOCRVLModel {
538538
current_ids = Tensor::new(&[next_token], &self.device)?.unsqueeze(0)?;
539539

540540
// Subsequent forward passes (text only, using KV cache)
541+
#[allow(clippy::explicit_counter_loop)]
541542
for _ in 1..max_new_tokens {
542543
let logits = self.forward(&current_ids, None, None, seqlen_offset)?;
543544
let next_token = logits
@@ -600,6 +601,7 @@ impl PaddleOCRVLModel {
600601

601602
// Subsequent forward passes (text only, using KV cache)
602603
// Uses same incremental decoding as single-image generation
604+
#[allow(clippy::explicit_counter_loop)]
603605
for _ in 1..max_new_tokens {
604606
let logits = self.forward(&current_ids, None, None, seqlen_offset)?;
605607
let next_token = logits
@@ -665,6 +667,7 @@ impl PaddleOCRVLModel {
665667
current_ids = Tensor::new(&[next_token], &self.device)?.unsqueeze(0)?;
666668

667669
// Subsequent forward passes (text only, using KV cache)
670+
#[allow(clippy::explicit_counter_loop)]
668671
for _ in 1..max_new_tokens {
669672
let logits = self.forward(&current_ids, None, None, seqlen_offset)?;
670673
let next_token = logits
@@ -871,6 +874,7 @@ impl PaddleOCRVLModel {
871874
current_ids = Tensor::new(&[next_token], &self.device)?.unsqueeze(0)?;
872875

873876
// Subsequent forward passes (text only, using KV cache)
877+
#[allow(clippy::explicit_counter_loop)]
874878
for _ in 1..max_new_tokens {
875879
let logits = self.forward(&current_ids, None, None, seqlen_offset)?;
876880
let logits = apply_repetition_penalty(&logits, &generated_tokens, repetition_penalty)?;
@@ -1047,6 +1051,7 @@ impl PaddleOCRVLModel {
10471051
let mut seqlen_offset = input_ids.dim(1)?;
10481052
let mut current_ids = Tensor::new(&[next_token], &self.device)?.unsqueeze(0)?;
10491053

1054+
#[allow(clippy::explicit_counter_loop)]
10501055
for step in 1..max_steps {
10511056
// Compute position for M-RoPE
10521057
let pos = seqlen_offset as i64 + self.mrope_position_delta;

candle-transformers/src/models/stella_en_v5.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,7 @@ impl Attention {
288288
let hidden_sz = cfg.hidden_size;
289289
let num_heads = cfg.num_attention_heads;
290290
let num_kv_heads = cfg.num_key_value_heads;
291-
let num_kv_groups = if num_kv_heads > 0 {
292-
num_heads / num_kv_heads
293-
} else {
294-
0
295-
};
291+
let num_kv_groups = num_heads.checked_div(num_kv_heads).unwrap_or(0);
296292
let head_dim = hidden_sz / num_heads;
297293

298294
let (qkv_proj, o_proj) = match cfg.variant {

0 commit comments

Comments
 (0)