1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! CUDA Scheduler Core (PMAT-802)
//!
//! Core CudaScheduler implementation for direct CUDA execution.
#[cfg(feature = "cuda")]
#[allow(unused_imports)]
use crate::cuda::CudaExecutor;
#[cfg(feature = "cuda")]
#[allow(unused_imports)]
use crate::error::{RealizarError, Result};
// Unlike HybridScheduler, this ALWAYS uses CUDA (even for m=1)
// ============================================================================
/// CUDA-native scheduler for GpuModel inference
///
/// Key difference from HybridScheduler:
/// - ALWAYS uses CudaExecutor for matmul (no CPU fallback for m=1)
/// - Direct CUDA kernel execution instead of trueno wgpu
/// - Eliminates the m=1 CPU restriction that causes 1090x slowdown
///
/// ## Performance Impact
///
/// The HybridScheduler forces CPU for m=1 (single-token generation), which
/// causes token-by-token inference to run on CPU. CudaScheduler eliminates
/// this restriction, enabling full GPU acceleration for generation.
#[cfg(feature = "cuda")]
pub struct CudaScheduler {
executor: crate::cuda::CudaExecutor,
}
#[cfg(feature = "cuda")]
impl CudaScheduler {
/// Create a new CUDA scheduler
///
/// # Errors
///
/// Returns error if CUDA is not available or initialization fails.
pub fn new() -> Result<Self> {
let executor = crate::cuda::CudaExecutor::new(0).map_err(|e| RealizarError::GpuError {
reason: format!("Failed to create CudaExecutor: {}", e),
})?;
Ok(Self { executor })
}
/// Check if CUDA is available
#[must_use]
pub fn has_cuda(&self) -> bool {
true // If we have a CudaScheduler, CUDA is available
}
/// Check if this scheduler uses CUDA for the given matrix dimensions
///
/// Unlike HybridScheduler, CudaScheduler ALWAYS returns true (uses CUDA).
#[must_use]
#[allow(clippy::unused_self)]
pub fn uses_cuda_for(&self, _m: usize, _k: usize, _n: usize) -> bool {
true // Always use CUDA - this is the key difference from HybridScheduler
}
/// Execute matmul using CUDA
///
/// Same interface as HybridScheduler::matmul for drop-in replacement.
///
/// # Errors
///
/// Returns error if CUDA execution fails.
#[allow(clippy::many_single_char_names)]
pub fn matmul(
&mut self,
a: &[f32],
b: &[f32],
m: usize,
k: usize,
n: usize,
) -> Result<Vec<f32>> {
let mut output = vec![0.0f32; m * n];
self.executor
.gemm(a, b, &mut output, m as u32, n as u32, k as u32)
.map_err(|e| RealizarError::GpuError {
reason: format!("CUDA GEMM failed: {}", e),
})?;
Ok(output)
}
/// C[m, n] = A[m, k] @ B^T with B row-major `[n, k]` — a dequantized weight
/// in its native `[out, in]` layout (#3975). Use [`Self::matmul`] only when B
/// really is `[k, n]`.
///
/// # Errors
///
/// Returns error if CUDA execution fails.
pub fn matmul_bt(
&mut self,
a: &[f32],
b: &[f32],
m: usize,
k: usize,
n: usize,
) -> Result<Vec<f32>> {
let mut output = vec![0.0f32; m * n];
self.executor
.gemm_bt(a, b, &mut output, m as u32, n as u32, k as u32)
.map_err(|e| RealizarError::GpuError {
reason: format!("CUDA GEMM (B^T) failed: {}", e),
})?;
Ok(output)
}
/// Get device name
pub fn device_name(&self) -> Result<String> {
self.executor
.device_name()
.map_err(|e| RealizarError::GpuError {
reason: format!("Failed to get device name: {}", e),
})
}
/// Cache a weight matrix on GPU (PARITY-120)
///
/// Weights stay on GPU and are reused for all forward passes.
///
/// # Errors
///
/// Returns error if GPU memory allocation fails.
pub fn cache_weight(&mut self, name: &str, weight: &[f32]) -> Result<()> {
self.executor
.load_weights(name, weight)
.map(|_| ())
.map_err(|e| RealizarError::GpuError {
reason: format!("Failed to cache weight '{}': {}", name, e),
})
}
/// Check if a weight is cached
#[must_use]
pub fn has_cached_weight(&self, name: &str) -> bool {
self.executor.has_weights(name)
}
/// Get number of cached weights
#[must_use]
pub fn cached_weight_count(&self) -> usize {
self.executor.cached_weight_count()
}
/// Execute matmul using cached weight (PARITY-120)
///
/// Uses pre-loaded weight on GPU, only transfers input/output.
/// This is the fast path for single-token generation.
///
/// # Arguments
///
/// * `weight_name` - Name of cached weight (from `cache_weight`)
/// * `x` - Input vector (k elements)
/// * `k` - Input dimension
/// * `n` - Output dimension
///
/// # Errors
///
/// Returns error if weight not cached or CUDA fails.
pub fn matmul_cached(
&mut self,
weight_name: &str,
x: &[f32],
k: usize,
n: usize,
) -> Result<Vec<f32>> {
let mut output = vec![0.0f32; n];
self.executor
.gemv_cached(weight_name, x, &mut output, k as u32, n as u32)
.map_err(|e| RealizarError::GpuError {
reason: format!("CUDA cached GEMV failed: {}", e),
})?;
Ok(output)
}
}
#[cfg(test)]
#[cfg(feature = "cuda")]
mod tests {
use super::*;
#[test]
fn test_cuda_scheduler_new() {
// CudaScheduler::new() should succeed when a CUDA device is present.
// On a GPU-less host it returns Err (CudaNotAvailable) — skip rather
// than fail, so the suite is portable (APR-MONO §S #1981).
match CudaScheduler::new() {
Ok(scheduler) => assert!(scheduler.has_cuda()),
Err(e) => {
eprintln!("SKIP: CUDA scheduler unavailable: {e:?}");
},
}
}
#[test]
fn test_cuda_scheduler_has_cuda() {
let scheduler = crate::cuda_scheduler_or_skip!();
assert!(scheduler.has_cuda());
}
#[test]
fn test_cuda_scheduler_uses_cuda_for_all_dims() {
let scheduler = crate::cuda_scheduler_or_skip!();
// CudaScheduler ALWAYS uses CUDA, unlike HybridScheduler
assert!(scheduler.uses_cuda_for(1, 64, 64)); // m=1 (single token)
assert!(scheduler.uses_cuda_for(8, 256, 256)); // batch
assert!(scheduler.uses_cuda_for(1, 1, 1)); // tiny
assert!(scheduler.uses_cuda_for(1024, 4096, 4096)); // large
}
#[test]
fn test_cuda_scheduler_device_name() {
let scheduler = crate::cuda_scheduler_or_skip!();
let name = scheduler.device_name();
assert!(name.is_ok());
let name = name.expect("name");
// Should contain GPU name
assert!(!name.is_empty());
}
#[test]
fn test_cuda_scheduler_matmul_basic() {
let mut scheduler = crate::cuda_scheduler_or_skip!();
// 2x3 @ 3x2 = 2x2
let a = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let b = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let result = scheduler.matmul(&a, &b, 2, 3, 2);
assert!(result.is_ok());
let output = result.expect("output");
assert_eq!(output.len(), 4);
}
#[test]
fn test_cuda_scheduler_matmul_single_element() {
let mut scheduler = crate::cuda_scheduler_or_skip!();
// 1x1 @ 1x1 = 1x1
let a = vec![3.0];
let b = vec![4.0];
let result = scheduler.matmul(&a, &b, 1, 1, 1);
assert!(result.is_ok());
let output = result.expect("output");
assert_eq!(output.len(), 1);
// 3 * 4 = 12
assert!((output[0] - 12.0).abs() < 0.1);
}
#[test]
fn test_cuda_scheduler_matmul_larger() {
let mut scheduler = crate::cuda_scheduler_or_skip!();
// 4x64 @ 64x32 = 4x32
let a: Vec<f32> = (0..256).map(|i| (i as f32) * 0.01).collect();
let b: Vec<f32> = (0..2048).map(|i| (i as f32) * 0.001).collect();
let result = scheduler.matmul(&a, &b, 4, 64, 32);
assert!(result.is_ok());
let output = result.expect("output");
assert_eq!(output.len(), 128);
}
#[test]
fn test_cuda_scheduler_cache_weight() {
let mut scheduler = crate::cuda_scheduler_or_skip!();
let weight = vec![1.0f32; 256 * 128];
let result = scheduler.cache_weight("test_weight", &weight);
assert!(result.is_ok());
assert!(scheduler.has_cached_weight("test_weight"));
assert!(!scheduler.has_cached_weight("nonexistent"));
}
#[test]
fn test_cuda_scheduler_cached_weight_count() {
let mut scheduler = crate::cuda_scheduler_or_skip!();
let initial_count = scheduler.cached_weight_count();
let weight = vec![1.0f32; 64 * 64];
scheduler
.cache_weight("weight_1", &weight)
.expect("cache_weight");
assert_eq!(scheduler.cached_weight_count(), initial_count + 1);
scheduler
.cache_weight("weight_2", &weight)
.expect("cache_weight");
assert_eq!(scheduler.cached_weight_count(), initial_count + 2);
}
#[test]
fn test_cuda_scheduler_matmul_cached() {
let mut scheduler = crate::cuda_scheduler_or_skip!();
// Cache a 64x32 weight matrix
let weight: Vec<f32> = (0..2048).map(|i| (i as f32) * 0.001).collect();
scheduler
.cache_weight("cached_test", &weight)
.expect("cache_weight");
// GEMV: 1x64 @ 64x32 = 1x32
let input: Vec<f32> = (0..64).map(|i| (i as f32) * 0.1).collect();
let result = scheduler.matmul_cached("cached_test", &input, 64, 32);
assert!(result.is_ok());
let output = result.expect("output");
assert_eq!(output.len(), 32);
}
#[test]
fn test_cuda_scheduler_matmul_identity() {
let mut scheduler = crate::cuda_scheduler_or_skip!();
// Identity matrix test: I @ v = v
// 4x4 identity @ 4x1 = 4x1
#[rustfmt::skip]
let identity = vec![
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
];
let v = vec![1.0, 2.0, 3.0, 4.0];
let result = scheduler.matmul(&identity, &v, 4, 4, 1);
assert!(result.is_ok());
let output = result.expect("output");
for (i, &expected) in v.iter().enumerate() {
assert!(
(output[i] - expected).abs() < 0.01,
"idx={} got={} expected={}",
i,
output[i],
expected
);
}
}
}