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