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