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 d1;
10pub mod d1_parse;
11pub mod dflash;
12pub mod dit;
13pub mod dsv4;
14pub mod dory;
15pub mod dsv41;
16pub mod dsv41_encoding;
17pub mod dsv41_vision;
18pub mod egemma2;
19pub mod egemma2_audio;
20pub mod egemma2_mm;
21pub mod egemma2_vision;
22pub(crate) mod expert_store;
23pub mod fcd;
24pub mod fcd_ops;
25#[cfg(feature = "gpu")]
26pub mod nvg_train;
27pub mod f32_backend;
28pub mod g3n;
29pub mod g4e;
30pub mod gptq_capture;
31pub mod gpu;
32#[cfg(target_os = "macos")]
33pub mod gpu_metal;
34#[cfg(feature = "cuda")]
35pub mod gpu_cuda;
36#[cfg(feature = "gpu")]
37pub mod gpu_wgpu;
38/// The wgpu crate the device types of `gpu_wgpu` come from (offline tools
39/// hold buffers across calls).
40#[cfg(feature = "gpu")]
41pub use wgpu;
42pub mod imagegen;
43pub mod inference;
44pub mod kv_cache;
45pub mod lfm3_audio;
46pub mod lfm3_speech;
47pub mod qwen3_tts;
48pub mod haru;
49pub mod lfm3_vl;
50pub mod linear_core;
51pub mod loader;
52pub mod lookup;
53pub mod mimo_moe;
54pub(crate) mod minicpm_tail;
55pub mod ltxaudio;
56pub mod ltxdit;
57pub mod ltxdur;
58pub mod ltxenc;
59pub mod ltxlora;
60pub mod ltxpipe;
61pub mod ltxte;
62pub mod ltxups;
63pub mod ltxvae;
64pub mod media;
65pub mod mimo_audio;
66pub mod mimo_mm;
67pub mod mimo_ingress;
68pub mod mimo_vision;
69pub mod minicpm_ocr;
70pub mod onevision2;
71pub mod mm_ab;
72pub mod h3imagegen;
73pub mod mmh3;
74pub mod mmh3ups;
75pub mod music3;
76pub mod moss_image;
77pub mod moss_imagegen;
78pub mod nystrom;
79pub mod pin;
80pub mod pipeline;
81/// Gradium Phonon text-to-speech execution for self-contained CMF packages.
82#[cfg(feature = "phonon")]
83pub mod phonon;
84pub mod pool;
85pub mod primal_vae;
86pub mod prism;
87pub mod qtensor;
88pub mod qwen3te;
89pub mod qwen3vis;
90pub mod qwen4_exp;
91pub mod qwen4_o1;
92pub mod qwen_image;
93pub mod qwen_image21;
94pub mod qwen_image21_vae;
95pub mod qwen_image21gen;
96pub mod qwen_image_encoder;
97pub mod qwen_image_ops;
98pub mod qwen_image_vae;
99pub mod qwen_image_vision;
100pub mod qwen_imagegen;
101pub mod router;
102pub mod runtime;
103pub mod s2v;
104pub mod s2v_enc;
105pub mod s2v_vae;
106pub mod s2vgen;
107pub mod sam3;
108pub mod sam3vid;
109pub(crate) mod sam3vid_net;
110pub mod sampler;
111pub mod sensenova;
112pub mod skillbake;
113pub mod swarm;
114pub mod textenc;
115pub mod tokenizer;
116pub mod uocr;
117pub mod vae;
118pub mod vae3d;
119pub mod videogen;
120/// NVIDIA Nemotron 3.5 Transcribe: audio perception + transcription loop.
121pub mod nemotron_asr;
122/// Nemotron-H hybrid Mamba-2 / attention / MoE decoder.
123pub mod nemotron_h;
124/// Native Whisper encoder/decoder inference over CMF checkpoints.
125pub mod whisper;
126pub mod zimage;
127pub mod zimagegen;
128/// The native Vulkan lane — an accelerator behind a capability probe,
129/// present only where Vulkan is.
130#[cfg(all(
131    feature = "gpu",
132    any(target_os = "linux", target_os = "windows", target_os = "android")
133))]
134pub use nystrom::NystromState;
135pub use pipeline::{GenerateResult, MtpFfnProbe, Pipeline, TokenCallback, TokenTrace};
136pub use runtime::CortiqRuntime;
137
138/// Test-only: N empty Metal command-buffer round trips, total seconds.
139#[doc(hidden)]
140#[cfg(target_os = "macos")]
141pub fn gpu_empty_submit_for_test(n: usize) -> f64 {
142    gpu_metal::empty_submit_bench(n)
143}
144
145/// Test-only: N pipelined empty submits, one final wait.
146#[doc(hidden)]
147#[cfg(target_os = "macos")]
148pub fn gpu_pipelined_submit_for_test(n: usize) -> f64 {
149    gpu_metal::pipelined_submit_bench(n)
150}
151
152/// Test-only: build a q1 MoeJob trio (weight 1.0).
153#[doc(hidden)]
154#[cfg(target_os = "macos")]
155pub fn gpu_moe_job_for_test(
156    gi: usize,
157    ui: usize,
158    di: usize,
159    inter: usize,
160    hidden: usize,
161    x: Vec<f32>,
162) -> gpu::MoeJob<'static> {
163    gpu::MoeJob {
164        gate: (gi, inter, hidden, &[]),
165        up: (ui, inter, hidden, &[]),
166        down: (di, hidden, inter, &[]),
167        xs_gate: x.clone(),
168        xs_up: x,
169        down_col: &[],
170        w: 1.0,
171        q1: true,
172        q4t: false,
173        q4tp: false,
174        gu_q2: false,
175        swiglu_limit: 0.0,
176        act: 0,
177    }
178}
179
180/// Test-only: run the metal moe_block on one job.
181#[doc(hidden)]
182#[cfg(target_os = "macos")]
183pub fn gpu_moe_block_for_test(
184    model: &std::sync::Arc<cortiq_core::CmfModel>,
185    job: gpu::MoeJob<'_>,
186    out: &mut [f32],
187) -> bool {
188    gpu_metal::moe_block(model, &[job], out)
189}
190
191/// Test-only: q1 matvec_batch — jobs (idx, rows, cols); first two share
192/// x, the third takes xi.
193#[doc(hidden)]
194#[cfg(target_os = "macos")]
195pub fn gpu_batch_q1_for_test(
196    model: &std::sync::Arc<cortiq_core::CmfModel>,
197    shapes: &[(usize, usize, usize)],
198    x: &[f32],
199    xi: &[f32],
200    outs: &mut [&mut [f32]],
201) -> bool {
202    let jobs: Vec<gpu::BatchJob> = shapes
203        .iter()
204        .enumerate()
205        .map(|(k, &(idx, rows, cols))| gpu::BatchJob {
206            idx,
207            rows,
208            cols,
209            row_scale: &[],
210            xs: if k < 2 { x.to_vec() } else { xi.to_vec() },
211            layout: gpu::BatchLayout::Q1,
212        })
213        .collect();
214    gpu_metal::matvec_batch(model, &jobs, outs)
215}
216
217/// Test-only direct handle to the Metal q1 matvec (micro-benchmarks).
218#[doc(hidden)]
219#[cfg(target_os = "macos")]
220pub fn gpu_q1_matvec_for_test(
221    model: &std::sync::Arc<cortiq_core::CmfModel>,
222    idx: usize,
223    xs: &[f32],
224    rows: usize,
225    cols: usize,
226    out: &mut [f32],
227) -> bool {
228    gpu_metal::q1_matvec(model, idx, xs, rows, cols, out)
229}
230pub use sampler::SamplerConfig;
231/// Re-exported for offline tools (the NVG encoder's f64 GEMMs in the CLI).
232pub use matrixmultiply;
233
234/// Device forward of one S2V input for `cortiq s2v-check` (None without a device path).
235pub fn s2v_check_device(dit: &s2v::S2vDit, i: &s2v::S2vInput<'_>) -> Option<Vec<f32>> {
236    gpu::s2v_check_forward(dit, i)
237}