mlx-native 0.9.6

Pure-Rust Metal GPU compute library for MLX-compatible inference on Apple Silicon
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
//! Dense F16 matrix multiply for the lm_head vocabulary projection.
//!
//! Computes `C = A * B^T` where A is [M, K] f16, B is [N, K] f16,
//! and C is [M, N] f16.
//!
//! Two GPU kernels:
//!
//! - `dense_matvec_f16` — specialised M=1 mat-vec (decode hot path).
//!   Uses vectorised half4 loads + simd_sum, modelled after the llama.cpp
//!   `kernel_mul_mv_f16_f32` pattern.
//!
//! - `dense_gemm_f16` — tiled GEMM for M>1 with simdgroup_matrix MMA.

use metal::MTLSize;

use crate::buffer::MlxBuffer;
use crate::encoder::CommandEncoder;
use crate::error::{MlxError, Result};
use crate::kernel_registry::KernelRegistry;

use super::encode_helpers::{
    as_bytes, encode_threadgroups_with_args, encode_threadgroups_with_args_and_shared, KernelArg,
};

/// MSL source for the dense GEMM kernel (embedded at compile time).
pub static DENSE_GEMM_SHADER_SOURCE: &str = include_str!("../shaders/dense_gemm.metal");

/// Register dense GEMM shader source with the given kernel registry.
pub fn register(registry: &mut KernelRegistry) {
    registry.register_source("dense_gemm_f16", DENSE_GEMM_SHADER_SOURCE);
    registry.register_source("dense_matvec_f16", DENSE_GEMM_SHADER_SOURCE);
    registry.register_source("dense_matvec_f16w_f32io", DENSE_GEMM_SHADER_SOURCE);
    registry.register_source("dense_matvec_bf16w_f32io", DENSE_GEMM_SHADER_SOURCE);
    registry.register_source("dense_matvec_f32", DENSE_GEMM_SHADER_SOURCE);
    registry.register_source(
        "dense_matvec_f32_nsg4_exact_k4096",
        DENSE_GEMM_SHADER_SOURCE,
    );
}

/// MSL-compatible params struct for dense GEMM.
///
/// Must match `DenseGemmParams` in `dense_gemm.metal`.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct GpuDenseGemmParams {
    m: u32,
    n: u32,
    k: u32,
}

/// Parameters for a dense GEMM operation.
pub struct DenseGemmF16Params {
    /// Number of rows in A (and C).
    pub m: u32,
    /// Number of rows in B (columns in C).  C = A * B^T is [M, N].
    pub n: u32,
    /// Inner dimension (columns of A and B).
    pub k: u32,
}

/// Dispatch a dense F16 matrix multiply on the GPU: `C = A * B^T`.
///
/// A is `[M, K]` f16, B is `[N, K]` f16, C is `[M, N]` f16.
///
/// For M=1 (decode path), dispatches the specialised `dense_matvec_f16`
/// kernel which uses vectorised loads + SIMD reduction — typically 10-20x
/// faster than the tiled GEMM for single-row inputs.
///
/// # Arguments
///
/// * `encoder`  - Command encoder to record the dispatch into.
/// * `registry` - Kernel registry (must have `dense_gemm_f16` registered).
/// * `device`   - Metal device for pipeline compilation.
/// * `a`        - Matrix A buffer `[M, K]` (f16).
/// * `b`        - Matrix B buffer `[N, K]` (f16).
/// * `output`   - Output buffer C `[M, N]` (f16).
/// * `params`   - GEMM dimensions.
///
/// # Errors
///
/// Returns `MlxError::InvalidArgument` if dimensions are 0 or buffers are
/// too small.
pub fn dispatch_dense_gemm_f16(
    encoder: &mut CommandEncoder,
    registry: &mut KernelRegistry,
    device: &metal::DeviceRef,
    a: &MlxBuffer,
    b: &MlxBuffer,
    output: &MlxBuffer,
    params: &DenseGemmF16Params,
) -> Result<()> {
    if params.m == 0 || params.n == 0 || params.k == 0 {
        return Err(MlxError::InvalidArgument(
            "dense_gemm_f16: M, N, and K must all be > 0".into(),
        ));
    }

    let a_bytes = params.m as usize * params.k as usize * 2; // f16 = 2 bytes
    if a.byte_len() < a_bytes {
        return Err(MlxError::InvalidArgument(format!(
            "dense_gemm_f16: A buffer too small: need {} bytes, have {}",
            a_bytes,
            a.byte_len()
        )));
    }
    let b_bytes = params.n as usize * params.k as usize * 2;
    if b.byte_len() < b_bytes {
        return Err(MlxError::InvalidArgument(format!(
            "dense_gemm_f16: B buffer too small: need {} bytes, have {}",
            b_bytes,
            b.byte_len()
        )));
    }
    let c_bytes = params.m as usize * params.n as usize * 2;
    if output.byte_len() < c_bytes {
        return Err(MlxError::InvalidArgument(format!(
            "dense_gemm_f16: output buffer too small: need {} bytes, have {}",
            c_bytes,
            output.byte_len()
        )));
    }

    if params.m == 1 {
        dispatch_matvec_f16(encoder, registry, device, a, b, output, params)
    } else {
        dispatch_gemm_tiled_f16(encoder, registry, device, a, b, output, params)
    }
}

/// Specialised M=1 mat-vec kernel dispatch.
///
/// Kernel constants (must match `dense_gemm.metal`):
///   N_DST       = 4  (rows per simdgroup)
///   N_SIMDGROUP = 2  (simdgroups per threadgroup)
///   N_SIMDWIDTH = 32 (Apple SIMD width)
///
/// Dispatch geometry:
///   threadgroups:     (ceil(N / 8), 1, 1)
///   threads_per_tg:   (32, N_SIMDGROUP, 1)   — 32 lanes × 2 simdgroups = 64 threads
fn dispatch_matvec_f16(
    encoder: &mut CommandEncoder,
    registry: &mut KernelRegistry,
    device: &metal::DeviceRef,
    a: &MlxBuffer,
    b: &MlxBuffer,
    output: &MlxBuffer,
    params: &DenseGemmF16Params,
) -> Result<()> {
    let pipeline = registry.get_pipeline("dense_matvec_f16", device)?;

    let gpu_params = GpuDenseGemmParams {
        m: params.m,
        n: params.n,
        k: params.k,
    };

    let n_dst: u64 = 4;
    let n_simdgroup: u64 = 2;
    let rows_per_tg = n_dst * n_simdgroup; // 8

    let threadgroups = MTLSize::new((params.n as u64 + rows_per_tg - 1) / rows_per_tg, 1, 1);
    let threads_per_tg = MTLSize::new(32, n_simdgroup, 1);

    encode_threadgroups_with_args(
        encoder,
        pipeline,
        &[
            (0, KernelArg::Buffer(a)),
            (1, KernelArg::Buffer(b)),
            (2, KernelArg::Buffer(output)),
            (3, KernelArg::Bytes(as_bytes(&gpu_params))),
        ],
        threadgroups,
        threads_per_tg,
    );

    Ok(())
}

/// Dispatch a mixed-precision mat-vec: F32 input × F16 weights → F32 output.
///
/// Eliminates the F32→F16 cast on input and F16→F32 cast on output compared
/// to the pure-F16 path. M must be 1 (decode path only).
///
/// * `a`      - Input buffer `[1, K]` (f32)
/// * `b`      - Weight buffer `[N, K]` (f16)
/// * `output` - Output buffer `[1, N]` (f32)
pub fn dispatch_dense_matvec_f16w_f32io(
    encoder: &mut CommandEncoder,
    registry: &mut KernelRegistry,
    device: &metal::DeviceRef,
    a: &MlxBuffer,
    b: &MlxBuffer,
    output: &MlxBuffer,
    params: &DenseGemmF16Params,
) -> Result<()> {
    if params.m != 1 {
        return Err(MlxError::InvalidArgument(
            "dense_matvec_f16w_f32io: M must be 1 (decode only)".into(),
        ));
    }
    let pipeline = registry.get_pipeline("dense_matvec_f16w_f32io", device)?;

    let gpu_params = GpuDenseGemmParams {
        m: params.m,
        n: params.n,
        k: params.k,
    };

    let n_dst: u64 = 4;
    let n_simdgroup: u64 = 2;
    let rows_per_tg = n_dst * n_simdgroup;

    let threadgroups = MTLSize::new((params.n as u64 + rows_per_tg - 1) / rows_per_tg, 1, 1);
    let threads_per_tg = MTLSize::new(32, n_simdgroup, 1);

    encode_threadgroups_with_args(
        encoder,
        pipeline,
        &[
            (0, KernelArg::Buffer(a)),
            (1, KernelArg::Buffer(b)),
            (2, KernelArg::Buffer(output)),
            (3, KernelArg::Bytes(as_bytes(&gpu_params))),
        ],
        threadgroups,
        threads_per_tg,
    );

    Ok(())
}

/// Dispatch a BF16-weight mat-vec: BF16 weights × F32 input → F32 output.
///
/// Use this for lm_head decode when weights have been cast F32→BF16.
/// Produces numerically identical results to `dense_matmul_bf16_f32_tensor`
/// for M=1 while using the much faster SIMD mat-vec instead of the tiled GEMM.
/// M must be 1.
///
/// * `a`      - Input buffer `[1, K]` (f32)
/// * `b`      - Weight buffer `[N, K]` (bf16)
/// * `output` - Output buffer `[1, N]` (f32)
pub fn dispatch_dense_matvec_bf16w_f32io(
    encoder: &mut CommandEncoder,
    registry: &mut KernelRegistry,
    device: &metal::DeviceRef,
    a: &MlxBuffer,
    b: &MlxBuffer,
    output: &MlxBuffer,
    params: &DenseGemmF16Params,
) -> Result<()> {
    if params.m != 1 {
        return Err(MlxError::InvalidArgument(
            "dense_matvec_bf16w_f32io: M must be 1 (decode only)".into(),
        ));
    }
    let pipeline = registry.get_pipeline("dense_matvec_bf16w_f32io", device)?;

    let gpu_params = GpuDenseGemmParams {
        m: params.m,
        n: params.n,
        k: params.k,
    };

    let n_dst: u64 = 4;
    let n_simdgroup: u64 = 2;
    let rows_per_tg = n_dst * n_simdgroup;

    let threadgroups = MTLSize::new((params.n as u64 + rows_per_tg - 1) / rows_per_tg, 1, 1);
    let threads_per_tg = MTLSize::new(32, n_simdgroup, 1);

    encode_threadgroups_with_args(
        encoder,
        pipeline,
        &[
            (0, KernelArg::Buffer(a)),
            (1, KernelArg::Buffer(b)),
            (2, KernelArg::Buffer(output)),
            (3, KernelArg::Bytes(as_bytes(&gpu_params))),
        ],
        threadgroups,
        threads_per_tg,
    );

    Ok(())
}

/// Dispatch a pure F32 mat-vec: F32 input × F32 weights → F32 output.
///
/// This is the highest-precision M=1 path — no weight casting, no precision
/// loss. Used for lm_head decode where weights are stored as F32. M must be 1.
///
/// * `a`      - Input buffer `[1, K]` (f32)
/// * `b`      - Weight buffer `[N, K]` (f32)
/// * `output` - Output buffer `[1, N]` (f32)
pub fn dispatch_dense_matvec_f32(
    encoder: &mut CommandEncoder,
    registry: &mut KernelRegistry,
    device: &metal::DeviceRef,
    a: &MlxBuffer,
    b: &MlxBuffer,
    output: &MlxBuffer,
    params: &DenseGemmF16Params,
) -> Result<()> {
    if params.m != 1 {
        return Err(MlxError::InvalidArgument(
            "dense_matvec_f32: M must be 1 (decode only)".into(),
        ));
    }
    let gpu_params = GpuDenseGemmParams {
        m: params.m,
        n: params.n,
        k: params.k,
    };

    if params.n == 256 && params.k == 4096 {
        const ROWS_PER_TG: u64 = 2;
        const SIMD_GROUPS: u64 = 4;
        const K_STEPS: u64 = 32;
        const SHARED_BYTES: u64 = ROWS_PER_TG * K_STEPS * 32 * 4;
        let pipeline =
            registry.get_pipeline("dense_matvec_f32_nsg4_exact_k4096", device)?;
        let threadgroups = MTLSize::new(
            (params.n as u64 + ROWS_PER_TG - 1) / ROWS_PER_TG,
            1,
            1,
        );
        let threads_per_tg = MTLSize::new(32, SIMD_GROUPS, 1);
        encode_threadgroups_with_args_and_shared(
            encoder,
            pipeline,
            &[
                (0, KernelArg::Buffer(a)),
                (1, KernelArg::Buffer(b)),
                (2, KernelArg::Buffer(output)),
                (3, KernelArg::Bytes(as_bytes(&gpu_params))),
            ],
            &[(0, SHARED_BYTES)],
            threadgroups,
            threads_per_tg,
        );
    } else {
        let pipeline = registry.get_pipeline("dense_matvec_f32", device)?;
        const ROWS_PER_TG: u64 = 8;
        const SIMD_GROUPS: u64 = 2;
        let threadgroups = MTLSize::new(
            (params.n as u64 + ROWS_PER_TG - 1) / ROWS_PER_TG,
            1,
            1,
        );
        let threads_per_tg = MTLSize::new(32, SIMD_GROUPS, 1);
        encode_threadgroups_with_args(
            encoder,
            pipeline,
            &[
                (0, KernelArg::Buffer(a)),
                (1, KernelArg::Buffer(b)),
                (2, KernelArg::Buffer(output)),
                (3, KernelArg::Bytes(as_bytes(&gpu_params))),
            ],
            threadgroups,
            threads_per_tg,
        );
    }

    Ok(())
}

/// Tiled GEMM dispatch for M>1 using simdgroup_matrix MMA.
///
/// Tile: BM=32, BN=32, BK=16, WM=2, WN=2 → 128 threads per threadgroup.
fn dispatch_gemm_tiled_f16(
    encoder: &mut CommandEncoder,
    registry: &mut KernelRegistry,
    device: &metal::DeviceRef,
    a: &MlxBuffer,
    b: &MlxBuffer,
    output: &MlxBuffer,
    params: &DenseGemmF16Params,
) -> Result<()> {
    let pipeline = registry.get_pipeline("dense_gemm_f16", device)?;

    let gpu_params = GpuDenseGemmParams {
        m: params.m,
        n: params.n,
        k: params.k,
    };

    let bm: u64 = 32;
    let bn: u64 = 32;
    let tgp_size: u64 = 128; // WM * WN * 32 = 2*2*32

    let threadgroups = MTLSize::new(
        (params.n as u64 + bn - 1) / bn,
        (params.m as u64 + bm - 1) / bm,
        1,
    );
    let threads_per_tg = MTLSize::new(tgp_size, 1, 1);

    encode_threadgroups_with_args(
        encoder,
        pipeline,
        &[
            (0, KernelArg::Buffer(a)),
            (1, KernelArg::Buffer(b)),
            (2, KernelArg::Buffer(output)),
            (3, KernelArg::Bytes(as_bytes(&gpu_params))),
        ],
        threadgroups,
        threads_per_tg,
    );

    Ok(())
}