boostr 0.1.0

ML framework built on numr - attention, quantization, model architectures
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! impl QuantMatmulOps<CudaRuntime> for CudaClient

use crate::error::{Error, Result};
use crate::quant::traits::QuantMatmulOps;
use crate::quant::{QuantFormat, QuantTensor};
use cudarc::driver::PushKernelArg;
use cudarc::driver::safe::LaunchConfig;
use numr::dtype::DType;
use numr::runtime::Device;
use numr::runtime::cuda::{CudaClient, CudaRuntime};
use numr::tensor::Tensor;

use super::super::int4_gemm as int4_dispatch;
use super::super::kernels::{
    self, GEMV_Q2_K_MODULE, GEMV_Q3_K_MODULE, GEMV_Q5_K_MODULE, QUANT_GEMV_MODULE,
};
use super::fallback::{quant_matmul_via_dequant, quant_swiglu_via_dequant};
use super::format_dispatch::{dispatch_gemv, dispatch_matmul};
use super::helpers::{quantize_activation_q8_1, validate_input_cuda};

impl QuantMatmulOps<CudaRuntime> for CudaClient {
    fn int4_gemm(
        &self,
        input: &Tensor<CudaRuntime>,
        qweight: &Tensor<CudaRuntime>,
        scales: &Tensor<CudaRuntime>,
        zeros: &Tensor<CudaRuntime>,
        group_size: usize,
    ) -> Result<Tensor<CudaRuntime>> {
        let (m, k) = validate_input_cuda(input)?;
        let n = qweight.shape()[1] * 8;
        let act_contig = input.contiguous();

        let mut out_shape = input.shape()[..input.shape().len() - 1].to_vec();
        out_shape.push(n);
        let output = Tensor::<CudaRuntime>::empty(&out_shape, DType::F32, input.device());
        int4_dispatch::launch_int4_gemm(
            self,
            &act_contig,
            qweight,
            scales,
            zeros,
            &output,
            m as u32,
            k as u32,
            n as u32,
            group_size as u32,
        )?;
        Ok(output)
    }

    fn int4_gemm_gptq(
        &self,
        input: &Tensor<CudaRuntime>,
        qweight: &Tensor<CudaRuntime>,
        qzeros: &Tensor<CudaRuntime>,
        scales: &Tensor<CudaRuntime>,
        g_idx: &Tensor<CudaRuntime>,
    ) -> Result<Tensor<CudaRuntime>> {
        let (m, k) = validate_input_cuda(input)?;
        let n = qweight.shape()[1];
        let act_contig = input.contiguous();

        let mut out_shape = input.shape()[..input.shape().len() - 1].to_vec();
        out_shape.push(n);
        let output = Tensor::<CudaRuntime>::empty(&out_shape, DType::F32, input.device());
        int4_dispatch::launch_int4_gemm_gptq(
            self,
            &act_contig,
            qweight,
            qzeros,
            scales,
            g_idx,
            &output,
            m as u32,
            k as u32,
            n as u32,
        )?;
        Ok(output)
    }

    fn marlin_gemm(
        &self,
        input: &Tensor<CudaRuntime>,
        weight: &Tensor<CudaRuntime>,
        scales: &Tensor<CudaRuntime>,
        zeros: &Tensor<CudaRuntime>,
        group_size: usize,
    ) -> Result<Tensor<CudaRuntime>> {
        let (m, k) = validate_input_cuda(input)?;
        let n = weight.shape()[1];
        let act_contig = input.contiguous();

        let mut out_shape = input.shape()[..input.shape().len() - 1].to_vec();
        out_shape.push(n);
        let output = Tensor::<CudaRuntime>::empty(&out_shape, DType::F32, input.device());
        int4_dispatch::launch_marlin_gemm(
            self,
            &act_contig,
            weight,
            scales,
            zeros,
            &output,
            m as u32,
            k as u32,
            n as u32,
            group_size as u32,
        )?;
        Ok(output)
    }

    fn quant_matmul(
        &self,
        activation: &Tensor<CudaRuntime>,
        weight: &QuantTensor<CudaRuntime>,
    ) -> Result<Tensor<CudaRuntime>> {
        if activation.dtype() != DType::F32 {
            return Err(Error::QuantError {
                reason: format!(
                    "quant_matmul activation must be F32, got {:?}",
                    activation.dtype()
                ),
            });
        }

        let w_shape = weight.shape();
        if w_shape.len() != 2 {
            return Err(Error::QuantError {
                reason: format!("quant_matmul weight must be 2D [N, K], got {:?}", w_shape),
            });
        }
        let n = w_shape[0];
        let k = w_shape[1];

        let a_shape = activation.shape();
        if a_shape.is_empty() {
            return Err(Error::QuantError {
                reason: "quant_matmul activation must be at least 1D".into(),
            });
        }
        let a_k = a_shape[a_shape.len() - 1];
        if a_k != k {
            return Err(Error::QuantError {
                reason: format!(
                    "quant_matmul dimension mismatch: activation K={}, weight K={}",
                    a_k, k
                ),
            });
        }

        let m = a_shape.iter().product::<usize>() / k;
        let act_contig = activation.contiguous();

        let mut out_shape = a_shape[..a_shape.len() - 1].to_vec();
        out_shape.push(n);
        let output = Tensor::<CudaRuntime>::empty(&out_shape, DType::F32, activation.device());
        let output_ptr = output.ptr();

        if m <= 64 {
            match dispatch_gemv(self, &act_contig, weight, output_ptr, m, k, n)? {
                Some(()) => {}
                None => return quant_matmul_via_dequant(self, activation, weight),
            }
        } else {
            match dispatch_matmul(self, &act_contig, weight, output_ptr, m, k, n)? {
                Some(()) => {}
                None => return quant_matmul_via_dequant(self, activation, weight),
            }
        }

        Ok(output)
    }

    fn quant_matmul_batch(
        &self,
        activation: &Tensor<CudaRuntime>,
        weights: &[&QuantTensor<CudaRuntime>],
    ) -> Result<Vec<Tensor<CudaRuntime>>> {
        if weights.is_empty() {
            return Ok(vec![]);
        }

        if activation.dtype() != DType::F32 {
            return Err(Error::QuantError {
                reason: format!(
                    "quant_matmul_batch activation must be F32, got {:?}",
                    activation.dtype()
                ),
            });
        }

        let a_shape = activation.shape();
        if a_shape.is_empty() {
            return Err(Error::QuantError {
                reason: "quant_matmul_batch activation must be at least 1D".into(),
            });
        }
        let k = a_shape[a_shape.len() - 1];
        let m = a_shape.iter().product::<usize>() / k;
        let act_contig = activation.contiguous();

        // Check if all weights support dp4a (Q4_K, Q6_K, Q8_0, Q5_K, Q3_K, Q2_K)
        let all_dp4a = weights.iter().all(|w| {
            matches!(
                w.format(),
                QuantFormat::Q4K
                    | QuantFormat::Q6K
                    | QuantFormat::Q8_0
                    | QuantFormat::Q5K
                    | QuantFormat::Q3K
                    | QuantFormat::Q2K
            )
        });
        let use_dp4a = all_dp4a && m <= 4 && k % 32 == 0;

        if use_dp4a {
            // Quantize activation to Q8_1 ONCE, reuse for all weights
            let q8_buf = quantize_activation_q8_1(self, &act_contig, m, k)?;
            let q8_ptr = q8_buf.ptr();
            let device_index = activation.device().id();

            let m_u32 = m as u32;
            let k_u32 = k as u32;

            // Pre-load modules for all formats that might appear
            let module_main =
                kernels::get_or_load_module(self.context(), device_index, QUANT_GEMV_MODULE)?;
            let func_q4k = kernels::get_kernel_function(&module_main, "quant_gemv_q4_k_q8_1_mwr")?;
            let func_q6k = kernels::get_kernel_function(&module_main, "quant_gemv_q6_k_q8_1_mwr")?;
            let func_q8_0 = kernels::get_kernel_function(&module_main, "quant_gemv_q8_0_q8_1_mwr")?;

            // Lazily load per-format modules only if needed
            let has_q5k = weights.iter().any(|w| w.format() == QuantFormat::Q5K);
            let has_q3k = weights.iter().any(|w| w.format() == QuantFormat::Q3K);
            let has_q2k = weights.iter().any(|w| w.format() == QuantFormat::Q2K);

            let func_q5k = if has_q5k {
                let m =
                    kernels::get_or_load_module(self.context(), device_index, GEMV_Q5_K_MODULE)?;
                Some(kernels::get_kernel_function(
                    &m,
                    "quant_gemv_q5_k_q8_1_mwr",
                )?)
            } else {
                None
            };
            let func_q3k = if has_q3k {
                let m =
                    kernels::get_or_load_module(self.context(), device_index, GEMV_Q3_K_MODULE)?;
                Some(kernels::get_kernel_function(
                    &m,
                    "quant_gemv_q3_k_q8_1_mwr",
                )?)
            } else {
                None
            };
            let func_q2k = if has_q2k {
                let m =
                    kernels::get_or_load_module(self.context(), device_index, GEMV_Q2_K_MODULE)?;
                Some(kernels::get_kernel_function(
                    &m,
                    "quant_gemv_q2_k_q8_1_mwr",
                )?)
            } else {
                None
            };

            let mut results = Vec::with_capacity(weights.len());
            for w in weights {
                let w_shape = w.shape();
                if w_shape.len() != 2 || w_shape[1] != k {
                    return Err(Error::QuantError {
                        reason: format!(
                            "quant_matmul_batch weight shape mismatch: {:?}, expected [N, {}]",
                            w_shape, k
                        ),
                    });
                }
                let n = w_shape[0];
                let n_u32 = n as u32;

                let func = match w.format() {
                    QuantFormat::Q4K => &func_q4k,
                    QuantFormat::Q6K => &func_q6k,
                    QuantFormat::Q8_0 => &func_q8_0,
                    QuantFormat::Q5K => func_q5k.as_ref().ok_or_else(|| Error::QuantError {
                        reason: "Q5K GEMV module failed to load".into(),
                    })?,
                    QuantFormat::Q3K => func_q3k.as_ref().ok_or_else(|| Error::QuantError {
                        reason: "Q3K GEMV module failed to load".into(),
                    })?,
                    QuantFormat::Q2K => func_q2k.as_ref().ok_or_else(|| Error::QuantError {
                        reason: "Q2K GEMV module failed to load".into(),
                    })?,
                    _ => unreachable!(),
                };

                let mut out_shape = a_shape[..a_shape.len() - 1].to_vec();
                out_shape.push(n);
                let output =
                    Tensor::<CudaRuntime>::empty(&out_shape, DType::F32, activation.device());
                let output_ptr = output.ptr();
                let weight_ptr = w.storage().ptr();

                // MWR: one output column per block, 128 threads (4 warps)
                let cfg = LaunchConfig {
                    grid_dim: (n_u32, m_u32, 1),
                    block_dim: (128, 1, 1),
                    shared_mem_bytes: 0,
                };

                unsafe {
                    let mut builder = self.stream().launch_builder(func);
                    builder.arg(&q8_ptr);
                    builder.arg(&weight_ptr);
                    builder.arg(&output_ptr);
                    builder.arg(&m_u32);
                    builder.arg(&k_u32);
                    builder.arg(&n_u32);
                    builder.launch(cfg).map_err(|e| Error::QuantError {
                        reason: format!("CUDA dp4a mr batch launch failed: {:?}", e),
                    })?;
                }

                results.push(output);
            }
            Ok(results)
        } else {
            // Fallback: call quant_matmul individually
            weights
                .iter()
                .map(|w| self.quant_matmul(activation, w))
                .collect()
        }
    }

    fn quant_swiglu(
        &self,
        activation: &Tensor<CudaRuntime>,
        gate_weight: &QuantTensor<CudaRuntime>,
        up_weight: &QuantTensor<CudaRuntime>,
    ) -> Result<Tensor<CudaRuntime>> {
        let (m, k) = validate_input_cuda(activation)?;
        let n = gate_weight.shape()[0];
        let device_index = activation.device().id();

        if up_weight.shape()[0] != n || up_weight.shape()[1] != k {
            return Err(Error::QuantError {
                reason: format!(
                    "gate_weight shape {:?} vs up_weight shape {:?}",
                    gate_weight.shape(),
                    up_weight.shape()
                ),
            });
        }
        if gate_weight.format() != up_weight.format() {
            return Err(Error::QuantError {
                reason: format!(
                    "gate format {:?} != up format {:?}",
                    gate_weight.format(),
                    up_weight.format()
                ),
            });
        }

        let act_contig = activation.contiguous();
        let a_shape = activation.shape();
        let mut out_shape = a_shape[..a_shape.len() - 1].to_vec();
        out_shape.push(n);
        let output = Tensor::<CudaRuntime>::empty(&out_shape, DType::F32, activation.device());
        let output_ptr = output.ptr();
        let m_u32 = m as u32;
        let k_u32 = k as u32;
        let n_u32 = n as u32;

        // Use fused kernel for GEMV path (decode + short prefill)
        let use_fused = m <= 64
            && matches!(
                gate_weight.format(),
                QuantFormat::Q4K
                    | QuantFormat::Q6K
                    | QuantFormat::Q8_0
                    | QuantFormat::Q5K
                    | QuantFormat::Q3K
                    | QuantFormat::Q2K
            )
            && k % 32 == 0;

        if use_fused {
            let q8_buf = quantize_activation_q8_1(self, &act_contig, m, k)?;
            let q8_ptr = q8_buf.ptr();
            let gate_ptr = gate_weight.storage().ptr();
            let up_ptr = up_weight.storage().ptr();

            let (kernel_name, module_name) = match gate_weight.format() {
                QuantFormat::Q4K => ("fused_swiglu_q4k_q8_1_mwr", QUANT_GEMV_MODULE),
                QuantFormat::Q6K => ("fused_swiglu_q6k_q8_1_mwr", QUANT_GEMV_MODULE),
                QuantFormat::Q8_0 => ("fused_swiglu_q8_0_q8_1_mwr", QUANT_GEMV_MODULE),
                QuantFormat::Q5K => ("fused_swiglu_q5k_q8_1_mwr", GEMV_Q5_K_MODULE),
                QuantFormat::Q3K => ("fused_swiglu_q3k_q8_1_mwr", GEMV_Q3_K_MODULE),
                QuantFormat::Q2K => ("fused_swiglu_q2k_q8_1_mwr", GEMV_Q2_K_MODULE),
                _ => unreachable!(),
            };

            let cfg = LaunchConfig {
                grid_dim: (n_u32, m_u32, 1),
                block_dim: (128, 1, 1),
                shared_mem_bytes: 0,
            };

            let module = kernels::get_or_load_module(self.context(), device_index, module_name)?;
            let func = kernels::get_kernel_function(&module, kernel_name)?;

            unsafe {
                let mut builder = self.stream().launch_builder(&func);
                builder.arg(&q8_ptr);
                builder.arg(&gate_ptr);
                builder.arg(&up_ptr);
                builder.arg(&output_ptr);
                builder.arg(&m_u32);
                builder.arg(&k_u32);
                builder.arg(&n_u32);
                builder.launch(cfg).map_err(|e| Error::QuantError {
                    reason: format!("CUDA {} launch failed: {:?}", kernel_name, e),
                })?;
            }

            Ok(output)
        } else if m <= 64 {
            // Generic fused SwiGLU: gate+up matmul + silu in one kernel
            quant_swiglu_via_dequant(self, &act_contig, gate_weight, up_weight, &output, m, k, n)
                .map(|_| output)
        } else {
            // Large batch: separate matmuls + fused silu_mul
            let gate = self.quant_matmul(activation, gate_weight)?;
            let up = self.quant_matmul(activation, up_weight)?;
            use numr::ops::ActivationOps;
            self.silu_mul(&gate, &up).map_err(Error::Numr)
        }
    }
}