ferrum-models 0.7.7

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
442
443
444
445
446
447
448
449
450
451
452
453
//! One Qwen3-MoE unified forward transformer layer.

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

use super::qwen3_moe::Qwen3MoeModel;
use super::qwen3_moe_forward_unified::{
    unified_stage_finish, unified_stage_start, UnifiedLayerProfile,
};

pub(crate) struct UnifiedLayerParams<'a, B: Backend> {
    pub ctx: &'a mut B::Context,
    pub li: usize,
    pub residual: &'a mut B::Buffer,
    pub m_total: usize,
    pub num_seqs: usize,
    pub max_q_len: usize,
    pub max_kv_len: usize,
    pub max_blocks_per_seq: usize,
    pub block_size: usize,
    pub nh: usize,
    pub nkv: usize,
    pub hd: usize,
    pub h: usize,
    pub inter: usize,
    pub eps: f32,
    pub qk_mode: i32,
    pub top_k: usize,
    pub n_exp: usize,
    pub norm_topk_prob: bool,
    pub use_vllm_tiled_q4: bool,
    pub tile_q4_count: usize,
    pub prof: Option<&'a mut UnifiedLayerProfile>,
}

impl<B, K> Qwen3MoeModel<B, K>
where
    B: MoeLlmBackend + BackendPagedKv,
    K: KvDtypeKind,
{
    /// One transformer layer for the unified forward. Mirrors Llama's
    /// `unified_forward_layer` for steps 1-6 (shared attention path) and
    /// dispatches MoE via `moe_forward_bucketed` for the FFN block.
    /// Uses LEGACY scratch fields (sized for `max_tokens`) — no per-call
    /// realloc, no buffer duplication.
    pub(crate) fn unified_forward_layer(&mut self, params: UnifiedLayerParams<'_, B>) {
        let UnifiedLayerParams {
            ctx,
            li,
            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,
            mut prof,
        } = params;
        let attn_layer = &self.attn_layers[li];
        let moe_layer = &self.moe_layers[li];
        let dummy_w = &attn_layer.input_ln_w;
        let q_norm_w = attn_layer.q_norm_w.as_ref().unwrap_or(dummy_w);
        let k_norm_w = attn_layer.k_norm_w.as_ref().unwrap_or(dummy_w);

        // 1. Input rms_norm [m_total, h] → scratch.norm_out
        let prof_enabled = prof.is_some();
        let t_input_norm = unified_stage_start::<B>(ctx, prof_enabled);
        B::rms_norm(
            ctx,
            residual,
            &attn_layer.input_ln_w,
            eps,
            &mut self.scratch.norm_out,
            m_total,
            h,
        );
        if let Some(prof) = prof.as_mut() {
            prof.input_norm_us += unified_stage_finish::<B>(ctx, t_input_norm);
        }

        // 2. qkv_proj GEMM (m = M_total): norm_out → qkv_out
        let t_qkv = unified_stage_start::<B>(ctx, prof_enabled);
        attn_layer.qkv_proj.forward(
            ctx,
            &self.scratch.norm_out,
            &mut self.scratch.qkv_out,
            m_total,
        );
        if let Some(prof) = prof.as_mut() {
            prof.qkv_us += unified_stage_finish::<B>(ctx, t_qkv);
        }

        // 3. varlen split_qkv_norm_rope_into_paged_cache
        let pools = self
            .paged_pools
            .as_mut()
            .expect("unified_forward_layer requires paged_pools");
        let pool_ptr = (
            &mut pools[li].0 as *mut B::Buffer,
            &mut pools[li].1 as *mut B::Buffer,
        );
        let use_fa_layout_varlen = self.use_vllm_paged_attn
            && (self.runtime_env.fa_layout_varlen
                || self.runtime_env.fa2_direct_ffi
                || self.runtime_env.fa2_source);
        let fa_pool_ptr = if use_fa_layout_varlen {
            let pools = self
                .paged_fa_pools
                .as_mut()
                .expect("FA-layout varlen or FA2 requires paged_fa_pools");
            Some((
                &mut pools[li].0 as *mut B::Buffer,
                &mut pools[li].1 as *mut B::Buffer,
            ))
        } else {
            None
        };
        // SAFETY: pools allocated once, no concurrent mutation.
        let (pool_k, pool_v) = unsafe { (&mut *pool_ptr.0, &mut *pool_ptr.1) };

        {
            let cu_seqlens_buf = self
                .scratch
                .unified_cu_seqlens_q
                .as_ref()
                .expect("unified_cu_seqlens_q missing");
            let pos_offsets_buf = self
                .scratch
                .unified_pos_offsets
                .as_ref()
                .expect("unified_pos_offsets missing");
            let bt_buf = self
                .scratch
                .unified_block_tables
                .as_ref()
                .expect("unified_block_tables missing");
            let varlen_dispatch = if self.use_vllm_paged_attn {
                B::split_qkv_norm_rope_into_paged_cache_varlen_vllm
            } else {
                B::split_qkv_norm_rope_into_paged_cache_varlen
            };
            let t_split_cache = unified_stage_start::<B>(ctx, prof_enabled);
            varlen_dispatch(
                ctx,
                &self.scratch.qkv_out,
                q_norm_w,
                k_norm_w,
                &self.rope.cos,
                &self.rope.sin,
                &mut self.scratch.q_head_major,
                pool_k,
                pool_v,
                cu_seqlens_buf,
                pos_offsets_buf,
                bt_buf,
                num_seqs,
                m_total,
                nh,
                nkv,
                hd,
                eps,
                qk_mode,
                block_size,
                max_blocks_per_seq,
            )
            .expect("Qwen3Moe unified: split_qkv_norm_rope_into_paged_cache_varlen");
            if let Some((fa_k_ptr, fa_v_ptr)) = fa_pool_ptr {
                // Maintain an FA-compatible K/V view in parallel with the
                // vLLM decode layout. The block table is shared, so release
                // and cache lifetime rules stay unchanged.
                let (fa_pool_k, fa_pool_v) = unsafe { (&mut *fa_k_ptr, &mut *fa_v_ptr) };
                B::split_qkv_norm_rope_into_paged_cache_varlen(
                    ctx,
                    &self.scratch.qkv_out,
                    q_norm_w,
                    k_norm_w,
                    &self.rope.cos,
                    &self.rope.sin,
                    &mut self.scratch.q_head_major,
                    fa_pool_k,
                    fa_pool_v,
                    cu_seqlens_buf,
                    pos_offsets_buf,
                    bt_buf,
                    num_seqs,
                    m_total,
                    nh,
                    nkv,
                    hd,
                    eps,
                    qk_mode,
                    block_size,
                    max_blocks_per_seq,
                )
                .expect("Qwen3Moe unified: split_qkv_norm_rope_into_paged_cache_varlen fa-layout");
            }
            if let Some(prof) = prof.as_mut() {
                prof.split_cache_us += unified_stage_finish::<B>(ctx, t_split_cache);
            }
        }

        // 4. paged_varlen_attention
        {
            let cu_seqlens_buf = self
                .scratch
                .unified_cu_seqlens_q
                .as_ref()
                .expect("unified_cu_seqlens_q missing");
            let pos_offsets_buf = self
                .scratch
                .unified_pos_offsets
                .as_ref()
                .expect("unified_pos_offsets missing");
            let seq_lens_buf = self
                .scratch
                .unified_seq_lens
                .as_ref()
                .expect("unified_seq_lens missing");
            let bt_buf = self
                .scratch
                .unified_block_tables
                .as_ref()
                .expect("unified_block_tables missing");
            let t_attn = unified_stage_start::<B>(ctx, prof_enabled);
            if self.use_vllm_paged_attn {
                if let Some((fa_k_ptr, fa_v_ptr)) = fa_pool_ptr {
                    let (fa_pool_k, fa_pool_v) = unsafe { (&mut *fa_k_ptr, &mut *fa_v_ptr) };
                    if self.runtime_env.fa2_direct_ffi || self.runtime_env.fa2_source {
                        let lse_buf = self
                            .scratch
                            .unified_attn_lse
                            .as_mut()
                            .expect("unified_attn_lse missing");
                        B::paged_varlen_attention_fa2_ffi(
                            ctx,
                            &self.scratch.q_head_major,
                            fa_pool_k,
                            fa_pool_v,
                            &mut self.scratch.attn_head_major_out,
                            lse_buf,
                            cu_seqlens_buf,
                            seq_lens_buf,
                            bt_buf,
                            num_seqs,
                            m_total,
                            max_q_len,
                            max_kv_len,
                            nh,
                            nkv,
                            hd,
                            block_size,
                            max_blocks_per_seq,
                        )
                        .expect("Qwen3Moe unified: paged_varlen_attention_fa2_ffi");
                    } else {
                        B::paged_varlen_attention(
                            ctx,
                            &self.scratch.q_head_major,
                            fa_pool_k,
                            fa_pool_v,
                            &mut self.scratch.attn_head_major_out,
                            cu_seqlens_buf,
                            pos_offsets_buf,
                            bt_buf,
                            num_seqs,
                            m_total,
                            max_kv_len,
                            nh,
                            nkv,
                            hd,
                            block_size,
                            max_blocks_per_seq,
                        )
                        .expect("Qwen3Moe unified: paged_varlen_attention fa-layout");
                    }
                } else if use_vllm_tiled_q4 {
                    let tile_seqs = self
                        .scratch
                        .unified_tile_q4_seqs
                        .as_ref()
                        .expect("unified_tile_q4_seqs missing");
                    let tile_starts = self
                        .scratch
                        .unified_tile_q4_starts
                        .as_ref()
                        .expect("unified_tile_q4_starts missing");
                    B::paged_varlen_attention_vllm_tiled_q4(
                        ctx,
                        &self.scratch.q_head_major,
                        pool_k,
                        pool_v,
                        &mut self.scratch.attn_head_major_out,
                        cu_seqlens_buf,
                        pos_offsets_buf,
                        bt_buf,
                        tile_seqs,
                        tile_starts,
                        tile_q4_count,
                        max_kv_len,
                        nh,
                        nkv,
                        hd,
                        block_size,
                        max_blocks_per_seq,
                    )
                    .expect("Qwen3Moe unified: paged_varlen_attention_vllm_tiled_q4");
                } else {
                    B::paged_varlen_attention_vllm(
                        ctx,
                        &self.scratch.q_head_major,
                        pool_k,
                        pool_v,
                        &mut self.scratch.attn_head_major_out,
                        cu_seqlens_buf,
                        pos_offsets_buf,
                        bt_buf,
                        num_seqs,
                        m_total,
                        max_kv_len,
                        nh,
                        nkv,
                        hd,
                        block_size,
                        max_blocks_per_seq,
                    )
                    .expect("Qwen3Moe unified: paged_varlen_attention_vllm");
                }
            } else {
                B::paged_varlen_attention(
                    ctx,
                    &self.scratch.q_head_major,
                    pool_k,
                    pool_v,
                    &mut self.scratch.attn_head_major_out,
                    cu_seqlens_buf,
                    pos_offsets_buf,
                    bt_buf,
                    num_seqs,
                    m_total,
                    max_kv_len,
                    nh,
                    nkv,
                    hd,
                    block_size,
                    max_blocks_per_seq,
                )
                .expect("Qwen3Moe unified: paged_varlen_attention");
            }
            if let Some(prof) = prof.as_mut() {
                prof.attn_us += unified_stage_finish::<B>(ctx, t_attn);
            }
        }

        // 5. o_proj (m = M_total): attn_head_major_out → o_proj_out
        let t_o_proj = unified_stage_start::<B>(ctx, prof_enabled);
        attn_layer.o_proj.forward(
            ctx,
            &self.scratch.attn_head_major_out,
            &mut self.scratch.o_proj_out,
            m_total,
        );
        if let Some(prof) = prof.as_mut() {
            prof.o_proj_us += unified_stage_finish::<B>(ctx, t_o_proj);
        }

        // 6. fused_add_rms_norm: residual += o_proj_out; norm → scratch.norm_out
        //    (reusing scratch.norm_out as the MoE input — the legacy MoE
        //    forward also reads scratch.norm_out, so this naturally
        //    feeds the next step.)
        let t_post_norm = unified_stage_start::<B>(ctx, prof_enabled);
        B::fused_add_rms_norm(
            ctx,
            residual,
            &self.scratch.o_proj_out,
            &attn_layer.post_ln_w,
            eps,
            &mut self.scratch.norm_out,
            m_total,
            h,
        );
        if let Some(prof) = prof.as_mut() {
            prof.post_norm_us += unified_stage_finish::<B>(ctx, t_post_norm);
        }

        // 7. Router GEMM: norm_out → router_logits
        let t_router = unified_stage_start::<B>(ctx, prof_enabled);
        moe_layer.router.forward(
            ctx,
            &self.scratch.norm_out,
            &mut self.scratch.router_logits,
            m_total,
        );
        if let Some(prof) = prof.as_mut() {
            prof.router_us += unified_stage_finish::<B>(ctx, t_router);
        }

        // 8. MoE forward (bucketed CUDA path).
        //    Reads scratch.norm_out + scratch.router_logits. Writes scratch.moe_out.
        //    M = m_total; MoE routes each token independently (no per-request boundary).
        let t_moe = unified_stage_start::<B>(ctx, prof_enabled);
        crate::moe::moe_forward_bucketed::<B>(crate::moe::dispatch::MoeForwardBucketedParams {
            ctx,
            x: &self.scratch.norm_out,
            router_logits: &self.scratch.router_logits,
            out: &mut self.scratch.moe_out,
            batch: m_total,
            hidden_size: h,
            expert_intermediate: inter,
            num_experts: n_exp,
            top_k,
            norm_topk_prob,
            experts: &moe_layer.experts,
            x_packed: &mut self.scratch.x_packed_bucket,
            gate_up_packed: &mut self.scratch.gate_up_packed_bucket,
            silu_packed: &mut self.scratch.silu_stacked,
            down_packed: &mut self.scratch.down_out_stacked,
            route_scratch: &mut self.scratch.moe_route_scratch,
            device_route: Some(crate::moe::dispatch::DeviceRouteScratch {
                selected_ids: &mut self.scratch.selected_ids_buf,
                pair_weights: &mut self.scratch.route_pair_weights_dev,
                pairs_by_token: &mut self.scratch.route_pairs_dev,
                packed_token_idx: &mut self.scratch.route_packed_idx_dev,
                expert_offsets: &mut self.scratch.route_expert_offsets_dev,
                sorted_tokens: &mut self.scratch.route_sorted_tokens_dev,
                block_ids: &mut self.scratch.route_block_ids_dev,
                total_post_pad: &mut self.scratch.route_total_post_pad_dev,
            }),
        })
        .expect("Qwen3Moe unified: moe_forward_bucketed");
        if let Some(prof) = prof.as_mut() {
            prof.moe_us += unified_stage_finish::<B>(ctx, t_moe);
        }

        // 9. residual += moe_out
        let t_residual_add = unified_stage_start::<B>(ctx, prof_enabled);
        B::add_inplace(ctx, residual, &self.scratch.moe_out, m_total * h);
        if let Some(prof) = prof.as_mut() {
            prof.residual_add_us += unified_stage_finish::<B>(ctx, t_residual_add);
        }
    }
}