Skip to main content

cortiq_engine/
gpu.rs

1//! Facade for GPU backends: a single call entry point for qtensor/pipeline/
2//! linear_core. Job types and the threshold are canonical HERE; behind the
3//! facade dispatch goes to a platform backend:
4//!   - `gpu_metal` (Apple Silicon, unified memory + no-copy buffers);
5//!   - `gpu_wgpu` (C1: Vulkan/DX12/Metal — NVIDIA/Radeon/Intel/Apple,
6//!     weights resident in VRAM), available under `--features gpu`.
7//!
8//! Runtime selection via `CMF_GPU`: `1` — native Metal (macOS) or wgpu
9//! (other OSes); `wgpu` — force wgpu (including for the local
10//! Metal-via-wgpu parity test). Any backend refusal — `false` and the honest
11//! CPU path, no partial results.
12
13use cortiq_core::CmfModel;
14use std::cell::Cell;
15use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU32, AtomicU64, Ordering};
16use std::sync::{Arc, OnceLock};
17
18thread_local! {
19    /// Index of the current forward layer (−1 = outside a numbered layer:
20    /// lm_head/embed — always allowed). The pipeline sets it before
21    /// each layer so that the GPU/CPU layer-split works.
22    static CUR_LAYER: Cell<i64> = const { Cell::new(-1) };
23    /// Inside `cpu_scope` every GPU gate reports disabled: the timed CPU
24    /// arm of a probe (and a class that lost its probe) must run PURE
25    /// CPU, or inner per-op hooks would re-enter the GPU and poison the
26    /// comparison.
27    static CPU_ONLY: Cell<bool> = const { Cell::new(false) };
28    /// "This op paid a one-off cost" (weight upload / first pipeline
29    /// build): backends set it, `probe_record` discards the sample so
30    /// only steady-state timings compete.
31    static PROBE_COLD: Cell<bool> = const { Cell::new(false) };
32}
33
34/// Run `f` with the GPU gates off on this thread (pure-CPU arm).
35pub fn cpu_scope<R>(f: impl FnOnce() -> R) -> R {
36    struct Restore(bool);
37    impl Drop for Restore {
38        fn drop(&mut self) {
39            CPU_ONLY.with(|c| c.set(self.0));
40        }
41    }
42    let previous = CPU_ONLY.with(|c| c.replace(true));
43    let _restore = Restore(previous);
44    f()
45}
46
47/// Backends: note a one-off cost (weight upload, buffer-cache fill) so
48/// the probe discards this sample.
49pub(crate) fn probe_note_cold() {
50    PROBE_COLD.with(|c| c.set(true));
51}
52
53/// Peek the cold flag without consuming it (`probe_record` consumes).
54/// Contention heuristics use this: a slow COLD op is a one-off build
55/// cost, not evidence the device is busy.
56pub(crate) fn probe_was_cold() -> bool {
57    PROBE_COLD.with(|c| c.get())
58}
59
60/// Pipeline: mark the current layer (or −1 outside layers) for layer-split.
61pub fn set_layer(l: i64) {
62    CUR_LAYER.with(|c| c.set(l));
63}
64
65/// The layer `set_layer` last marked on this thread (−1 outside layers).
66pub fn cur_layer() -> i64 {
67    CUR_LAYER.with(|c| c.get())
68}
69
70/// Parse `CMF_GPU_LAYERS` («0-19», «0,2,4», «0-9,30-39») once.
71/// None = no restriction (all layers on GPU). Garbage → also no restriction.
72fn layer_ranges() -> &'static Option<Vec<(i64, i64)>> {
73    static R: OnceLock<Option<Vec<(i64, i64)>>> = OnceLock::new();
74    R.get_or_init(|| {
75        let s = std::env::var("CMF_GPU_LAYERS").ok()?;
76        let mut v = Vec::new();
77        for part in s.split(',') {
78            let part = part.trim();
79            match part.split_once('-') {
80                Some((a, b)) => v.push((a.trim().parse().ok()?, b.trim().parse().ok()?)),
81                None => {
82                    let x: i64 = part.parse().ok()?;
83                    v.push((x, x));
84                }
85            }
86        }
87        Some(v)
88    })
89}
90
91fn layer_allowed() -> bool {
92    match layer_ranges() {
93        None => true,
94        Some(ranges) => {
95            let cur = CUR_LAYER.with(|c| c.get());
96            cur < 0 || ranges.iter().any(|(a, b)| cur >= *a && cur <= *b)
97        }
98    }
99}
100
101/// GPU allowed FOR THE CURRENT LAYER: backend is initialized AND the layer
102/// falls within `CMF_GPU_LAYERS` (GPU/CPU layer-split) AND we are not
103/// inside a `cpu_scope`. Op gates call this.
104pub fn enabled_here() -> bool {
105    !CPU_ONLY.with(|c| c.get()) && enabled() && layer_allowed()
106}
107
108// ── Runtime GPU-vs-CPU probe ────────────────────────────────────────────
109// CMF_GPU=1 does not TRUST that the device wins — it MEASURES. For each
110// op class the first calls alternate arms: GPU timed vs pure-CPU timed
111// (under cpu_scope). Cold GPU calls (weight upload / cache fill) are
112// discarded; after PROBE_SAMPLES clean samples per arm the faster arm is
113// chosen for the rest of the process. Rationale: submit+poll latency
114// differs by an order of magnitude across driver stacks (Metal/PCIe
115// ~3-4 ms, Vulkan/4090 ~0.3 ms) — a static threshold cannot know whether
116// per-op offload pays off HERE. CMF_GPU_PROBE=0 → always trust the GPU.
117
118/// GPU-eligible op classes, each with an independent probe.
119#[derive(Clone, Copy)]
120pub enum OpClass {
121    /// Whole FFN chain in one submission (dense / MoE block).
122    Ffn = 0,
123    /// Large hybrid CPU∥GPU matvec (lm_head class).
124    Matvec = 1,
125    /// Prefill GEMM (matmat).
126    Matmat = 2,
127    /// Batched matvecs of one input (QKV).
128    Batch = 3,
129    /// Prefill GEMM at image-diffusion widths (b ≥ 128). Probed apart
130    /// from `Matmat`: one imagegen process runs BOTH populations
131    /// (prompt encode b≈40 where the GPU wins big, DiT b≥256 where
132    /// the CPU AMX arm is competitive) — a single shared verdict locks
133    /// the wrong arm for whichever population samples second.
134    MatmatWide = 4,
135}
136
137/// Probe verdict for one call.
138pub enum ProbeArm {
139    /// Run the GPU path (during probing: timed, recorded).
140    Gpu,
141    /// Probing: run the CPU path under `cpu_scope`, timed, recorded.
142    CpuTimed,
143    /// Decided: CPU won — run the CPU path (under `cpu_scope`).
144    Cpu,
145}
146
147/// Clean samples per arm before a class decides.
148const PROBE_SAMPLES: u32 = 6;
149
150struct Probe {
151    /// 0 = probing, 1 = GPU won, 2 = CPU won.
152    state: AtomicU8,
153    flip: AtomicU32,
154    gpu_ns: AtomicU64,
155    gpu_n: AtomicU32,
156    cpu_ns: AtomicU64,
157    cpu_n: AtomicU32,
158    /// Best (minimum) sample per arm. The DECISION compares these:
159    /// means are poisoned by one-off cold costs the cold-flag cannot
160    /// see — e.g. the CPU arm's first mmap-cold expert matvec page
161    /// faults its weights in and reads 3× its steady state, which
162    /// locked the GPU arm on a 35B MoE at a 4× real-world loss. The
163    /// minimum is each arm's honest steady-state pace.
164    gpu_min: AtomicU64,
165    cpu_min: AtomicU64,
166}
167
168impl Probe {
169    const fn new() -> Self {
170        Self {
171            state: AtomicU8::new(0),
172            flip: AtomicU32::new(0),
173            gpu_ns: AtomicU64::new(0),
174            gpu_n: AtomicU32::new(0),
175            cpu_ns: AtomicU64::new(0),
176            cpu_n: AtomicU32::new(0),
177            gpu_min: AtomicU64::new(u64::MAX),
178            cpu_min: AtomicU64::new(u64::MAX),
179        }
180    }
181}
182
183static PROBES: [Probe; 5] = [
184    Probe::new(),
185    Probe::new(),
186    Probe::new(),
187    Probe::new(),
188    Probe::new(),
189];
190
191fn probe_on() -> bool {
192    static ON: OnceLock<bool> = OnceLock::new();
193    *ON.get_or_init(|| {
194        std::env::var("CMF_GPU_PROBE")
195            .map(|v| v != "0" && v != "off")
196            .unwrap_or(true)
197    })
198}
199
200/// q1 ops on the native Metal backend skip the probe entirely: the CPU
201/// q1 kernel is load-port-bound, the GPU one wins warm — and probe
202/// alternation itself cools the device between samples (measured: block
203/// times 5.8 ms warm vs 8.8 ms mixed). Other backends keep probing.
204pub fn q1_force() -> bool {
205    #[cfg(target_os = "macos")]
206    {
207        backend() == Backend::Metal
208    }
209    #[cfg(not(target_os = "macos"))]
210    {
211        false
212    }
213}
214
215/// Should a FUSED whole-block path trust the device instead of asking
216/// the per-op probe? True on native Metal and on discrete wgpu adapters.
217///
218/// The probe answers "is one wide matmat faster on the GPU", and for the
219/// DiT on Metal that is a coin flip — measured 2.62 ms GPU vs 2.56 ms
220/// CPU, a 2% spread that lands on either arm run to run. But the fused
221/// block's advantage is not per-op speed, it is that the hidden state,
222/// the packs and the attention panels never leave the device: end to end
223/// the whole-block path renders a 512² Lumina step in ~5.4 s against
224/// ~8.4 s when the probe happens to pick the CPU. Gating a fusion win on
225/// a per-op tie made every second render half-speed at random.
226///
227/// On a discrete card the verdict is never in doubt — an RTX 3090 against
228/// a 256-core EPYC measured 11.5 ms vs 31 ms per wide op, four runs out
229/// of four — so the probe's sampling phase is pure cost: it alone was 10%
230/// of a 512² render (74.3 s against 66.9 s with the probe off). Integrated
231/// and mobile adapters keep probing; there the submit latency is real and
232/// can genuinely lose.
233pub fn fused_block_trusted() -> bool {
234    #[cfg(target_os = "macos")]
235    if backend() == Backend::Metal {
236        return true;
237    }
238    wgpu_graph_default()
239}
240
241/// Which arm should this GPU-eligible call take? Consult AFTER the
242/// eligibility gates (`enabled_here` / `min_rows`) so only real
243/// candidates alternate.
244pub fn probe_arm(c: OpClass) -> ProbeArm {
245    // Every arbitrated call starts with a clean cold flag: both the
246    // sample discard in `probe_record` and the contention kill-switch
247    // read it AFTER the op, so a stale note from a previous call on
248    // this thread must not leak in.
249    PROBE_COLD.with(|f| f.set(false));
250    if !probe_on() {
251        return ProbeArm::Gpu;
252    }
253    let p = &PROBES[c as usize];
254    match p.state.load(Ordering::Relaxed) {
255        1 => ProbeArm::Gpu,
256        2 => ProbeArm::Cpu,
257        _ => {
258            if p.flip.fetch_add(1, Ordering::Relaxed) % 2 == 0 {
259                ProbeArm::Gpu
260            } else {
261                ProbeArm::CpuTimed
262            }
263        }
264    }
265}
266
267/// Record a timed arm sample; on the `PROBE_SAMPLES`-th clean sample of
268/// BOTH arms the class decides for the rest of the process.
269pub fn probe_record(c: OpClass, gpu: bool, dur: std::time::Duration) {
270    let p = &PROBES[c as usize];
271    if p.state.load(Ordering::Relaxed) != 0 {
272        return;
273    }
274    if gpu && PROBE_COLD.with(|f| f.replace(false)) {
275        return; // one-off cost in this call — not a steady-state sample
276    }
277    let ns = dur.as_nanos().min(u64::MAX as u128) as u64;
278    if gpu {
279        p.gpu_ns.fetch_add(ns, Ordering::Relaxed);
280        p.gpu_n.fetch_add(1, Ordering::Relaxed);
281        p.gpu_min.fetch_min(ns, Ordering::Relaxed);
282    } else {
283        p.cpu_ns.fetch_add(ns, Ordering::Relaxed);
284        p.cpu_n.fetch_add(1, Ordering::Relaxed);
285        p.cpu_min.fetch_min(ns, Ordering::Relaxed);
286    }
287    let (gn, cn) = (
288        p.gpu_n.load(Ordering::Relaxed),
289        p.cpu_n.load(Ordering::Relaxed),
290    );
291    if gn >= 2 && cn >= 2 {
292        // Decide on each arm's BEST sample — the steady-state pace.
293        // Means carry one-off cold costs (mmap page-in on the CPU arm)
294        // that the cold-flag machinery cannot see.
295        let g = p.gpu_min.load(Ordering::Relaxed) as f64;
296        let cp = p.cpu_min.load(Ordering::Relaxed) as f64;
297        // Early verdict on a ≥3× gap — no reason to keep feeding the
298        // losing arm; close races take the full sample count.
299        if (gn < PROBE_SAMPLES || cn < PROBE_SAMPLES) && g < cp * 3.0 && cp < g * 3.0 {
300            return;
301        }
302        let winner = if g <= cp { 1 } else { 2 };
303        if p.state
304            .compare_exchange(0, winner, Ordering::Relaxed, Ordering::Relaxed)
305            .is_ok()
306        {
307            tracing::info!(
308                "gpu probe [{}]: gpu {:.2} ms vs cpu {:.2} ms per op → {}",
309                ["ffn", "matvec", "matmat", "qkv-batch", "matmat-wide"][c as usize],
310                g / 1e6,
311                cp / 1e6,
312                if winner == 1 { "gpu" } else { "cpu" },
313            );
314        }
315    }
316}
317
318/// Is the class still collecting samples? (Call sites use this to route
319/// cold-weight calls away from the GPU arm during probing.)
320pub fn probe_deciding(c: OpClass) -> bool {
321    probe_on() && PROBES[c as usize].state.load(Ordering::Relaxed) == 0
322}
323
324/// Probing helper: true — tensor `idx`'s quant weights are ALREADY
325/// device-resident (a clean GPU sample is possible now); false — they
326/// were not (the upload starts within the VRAM budget, so a later call
327/// finds them warm) or the tensor cannot go to the GPU at all. Keeps the
328/// probe from billing a full cold dispatch+readback to a sample it will
329/// discard anyway. The verdict needs only a couple of warm tensors, so
330/// probe-driven uploads are capped — the losing-GPU machine should not
331/// pay for uploading the whole layer stack it will never use; if the GPU
332/// wins, the rest uploads lazily on demand, in the same first-touch order.
333#[allow(unused_variables)]
334pub fn q8_resident_or_upload(model: &Arc<CmfModel>, idx: usize) -> bool {
335    static PROBE_UPLOADS: AtomicU32 = AtomicU32::new(0);
336    let may_upload = PROBE_UPLOADS.load(Ordering::Relaxed) < 4;
337    let resident = match backend() {
338        #[cfg(target_os = "macos")]
339        Backend::Metal => crate::gpu_metal::q8_resident_or_upload(model, idx, may_upload),
340        #[cfg(feature = "gpu")]
341        Backend::Wgpu => crate::gpu_wgpu::q8_resident_or_upload(model, idx, may_upload),
342        Backend::None => false,
343    };
344    if !resident && may_upload {
345        PROBE_UPLOADS.fetch_add(1, Ordering::Relaxed);
346    }
347    resident
348}
349
350/// Test hook: reset all probes to the undecided state.
351#[cfg(test)]
352pub(crate) fn probe_reset() {
353    for p in &PROBES {
354        p.state.store(0, Ordering::Relaxed);
355        p.flip.store(0, Ordering::Relaxed);
356        p.gpu_ns.store(0, Ordering::Relaxed);
357        p.gpu_n.store(0, Ordering::Relaxed);
358        p.cpu_ns.store(0, Ordering::Relaxed);
359        p.cpu_n.store(0, Ordering::Relaxed);
360    }
361}
362
363#[cfg(test)]
364mod probe_tests {
365    use super::*;
366    use std::time::Duration;
367
368    // One test fn: PROBES is process-global and probe_reset touches all
369    // classes — parallel test threads would race.
370    #[test]
371    fn probe_alternates_discards_cold_and_decides() {
372        probe_reset();
373        // Probing: arms alternate.
374        assert!(matches!(probe_arm(OpClass::Ffn), ProbeArm::Gpu));
375        assert!(matches!(probe_arm(OpClass::Ffn), ProbeArm::CpuTimed));
376
377        // A cold GPU sample (upload noted) must be discarded: feed a
378        // catastrophic cold sample, then clean fast-GPU samples — GPU
379        // wins only if the cold one did not count.
380        probe_note_cold();
381        probe_record(OpClass::Ffn, true, Duration::from_secs(1000));
382        for _ in 0..PROBE_SAMPLES {
383            probe_record(OpClass::Ffn, true, Duration::from_millis(1));
384            probe_record(OpClass::Ffn, false, Duration::from_millis(4));
385        }
386        assert!(matches!(probe_arm(OpClass::Ffn), ProbeArm::Gpu));
387
388        // The reverse: a class where the CPU arm is faster decides CPU.
389        for _ in 0..PROBE_SAMPLES {
390            probe_record(OpClass::Matmat, true, Duration::from_millis(4));
391            probe_record(OpClass::Matmat, false, Duration::from_millis(1));
392        }
393        assert!(matches!(probe_arm(OpClass::Matmat), ProbeArm::Cpu));
394
395        // cpu_scope: gates off inside, restored after.
396        cpu_scope(|| CPU_ONLY.with(|c| assert!(c.get())));
397        CPU_ONLY.with(|c| assert!(!c.get()));
398        cpu_scope(|| {
399            cpu_scope(|| CPU_ONLY.with(|c| assert!(c.get())));
400            CPU_ONLY.with(|c| assert!(c.get()));
401        });
402        let _ = std::panic::catch_unwind(|| cpu_scope(|| panic!("scope test")));
403        CPU_ONLY.with(|c| assert!(!c.get()));
404        probe_reset();
405    }
406}
407
408/// Default row threshold: the GPU takes only larger matrices (lm_head
409/// class). Below it, the dispatch/readback cost does not pay off on unified memory.
410pub const GPU_MIN_ROWS: usize = 65_536;
411
412/// Effective threshold: `CMF_GPU_MIN_ROWS` overrides. Defaults differ
413/// by device class: on a DISCRETE card VRAM bandwidth pays off even for
414/// FFN/QKV-class matrices (4096), on unified memory only lm_head-class
415/// is worth the dispatch/readback (65536). Field case behind this: a
416/// 35B model on an RTX 4090 saw ~0 offload because every layer matrix
417/// sat below the old universal 65536.
418pub fn min_rows() -> usize {
419    if let Some(v) = std::env::var("CMF_GPU_MIN_ROWS")
420        .ok()
421        .and_then(|v| v.parse().ok())
422    {
423        return v;
424    }
425    if discrete() { 4096 } else { GPU_MIN_ROWS }
426}
427
428/// Is the active backend a discrete card (PCIe VRAM)?
429pub fn discrete() -> bool {
430    match backend() {
431        #[cfg(feature = "gpu")]
432        Backend::Wgpu => crate::gpu_wgpu::is_discrete(),
433        #[cfg(target_os = "macos")]
434        Backend::Metal => false, // UMA by the init() guard
435        Backend::None => false,
436    }
437}
438
439/// A single MoE-FFN job (an expert with its own weight), executed in one
440/// submission: (rows, cols, idx, row_scale) for gate/up/down + prescaled
441/// inputs + the down θ-field + the blending weight.
442pub struct MoeJob<'a> {
443    pub gate: (usize, usize, usize, &'a [f32]),
444    pub up: (usize, usize, usize, &'a [f32]),
445    pub down: (usize, usize, usize, &'a [f32]),
446    pub xs_gate: Vec<f32>,
447    pub xs_up: Vec<f32>,
448    pub down_col: &'a [f32],
449    pub w: f32,
450    /// q1 trio: scales live inside the 6-byte tiles (row_scale slices
451    /// empty, xs raw f32). Backends without a q1 kernel refuse the job.
452    pub q1: bool,
453    /// q4_tiled trio: scales inside the 18-byte tiles (row_scale
454    /// slices empty, xs raw f32) — the MoE-hybrid coder class.
455    pub q4t: bool,
456    /// q4tp trio: same raw-xs contract, 16-byte nibble stride and the scale
457    /// on a per-row ladder. Without this the experts of a q4tp MoE model fall
458    /// to the CPU while every other dtype rides the device.
459    pub q4tp: bool,
460}
461
462/// A single independent batch matvec (GDN projections of one input).
463pub struct BatchJob<'a> {
464    pub idx: usize,
465    pub rows: usize,
466    pub cols: usize,
467    pub row_scale: &'a [f32],
468    pub xs: Vec<f32>,
469    /// Weight layout. Was a bare `q1: bool`, which could only ever spell two
470    /// of the four and silently sent everything else back to the CPU — the
471    /// GDN projections of a q4t/q4tp model never reached the device at all.
472    pub layout: BatchLayout,
473}
474
475/// Which kernel a batched matvec needs. q8 carries row scales in a side
476/// buffer; the rest embed them in the payload and differ in stride.
477#[derive(Clone, Copy, PartialEq, Eq, Debug)]
478pub enum BatchLayout {
479    Q8,
480    Q1,
481    Q4t,
482    Q4tp,
483}
484
485#[derive(Clone, Copy, PartialEq, Eq)]
486enum Backend {
487    None,
488    #[cfg(target_os = "macos")]
489    Metal,
490    #[cfg(feature = "gpu")]
491    Wgpu,
492}
493
494fn backend() -> Backend {
495    #[cfg(feature = "gpu")]
496    if crate::gpu_wgpu::selected() {
497        return if crate::gpu_wgpu::enabled() {
498            Backend::Wgpu
499        } else {
500            Backend::None
501        };
502    }
503    #[cfg(target_os = "macos")]
504    if crate::gpu_metal::enabled() {
505        return Backend::Metal;
506    }
507    Backend::None
508}
509
510/// GPU enabled and initialized on the selected backend?
511/// Whether THIS build can bring a GPU up on THIS device: a compiled-in
512/// backend plus a live adapter. The mobile FFI exposes it so an app can
513/// tell "GPU off" from "GPU impossible" (a CPU-only .so ships no
514/// backend at all). Cached after the first call.
515pub fn backend_available() -> bool {
516    #[cfg(target_os = "macos")]
517    {
518        // The Metal path is always compiled on macOS.
519        true
520    }
521    #[cfg(all(feature = "gpu", not(target_os = "macos")))]
522    {
523        static AVAIL: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
524        *AVAIL.get_or_init(crate::gpu_wgpu::adapter_probe)
525    }
526    #[cfg(all(not(feature = "gpu"), not(target_os = "macos")))]
527    {
528        false
529    }
530}
531
532pub fn enabled() -> bool {
533    backend() != Backend::None
534}
535
536/// Default-on condition for the wgpu whole-token graph: the wgpu
537/// backend on a DISCRETE adapter. NOT plain `enabled()` (macOS/Metal
538/// must not pay a per-token layer scan for a graph its backend
539/// refuses), and NOT integrated adapters: the graph's ~300 barriered
540/// dispatches per token are cheap on desktop immediate-mode GPUs but
541/// tiled mobile GPUs (Adreno/Mali) drain the pipeline at every barrier
542/// — field report: 0.2 tok/s on-graph vs 15 tok/s on the CPU. On
543/// integrated adapters the per-op probe path arbitrates each op class
544/// against the CPU instead; CMF_GPU_WGPU_GRAPH=1 still forces the
545/// graph anywhere.
546/// Is the wgpu backend active at all (any adapter)? Eligibility gate
547/// for the whole-token graph — whether it actually RUNS is decided by
548/// `wgpu_graph_default` (trusted on discrete) or the generation race.
549pub fn wgpu_active() -> bool {
550    #[cfg(feature = "gpu")]
551    {
552        matches!(backend(), Backend::Wgpu)
553    }
554    #[cfg(not(feature = "gpu"))]
555    {
556        false
557    }
558}
559
560pub fn wgpu_graph_default() -> bool {
561    #[cfg(feature = "gpu")]
562    {
563        matches!(backend(), Backend::Wgpu) && crate::gpu_wgpu::discrete_active()
564    }
565    #[cfg(not(feature = "gpu"))]
566    {
567        false
568    }
569}
570
571/// q8_row/q8_2f matvec, rows [row0, row0+rows). `xs` — prescaled by the θ-field.
572#[allow(clippy::too_many_arguments, unused_variables)]
573pub fn q8_matvec_range(
574    model: &Arc<CmfModel>,
575    idx: usize,
576    row0: usize,
577    row_scale: &[f32],
578    xs: &[f32],
579    rows: usize,
580    cols: usize,
581    out: &mut [f32],
582) -> bool {
583    match backend() {
584        #[cfg(target_os = "macos")]
585        Backend::Metal => {
586            crate::gpu_metal::q8_matvec_range(model, idx, row0, row_scale, xs, rows, cols, out)
587        }
588        #[cfg(feature = "gpu")]
589        Backend::Wgpu => {
590            crate::gpu_wgpu::q8_matvec_range(model, idx, row0, row_scale, xs, rows, cols, out)
591        }
592        Backend::None => false,
593    }
594}
595
596/// GEMM of a prefill batch: `pre` — prescaled inputs row-major [b, cols],
597/// out — row-major [b, rows].
598#[allow(clippy::too_many_arguments, unused_variables)]
599pub fn q8_matmat(
600    model: &Arc<CmfModel>,
601    idx: usize,
602    row_scale: &[f32],
603    pre: &[f32],
604    b: usize,
605    rows: usize,
606    cols: usize,
607    out: &mut [f32],
608) -> bool {
609    match backend() {
610        #[cfg(target_os = "macos")]
611        Backend::Metal => {
612            crate::gpu_metal::q8_matmat(model, idx, row_scale, pre, b, rows, cols, out)
613        }
614        #[cfg(feature = "gpu")]
615        Backend::Wgpu => crate::gpu_wgpu::q8_matmat(model, idx, row_scale, pre, b, rows, cols, out),
616        Backend::None => false,
617    }
618}
619
620/// q1 matvec: raw f32 activations, tile-embedded scales. Metal only
621/// for now (wgpu q1 WGSL is queued); false = CPU fallback.
622#[allow(unused_variables)]
623pub fn q1_matvec(
624    model: &Arc<CmfModel>,
625    idx: usize,
626    xs: &[f32],
627    rows: usize,
628    cols: usize,
629    out: &mut [f32],
630) -> bool {
631    match backend() {
632        #[cfg(target_os = "macos")]
633        Backend::Metal => crate::gpu_metal::q1_matvec(model, idx, xs, rows, cols, out),
634        #[cfg(feature = "gpu")]
635        Backend::Wgpu => crate::gpu_wgpu::q1_matvec(model, idx, xs, rows, cols, out),
636        Backend::None => false,
637    }
638}
639
640/// Whole attention sub-block on the wgpu token graph (drop-in for
641/// `qwen_attention`): normed hidden in, O-projection out, resident device
642/// K/V mirror. false = refusal / not the wgpu backend → CPU path.
643#[allow(clippy::too_many_arguments)]
644pub fn attn_dropin(
645    model: &Arc<CmfModel>,
646    kv_id: u64,
647    layer: usize,
648    normed: &[f32],
649    wq_idx: usize,
650    wk_idx: usize,
651    wv_idx: usize,
652    wo_idx: usize,
653    q_norm: Option<&[f32]>,
654    k_norm: Option<&[f32]>,
655    invf: &[f32],
656    nh: usize,
657    nkv: usize,
658    hd: usize,
659    rd: usize,
660    hidden: usize,
661    pos: usize,
662    cap: usize,
663    gemma: bool,
664    eps: f32,
665    cpu_k: &[Vec<f32>],
666    cpu_v: &[Vec<f32>],
667    out: &mut [f32],
668) -> bool {
669    match backend() {
670        #[cfg(feature = "gpu")]
671        Backend::Wgpu => crate::gpu_wgpu::attn_dropin_gpu(
672            model, kv_id, layer, normed, wq_idx, wk_idx, wv_idx, wo_idx, q_norm, k_norm, invf, nh,
673            nkv, hd, rd, hidden, pos, cap, gemma, eps, cpu_k, cpu_v, out,
674        ),
675        #[allow(unused_variables)]
676        _ => false,
677    }
678}
679
680/// One weight in the whole-token graph: tensor idx + a codec tag (0=q8_row,
681/// 1=q1, 2=q4_tiled, 3=q1t, 4=f32) + per-row scales (q8_row only) + the raw f32
682/// data (kind 4 only — small unquantized projections like GDN in_proj_a/b).
683pub struct GraphW<'a> {
684    pub idx: usize,
685    pub kind: u8,
686    pub row_scale: &'a [f32],
687    pub data: &'a [f32],
688}
689
690/// A layer's token-mixing op: standard attention or a GDN (linear-attention)
691/// block. The surrounding norms + SwiGLU FFN are common to both.
692pub enum GraphAttn<'a> {
693    Full {
694        wq: GraphW<'a>,
695        wk: GraphW<'a>,
696        wv: GraphW<'a>,
697        wo: GraphW<'a>,
698        q_norm: Option<&'a [f32]>,
699        k_norm: Option<&'a [f32]>,
700        /// (bq, bk, bv) attention biases (Qwen2). None ⇒ no bias.
701        bias: Option<(&'a [f32], &'a [f32], &'a [f32])>,
702        /// Qwen3.5 gated attention: wq emits 2·nh·hd (q||gate per head), the
703        /// attention output is scaled by sigmoid(gate) before the O projection.
704        output_gate: bool,
705        cpu_k: &'a [Vec<f32>],
706        cpu_v: &'a [Vec<f32>],
707    },
708    Gdn {
709        qkv: GraphW<'a>,
710        z: GraphW<'a>,
711        a: GraphW<'a>,
712        b: GraphW<'a>,
713        out: GraphW<'a>,
714        conv1d: &'a [f32],
715        a_log: &'a [f32],
716        dt_bias: &'a [f32],
717        norm: &'a [f32],
718        nv: usize,
719        nk: usize,
720        dk: usize,
721        dv: usize,
722        kk: usize,
723        /// CPU recurrent state `[ring (kk-1)·cdim | S nv·dk·dv]` — seeds the
724        /// device mirror when prefill ran on the host (o1 collection, CPU
725        /// fallback): a zero-initialized device state at decode is exactly
726        /// the "coherent but contextless" garble.
727        cpu_state: &'a [f32],
728    },
729}
730
731/// Per-layer weights for the whole-token wgpu graph.
732pub struct GraphLayer<'a> {
733    pub input_norm: &'a [f32],
734    pub attn: GraphAttn<'a>,
735    pub post_norm: &'a [f32],
736    pub ffn: GraphFfn<'a>,
737}
738
739/// The FFN of one graph layer: a dense SwiGLU trio, or a routed MoE —
740/// router + top-k selection + all selected experts run ON DEVICE (the
741/// routing decision depends on the resident hidden state, so a CPU
742/// round-trip per layer would forfeit the one-submit design).
743pub enum GraphFfn<'a> {
744    Dense {
745        gate: GraphW<'a>,
746        up: GraphW<'a>,
747        down: GraphW<'a>,
748    },
749    Moe {
750        /// Router logits weight (f32, kind 4) `[n_exp, hidden]`.
751        router: GraphW<'a>,
752        /// Shared-expert sigmoid gate (f32) `[1, hidden]`.
753        shared_gate: GraphW<'a>,
754        /// Per-expert q4_tiled directory indices `(gate, up, down)`;
755        /// the SHARED expert rides as the LAST entry — the select
756        /// kernel pins it with the sigmoid weight.
757        experts: Vec<(usize, usize, usize)>,
758        /// Routed experts (shared excluded).
759        n_exp: usize,
760        top_k: usize,
761        inter: usize,
762        norm_topk: bool,
763        /// Expert weight layout, uniform across the layer: `false` =
764        /// q4_tiled (18 B tiles, inline f16 scale), `true` = q4tp
765        /// (16 B nibbles + a per-row ladder plane). The two differ only
766        /// in where the scale comes from, so they share every kernel
767        /// but the weight-staging block.
768        q4tp: bool,
769        /// `true` = the gate/up experts are `q2tp` (2-bit plane) while
770        /// `down` stays q4tp — the mixed profile a 2-bit-class checkpoint
771        /// converts into. Only meaningful with `q4tp: true`.
772        gu_q2: bool,
773    },
774}
775
776/// Whole-token decode graph on wgpu: the entire layer stack in ONE submit,
777/// hidden resident, one readback. Updates `h` in place. false = refusal.
778/// `loop_norm_at`: virtual layer indices after which `final_norm` is applied
779/// (Looped Transformer mid-stack norm). Empty for standard models.
780#[allow(clippy::too_many_arguments)]
781pub fn forward_token_graph(
782    model: &Arc<CmfModel>,
783    kv_id: u64,
784    layers: &[GraphLayer],
785    // Per-layer sealed o1 (Nystrom) state; Some = replace this layer's
786    // exact attention with the O(1) kernels. wgpu only.
787    o1: &[Option<Vec<crate::nystrom::O1DeviceView<'_>>>],
788    o1_epoch: u64,
789    invf: &[f32],
790    h: &mut [f32],
791    nh: usize,
792    nkv: usize,
793    hd: usize,
794    rd: usize,
795    hidden: usize,
796    inter: usize,
797    position: usize,
798    cap: usize,
799    gemma: bool,
800    eps: f32,
801    lm_head: Option<(&GraphW, usize)>,
802    final_norm: &[f32],
803    logits: &mut Vec<f32>,
804    loop_norm_at: &[usize],
805    steps: usize,
806    embed: Option<(&GraphW, usize, f32)>,
807    ids_out: Option<&mut Vec<u32>>,
808) -> bool {
809    match backend() {
810        #[cfg(feature = "gpu")]
811        Backend::Wgpu => crate::gpu_wgpu::forward_token_graph(
812            model,
813            kv_id,
814            layers,
815            o1,
816            o1_epoch,
817            invf,
818            h,
819            nh,
820            nkv,
821            hd,
822            rd,
823            hidden,
824            inter,
825            position,
826            cap,
827            gemma,
828            eps,
829            lm_head,
830            final_norm,
831            logits,
832            loop_norm_at,
833            steps,
834            embed,
835            ids_out,
836        ),
837        #[allow(unused_variables)]
838        _ => {
839            let _ = (lm_head, final_norm, logits, loop_norm_at);
840            false
841        }
842    }
843}
844
845/// Batched prefill: k contiguous positions through the whole graph in one submit
846/// (projections/FFN as GEMMs, attention/GDN looped over scratch). `h` is
847/// [k·hidden] in/out; `positions` len k. wgpu only.
848#[allow(clippy::too_many_arguments)]
849pub fn forward_batch_graph(
850    model: &Arc<CmfModel>,
851    kv_id: u64,
852    layers: &[GraphLayer],
853    invf: &[f32],
854    h: &mut [f32],
855    nh: usize,
856    nkv: usize,
857    hd: usize,
858    rd: usize,
859    hidden: usize,
860    inter: usize,
861    positions: &[usize],
862    cap: usize,
863    gemma: bool,
864    eps: f32,
865    k: usize,
866) -> bool {
867    match backend() {
868        #[cfg(feature = "gpu")]
869        Backend::Wgpu => crate::gpu_wgpu::forward_batch_graph(
870            model, kv_id, layers, invf, h, nh, nkv, hd, rd, hidden, inter, positions, cap, gemma,
871            eps, k,
872        ),
873        _ => false,
874    }
875}
876
877/// Drop the wgpu token graph's device K/V mirror for a pipeline.
878pub fn graph_kv_reset(_kv_id: u64) {
879    #[cfg(feature = "gpu")]
880    if backend() == Backend::Wgpu {
881        crate::gpu_wgpu::kv_mirror_reset(_kv_id);
882    }
883}
884
885/// Ternary (q1t) BASE matvec on the GPU — fills `out` with the base dot; the
886/// caller adds the sparse overlay on the CPU. Metal only for now (wgpu q1t not
887/// yet written → CPU fallback).
888pub fn q1t_matvec(
889    model: &Arc<CmfModel>,
890    idx: usize,
891    xs: &[f32],
892    rows: usize,
893    cols: usize,
894    out: &mut [f32],
895) -> bool {
896    match backend() {
897        #[cfg(target_os = "macos")]
898        Backend::Metal => {
899            if metal_q1t_enabled() {
900                crate::gpu_metal::q1t_matvec(model, idx, xs, rows, cols, out)
901            } else {
902                false
903            }
904        }
905        #[cfg(feature = "gpu")]
906        Backend::Wgpu => crate::gpu_wgpu::q1t_matvec(model, idx, xs, rows, cols, out),
907        Backend::None => false,
908    }
909}
910
911/// q4_block matvec on the GPU — wgpu only (Metal drives q4_block through the
912/// whole-token graph, not a standalone matvec).
913#[allow(unused_variables)]
914pub fn q4b_matvec(
915    model: &Arc<CmfModel>,
916    idx: usize,
917    xs: &[f32],
918    rows: usize,
919    cols: usize,
920    out: &mut [f32],
921) -> bool {
922    match backend() {
923        #[cfg(target_os = "macos")]
924        Backend::Metal => false,
925        #[cfg(feature = "gpu")]
926        Backend::Wgpu => crate::gpu_wgpu::q4b_matvec(model, idx, xs, rows, cols, out),
927        Backend::None => false,
928    }
929}
930
931/// q1t batched GEMM (prefill) — base + overlay on-device (Metal simdgroup or
932/// wgpu register-blocked).
933pub fn q1t_matmat(
934    model: &Arc<CmfModel>,
935    idx: usize,
936    xs: &[f32],
937    b: usize,
938    rows: usize,
939    cols: usize,
940    out: &mut [f32],
941) -> bool {
942    match backend() {
943        #[cfg(target_os = "macos")]
944        // Batched prefill and single-token decode are both enabled. On the
945        // real 14.8B Q1T model prefill PPL was within 0.3% of CPU (7.942 vs
946        // 7.966), and the alignment-safe decode kernel reached 3.52e-6 max_rel.
947        Backend::Metal => crate::gpu_metal::q1t_matmat(model, idx, xs, b, rows, cols, out),
948        #[cfg(feature = "gpu")]
949        Backend::Wgpu => crate::gpu_wgpu::q1t_matmat(model, idx, xs, b, rows, cols, out),
950        Backend::None => false,
951    }
952}
953
954/// Native Metal Q1T switch. Enabled by default after the byte-packed Q1T
955/// fields were changed to alignment-safe loads; keep an explicit emergency
956/// fallback for device/driver diagnostics.
957#[cfg(target_os = "macos")]
958pub(crate) fn metal_q1t_enabled() -> bool {
959    std::env::var("CMF_METAL_Q1T")
960        .map(|v| v != "0" && !v.eq_ignore_ascii_case("off"))
961        .unwrap_or(true)
962}
963
964/// Batched q1 GEMM (prefill). wgpu only — Metal has its own block path.
965pub fn q1_matmat(
966    model: &Arc<CmfModel>,
967    idx: usize,
968    xs: &[f32],
969    b: usize,
970    rows: usize,
971    cols: usize,
972    out: &mut [f32],
973) -> bool {
974    match backend() {
975        #[cfg(feature = "gpu")]
976        Backend::Wgpu => crate::gpu_wgpu::q1_matmat(model, idx, xs, b, rows, cols, out),
977        #[allow(unused_variables)]
978        _ => false,
979    }
980}
981
982/// Contention kill for the wide imagegen GEMM/FFN paths: one grossly
983/// slow op under a work-proportional budget (fair-device ops are
984/// ≤~100 ms even at 1024px) means another process owns the device —
985/// verdicts are per-process, so CPU for the rest of this one.
986static MM_KILL: AtomicBool = AtomicBool::new(false);
987pub(crate) fn mm_killed() -> bool {
988    MM_KILL.load(Ordering::Relaxed)
989}
990pub(crate) fn mm_kill() {
991    MM_KILL.store(true, Ordering::Relaxed);
992}
993
994/// Fused DiT SwiGLU FFN on the device: g=X·W1ᵀ, u=X·W3ᵀ, silu(g)·u,
995/// Causal chunk attention on the device: `b` queries against `s0 + b`
996/// cached keys. wgpu only — Metal's chunk graph keeps attention inside
997/// the resident block and never calls out.
998#[allow(unused_variables, clippy::too_many_arguments)]
999pub fn chunk_attend(
1000    q: &[f32],
1001    k: &[&[f32]],
1002    v: &[&[f32]],
1003    b: usize,
1004    s0: usize,
1005    nh: usize,
1006    nkv: usize,
1007    hd: usize,
1008    scale: f32,
1009    out: &mut [f32],
1010) -> bool {
1011    match backend() {
1012        #[cfg(feature = "gpu")]
1013        Backend::Wgpu => {
1014            crate::gpu_wgpu::chunk_attend(q, k, v, b, s0, nh, nkv, hd, scale, out)
1015        }
1016        #[allow(unreachable_patterns)]
1017        _ => false,
1018    }
1019}
1020
1021/// Fused QKV projection: one upload of the normed chunk, three GEMMs,
1022/// one readback of Q|K|V back to back. Metal has no twin yet — its
1023/// chunk graph keeps the whole layer resident and never surfaces QKV.
1024#[allow(unused_variables, clippy::too_many_arguments)]
1025pub fn q4t_qkv(
1026    model: &Arc<CmfModel>,
1027    wq: usize,
1028    wk: usize,
1029    wv: usize,
1030    xs: &[f32],
1031    b: usize,
1032    cols: usize,
1033    rq: usize,
1034    rk: usize,
1035    rv: usize,
1036    out: &mut [f32],
1037) -> bool {
1038    match backend() {
1039        #[cfg(feature = "gpu")]
1040        Backend::Wgpu => {
1041            crate::gpu_wgpu::q4t_qkv(model, wq, wk, wv, xs, b, cols, rq, rk, rv, out)
1042        }
1043        #[allow(unreachable_patterns)]
1044        _ => false,
1045    }
1046}
1047
1048/// y=·W2ᵀ — one command buffer, only X and Y cross the CPU boundary.
1049#[allow(unused_variables, clippy::too_many_arguments)]
1050pub fn q4tp_ffn(
1051    model: &Arc<CmfModel>,
1052    w1: usize,
1053    w3: usize,
1054    w2: usize,
1055    xs: &[f32],
1056    b: usize,
1057    hidden: usize,
1058    inter: usize,
1059    out: &mut [f32],
1060) -> bool {
1061    match backend() {
1062        #[cfg(target_os = "macos")]
1063        Backend::Metal => crate::gpu_metal::q4tp_ffn(model, w1, w3, w2, xs, b, hidden, inter, out),
1064        #[cfg(feature = "gpu")]
1065        // No wgpu twin yet: the fused DiT chain there is q4t-only, so a q4tp
1066        // model keeps the unfused wgpu path rather than a wrong kernel.
1067        Backend::Wgpu => false,
1068        #[allow(unreachable_patterns)]
1069        _ => false,
1070    }
1071}
1072
1073pub fn q4t_ffn(
1074    model: &Arc<CmfModel>,
1075    w1: usize,
1076    w3: usize,
1077    w2: usize,
1078    xs: &[f32],
1079    b: usize,
1080    hidden: usize,
1081    inter: usize,
1082    out: &mut [f32],
1083) -> bool {
1084    match backend() {
1085        #[cfg(target_os = "macos")]
1086        Backend::Metal => crate::gpu_metal::q4t_ffn(model, w1, w3, w2, xs, b, hidden, inter, out),
1087        #[cfg(feature = "gpu")]
1088        Backend::Wgpu => crate::gpu_wgpu::q4t_ffn(model, w1, w3, w2, xs, b, hidden, inter, out),
1089        #[allow(unreachable_patterns)]
1090        _ => false,
1091    }
1092}
1093
1094/// One whole modulated DiT block for `dit_block`: geometry, norm
1095/// weights, AdaLN scale/gate vectors (gates pre-tanh'd), a per-token
1096/// f32 RoPE cos/sin table, and the directory indices of the seven
1097/// q4t projections. `x` is in-out `[n, hidden]`.
1098pub struct DitBlockArgs<'a> {
1099    pub n: usize,
1100    pub hidden: usize,
1101    pub inter: usize,
1102    pub nh: usize,
1103    pub nkv: usize,
1104    pub hd: usize,
1105    pub eps: f32,
1106    pub rope_cos: &'a [f32],
1107    pub rope_sin: &'a [f32],
1108    pub norm1: &'a [f32],
1109    pub norm2: &'a [f32],
1110    pub ffn_norm1: &'a [f32],
1111    pub ffn_norm2: &'a [f32],
1112    pub norm_q: &'a [f32],
1113    pub norm_k: &'a [f32],
1114    pub s_msa: &'a [f32],
1115    pub gate_msa: &'a [f32],
1116    pub s_mlp: &'a [f32],
1117    pub gate_mlp: &'a [f32],
1118    pub wq: usize,
1119    pub wk: usize,
1120    pub wv: usize,
1121    pub wo: usize,
1122    pub w1: usize,
1123    pub w3: usize,
1124    pub w2: usize,
1125}
1126
1127/// One whole modulated DiT block on the device — norms, qkv, RoPE,
1128/// attention, residuals and the SwiGLU FFN in a single command
1129/// buffer; only `x` crosses the CPU boundary (in and out).
1130#[allow(unused_variables)]
1131pub fn dit_block(model: &Arc<CmfModel>, a: &DitBlockArgs, x: &mut [f32]) -> bool {
1132    match backend() {
1133        #[cfg(target_os = "macos")]
1134        Backend::Metal => crate::gpu_metal::dit_block(model, a, x),
1135        _ => false,
1136    }
1137}
1138
1139/// One VAE resnet block for `vae_resnet`: norm/conv weights and the
1140/// channel/shape geometry. `shortcut` is the 1×1 projection (w, b, k)
1141/// when in/out channels differ.
1142pub struct VaeResnetArgs<'a> {
1143    pub groups: usize,
1144    pub ic: usize,
1145    pub oc: usize,
1146    pub h: usize,
1147    pub w: usize,
1148    pub n1w: &'a [f32],
1149    pub n1b: &'a [f32],
1150    pub c1w: &'a [f32],
1151    pub c1b: &'a [f32],
1152    pub c1k: usize,
1153    pub n2w: &'a [f32],
1154    pub n2b: &'a [f32],
1155    pub c2w: &'a [f32],
1156    pub c2b: &'a [f32],
1157    pub c2k: usize,
1158    pub shortcut: Option<(&'a [f32], &'a [f32], usize)>,
1159}
1160
1161/// One whole VAE resnet block on the device (norm+silu → conv ×2 →
1162/// shortcut → add, one command buffer).
1163#[allow(unused_variables)]
1164pub fn vae_resnet(a: &VaeResnetArgs, x: &[f32], out: &mut [f32]) -> bool {
1165    match backend() {
1166        #[cfg(target_os = "macos")]
1167        Backend::Metal => crate::gpu_metal::vae_resnet(a, x, out),
1168        _ => false,
1169    }
1170}
1171
1172/// Nearest-2× upsample fused with the following conv — the small
1173/// pre-upsample image is what crosses the CPU boundary.
1174#[allow(unused_variables, clippy::too_many_arguments)]
1175pub fn vae_upsample_conv(
1176    w: &[f32],
1177    bias: &[f32],
1178    x: &[f32],
1179    ic: usize,
1180    oc: usize,
1181    h: usize,
1182    w_img: usize,
1183    k: usize,
1184    out: &mut [f32],
1185) -> bool {
1186    match backend() {
1187        #[cfg(target_os = "macos")]
1188        Backend::Metal => crate::gpu_metal::vae_upsample_conv(w, bias, x, ic, oc, h, w_img, k, out),
1189        _ => false,
1190    }
1191}
1192
1193/// VAE conv2d on the device (implicit GEMM — the CPU path pays for a
1194/// multi-GB im2col matrix at high resolutions).
1195#[allow(unused_variables, clippy::too_many_arguments)]
1196pub fn vae_conv2d(
1197    w: &[f32],
1198    bias: &[f32],
1199    x: &[f32],
1200    ic: usize,
1201    oc: usize,
1202    h: usize,
1203    w_img: usize,
1204    k: usize,
1205    out: &mut [f32],
1206) -> bool {
1207    match backend() {
1208        #[cfg(target_os = "macos")]
1209        Backend::Metal => crate::gpu_metal::vae_conv2d(w, bias, x, ic, oc, h, w_img, k, out),
1210        _ => false,
1211    }
1212}
1213
1214/// DiT full bidirectional attention on the device (all heads:
1215/// scores GEMM → row softmax → P·V → panel unstack, one command
1216/// buffer). Head-major inputs; out is [n, nh·hd].
1217#[allow(unused_variables, clippy::too_many_arguments)]
1218pub fn dit_attention(
1219    qh: &[f32],
1220    kh: &[f32],
1221    vh: &[f32],
1222    nh: usize,
1223    nkv: usize,
1224    n: usize,
1225    hd: usize,
1226    scale: f32,
1227    out: &mut [f32],
1228) -> bool {
1229    match backend() {
1230        #[cfg(target_os = "macos")]
1231        Backend::Metal => crate::gpu_metal::dit_attention(qh, kh, vh, nh, nkv, n, hd, scale, out),
1232        #[cfg(feature = "gpu")]
1233        Backend::Wgpu => crate::gpu_wgpu::dit_attention(qh, kh, vh, nh, nkv, n, hd, scale, out),
1234        #[allow(unreachable_patterns)]
1235        _ => false,
1236    }
1237}
1238
1239/// Batched q4t GEMM on the device (imagegen DiT prefill shapes).
1240/// Metal: q4t_mul_mm decodes the mmap-resident tiles inside the
1241/// GEMM's K loop. wgpu (Vulkan/DX12 → NVIDIA/AMD/Intel/Adreno/Mali):
1242/// the register-blocked WGSL twin, weights cached in VRAM.
1243#[allow(unused_variables)]
1244pub fn q4tp_matmat(
1245    model: &Arc<CmfModel>,
1246    idx: usize,
1247    xs: &[f32],
1248    b: usize,
1249    rows: usize,
1250    cols: usize,
1251    out: &mut [f32],
1252) -> bool {
1253    match backend() {
1254        #[cfg(target_os = "macos")]
1255        Backend::Metal => crate::gpu_metal::q4tp_matmat(model, idx, xs, b, rows, cols, out),
1256        #[cfg(feature = "gpu")]
1257        Backend::Wgpu => crate::gpu_wgpu::q4tp_matmat(model, idx, xs, b, rows, cols, out),
1258        #[allow(unreachable_patterns)]
1259        _ => false,
1260    }
1261}
1262
1263pub fn q4t_matmat(
1264    model: &Arc<CmfModel>,
1265    idx: usize,
1266    xs: &[f32],
1267    b: usize,
1268    rows: usize,
1269    cols: usize,
1270    out: &mut [f32],
1271) -> bool {
1272    match backend() {
1273        #[cfg(target_os = "macos")]
1274        Backend::Metal => crate::gpu_metal::q4t_matmat(model, idx, xs, b, rows, cols, out),
1275        #[cfg(feature = "gpu")]
1276        Backend::Wgpu => crate::gpu_wgpu::q4t_matmat(model, idx, xs, b, rows, cols, out),
1277        #[allow(unreachable_patterns)]
1278        _ => false,
1279    }
1280}
1281
1282/// Whole-block token-graph types re-exported from the Metal backend.
1283#[cfg(target_os = "macos")]
1284pub use crate::gpu_metal::{
1285    AttnDeviceParams, AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, GraphDims, TokenGraph, kv_mirror_drop,
1286    kv_mirror_read_last, kv_mirror_take_imp,
1287};
1288
1289/// A BLOCK of consecutive q1 GDN layers in one submission (Metal only).
1290#[cfg(target_os = "macos")]
1291pub fn gdn_block(
1292    model: &Arc<CmfModel>,
1293    layers: &[GdnGpuLayer],
1294    states: &mut [&mut [f32]],
1295    cfg: &GdnGpuCfg,
1296    h: &mut [f32],
1297) -> bool {
1298    match backend() {
1299        Backend::Metal => crate::gpu_metal::gdn_block(model, layers, states, cfg, h),
1300        _ => false,
1301    }
1302}
1303
1304/// A layer's MoE-FFN in one submission (amortizing the dispatch cost).
1305#[allow(unused_variables)]
1306pub fn moe_block(model: &Arc<CmfModel>, jobs: &[MoeJob], out: &mut [f32]) -> bool {
1307    match backend() {
1308        #[cfg(target_os = "macos")]
1309        Backend::Metal => crate::gpu_metal::moe_block(model, jobs, out),
1310        #[cfg(feature = "gpu")]
1311        Backend::Wgpu => crate::gpu_wgpu::moe_block(model, jobs, out),
1312        Backend::None => false,
1313    }
1314}
1315
1316/// Independent matvecs of one input in a single submission (GDN projections).
1317#[allow(unused_variables)]
1318pub fn matvec_batch(model: &Arc<CmfModel>, jobs: &[BatchJob], out: &mut [&mut [f32]]) -> bool {
1319    match backend() {
1320        #[cfg(target_os = "macos")]
1321        Backend::Metal => crate::gpu_metal::matvec_batch(model, jobs, out),
1322        #[cfg(feature = "gpu")]
1323        Backend::Wgpu => crate::gpu_wgpu::matvec_batch(model, jobs, out),
1324        Backend::None => false,
1325    }
1326}
1327
1328// ── Whole-token wgpu graph race (generation granularity) ─────────────
1329// On integrated/mobile adapters the graph is neither trusted nor banned
1330// a priori — it RACES the normal path: generations alternate arms (the
1331// normal path first — known-good UX — then the graph), per-token wall
1332// times accumulate per arm, and once both arms have enough steady
1333// samples the faster one wins for the process. Arm switches happen ONLY
1334// at generation boundaries (`kv_cache.clear()` resets state), so the
1335// device KV mirror and the CPU cache never diverge mid-sequence. The
1336// single exception is the first-token bail: the very first decode token
1337// of a graph generation may be discarded and recomputed on the CPU
1338// path (the prompt KV is CPU-owned at that point, so this is safe) —
1339// a tiled mobile GPU that drains its pipeline at every barrier turns
1340// the ~300-dispatch graph into seconds per token (field report: 0.2
1341// tok/s vs 15 on the CPU), and one token is all it takes to see that.
1342static GRAPH_RACE_STATE: AtomicU8 = AtomicU8::new(0); // 0 racing, 1 graph won, 2 normal won
1343static GRAPH_RACE_FLIP: AtomicU32 = AtomicU32::new(0);
1344static GRAPH_RACE_ARM_GRAPH: AtomicU8 = AtomicU8::new(0); // this generation's arm
1345static GRAPH_RACE_TOK: AtomicU32 = AtomicU32::new(0); // token index within the generation
1346static GRAPH_NS: [AtomicU64; 2] = [AtomicU64::new(0), AtomicU64::new(0)]; // [normal, graph]
1347static GRAPH_N: [AtomicU32; 2] = [AtomicU32::new(0), AtomicU32::new(0)];
1348
1349/// Steady per-token samples per arm before the race decides.
1350const GRAPH_RACE_SAMPLES: u32 = 4;
1351
1352/// Called at every generation start (fresh KV). Applies a pending
1353/// verdict and picks this generation's arm while racing.
1354pub fn graph_race_begin_generation() {
1355    GRAPH_RACE_TOK.store(0, Ordering::Relaxed);
1356    if GRAPH_RACE_STATE.load(Ordering::Relaxed) != 0 {
1357        return;
1358    }
1359    let (gn, cn) = (
1360        GRAPH_N[1].load(Ordering::Relaxed),
1361        GRAPH_N[0].load(Ordering::Relaxed),
1362    );
1363    if gn >= GRAPH_RACE_SAMPLES && cn >= GRAPH_RACE_SAMPLES {
1364        let g_avg = GRAPH_NS[1].load(Ordering::Relaxed) / gn as u64;
1365        let c_avg = GRAPH_NS[0].load(Ordering::Relaxed) / cn as u64;
1366        let verdict = if g_avg < c_avg { 1 } else { 2 };
1367        GRAPH_RACE_STATE.store(verdict, Ordering::Relaxed);
1368        tracing::info!(
1369            "wgpu graph race: graph {:.2} ms/tok vs normal {:.2} ms/tok -> {}",
1370            g_avg as f64 / 1e6,
1371            c_avg as f64 / 1e6,
1372            if verdict == 1 { "graph" } else { "normal path" }
1373        );
1374        return;
1375    }
1376    let flip = GRAPH_RACE_FLIP.fetch_add(1, Ordering::Relaxed);
1377    GRAPH_RACE_ARM_GRAPH.store((flip % 2 == 1) as u8, Ordering::Relaxed);
1378}
1379
1380/// Should this decode token try the graph? `trusted` (discrete adapter,
1381/// explicit env, or a GDN hybrid whose state lives on the device) skips
1382/// the race entirely.
1383pub fn graph_race_use_graph(trusted: bool) -> bool {
1384    if trusted {
1385        return true;
1386    }
1387    match GRAPH_RACE_STATE.load(Ordering::Relaxed) {
1388        1 => true,
1389        2 => false,
1390        _ => GRAPH_RACE_ARM_GRAPH.load(Ordering::Relaxed) == 1,
1391    }
1392}
1393
1394/// First decode token of a racing graph generation: hopeless already?
1395/// (>4x the normal path's per-token average AND over a second.) Settles
1396/// the race immediately; the caller discards the graph result and
1397/// recomputes this token on the normal path.
1398pub fn graph_race_first_token_hopeless(dur: std::time::Duration) -> bool {
1399    if GRAPH_RACE_STATE.load(Ordering::Relaxed) != 0 {
1400        return false;
1401    }
1402    let first = GRAPH_RACE_TOK.load(Ordering::Relaxed) == 0;
1403    let cn = GRAPH_N[0].load(Ordering::Relaxed);
1404    if !first || cn == 0 {
1405        return false;
1406    }
1407    let c_avg = GRAPH_NS[0].load(Ordering::Relaxed) / cn as u64;
1408    let ns = dur.as_nanos() as u64;
1409    if ns > 1_000_000_000 && ns > 4 * c_avg {
1410        GRAPH_RACE_STATE.store(2, Ordering::Relaxed);
1411        tracing::info!(
1412            "wgpu graph race: first graph token {:.0} ms vs normal {:.2} ms/tok — hopeless, normal path wins",
1413            ns as f64 / 1e6,
1414            c_avg as f64 / 1e6
1415        );
1416        return true;
1417    }
1418    false
1419}
1420
1421/// Record one decode-token wall time for the racing arm. The first
1422/// token of each generation is discarded (KV-mirror upload / cold
1423/// caches on the graph arm; cold mmap on the normal arm).
1424pub fn graph_race_record(used_graph: bool, dur: std::time::Duration) {
1425    if GRAPH_RACE_STATE.load(Ordering::Relaxed) != 0 {
1426        return;
1427    }
1428    let tok = GRAPH_RACE_TOK.fetch_add(1, Ordering::Relaxed);
1429    if tok == 0 {
1430        return;
1431    }
1432    let i = used_graph as usize;
1433    GRAPH_NS[i].fetch_add(dur.as_nanos() as u64, Ordering::Relaxed);
1434    GRAPH_N[i].fetch_add(1, Ordering::Relaxed);
1435}