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
impl GpuModel {
/// GPU-accelerated forward pass
///
/// Uses HybridScheduler for matrix multiplications.
///
/// # Arguments
///
/// * `token_ids` - Input token IDs
///
/// # Returns
///
/// Logits tensor with shape `[seq_len, vocab_size]`
///
/// # Errors
///
/// Returns error if forward pass fails
pub fn forward_gpu(&mut self, token_ids: &[usize]) -> Result<Vec<f32>> {
if token_ids.is_empty() {
return Err(RealizarError::InvalidShape {
reason: "Token IDs cannot be empty".to_string(),
});
}
let seq_len = token_ids.len();
let hidden_dim = self.config.hidden_dim;
// Step 1: Embed tokens
let mut hidden = Vec::with_capacity(seq_len * hidden_dim);
for &token_id in token_ids {
if token_id >= self.config.vocab_size {
return Err(RealizarError::InvalidShape {
reason: format!(
"Token ID {} out of bounds (vocab_size={})",
token_id, self.config.vocab_size
),
});
}
let offset = token_id * hidden_dim;
hidden.extend_from_slice(&self.embedding_weights[offset..offset + hidden_dim]);
}
// Step 2: Pass through transformer blocks
for block_idx in 0..self.block_weights.len() {
hidden = self.forward_block_idx(&hidden, seq_len, block_idx)?;
}
// Step 3: Final layer norm
hidden = self.layer_norm(&hidden, &self.final_norm_weight, &self.final_norm_bias);
// Step 4: LM head projection
// [seq_len, hidden_dim] @ [hidden_dim, vocab_size] -> [seq_len, vocab_size]
// Phase 22 FIX: Use lm_head_weight_t (transposed) which is [hidden_dim, vocab_size]
// The original lm_head_weight is [vocab_size, hidden_dim] (APR convention)
// IMP-090: Use CPU fallback for large vocab to avoid GPU buffer overflow
let lm_head_elements = hidden_dim * self.config.vocab_size;
let logits = if exceeds_gpu_buffer_limit(lm_head_elements) {
// CPU fallback for large vocab (>256MB weight matrix)
cpu_matmul(
&hidden,
&self.lm_head_weight_t,
seq_len,
hidden_dim,
self.config.vocab_size,
)
} else {
// GPU path for smaller vocab (IMP-1005: use do_matmul for CUDA)
// Clone weights to avoid borrow conflict with &mut self in do_matmul
let lm_weight = self.lm_head_weight_t.clone();
self.do_matmul(
&hidden,
&lm_weight,
seq_len,
hidden_dim,
self.config.vocab_size,
)?
};
// Add bias
let mut output = logits;
for i in 0..seq_len {
for j in 0..self.config.vocab_size {
output[i * self.config.vocab_size + j] += self.lm_head_bias[j];
}
}
Ok(output)
}
/// GPU-accelerated forward pass with layer-by-layer tracing (PMAT-216)
///
/// This method enables diagnostics by capturing activation statistics
/// at each layer, matching the CPU `AprTransformer::forward_traced()` API.
///
/// # Five Whys Root Cause (PMAT-216)
///
/// The GPU path previously lacked tracing, allowing bugs to ship undetected.
/// This method ensures GPU/CPU parity can be verified at each layer.
///
/// # Arguments
///
/// * `token_ids` - Input token IDs
///
/// # Returns
///
/// `ForwardTrace` containing logits and per-layer activation statistics
///
/// # Errors
///
/// Returns error if forward pass fails
pub fn forward_traced_gpu(&mut self, token_ids: &[usize]) -> Result<ForwardTrace> {
if token_ids.is_empty() {
return Err(RealizarError::InvalidShape {
reason: "Token IDs cannot be empty".to_string(),
});
}
let seq_len = token_ids.len();
let hidden_dim = self.config.hidden_dim;
// Step 1: Embed tokens
let mut hidden = Vec::with_capacity(seq_len * hidden_dim);
for &token_id in token_ids {
if token_id >= self.config.vocab_size {
return Err(RealizarError::InvalidShape {
reason: format!(
"Token ID {} out of bounds (vocab_size={})",
token_id, self.config.vocab_size
),
});
}
let offset = token_id * hidden_dim;
hidden.extend_from_slice(&self.embedding_weights[offset..offset + hidden_dim]);
}
let embed_stats = ActivationStats::from_slice(&hidden);
// Step 2: Pass through transformer blocks with tracing
let mut layer_activations = Vec::with_capacity(self.block_weights.len());
for block_idx in 0..self.block_weights.len() {
let layer_trace = self.forward_block_traced(&hidden, seq_len, block_idx)?;
hidden = layer_trace.0;
layer_activations.push(layer_trace.1);
}
// Step 3: Final layer norm
hidden = self.layer_norm(&hidden, &self.final_norm_weight, &self.final_norm_bias);
let final_norm_stats = ActivationStats::from_slice(&hidden);
// Step 4: LM head projection (only last token for logits)
let last_hidden_start = (seq_len - 1) * hidden_dim;
let last_hidden = &hidden[last_hidden_start..last_hidden_start + hidden_dim];
// Use lm_head_weight_t (transposed) for matmul
let logits = cpu_matmul(
last_hidden,
&self.lm_head_weight_t,
1,
hidden_dim,
self.config.vocab_size,
);
// Add bias
let mut logits_with_bias = logits;
for j in 0..self.config.vocab_size {
logits_with_bias[j] += self.lm_head_bias[j];
}
let logits_stats = ActivationStats::from_slice(&logits_with_bias);
Ok(ForwardTrace {
input_tokens: token_ids.iter().map(|&x| x as u32).collect(),
embed_stats,
layer_activations,
final_norm_stats,
logits_stats,
logits: logits_with_bias,
})
}
/// Forward pass through a single block with tracing (PMAT-216)
///
/// Returns (output_hidden, LayerActivation) for diagnostics.
fn forward_block_traced(
&mut self,
input: &[f32],
seq_len: usize,
block_idx: usize,
) -> Result<(Vec<f32>, LayerActivation)> {
let hidden_dim = self.config.hidden_dim;
let intermediate_dim = self.config.intermediate_dim;
let qkv_dim = self.config.qkv_dim();
let block = &self.block_weights[block_idx];
// Pre-norm
let normed = Self::layer_norm_static(
input,
&block.attn_norm_weight,
&block.attn_norm_bias,
hidden_dim,
self.config.eps,
);
let attn_norm_stats = ActivationStats::from_slice(&normed);
// QKV projection
let qkv_weight = self.block_weights[block_idx].qkv_weight.clone();
let mut qkv = self.do_matmul(&normed, &qkv_weight, seq_len, hidden_dim, qkv_dim)?;
// Apply RoPE
let num_heads = self.config.num_heads;
let num_kv_heads = self.config.num_kv_heads;
let head_dim = self.config.head_dim();
let kv_dim = self.config.kv_dim();
let rope_theta = self.config.rope_theta;
for pos in 0..seq_len {
let qkv_start = pos * qkv_dim;
Self::apply_rope_inline(
&mut qkv[qkv_start..qkv_start + hidden_dim],
num_heads,
head_dim,
rope_theta,
pos,
);
Self::apply_rope_inline(
&mut qkv[qkv_start + hidden_dim..qkv_start + hidden_dim + kv_dim],
num_kv_heads,
head_dim,
rope_theta,
pos,
);
}
let qkv_stats = ActivationStats::from_slice(&qkv);
// Attention
let attn_out = self.optimized_gqa_attention(&qkv, seq_len)?;
// Clone all weights/biases upfront to avoid borrow conflicts with do_matmul
let out_weight = self.block_weights[block_idx].out_weight.clone();
let out_bias = self.block_weights[block_idx].out_bias.clone();
let ffn_norm_weight = self.block_weights[block_idx].ffn_norm_weight.clone();
let ffn_norm_bias = self.block_weights[block_idx].ffn_norm_bias.clone();
let ffn_fc1_weight = self.block_weights[block_idx].ffn_fc1_weight.clone();
let ffn_fc1_bias = self.block_weights[block_idx].ffn_fc1_bias.clone();
let ffn_gate_weight = self.block_weights[block_idx].ffn_gate_weight.clone();
let ffn_fc2_weight = self.block_weights[block_idx].ffn_fc2_weight.clone();
let ffn_fc2_bias = self.block_weights[block_idx].ffn_fc2_bias.clone();
// Output projection
let projected = self.do_matmul(&attn_out, &out_weight, seq_len, hidden_dim, hidden_dim)?;
// Residual 1
let mut residual1: Vec<f32> = input
.iter()
.zip(projected.iter())
.enumerate()
.map(|(i, (&inp, &proj))| inp + proj + out_bias[i % hidden_dim])
.collect();
let attn_out_stats = ActivationStats::from_slice(&residual1);
// FFN pre-norm
let ffn_normed = Self::layer_norm_static(
&residual1,
&ffn_norm_weight,
&ffn_norm_bias,
hidden_dim,
self.config.eps,
);
let ffn_norm_stats = ActivationStats::from_slice(&ffn_normed);
// FFN
let ffn_output: Vec<f32> = if let Some(gate_weight) = ffn_gate_weight {
// SwiGLU
let up_out = self.do_matmul(
&ffn_normed,
&ffn_fc1_weight,
seq_len,
hidden_dim,
intermediate_dim,
)?;
let gate_out = self.do_matmul(
&ffn_normed,
&gate_weight,
seq_len,
hidden_dim,
intermediate_dim,
)?;
let activated: Vec<f32> = gate_out
.iter()
.zip(up_out.iter())
.map(|(&g, &u)| {
let silu = g / (1.0 + (-g).exp());
silu * u
})
.collect();
let down = self.do_matmul(
&activated,
&ffn_fc2_weight,
seq_len,
intermediate_dim,
hidden_dim,
)?;
down.iter()
.enumerate()
.map(|(i, &d)| d + ffn_fc2_bias[i % hidden_dim])
.collect()
} else {
// Standard GELU MLP
let up_out = self.do_matmul(
&ffn_normed,
&ffn_fc1_weight,
seq_len,
hidden_dim,
intermediate_dim,
)?;
let activated: Vec<f32> = up_out
.iter()
.enumerate()
.map(|(i, &x)| {
let biased = x + ffn_fc1_bias[i % intermediate_dim];
0.5 * biased
* (1.0 + (0.797_884_6 * (biased + 0.044_715 * biased.powi(3))).tanh())
})
.collect();
let down = self.do_matmul(
&activated,
&ffn_fc2_weight,
seq_len,
intermediate_dim,
hidden_dim,
)?;
down.iter()
.enumerate()
.map(|(i, &d)| d + ffn_fc2_bias[i % hidden_dim])
.collect()
};
let ffn_out_stats = ActivationStats::from_slice(&ffn_output);
// Residual 2
for i in 0..residual1.len() {
residual1[i] += ffn_output[i];
}
let output_stats = ActivationStats::from_slice(&residual1);
Ok((
residual1,
LayerActivation {
layer_idx: block_idx,
attn_norm_stats,
qkv_stats,
attn_out_stats,
ffn_norm_stats,
ffn_out_stats,
output_stats,
},
))
}
}