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