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