Skip to main content

cortiq_engine/
lib.rs

1//! Cortiq inference engine — sparse forward pass, attention, tokenization, sampling.
2
3pub mod attention;
4pub mod audiovae;
5pub mod dit;
6pub mod dsv4;
7pub mod fcd;
8pub mod fcd_ops;
9pub mod g3n;
10pub mod gptq_capture;
11pub mod gpu;
12#[cfg(target_os = "macos")]
13pub mod gpu_metal;
14#[cfg(feature = "gpu")]
15pub mod gpu_wgpu;
16/// The native Vulkan lane — an accelerator behind a capability probe,
17/// present only where Vulkan is.
18#[cfg(all(
19    feature = "gpu",
20    any(target_os = "linux", target_os = "windows", target_os = "android")
21))]
22pub mod vulkan;
23pub mod imagegen;
24pub mod inference;
25pub mod kv_cache;
26pub mod linear_core;
27pub mod loader;
28pub mod mmh3;
29pub mod nystrom;
30pub mod pin;
31pub mod pipeline;
32pub mod pool;
33pub mod qtensor;
34pub mod qwen3te;
35pub mod qwen3vis;
36pub mod router;
37pub mod runtime;
38pub mod sampler;
39pub mod skillbake;
40pub mod swarm;
41pub mod textenc;
42pub mod tokenizer;
43pub mod vae;
44pub mod vae3d;
45pub mod videogen;
46
47pub use nystrom::NystromState;
48pub use pipeline::{GenerateResult, Pipeline, TokenCallback, TokenTrace};
49pub use runtime::CortiqRuntime;
50
51/// Test-only: N empty Metal command-buffer round trips, total seconds.
52#[doc(hidden)]
53#[cfg(target_os = "macos")]
54pub fn gpu_empty_submit_for_test(n: usize) -> f64 {
55    gpu_metal::empty_submit_bench(n)
56}
57
58/// Test-only: N pipelined empty submits, one final wait.
59#[doc(hidden)]
60#[cfg(target_os = "macos")]
61pub fn gpu_pipelined_submit_for_test(n: usize) -> f64 {
62    gpu_metal::pipelined_submit_bench(n)
63}
64
65/// Test-only: build a q1 MoeJob trio (weight 1.0).
66#[doc(hidden)]
67#[cfg(target_os = "macos")]
68pub fn gpu_moe_job_for_test(
69    gi: usize,
70    ui: usize,
71    di: usize,
72    inter: usize,
73    hidden: usize,
74    x: Vec<f32>,
75) -> gpu::MoeJob<'static> {
76    gpu::MoeJob {
77        gate: (gi, inter, hidden, &[]),
78        up: (ui, inter, hidden, &[]),
79        down: (di, hidden, inter, &[]),
80        xs_gate: x.clone(),
81        xs_up: x,
82        down_col: &[],
83        w: 1.0,
84        q1: true,
85        q4t: false,
86        q4tp: false,
87            swiglu_limit: 0.0,
88    }
89}
90
91/// Test-only: run the metal moe_block on one job.
92#[doc(hidden)]
93#[cfg(target_os = "macos")]
94pub fn gpu_moe_block_for_test(
95    model: &std::sync::Arc<cortiq_core::CmfModel>,
96    job: gpu::MoeJob<'_>,
97    out: &mut [f32],
98) -> bool {
99    gpu_metal::moe_block(model, &[job], out)
100}
101
102/// Test-only: q1 matvec_batch — jobs (idx, rows, cols); first two share
103/// x, the third takes xi.
104#[doc(hidden)]
105#[cfg(target_os = "macos")]
106pub fn gpu_batch_q1_for_test(
107    model: &std::sync::Arc<cortiq_core::CmfModel>,
108    shapes: &[(usize, usize, usize)],
109    x: &[f32],
110    xi: &[f32],
111    outs: &mut [&mut [f32]],
112) -> bool {
113    let jobs: Vec<gpu::BatchJob> = shapes
114        .iter()
115        .enumerate()
116        .map(|(k, &(idx, rows, cols))| gpu::BatchJob {
117            idx,
118            rows,
119            cols,
120            row_scale: &[],
121            xs: if k < 2 { x.to_vec() } else { xi.to_vec() },
122            layout: gpu::BatchLayout::Q1,
123        })
124        .collect();
125    gpu_metal::matvec_batch(model, &jobs, outs)
126}
127
128/// Test-only direct handle to the Metal q1 matvec (micro-benchmarks).
129#[doc(hidden)]
130#[cfg(target_os = "macos")]
131pub fn gpu_q1_matvec_for_test(
132    model: &std::sync::Arc<cortiq_core::CmfModel>,
133    idx: usize,
134    xs: &[f32],
135    rows: usize,
136    cols: usize,
137    out: &mut [f32],
138) -> bool {
139    gpu_metal::q1_matvec(model, idx, xs, rows, cols, out)
140}
141pub use sampler::SamplerConfig;