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