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