MLX
Loading...
Searching...
No Matches
sdpa_vector.h
Go to the documentation of this file.
1// Copyright © 2024 Apple Inc.
2
3#include <metal_simdgroup>
4
5using namespace metal;
6
7template <typename T, int D>
8[[kernel]] void sdpa_vector(
9 const device T* queries [[buffer(0)]],
10 const device T* keys [[buffer(1)]],
11 const device T* values [[buffer(2)]],
12 device T* out [[buffer(3)]],
13 const constant int& gqa_factor,
14 const constant int& N,
15 const constant size_t& k_stride,
16 const constant float& scale,
17 uint3 tid [[threadgroup_position_in_grid]],
18 uint simd_gid [[simdgroup_index_in_threadgroup]],
19 uint simd_lid [[thread_index_in_simdgroup]]) {
20 constexpr int BN = 32;
21 constexpr int BD = 32;
22 constexpr int elem_per_thread = D / BD;
23
24 const int stride = BN * D;
25
26 typedef float U;
27
28 thread U q[elem_per_thread];
29 thread U k[elem_per_thread];
30 thread U o[elem_per_thread];
31
32 threadgroup U outputs[BN * BD];
33 threadgroup U max_scores[BN];
34 threadgroup U sum_exp_scores[BN];
35
36 // Adjust positions
37 const int head_idx = tid.y;
38 const int kv_head_idx = head_idx / gqa_factor;
39 queries += head_idx * D + simd_lid * elem_per_thread;
40 keys += kv_head_idx * k_stride + simd_gid * D + simd_lid * elem_per_thread;
41 values += kv_head_idx * k_stride + simd_gid * D + simd_lid * elem_per_thread;
42 out += head_idx * D + simd_gid * elem_per_thread;
43
44 // Read the query and 0 the output accumulator
45 for (int i = 0; i < elem_per_thread; i++) {
46 q[i] = static_cast<U>(scale) * queries[i];
47 }
48 for (int i = 0; i < elem_per_thread; i++) {
49 o[i] = 0;
50 }
51
52 U max_score = -INFINITY;
53 U sum_exp_score = 0;
54
55 // For each key
56 for (int i = simd_gid; i < N; i += BN) {
57 // Read the key
58 for (int i = 0; i < elem_per_thread; i++) {
59 k[i] = keys[i];
60 }
61
62 // Compute the i-th score
63 U score = 0;
64 for (int i = 0; i < elem_per_thread; i++) {
65 score += q[i] * k[i];
66 }
67 score = simd_sum(score);
68
69 // Update the accumulators
70 U new_max = max(max_score, score);
71 U factor = fast::exp(max_score - new_max);
72 U exp_score = fast::exp(score - new_max);
73
74 max_score = new_max;
75 sum_exp_score = sum_exp_score * factor + exp_score;
76
77 // Update the output accumulator
78 for (int i = 0; i < elem_per_thread; i++) {
79 o[i] = o[i] * factor + exp_score * values[i];
80 }
81
82 // Move the pointers to the next kv
83 keys += stride;
84 values += stride;
85 }
86 threadgroup_barrier(mem_flags::mem_threadgroup);
87
88 // Each thread has a partial part of the output so we need to combine them.
89
90 // First let's communicate the max and sum_exp
91 if (simd_lid == 0) {
92 max_scores[simd_gid] = max_score;
93 sum_exp_scores[simd_gid] = sum_exp_score;
94 }
95 threadgroup_barrier(mem_flags::mem_threadgroup);
96 max_score = max_scores[simd_lid];
97 U new_max = simd_max(max_score);
98 U factor = fast::exp(max_score - new_max);
99 sum_exp_score = simd_sum(sum_exp_scores[simd_lid] * factor);
100
101 // Now we need to aggregate all the outputs
102 for (int i = 0; i < elem_per_thread; i++) {
103 outputs[simd_lid * BD + simd_gid] = o[i];
104 threadgroup_barrier(mem_flags::mem_threadgroup);
105 o[i] = simd_sum(outputs[simd_gid * BD + simd_lid] * factor) / sum_exp_score;
106 threadgroup_barrier(mem_flags::mem_threadgroup);
107 }
108
109 // And write the output
110 if (simd_lid == 0) {
111 for (int i = 0; i < elem_per_thread; i++) {
112 out[i] = static_cast<T>(o[i]);
113 }
114 }
115}
METAL_FUNC bfloat16_t exp(bfloat16_t x)
Definition bf16_math.h:242
Definition bf16.h:265
METAL_FUNC bfloat16_t simd_max(bfloat16_t data)
Definition bf16_math.h:392
METAL_FUNC bfloat16_t simd_sum(bfloat16_t data)
Definition bf16_math.h:392
METAL_FUNC bfloat16_t max(bfloat16_t x, bfloat16_t y)
Definition bf16_math.h:234
void sdpa_vector(const device T *queries, const device T *keys, const device T *values, device T *out, const constant int &gqa_factor, const constant int &N, const constant size_t &k_stride, const constant float &scale, uint3 tid, uint simd_gid, uint simd_lid)
Definition sdpa_vector.h:8