Skip to main content

cortiq_engine/
lib.rs

1//! Cortiq inference engine — sparse forward pass, attention, tokenization, sampling.
2
3pub mod attention;
4pub mod chat_template;
5pub mod cpuprof;
6pub mod audiovae;
7pub mod blr;
8pub mod bounded;
9pub mod dit;
10pub mod dsv4;
11pub mod dsv41;
12pub mod dsv41_encoding;
13pub mod dsv41_vision;
14pub mod egemma2;
15pub mod egemma2_audio;
16pub mod egemma2_mm;
17pub mod egemma2_vision;
18pub(crate) mod expert_store;
19pub mod fcd;
20pub mod fcd_ops;
21#[cfg(feature = "gpu")]
22pub mod nvg_train;
23pub mod f32_backend;
24pub mod g3n;
25pub mod g4e;
26pub mod gptq_capture;
27pub mod gpu;
28#[cfg(target_os = "macos")]
29pub mod gpu_metal;
30#[cfg(feature = "gpu")]
31pub mod gpu_wgpu;
32/// The wgpu crate the device types of `gpu_wgpu` come from (offline tools
33/// hold buffers across calls).
34#[cfg(feature = "gpu")]
35pub use wgpu;
36pub mod imagegen;
37pub mod inference;
38pub mod kv_cache;
39pub mod linear_core;
40pub mod loader;
41pub mod lookup;
42pub mod mimo_moe;
43pub(crate) mod minicpm_tail;
44pub mod ltxaudio;
45pub mod ltxdit;
46pub mod ltxdur;
47pub mod ltxenc;
48pub mod ltxlora;
49pub mod ltxpipe;
50pub mod ltxte;
51pub mod ltxups;
52pub mod ltxvae;
53pub mod media;
54pub mod mimo_audio;
55pub mod mimo_mm;
56pub mod mimo_ingress;
57pub mod mimo_vision;
58pub mod mm_ab;
59pub mod mmh3;
60pub mod mmh3ups;
61pub mod music3;
62pub mod moss_image;
63pub mod moss_imagegen;
64pub mod nystrom;
65pub mod pin;
66pub mod pipeline;
67pub mod pool;
68pub mod prism;
69pub mod qtensor;
70pub mod qwen3te;
71pub mod qwen3vis;
72pub mod qwen4_exp;
73pub mod qwen_image;
74pub mod qwen_image21;
75pub mod qwen_image21_vae;
76pub mod qwen_image21gen;
77pub mod qwen_image_encoder;
78pub mod qwen_image_ops;
79pub mod qwen_image_vae;
80pub mod qwen_image_vision;
81pub mod qwen_imagegen;
82pub mod router;
83pub mod runtime;
84pub mod sampler;
85pub mod skillbake;
86pub mod swarm;
87pub mod textenc;
88pub mod tokenizer;
89pub mod vae;
90pub mod vae3d;
91pub mod videogen;
92/// Native Whisper encoder/decoder inference over CMF checkpoints.
93pub mod whisper;
94pub mod zimage;
95pub mod zimagegen;
96/// The native Vulkan lane — an accelerator behind a capability probe,
97/// present only where Vulkan is.
98#[cfg(all(
99    feature = "gpu",
100    any(target_os = "linux", target_os = "windows", target_os = "android")
101))]
102pub use nystrom::NystromState;
103pub use pipeline::{GenerateResult, Pipeline, TokenCallback, TokenTrace};
104pub use runtime::CortiqRuntime;
105
106/// Test-only: N empty Metal command-buffer round trips, total seconds.
107#[doc(hidden)]
108#[cfg(target_os = "macos")]
109pub fn gpu_empty_submit_for_test(n: usize) -> f64 {
110    gpu_metal::empty_submit_bench(n)
111}
112
113/// Test-only: N pipelined empty submits, one final wait.
114#[doc(hidden)]
115#[cfg(target_os = "macos")]
116pub fn gpu_pipelined_submit_for_test(n: usize) -> f64 {
117    gpu_metal::pipelined_submit_bench(n)
118}
119
120/// Test-only: build a q1 MoeJob trio (weight 1.0).
121#[doc(hidden)]
122#[cfg(target_os = "macos")]
123pub fn gpu_moe_job_for_test(
124    gi: usize,
125    ui: usize,
126    di: usize,
127    inter: usize,
128    hidden: usize,
129    x: Vec<f32>,
130) -> gpu::MoeJob<'static> {
131    gpu::MoeJob {
132        gate: (gi, inter, hidden, &[]),
133        up: (ui, inter, hidden, &[]),
134        down: (di, hidden, inter, &[]),
135        xs_gate: x.clone(),
136        xs_up: x,
137        down_col: &[],
138        w: 1.0,
139        q1: true,
140        q4t: false,
141        q4tp: false,
142        gu_q2: false,
143        swiglu_limit: 0.0,
144        act: 0,
145    }
146}
147
148/// Test-only: run the metal moe_block on one job.
149#[doc(hidden)]
150#[cfg(target_os = "macos")]
151pub fn gpu_moe_block_for_test(
152    model: &std::sync::Arc<cortiq_core::CmfModel>,
153    job: gpu::MoeJob<'_>,
154    out: &mut [f32],
155) -> bool {
156    gpu_metal::moe_block(model, &[job], out)
157}
158
159/// Test-only: q1 matvec_batch — jobs (idx, rows, cols); first two share
160/// x, the third takes xi.
161#[doc(hidden)]
162#[cfg(target_os = "macos")]
163pub fn gpu_batch_q1_for_test(
164    model: &std::sync::Arc<cortiq_core::CmfModel>,
165    shapes: &[(usize, usize, usize)],
166    x: &[f32],
167    xi: &[f32],
168    outs: &mut [&mut [f32]],
169) -> bool {
170    let jobs: Vec<gpu::BatchJob> = shapes
171        .iter()
172        .enumerate()
173        .map(|(k, &(idx, rows, cols))| gpu::BatchJob {
174            idx,
175            rows,
176            cols,
177            row_scale: &[],
178            xs: if k < 2 { x.to_vec() } else { xi.to_vec() },
179            layout: gpu::BatchLayout::Q1,
180        })
181        .collect();
182    gpu_metal::matvec_batch(model, &jobs, outs)
183}
184
185/// Test-only direct handle to the Metal q1 matvec (micro-benchmarks).
186#[doc(hidden)]
187#[cfg(target_os = "macos")]
188pub fn gpu_q1_matvec_for_test(
189    model: &std::sync::Arc<cortiq_core::CmfModel>,
190    idx: usize,
191    xs: &[f32],
192    rows: usize,
193    cols: usize,
194    out: &mut [f32],
195) -> bool {
196    gpu_metal::q1_matvec(model, idx, xs, rows, cols, out)
197}
198pub use sampler::SamplerConfig;
199/// Re-exported for offline tools (the NVG encoder's f64 GEMMs in the CLI).
200pub use matrixmultiply;