Skip to content

Commit d2c7e94

Browse files
style: format code with cargo fmt
1 parent 1cc95c0 commit d2c7e94

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/engine/llama.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,29 @@ impl LlamaEngine {
303303
self
304304
}
305305

306+
/// Calculate adaptive batch size based on context length to prevent GGML assert failures
307+
/// with large prompts (Issue #140)
308+
fn calculate_adaptive_batch_size(ctx_len: usize) -> u32 {
309+
// Base batch size for smaller contexts
310+
const BASE_BATCH_SIZE: u32 = 2048;
311+
312+
// For larger contexts, scale up the batch size
313+
// Use context length as minimum, but cap at reasonable limits
314+
let adaptive_size = ctx_len.max(BASE_BATCH_SIZE as usize);
315+
316+
// Cap at 8192 to prevent excessive memory usage while handling large contexts
317+
// This allows for contexts up to 8192 tokens with large prompts
318+
let capped_size = adaptive_size.min(8192);
319+
320+
tracing::info!(
321+
"Batch size: {} (context: {}, adaptive calculation for large prompt support)",
322+
capped_size,
323+
ctx_len
324+
);
325+
326+
capped_size as u32
327+
}
328+
306329
/// Get information about the current GPU backend configuration
307330
#[allow(dead_code)]
308331
pub fn get_backend_info(&self) -> String {
@@ -389,7 +412,7 @@ impl InferenceEngine for LlamaEngine {
389412
};
390413
let ctx_params = llama::context::params::LlamaContextParams::default()
391414
.with_n_ctx(NonZeroU32::new(spec.ctx_len as u32))
392-
.with_n_batch(2048)
415+
.with_n_batch(Self::calculate_adaptive_batch_size(spec.ctx_len))
393416
.with_n_ubatch(512)
394417
.with_n_threads(spec.n_threads.unwrap_or_else(get_optimal_thread_count))
395418
.with_n_threads_batch(spec.n_threads.unwrap_or_else(get_optimal_thread_count));
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::fs;
2+
use tempfile::TempDir;
3+
use shimmy::engine::llama::LlamaEngine;
4+
use shimmy::engine::{GenOptions, ModelSpec};
5+
6+
#[cfg(test)]
7+
mod tests {
8+
use super::*;
9+
10+
#[test]
11+
fn test_calculate_adaptive_batch_size() {
12+
// Test with small context (should use base size)
13+
let small_ctx = LlamaEngine::calculate_adaptive_batch_size(1024);
14+
assert_eq!(small_ctx, 2048, "Small contexts should use base batch size");
15+
16+
// Test with medium context (should use base size)
17+
let medium_ctx = LlamaEngine::calculate_adaptive_batch_size(4096);
18+
assert_eq!(medium_ctx, 4096, "Medium contexts should scale up");
19+
20+
// Test with large context (should be capped)
21+
let large_ctx = LlamaEngine::calculate_adaptive_batch_size(16384);
22+
assert_eq!(large_ctx, 8192, "Large contexts should be capped at 8192");
23+
24+
// Test edge case at cap
25+
let at_cap = LlamaEngine::calculate_adaptive_batch_size(8192);
26+
assert_eq!(at_cap, 8192, "Context at cap should use cap value");
27+
}
28+
29+
#[test]
30+
fn test_large_prompt_batch_size_calculation() {
31+
// This test ensures that contexts large enough to handle the reported issue
32+
// (DeepSeek-R1-Distill-Qwen-7B with large system prompts) work correctly
33+
34+
// DeepSeek models typically use 4096 or 8192 context
35+
let deepseek_ctx = LlamaEngine::calculate_adaptive_batch_size(4096);
36+
assert!(deepseek_ctx >= 4096, "DeepSeek context should be supported");
37+
38+
// With large system prompts, we might need more batch capacity
39+
// The original issue had n_batch = 2048, which was insufficient
40+
assert!(deepseek_ctx > 2048, "Batch size should exceed the problematic 2048 limit");
41+
}
42+
43+
#[test]
44+
fn test_batch_size_reasonable_limits() {
45+
// Ensure we don't create excessively large batch sizes that would waste memory
46+
47+
// Very large contexts should still be capped
48+
let huge_ctx = LlamaEngine::calculate_adaptive_batch_size(32768);
49+
assert_eq!(huge_ctx, 8192, "Huge contexts should be capped to prevent memory waste");
50+
51+
// Edge case: context exactly at cap
52+
let exact_cap = LlamaEngine::calculate_adaptive_batch_size(8192);
53+
assert_eq!(exact_cap, 8192, "Exact cap should be allowed");
54+
}
55+
}

0 commit comments

Comments
 (0)