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