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