Skip to main content

cortiq_engine/
lib.rs

1//! Cortiq inference engine — sparse forward pass, attention, tokenization, sampling.
2
3pub mod attention;
4pub mod dit;
5pub mod fcd;
6pub mod fcd_ops;
7pub mod gptq_capture;
8pub mod gpu;
9#[cfg(target_os = "macos")]
10pub mod gpu_metal;
11#[cfg(feature = "gpu")]
12pub mod gpu_wgpu;
13pub mod imagegen;
14pub mod inference;
15pub mod kv_cache;
16pub mod linear_core;
17pub mod loader;
18pub mod nystrom;
19pub mod pipeline;
20pub mod pool;
21pub mod qtensor;
22pub mod router;
23pub mod runtime;
24pub mod sampler;
25pub mod skillbake;
26pub mod swarm;
27pub mod textenc;
28pub mod tokenizer;
29pub mod vae;
30
31pub use nystrom::NystromState;
32pub use pipeline::{GenerateResult, Pipeline, TokenCallback, TokenTrace};
33pub use runtime::CortiqRuntime;
34
35/// Test-only: N empty Metal command-buffer round trips, total seconds.
36#[doc(hidden)]
37#[cfg(target_os = "macos")]
38pub fn gpu_empty_submit_for_test(n: usize) -> f64 {
39    gpu_metal::empty_submit_bench(n)
40}
41
42/// Test-only: N pipelined empty submits, one final wait.
43#[doc(hidden)]
44#[cfg(target_os = "macos")]
45pub fn gpu_pipelined_submit_for_test(n: usize) -> f64 {
46    gpu_metal::pipelined_submit_bench(n)
47}
48
49/// Test-only: build a q1 MoeJob trio (weight 1.0).
50#[doc(hidden)]
51#[cfg(target_os = "macos")]
52pub fn gpu_moe_job_for_test(
53    gi: usize,
54    ui: usize,
55    di: usize,
56    inter: usize,
57    hidden: usize,
58    x: Vec<f32>,
59) -> gpu::MoeJob<'static> {
60    gpu::MoeJob {
61        gate: (gi, inter, hidden, &[]),
62        up: (ui, inter, hidden, &[]),
63        down: (di, hidden, inter, &[]),
64        xs_gate: x.clone(),
65        xs_up: x,
66        down_col: &[],
67        w: 1.0,
68        q1: true,
69    }
70}
71
72/// Test-only: run the metal moe_block on one job.
73#[doc(hidden)]
74#[cfg(target_os = "macos")]
75pub fn gpu_moe_block_for_test(
76    model: &std::sync::Arc<cortiq_core::CmfModel>,
77    job: gpu::MoeJob<'_>,
78    out: &mut [f32],
79) -> bool {
80    gpu_metal::moe_block(model, &[job], out)
81}
82
83/// Test-only: q1 matvec_batch — jobs (idx, rows, cols); first two share
84/// x, the third takes xi.
85#[doc(hidden)]
86#[cfg(target_os = "macos")]
87pub fn gpu_batch_q1_for_test(
88    model: &std::sync::Arc<cortiq_core::CmfModel>,
89    shapes: &[(usize, usize, usize)],
90    x: &[f32],
91    xi: &[f32],
92    outs: &mut [&mut [f32]],
93) -> bool {
94    let jobs: Vec<gpu::BatchJob> = shapes
95        .iter()
96        .enumerate()
97        .map(|(k, &(idx, rows, cols))| gpu::BatchJob {
98            idx,
99            rows,
100            cols,
101            row_scale: &[],
102            xs: if k < 2 { x.to_vec() } else { xi.to_vec() },
103            q1: true,
104        })
105        .collect();
106    gpu_metal::matvec_batch(model, &jobs, outs)
107}
108
109/// Test-only direct handle to the Metal q1 matvec (micro-benchmarks).
110#[doc(hidden)]
111#[cfg(target_os = "macos")]
112pub fn gpu_q1_matvec_for_test(
113    model: &std::sync::Arc<cortiq_core::CmfModel>,
114    idx: usize,
115    xs: &[f32],
116    rows: usize,
117    cols: usize,
118    out: &mut [f32],
119) -> bool {
120    gpu_metal::q1_matvec(model, idx, xs, rows, cols, out)
121}
122pub use sampler::SamplerConfig;