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