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
impl CudaExecutor {
/// PAR-043: Transformer layer using pre-indexed device pointers (async, no sync)
///
/// This is the **hot path** for decode. Eliminates ALL string formatting and HashMap
/// lookups (7 per layer = ~224 allocations/lookups per forward pass for 32 layers).
///
/// Measured improvement: ~10ms per token overhead → ~0.1ms per token overhead
///
/// # Arguments
///
/// * `input` - GPU buffer with hidden states [hidden_dim]
/// * `layer_idx` - Layer index (for incremental attention KV cache)
/// * `layer_weights` - Pre-indexed pointers from `indexed_layer_weights`
/// * `hidden_dim` - Model hidden dimension
/// * `intermediate_dim` - FFN intermediate dimension
/// * `epsilon` - RMSNorm epsilon
#[allow(clippy::too_many_arguments)]
pub fn transformer_layer_indexed(
&mut self,
input: &GpuBuffer<f32>,
layer_idx: usize,
layer_weights: &ValidatedLayerWeights,
hidden_dim: u32,
intermediate_dim: u32,
epsilon: f32,
) -> Result<GpuBuffer<f32>, GpuError> {
// PROHIBITION-OF-MIRACLES (T-COV-95): Validate pointers BEFORE kernel launch
// Null pointers corrupt GPU context - fail loudly at API boundary
if layer_weights.attn_norm_ptr == 0 {
return Err(GpuError::InvalidParameter(
"attn_norm_ptr is null (0)".into(),
));
}
if layer_weights.attn_q_ptr == 0 {
return Err(GpuError::InvalidParameter("attn_q_ptr is null (0)".into()));
}
if layer_weights.attn_k_ptr == 0 {
return Err(GpuError::InvalidParameter("attn_k_ptr is null (0)".into()));
}
if layer_weights.attn_v_ptr == 0 {
return Err(GpuError::InvalidParameter("attn_v_ptr is null (0)".into()));
}
if layer_weights.attn_output_ptr == 0 {
return Err(GpuError::InvalidParameter(
"attn_output_ptr is null (0)".into(),
));
}
if layer_weights.ffn_norm_ptr == 0 {
return Err(GpuError::InvalidParameter(
"ffn_norm_ptr is null (0)".into(),
));
}
if layer_weights.ffn_gate_ptr == 0 {
return Err(GpuError::InvalidParameter(
"ffn_gate_ptr is null (0)".into(),
));
}
if layer_weights.ffn_up_ptr == 0 {
return Err(GpuError::InvalidParameter("ffn_up_ptr is null (0)".into()));
}
if layer_weights.ffn_down_ptr == 0 {
return Err(GpuError::InvalidParameter(
"ffn_down_ptr is null (0)".into(),
));
}
// 1. Pre-attention RMSNorm using indexed gamma pointer
let normed = self.rmsnorm_gpu_ptr(
input,
layer_weights.attn_norm_ptr,
layer_weights.attn_norm_len,
hidden_dim,
epsilon,
)?;
// 2. Q/K/V projections using indexed pointers (no sync)
let q_dim = (self.kv_num_heads * self.kv_head_dim) as u32;
let kv_dim = (self.kv_num_kv_heads * self.kv_head_dim) as u32;
let q =
self.q4k_gemv_indexed_async(layer_weights.attn_q_ptr, &normed, q_dim, hidden_dim)?;
let k =
self.q4k_gemv_indexed_async(layer_weights.attn_k_ptr, &normed, kv_dim, hidden_dim)?;
// PMAT-232 CONTRACT: Exhaustive dispatch — no catch-all. See tensor-layout-v1.yaml quant_dispatch.
// NOTE: Indexed async path only has Q4K/Q6K kernels. Other types must use workspace path.
let v = match layer_weights.attn_v_qtype {
WeightQuantType::Q6K => {
self.q6k_gemv_indexed_async(layer_weights.attn_v_ptr, &normed, kv_dim, hidden_dim)?
},
WeightQuantType::Q4K => {
self.q4k_gemv_indexed_async(layer_weights.attn_v_ptr, &normed, kv_dim, hidden_dim)?
},
WeightQuantType::Q5K
| WeightQuantType::Q8_0
| WeightQuantType::Q4_0
| WeightQuantType::Q5_0
| WeightQuantType::Q4_1
| WeightQuantType::F32 => {
return Err(GpuError::InvalidParameter(format!(
"PMAT-232: V qtype {:?} not supported in indexed async path (use workspace path)",
layer_weights.attn_v_qtype
)));
},
};
// 3. Incremental attention (has internal sync for KV cache update)
let (attn_out, _seq_len) = self.incremental_attention_async(layer_idx, &q, &k, &v)?;
// 4. Output projection using indexed pointer (no sync)
let projected = self.q4k_gemv_indexed_async(
layer_weights.attn_output_ptr,
&attn_out,
hidden_dim,
q_dim,
)?;
// 5. First residual add (no sync)
let residual1 = self.residual_add_gpu(input, &projected, hidden_dim)?;
// 6. Pre-FFN RMSNorm using indexed gamma pointer
let ffn_normed = self.rmsnorm_gpu_ptr(
&residual1,
layer_weights.ffn_norm_ptr,
layer_weights.ffn_norm_len,
hidden_dim,
epsilon,
)?;
// 7. FFN SwiGLU using indexed pointers (no sync)
let ffn_out = self.fused_ffn_swiglu_indexed_gpu(
&ffn_normed,
layer_weights.ffn_gate_ptr,
layer_weights.ffn_up_ptr,
layer_weights.ffn_down_ptr,
hidden_dim,
intermediate_dim,
)?;
// 8. Second residual add (no sync)
let output = self.residual_add_gpu(&residual1, &ffn_out, hidden_dim)?;
Ok(output)
}
/// PAR-044: Transformer layer with zero allocations using workspace buffers
///
/// Uses pre-allocated workspace buffers for all intermediate tensors.
/// Eliminates ~288 buffer allocations per token.
///
/// # Buffer Usage
///
/// Workspace buffers used:
/// - hidden_buf1: normed, projected, ffn_normed, ffn_out (reused)
/// - hidden_buf2: residual1, final output
/// - q_buf: Q projection, then attention output
/// - k_buf: K projection
/// - v_buf: V projection
/// - ffn_gate_buf: FFN gate projection
/// - ffn_up_buf: FFN up projection
/// - ffn_act_buf: SwiGLU activation result
///
/// # Returns
///
/// Returns `Ok(())` on success. Output is written to workspace.hidden_buf2.
/// PAR-054: Transformer layer for graph capture (no debug output)
/// PAR-070: Takes position but uses indirect mode (reads from position_buf) during graph capture
#[allow(clippy::too_many_arguments)]
pub(crate) fn transformer_layer_workspace_for_capture(
&mut self,
input: &GpuBuffer<f32>,
layer_idx: usize,
layer_weights: &ValidatedLayerWeights,
hidden_dim: u32,
intermediate_dim: u32,
epsilon: f32,
position: u32,
) -> Result<(), GpuError> {
self.transformer_layer_workspace_inner(
input,
layer_idx,
layer_weights,
hidden_dim,
intermediate_dim,
epsilon,
position,
true,
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn transformer_layer_workspace(
&mut self,
input: &GpuBuffer<f32>,
layer_idx: usize,
layer_weights: &ValidatedLayerWeights,
hidden_dim: u32,
intermediate_dim: u32,
epsilon: f32,
position: u32, // PAR-070: Explicit position for RoPE and KV cache
) -> Result<(), GpuError> {
// PERF-001: skip_debug=true disables stream.synchronize() calls and debug prints
// that were causing ~4x slowdown (70 tok/s -> target 280+ tok/s)
// CORRECTNESS-001: Set GPU_DEBUG=1 to enable layer-by-layer debug output
static GPU_DEBUG: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let skip_debug = !*GPU_DEBUG.get_or_init(|| {
std::env::var("GPU_DEBUG")
.map(|v| v == "1")
.unwrap_or(false)
});
self.transformer_layer_workspace_inner(
input,
layer_idx,
layer_weights,
hidden_dim,
intermediate_dim,
epsilon,
position,
skip_debug,
)
}
}