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