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