metrale_gpu_runtime/
lib.rs1#![deny(warnings)]
12#![deny(clippy::all)]
13
14pub mod buffers;
15#[cfg(feature = "cuda")]
16pub mod cublaslt;
17#[cfg(not(feature = "cuda"))]
21#[path = "cublaslt_metal_stub.rs"]
22pub mod cublaslt;
23#[cfg(feature = "cuda")]
24pub mod cuda_backend;
25#[cfg(feature = "cuda")]
26pub mod cutlass;
27#[cfg(not(feature = "cuda"))]
28#[path = "cutlass_metal_stub.rs"]
29pub mod cutlass;
30#[cfg(feature = "cuda")]
31pub mod flashinfer;
32#[cfg(not(feature = "cuda"))]
33#[path = "flashinfer_metal_stub.rs"]
34pub mod flashinfer;
35pub mod gpu;
36#[path = "gpu_args.rs"]
37mod gpu_args;
38pub mod host_heap;
39pub mod kernel_args;
40#[cfg(feature = "metal")]
41pub mod metal_backend;
42pub mod op_cache;
43pub mod pinned_hosts;
44pub mod timing;
45
46pub fn ssm_tail_boundary(total_tokens: usize, block_size: usize) -> Option<usize> {
54 if block_size == 0 || total_tokens <= block_size {
55 return None;
56 }
57 let boundary = ((total_tokens - 1) / block_size) * block_size;
58 (boundary > 0).then_some(boundary)
59}
60
61pub fn ssm_tail_ckpt_enabled() -> bool {
68 static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
69 *ON.get_or_init(|| matches!(std::env::var("METRALE_SSM_TAIL_CKPT").as_deref(), Ok("1")))
70}
71
72pub fn set_hermetic(on: bool) {
76 if on {
77 let _ = HERMETIC.set(true);
78 }
79}
80
81static HERMETIC: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
82
83pub fn hermetic_enabled() -> bool {
87 static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
88 *ON.get_or_init(|| {
89 HERMETIC
90 .get()
91 .copied()
92 .unwrap_or_else(|| matches!(std::env::var("METRALE_HERMETIC").as_deref(), Ok("1")))
93 })
94}
95
96pub fn set_ssm_tail_midchunk(on: Option<bool>) {
101 if let Some(on) = on {
102 let _ = SSM_TAIL_MIDCHUNK.set(on);
103 }
104}
105
106static SSM_TAIL_MIDCHUNK: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
107
108pub fn ssm_tail_midchunk_enabled() -> bool {
114 *SSM_TAIL_MIDCHUNK.get_or_init(|| {
115 !matches!(
116 std::env::var("METRALE_SSM_TAIL_MIDCHUNK").as_deref(),
117 Ok("0")
118 )
119 })
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 #[test]
127 fn an_absent_flag_does_not_seal_the_midchunk_cell() {
128 for _ in 0..3 {
132 set_ssm_tail_midchunk(None);
133 }
134 set_ssm_tail_midchunk(Some(false));
135 assert!(
136 !ssm_tail_midchunk_enabled(),
137 "an absent flag must leave the cell open for the next writer"
138 );
139 set_ssm_tail_midchunk(Some(true));
140 assert!(!ssm_tail_midchunk_enabled(), "and a SET one is final");
141 }
142
143 #[test]
144 fn the_tail_boundary_is_the_last_block_strictly_below_the_prompt() {
145 assert_eq!(ssm_tail_boundary(0, 16), None);
148 assert_eq!(ssm_tail_boundary(16, 16), None, "not the prompt's own end");
149 assert_eq!(ssm_tail_boundary(17, 16), Some(16));
150 assert_eq!(ssm_tail_boundary(32, 16), Some(16), "strictly below");
151 assert_eq!(ssm_tail_boundary(33, 16), Some(32));
152 assert_eq!(ssm_tail_boundary(100, 0), None, "no division by zero");
153 }
154}
155
156#[cfg(feature = "cuda")]
157pub mod cuda_host;
158#[cfg(feature = "cuda")]
159pub mod kernel;
160pub mod mlx_int8;
161#[cfg(feature = "cuda")]
162pub mod registry;
163#[cfg(feature = "cuda")]
164pub mod stream;