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