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 qtensor;
42pub mod qwen3te;
43pub mod qwen3vis;
44pub mod qwen4_exp;
45pub mod qwen_image;
46pub mod qwen_image_encoder;
47pub mod qwen_image_ops;
48pub mod qwen_image_vae;
49pub mod qwen_image_vision;
50pub mod qwen_imagegen;
51pub mod router;
52pub mod runtime;
53pub mod sampler;
54pub mod skillbake;
55pub mod swarm;
56pub mod textenc;
57pub mod tokenizer;
58pub mod vae;
59pub mod vae3d;
60pub mod videogen;
61/// The native Vulkan lane — an accelerator behind a capability probe,
62/// present only where Vulkan is.
63#[cfg(all(
64    feature = "gpu",
65    any(target_os = "linux", target_os = "windows", target_os = "android")
66))]
67pub use nystrom::NystromState;
68pub use pipeline::{GenerateResult, Pipeline, TokenCallback, TokenTrace};
69pub use runtime::CortiqRuntime;
70
71/// Test-only: N empty Metal command-buffer round trips, total seconds.
72#[doc(hidden)]
73#[cfg(target_os = "macos")]
74pub fn gpu_empty_submit_for_test(n: usize) -> f64 {
75    gpu_metal::empty_submit_bench(n)
76}
77
78/// Test-only: N pipelined empty submits, one final wait.
79#[doc(hidden)]
80#[cfg(target_os = "macos")]
81pub fn gpu_pipelined_submit_for_test(n: usize) -> f64 {
82    gpu_metal::pipelined_submit_bench(n)
83}
84
85/// Test-only: build a q1 MoeJob trio (weight 1.0).
86#[doc(hidden)]
87#[cfg(target_os = "macos")]
88pub fn gpu_moe_job_for_test(
89    gi: usize,
90    ui: usize,
91    di: usize,
92    inter: usize,
93    hidden: usize,
94    x: Vec<f32>,
95) -> gpu::MoeJob<'static> {
96    gpu::MoeJob {
97        gate: (gi, inter, hidden, &[]),
98        up: (ui, inter, hidden, &[]),
99        down: (di, hidden, inter, &[]),
100        xs_gate: x.clone(),
101        xs_up: x,
102        down_col: &[],
103        w: 1.0,
104        q1: true,
105        q4t: false,
106        q4tp: false,
107        gu_q2: false,
108        swiglu_limit: 0.0,
109    }
110}
111
112/// Test-only: run the metal moe_block on one job.
113#[doc(hidden)]
114#[cfg(target_os = "macos")]
115pub fn gpu_moe_block_for_test(
116    model: &std::sync::Arc<cortiq_core::CmfModel>,
117    job: gpu::MoeJob<'_>,
118    out: &mut [f32],
119) -> bool {
120    gpu_metal::moe_block(model, &[job], out)
121}
122
123/// Test-only: q1 matvec_batch — jobs (idx, rows, cols); first two share
124/// x, the third takes xi.
125#[doc(hidden)]
126#[cfg(target_os = "macos")]
127pub fn gpu_batch_q1_for_test(
128    model: &std::sync::Arc<cortiq_core::CmfModel>,
129    shapes: &[(usize, usize, usize)],
130    x: &[f32],
131    xi: &[f32],
132    outs: &mut [&mut [f32]],
133) -> bool {
134    let jobs: Vec<gpu::BatchJob> = shapes
135        .iter()
136        .enumerate()
137        .map(|(k, &(idx, rows, cols))| gpu::BatchJob {
138            idx,
139            rows,
140            cols,
141            row_scale: &[],
142            xs: if k < 2 { x.to_vec() } else { xi.to_vec() },
143            layout: gpu::BatchLayout::Q1,
144        })
145        .collect();
146    gpu_metal::matvec_batch(model, &jobs, outs)
147}
148
149/// Test-only direct handle to the Metal q1 matvec (micro-benchmarks).
150#[doc(hidden)]
151#[cfg(target_os = "macos")]
152pub fn gpu_q1_matvec_for_test(
153    model: &std::sync::Arc<cortiq_core::CmfModel>,
154    idx: usize,
155    xs: &[f32],
156    rows: usize,
157    cols: usize,
158    out: &mut [f32],
159) -> bool {
160    gpu_metal::q1_matvec(model, idx, xs, rows, cols, out)
161}
162pub use sampler::SamplerConfig;