Skip to main content

cortiq_engine/
lib.rs

1//! Cortiq inference engine — sparse forward pass, attention, tokenization, sampling.
2
3pub mod attention;
4pub mod audiovae;
5pub mod dit;
6pub mod dsv4;
7pub mod dsv41;
8pub mod dsv41_encoding;
9pub mod dsv41_vision;
10pub mod fcd;
11pub mod fcd_ops;
12pub mod g3n;
13pub mod gptq_capture;
14pub mod gpu;
15#[cfg(target_os = "macos")]
16pub mod gpu_metal;
17#[cfg(feature = "gpu")]
18pub mod gpu_wgpu;
19pub mod imagegen;
20pub mod inference;
21pub mod kv_cache;
22pub mod linear_core;
23pub mod loader;
24pub mod ltxaudio;
25pub mod ltxdit;
26pub mod ltxdur;
27pub mod ltxenc;
28pub mod ltxlora;
29pub mod ltxpipe;
30pub mod ltxte;
31pub mod ltxups;
32pub mod ltxvae;
33pub mod mm_ab;
34pub mod mmh3;
35pub mod mmh3ups;
36pub mod music3;
37pub mod nystrom;
38pub mod pin;
39pub mod pipeline;
40pub mod pool;
41pub mod prism;
42pub mod qtensor;
43pub mod qwen3te;
44pub mod qwen3vis;
45pub mod qwen4_exp;
46pub mod qwen_image;
47pub mod qwen_image_encoder;
48pub mod qwen_image_ops;
49pub mod qwen_image_vae;
50pub mod qwen_image_vision;
51pub mod qwen_imagegen;
52pub mod router;
53pub mod runtime;
54pub mod sampler;
55pub mod skillbake;
56pub mod swarm;
57pub mod textenc;
58pub mod tokenizer;
59pub mod vae;
60pub mod vae3d;
61pub mod videogen;
62/// The native Vulkan lane — an accelerator behind a capability probe,
63/// present only where Vulkan is.
64#[cfg(all(
65    feature = "gpu",
66    any(target_os = "linux", target_os = "windows", target_os = "android")
67))]
68pub use nystrom::NystromState;
69pub use pipeline::{GenerateResult, Pipeline, TokenCallback, TokenTrace};
70pub use runtime::CortiqRuntime;
71
72/// Test-only: N empty Metal command-buffer round trips, total seconds.
73#[doc(hidden)]
74#[cfg(target_os = "macos")]
75pub fn gpu_empty_submit_for_test(n: usize) -> f64 {
76    gpu_metal::empty_submit_bench(n)
77}
78
79/// Test-only: N pipelined empty submits, one final wait.
80#[doc(hidden)]
81#[cfg(target_os = "macos")]
82pub fn gpu_pipelined_submit_for_test(n: usize) -> f64 {
83    gpu_metal::pipelined_submit_bench(n)
84}
85
86/// Test-only: build a q1 MoeJob trio (weight 1.0).
87#[doc(hidden)]
88#[cfg(target_os = "macos")]
89pub fn gpu_moe_job_for_test(
90    gi: usize,
91    ui: usize,
92    di: usize,
93    inter: usize,
94    hidden: usize,
95    x: Vec<f32>,
96) -> gpu::MoeJob<'static> {
97    gpu::MoeJob {
98        gate: (gi, inter, hidden, &[]),
99        up: (ui, inter, hidden, &[]),
100        down: (di, hidden, inter, &[]),
101        xs_gate: x.clone(),
102        xs_up: x,
103        down_col: &[],
104        w: 1.0,
105        q1: true,
106        q4t: false,
107        q4tp: false,
108        gu_q2: false,
109        swiglu_limit: 0.0,
110    }
111}
112
113/// Test-only: run the metal moe_block on one job.
114#[doc(hidden)]
115#[cfg(target_os = "macos")]
116pub fn gpu_moe_block_for_test(
117    model: &std::sync::Arc<cortiq_core::CmfModel>,
118    job: gpu::MoeJob<'_>,
119    out: &mut [f32],
120) -> bool {
121    gpu_metal::moe_block(model, &[job], out)
122}
123
124/// Test-only: q1 matvec_batch — jobs (idx, rows, cols); first two share
125/// x, the third takes xi.
126#[doc(hidden)]
127#[cfg(target_os = "macos")]
128pub fn gpu_batch_q1_for_test(
129    model: &std::sync::Arc<cortiq_core::CmfModel>,
130    shapes: &[(usize, usize, usize)],
131    x: &[f32],
132    xi: &[f32],
133    outs: &mut [&mut [f32]],
134) -> bool {
135    let jobs: Vec<gpu::BatchJob> = shapes
136        .iter()
137        .enumerate()
138        .map(|(k, &(idx, rows, cols))| gpu::BatchJob {
139            idx,
140            rows,
141            cols,
142            row_scale: &[],
143            xs: if k < 2 { x.to_vec() } else { xi.to_vec() },
144            layout: gpu::BatchLayout::Q1,
145        })
146        .collect();
147    gpu_metal::matvec_batch(model, &jobs, outs)
148}
149
150/// Test-only direct handle to the Metal q1 matvec (micro-benchmarks).
151#[doc(hidden)]
152#[cfg(target_os = "macos")]
153pub fn gpu_q1_matvec_for_test(
154    model: &std::sync::Arc<cortiq_core::CmfModel>,
155    idx: usize,
156    xs: &[f32],
157    rows: usize,
158    cols: usize,
159    out: &mut [f32],
160) -> bool {
161    gpu_metal::q1_matvec(model, idx, xs, rows, cols, out)
162}
163pub use sampler::SamplerConfig;