mistralrs-core 0.8.1

Fast, flexible LLM inference.
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// GDN (Gated Delta Net) Metal kernels for mistral.rs
// Ported from CUDA kernels in cuda/gdn.cu

#include <metal_stdlib>
using namespace metal;

// ============================================================================
// Kernel 1: gated_delta_rule_recurrence
//
// V-tiled recurrence with compile-time K dimension for register residency.
// Grid: (ceil(V/BV), B*H), Block: (BV,). Each thread owns BK registers of
// state. Threadgroup memory holds k_buf and q_buf (2*BK floats).
//
// q,k: [BH, S, K]  v: [BH, S, V]  g,beta: [BH, S]
// state: [BH, K, V] (in/out)  output: [BH, S, V]
// ============================================================================

template <int BK, int BV>
[[kernel]] void gated_delta_rule_kernel(
    const device float *q [[buffer(0)]], const device float *k [[buffer(1)]],
    const device float *v [[buffer(2)]], const device float *g [[buffer(3)]],
    const device float *beta [[buffer(4)]], device float *state [[buffer(5)]],
    device float *output [[buffer(6)]], constant int &seq_len [[buffer(7)]],
    constant int &v_dim [[buffer(8)]],
    uint2 tgpig [[threadgroup_position_in_grid]],
    uint tid [[thread_index_in_threadgroup]]) {
  const int v_tile = tgpig.x;
  const int bh = tgpig.y;
  const int v_idx = v_tile * BV + (int)tid;

  if (v_idx >= v_dim)
    return;

  const device float *q_bh = q + bh * seq_len * BK;
  const device float *k_bh = k + bh * seq_len * BK;
  const device float *v_bh = v + bh * seq_len * v_dim;
  const device float *g_bh = g + bh * seq_len;
  const device float *beta_bh = beta + bh * seq_len;
  device float *state_bh = state + bh * BK * v_dim;
  device float *out_bh = output + bh * seq_len * v_dim;

  threadgroup float k_buf[BK];
  threadgroup float q_buf[BK];

  // Load state column into registers
  float s[BK];
  for (int j = 0; j < BK; j++) {
    s[j] = state_bh[j * v_dim + v_idx];
  }

  for (int t = 0; t < seq_len; t++) {
    // Collaboratively load k_t into threadgroup memory
    for (int j = (int)tid; j < BK; j += BV) {
      k_buf[j] = k_bh[t * BK + j];
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    float decay = exp(g_bh[t]);
    float beta_t = beta_bh[t];
    float v_t = v_bh[t * v_dim + v_idx];

    // Fused pass 1: decay state + compute kv_mem
    float kv_mem = 0.0f;
    for (int j = 0; j < BK; j++) {
      s[j] *= decay;
      kv_mem = fma(s[j], k_buf[j], kv_mem);
    }

    float delta = (v_t - kv_mem) * beta_t;

    // Collaboratively load q_t into threadgroup memory
    for (int j = (int)tid; j < BK; j += BV) {
      q_buf[j] = q_bh[t * BK + j];
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // Fused pass 2: update state + compute output
    float y_t = 0.0f;
    for (int j = 0; j < BK; j++) {
      s[j] = fma(k_buf[j], delta, s[j]);
      y_t = fma(s[j], q_buf[j], y_t);
    }

    out_bh[t * v_dim + v_idx] = y_t;

    threadgroup_barrier(mem_flags::mem_threadgroup);
  }

  // Write state back
  for (int j = 0; j < BK; j++) {
    state_bh[j * v_dim + v_idx] = s[j];
  }
}

// Fallback kernel: runtime k_dim, still V-tiled
template <int BV, int MAX_K>
[[kernel]] void gated_delta_rule_kernel_fallback(
    const device float *q [[buffer(0)]], const device float *k [[buffer(1)]],
    const device float *v [[buffer(2)]], const device float *g [[buffer(3)]],
    const device float *beta [[buffer(4)]], device float *state [[buffer(5)]],
    device float *output [[buffer(6)]], constant int &seq_len [[buffer(7)]],
    constant int &k_dim [[buffer(8)]], constant int &v_dim [[buffer(9)]],
    threadgroup float *shared_mem [[threadgroup(0)]],
    uint2 tgpig [[threadgroup_position_in_grid]],
    uint tid [[thread_index_in_threadgroup]]) {
  const int v_tile = tgpig.x;
  const int bh = tgpig.y;
  const int v_idx = v_tile * BV + (int)tid;

  if (v_idx >= v_dim)
    return;

  const device float *q_bh = q + bh * seq_len * k_dim;
  const device float *k_bh = k + bh * seq_len * k_dim;
  const device float *v_bh = v + bh * seq_len * v_dim;
  const device float *g_bh = g + bh * seq_len;
  const device float *beta_bh = beta + bh * seq_len;
  device float *state_bh = state + bh * k_dim * v_dim;
  device float *out_bh = output + bh * seq_len * v_dim;

  threadgroup float *k_buf = shared_mem;
  threadgroup float *q_buf = shared_mem + k_dim;

  float s[MAX_K];
  for (int j = 0; j < k_dim; j++) {
    s[j] = state_bh[j * v_dim + v_idx];
  }

  for (int t = 0; t < seq_len; t++) {
    for (int j = (int)tid; j < k_dim; j += BV) {
      k_buf[j] = k_bh[t * k_dim + j];
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    float decay = exp(g_bh[t]);
    float beta_t = beta_bh[t];
    float v_t = v_bh[t * v_dim + v_idx];

    float kv_mem = 0.0f;
    for (int j = 0; j < k_dim; j++) {
      s[j] *= decay;
      kv_mem = fma(s[j], k_buf[j], kv_mem);
    }

    float delta = (v_t - kv_mem) * beta_t;

    for (int j = (int)tid; j < k_dim; j += BV) {
      q_buf[j] = q_bh[t * k_dim + j];
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    float y_t = 0.0f;
    for (int j = 0; j < k_dim; j++) {
      s[j] = fma(k_buf[j], delta, s[j]);
      y_t = fma(s[j], q_buf[j], y_t);
    }

    out_bh[t * v_dim + v_idx] = y_t;

    threadgroup_barrier(mem_flags::mem_threadgroup);
  }

  for (int j = 0; j < k_dim; j++) {
    state_bh[j * v_dim + v_idx] = s[j];
  }
}

// Explicit instantiations
template [[host_name("gated_delta_rule_128_64")]] [[kernel]]
void gated_delta_rule_kernel<128, 64>(
    const device float *, const device float *, const device float *,
    const device float *, const device float *, device float *, device float *,
    constant int &, constant int &, uint2, uint);

template [[host_name("gated_delta_rule_64_64")]] [[kernel]]
void gated_delta_rule_kernel<64, 64>(const device float *, const device float *,
                                     const device float *, const device float *,
                                     const device float *, device float *,
                                     device float *, constant int &,
                                     constant int &, uint2, uint);

template [[host_name("gated_delta_rule_fallback")]] [[kernel]]
void gated_delta_rule_kernel_fallback<64, 256>(
    const device float *, const device float *, const device float *,
    const device float *, const device float *, device float *, device float *,
    constant int &, constant int &, constant int &, threadgroup float *, uint2,
    uint);

// ============================================================================
// Kernel 1b: chunked_gated_delta_rule_recurrence (prefill optimization)
//
// Processes prefill tokens in BT-token chunks instead of one at a time.
// Same thread model as Kernel 1: one block per (v_tile, batch*head),
// one thread per V-column. Each thread owns BK registers of state.
//
// Uses BT=32 on Metal to fit within 32KB threadgroup memory limit.
//
// q,k: [BH, S, K]  v: [BH, S, V]  g,beta: [BH, S]
// state: [BH, K, V] (in/out)  output: [BH, S, V]
// ============================================================================

template <int BT, int BK, int BV>
[[kernel]] void chunked_gated_delta_rule_kernel(
    const device float *q [[buffer(0)]], const device float *k [[buffer(1)]],
    const device float *v [[buffer(2)]], const device float *g [[buffer(3)]],
    const device float *beta [[buffer(4)]], device float *state [[buffer(5)]],
    device float *output [[buffer(6)]], constant int &seq_len [[buffer(7)]],
    constant int &v_dim [[buffer(8)]],
    uint2 tgpig [[threadgroup_position_in_grid]],
    uint tid [[thread_index_in_threadgroup]]) {
  const int v_tile = tgpig.x;
  const int bh = tgpig.y;
  const int v_idx = v_tile * BV + (int)tid;

  if (v_idx >= v_dim)
    return;

  const int num_chunks = (seq_len + BT - 1) / BT;

  const device float *q_bh = q + bh * seq_len * BK;
  const device float *k_bh = k + bh * seq_len * BK;
  const device float *v_bh = v + bh * seq_len * v_dim;
  const device float *g_bh = g + bh * seq_len;
  const device float *beta_bh = beta + bh * seq_len;
  device float *state_bh = state + bh * BK * v_dim;
  device float *out_bh = output + bh * seq_len * v_dim;

  // Threadgroup memory
  threadgroup float k_chunk[BT * BK];
  threadgroup float kk_dot[BT * BT];
  threadgroup float gcum[BT];
  threadgroup float beta_s[BT];
  threadgroup float q_buf[BK];

  // Load state column into registers
  float s[BK];
  for (int j = 0; j < BK; j++) {
    s[j] = state_bh[j * v_dim + v_idx];
  }

  // Per-thread register array for corrected deltas
  float delta_arr[BT];

  for (int c = 0; c < num_chunks; c++) {
    const int chunk_start = c * BT;
    const int chunk_len = min(BT, seq_len - chunk_start);

    // === Phase 1: Cooperative load of k, beta, g into threadgroup memory ===
    for (int t = 0; t < chunk_len; t++) {
      for (int j = (int)tid; j < BK; j += BV) {
        k_chunk[t * BK + j] = k_bh[(chunk_start + t) * BK + j];
      }
    }
    if ((int)tid < chunk_len) {
      beta_s[tid] = beta_bh[chunk_start + tid];
      gcum[tid] = g_bh[chunk_start + tid];
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // === Phase 1b: Parallel prefix sum of g (Hillis-Steele) ===
    for (int stride = 1; stride < BT; stride <<= 1) {
      float prev = 0.0f;
      if ((int)tid < chunk_len && (int)tid >= stride)
        prev = gcum[tid - stride];
      threadgroup_barrier(mem_flags::mem_threadgroup);
      if ((int)tid < chunk_len && (int)tid >= stride)
        gcum[tid] += prev;
      threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // === Phase 2: Compute kk_dot[i][j] = dot(k[i], k[j]) for j < i ===
    for (int idx = (int)tid; idx < chunk_len * chunk_len; idx += BV) {
      int i = idx / chunk_len;
      int j = idx % chunk_len;
      if (j < i) {
        float dot = 0.0f;
        for (int d = 0; d < BK; d++) {
          dot = fma(k_chunk[i * BK + d], k_chunk[j * BK + d], dot);
        }
        kk_dot[i * BT + j] = dot;
      }
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    // === Phase 3: Forward substitution (per V-column, in registers) ===
    for (int i = 0; i < chunk_len; i++) {
      float v_i = v_bh[(chunk_start + i) * v_dim + v_idx];
      float decay_i = exp(gcum[i]);
      float beta_i = beta_s[i];

      // Inter-chunk contribution: state @ k[i] with decay
      float kv_mem = 0.0f;
      for (int d = 0; d < BK; d++) {
        kv_mem = fma(s[d] * decay_i, k_chunk[i * BK + d], kv_mem);
      }

      float rhs = beta_i * (v_i - kv_mem);

      // Subtract lower-triangular contributions (intra-chunk)
      for (int j = 0; j < i; j++) {
        float a_ij = beta_i * kk_dot[i * BT + j] * exp(gcum[i] - gcum[j]);
        rhs -= a_ij * delta_arr[j];
      }
      delta_arr[i] = rhs;
    }

    // === Phase 4: Output computation (per V-column) ===
    for (int i = 0; i < chunk_len; i++) {
      // Cooperatively load q[i] into threadgroup memory
      for (int j = (int)tid; j < BK; j += BV) {
        q_buf[j] = q_bh[(chunk_start + i) * BK + j];
      }
      threadgroup_barrier(mem_flags::mem_threadgroup);

      float decay_i = exp(gcum[i]);

      // Inter-chunk contribution: q[i] @ (state * decay)
      float o_val = 0.0f;
      for (int d = 0; d < BK; d++) {
        o_val = fma(q_buf[d], s[d] * decay_i, o_val);
      }

      // Intra-chunk contribution
      for (int j = 0; j <= i; j++) {
        float qk_dot = 0.0f;
        for (int d = 0; d < BK; d++) {
          qk_dot = fma(q_buf[d], k_chunk[j * BK + d], qk_dot);
        }
        o_val += qk_dot * delta_arr[j] * exp(gcum[i] - gcum[j]);
      }

      out_bh[(chunk_start + i) * v_dim + v_idx] = o_val;
      threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    // === Phase 5: State update for next chunk ===
    float g_total = gcum[chunk_len - 1];
    for (int d = 0; d < BK; d++) {
      float s_new = s[d] * exp(g_total);
      for (int t = 0; t < chunk_len; t++) {
        s_new += k_chunk[t * BK + d] * delta_arr[t] * exp(g_total - gcum[t]);
      }
      s[d] = s_new;
    }

    threadgroup_barrier(mem_flags::mem_threadgroup);
  }

  // Write final state back
  for (int j = 0; j < BK; j++) {
    state_bh[j * v_dim + v_idx] = s[j];
  }
}

// Explicit instantiations for chunked kernel (BT=32 to fit 32KB threadgroup
// memory)
template [[host_name("chunked_gated_delta_rule_32_128_64")]] [[kernel]]
void chunked_gated_delta_rule_kernel<32, 128, 64>(
    const device float *, const device float *, const device float *,
    const device float *, const device float *, device float *, device float *,
    constant int &, constant int &, uint2, uint);

template [[host_name("chunked_gated_delta_rule_32_64_64")]] [[kernel]]
void chunked_gated_delta_rule_kernel<32, 64, 64>(
    const device float *, const device float *, const device float *,
    const device float *, const device float *, device float *, device float *,
    constant int &, constant int &, uint2, uint);

// ============================================================================
// Kernel 2a: causal_conv1d_update (decode path, single step)
// ============================================================================

template <typename T>
[[kernel]] void causal_conv1d_update_kernel(
    const device T *x [[buffer(0)]], const device T *weight [[buffer(1)]],
    device T *conv_state [[buffer(2)]], device T *output [[buffer(3)]],
    constant int &batch_size [[buffer(4)]],
    constant int &conv_dim [[buffer(5)]],
    constant int &kernel_size [[buffer(6)]],
    uint2 gid [[thread_position_in_grid]]) {
  const int ch = gid.x;
  const int b = gid.y;

  if (ch >= conv_dim || b >= batch_size)
    return;

  device T *cs = conv_state + (b * conv_dim + ch) * kernel_size;
  const device T *w = weight + ch * kernel_size;

  // Shift state left by 1
  for (int i = 0; i < kernel_size - 1; i++) {
    cs[i] = cs[i + 1];
  }
  cs[kernel_size - 1] = x[b * conv_dim + ch];

  // Dot product with weight
  float acc = 0.0f;
  for (int i = 0; i < kernel_size; i++) {
    acc += (float)cs[i] * (float)w[i];
  }

  // SiLU activation
  float sig = 1.0f / (1.0f + exp(-acc));
  float result = acc * sig;

  output[b * conv_dim + ch] = (T)result;
}

#define instantiate_conv1d_update(type)                                        \
  template [[host_name("causal_conv1d_update_" #type)]] [[kernel]]             \
  void causal_conv1d_update_kernel<type>(                                      \
      const device type *, const device type *, device type *, device type *,  \
      constant int &, constant int &, constant int &, uint2);

instantiate_conv1d_update(half);
instantiate_conv1d_update(bfloat16_t);

// ============================================================================
// Kernel 2b: causal_conv1d_full (prefill path)
// ============================================================================

template <typename T>
[[kernel]] void causal_conv1d_full_kernel(
    const device T *x [[buffer(0)]], const device T *weight [[buffer(1)]],
    device T *output [[buffer(2)]], constant int &batch_size [[buffer(3)]],
    constant int &conv_dim [[buffer(4)]], constant int &seq_len [[buffer(5)]],
    constant int &kernel_size [[buffer(6)]],
    uint3 gid [[thread_position_in_grid]]) {
  const int ch = gid.x;
  const int pos = gid.y;
  const int b = gid.z;

  if (ch >= conv_dim || pos >= seq_len || b >= batch_size)
    return;

  const device T *x_bch = x + (b * conv_dim + ch) * seq_len;
  const device T *w = weight + ch * kernel_size;

  float acc = 0.0f;
  for (int i = 0; i < kernel_size; i++) {
    int src_pos = pos - (kernel_size - 1) + i;
    float x_val = (src_pos >= 0) ? (float)x_bch[src_pos] : 0.0f;
    acc += x_val * (float)w[i];
  }

  // SiLU
  float sig = 1.0f / (1.0f + exp(-acc));
  float result = acc * sig;

  output[(b * conv_dim + ch) * seq_len + pos] = (T)result;
}

template <typename T>
[[kernel]] void save_conv_state_kernel(const device T *x [[buffer(0)]],
                                       device T *conv_state_out [[buffer(1)]],
                                       constant int &batch_size [[buffer(2)]],
                                       constant int &conv_dim [[buffer(3)]],
                                       constant int &seq_len [[buffer(4)]],
                                       constant int &kernel_size [[buffer(5)]],
                                       uint2 gid [[thread_position_in_grid]]) {
  const int ch = gid.x;
  const int b = gid.y;

  if (ch >= conv_dim || b >= batch_size)
    return;

  const device T *x_bch = x + (b * conv_dim + ch) * seq_len;
  device T *cs = conv_state_out + (b * conv_dim + ch) * kernel_size;

  int pad = kernel_size - seq_len;
  for (int i = 0; i < kernel_size; i++) {
    if (i < pad) {
      cs[i] = (T)0.0f;
    } else {
      cs[i] = x_bch[seq_len - kernel_size + i];
    }
  }
}

#define instantiate_conv1d_full(type)                                          \
  template [[host_name("causal_conv1d_full_" #type)]] [[kernel]]               \
  void causal_conv1d_full_kernel<type>(                                        \
      const device type *, const device type *, device type *, constant int &, \
      constant int &, constant int &, constant int &, uint3);                  \
  template [[host_name("save_conv_state_" #type)]] [[kernel]]                  \
  void save_conv_state_kernel<type>(const device type *, device type *,        \
                                    constant int &, constant int &,            \
                                    constant int &, constant int &, uint2);

instantiate_conv1d_full(half);
instantiate_conv1d_full(bfloat16_t);

// ============================================================================
// Kernel 3: fused_gdn_gating
//
// Fuses: beta = sigmoid(b), g = -exp(a_log) * softplus(a + dt_bias)
// ============================================================================

template <typename T>
[[kernel]] void fused_gdn_gating_kernel(
    const device T *b_in [[buffer(0)]], const device T *a_in [[buffer(1)]],
    const device float *a_log [[buffer(2)]],
    const device float *dt_bias [[buffer(3)]], device T *beta_out [[buffer(4)]],
    device T *g_out [[buffer(5)]], constant int &total_elements [[buffer(6)]],
    constant int &num_heads [[buffer(7)]],
    uint gid [[thread_position_in_grid]]) {
  const int idx = gid;
  if (idx >= total_elements)
    return;

  int head_idx = idx % num_heads;

  // beta = sigmoid(b)
  float b_val = (float)b_in[idx];
  float beta_val = 1.0f / (1.0f + exp(-b_val));

  // g = -exp(a_log) * softplus(a + dt_bias)
  float a_val = (float)a_in[idx];
  float a_log_val = a_log[head_idx];
  float dt_bias_val = dt_bias[head_idx];

  float sp_input = a_val + dt_bias_val;
  float softplus_val = log(1.0f + exp(sp_input));
  float g_val = -exp(a_log_val) * softplus_val;

  beta_out[idx] = (T)beta_val;
  g_out[idx] = (T)g_val;
}

#define instantiate_gdn_gating(type)                                           \
  template [[host_name("fused_gdn_gating_" #type)]] [[kernel]]                 \
  void fused_gdn_gating_kernel<type>(                                          \
      const device type *, const device type *, const device float *,          \
      const device float *, device type *, device type *, constant int &,      \
      constant int &, uint);

instantiate_gdn_gating(half);
instantiate_gdn_gating(bfloat16_t);