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
//! The real-checkpoint Gemma 4 decode flow for [`RealForwardRunner`].
mod attn;
mod attn_batch;
mod moe;
mod moe_batch;
mod prefill;
mod shared_expert;
mod state;
pub(crate) use state::RealGemmaState;
use std::time::Instant;
use foundation::LogitValue;
use crate::real_forward::RealForwardRunner;
use crate::real_forward_dispatch::{encode_embed_any, encode_gemv_any};
use crate::real_forward_types::RealForwardError;
use crate::real_forward_utils::norm_view;
use crate::resid_capture::encode_resid_capture;
use crate::steering::encode_steering;
const RMS_EPS: f32 = 1e-6;
/// Keeps asynchronously submitted routed work from outliving the no-copy
/// model buffers it reads when a later encode operation returns an error.
#[derive(Default)]
struct PendingRouted(Option<gpu::CommittedPass>);
impl PendingRouted {
fn take(&mut self) -> Option<gpu::CommittedPass> {
self.0.take()
}
fn set(&mut self, pass: gpu::CommittedPass) {
debug_assert!(self.0.is_none(), "routed pipeline depth is 1");
self.0 = Some(pass);
}
}
impl Drop for PendingRouted {
fn drop(&mut self) {
if let Some(pending) = self.0.take() {
pending.wait();
}
}
}
impl RealForwardRunner {
/// Times the whole forward pass into `phases.total_nanos`; the inner
/// function accumulates the per-phase buckets it is carved into.
pub(crate) fn produce_real_gemma4(
&mut self,
token: i32,
position: usize,
logits: &mut [LogitValue],
) -> Result<(), RealForwardError> {
let started = Instant::now();
let result = self.produce_real_gemma4_inner(token, position, logits);
self.phases.calls += 1;
self.phases.total_nanos += started.elapsed().as_nanos() as u64;
result
}
fn produce_real_gemma4_inner(
&mut self,
token: i32,
position: usize,
logits: &mut [LogitValue],
) -> Result<(), RealForwardError> {
let arch = self.arch.clone();
let hidden = arch.hidden_size as usize;
let inter = arch.intermediate_size as usize;
let moe_inter = arch.moe_intermediate_size as u32;
let vocab = arch.vocab_size as usize;
let num_experts = arch.num_experts as usize;
let top_k = arch.top_k_experts as usize;
let use_silu = arch.hidden_activation.contains("silu");
let embed_scale = if arch.embedding_scaled_by_sqrt_hidden {
(hidden as f32).sqrt()
} else {
1.0
};
let gpu_err = RealForwardError::Gpu;
if position != self.kv.position() {
return Err(RealForwardError::Unsupported(format!(
"non-sequential position {position}; KV cache is at {}",
self.kv.position()
)));
}
if (token as usize) >= vocab {
return Err(RealForwardError::Unsupported(format!(
"token id {token} outside vocab {vocab}"
)));
}
let seq_len = (position + 1) as u32;
let embed_name = "language_model.model.embed_tokens.weight";
let base = self.index.header.index_size;
let mut pass = self.context.begin_pass_labeled("cb1 (attn+router)");
encode_embed_any(
&mut self.context,
&pass,
&self.weights,
&self.index,
embed_name,
(&self.scratch.x, 0),
token as u32,
hidden as u32,
vocab,
embed_scale,
)?;
let mut pending_routed = PendingRouted::default();
let dense = self.real.as_ref().expect("real state present").dense;
let sequential = moe::RoutedSlot::sequential();
let h1 = self.real.as_ref().expect("real state present").h1.clone();
if dense {
// THE DENSE TRUNK: attention (tail without the router pieces),
// then one FFN pass. The pass shape mirrors the MoE loop's
// non-pipelined form -- cb1 commits and waits (the tail's
// readbacks, if any, sit behind it), the FFN pass stays open
// and carries the next layer's attention, the head rides the
// last one. No routed pipeline, no expert cache: there is
// nothing to overlap. The FFN pass's GPU time lands in
// `final_cb` because it commits with the head's command buffer;
// `TURBOSPARK_PHASES` on a dense install reads accordingly.
for layer in 0..arch.num_layers as usize {
self.encode_gemma4_layer_attn_and_router(&pass, layer, position, seq_len, base, 0)?;
let cb1 = pass.commit().waiting_on_drop();
let t_wait = Instant::now();
self.phases.cb1_gpu_nanos += (cb1.wait_with_gpu_time() * 1e9) as u64;
self.phases.gpu_wait_nanos += t_wait.elapsed().as_nanos() as u64;
pass = self.context.begin_pass_labeled("dense ffn cb");
self.encode_gemma4_dense_ffn(&pass, layer, hidden, inter, use_silu)?;
encode_steering(
&mut self.context,
&pass,
&self.scratch,
self.steering.as_ref(),
layer,
hidden,
1,
0,
)?;
encode_resid_capture(
&mut self.context,
&pass,
&self.scratch,
self.resid_capture.as_ref(),
layer,
hidden,
0,
)?;
}
} else {
for layer in 0..arch.num_layers as usize {
self.encode_gemma4_layer_attn_and_router(&pass, layer, position, seq_len, base, 0)?;
let cb1 = pass.commit().waiting_on_drop();
let _shared_pass = if self.shared_cb_overlap {
Some(self.encode_shared_expert_branch(
layer,
hidden,
inter,
use_silu,
&sequential,
(&h1, 0),
)?)
} else {
None
};
let t_wait = Instant::now();
self.phases.cb1_gpu_nanos += (cb1.wait_with_gpu_time() * 1e9) as u64;
self.phases.gpu_wait_nanos += t_wait.elapsed().as_nanos() as u64;
if let Some(pending) = pending_routed.take() {
let t_retire = Instant::now();
self.phases.routed_cb_gpu_nanos += (pending.wait_with_gpu_time() * 1e9) as u64;
self.phases.pipeline_wait_nanos += t_retire.elapsed().as_nanos() as u64;
}
pass = self.context.begin_pass_labeled("routed cb");
self.encode_gemma4_layer_routed_moe(
&pass,
layer,
hidden,
inter,
moe_inter,
num_experts,
top_k,
use_silu,
&sequential,
)?;
// The layer's OUTPUT, on the SAME "routed cb" pass the routed
// tail just encoded into: `encode_gemma4_layer_routed_moe`
// ends with `layer_scalar`'s `encode_scalar_mul` over the
// whole accumulated residual, which is the true end of this
// layer's contribution and what the next layer's attention
// reads. The sequential decode path is always token 0 of
// `scratch.x`, so the offset is 0; the chunked-prefill driver
// (`prefill.rs`) and the batched-routed tail
// (`moe_batch.rs`) are the other two call sites, each at
// their own token's offset.
encode_steering(
&mut self.context,
&pass,
&self.scratch,
self.steering.as_ref(),
layer,
hidden,
1,
0,
)?;
encode_resid_capture(
&mut self.context,
&pass,
&self.scratch,
self.resid_capture.as_ref(),
layer,
hidden,
0,
)?;
if self.routed_pipeline {
pending_routed.set(pass.commit().waiting_on_drop());
pass = self.context.begin_pass_labeled("cb1 (attn+router)");
}
}
if let Some(pending) = pending_routed.take() {
let t_retire = Instant::now();
self.phases.routed_cb_gpu_nanos += (pending.wait_with_gpu_time() * 1e9) as u64;
self.phases.pipeline_wait_nanos += t_retire.elapsed().as_nanos() as u64;
}
}
if !self.skip_head {
pass.relabel("final cb (head)");
let final_norm = norm_view(
&self.weights,
&self.index,
"language_model.model.norm.weight",
hidden,
)?;
gpu::encode_rms_norm_bf16w(
&mut self.context,
&pass,
(&self.scratch.x, 0),
final_norm,
(&self.scratch.normed, 0),
hidden as u32,
RMS_EPS,
)
.map_err(gpu_err)?;
let head_name = if arch.tie_word_embeddings {
embed_name.to_string()
} else {
"language_model.lm_head.weight".to_string()
};
encode_gemv_any(
&mut self.context,
&pass,
&self.weights,
&self.index,
&head_name,
vocab,
hidden,
(&self.scratch.normed, 0),
(&self.scratch.logits, 0),
)?;
if arch.final_logit_softcap > 0.0 {
gpu::encode_logit_softcap(
&mut self.context,
&pass,
(&self.scratch.logits, 0),
arch.final_logit_softcap as f32,
vocab as u32,
)
.map_err(gpu_err)?;
}
} else {
pass.relabel("final cb (prefill, no head)");
}
let t_wait = Instant::now();
self.phases.final_cb_gpu_nanos += (pass.commit_and_wait_with_gpu_time() * 1e9) as u64;
self.phases.final_wait_nanos += t_wait.elapsed().as_nanos() as u64;
self.kv.advance();
// The command buffer has been waited on, so every layer's capture
// region is final. `record_pass` keeps at most one snapshot per
// generation and decides which by `skip_head` -- called on every
// pass rather than guarded here, matching the qwen and llama flows
// (`crates/runtime/CLAUDE.md` Gotcha 20: the encode half alone
// gives "no non-prefill pass ran; wrote nothing").
let skip_head = self.skip_head;
if let Some(capture) = self.resid_capture.as_mut() {
capture.record_pass(position, skip_head);
}
if self.skip_head {
return Ok(());
}
// Read the head STRAIGHT into the caller's slice. The owned-`Vec`
// form of this cost a 512 KiB allocation and a second 512 KiB copy
// per decoded token, outside every profiling bucket in this repo
// (AGENTS.md Gotcha 23). The length check moves ahead of the read
// because it was only ever comparing `vocab` to `logits.len()`.
if vocab != logits.len() {
return Err(RealForwardError::Unsupported(format!(
"vocab mismatch: model has {}, caller expected {}",
vocab,
logits.len()
)));
}
gpu::read_buffer_f16_into(&self.scratch.logits, 0, logits);
Ok(())
}
}