ferrum-models 0.7.5

Model architectures (LLaMA, Qwen, BERT) for Ferrum inference
Documentation
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
//! Qwen3-MoE unified mixed-batch forward — vLLM-style.
//!
//! Design (per vLLM v1 `gpu_model_runner.py`):
//! - SHARED scratch: residual / norm_out / qkv_out / moe_out are the
//!   same buffers the legacy decode/prefill paths use, sized for
//!   `max_tokens` at scratch construction. No per-call realloc.
//! - Flat `[M_total, hidden]` activations, ONE forward call per iter.
//! - Per-call index buffers (cu_seqlens_q / pos_offsets / block_tables)
//!   are tiny (max_seqs × few bytes), pre-allocated by
//!   `ensure_unified_scratch`.
//! - Variable q_len handled inside the varlen attention kernel.
//! - MoE block: ONE `moe_forward_bucketed` call at M = M_total — MoE
//!   doesn't care about per-request boundaries (every token routes
//!   independently).
//!
//! Returns one `Option<Vec<f32>>` per item: `Some(logits)` only for
//! items whose `is_final_chunk = true`.

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

use ferrum_bench_core::{global_profile, profile_fields_from_json};
use ferrum_interfaces::kv_dtype::KvDtypeKind;
use ferrum_kernels::backend::{Backend, BackendPagedKv, MoeLlmBackend};

use super::qwen3_moe::Qwen3MoeModel;
use super::qwen3_moe_forward_unified_layer::UnifiedLayerParams;

#[derive(Default)]
pub(crate) struct UnifiedLayerProfile {
    pub(super) input_norm_us: u64,
    pub(super) qkv_us: u64,
    pub(super) split_cache_us: u64,
    pub(super) attn_us: u64,
    pub(super) o_proj_us: u64,
    pub(super) post_norm_us: u64,
    pub(super) router_us: u64,
    pub(super) moe_us: u64,
    pub(super) residual_add_us: u64,
    pub(super) final_norm_us: u64,
    pub(super) sample_gather_us: u64,
    pub(super) lm_head_us: u64,
    pub(super) readback_us: u64,
}

static UNIFIED_LAYER_PROF_CALLS: AtomicU64 = AtomicU64::new(0);

pub(super) fn unified_stage_start<B: Backend>(
    ctx: &mut B::Context,
    enabled: bool,
) -> Option<Instant> {
    if enabled {
        B::sync(ctx);
        Some(Instant::now())
    } else {
        None
    }
}

pub(super) fn unified_stage_finish<B: Backend>(
    ctx: &mut B::Context,
    start: Option<Instant>,
) -> u64 {
    if let Some(start) = start {
        B::sync(ctx);
        start.elapsed().as_micros() as u64
    } else {
        0
    }
}

fn log_unified_layer_profile(
    prof: &UnifiedLayerProfile,
    m_total: usize,
    num_seqs: usize,
    num_sampled: usize,
    num_layers: usize,
    every: u64,
) {
    let call = UNIFIED_LAYER_PROF_CALLS.fetch_add(1, Ordering::Relaxed) + 1;
    if call > 8 && call % every != 0 {
        return;
    }

    let layer_sum = prof.input_norm_us
        + prof.qkv_us
        + prof.split_cache_us
        + prof.attn_us
        + prof.o_proj_us
        + prof.post_norm_us
        + prof.router_us
        + prof.moe_us
        + prof.residual_add_us;
    let final_sum = prof.final_norm_us + prof.sample_gather_us + prof.lm_head_us + prof.readback_us;
    eprintln!(
        "[unified-layer-prof] call#{} m={} seqs={} sampled={} layers={} layer_sum={}us final_sum={}us \
         input_norm={} qkv={} split_cache={} attn={} o_proj={} post_norm={} router={} moe={} residual_add={} \
         final_norm={} sample_gather={} lm_head={} readback={} (us)",
        call,
        m_total,
        num_seqs,
        num_sampled,
        num_layers,
        layer_sum,
        final_sum,
        prof.input_norm_us,
        prof.qkv_us,
        prof.split_cache_us,
        prof.attn_us,
        prof.o_proj_us,
        prof.post_norm_us,
        prof.router_us,
        prof.moe_us,
        prof.residual_add_us,
        prof.final_norm_us,
        prof.sample_gather_us,
        prof.lm_head_us,
        prof.readback_us,
    );
    let profile = global_profile();
    if profile.is_enabled() {
        let _ = profile.push_event(
            "unified_layer_prof",
            profile_fields_from_json(serde_json::json!({
                "call": call,
                "m": m_total,
                "seqs": num_seqs,
                "sampled": num_sampled,
                "layers": num_layers,
            })),
            profile_fields_from_json(serde_json::json!({
                "layer_sum": layer_sum,
                "final_sum": final_sum,
                "input_norm": prof.input_norm_us,
                "qkv": prof.qkv_us,
                "split_cache": prof.split_cache_us,
                "attn": prof.attn_us,
                "o_proj": prof.o_proj_us,
                "post_norm": prof.post_norm_us,
                "router": prof.router_us,
                "moe": prof.moe_us,
                "residual_add": prof.residual_add_us,
                "final_norm": prof.final_norm_us,
                "sample_gather": prof.sample_gather_us,
                "lm_head": prof.lm_head_us,
                "readback": prof.readback_us,
            })),
            false,
        );
    }
}

impl<B, K> Qwen3MoeModel<B, K>
where
    B: MoeLlmBackend + BackendPagedKv,
    K: KvDtypeKind,
{
    pub(crate) fn unified_forward_internal(
        &mut self,
        items: &[(String, Vec<u32>, usize, bool)],
    ) -> Vec<Option<Vec<f32>>> {
        if items.is_empty() {
            return Vec::new();
        }

        // ── 1. Shared helpers for per-item bookkeeping ──
        let (q_lens, cu_seqlens_q, m_total) =
            crate::common::decoder_unified::compute_cu_seqlens_q(items);
        let pos_offsets = crate::common::decoder_unified::compute_pos_offsets(items);
        let seq_lens: Vec<u32> = pos_offsets
            .iter()
            .zip(q_lens.iter())
            .map(|(&pos, &q_len)| pos + q_len as u32)
            .collect();
        let max_q_len = q_lens.iter().copied().max().unwrap_or(0);
        let max_kv_len = crate::common::decoder_unified::compute_max_kv_len(items);
        let num_seqs = items.len();
        let final_indices =
            crate::common::decoder_unified::compute_final_indices(items, &cu_seqlens_q);
        let num_sampled = final_indices.len();

        // ── 2. KV cache discovery ──
        for (cid, _, _, _) in items {
            self.ensure_kv(cid);
        }
        if self.paged_pools.is_none() {
            panic!(
                "Qwen3MoeModel::unified_forward_internal called without paged_pools; \
                 set FERRUM_METAL_PAGED_KV=1"
            );
        }

        // ── 3. Snapshot config + ensure unified INDEX scratch ──
        let h = self.cfg.base.hidden_size;
        let nh = self.cfg.base.num_heads;
        let nkv = self.cfg.base.num_kv_heads;
        let hd = self.cfg.base.head_dim;
        let eps = self.cfg.base.rms_norm_eps;
        let qk_mode: i32 = if self.cfg.base.has_qk_norm { 1 } else { 2 };
        let vocab = self.cfg.base.vocab_size;
        let num_layers = self.cfg.base.num_layers;
        let inter = self.cfg.expert_intermediate_size;
        let top_k = self.cfg.num_experts_per_tok;
        let n_exp = self.cfg.num_experts;
        let norm_topk_prob = self.cfg.norm_topk_prob;

        let (paged_max_seqs, max_blocks_per_seq) = self
            .paged_dims
            .expect("paged_dims missing — ensure_kv must set this");
        let block_size = 16usize;
        debug_assert!(paged_max_seqs >= num_seqs);
        debug_assert!(m_total <= self.scratch.max_tokens);
        let max_seqs = paged_max_seqs.max(num_seqs);
        let cfg_clone = self.cfg.clone();
        self.scratch
            .ensure_unified_scratch(&cfg_clone, max_seqs, max_blocks_per_seq);

        let mut ctx = B::new_context();

        // ── 4. Take legacy residual, embed all tokens into it ──
        let mut residual = self
            .scratch
            .residual
            .take()
            .expect("scratch.residual missing");
        let all_tokens = crate::common::decoder_unified::concat_q_tokens(items);
        debug_assert_eq!(all_tokens.len(), m_total);
        B::embedding_lookup(&mut ctx, &self.embed, &all_tokens, &mut residual, h);

        // ── 5. Upload index buffers ──
        {
            let csq = self
                .scratch
                .unified_cu_seqlens_q
                .as_mut()
                .expect("unified_cu_seqlens_q missing");
            B::write_typed::<u32>(&mut ctx, csq, &cu_seqlens_q);
        }
        {
            let po = self
                .scratch
                .unified_pos_offsets
                .as_mut()
                .expect("unified_pos_offsets missing");
            B::write_typed::<u32>(&mut ctx, po, &pos_offsets);
        }
        {
            let sl = self
                .scratch
                .unified_seq_lens
                .as_mut()
                .expect("unified_seq_lens missing");
            B::write_typed::<u32>(&mut ctx, sl, &seq_lens);
        }
        let stacked =
            crate::common::decoder_unified::stack_block_tables(items, max_blocks_per_seq, |cid| {
                self.kv_caches
                    .get(cid)
                    .expect("kv cache missing for unified item")[0]
                    .paged_block_indices
                    .clone()
            });
        {
            let bt = self
                .scratch
                .unified_block_tables
                .as_mut()
                .expect("unified_block_tables missing");
            B::write_typed::<u32>(&mut ctx, bt, &stacked);
        }

        let use_vllm_tiled_q4 =
            self.use_vllm_paged_attn && self.runtime_env.vllm_varlen_tiled_q4 && m_total > num_seqs;
        let mut tile_q4_count = 0usize;
        if use_vllm_tiled_q4 {
            let mut tile_seqs = Vec::with_capacity(m_total);
            let mut tile_starts = Vec::with_capacity(m_total);
            for (seq_idx, &q_len) in q_lens.iter().enumerate() {
                for start in (0..q_len).step_by(4) {
                    tile_seqs.push(seq_idx as u32);
                    tile_starts.push(start as u32);
                }
            }
            tile_q4_count = tile_seqs.len();
            let tile_seqs_buf = self
                .scratch
                .unified_tile_q4_seqs
                .as_mut()
                .expect("unified_tile_q4_seqs missing");
            B::write_typed::<u32>(&mut ctx, tile_seqs_buf, &tile_seqs);
            let tile_starts_buf = self
                .scratch
                .unified_tile_q4_starts
                .as_mut()
                .expect("unified_tile_q4_starts missing");
            B::write_typed::<u32>(&mut ctx, tile_starts_buf, &tile_starts);
        }

        // Pre-grow Marlin gather scratch for worst-case GEMM input m.
        let max_marlin_required = m_total * inter;
        B::pregrow_marlin_gather_scratch(&mut ctx, max_marlin_required);
        B::sync(&mut ctx);

        // ── 6. Layer loop ──
        let mut layer_prof = if self
            .runtime_env
            .unified_layer_prof_selected(m_total, num_seqs)
        {
            Some(UnifiedLayerProfile::default())
        } else {
            None
        };
        for li in 0..num_layers {
            self.unified_forward_layer(UnifiedLayerParams {
                ctx: &mut ctx,
                li,
                residual: &mut residual,
                m_total,
                num_seqs,
                max_q_len,
                max_kv_len,
                max_blocks_per_seq,
                block_size,
                nh,
                nkv,
                hd,
                h,
                inter,
                eps,
                qk_mode,
                top_k,
                n_exp,
                norm_topk_prob,
                use_vllm_tiled_q4,
                tile_q4_count,
                prof: layer_prof.as_mut(),
            });
        }

        // ── 7. Final rms_norm + lm_head on per-item last tokens ──
        let prof_enabled = layer_prof.is_some();
        let t_final_norm = unified_stage_start::<B>(&mut ctx, prof_enabled);
        B::rms_norm(
            &mut ctx,
            &residual,
            &self.final_norm_w,
            eps,
            &mut self.scratch.norm_out,
            m_total,
            h,
        );
        if let Some(prof) = layer_prof.as_mut() {
            prof.final_norm_us += unified_stage_finish::<B>(&mut ctx, t_final_norm);
        }

        if num_sampled > 0 {
            let packed_normed = self
                .scratch
                .unified_packed_normed
                .as_mut()
                .expect("unified_packed_normed missing");
            let t_sample_gather = unified_stage_start::<B>(&mut ctx, prof_enabled);
            for (j, &(_, global)) in final_indices.iter().enumerate() {
                B::copy_slice(
                    &mut ctx,
                    &self.scratch.norm_out,
                    global * h,
                    packed_normed,
                    j * h,
                    h,
                );
            }
            if let Some(prof) = layer_prof.as_mut() {
                prof.sample_gather_us += unified_stage_finish::<B>(&mut ctx, t_sample_gather);
            }
            let packed_logits = self
                .scratch
                .unified_packed_logits
                .as_mut()
                .expect("unified_packed_logits missing");
            let t_lm_head = unified_stage_start::<B>(&mut ctx, prof_enabled);
            self.lm_head
                .forward(&mut ctx, packed_normed, packed_logits, num_sampled);
            if let Some(prof) = layer_prof.as_mut() {
                prof.lm_head_us += unified_stage_finish::<B>(&mut ctx, t_lm_head);
            }
        }

        // ── 8. Sync + readback per-item logits ──
        let t_readback = unified_stage_start::<B>(&mut ctx, prof_enabled);
        B::sync(&mut ctx);
        let mut out: Vec<Option<Vec<f32>>> = (0..items.len()).map(|_| None).collect();
        if num_sampled > 0 {
            let packed_logits = self
                .scratch
                .unified_packed_logits
                .as_ref()
                .expect("unified_packed_logits missing");
            if self.runtime_env.unified_greedy_argmax {
                let tokens = B::argmax_rows_f16(&mut ctx, packed_logits, num_sampled, vocab)
                    .expect("Qwen3Moe unified: argmax_rows_f16");
                for (j, &(orig_idx, _)) in final_indices.iter().enumerate() {
                    out[orig_idx] = Some(vec![tokens[j] as f32]);
                }
            } else {
                let logits_flat = B::to_vec(packed_logits, num_sampled * vocab);
                for (j, &(orig_idx, _)) in final_indices.iter().enumerate() {
                    let row = logits_flat[j * vocab..(j + 1) * vocab].to_vec();
                    out[orig_idx] = Some(row);
                }
            }
        }
        if let Some(prof) = layer_prof.as_mut() {
            prof.readback_us += unified_stage_finish::<B>(&mut ctx, t_readback);
            log_unified_layer_profile(
                prof,
                m_total,
                num_seqs,
                num_sampled,
                num_layers,
                self.runtime_env.unified_layer_prof_every,
            );
        }

        // ── 9. Bump cache.len per item ──
        for (i, (cid, _, _, _)) in items.iter().enumerate() {
            let caches = self
                .kv_caches
                .get_mut(cid)
                .expect("kv cache missing post-loop");
            for c in caches.iter_mut() {
                c.len += q_lens[i];
            }
        }

        // ── 10. Restore residual ──
        self.scratch.residual = Some(residual);

        out
    }
}