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
impl CudaExecutor {
/// Validate that all RMSNorm gamma weights (per-layer + output) are cached.
///
/// Returns an error if any required gamma buffer is missing from `rmsnorm_cache`.
fn validate_rmsnorm_cache_for_logits(
&self,
num_layers: usize,
) -> Result<(), GpuError> {
for layer_idx in 0..num_layers {
let attn_name = format!("blk.{}.attn_norm.gamma", layer_idx);
if !self.rmsnorm_cache.contains_key(&attn_name) {
return Err(GpuError::InvalidLaunchConfig(format!(
"PAR-023: attn_norm not cached for layer {}",
layer_idx
)));
}
let ffn_name = format!("blk.{}.ffn_norm.gamma", layer_idx);
if !self.rmsnorm_cache.contains_key(&ffn_name) {
return Err(GpuError::InvalidLaunchConfig(format!(
"PAR-023: ffn_norm not cached for layer {}",
layer_idx
)));
}
}
if !self.rmsnorm_cache.contains_key("output_norm.gamma") {
return Err(GpuError::InvalidLaunchConfig(
"PAR-023: output_norm not cached".to_string(),
));
}
Ok(())
}
/// GH-215 FIX: Pad input embedding to Q4K super-block boundary (256) and upload.
///
/// Q4K GEMV kernels access `activations[sb_idx*256+val_idx]` which can exceed
/// the logical dimension for non-256-aligned models (e.g., `hidden_dim=896`).
fn pad_and_upload_input(&self, input: &[f32]) -> Result<GpuBuffer<f32>, GpuError> {
let padded_len = ((input.len() + 255) / 256) * 256;
let padded_input: std::borrow::Cow<'_, [f32]> = if padded_len > input.len() {
let mut padded = vec![0.0f32; padded_len];
padded[..input.len()].copy_from_slice(input);
std::borrow::Cow::Owned(padded)
} else {
std::borrow::Cow::Borrowed(input)
};
GpuBuffer::from_host(&self.context, &padded_input)
}
/// PAR-044: Run all transformer layers via the zero-allocation workspace path.
///
/// Layer 0 reads from the externally-provided `hidden_gpu`; layers 1+ read from
/// `workspace.hidden_buf2` (the output of the previous layer).
#[allow(clippy::too_many_arguments)]
fn run_workspace_layers(
&mut self,
hidden_gpu: &GpuBuffer<f32>,
num_layers: usize,
hidden_dim: u32,
intermediate_dim: u32,
epsilon: f32,
position: u32,
) -> Result<(), GpuError> {
// GH-559: Per-layer debug (moved OnceLock here for early use)
static DEBUG_LAYERS_EARLY: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let debug_layers_early = *DEBUG_LAYERS_EARLY.get_or_init(|| {
std::env::var("GPU_DEBUG_ALL_LAYERS")
.map(|v| v == "1")
.unwrap_or(false)
});
// GH-559: Check embedding upload
if debug_layers_early {
let n = (hidden_dim as usize).min(hidden_gpu.len());
let mut host = vec![0.0f32; n];
if hidden_gpu.copy_to_host(&mut host).is_ok() {
let sum: f32 = host.iter().sum();
let rms = (host.iter().map(|x| x * x).sum::<f32>() / n as f32).sqrt();
eprintln!(
"[GH-559] Layer 0 INPUT (hidden_gpu embed): sum={:.6}, rms={:.6}, first5={:?}",
sum, rms, &host[..5.min(n)]
);
}
}
// Layer 0: input from external hidden_gpu
// PAR-070: Pass explicit position for RoPE and KV cache
if num_layers > 0 {
let layer_weights = self.indexed_layer_weights[0].clone();
self.transformer_layer_workspace(
hidden_gpu,
0,
&layer_weights,
hidden_dim,
intermediate_dim,
epsilon,
position,
)?;
}
// GH-559: Per-layer debug checksum for GPU parity diagnosis
static DEBUG_LAYERS: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let debug_layers = *DEBUG_LAYERS.get_or_init(|| {
std::env::var("GPU_DEBUG_ALL_LAYERS")
.map(|v| v == "1")
.unwrap_or(false)
});
// GH-559: Check layer 0 output
if debug_layers {
if let Some(ref buf2) = self.workspace.hidden_buf2 {
let n = (hidden_dim as usize).min(buf2.len());
let mut host = vec![0.0f32; n];
if buf2.copy_to_host(&mut host).is_ok() {
let sum: f32 = host.iter().sum();
let rms = (host.iter().map(|x| x * x).sum::<f32>() / n as f32).sqrt();
eprintln!(
"[GH-559] Layer 0/28 OUTPUT (hidden_buf2): sum={:.6}, rms={:.6}, first5={:?}",
sum, rms, &host[..5.min(n)]
);
// GH-559: Dump per-super-block sums to find divergent region
for sb in 0..(n / 256) {
let idx = sb * 256;
let end = (idx + 5).min(n);
let sb_sum: f32 = host[idx..idx+256.min(n-idx)].iter().sum();
eprintln!(
"[GH-559-GPU] L0 sb{}: idx={}, sum={:.4}, vals={:?}",
sb, idx, sb_sum, &host[idx..end]
);
}
}
}
}
// Layers 1+: input from hidden_buf2 (output of previous layer)
// Use raw pointer to avoid borrow conflict with &mut self
for layer_idx in 1..num_layers {
let layer_weights = self.indexed_layer_weights[layer_idx].clone();
// SAFETY: hidden_buf2 is initialized and remains valid throughout
// We get ptr/len before the mutable borrow, avoiding conflict
let buf_ptr = self
.workspace
.hidden_buf2
.as_ref()
.expect("hidden_buf2 must be initialized")
.as_ptr();
let buf_len = self
.workspace
.hidden_buf2
.as_ref()
.expect("hidden_buf2 must be initialized")
.len();
// Create temporary non-owning view of hidden_buf2
// SAFETY: Memory safety ensured by bounds checking and alignment
// SAFETY: Pointer valid from allocation, length verified, used within scope
let input_buf = unsafe { GpuBuffer::<f32>::from_raw_parts(buf_ptr, buf_len) };
// GH-559: Download hidden state checksum BEFORE this layer
if debug_layers {
let mut host_buf = vec![0.0f32; buf_len.min(hidden_dim as usize)];
if input_buf.copy_to_host(&mut host_buf).is_ok() {
let sum: f32 = host_buf.iter().sum();
let rms: f32 = (host_buf.iter().map(|x| x * x).sum::<f32>()
/ host_buf.len() as f32)
.sqrt();
eprintln!(
"[GH-559] Layer {}/{} input: sum={:.6}, rms={:.6}, first5={:?}",
layer_idx,
num_layers,
sum,
rms,
&host_buf[..5.min(host_buf.len())]
);
}
}
// PAR-070: Pass explicit position for RoPE and KV cache
self.transformer_layer_workspace(
&input_buf,
layer_idx,
&layer_weights,
hidden_dim,
intermediate_dim,
epsilon,
position,
)?;
// Prevent Drop from freeing the borrowed memory
std::mem::forget(input_buf);
}
Ok(())
}
/// PAR-043: Run all transformer layers via the indexed path (O(1) weight access).
#[allow(clippy::too_many_arguments)]
fn run_indexed_layers(
&mut self,
mut hidden_gpu: GpuBuffer<f32>,
num_layers: usize,
hidden_dim: u32,
intermediate_dim: u32,
epsilon: f32,
) -> Result<GpuBuffer<f32>, GpuError> {
for layer_idx in 0..num_layers {
let layer_weights = self.indexed_layer_weights[layer_idx].clone();
hidden_gpu = self.transformer_layer_indexed(
&hidden_gpu,
layer_idx,
&layer_weights,
hidden_dim,
intermediate_dim,
epsilon,
)?;
}
Ok(hidden_gpu)
}
/// Legacy path: run all transformer layers via HashMap lookups + string formatting.
#[allow(clippy::too_many_arguments)]
fn run_legacy_layers(
&mut self,
mut hidden_gpu: GpuBuffer<f32>,
num_layers: usize,
layer_keys: &[(String, String)],
hidden_dim: u32,
intermediate_dim: u32,
epsilon: f32,
) -> Result<GpuBuffer<f32>, GpuError> {
for layer_idx in 0..num_layers {
let prefix = format!("blk.{}", layer_idx);
let (ref attn_name, ref ffn_name) = layer_keys[layer_idx];
let attn_gamma = self.rmsnorm_cache.get(attn_name).ok_or_else(|| {
GpuError::InvalidLaunchConfig(format!(
"PAR-023: Missing cached gamma for {}",
attn_name
))
})?;
let attn_ptr = attn_gamma.as_ptr();
let attn_len = attn_gamma.len();
let ffn_gamma = self.rmsnorm_cache.get(ffn_name).ok_or_else(|| {
GpuError::InvalidLaunchConfig(format!(
"PAR-023: Missing cached gamma for {}",
ffn_name
))
})?;
let ffn_ptr = ffn_gamma.as_ptr();
let ffn_len = ffn_gamma.len();
hidden_gpu = self.transformer_layer_gpu_cached(
&hidden_gpu,
layer_idx,
&prefix,
hidden_dim,
intermediate_dim,
attn_ptr,
attn_len,
ffn_ptr,
ffn_len,
epsilon,
)?;
}
Ok(hidden_gpu)
}
/// CORRECTNESS-001: Debug-dump the hidden state before output norm (GPU_DEBUG=1 only).
fn debug_dump_hidden_state(
&mut self,
hidden_gpu: &GpuBuffer<f32>,
workspace_used: bool,
debug_enabled: bool,
) -> Result<(), GpuError> {
if !debug_enabled {
return Ok(());
}
self.stream.synchronize()?;
let hidden_to_check = if workspace_used {
let ptr = self
.workspace
.hidden_buf2
.as_ref()
.expect("hidden_buf2 must be initialized")
.as_ptr();
let len = self
.workspace
.hidden_buf2
.as_ref()
.expect("hidden_buf2 must be initialized")
.len();
// SAFETY: Memory safety ensured by bounds checking and alignment
unsafe { GpuBuffer::<f32>::from_raw_parts(ptr, len) }
} else {
// SAFETY: Memory safety ensured by bounds checking and alignment
unsafe { GpuBuffer::<f32>::from_raw_parts(hidden_gpu.as_ptr(), hidden_gpu.len()) }
};
let mut hidden_host = vec![0.0f32; hidden_to_check.len()];
hidden_to_check.copy_to_host(&mut hidden_host)?;
std::mem::forget(hidden_to_check);
let sum: f32 = hidden_host.iter().sum();
let sum_sq: f32 = hidden_host.iter().map(|x| x * x).sum();
eprintln!(
"[CORRECTNESS-001] Hidden before output_norm: first 5 = {:?}, sum = {:.4}, rms = {:.4}",
&hidden_host[..5.min(hidden_host.len())],
sum,
(sum_sq / hidden_host.len() as f32).sqrt()
);
Ok(())
}
/// Apply output RMSNorm on GPU, selecting workspace or external hidden buffer.
#[allow(clippy::too_many_arguments)]
fn apply_output_rmsnorm(
&mut self,
hidden_gpu: &GpuBuffer<f32>,
workspace_used: bool,
hidden_dim: u32,
epsilon: f32,
) -> Result<GpuBuffer<f32>, GpuError> {
let output_norm_gamma = self.rmsnorm_cache.get("output_norm.gamma").ok_or_else(|| {
GpuError::InvalidLaunchConfig(
"PAR-023: Missing cached gamma for output_norm.gamma".to_string(),
)
})?;
let output_gamma_ptr = output_norm_gamma.as_ptr();
let output_gamma_len = output_norm_gamma.len();
if workspace_used {
// PAR-044 FIX: Use hidden_buf2 directly (no D2D copy)
let hidden_ptr = self
.workspace
.hidden_buf2
.as_ref()
.expect("hidden_buf2 must be initialized")
.as_ptr();
let hidden_len = self
.workspace
.hidden_buf2
.as_ref()
.expect("hidden_buf2 must be initialized")
.len();
// SAFETY: Memory safety ensured by bounds checking and alignment
// SAFETY: Pointer valid from allocation, length verified, used within scope
let hidden_input = unsafe { GpuBuffer::<f32>::from_raw_parts(hidden_ptr, hidden_len) };
let result = self.rmsnorm_gpu_ptr(
&hidden_input,
output_gamma_ptr,
output_gamma_len,
hidden_dim,
epsilon,
)?;
std::mem::forget(hidden_input);
Ok(result)
} else {
self.rmsnorm_gpu_ptr(
hidden_gpu,
output_gamma_ptr,
output_gamma_len,
hidden_dim,
epsilon,
)
}
}
/// CORRECTNESS-002: Debug-dump the normed hidden state before LM head (GPU_DEBUG=1 only).
fn debug_dump_normed_hidden(
&mut self,
normed_hidden: &GpuBuffer<f32>,
debug_enabled: bool,
) -> Result<(), GpuError> {
if !debug_enabled {
return Ok(());
}
self.stream.synchronize()?;
let mut normed_host = vec![0.0f32; normed_hidden.len()];
normed_hidden.copy_to_host(&mut normed_host)?;
let sum: f32 = normed_host.iter().sum();
let sum_sq: f32 = normed_host.iter().map(|x| x * x).sum();
eprintln!(
"[CORRECTNESS-002] Normed hidden: first 5 = {:?}, sum = {:.4}, rms = {:.4}",
&normed_host[..5.min(normed_host.len())],
sum,
(sum_sq / normed_host.len() as f32).sqrt()
);
Ok(())
}
/// PAR-023: Fully GPU-resident forward to logits (minimal syncs)
///
/// Runs all transformer layers + output norm + LM head projection entirely on GPU,
/// only downloading the final logits. This eliminates the CPU round-trip for output norm.
///
/// # Sync Count
///
/// - Input embedding upload: 1 sync
/// - All transformer layers: 0 syncs (attention has internal D2D)
/// - Output RMSNorm: 0 syncs (on GPU)
/// - LM head projection: 0 syncs (on GPU)
/// - Logits download: 1 sync
/// - **Total: 2 syncs** vs 3+ syncs (with CPU output norm)
///
/// # Requirements
///
/// Must call `preload_rmsnorm_weights()` and `preload_output_norm()` before use.
/// LM head weights must be pre-cached via `load_quantized_weights("output.weight", ...)`.
///
/// # Arguments
///
/// * `input` - Input embedding [hidden_dim]
/// * `logits` - Output logits buffer [vocab_size]
/// * `position` - Token position for RoPE and KV cache (PAR-070: CORRECTNESS-001 fix)
/// * `num_layers` - Number of transformer layers
/// * `hidden_dim` - Hidden dimension
/// * `intermediate_dim` - FFN intermediate dimension
/// * `vocab_size` - Output vocabulary size
/// * `epsilon` - RMSNorm epsilon
#[allow(clippy::too_many_arguments)]
pub fn forward_all_layers_gpu_to_logits(
&mut self,
input: &[f32],
logits: &mut [f32],
position: u32,
num_layers: usize,
hidden_dim: u32,
intermediate_dim: u32,
vocab_size: u32,
epsilon: f32,
) -> Result<(), GpuError> {
// PERF-002: Debug code removed for performance (was PAR-058-DEBUG)
// NaN checks required D2H transfer on every token - ~10ms overhead each
// 1. Validate all RMSNorm weights are cached (including output norm)
self.validate_rmsnorm_cache_for_logits(num_layers)?;
// 2. Collect all cache key names
let layer_keys: Vec<(String, String)> = (0..num_layers)
.map(|i| {
(
format!("blk.{}.attn_norm.gamma", i),
format!("blk.{}.ffn_norm.gamma", i),
)
})
.collect();
// 3. Upload input embedding - sync point #1
// PAR-044: Check if we can use zero-allocation workspace path
let use_workspace = self.has_workspace()
&& self.has_indexed_weights()
&& self.indexed_layer_weights.len() == num_layers;
let mut hidden_gpu = self.pad_and_upload_input(input)?;
// 4. Chain all transformer layers (no intermediate syncs)
// PAR-044: Use workspace path for zero-allocation forward (fastest)
// PAR-043: Use indexed path if weights are pre-indexed (10x faster per-token)
// PAR-044 FIX: Track which buffer has output to avoid unnecessary D2D copy
// PAR-044: Workspace path enabled - confirmed same performance as indexed path
// See five-whys-gpu-performance-gap for analysis
let workspace_used = if use_workspace {
self.run_workspace_layers(
&hidden_gpu, num_layers, hidden_dim, intermediate_dim, epsilon, position,
)?;
true
} else if self.has_indexed_weights() && self.indexed_layer_weights.len() == num_layers {
hidden_gpu = self.run_indexed_layers(
hidden_gpu, num_layers, hidden_dim, intermediate_dim, epsilon,
)?;
false
} else {
hidden_gpu = self.run_legacy_layers(
hidden_gpu, num_layers, &layer_keys, hidden_dim, intermediate_dim, epsilon,
)?;
false
};
// PERF-002: Debug code removed (was PAR-058-DEBUG hidden state check)
// D2H transfer + NaN check was ~15ms overhead per token
// CORRECTNESS-001: Compare hidden state before output norm
static HIDDEN_DEBUG: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let debug_enabled = *HIDDEN_DEBUG.get_or_init(|| {
std::env::var("GPU_DEBUG")
.map(|v| v == "1")
.unwrap_or(false)
});
self.debug_dump_hidden_state(&hidden_gpu, workspace_used, debug_enabled)?;
// 5. Output RMSNorm on GPU (no sync)
// PAR-PROFILE: Brick timer for output norm
let profiling = self.profiler.is_enabled();
let timer_output_norm = if profiling {
self.start_brick_id(trueno::BrickId::RmsNorm)
} else {
None
};
let normed_hidden = self.apply_output_rmsnorm(
&hidden_gpu, workspace_used, hidden_dim, epsilon,
)?;
// CORRECTNESS-002: Debug normed_hidden output (before LM head)
self.debug_dump_normed_hidden(&normed_hidden, debug_enabled)?;
// PAR-PROFILE: Stop output norm timer, start LM head timer
if profiling {
self.stop_brick_id(timer_output_norm, 1);
}
// 6-7. LM head projection + final sync + download
self.dispatch_lm_head_and_download(
&normed_hidden, logits, vocab_size, hidden_dim, debug_enabled,
)
}
}
include!("logits.rs");
include!("forward_from_forward_from_forward.rs");