1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
impl OwnedQuantizedModel {
/// Get most likely next token
///
/// # Errors
///
/// Returns error if forward pass fails
pub fn predict_next(&self, token_ids: &[u32]) -> Result<u32> {
let logits = self.forward(token_ids)?;
let (max_idx, _) = logits
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.ok_or_else(|| RealizarError::InvalidShape {
reason: "Empty logits".to_string(),
})?;
Ok(max_idx as u32)
}
/// Generate tokens using fused Q4_K operations (IMP-100)
///
/// This is the HTTP serving entry point for quantized inference.
///
/// # Arguments
///
/// * `prompt` - Initial token IDs
/// * `config` - Generation configuration
///
/// # Returns
///
/// Generated token sequence including prompt
///
/// # Errors
///
/// Returns error if forward pass fails
pub fn generate(&self, prompt: &[u32], config: &QuantizedGenerateConfig) -> Result<Vec<u32>> {
if prompt.is_empty() {
return Err(RealizarError::InvalidShape {
reason: "Prompt cannot be empty".to_string(),
});
}
// GH-167: Check context length before GPU dispatch to avoid cryptic CUDA errors
if prompt.len() > self.config.context_length {
return Err(RealizarError::ContextLimitExceeded {
provided: prompt.len(),
maximum: self.config.context_length,
});
}
let mut tokens = prompt.to_vec();
let max_len = prompt.len() + config.max_tokens;
// PMAT-819: seed the sampler RNG from config.seed so the OpenAI `seed`
// API contract holds (same prompt+seed+params => same tokens). Greedy
// (temperature==0 || top_k==1) never touches the RNG, so the default
// config is byte-for-byte unchanged.
let mut rng = StdRng::seed_from_u64(config.seed);
for _ in 0..config.max_tokens {
// Forward pass with fused Q4_K ops (1.37x faster)
let mut logits = self.forward(&tokens)?;
// PMAT-814: apply repetition penalty in place over the recent context
// BEFORE both greedy argmax and sampling (no-op when repeat_penalty == 1.0).
Self::apply_repeat_penalty(
&mut logits,
&tokens,
config.repeat_penalty,
config.repeat_last_n,
);
// Sample next token
let next_token = if config.temperature == 0.0 || config.top_k == 1 {
// Greedy decoding
Self::argmax(&logits)
} else {
// Temperature + top-k sampling (seeded for reproducibility)
Self::sample_topk_seeded(&logits, config.temperature, config.top_k, &mut rng)
};
// Check stop condition
if config.stop_tokens.contains(&next_token) {
break;
}
tokens.push(next_token);
// Check max length
if tokens.len() >= max_len {
break;
}
}
Ok(tokens)
}
/// Greedy argmax over logits
pub(crate) fn argmax(logits: &[f32]) -> u32 {
logits
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.map_or(0, |(idx, _)| idx as u32)
}
/// Apply a repetition penalty to `logits` in place (PMAT-814).
///
/// Mirrors the live MoE path (`infer/qwen3_moe_generate.rs::sample_from_logits`)
/// and Candle's `apply_repeat_penalty`: every token in the recency window has its
/// logit divided by `penalty` when positive and multiplied by `penalty` when
/// non-positive, so a larger `penalty` always shrinks the chance of repeating a
/// recently-seen token regardless of its logit sign.
///
/// The window is the last `last_n` entries of `recent_tokens` (the full decoded
/// context — prompt + generated — exactly as `repeat_last_n` is interpreted on the
/// MoE path and by llama.cpp's default), so callers pass the entire `tokens` vector.
///
/// # No-op guarantee (no-regression)
///
/// When `penalty == 1.0` (the default), `last_n == 0`, or `recent_tokens` is empty,
/// this returns immediately without touching `logits` — every all-default `apr run`
/// / `apr serve` request is byte-identical to the pre-PMAT-814 path (greedy argmax
/// and top-k/top-p sampling alike).
pub(crate) fn apply_repeat_penalty(
logits: &mut [f32],
recent_tokens: &[u32],
penalty: f32,
last_n: usize,
) {
if penalty == 1.0 || last_n == 0 || recent_tokens.is_empty() {
return;
}
let start = recent_tokens.len().saturating_sub(last_n);
for &token in &recent_tokens[start..] {
let idx = token as usize;
if idx < logits.len() {
if logits[idx] <= 0.0 {
logits[idx] *= penalty;
} else {
logits[idx] /= penalty;
}
}
}
}
/// Top-k sampling with temperature, drawing from the given uniform sample `r ∈ [0,1)`.
///
/// Pure: the only source of randomness is the caller-supplied `r`. This lets both the
/// entropy-seeded [`Self::sample_topk`] and the seeded [`Self::sample_topk_seeded`]
/// share one inverse-CDF implementation, so a seeded RNG fully determines the token.
fn sample_topk_with_draw(logits: &[f32], temperature: f32, top_k: usize, r: f32) -> u32 {
// Apply temperature
let scaled: Vec<f32> = logits.iter().map(|&x| x / temperature).collect();
// Get top-k indices
let mut indexed: Vec<(usize, f32)> = scaled.iter().copied().enumerate().collect();
indexed.sort_by(|(_, a), (_, b)| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
indexed.truncate(top_k);
// Softmax over top-k
let max_val = indexed.first().map_or(0.0, |(_, v)| *v);
let exp_sum: f32 = indexed.iter().map(|(_, v)| (v - max_val).exp()).sum();
let probs: Vec<(usize, f32)> = indexed
.iter()
.map(|(i, v)| (*i, (v - max_val).exp() / exp_sum))
.collect();
// Inverse-CDF draw from the categorical distribution
let mut cumulative = 0.0;
for &(idx, prob) in &probs {
cumulative += prob;
if cumulative >= r {
return idx as u32;
}
}
probs.last().map_or(0, |(idx, _)| *idx as u32)
}
/// Top-k sampling with temperature (entropy-seeded RNG).
///
/// Backward-compatible: uses a fresh process-entropy RNG, so output is NOT reproducible.
/// For deterministic / seeded sampling (the OpenAI `seed` API contract), use
/// [`Self::sample_topk_seeded`].
pub fn sample_topk(logits: &[f32], temperature: f32, top_k: usize) -> u32 {
let r: f32 = rand::rng().random();
Self::sample_topk_with_draw(logits, temperature, top_k, r)
}
/// Top-k sampling with temperature, drawing from a caller-owned seeded RNG.
///
/// PMAT-819: closes the dense-path seed-determinism gap. The HTTP `/v1/chat/completions`
/// dense decode loops own one [`StdRng`] seeded from `QuantizedGenerateConfig.seed` and
/// advance it once per sampled token, so the same `(prompt, seed, temperature, top_k)`
/// produces byte-identical output across runs — matching the qwen3_moe path
/// (`infer/qwen3_moe_generate.rs::sample_from_logits`) which already seeds from config.
///
/// Discharges `openai-serve-sampling-determinism-v1` F-SEED-DETERMINISM-001/002.
pub fn sample_topk_seeded(
logits: &[f32],
temperature: f32,
top_k: usize,
rng: &mut StdRng,
) -> u32 {
let r: f32 = rng.random();
Self::sample_topk_with_draw(logits, temperature, top_k, r)
}
/// Generate tokens using KV cache for efficient autoregressive decoding (IMP-101)
///
/// This is O(n) per token instead of O(n²) due to KV cache reuse.
///
/// # Arguments
/// * `prompt` - Input token IDs
/// * `config` - Generation configuration
///
/// # Returns
/// Generated token sequence including prompt
///
/// # Errors
/// Returns error if forward pass fails
pub fn generate_with_cache(
&self,
prompt: &[u32],
config: &QuantizedGenerateConfig,
) -> Result<Vec<u32>> {
if prompt.is_empty() {
return Err(RealizarError::InvalidShape {
reason: "Prompt cannot be empty".to_string(),
});
}
// GH-167: Check context length before processing to avoid cryptic CUDA errors
if prompt.len() > self.config.context_length {
return Err(RealizarError::ContextLimitExceeded {
provided: prompt.len(),
maximum: self.config.context_length,
});
}
let max_seq_len = prompt.len() + config.max_tokens;
let mut cache = OwnedQuantizedKVCache::from_config(&self.config, max_seq_len);
let mut tokens = prompt.to_vec();
// PMAT-819: seeded sampler RNG (OpenAI `seed` determinism). This is the
// production HTTP dense path (try_quantized_backend -> generate_with_cache).
let mut rng = StdRng::seed_from_u64(config.seed);
// GH-104: BrickProfiler for per-operation timing in autoregressive path
let mut profiler = if config.trace {
BrickProfiler::new()
} else {
BrickProfiler::disabled()
};
if config.trace {
profiler.set_num_layers(self.config.num_layers);
}
// PMAT-TRACE-GGUF-001: Trace config info
if config.trace {
eprintln!(
"[TRACE-CACHE] GGUF model: {} layers, hidden_dim={}, vocab={}",
self.config.num_layers, self.config.hidden_dim, self.config.vocab_size
);
eprintln!(
"[TRACE-CACHE] Prefill: {} tokens, max_gen={}",
prompt.len(),
config.max_tokens
);
}
// Process prompt tokens (prefill), keeping the logits from the last position
// The logits from processing token[n-1] at position n-1 predict token[n]
let prefill_start = std::time::Instant::now();
let mut logits = Vec::new();
if config.trace {
profiler.start_inference();
for (pos, &token_id) in prompt.iter().enumerate() {
logits = self.forward_single_with_cache_profiled(
token_id, &mut cache, pos, &mut profiler,
)?;
}
} else {
for (pos, &token_id) in prompt.iter().enumerate() {
logits = self.forward_single_with_cache(token_id, &mut cache, pos)?;
}
}
if config.trace {
eprintln!(
"[TRACE-CACHE] Prefill complete: {} tokens in {:?}",
prompt.len(),
prefill_start.elapsed()
);
}
// Generate new tokens
// First iteration uses logits from prefill, subsequent use logits from forward pass
for gen_idx in 0..config.max_tokens {
let token_start = std::time::Instant::now();
// DEBUG: Print logits info for first generated token
if gen_idx == 0 && std::env::var("REALIZAR_DEBUG_LOGITS").is_ok() {
let sum: f32 = logits.iter().sum();
let max_val = logits.iter().copied().fold(f32::NEG_INFINITY, f32::max);
let min_val = logits.iter().copied().fold(f32::INFINITY, f32::min);
let top_5: Vec<(usize, f32)> = {
let mut indexed: Vec<_> =
logits.iter().enumerate().map(|(i, &v)| (i, v)).collect();
indexed.sort_by(|(_, a), (_, b)| {
b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
});
indexed.into_iter().take(5).collect()
};
eprintln!(
"[DEBUG-LOGITS] len={}, sum={:.4}, min={:.4}, max={:.4}",
logits.len(),
sum,
min_val,
max_val
);
eprintln!("[DEBUG-LOGITS] top 5 token ids and logits: {:?}", top_5);
eprintln!(
"[DEBUG-LOGITS] logits[0..5]: {:?}",
&logits[..5.min(logits.len())]
);
}
// PMAT-814: apply repetition penalty in place over the recent context
// BEFORE both greedy argmax and sampling (no-op when repeat_penalty == 1.0).
crate::gguf::OwnedQuantizedModel::apply_repeat_penalty(
&mut logits,
&tokens,
config.repeat_penalty,
config.repeat_last_n,
);
// Sample next token (PMAT-819: seeded for OpenAI `seed` determinism)
let next_token = if config.temperature == 0.0 || config.top_k == 1 {
ops::argmax(&logits)
} else {
crate::gguf::OwnedQuantizedModel::sample_topk_seeded(
&logits,
config.temperature,
config.top_k,
&mut rng,
)
};
// DEBUG: Print selected token
if gen_idx == 0 && std::env::var("REALIZAR_DEBUG_LOGITS").is_ok() {
eprintln!(
"[DEBUG-LOGITS] selected token: {} (logit={:.4})",
next_token,
logits.get(next_token as usize).copied().unwrap_or(f32::NAN)
);
}
// Check stop condition
if config.stop_tokens.contains(&next_token) {
break;
}
tokens.push(next_token);
// Check max length
if tokens.len() >= max_seq_len {
break;
}
// Get logits for next iteration by forwarding the newly sampled token
// Position is prompt.len() + gen_idx (where token was just added)
let position = prompt.len() + gen_idx;
if config.trace {
logits = self.forward_single_with_cache_profiled(
next_token, &mut cache, position, &mut profiler,
)?;
} else {
logits = self.forward_single_with_cache(next_token, &mut cache, position)?;
}
// PMAT-TRACE-GGUF-001: Per-token timing
if config.trace {
eprintln!(
"[TRACE-CACHE] pos={}: {} layers took {:?}",
position,
self.config.num_layers,
token_start.elapsed()
);
}
}
// GH-104: Print BrickProfiler report when tracing is enabled
if config.trace {
profiler.stop_inference();
let generated = tokens.len().saturating_sub(prompt.len());
profiler.set_tokens(prompt.len() + generated);
let report = profiler.report();
eprintln!("[BRICK-PROFILE] === Autoregressive Path Profile ===");
eprintln!(
"[BRICK-PROFILE] Total: {:.2}ms, {} tokens ({} prefill + {} decode), {:.1} tok/s",
report.total_inference_us / 1000.0,
report.tokens_processed,
prompt.len(),
generated,
report.throughput_tok_s,
);
let breakdown = report.percentage_breakdown();
for (name, stats) in report.sorted_by_time() {
let pct = breakdown.get(name).copied().unwrap_or(0.0);
eprintln!(
"[BRICK-PROFILE] {:<20} {:>8.2}ms ({:>5.1}%) avg={:.1}us count={}",
name,
stats.total_us / 1000.0,
pct,
stats.avg_us,
stats.count,
);
}
}
Ok(tokens)
}
/// Generate tokens with streaming callback (PMAT-087)
///
/// Same as `generate_with_cache` but calls `on_token` after each token
/// is generated, enabling true streaming to clients.
///
/// # Arguments
/// * `prompt` - Input token IDs
/// * `config` - Generation configuration
/// * `on_token` - Callback called for each generated token. Return `false` to stop.
///
/// # Returns
/// Generated token sequence including prompt
///
/// # Errors
/// Returns error if generation fails
pub fn generate_with_cache_streaming<F>(
&self,
prompt: &[u32],
config: &QuantizedGenerateConfig,
mut on_token: F,
) -> Result<Vec<u32>>
where
F: FnMut(u32) -> bool,
{
if prompt.is_empty() {
return Err(RealizarError::InvalidShape {
reason: "Prompt cannot be empty".to_string(),
});
}
// GH-167: Check context length before processing to avoid cryptic CUDA errors
if prompt.len() > self.config.context_length {
return Err(RealizarError::ContextLimitExceeded {
provided: prompt.len(),
maximum: self.config.context_length,
});
}
let max_seq_len = prompt.len() + config.max_tokens;
let mut cache = OwnedQuantizedKVCache::from_config(&self.config, max_seq_len);
let mut tokens = prompt.to_vec();
// PMAT-819: seeded sampler RNG (OpenAI `seed` determinism, streaming dense path).
let mut rng = StdRng::seed_from_u64(config.seed);
// GH-104: BrickProfiler for per-operation timing in streaming path
let mut profiler = if config.trace {
BrickProfiler::new()
} else {
BrickProfiler::disabled()
};
if config.trace {
profiler.set_num_layers(self.config.num_layers);
}
// PMAT-TRACE-GGUF-001: Trace config info
if config.trace {
eprintln!(
"[TRACE-CACHE] GGUF streaming: {} layers, hidden_dim={}, vocab={}",
self.config.num_layers, self.config.hidden_dim, self.config.vocab_size
);
eprintln!(
"[TRACE-CACHE] Prefill: {} tokens, max_gen={}",
prompt.len(),
config.max_tokens
);
}
// Process prompt tokens (prefill)
let prefill_start = std::time::Instant::now();
let mut logits = Vec::new();
if config.trace {
profiler.start_inference();
for (pos, &token_id) in prompt.iter().enumerate() {
logits = self.forward_single_with_cache_profiled(
token_id, &mut cache, pos, &mut profiler,
)?;
}
} else {
for (pos, &token_id) in prompt.iter().enumerate() {
logits = self.forward_single_with_cache(token_id, &mut cache, pos)?;
}
}
if config.trace {
eprintln!(
"[TRACE-CACHE] Prefill complete: {} tokens in {:?}",
prompt.len(),
prefill_start.elapsed()
);
}
// Generate new tokens with streaming
for gen_idx in 0..config.max_tokens {
let token_start = std::time::Instant::now();
// PMAT-814: apply repetition penalty in place over the recent context
// BEFORE both greedy argmax and sampling (no-op when repeat_penalty == 1.0).
crate::gguf::OwnedQuantizedModel::apply_repeat_penalty(
&mut logits,
&tokens,
config.repeat_penalty,
config.repeat_last_n,
);
// Sample next token (PMAT-819: seeded for OpenAI `seed` determinism)
let next_token = if config.temperature == 0.0 || config.top_k == 1 {
ops::argmax(&logits)
} else {
crate::gguf::OwnedQuantizedModel::sample_topk_seeded(
&logits,
config.temperature,
config.top_k,
&mut rng,
)
};
// Check stop condition
if config.stop_tokens.contains(&next_token) {
break;
}
tokens.push(next_token);
// PMAT-087: Call streaming callback - stop if it returns false
if !on_token(next_token) {
break;
}
// Check max length
if tokens.len() >= max_seq_len {
break;
}
// Get logits for next iteration
let position = prompt.len() + gen_idx;
if config.trace {
logits = self.forward_single_with_cache_profiled(
next_token, &mut cache, position, &mut profiler,
)?;
} else {
logits = self.forward_single_with_cache(next_token, &mut cache, position)?;
}
// PMAT-TRACE-GGUF-001: Per-token timing
if config.trace {
eprintln!(
"[TRACE-CACHE] pos={}: {} layers took {:?}",
position,
self.config.num_layers,
token_start.elapsed()
);
}
}
// GH-104: Print BrickProfiler report when tracing is enabled
if config.trace {
profiler.stop_inference();
let generated = tokens.len().saturating_sub(prompt.len());
profiler.set_tokens(prompt.len() + generated);
let report = profiler.report();
eprintln!("[BRICK-PROFILE] === Streaming Path Profile ===");
eprintln!(
"[BRICK-PROFILE] Total: {:.2}ms, {} tokens ({} prefill + {} decode), {:.1} tok/s",
report.total_inference_us / 1000.0,
report.tokens_processed,
prompt.len(),
generated,
report.throughput_tok_s,
);
let breakdown = report.percentage_breakdown();
for (name, stats) in report.sorted_by_time() {
let pct = breakdown.get(name).copied().unwrap_or(0.0);
eprintln!(
"[BRICK-PROFILE] {:<20} {:>8.2}ms ({:>5.1}%) avg={:.1}us count={}",
name,
stats.total_us / 1000.0,
pct,
stats.avg_us,
stats.count,
);
}
}
Ok(tokens)
}
}