Commit
·
364f72d
0
Parent(s):
Add metal flash sdpa
Browse files- .gitattributes +35 -0
- README.md +69 -0
- benchmark_flash_sdpa.py +301 -0
- build.toml +18 -0
- flake.nix +17 -0
- sdpa-metal/common.h +7 -0
- sdpa-metal/scaled_dot_product_attention.metal +2070 -0
- sdpa-metal/scaled_dot_product_attention.mm +330 -0
- tests/__init__.py +0 -0
- tests/test_flash_attention.py +1132 -0
- torch-ext/sdpa_flash/__init__.py +11 -0
- torch-ext/sdpa_flash/_custom_ops.py +117 -0
- torch-ext/torch_binding.cpp +11 -0
- torch-ext/torch_binding.h +16 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- kernel
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
# Metal Flash Attention
|
| 8 |
+
|
| 9 |
+
A PyTorch extension that provides optimized Metal implementations of Flash Attention kernels for Metal.
|
| 10 |
+
|
| 11 |
+
## Supported Features
|
| 12 |
+
|
| 13 |
+
- Variable-length sequences without padding
|
| 14 |
+
- Causal masking
|
| 15 |
+
- Grouped Query Attention (GQA) and Multi-Query Attention (MQA)
|
| 16 |
+
- Softcapping support for attention score regularization
|
| 17 |
+
- Data types: `float32`, `float16`, `bfloat16`
|
| 18 |
+
- Head dimensions: `32`, `64`, `72`, `80`, `96`, `128`, `256`
|
| 19 |
+
|
| 20 |
+
## API Reference
|
| 21 |
+
|
| 22 |
+
### flash_attention_varlen
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
sdpa_flash.flash_attention_varlen(
|
| 26 |
+
out: torch.Tensor,
|
| 27 |
+
query: torch.Tensor,
|
| 28 |
+
key: torch.Tensor,
|
| 29 |
+
value: torch.Tensor,
|
| 30 |
+
cu_seqlens_q: torch.Tensor,
|
| 31 |
+
cu_seqlens_k: torch.Tensor,
|
| 32 |
+
max_seqlen_q: int,
|
| 33 |
+
max_seqlen_k: int,
|
| 34 |
+
do_causal: bool,
|
| 35 |
+
scale: float,
|
| 36 |
+
softcapping: float
|
| 37 |
+
) -> None
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
- **out**: Output tensor `[total_q_tokens, num_heads, head_dim]`, modified in-place.
|
| 41 |
+
- **query/key/value**: Input tensors `[total_tokens, num_heads(_kv), head_dim]`.
|
| 42 |
+
- **cu_seqlens_q/cu_seqlens_k**: Cumulative sequence lengths (`torch.int32`), `[batch_size + 1]`.
|
| 43 |
+
- **max_seqlen_q/max_seqlen_k**: Maximum sequence lengths.
|
| 44 |
+
- **do_causal**: Enable causal masking.
|
| 45 |
+
- **scale**: Attention score scaling factor (e.g., `1/sqrt(head_dim)`).
|
| 46 |
+
- **softcapping**: Softcapping value for score regularization (use `1.0` for no softcapping).
|
| 47 |
+
|
| 48 |
+
### flash_attn_varlen_func
|
| 49 |
+
|
| 50 |
+
Compatibility wrapper matching the original Flash Attention API:
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
out = sdpa_flash.flash_attn_varlen_func(
|
| 54 |
+
q: torch.Tensor,
|
| 55 |
+
k: torch.Tensor,
|
| 56 |
+
v: torch.Tensor,
|
| 57 |
+
cu_seqlens_q: torch.Tensor,
|
| 58 |
+
cu_seqlens_k: torch.Tensor,
|
| 59 |
+
max_seqlen_q: int,
|
| 60 |
+
max_seqlen_k: int,
|
| 61 |
+
dropout_p: float = 0.0,
|
| 62 |
+
softmax_scale: Optional[float] = None,
|
| 63 |
+
causal: bool = False,
|
| 64 |
+
window_size: Tuple[int, int] = (-1, -1),
|
| 65 |
+
alibi_slopes: Optional[torch.Tensor] = None,
|
| 66 |
+
deterministic: bool = False,
|
| 67 |
+
return_attn_probs: bool = False
|
| 68 |
+
)
|
| 69 |
+
```
|
benchmark_flash_sdpa.py
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Benchmark script for metal-sdpa-flash (Flash SDPA)"""
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import time
|
| 6 |
+
import sdpa_flash
|
| 7 |
+
from typing import List, Tuple
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_cu_seqlens(seq_lengths: List[int]) -> torch.Tensor:
|
| 12 |
+
"""Create cumulative sequence lengths tensor."""
|
| 13 |
+
cu_seqlens = [0]
|
| 14 |
+
for length in seq_lengths:
|
| 15 |
+
cu_seqlens.append(cu_seqlens[-1] + length)
|
| 16 |
+
return torch.tensor(cu_seqlens, dtype=torch.int32, device="mps")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def warmup(func, *args, num_warmup=10):
|
| 20 |
+
"""Warmup the GPU by running the function multiple times"""
|
| 21 |
+
for _ in range(num_warmup):
|
| 22 |
+
func(*args)
|
| 23 |
+
torch.mps.synchronize()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def benchmark_flash_sdpa(
|
| 27 |
+
batch_size: int,
|
| 28 |
+
num_heads: int,
|
| 29 |
+
seq_len: int,
|
| 30 |
+
head_dim: int,
|
| 31 |
+
dtype: torch.dtype,
|
| 32 |
+
causal: bool = False,
|
| 33 |
+
num_iterations: int = 100,
|
| 34 |
+
) -> float:
|
| 35 |
+
"""Benchmark Flash SDPA with given parameters"""
|
| 36 |
+
|
| 37 |
+
# Create sequence lengths (all equal for fair comparison)
|
| 38 |
+
seq_lengths = [seq_len] * batch_size
|
| 39 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 40 |
+
total_tokens = sum(seq_lengths)
|
| 41 |
+
|
| 42 |
+
# Create input tensors in Flash format (total_tokens, num_heads, head_dim)
|
| 43 |
+
query = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 44 |
+
key = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 45 |
+
value = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 46 |
+
out = torch.empty_like(query)
|
| 47 |
+
|
| 48 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 49 |
+
|
| 50 |
+
# Define the function to benchmark
|
| 51 |
+
def run_flash_sdpa():
|
| 52 |
+
sdpa_flash.flash_attention_varlen(
|
| 53 |
+
out=out,
|
| 54 |
+
query=query,
|
| 55 |
+
key=key,
|
| 56 |
+
value=value,
|
| 57 |
+
cu_seqlens_q=cu_seqlens,
|
| 58 |
+
cu_seqlens_k=cu_seqlens,
|
| 59 |
+
max_seqlen_q=seq_len,
|
| 60 |
+
max_seqlen_k=seq_len,
|
| 61 |
+
mask=None,
|
| 62 |
+
do_causal=causal,
|
| 63 |
+
scale=scale,
|
| 64 |
+
softcapping=1.0,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Warmup
|
| 68 |
+
warmup(run_flash_sdpa, num_warmup=10)
|
| 69 |
+
|
| 70 |
+
# Benchmark
|
| 71 |
+
torch.mps.synchronize()
|
| 72 |
+
start_time = time.perf_counter()
|
| 73 |
+
|
| 74 |
+
for _ in range(num_iterations):
|
| 75 |
+
run_flash_sdpa()
|
| 76 |
+
|
| 77 |
+
torch.mps.synchronize()
|
| 78 |
+
end_time = time.perf_counter()
|
| 79 |
+
|
| 80 |
+
avg_time_ms = (end_time - start_time) * 1000 / num_iterations
|
| 81 |
+
return avg_time_ms
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def benchmark_flash_gqa(
|
| 85 |
+
batch_size: int,
|
| 86 |
+
num_heads_q: int,
|
| 87 |
+
num_heads_kv: int,
|
| 88 |
+
seq_len: int,
|
| 89 |
+
head_dim: int,
|
| 90 |
+
dtype: torch.dtype,
|
| 91 |
+
causal: bool = False,
|
| 92 |
+
num_iterations: int = 100,
|
| 93 |
+
) -> float:
|
| 94 |
+
"""Benchmark Flash Attention with Grouped Query Attention"""
|
| 95 |
+
|
| 96 |
+
# Create sequence lengths
|
| 97 |
+
seq_lengths = [seq_len] * batch_size
|
| 98 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 99 |
+
total_tokens = sum(seq_lengths)
|
| 100 |
+
|
| 101 |
+
# Create input tensors with different head counts
|
| 102 |
+
query = torch.randn(total_tokens, num_heads_q, head_dim, dtype=dtype, device="mps")
|
| 103 |
+
key = torch.randn(total_tokens, num_heads_kv, head_dim, dtype=dtype, device="mps")
|
| 104 |
+
value = torch.randn(total_tokens, num_heads_kv, head_dim, dtype=dtype, device="mps")
|
| 105 |
+
out = torch.empty_like(query)
|
| 106 |
+
|
| 107 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 108 |
+
|
| 109 |
+
# Define the function to benchmark
|
| 110 |
+
def run_flash_gqa():
|
| 111 |
+
sdpa_flash.flash_attention_varlen(
|
| 112 |
+
out=out,
|
| 113 |
+
query=query,
|
| 114 |
+
key=key,
|
| 115 |
+
value=value,
|
| 116 |
+
cu_seqlens_q=cu_seqlens,
|
| 117 |
+
cu_seqlens_k=cu_seqlens,
|
| 118 |
+
max_seqlen_q=seq_len,
|
| 119 |
+
max_seqlen_k=seq_len,
|
| 120 |
+
mask=None,
|
| 121 |
+
do_causal=causal,
|
| 122 |
+
scale=scale,
|
| 123 |
+
softcapping=1.0,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# Warmup
|
| 127 |
+
warmup(run_flash_gqa, num_warmup=10)
|
| 128 |
+
|
| 129 |
+
# Benchmark
|
| 130 |
+
torch.mps.synchronize()
|
| 131 |
+
start_time = time.perf_counter()
|
| 132 |
+
|
| 133 |
+
for _ in range(num_iterations):
|
| 134 |
+
run_flash_gqa()
|
| 135 |
+
|
| 136 |
+
torch.mps.synchronize()
|
| 137 |
+
end_time = time.perf_counter()
|
| 138 |
+
|
| 139 |
+
avg_time_ms = (end_time - start_time) * 1000 / num_iterations
|
| 140 |
+
return avg_time_ms
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def benchmark_variable_length(
|
| 144 |
+
seq_lengths: List[int],
|
| 145 |
+
num_heads: int,
|
| 146 |
+
head_dim: int,
|
| 147 |
+
dtype: torch.dtype,
|
| 148 |
+
causal: bool = False,
|
| 149 |
+
num_iterations: int = 100,
|
| 150 |
+
) -> float:
|
| 151 |
+
"""Benchmark Flash Attention with variable sequence lengths"""
|
| 152 |
+
|
| 153 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 154 |
+
total_tokens = sum(seq_lengths)
|
| 155 |
+
max_seqlen = max(seq_lengths)
|
| 156 |
+
|
| 157 |
+
# Create input tensors
|
| 158 |
+
query = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 159 |
+
key = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 160 |
+
value = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 161 |
+
out = torch.empty_like(query)
|
| 162 |
+
|
| 163 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 164 |
+
|
| 165 |
+
# Define the function to benchmark
|
| 166 |
+
def run_varlen():
|
| 167 |
+
sdpa_flash.flash_attention_varlen(
|
| 168 |
+
out=out,
|
| 169 |
+
query=query,
|
| 170 |
+
key=key,
|
| 171 |
+
value=value,
|
| 172 |
+
cu_seqlens_q=cu_seqlens,
|
| 173 |
+
cu_seqlens_k=cu_seqlens,
|
| 174 |
+
max_seqlen_q=max_seqlen,
|
| 175 |
+
max_seqlen_k=max_seqlen,
|
| 176 |
+
mask=None,
|
| 177 |
+
do_causal=causal,
|
| 178 |
+
scale=scale,
|
| 179 |
+
softcapping=1.0,
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# Warmup
|
| 183 |
+
warmup(run_varlen, num_warmup=10)
|
| 184 |
+
|
| 185 |
+
# Benchmark
|
| 186 |
+
torch.mps.synchronize()
|
| 187 |
+
start_time = time.perf_counter()
|
| 188 |
+
|
| 189 |
+
for _ in range(num_iterations):
|
| 190 |
+
run_varlen()
|
| 191 |
+
|
| 192 |
+
torch.mps.synchronize()
|
| 193 |
+
end_time = time.perf_counter()
|
| 194 |
+
|
| 195 |
+
avg_time_ms = (end_time - start_time) * 1000 / num_iterations
|
| 196 |
+
return avg_time_ms
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def main():
|
| 200 |
+
print("=" * 80)
|
| 201 |
+
print("Metal Flash SDPA Benchmark")
|
| 202 |
+
print("=" * 80)
|
| 203 |
+
|
| 204 |
+
# Test configurations (matching the plain SDPA benchmark)
|
| 205 |
+
configs = [
|
| 206 |
+
# (batch_size, num_heads, seq_len, head_dim, dtype, causal, name)
|
| 207 |
+
(1, 32, 512, 64, torch.float32, False, "Small seq, float32"),
|
| 208 |
+
(1, 32, 512, 64, torch.float16, False, "Small seq, float16"),
|
| 209 |
+
(1, 32, 512, 64, torch.bfloat16, False, "Small seq, bfloat16"),
|
| 210 |
+
|
| 211 |
+
(4, 32, 2048, 64, torch.float16, False, "Medium seq, float16"),
|
| 212 |
+
(4, 32, 2048, 64, torch.float16, True, "Medium seq, float16, causal"),
|
| 213 |
+
|
| 214 |
+
(2, 32, 4096, 64, torch.float16, False, "Large seq, float16"),
|
| 215 |
+
(2, 32, 4096, 64, torch.float16, True, "Large seq, float16, causal"),
|
| 216 |
+
|
| 217 |
+
# Different head dimensions
|
| 218 |
+
(2, 32, 2048, 32, torch.float16, False, "head_dim=32"),
|
| 219 |
+
(2, 32, 2048, 64, torch.float16, False, "head_dim=64"),
|
| 220 |
+
(2, 32, 2048, 128, torch.float16, False, "head_dim=128"),
|
| 221 |
+
|
| 222 |
+
# Vector kernel cases (q_seq=1) - Flash doesn't have a special vector kernel
|
| 223 |
+
# but we benchmark these cases for fair comparison with plain SDPA
|
| 224 |
+
(16, 32, 1, 64, torch.float16, False, "Vector kernel (q_seq=1)"),
|
| 225 |
+
(16, 32, 1, 128, torch.float16, False, "Vector kernel (q_seq=1, head_dim=128)"),
|
| 226 |
+
]
|
| 227 |
+
|
| 228 |
+
print("\nFlash Attention Benchmarks:")
|
| 229 |
+
print("-" * 80)
|
| 230 |
+
print(f"{'Config':<40} {'Time (ms)':<15} {'TFLOPS':<15}")
|
| 231 |
+
print("-" * 80)
|
| 232 |
+
|
| 233 |
+
for batch_size, num_heads, seq_len, head_dim, dtype, causal, name in configs:
|
| 234 |
+
time_ms = benchmark_flash_sdpa(
|
| 235 |
+
batch_size, num_heads, seq_len, head_dim, dtype, causal
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
# Calculate FLOPS (approximate)
|
| 239 |
+
# Attention: 2 * batch * heads * seq_len^2 * head_dim
|
| 240 |
+
flops = 2 * batch_size * num_heads * seq_len * seq_len * head_dim
|
| 241 |
+
tflops = (flops / 1e12) / (time_ms / 1000)
|
| 242 |
+
|
| 243 |
+
print(f"{name:<40} {time_ms:<15.3f} {tflops:<15.2f}")
|
| 244 |
+
|
| 245 |
+
# GQA benchmarks
|
| 246 |
+
print("\n\nGrouped Query Attention (GQA) Benchmarks:")
|
| 247 |
+
print("-" * 80)
|
| 248 |
+
print(f"{'Config':<40} {'Time (ms)':<15} {'TFLOPS':<15}")
|
| 249 |
+
print("-" * 80)
|
| 250 |
+
|
| 251 |
+
gqa_configs = [
|
| 252 |
+
# (batch_size, num_heads_q, num_heads_kv, seq_len, head_dim, dtype, causal, name)
|
| 253 |
+
(2, 32, 8, 2048, 64, torch.float16, False, "GQA 4:1 ratio"),
|
| 254 |
+
(2, 32, 4, 2048, 64, torch.float16, False, "GQA 8:1 ratio"),
|
| 255 |
+
(2, 32, 1, 2048, 64, torch.float16, False, "MQA (32:1 ratio)"),
|
| 256 |
+
(2, 32, 8, 2048, 128, torch.float16, False, "GQA 4:1, head_dim=128"),
|
| 257 |
+
]
|
| 258 |
+
|
| 259 |
+
for batch_size, num_heads_q, num_heads_kv, seq_len, head_dim, dtype, causal, name in gqa_configs:
|
| 260 |
+
time_ms = benchmark_flash_gqa(
|
| 261 |
+
batch_size, num_heads_q, num_heads_kv, seq_len, head_dim, dtype, causal
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# Calculate FLOPS for GQA
|
| 265 |
+
flops = 2 * batch_size * num_heads_q * seq_len * seq_len * head_dim
|
| 266 |
+
tflops = (flops / 1e12) / (time_ms / 1000)
|
| 267 |
+
|
| 268 |
+
print(f"{name:<40} {time_ms:<15.3f} {tflops:<15.2f}")
|
| 269 |
+
|
| 270 |
+
# Variable length sequences (unique to Flash Attention)
|
| 271 |
+
print("\n\nVariable Length Sequence Benchmarks:")
|
| 272 |
+
print("-" * 80)
|
| 273 |
+
print(f"{'Config':<40} {'Time (ms)':<15} {'TFLOPS':<15}")
|
| 274 |
+
print("-" * 80)
|
| 275 |
+
|
| 276 |
+
varlen_configs = [
|
| 277 |
+
# (seq_lengths, num_heads, head_dim, dtype, causal, name)
|
| 278 |
+
([512, 1024, 2048, 4096], 32, 64, torch.float16, False, "Variable [512-4096]"),
|
| 279 |
+
([128, 256, 512, 1024, 2048], 32, 64, torch.float16, False, "Variable [128-2048]"),
|
| 280 |
+
([2048, 2048, 2048, 2048], 32, 64, torch.float16, False, "Fixed 4x2048 (baseline)"),
|
| 281 |
+
]
|
| 282 |
+
|
| 283 |
+
for seq_lengths, num_heads, head_dim, dtype, causal, name in varlen_configs:
|
| 284 |
+
time_ms = benchmark_variable_length(
|
| 285 |
+
seq_lengths, num_heads, head_dim, dtype, causal
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
# Calculate FLOPS for variable length
|
| 289 |
+
total_flops = 0
|
| 290 |
+
for seq_len in seq_lengths:
|
| 291 |
+
total_flops += 2 * num_heads * seq_len * seq_len * head_dim
|
| 292 |
+
tflops = (total_flops / 1e12) / (time_ms / 1000)
|
| 293 |
+
|
| 294 |
+
print(f"{name:<40} {time_ms:<15.3f} {tflops:<15.2f}")
|
| 295 |
+
|
| 296 |
+
print("\n" + "=" * 80)
|
| 297 |
+
print("Benchmark completed!")
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
if __name__ == "__main__":
|
| 301 |
+
main()
|
build.toml
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[general]
|
| 2 |
+
name = "sdpa_flash"
|
| 3 |
+
universal = false
|
| 4 |
+
|
| 5 |
+
[torch]
|
| 6 |
+
src = [
|
| 7 |
+
"torch-ext/torch_binding.cpp",
|
| 8 |
+
"torch-ext/torch_binding.h",
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
[kernel.sdpa_metal]
|
| 12 |
+
backend = "metal"
|
| 13 |
+
src = [
|
| 14 |
+
"sdpa-metal/scaled_dot_product_attention.mm",
|
| 15 |
+
"sdpa-metal/scaled_dot_product_attention.metal",
|
| 16 |
+
"sdpa-metal/common.h",
|
| 17 |
+
]
|
| 18 |
+
depends = [ "torch" ]
|
flake.nix
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
description = "Flake for SDPA kernel";
|
| 3 |
+
|
| 4 |
+
inputs = {
|
| 5 |
+
kernel-builder.url = "path:../..";
|
| 6 |
+
};
|
| 7 |
+
|
| 8 |
+
outputs =
|
| 9 |
+
{
|
| 10 |
+
self,
|
| 11 |
+
kernel-builder,
|
| 12 |
+
}:
|
| 13 |
+
kernel-builder.lib.genFlakeOutputs {
|
| 14 |
+
path = ./.;
|
| 15 |
+
rev = self.shortRev or self.dirtyShortRev or self.lastModifiedDate;
|
| 16 |
+
};
|
| 17 |
+
}
|
sdpa-metal/common.h
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#ifndef SDPA_METAL_COMMON_H
|
| 2 |
+
#define SDPA_METAL_COMMON_H
|
| 3 |
+
|
| 4 |
+
// Common definitions for Metal kernels
|
| 5 |
+
// This file is included by Metal shaders, so it should not contain C++ code
|
| 6 |
+
|
| 7 |
+
#endif // SDPA_METAL_COMMON_H
|
sdpa-metal/scaled_dot_product_attention.metal
ADDED
|
@@ -0,0 +1,2070 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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metal_simdgroup>
|
| 5 |
+
|
| 6 |
+
using namespace metal;
|
| 7 |
+
|
| 8 |
+
#define STEEL_CONST static constant constexpr const
|
| 9 |
+
#define STEEL_PRAGMA_UNROLL _Pragma("clang loop unroll(full)")
|
| 10 |
+
|
| 11 |
+
#if defined(__HAVE_BFLOAT__)
|
| 12 |
+
|
| 13 |
+
typedef bfloat bfloat16_t;
|
| 14 |
+
typedef half float16_t;
|
| 15 |
+
|
| 16 |
+
#else
|
| 17 |
+
|
| 18 |
+
typedef half float16_t;
|
| 19 |
+
|
| 20 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 21 |
+
// Helpers
|
| 22 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 23 |
+
|
| 24 |
+
constexpr METAL_FUNC uint16_t float_to_bfloat_bits(float x) {
|
| 25 |
+
// Check for nan
|
| 26 |
+
if ((as_type<uint32_t>(x) & ~_fp_encoding_traits<float>::sign_mask) >
|
| 27 |
+
_fp_encoding_traits<float>::inf_mask) {
|
| 28 |
+
return uint16_t(as_type<uint32_t>(0x7FC0));
|
| 29 |
+
}
|
| 30 |
+
// Take bits
|
| 31 |
+
uint32_t float_bits = as_type<uint32_t>(x);
|
| 32 |
+
|
| 33 |
+
// Round to nearest even
|
| 34 |
+
float_bits += ((float_bits >> 16) & 1) + as_type<uint32_t>(0x7FFF);
|
| 35 |
+
|
| 36 |
+
// Take upper 16 bits
|
| 37 |
+
return float_bits >> 16;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
constexpr METAL_FUNC float bfloat_bits_to_float(uint16_t x) {
|
| 41 |
+
// Upper 16 bits are the data and lower 16 bits are 0s
|
| 42 |
+
return as_type<float>((uint32_t)x << 16);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
struct _MLX_BFloat16;
|
| 46 |
+
|
| 47 |
+
template <typename T>
|
| 48 |
+
static constexpr constant bool can_convert_to_bfloat =
|
| 49 |
+
!is_same_v<T, _MLX_BFloat16> && is_convertible_v<T, float>;
|
| 50 |
+
|
| 51 |
+
template <typename T>
|
| 52 |
+
static constexpr constant bool can_convert_from_bfloat =
|
| 53 |
+
!is_same_v<T, _MLX_BFloat16> && is_convertible_v<float, T>;
|
| 54 |
+
|
| 55 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 56 |
+
// Bfloat struct
|
| 57 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 58 |
+
|
| 59 |
+
struct _MLX_BFloat16 {
|
| 60 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 61 |
+
// Constructors
|
| 62 |
+
uint16_t bits_;
|
| 63 |
+
_MLX_BFloat16() thread = default;
|
| 64 |
+
_MLX_BFloat16() threadgroup = default;
|
| 65 |
+
_MLX_BFloat16() device = default;
|
| 66 |
+
_MLX_BFloat16() constant = default;
|
| 67 |
+
|
| 68 |
+
struct bits_to_bfloat_struct {};
|
| 69 |
+
static constexpr METAL_FUNC bits_to_bfloat_struct bits_to_bfloat() {
|
| 70 |
+
return bits_to_bfloat_struct();
|
| 71 |
+
}
|
| 72 |
+
constexpr METAL_FUNC _MLX_BFloat16(uint16_t bits, bits_to_bfloat_struct)
|
| 73 |
+
: bits_(bits) {}
|
| 74 |
+
|
| 75 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 76 |
+
// Conversions to bfloat
|
| 77 |
+
|
| 78 |
+
template <
|
| 79 |
+
typename T,
|
| 80 |
+
typename = typename enable_if<can_convert_to_bfloat<T>>::type>
|
| 81 |
+
constexpr METAL_FUNC _MLX_BFloat16(T x) thread
|
| 82 |
+
: bits_(float_to_bfloat_bits(static_cast<float>(x))) {}
|
| 83 |
+
|
| 84 |
+
template <
|
| 85 |
+
typename T,
|
| 86 |
+
typename = typename enable_if<can_convert_to_bfloat<T>>::type>
|
| 87 |
+
constexpr METAL_FUNC _MLX_BFloat16(T x) threadgroup
|
| 88 |
+
: bits_(float_to_bfloat_bits(static_cast<float>(x))) {}
|
| 89 |
+
|
| 90 |
+
template <
|
| 91 |
+
typename T,
|
| 92 |
+
typename = typename enable_if<can_convert_to_bfloat<T>>::type>
|
| 93 |
+
constexpr METAL_FUNC _MLX_BFloat16(T x) device
|
| 94 |
+
: bits_(float_to_bfloat_bits(static_cast<float>(x))) {}
|
| 95 |
+
|
| 96 |
+
template <
|
| 97 |
+
typename T,
|
| 98 |
+
typename = typename enable_if<can_convert_to_bfloat<T>>::type>
|
| 99 |
+
constexpr METAL_FUNC _MLX_BFloat16(T x) constant
|
| 100 |
+
: bits_(float_to_bfloat_bits(static_cast<float>(x))) {}
|
| 101 |
+
|
| 102 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 103 |
+
// Conversions from bfloat
|
| 104 |
+
|
| 105 |
+
template <
|
| 106 |
+
typename T,
|
| 107 |
+
typename = typename enable_if<can_convert_from_bfloat<T>>::type>
|
| 108 |
+
constexpr METAL_FUNC operator T() const thread {
|
| 109 |
+
return static_cast<T>(bfloat_bits_to_float(bits_));
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
template <
|
| 113 |
+
typename T,
|
| 114 |
+
typename = typename enable_if<can_convert_from_bfloat<T>>::type>
|
| 115 |
+
constexpr METAL_FUNC operator T() const threadgroup {
|
| 116 |
+
return static_cast<T>(bfloat_bits_to_float(bits_));
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
template <
|
| 120 |
+
typename T,
|
| 121 |
+
typename = typename enable_if<can_convert_from_bfloat<T>>::type>
|
| 122 |
+
constexpr METAL_FUNC operator T() const device {
|
| 123 |
+
return static_cast<T>(bfloat_bits_to_float(bits_));
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
template <
|
| 127 |
+
typename T,
|
| 128 |
+
typename = typename enable_if<can_convert_from_bfloat<T>>::type>
|
| 129 |
+
constexpr METAL_FUNC operator T() const constant {
|
| 130 |
+
return static_cast<T>(bfloat_bits_to_float(bits_));
|
| 131 |
+
}
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 135 |
+
// Bfloat operators
|
| 136 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 137 |
+
|
| 138 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 139 |
+
// Unary ops
|
| 140 |
+
constexpr METAL_FUNC _MLX_BFloat16 operator-(_MLX_BFloat16 x) {
|
| 141 |
+
return -static_cast<float>(x);
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 145 |
+
// Binary operators
|
| 146 |
+
#define bfloat_binop_base(__op__, __operator__, otype, atype, btype, ctype) \
|
| 147 |
+
constexpr METAL_FUNC otype __operator__(atype lhs, btype rhs) { \
|
| 148 |
+
return static_cast<ctype>(lhs) __op__ static_cast<ctype>(rhs); \
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
#define bfloat_binop_helper(__op__, __operator__, otype, itype, ctype) \
|
| 152 |
+
constexpr METAL_FUNC otype __operator__(_MLX_BFloat16 lhs, itype rhs) { \
|
| 153 |
+
return static_cast<ctype>(lhs) __op__ static_cast<ctype>(rhs); \
|
| 154 |
+
} \
|
| 155 |
+
constexpr METAL_FUNC otype __operator__(itype lhs, _MLX_BFloat16 rhs) { \
|
| 156 |
+
return static_cast<ctype>(lhs) __op__ static_cast<ctype>(rhs); \
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 160 |
+
// Arithmetic Operators
|
| 161 |
+
#define bfloat_binop(_op_, _operator_) \
|
| 162 |
+
bfloat_binop_base( \
|
| 163 |
+
_op_, _operator_, _MLX_BFloat16, _MLX_BFloat16, _MLX_BFloat16, float); \
|
| 164 |
+
bfloat_binop_helper(_op_, _operator_, float, float, float); \
|
| 165 |
+
bfloat_binop_helper(_op_, _operator_, float, half, float); \
|
| 166 |
+
bfloat_binop_helper(_op_, _operator_, _MLX_BFloat16, int32_t, float); \
|
| 167 |
+
bfloat_binop_helper(_op_, _operator_, _MLX_BFloat16, uint32_t, float); \
|
| 168 |
+
bfloat_binop_helper(_op_, _operator_, _MLX_BFloat16, int64_t, float); \
|
| 169 |
+
bfloat_binop_helper(_op_, _operator_, _MLX_BFloat16, uint64_t, float);
|
| 170 |
+
|
| 171 |
+
bfloat_binop(+, operator+);
|
| 172 |
+
bfloat_binop(-, operator-);
|
| 173 |
+
bfloat_binop(*, operator*);
|
| 174 |
+
bfloat_binop(/, operator/);
|
| 175 |
+
|
| 176 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 177 |
+
// Comparison ops
|
| 178 |
+
#define bfloat_compop(__op__, __operator__) \
|
| 179 |
+
bfloat_binop_base( \
|
| 180 |
+
__op__, __operator__, bool, _MLX_BFloat16, _MLX_BFloat16, float); \
|
| 181 |
+
bfloat_binop_helper(__op__, __operator__, bool, float, float); \
|
| 182 |
+
bfloat_binop_helper(__op__, __operator__, bool, half, float); \
|
| 183 |
+
bfloat_binop_helper(__op__, __operator__, bool, int32_t, float); \
|
| 184 |
+
bfloat_binop_helper(__op__, __operator__, bool, uint32_t, float); \
|
| 185 |
+
bfloat_binop_helper(__op__, __operator__, bool, int64_t, float); \
|
| 186 |
+
bfloat_binop_helper(__op__, __operator__, bool, uint64_t, float);
|
| 187 |
+
|
| 188 |
+
bfloat_compop(>, operator>);
|
| 189 |
+
bfloat_compop(<, operator<);
|
| 190 |
+
bfloat_compop(>=, operator>=);
|
| 191 |
+
bfloat_compop(<=, operator<=);
|
| 192 |
+
bfloat_compop(==, operator==);
|
| 193 |
+
bfloat_compop(!=, operator!=);
|
| 194 |
+
|
| 195 |
+
#undef bfloat_compop
|
| 196 |
+
#undef bfloat_binop_base
|
| 197 |
+
#undef bfloat_binop_helper
|
| 198 |
+
#undef bfloat_binop
|
| 199 |
+
|
| 200 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 201 |
+
// Inplace Operators
|
| 202 |
+
#define bfloat_inplace_op_helper(__op__, __operator__, itype, addr_space) \
|
| 203 |
+
constexpr METAL_FUNC addr_space _MLX_BFloat16& __operator__( \
|
| 204 |
+
addr_space _MLX_BFloat16& lhs, itype rhs) { \
|
| 205 |
+
lhs = static_cast<float>(lhs) __op__ static_cast<float>(rhs); \
|
| 206 |
+
return lhs; \
|
| 207 |
+
} \
|
| 208 |
+
constexpr METAL_FUNC addr_space itype& __operator__( \
|
| 209 |
+
addr_space itype& lhs, _MLX_BFloat16 rhs) { \
|
| 210 |
+
lhs = static_cast<float>(lhs) __op__ static_cast<float>(rhs); \
|
| 211 |
+
return lhs; \
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
#define bfloat_inplace_op_addr_space_helper(__op__, __operator__, itype) \
|
| 215 |
+
bfloat_inplace_op_helper(__op__, __operator__, itype, device); \
|
| 216 |
+
bfloat_inplace_op_helper(__op__, __operator__, itype, thread); \
|
| 217 |
+
bfloat_inplace_op_helper(__op__, __operator__, itype, threadgroup);
|
| 218 |
+
|
| 219 |
+
#define bfloat_inplace_op(itype) \
|
| 220 |
+
bfloat_inplace_op_addr_space_helper(+, operator+=, itype); \
|
| 221 |
+
bfloat_inplace_op_addr_space_helper(-, operator-=, itype); \
|
| 222 |
+
bfloat_inplace_op_addr_space_helper(*, operator*=, itype); \
|
| 223 |
+
bfloat_inplace_op_addr_space_helper(/, operator/=, itype);
|
| 224 |
+
|
| 225 |
+
bfloat_inplace_op(float);
|
| 226 |
+
bfloat_inplace_op(half);
|
| 227 |
+
bfloat_inplace_op(int16_t);
|
| 228 |
+
bfloat_inplace_op(int32_t);
|
| 229 |
+
bfloat_inplace_op(int64_t);
|
| 230 |
+
bfloat_inplace_op(uint16_t);
|
| 231 |
+
bfloat_inplace_op(uint32_t);
|
| 232 |
+
bfloat_inplace_op(uint64_t);
|
| 233 |
+
|
| 234 |
+
#undef bfloat_inplace_op_helper
|
| 235 |
+
#undef bfloat_inplace_op_addr_space_helper
|
| 236 |
+
#undef bfloat_inplace_op
|
| 237 |
+
|
| 238 |
+
#define bfloat_inplace_op_helper(__op__, __operator__, addr_space) \
|
| 239 |
+
constexpr METAL_FUNC addr_space _MLX_BFloat16& __operator__( \
|
| 240 |
+
addr_space _MLX_BFloat16& lhs, _MLX_BFloat16 rhs) { \
|
| 241 |
+
lhs = static_cast<float>(lhs) __op__ static_cast<float>(rhs); \
|
| 242 |
+
return lhs; \
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
#define bfloat_inplace_op_addr_space_helper(__op__, __operator__) \
|
| 246 |
+
bfloat_inplace_op_helper(__op__, __operator__, device); \
|
| 247 |
+
bfloat_inplace_op_helper(__op__, __operator__, thread); \
|
| 248 |
+
bfloat_inplace_op_helper(__op__, __operator__, threadgroup);
|
| 249 |
+
|
| 250 |
+
bfloat_inplace_op_addr_space_helper(+, operator+=);
|
| 251 |
+
bfloat_inplace_op_addr_space_helper(-, operator-=);
|
| 252 |
+
bfloat_inplace_op_addr_space_helper(*, operator*=);
|
| 253 |
+
bfloat_inplace_op_addr_space_helper(/, operator/=);
|
| 254 |
+
|
| 255 |
+
#undef bfloat_inplace_op_helper
|
| 256 |
+
#undef bfloat_inplace_op_addr_space_helper
|
| 257 |
+
|
| 258 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 259 |
+
// Bfloat typedef
|
| 260 |
+
/////////////////////////////////////////////////////////////////////////////
|
| 261 |
+
|
| 262 |
+
typedef struct _MLX_BFloat16 bfloat16_t;
|
| 263 |
+
|
| 264 |
+
#endif
|
| 265 |
+
|
| 266 |
+
// ============ "mlx/backend/metal/kernels/scaled_dot_product_attention_params.h"
|
| 267 |
+
|
| 268 |
+
struct MLXFastAttentionParams {
|
| 269 |
+
const int M;
|
| 270 |
+
const int N;
|
| 271 |
+
const int K;
|
| 272 |
+
|
| 273 |
+
const int ldq; // ldq == ldo
|
| 274 |
+
const int ldk;
|
| 275 |
+
const int ldv;
|
| 276 |
+
const int lds;
|
| 277 |
+
const int ldo;
|
| 278 |
+
|
| 279 |
+
const int tiles_n;
|
| 280 |
+
const int tiles_m;
|
| 281 |
+
|
| 282 |
+
const int batch_stride_q;
|
| 283 |
+
const int batch_stride_k;
|
| 284 |
+
const int batch_stride_v;
|
| 285 |
+
const int batch_stride_o;
|
| 286 |
+
|
| 287 |
+
const int swizzle_log;
|
| 288 |
+
const int gemm_n_iterations_aligned;
|
| 289 |
+
const int gemm_k_iterations_aligned;
|
| 290 |
+
const int gemm_sv_m_block_iterations;
|
| 291 |
+
|
| 292 |
+
const int batch_ndim;
|
| 293 |
+
const float alpha;
|
| 294 |
+
const float softcapping;
|
| 295 |
+
};
|
| 296 |
+
|
| 297 |
+
struct MLXScaledDotProductAttentionParams {
|
| 298 |
+
// Associated dimensions & transposition information
|
| 299 |
+
const uint QUERY_SEQUENCE_LENGTH = 1;
|
| 300 |
+
const uint N_Q_HEADS = 32;
|
| 301 |
+
const uint N_KV_HEADS = 32;
|
| 302 |
+
const uint KV_TILES = 1;
|
| 303 |
+
const float INV_ALPHA = 0.08838834764831843f;
|
| 304 |
+
};
|
| 305 |
+
|
| 306 |
+
// ============ "mlx/backend/metal/kernels/scaled_dot_product_attention_params.sdpa_vector"
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
// ============ "mlx/backend/metal/kernels/utils.h"
|
| 310 |
+
|
| 311 |
+
template <typename U>
|
| 312 |
+
struct Limits {
|
| 313 |
+
static const constant U max = metal::numeric_limits<U>::max();
|
| 314 |
+
static const constant U min = metal::numeric_limits<U>::min();
|
| 315 |
+
static const constant U finite_max = metal::numeric_limits<U>::max();
|
| 316 |
+
static const constant U finite_min = metal::numeric_limits<U>::min();
|
| 317 |
+
};
|
| 318 |
+
|
| 319 |
+
#define instantiate_default_limit(type) \
|
| 320 |
+
template <> \
|
| 321 |
+
struct Limits<type> { \
|
| 322 |
+
static constexpr constant type max = metal::numeric_limits<type>::max(); \
|
| 323 |
+
static constexpr constant type min = metal::numeric_limits<type>::min(); \
|
| 324 |
+
static constexpr constant type finite_max = \
|
| 325 |
+
metal::numeric_limits<type>::max(); \
|
| 326 |
+
static constexpr constant type finite_min = \
|
| 327 |
+
metal::numeric_limits<type>::min(); \
|
| 328 |
+
};
|
| 329 |
+
|
| 330 |
+
instantiate_default_limit(uint8_t);
|
| 331 |
+
instantiate_default_limit(uint16_t);
|
| 332 |
+
instantiate_default_limit(uint32_t);
|
| 333 |
+
instantiate_default_limit(uint64_t);
|
| 334 |
+
instantiate_default_limit(int8_t);
|
| 335 |
+
instantiate_default_limit(int16_t);
|
| 336 |
+
instantiate_default_limit(int32_t);
|
| 337 |
+
instantiate_default_limit(int64_t);
|
| 338 |
+
|
| 339 |
+
#define instantiate_float_limit(type) \
|
| 340 |
+
template <> \
|
| 341 |
+
struct Limits<type> { \
|
| 342 |
+
static constexpr constant type max = \
|
| 343 |
+
metal::numeric_limits<type>::infinity(); \
|
| 344 |
+
static constexpr constant type min = \
|
| 345 |
+
-metal::numeric_limits<type>::infinity(); \
|
| 346 |
+
static constexpr constant type finite_max = \
|
| 347 |
+
metal::numeric_limits<type>::max(); \
|
| 348 |
+
static constexpr constant type finite_min = \
|
| 349 |
+
-metal::numeric_limits<type>::max(); \
|
| 350 |
+
};
|
| 351 |
+
|
| 352 |
+
instantiate_float_limit(half);
|
| 353 |
+
instantiate_float_limit(float);
|
| 354 |
+
instantiate_float_limit(bfloat16_t);
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
// ============ "mlx/backend/metal/kernels/steel/attn/loader.h"
|
| 358 |
+
|
| 359 |
+
template <
|
| 360 |
+
typename T,
|
| 361 |
+
short BROWS,
|
| 362 |
+
short BCOLS,
|
| 363 |
+
short dst_ld,
|
| 364 |
+
short reduction_dim,
|
| 365 |
+
short tgp_size,
|
| 366 |
+
short alignment = 1,
|
| 367 |
+
short n_reads = (BCOLS * BROWS) / (tgp_size),
|
| 368 |
+
short TCOLS = BCOLS / n_reads,
|
| 369 |
+
short TROWS = tgp_size / TCOLS>
|
| 370 |
+
struct BlockLoader {
|
| 371 |
+
STEEL_CONST short n_rows = (BROWS + TROWS - 1) / TROWS;
|
| 372 |
+
STEEL_CONST short vec_size = n_reads;
|
| 373 |
+
|
| 374 |
+
// Leading dimension for src
|
| 375 |
+
const int src_ld;
|
| 376 |
+
const int tile_stride;
|
| 377 |
+
|
| 378 |
+
// Thread location indices
|
| 379 |
+
const short thread_idx;
|
| 380 |
+
const short bi;
|
| 381 |
+
const short bj;
|
| 382 |
+
|
| 383 |
+
// threadgroup and device memory
|
| 384 |
+
threadgroup T* dst;
|
| 385 |
+
const device T* src;
|
| 386 |
+
|
| 387 |
+
struct alignas(alignment * sizeof(T)) ReadVector {
|
| 388 |
+
uint8_t v[sizeof(T) * vec_size];
|
| 389 |
+
};
|
| 390 |
+
|
| 391 |
+
/* Constructor */
|
| 392 |
+
METAL_FUNC BlockLoader(
|
| 393 |
+
const device T* src_,
|
| 394 |
+
const int src_ld_,
|
| 395 |
+
threadgroup T* dst_,
|
| 396 |
+
ushort simd_group_id [[simdgroup_index_in_threadgroup]],
|
| 397 |
+
ushort simd_lane_id [[thread_index_in_simdgroup]])
|
| 398 |
+
: src_ld(src_ld_),
|
| 399 |
+
tile_stride(reduction_dim ? BCOLS : BROWS * src_ld),
|
| 400 |
+
thread_idx(simd_group_id * 32 + simd_lane_id),
|
| 401 |
+
bi(thread_idx / TCOLS),
|
| 402 |
+
bj(vec_size * (thread_idx % TCOLS)),
|
| 403 |
+
dst(dst_ + bi * dst_ld + bj),
|
| 404 |
+
src(src_ + bi * src_ld + bj) {}
|
| 405 |
+
|
| 406 |
+
/* Apply operation to threadgroup without bound checking */
|
| 407 |
+
template <typename UnaryOp>
|
| 408 |
+
METAL_FUNC void apply_inplace_op(thread const UnaryOp& op) const {
|
| 409 |
+
STEEL_PRAGMA_UNROLL
|
| 410 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 411 |
+
STEEL_PRAGMA_UNROLL
|
| 412 |
+
for (short j = 0; j < vec_size; j++) {
|
| 413 |
+
dst[i * dst_ld + j] = op.apply(dst[i * dst_ld + j]);
|
| 414 |
+
}
|
| 415 |
+
}
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
/* Load from device memory into threadgroup memory - without bound checking */
|
| 419 |
+
METAL_FUNC void load_unsafe() const {
|
| 420 |
+
STEEL_PRAGMA_UNROLL
|
| 421 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 422 |
+
*((threadgroup ReadVector*)(&dst[i * dst_ld])) =
|
| 423 |
+
*((const device ReadVector*)(&src[i * src_ld]));
|
| 424 |
+
}
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
/* Load from device memory into threadgroup memory - with bound checking */
|
| 428 |
+
METAL_FUNC void load_safe(short2 src_tile_dim) const {
|
| 429 |
+
src_tile_dim = src_tile_dim - short2(bj, bi);
|
| 430 |
+
|
| 431 |
+
// Skip loading if thread has no valid reads
|
| 432 |
+
if (src_tile_dim.x <= 0 || src_tile_dim.y <= 0) {
|
| 433 |
+
STEEL_PRAGMA_UNROLL
|
| 434 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 435 |
+
STEEL_PRAGMA_UNROLL
|
| 436 |
+
for (short j = 0; j < vec_size; j++) {
|
| 437 |
+
dst[i * dst_ld + j] = T(0);
|
| 438 |
+
}
|
| 439 |
+
}
|
| 440 |
+
return;
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
// Use fast thread memory for bound checks
|
| 444 |
+
bool tmp_idx[vec_size];
|
| 445 |
+
T tmp_val[vec_size];
|
| 446 |
+
|
| 447 |
+
STEEL_PRAGMA_UNROLL
|
| 448 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 449 |
+
// Make sure tmp_idx only contains valid indices
|
| 450 |
+
STEEL_PRAGMA_UNROLL
|
| 451 |
+
for (short j = 0; j < vec_size; j++) {
|
| 452 |
+
tmp_idx[j] = (i < src_tile_dim.y) && (j < src_tile_dim.x);
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
// Read valid indices into tmp_val
|
| 456 |
+
STEEL_PRAGMA_UNROLL
|
| 457 |
+
for (short j = 0; j < vec_size; j++) {
|
| 458 |
+
tmp_val[j] = src[(tmp_idx[j] ? i * src_ld + j : 0)];
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
// Zero out uneeded values
|
| 462 |
+
STEEL_PRAGMA_UNROLL
|
| 463 |
+
for (short j = 0; j < vec_size; j++) {
|
| 464 |
+
tmp_val[j] = tmp_idx[j] ? tmp_val[j] : T(0);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
// Copy values to threadgroup memory
|
| 468 |
+
STEEL_PRAGMA_UNROLL
|
| 469 |
+
for (short j = 0; j < vec_size; j++) {
|
| 470 |
+
dst[i * dst_ld + j] = tmp_val[j];
|
| 471 |
+
}
|
| 472 |
+
}
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
/* Iteration helper */
|
| 476 |
+
METAL_FUNC void next() {
|
| 477 |
+
src += tile_stride;
|
| 478 |
+
}
|
| 479 |
+
};
|
| 480 |
+
|
| 481 |
+
template <int R, int C>
|
| 482 |
+
struct CShape {
|
| 483 |
+
STEEL_CONST int kRows = R;
|
| 484 |
+
STEEL_CONST int kCols = C;
|
| 485 |
+
};
|
| 486 |
+
|
| 487 |
+
template <
|
| 488 |
+
typename T,
|
| 489 |
+
short BROWS,
|
| 490 |
+
short BCOLS,
|
| 491 |
+
short kDstStrRow,
|
| 492 |
+
short kDstStrCol,
|
| 493 |
+
short reduction_dim,
|
| 494 |
+
short tgp_size,
|
| 495 |
+
short n_reads = (BCOLS * BROWS) / (tgp_size),
|
| 496 |
+
short TCOLS = BCOLS / n_reads,
|
| 497 |
+
short TROWS = tgp_size / TCOLS>
|
| 498 |
+
struct BlockLoaderT {
|
| 499 |
+
STEEL_CONST short n_rows = (BROWS + TROWS - 1) / TROWS;
|
| 500 |
+
STEEL_CONST short vec_size = n_reads;
|
| 501 |
+
|
| 502 |
+
// Leading dimension for src
|
| 503 |
+
const int src_ld;
|
| 504 |
+
const int tile_stride;
|
| 505 |
+
|
| 506 |
+
// Thread location indices
|
| 507 |
+
const short thread_idx;
|
| 508 |
+
const short bi;
|
| 509 |
+
const short bj;
|
| 510 |
+
|
| 511 |
+
// threadgroup and device memory
|
| 512 |
+
threadgroup T* dst;
|
| 513 |
+
const device T* src;
|
| 514 |
+
|
| 515 |
+
/* Constructor */
|
| 516 |
+
METAL_FUNC BlockLoaderT(
|
| 517 |
+
const device T* src_,
|
| 518 |
+
const int src_ld_,
|
| 519 |
+
threadgroup T* dst_,
|
| 520 |
+
ushort simd_group_id [[simdgroup_index_in_threadgroup]],
|
| 521 |
+
ushort simd_lane_id [[thread_index_in_simdgroup]])
|
| 522 |
+
: src_ld(src_ld_),
|
| 523 |
+
tile_stride(reduction_dim ? BCOLS : BROWS * src_ld),
|
| 524 |
+
thread_idx(simd_group_id * 32 + simd_lane_id),
|
| 525 |
+
bi(thread_idx / TCOLS),
|
| 526 |
+
bj(vec_size * (thread_idx % TCOLS)),
|
| 527 |
+
dst(dst_ + bi * kDstStrRow + bj * kDstStrCol),
|
| 528 |
+
src(src_ + bi * src_ld + bj) {}
|
| 529 |
+
|
| 530 |
+
/* Apply operation to threadgroup without bound checking */
|
| 531 |
+
template <typename UnaryOp>
|
| 532 |
+
METAL_FUNC void apply_inplace_op(thread const UnaryOp& op) const {
|
| 533 |
+
STEEL_PRAGMA_UNROLL
|
| 534 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 535 |
+
STEEL_PRAGMA_UNROLL
|
| 536 |
+
for (short j = 0; j < vec_size; j++) {
|
| 537 |
+
dst[i * kDstStrRow + j * kDstStrCol] =
|
| 538 |
+
op.apply(dst[i * kDstStrRow + j * kDstStrCol]);
|
| 539 |
+
}
|
| 540 |
+
}
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
/* Load from device memory into threadgroup memory - without bound checking */
|
| 544 |
+
METAL_FUNC void load_unsafe() const {
|
| 545 |
+
STEEL_PRAGMA_UNROLL
|
| 546 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 547 |
+
STEEL_PRAGMA_UNROLL
|
| 548 |
+
for (short j = 0; j < vec_size; j++) {
|
| 549 |
+
dst[i * kDstStrRow + j * kDstStrCol] = src[i * src_ld + j];
|
| 550 |
+
}
|
| 551 |
+
}
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
/* Load from device memory into threadgroup memory - with bound checking */
|
| 555 |
+
METAL_FUNC void load_safe(short2 src_tile_dim) const {
|
| 556 |
+
src_tile_dim = src_tile_dim - short2(bj, bi);
|
| 557 |
+
|
| 558 |
+
// Skip loading if thread has no valid reads
|
| 559 |
+
if (src_tile_dim.x <= 0 || src_tile_dim.y <= 0) {
|
| 560 |
+
STEEL_PRAGMA_UNROLL
|
| 561 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 562 |
+
STEEL_PRAGMA_UNROLL
|
| 563 |
+
for (short j = 0; j < vec_size; j++) {
|
| 564 |
+
dst[i * kDstStrRow + j * kDstStrCol] = T(0);
|
| 565 |
+
}
|
| 566 |
+
}
|
| 567 |
+
return;
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
// Use fast thread memory for bound checks
|
| 571 |
+
bool tmp_idx[vec_size];
|
| 572 |
+
T tmp_val[vec_size];
|
| 573 |
+
|
| 574 |
+
STEEL_PRAGMA_UNROLL
|
| 575 |
+
for (short i = 0; i < BROWS; i += TROWS) {
|
| 576 |
+
// Make sure tmp_idx only contains valid indices
|
| 577 |
+
STEEL_PRAGMA_UNROLL
|
| 578 |
+
for (short j = 0; j < vec_size; j++) {
|
| 579 |
+
tmp_idx[j] = (i < src_tile_dim.y) && (j < src_tile_dim.x);
|
| 580 |
+
}
|
| 581 |
+
|
| 582 |
+
// Read valid indices into tmp_val
|
| 583 |
+
STEEL_PRAGMA_UNROLL
|
| 584 |
+
for (short j = 0; j < vec_size; j++) {
|
| 585 |
+
tmp_val[j] = src[(tmp_idx[j] ? i * src_ld + j : 0)];
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
+
// Zero out uneeded values
|
| 589 |
+
STEEL_PRAGMA_UNROLL
|
| 590 |
+
for (short j = 0; j < vec_size; j++) {
|
| 591 |
+
tmp_val[j] = tmp_idx[j] ? tmp_val[j] : T(0);
|
| 592 |
+
}
|
| 593 |
+
|
| 594 |
+
// Copy values to threadgroup memory
|
| 595 |
+
STEEL_PRAGMA_UNROLL
|
| 596 |
+
for (short j = 0; j < vec_size; j++) {
|
| 597 |
+
dst[i * kDstStrRow + j * kDstStrCol] = tmp_val[j];
|
| 598 |
+
}
|
| 599 |
+
}
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
/* Iteration helper */
|
| 603 |
+
METAL_FUNC void next() {
|
| 604 |
+
src += tile_stride;
|
| 605 |
+
}
|
| 606 |
+
};
|
| 607 |
+
|
| 608 |
+
// ============ "mlx/backend/metal/kernels/steel/utils/type_traits.h"
|
| 609 |
+
|
| 610 |
+
template <typename... Ts>
|
| 611 |
+
struct make_void {
|
| 612 |
+
typedef void type;
|
| 613 |
+
};
|
| 614 |
+
|
| 615 |
+
template <typename... Ts>
|
| 616 |
+
using void_t = typename make_void<Ts...>::type;
|
| 617 |
+
|
| 618 |
+
template <typename T>
|
| 619 |
+
struct pointer_element {};
|
| 620 |
+
|
| 621 |
+
template <typename T>
|
| 622 |
+
struct pointer_element<thread T*> {
|
| 623 |
+
using type = remove_cv_t<T>;
|
| 624 |
+
};
|
| 625 |
+
template <typename T>
|
| 626 |
+
struct pointer_element<device T*> {
|
| 627 |
+
using type = remove_cv_t<T>;
|
| 628 |
+
};
|
| 629 |
+
template <typename T>
|
| 630 |
+
struct pointer_element<constant T*> {
|
| 631 |
+
using type = remove_cv_t<T>;
|
| 632 |
+
};
|
| 633 |
+
template <typename T>
|
| 634 |
+
struct pointer_element<threadgroup T*> {
|
| 635 |
+
using type = remove_cv_t<T>;
|
| 636 |
+
};
|
| 637 |
+
|
| 638 |
+
template <typename T>
|
| 639 |
+
using pointer_element_t = typename pointer_element<remove_cv_t<T>>::type;
|
| 640 |
+
|
| 641 |
+
// ============ "mlx/backend/metal/kernels/steel/utils/integral_constant.h"
|
| 642 |
+
|
| 643 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 644 |
+
// Integral constant with casting
|
| 645 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 646 |
+
|
| 647 |
+
template <int val>
|
| 648 |
+
using Int = integral_constant<int, val>;
|
| 649 |
+
|
| 650 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 651 |
+
// Binary Operators on Integral constants
|
| 652 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 653 |
+
|
| 654 |
+
#define integral_const_binop(__op__, __operator__) \
|
| 655 |
+
template <typename T, T tv, typename U, U uv> \
|
| 656 |
+
METAL_FUNC constexpr auto __operator__( \
|
| 657 |
+
integral_constant<T, tv>, integral_constant<U, uv>) { \
|
| 658 |
+
constexpr auto res = tv __op__ uv; \
|
| 659 |
+
return integral_constant<decltype(res), res>{}; \
|
| 660 |
+
}
|
| 661 |
+
|
| 662 |
+
integral_const_binop(+, operator+);
|
| 663 |
+
integral_const_binop(-, operator-);
|
| 664 |
+
integral_const_binop(*, operator*);
|
| 665 |
+
integral_const_binop(/, operator/);
|
| 666 |
+
|
| 667 |
+
integral_const_binop(==, operator==);
|
| 668 |
+
integral_const_binop(!=, operator!=);
|
| 669 |
+
integral_const_binop(<, operator<);
|
| 670 |
+
integral_const_binop(>, operator>);
|
| 671 |
+
integral_const_binop(<=, operator<=);
|
| 672 |
+
integral_const_binop(>=, operator>=);
|
| 673 |
+
|
| 674 |
+
integral_const_binop(&&, operator&&);
|
| 675 |
+
integral_const_binop(||, operator||);
|
| 676 |
+
|
| 677 |
+
#undef integral_const_binop
|
| 678 |
+
|
| 679 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 680 |
+
// Reduction operators
|
| 681 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 682 |
+
|
| 683 |
+
template <typename T>
|
| 684 |
+
METAL_FUNC constexpr T sum(T x) {
|
| 685 |
+
return x;
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
template <typename T, typename... Us>
|
| 689 |
+
METAL_FUNC constexpr auto sum(T x, Us... us) {
|
| 690 |
+
return x + sum(us...);
|
| 691 |
+
}
|
| 692 |
+
|
| 693 |
+
// ============ "mlx/backend/metal/kernels/steel/gemm/transforms.h"
|
| 694 |
+
|
| 695 |
+
template <typename OutT, typename InT>
|
| 696 |
+
struct TransformNone {
|
| 697 |
+
static METAL_FUNC OutT apply(InT x) {
|
| 698 |
+
return static_cast<OutT>(x);
|
| 699 |
+
}
|
| 700 |
+
|
| 701 |
+
static METAL_FUNC OutT apply(InT x, OutT) {
|
| 702 |
+
return static_cast<OutT>(x);
|
| 703 |
+
}
|
| 704 |
+
};
|
| 705 |
+
|
| 706 |
+
template <typename OutT, typename InT>
|
| 707 |
+
struct TransformAdd {
|
| 708 |
+
TransformAdd(const float, const float) {}
|
| 709 |
+
|
| 710 |
+
static METAL_FUNC OutT apply(InT x) {
|
| 711 |
+
return static_cast<OutT>(x);
|
| 712 |
+
}
|
| 713 |
+
|
| 714 |
+
static METAL_FUNC OutT apply(InT x, OutT c) {
|
| 715 |
+
return static_cast<OutT>(x) + c;
|
| 716 |
+
}
|
| 717 |
+
};
|
| 718 |
+
|
| 719 |
+
template <typename OutT, typename InT>
|
| 720 |
+
struct TransformAxpby {
|
| 721 |
+
const float alpha;
|
| 722 |
+
const float beta;
|
| 723 |
+
|
| 724 |
+
TransformAxpby(const float alpha_, const float beta_)
|
| 725 |
+
: alpha(alpha_), beta(beta_) {}
|
| 726 |
+
|
| 727 |
+
static METAL_FUNC OutT apply(InT x) {
|
| 728 |
+
return static_cast<OutT>(x);
|
| 729 |
+
}
|
| 730 |
+
|
| 731 |
+
METAL_FUNC OutT apply(InT x, OutT c) const {
|
| 732 |
+
return static_cast<OutT>(x * alpha + (beta * c));
|
| 733 |
+
}
|
| 734 |
+
};
|
| 735 |
+
|
| 736 |
+
template <typename T>
|
| 737 |
+
struct AccumHelper {
|
| 738 |
+
typedef float accum_type;
|
| 739 |
+
};
|
| 740 |
+
|
| 741 |
+
struct BlockSwizzle {
|
| 742 |
+
static METAL_FUNC int2
|
| 743 |
+
swizzle(uint3 tid [[threadgroup_position_in_grid]], const int swizzle_log) {
|
| 744 |
+
const int tid_x = (tid.x) >> swizzle_log;
|
| 745 |
+
const int tid_y =
|
| 746 |
+
((tid.y) << swizzle_log) + ((tid.x) & ((1 << swizzle_log) - 1));
|
| 747 |
+
return int2(tid_x, tid_y);
|
| 748 |
+
}
|
| 749 |
+
};
|
| 750 |
+
|
| 751 |
+
// ============ "mlx/backend/metal/kernels/steel/attn/mma.h"
|
| 752 |
+
|
| 753 |
+
template <typename RInt, typename CInt>
|
| 754 |
+
struct Shape2D {
|
| 755 |
+
RInt r;
|
| 756 |
+
CInt c;
|
| 757 |
+
|
| 758 |
+
Shape2D(RInt r_, CInt c_) : r(r_), c(c_) {}
|
| 759 |
+
};
|
| 760 |
+
|
| 761 |
+
template <typename Shape, typename Layout>
|
| 762 |
+
struct Layout2D {
|
| 763 |
+
Shape shape;
|
| 764 |
+
Layout layout;
|
| 765 |
+
};
|
| 766 |
+
|
| 767 |
+
template <typename T, int kFragRows_, int kFragCols_>
|
| 768 |
+
struct BaseMMAFrag {
|
| 769 |
+
static_assert(
|
| 770 |
+
kFragRows_ == 8,
|
| 771 |
+
"Only 8 x 8 fragment matrices are currently supported");
|
| 772 |
+
static_assert(
|
| 773 |
+
kFragCols_ == 8,
|
| 774 |
+
"Only 8 x 8 fragment matrices are currently supported");
|
| 775 |
+
};
|
| 776 |
+
|
| 777 |
+
template <typename T>
|
| 778 |
+
struct BaseMMAFrag<T, 8, 8> {
|
| 779 |
+
STEEL_CONST int kFragRows = 8;
|
| 780 |
+
STEEL_CONST int kFragCols = 8;
|
| 781 |
+
|
| 782 |
+
STEEL_CONST int kElemsPerFrag = (kFragRows * kFragCols) / 32;
|
| 783 |
+
|
| 784 |
+
STEEL_CONST int kElemRows = 1;
|
| 785 |
+
STEEL_CONST int kElemCols = 2;
|
| 786 |
+
|
| 787 |
+
static_assert(
|
| 788 |
+
kElemRows * kElemCols == kElemsPerFrag,
|
| 789 |
+
"MMAFrag shape is not consistent with MMAFrag size");
|
| 790 |
+
|
| 791 |
+
typedef metal::simdgroup_matrix<T, kFragRows, kFragCols> mat_type;
|
| 792 |
+
typedef metal::vec<T, kElemsPerFrag> frag_type;
|
| 793 |
+
typedef metal::vec<T, kElemRows> row_frag_type;
|
| 794 |
+
typedef metal::vec<T, kElemCols> col_frag_type;
|
| 795 |
+
|
| 796 |
+
template <typename U>
|
| 797 |
+
using dtype_mat_t = typename metal::simdgroup_matrix<U, kFragRows, kFragCols>;
|
| 798 |
+
|
| 799 |
+
template <typename U>
|
| 800 |
+
using dtype_frag_t = typename metal::vec<U, kElemsPerFrag>;
|
| 801 |
+
|
| 802 |
+
METAL_FUNC static constexpr short2 get_coord(ushort simd_lane_id
|
| 803 |
+
[[thread_index_in_simdgroup]]) {
|
| 804 |
+
const short qid = simd_lane_id / 4;
|
| 805 |
+
const short fm = (qid & 4) + ((simd_lane_id / 2) % 4);
|
| 806 |
+
const short fn = (qid & 2) * 2 + (simd_lane_id % 2) * 2;
|
| 807 |
+
return short2{fn, fm};
|
| 808 |
+
}
|
| 809 |
+
|
| 810 |
+
template <typename SrcPtrType, typename StrX, typename StrY>
|
| 811 |
+
METAL_FUNC static constexpr void
|
| 812 |
+
load(thread frag_type& dst, SrcPtrType src, StrX str_x, StrY str_y) {
|
| 813 |
+
STEEL_PRAGMA_UNROLL
|
| 814 |
+
for (short i = 0; i < kElemRows; i++) {
|
| 815 |
+
STEEL_PRAGMA_UNROLL
|
| 816 |
+
for (short j = 0; j < kElemCols; j++) {
|
| 817 |
+
dst[i * kElemCols + j] = static_cast<T>(src[i * str_x.value + j * str_y.value]);
|
| 818 |
+
}
|
| 819 |
+
}
|
| 820 |
+
}
|
| 821 |
+
|
| 822 |
+
template <
|
| 823 |
+
typename SrcPtrType,
|
| 824 |
+
typename StrX,
|
| 825 |
+
typename StrY,
|
| 826 |
+
typename LimX,
|
| 827 |
+
typename LimY,
|
| 828 |
+
typename OffX,
|
| 829 |
+
typename OffY>
|
| 830 |
+
METAL_FUNC static constexpr void load_safe(
|
| 831 |
+
thread frag_type& dst,
|
| 832 |
+
SrcPtrType src,
|
| 833 |
+
StrX str_x,
|
| 834 |
+
StrY str_y,
|
| 835 |
+
LimX lim_x,
|
| 836 |
+
LimY lim_y,
|
| 837 |
+
OffX off_x = Int<0>{},
|
| 838 |
+
OffY off_y = Int<0>{}) {
|
| 839 |
+
STEEL_PRAGMA_UNROLL
|
| 840 |
+
for (short i = 0; i < kElemRows; i++) {
|
| 841 |
+
STEEL_PRAGMA_UNROLL
|
| 842 |
+
for (short j = 0; j < kElemCols; j++) {
|
| 843 |
+
if ((off_x + i) < lim_x && (off_y + j) < lim_y) {
|
| 844 |
+
dst[i * kElemCols + j] =
|
| 845 |
+
static_cast<T>(src[(off_x + i) * str_x + (off_y + j) * str_y.value]);
|
| 846 |
+
} else {
|
| 847 |
+
dst[i * kElemCols + j] = T(0);
|
| 848 |
+
}
|
| 849 |
+
}
|
| 850 |
+
}
|
| 851 |
+
}
|
| 852 |
+
|
| 853 |
+
template <typename DstPtrType, typename StrX, typename StrY>
|
| 854 |
+
METAL_FUNC static constexpr void
|
| 855 |
+
store(const thread frag_type& src, DstPtrType dst, StrX str_x, StrY str_y) {
|
| 856 |
+
using U = pointer_element_t<DstPtrType>;
|
| 857 |
+
|
| 858 |
+
STEEL_PRAGMA_UNROLL
|
| 859 |
+
for (short i = 0; i < kElemRows; i++) {
|
| 860 |
+
STEEL_PRAGMA_UNROLL
|
| 861 |
+
for (short j = 0; j < kElemCols; j++) {
|
| 862 |
+
dst[i * str_x + j * str_y.value] = static_cast<U>(src[i * kElemCols + j]);
|
| 863 |
+
}
|
| 864 |
+
}
|
| 865 |
+
}
|
| 866 |
+
|
| 867 |
+
template <
|
| 868 |
+
typename DstPtrType,
|
| 869 |
+
typename StrX,
|
| 870 |
+
typename StrY,
|
| 871 |
+
typename LimX,
|
| 872 |
+
typename LimY,
|
| 873 |
+
typename OffX,
|
| 874 |
+
typename OffY>
|
| 875 |
+
METAL_FUNC static constexpr void store_safe(
|
| 876 |
+
const thread frag_type& src,
|
| 877 |
+
DstPtrType dst,
|
| 878 |
+
StrX str_x,
|
| 879 |
+
StrY str_y,
|
| 880 |
+
LimX lim_x,
|
| 881 |
+
LimY lim_y,
|
| 882 |
+
OffX off_x = Int<0>{},
|
| 883 |
+
OffY off_y = Int<0>{}) {
|
| 884 |
+
using U = pointer_element_t<DstPtrType>;
|
| 885 |
+
|
| 886 |
+
STEEL_PRAGMA_UNROLL
|
| 887 |
+
for (short i = 0; i < kElemRows; i++) {
|
| 888 |
+
STEEL_PRAGMA_UNROLL
|
| 889 |
+
for (short j = 0; j < kElemCols; j++) {
|
| 890 |
+
if ((off_x + i) < lim_x && (off_y + j) < lim_y) {
|
| 891 |
+
dst[(off_x + i) * str_x + (off_y + j) * str_y.value] =
|
| 892 |
+
static_cast<U>(src[i * kElemCols + j]);
|
| 893 |
+
}
|
| 894 |
+
}
|
| 895 |
+
}
|
| 896 |
+
}
|
| 897 |
+
|
| 898 |
+
template <typename Atype, typename Btype, typename Ctype>
|
| 899 |
+
METAL_FUNC static constexpr void mma(
|
| 900 |
+
thread frag_type& D,
|
| 901 |
+
thread dtype_frag_t<Atype>& A,
|
| 902 |
+
thread dtype_frag_t<Btype>& B,
|
| 903 |
+
thread dtype_frag_t<Ctype>& C) {
|
| 904 |
+
mat_type D_mat;
|
| 905 |
+
dtype_mat_t<Atype> A_mat;
|
| 906 |
+
dtype_mat_t<Btype> B_mat;
|
| 907 |
+
dtype_mat_t<Ctype> C_mat;
|
| 908 |
+
|
| 909 |
+
reinterpret_cast<thread dtype_frag_t<Atype>&>(A_mat.thread_elements()) = A;
|
| 910 |
+
reinterpret_cast<thread dtype_frag_t<Btype>&>(B_mat.thread_elements()) = B;
|
| 911 |
+
reinterpret_cast<thread dtype_frag_t<Ctype>&>(C_mat.thread_elements()) = C;
|
| 912 |
+
|
| 913 |
+
mma(D_mat, A_mat, B_mat, C_mat);
|
| 914 |
+
|
| 915 |
+
D = reinterpret_cast<thread frag_type&>(D_mat.thread_elements());
|
| 916 |
+
}
|
| 917 |
+
|
| 918 |
+
template <typename Atype, typename Btype, typename Ctype>
|
| 919 |
+
METAL_FUNC static constexpr void mma(
|
| 920 |
+
thread mat_type& D,
|
| 921 |
+
thread dtype_mat_t<Atype>& A,
|
| 922 |
+
thread dtype_mat_t<Btype>& B,
|
| 923 |
+
thread dtype_mat_t<Ctype>& C) {
|
| 924 |
+
simdgroup_multiply_accumulate(D, A, B, C);
|
| 925 |
+
}
|
| 926 |
+
|
| 927 |
+
template <typename Op>
|
| 928 |
+
METAL_FUNC static constexpr void row_reduce(
|
| 929 |
+
thread const frag_type& inp_vals,
|
| 930 |
+
thread T* reduced_vals) {
|
| 931 |
+
T thr_reduce = Op::apply(inp_vals.x, inp_vals.y);
|
| 932 |
+
|
| 933 |
+
T qgr_reduce = simd_shuffle_xor(thr_reduce, ushort(1));
|
| 934 |
+
qgr_reduce = Op::apply(thr_reduce, qgr_reduce);
|
| 935 |
+
|
| 936 |
+
T sgr_reduce = simd_shuffle_xor(qgr_reduce, ushort(8));
|
| 937 |
+
sgr_reduce = Op::apply(qgr_reduce, sgr_reduce);
|
| 938 |
+
|
| 939 |
+
reduced_vals[0] = Op::apply(reduced_vals[0], sgr_reduce);
|
| 940 |
+
}
|
| 941 |
+
|
| 942 |
+
template <typename Op>
|
| 943 |
+
METAL_FUNC static constexpr void row_bin_op(
|
| 944 |
+
thread frag_type& inp_vals,
|
| 945 |
+
thread T* row_vals) {
|
| 946 |
+
STEEL_PRAGMA_UNROLL
|
| 947 |
+
for (short i = 0; i < kElemRows; i++) {
|
| 948 |
+
STEEL_PRAGMA_UNROLL
|
| 949 |
+
for (short j = 0; j < kElemCols; j++) {
|
| 950 |
+
inp_vals[i * kElemCols + j] =
|
| 951 |
+
Op::apply(inp_vals[i * kElemCols + j], row_vals[i]);
|
| 952 |
+
}
|
| 953 |
+
}
|
| 954 |
+
}
|
| 955 |
+
};
|
| 956 |
+
|
| 957 |
+
template <
|
| 958 |
+
typename T,
|
| 959 |
+
int kTileRows_,
|
| 960 |
+
int kTileCols_,
|
| 961 |
+
class MMAFrag_ = BaseMMAFrag<T, 8, 8>>
|
| 962 |
+
struct MMATile {
|
| 963 |
+
using MMAFrag_t = MMAFrag_;
|
| 964 |
+
using elem_type = T;
|
| 965 |
+
STEEL_CONST int kFragRows = MMAFrag_t::kFragRows;
|
| 966 |
+
STEEL_CONST int kFragCols = MMAFrag_t::kFragCols;
|
| 967 |
+
STEEL_CONST int kElemsPerFrag = MMAFrag_t::kElemsPerFrag;
|
| 968 |
+
|
| 969 |
+
STEEL_CONST int kTileRows = kTileRows_;
|
| 970 |
+
STEEL_CONST int kTileCols = kTileCols_;
|
| 971 |
+
|
| 972 |
+
STEEL_CONST int kRows = kTileRows * kFragRows;
|
| 973 |
+
STEEL_CONST int kCols = kTileCols * kFragCols;
|
| 974 |
+
|
| 975 |
+
STEEL_CONST int kNumFrags = kTileRows * kTileCols;
|
| 976 |
+
STEEL_CONST int kElemsPerTile = kNumFrags * kElemsPerFrag;
|
| 977 |
+
|
| 978 |
+
STEEL_CONST int kRowsPerThread = kTileRows * MMAFrag_t::kElemRows;
|
| 979 |
+
STEEL_CONST int kColsPerThread = kTileCols * MMAFrag_t::kElemCols;
|
| 980 |
+
|
| 981 |
+
typedef typename MMAFrag_t::mat_type mat_type;
|
| 982 |
+
typedef typename MMAFrag_t::frag_type frag_type;
|
| 983 |
+
|
| 984 |
+
frag_type val_frags[kNumFrags]; // = {frag_type(0)};
|
| 985 |
+
|
| 986 |
+
METAL_FUNC MMATile() thread {}
|
| 987 |
+
|
| 988 |
+
METAL_FUNC constexpr void clear() {
|
| 989 |
+
STEEL_PRAGMA_UNROLL
|
| 990 |
+
for (short i = 0; i < kNumFrags; ++i) {
|
| 991 |
+
val_frags[i] = frag_type(0);
|
| 992 |
+
}
|
| 993 |
+
}
|
| 994 |
+
|
| 995 |
+
METAL_FUNC constexpr thread frag_type& frag_at(const short i, const short j) {
|
| 996 |
+
return val_frags[i * kTileCols + j];
|
| 997 |
+
}
|
| 998 |
+
|
| 999 |
+
METAL_FUNC constexpr const thread frag_type& frag_at(
|
| 1000 |
+
const short i,
|
| 1001 |
+
const short j) const {
|
| 1002 |
+
return val_frags[i * kTileCols + j];
|
| 1003 |
+
}
|
| 1004 |
+
|
| 1005 |
+
METAL_FUNC mat_type mat_at(const short i, const short j) {
|
| 1006 |
+
mat_type val_mat;
|
| 1007 |
+
STEEL_PRAGMA_UNROLL
|
| 1008 |
+
for (short ii = 0; ii < kElemsPerFrag; ++ii) {
|
| 1009 |
+
val_mat.thread_elements()[ii] = frag_at(i, j)[ii];
|
| 1010 |
+
}
|
| 1011 |
+
return val_mat;
|
| 1012 |
+
}
|
| 1013 |
+
|
| 1014 |
+
METAL_FUNC thread elem_type* elems() {
|
| 1015 |
+
return reinterpret_cast<thread elem_type*>(val_frags);
|
| 1016 |
+
}
|
| 1017 |
+
|
| 1018 |
+
METAL_FUNC const thread elem_type* elems() const {
|
| 1019 |
+
return reinterpret_cast<const thread elem_type*>(val_frags);
|
| 1020 |
+
}
|
| 1021 |
+
|
| 1022 |
+
template <typename Op>
|
| 1023 |
+
METAL_FUNC void row_reduce(thread T vals[kRowsPerThread]) const {
|
| 1024 |
+
STEEL_PRAGMA_UNROLL
|
| 1025 |
+
for (short i = 0; i < kTileRows; ++i) {
|
| 1026 |
+
STEEL_PRAGMA_UNROLL
|
| 1027 |
+
for (short j = 0; j < kTileCols; ++j) {
|
| 1028 |
+
MMAFrag_t::template row_reduce<Op>(
|
| 1029 |
+
frag_at(i, j), &vals[i * MMAFrag_t::kElemRows]);
|
| 1030 |
+
}
|
| 1031 |
+
}
|
| 1032 |
+
}
|
| 1033 |
+
|
| 1034 |
+
template <typename Op>
|
| 1035 |
+
METAL_FUNC void row_bin_op(thread T vals[kRowsPerThread]) {
|
| 1036 |
+
STEEL_PRAGMA_UNROLL
|
| 1037 |
+
for (short i = 0; i < kTileRows; ++i) {
|
| 1038 |
+
STEEL_PRAGMA_UNROLL
|
| 1039 |
+
for (short j = 0; j < kTileCols; ++j) {
|
| 1040 |
+
MMAFrag_t::template row_bin_op<Op>(
|
| 1041 |
+
frag_at(i, j), &vals[i * MMAFrag_t::kElemRows]);
|
| 1042 |
+
}
|
| 1043 |
+
}
|
| 1044 |
+
}
|
| 1045 |
+
|
| 1046 |
+
template <typename U, int w_x, int w_y, int str_x, int str_y>
|
| 1047 |
+
METAL_FUNC void load(const threadgroup U* src) {
|
| 1048 |
+
STEEL_PRAGMA_UNROLL
|
| 1049 |
+
for (short i = 0; i < kTileRows; ++i) {
|
| 1050 |
+
STEEL_PRAGMA_UNROLL
|
| 1051 |
+
for (short j = 0; j < kTileCols; ++j) {
|
| 1052 |
+
MMAFrag_t::load(
|
| 1053 |
+
frag_at(i, j),
|
| 1054 |
+
&(
|
| 1055 |
+
src[(i * kFragRows) * w_x * str_x +
|
| 1056 |
+
(j * kFragCols) * w_y * str_y]),
|
| 1057 |
+
Int<str_x>{},
|
| 1058 |
+
Int<str_y>{});
|
| 1059 |
+
}
|
| 1060 |
+
}
|
| 1061 |
+
}
|
| 1062 |
+
|
| 1063 |
+
template <typename U, int w_x, int w_y, int str_x, int str_y>
|
| 1064 |
+
METAL_FUNC void store(threadgroup U* dst) const {
|
| 1065 |
+
STEEL_PRAGMA_UNROLL
|
| 1066 |
+
for (short i = 0; i < kTileRows; ++i) {
|
| 1067 |
+
STEEL_PRAGMA_UNROLL
|
| 1068 |
+
for (short j = 0; j < kTileCols; ++j) {
|
| 1069 |
+
MMAFrag_t::store(
|
| 1070 |
+
frag_at(i, j),
|
| 1071 |
+
&(
|
| 1072 |
+
dst[(i * kFragRows) * w_x * str_x +
|
| 1073 |
+
(j * kFragCols) * w_y * str_y]),
|
| 1074 |
+
Int<str_x>{},
|
| 1075 |
+
Int<str_y>{});
|
| 1076 |
+
}
|
| 1077 |
+
}
|
| 1078 |
+
}
|
| 1079 |
+
|
| 1080 |
+
template <typename U, int w_x, int w_y>
|
| 1081 |
+
METAL_FUNC void load(const device U* src, const int ld) {
|
| 1082 |
+
STEEL_PRAGMA_UNROLL
|
| 1083 |
+
for (short i = 0; i < kTileRows; ++i) {
|
| 1084 |
+
STEEL_PRAGMA_UNROLL
|
| 1085 |
+
for (short j = 0; j < kTileCols; ++j) {
|
| 1086 |
+
MMAFrag_t::load(
|
| 1087 |
+
frag_at(i, j),
|
| 1088 |
+
&(src[(i * kFragRows) * w_x * ld + (j * kFragCols) * w_y]),
|
| 1089 |
+
ld,
|
| 1090 |
+
Int<1>{});
|
| 1091 |
+
}
|
| 1092 |
+
}
|
| 1093 |
+
}
|
| 1094 |
+
|
| 1095 |
+
template <typename U, int w_x, int w_y>
|
| 1096 |
+
METAL_FUNC void store(device U* dst, const int ld) const {
|
| 1097 |
+
STEEL_PRAGMA_UNROLL
|
| 1098 |
+
for (short i = 0; i < kTileRows; ++i) {
|
| 1099 |
+
STEEL_PRAGMA_UNROLL
|
| 1100 |
+
for (short j = 0; j < kTileCols; ++j) {
|
| 1101 |
+
MMAFrag_t::store(
|
| 1102 |
+
frag_at(i, j),
|
| 1103 |
+
&(dst[(i * kFragRows) * w_x * ld + (j * kFragCols) * w_y]),
|
| 1104 |
+
ld,
|
| 1105 |
+
Int<1>{});
|
| 1106 |
+
}
|
| 1107 |
+
}
|
| 1108 |
+
}
|
| 1109 |
+
|
| 1110 |
+
template <typename U, int w_x, int w_y>
|
| 1111 |
+
METAL_FUNC void
|
| 1112 |
+
load_safe(const device U* src, const int ld, const short2 src_tile_dims) {
|
| 1113 |
+
STEEL_PRAGMA_UNROLL
|
| 1114 |
+
for (int i = 0; i < kTileRows; ++i) {
|
| 1115 |
+
STEEL_PRAGMA_UNROLL
|
| 1116 |
+
for (int j = 0; j < kTileCols; ++j) {
|
| 1117 |
+
MMAFrag_t::load_safe(
|
| 1118 |
+
frag_at(i, j),
|
| 1119 |
+
src,
|
| 1120 |
+
ld,
|
| 1121 |
+
Int<1>{},
|
| 1122 |
+
src_tile_dims.y,
|
| 1123 |
+
src_tile_dims.x,
|
| 1124 |
+
(i * kFragRows) * w_x,
|
| 1125 |
+
(j * kFragCols) * w_y);
|
| 1126 |
+
}
|
| 1127 |
+
}
|
| 1128 |
+
}
|
| 1129 |
+
|
| 1130 |
+
template <typename U, int w_x, int w_y>
|
| 1131 |
+
METAL_FUNC void
|
| 1132 |
+
store_safe(device U* dst, const int ld, const short2 dst_tile_dims) const {
|
| 1133 |
+
STEEL_PRAGMA_UNROLL
|
| 1134 |
+
for (int i = 0; i < kTileRows; ++i) {
|
| 1135 |
+
STEEL_PRAGMA_UNROLL
|
| 1136 |
+
for (int j = 0; j < kTileCols; ++j) {
|
| 1137 |
+
MMAFrag_t::store_safe(
|
| 1138 |
+
frag_at(i, j),
|
| 1139 |
+
dst,
|
| 1140 |
+
ld,
|
| 1141 |
+
Int<1>{},
|
| 1142 |
+
dst_tile_dims.y,
|
| 1143 |
+
dst_tile_dims.x,
|
| 1144 |
+
(i * kFragRows) * w_x,
|
| 1145 |
+
(j * kFragCols) * w_y);
|
| 1146 |
+
}
|
| 1147 |
+
}
|
| 1148 |
+
}
|
| 1149 |
+
};
|
| 1150 |
+
|
| 1151 |
+
template <
|
| 1152 |
+
typename Dtype,
|
| 1153 |
+
typename Atype,
|
| 1154 |
+
typename Btype,
|
| 1155 |
+
typename Ctype,
|
| 1156 |
+
int M,
|
| 1157 |
+
int N,
|
| 1158 |
+
int K,
|
| 1159 |
+
class MMAFragD,
|
| 1160 |
+
class MMAFragA,
|
| 1161 |
+
class MMAFragB,
|
| 1162 |
+
class MMAFragC>
|
| 1163 |
+
METAL_FUNC void tile_matmad(
|
| 1164 |
+
thread MMATile<Dtype, M, N, MMAFragD>& D,
|
| 1165 |
+
thread MMATile<Atype, M, K, MMAFragA>& A,
|
| 1166 |
+
thread MMATile<Btype, K, N, MMAFragB>& B,
|
| 1167 |
+
thread MMATile<Ctype, M, N, MMAFragC>& C) {
|
| 1168 |
+
STEEL_PRAGMA_UNROLL
|
| 1169 |
+
for (short m = 0; m < M; ++m) {
|
| 1170 |
+
STEEL_PRAGMA_UNROLL
|
| 1171 |
+
for (short n = 0; n < N; ++n) {
|
| 1172 |
+
short m_serp = m; //(n % 2) ? (M - 1 - m) : m;
|
| 1173 |
+
short n_serp = (m % 2) ? (N - 1 - n) : n;
|
| 1174 |
+
|
| 1175 |
+
STEEL_PRAGMA_UNROLL
|
| 1176 |
+
for (short k = 0; k < K; ++k) {
|
| 1177 |
+
MMAFragD::mma(
|
| 1178 |
+
D.frag_at(m_serp, n_serp),
|
| 1179 |
+
A.frag_at(m_serp, k),
|
| 1180 |
+
B.frag_at(k, n_serp),
|
| 1181 |
+
C.frag_at(m_serp, n_serp));
|
| 1182 |
+
}
|
| 1183 |
+
}
|
| 1184 |
+
}
|
| 1185 |
+
}
|
| 1186 |
+
|
| 1187 |
+
template <
|
| 1188 |
+
typename T,
|
| 1189 |
+
typename U,
|
| 1190 |
+
int BM,
|
| 1191 |
+
int BN,
|
| 1192 |
+
int BK,
|
| 1193 |
+
int WM,
|
| 1194 |
+
int WN,
|
| 1195 |
+
bool transpose_a,
|
| 1196 |
+
bool transpose_b,
|
| 1197 |
+
short lda_tgp,
|
| 1198 |
+
short ldb_tgp,
|
| 1199 |
+
typename AccumType = float,
|
| 1200 |
+
typename Epilogue = TransformNone<U, AccumType>>
|
| 1201 |
+
struct BlockMMA {
|
| 1202 |
+
// MMAFrag size
|
| 1203 |
+
STEEL_CONST short kFragSize = 8;
|
| 1204 |
+
using MMAFrag_acc_t = BaseMMAFrag<AccumType, kFragSize, kFragSize>;
|
| 1205 |
+
|
| 1206 |
+
// Warp tile simdgroup matrix strides along M
|
| 1207 |
+
STEEL_CONST short TM_stride = kFragSize * WM;
|
| 1208 |
+
// Warp tile simdgroup matrix strides along M
|
| 1209 |
+
STEEL_CONST short TN_stride = kFragSize * WN;
|
| 1210 |
+
|
| 1211 |
+
// Warp tile size along M
|
| 1212 |
+
STEEL_CONST short TM = BM / TM_stride;
|
| 1213 |
+
// Warp tile size along N
|
| 1214 |
+
STEEL_CONST short TN = BN / TN_stride;
|
| 1215 |
+
|
| 1216 |
+
// Threadgroup A strides
|
| 1217 |
+
STEEL_CONST short A_str_m = transpose_a ? 1 : lda_tgp; // M
|
| 1218 |
+
STEEL_CONST short A_str_k = transpose_a ? lda_tgp : 1; // K
|
| 1219 |
+
|
| 1220 |
+
// Threadgroup B strides
|
| 1221 |
+
STEEL_CONST short B_str_k = transpose_b ? 1 : ldb_tgp; // K
|
| 1222 |
+
STEEL_CONST short B_str_n = transpose_b ? ldb_tgp : 1; // N
|
| 1223 |
+
|
| 1224 |
+
// Threadgroup strides along K
|
| 1225 |
+
STEEL_CONST short tile_stride_a = kFragSize * A_str_k;
|
| 1226 |
+
STEEL_CONST short tile_stride_b = kFragSize * B_str_k;
|
| 1227 |
+
|
| 1228 |
+
// Simdgroup matrices
|
| 1229 |
+
MMATile<AccumType, TM, 1, MMAFrag_acc_t> Atile;
|
| 1230 |
+
MMATile<AccumType, 1, TN, MMAFrag_acc_t> Btile;
|
| 1231 |
+
MMATile<AccumType, TM, TN, MMAFrag_acc_t> Ctile;
|
| 1232 |
+
|
| 1233 |
+
// Offsets within threadgroup
|
| 1234 |
+
short sm;
|
| 1235 |
+
short sn;
|
| 1236 |
+
|
| 1237 |
+
short As_offset;
|
| 1238 |
+
short Bs_offset;
|
| 1239 |
+
|
| 1240 |
+
/* Constructor */
|
| 1241 |
+
METAL_FUNC BlockMMA(
|
| 1242 |
+
ushort simd_group_id [[simdgroup_index_in_threadgroup]],
|
| 1243 |
+
ushort simd_lane_id [[thread_index_in_simdgroup]]) {
|
| 1244 |
+
// Determine thread position in simdgroup matrix
|
| 1245 |
+
short tm = kFragSize * (simd_group_id / WN);
|
| 1246 |
+
short tn = kFragSize * (simd_group_id % WN);
|
| 1247 |
+
|
| 1248 |
+
short2 simd_coord = MMAFrag_acc_t::get_coord(simd_lane_id);
|
| 1249 |
+
sm = simd_coord.y;
|
| 1250 |
+
sn = simd_coord.x;
|
| 1251 |
+
|
| 1252 |
+
// Determine thread and simdgroup offset
|
| 1253 |
+
As_offset = (tm + sm) * A_str_m + (sn)*A_str_k; // M, K
|
| 1254 |
+
Bs_offset = (sm)*B_str_k + (tn + sn) * B_str_n; // K, N
|
| 1255 |
+
|
| 1256 |
+
sm += tm;
|
| 1257 |
+
sn += tn;
|
| 1258 |
+
}
|
| 1259 |
+
|
| 1260 |
+
/* (BM, BK) X (BK, BN) multiply accumulate function */
|
| 1261 |
+
METAL_FUNC void mma(const threadgroup T* As, const threadgroup T* Bs) {
|
| 1262 |
+
// Adjust for simdgroup and thread location
|
| 1263 |
+
As += As_offset;
|
| 1264 |
+
Bs += Bs_offset;
|
| 1265 |
+
|
| 1266 |
+
// Iterate over BK in blocks of kFragSize
|
| 1267 |
+
STEEL_PRAGMA_UNROLL
|
| 1268 |
+
for (short kk = 0; kk < BK; kk += kFragSize) {
|
| 1269 |
+
simdgroup_barrier(mem_flags::mem_none);
|
| 1270 |
+
|
| 1271 |
+
Atile.template load<T, WM, 1, A_str_m, A_str_k>(As);
|
| 1272 |
+
|
| 1273 |
+
simdgroup_barrier(mem_flags::mem_none);
|
| 1274 |
+
|
| 1275 |
+
Btile.template load<T, 1, WN, B_str_k, B_str_n>(Bs);
|
| 1276 |
+
|
| 1277 |
+
simdgroup_barrier(mem_flags::mem_none);
|
| 1278 |
+
|
| 1279 |
+
tile_matmad(Ctile, Atile, Btile, Ctile);
|
| 1280 |
+
|
| 1281 |
+
// Progress to next simdgroup tile
|
| 1282 |
+
As += tile_stride_a;
|
| 1283 |
+
Bs += tile_stride_b;
|
| 1284 |
+
}
|
| 1285 |
+
}
|
| 1286 |
+
|
| 1287 |
+
/* Store results from simdgroup_matrix results into device memory */
|
| 1288 |
+
METAL_FUNC void store_result(device U* D, const int ldd) {
|
| 1289 |
+
// Apply epilogue
|
| 1290 |
+
STEEL_PRAGMA_UNROLL
|
| 1291 |
+
for (short i = 0; i < decltype(Ctile)::kElemsPerTile; i++) {
|
| 1292 |
+
Ctile.elems()[i] = Epilogue::apply(Ctile.elems()[i]);
|
| 1293 |
+
}
|
| 1294 |
+
|
| 1295 |
+
// Adjust for simdgroup and thread location
|
| 1296 |
+
D += sm * ldd + sn;
|
| 1297 |
+
|
| 1298 |
+
Ctile.template store<U, WM, WN>(D, ldd);
|
| 1299 |
+
}
|
| 1300 |
+
|
| 1301 |
+
METAL_FUNC void
|
| 1302 |
+
store_result_safe(device U* D, const int ldd, short2 dst_tile_dims) {
|
| 1303 |
+
// Apply epilogue
|
| 1304 |
+
STEEL_PRAGMA_UNROLL
|
| 1305 |
+
for (short i = 0; i < decltype(Ctile)::kElemsPerTile; i++) {
|
| 1306 |
+
Ctile.elems()[i] = Epilogue::apply(Ctile.elems()[i]);
|
| 1307 |
+
}
|
| 1308 |
+
|
| 1309 |
+
// Adjust for simdgroup and thread location
|
| 1310 |
+
D += sm * ldd + sn;
|
| 1311 |
+
dst_tile_dims -= short2(sn, sm);
|
| 1312 |
+
|
| 1313 |
+
if (dst_tile_dims.x <= 0 || dst_tile_dims.y <= 0)
|
| 1314 |
+
return;
|
| 1315 |
+
|
| 1316 |
+
Ctile.template store_safe<U, WM, WN>(D, ldd, dst_tile_dims);
|
| 1317 |
+
}
|
| 1318 |
+
|
| 1319 |
+
/* Apply epilogue */
|
| 1320 |
+
template <typename UnaryEpilogue>
|
| 1321 |
+
METAL_FUNC void apply_epilogue(thread const UnaryEpilogue& epilogue_op) {
|
| 1322 |
+
// Loop over all simdgroup tiles
|
| 1323 |
+
STEEL_PRAGMA_UNROLL
|
| 1324 |
+
for (short i = 0; i < decltype(Ctile)::kElemsPerTile; i++) {
|
| 1325 |
+
Ctile.elems()[i] = epilogue_op.apply(Ctile.elems()[i]);
|
| 1326 |
+
}
|
| 1327 |
+
}
|
| 1328 |
+
|
| 1329 |
+
/* Apply epilogue */
|
| 1330 |
+
template <typename BinaryEpilogue>
|
| 1331 |
+
METAL_FUNC void apply_epilogue(
|
| 1332 |
+
const device U* C,
|
| 1333 |
+
const int ldc,
|
| 1334 |
+
const int fdc,
|
| 1335 |
+
thread const BinaryEpilogue& epilogue_op) {
|
| 1336 |
+
// Adjust for simdgroup and thread location
|
| 1337 |
+
C += (sm)*ldc + (sn)*fdc;
|
| 1338 |
+
|
| 1339 |
+
// Loop over all simdgroup tiles
|
| 1340 |
+
STEEL_PRAGMA_UNROLL
|
| 1341 |
+
for (short i = 0; i < TM; i++) {
|
| 1342 |
+
STEEL_PRAGMA_UNROLL
|
| 1343 |
+
for (short j = 0; j < TN; j++) {
|
| 1344 |
+
// Get accumulated result and associated offset in C
|
| 1345 |
+
thread auto& accum = Ctile.frag_at(i, j);
|
| 1346 |
+
int offset_c = (i * TM_stride) * ldc + (j * TN_stride) * fdc;
|
| 1347 |
+
|
| 1348 |
+
// Apply epilogue
|
| 1349 |
+
STEEL_PRAGMA_UNROLL
|
| 1350 |
+
for (short k = 0; k < decltype(Ctile)::kElemsPerFrag; k++) {
|
| 1351 |
+
accum[k] = epilogue_op.apply(accum[k], C[offset_c + k * fdc]);
|
| 1352 |
+
}
|
| 1353 |
+
}
|
| 1354 |
+
}
|
| 1355 |
+
}
|
| 1356 |
+
|
| 1357 |
+
/* Apply epilogue */
|
| 1358 |
+
template <typename BinaryEpilogue>
|
| 1359 |
+
METAL_FUNC void apply_epilogue_safe(
|
| 1360 |
+
const device U* C,
|
| 1361 |
+
const int ldc,
|
| 1362 |
+
const int fdc,
|
| 1363 |
+
short2 dst_tile_dims,
|
| 1364 |
+
thread const BinaryEpilogue& epilogue_op) {
|
| 1365 |
+
// Adjust for simdgroup and thread location
|
| 1366 |
+
C += (sm)*ldc + (sn)*fdc;
|
| 1367 |
+
dst_tile_dims -= short2(sn, sm);
|
| 1368 |
+
|
| 1369 |
+
if (dst_tile_dims.x <= 0 || dst_tile_dims.y <= 0)
|
| 1370 |
+
return;
|
| 1371 |
+
|
| 1372 |
+
// Loop over all simdgroup tiles
|
| 1373 |
+
STEEL_PRAGMA_UNROLL
|
| 1374 |
+
for (short i = 0; i < TM; i++) {
|
| 1375 |
+
STEEL_PRAGMA_UNROLL
|
| 1376 |
+
for (short j = 0; j < TN; j++) {
|
| 1377 |
+
// Get accumulated result and associated offset in C
|
| 1378 |
+
thread auto& accum = Ctile.frag_at(i, j);
|
| 1379 |
+
int offset_c = (i * TM_stride) * ldc + (j * TN_stride) * fdc;
|
| 1380 |
+
|
| 1381 |
+
constexpr short kelems = decltype(Ctile)::kElemsPerFrag;
|
| 1382 |
+
|
| 1383 |
+
// Read C
|
| 1384 |
+
U c_elems[kelems] = {0};
|
| 1385 |
+
|
| 1386 |
+
STEEL_PRAGMA_UNROLL
|
| 1387 |
+
for (short k = 0; k < kelems; k++) {
|
| 1388 |
+
if ((j * TN_stride + k) < dst_tile_dims.x) {
|
| 1389 |
+
c_elems[k] = C[offset_c + k * fdc];
|
| 1390 |
+
}
|
| 1391 |
+
}
|
| 1392 |
+
|
| 1393 |
+
// Apply epilogue
|
| 1394 |
+
STEEL_PRAGMA_UNROLL
|
| 1395 |
+
for (short k = 0; k < kelems; k++) {
|
| 1396 |
+
accum[k] = epilogue_op.apply(accum[k], c_elems[k]);
|
| 1397 |
+
}
|
| 1398 |
+
}
|
| 1399 |
+
}
|
| 1400 |
+
}
|
| 1401 |
+
|
| 1402 |
+
/* Store results from simdgroup_matrix results into device memory */
|
| 1403 |
+
METAL_FUNC void store_result(
|
| 1404 |
+
device U* D,
|
| 1405 |
+
const int ldd,
|
| 1406 |
+
const device U* C,
|
| 1407 |
+
const int ldc,
|
| 1408 |
+
const int fdc,
|
| 1409 |
+
thread const Epilogue& epilogue_op) const {
|
| 1410 |
+
// Adjust for simdgroup and thread location
|
| 1411 |
+
C += (sm)*ldc + (sn)*fdc;
|
| 1412 |
+
D += (sm)*ldd + sn;
|
| 1413 |
+
|
| 1414 |
+
constexpr short kelems = decltype(Ctile)::kElemsPerFrag;
|
| 1415 |
+
|
| 1416 |
+
// Loop over all simdgroup tiles
|
| 1417 |
+
STEEL_PRAGMA_UNROLL
|
| 1418 |
+
for (short i = 0; i < TM; i++) {
|
| 1419 |
+
STEEL_PRAGMA_UNROLL
|
| 1420 |
+
for (short j = 0; j < TN; j++) {
|
| 1421 |
+
// Get accumulated result and associated offset in C
|
| 1422 |
+
thread const auto& accum = Ctile.frag_at(i, j);
|
| 1423 |
+
int offset_c = (i * TM_stride) * ldc + (j * TN_stride) * fdc;
|
| 1424 |
+
int offset_d = (i * TM_stride) * ldd + (j * TN_stride);
|
| 1425 |
+
|
| 1426 |
+
// Apply epilogue
|
| 1427 |
+
STEEL_PRAGMA_UNROLL
|
| 1428 |
+
for (short k = 0; k < kelems; k++) {
|
| 1429 |
+
D[offset_d + k] = epilogue_op.apply(accum[k], C[offset_c + k * fdc]);
|
| 1430 |
+
}
|
| 1431 |
+
}
|
| 1432 |
+
}
|
| 1433 |
+
}
|
| 1434 |
+
|
| 1435 |
+
METAL_FUNC void store_result_safe(
|
| 1436 |
+
device U* D,
|
| 1437 |
+
const int ldd,
|
| 1438 |
+
const device U* C,
|
| 1439 |
+
const int ldc,
|
| 1440 |
+
const int fdc,
|
| 1441 |
+
short2 dst_tile_dims,
|
| 1442 |
+
thread const Epilogue& epilogue_op) const {
|
| 1443 |
+
// Adjust for simdgroup and thread location
|
| 1444 |
+
C += (sm)*ldc + (sn)*fdc;
|
| 1445 |
+
D += (sm)*ldd + sn;
|
| 1446 |
+
dst_tile_dims -= short2(sn, sm);
|
| 1447 |
+
|
| 1448 |
+
if (dst_tile_dims.x <= 0 || dst_tile_dims.y <= 0)
|
| 1449 |
+
return;
|
| 1450 |
+
|
| 1451 |
+
constexpr short kelems = decltype(Ctile)::kElemsPerFrag;
|
| 1452 |
+
|
| 1453 |
+
STEEL_PRAGMA_UNROLL
|
| 1454 |
+
for (int i = 0; i < TM; i++) {
|
| 1455 |
+
if (i * TM_stride < dst_tile_dims.y) {
|
| 1456 |
+
STEEL_PRAGMA_UNROLL
|
| 1457 |
+
for (int j = 0; j < TN; j++) {
|
| 1458 |
+
// Get accumulated result and associated offset in C
|
| 1459 |
+
thread const auto& accum = Ctile.frag_at(i, j);
|
| 1460 |
+
int offset_c = (i * TM_stride) * ldc + (j * TN_stride) * fdc;
|
| 1461 |
+
int offset_d = (i * TM_stride) * ldd + (j * TN_stride);
|
| 1462 |
+
|
| 1463 |
+
// Apply epilogue
|
| 1464 |
+
STEEL_PRAGMA_UNROLL
|
| 1465 |
+
for (short k = 0; k < kelems; k++) {
|
| 1466 |
+
if ((j * TN_stride + k) < dst_tile_dims.x) {
|
| 1467 |
+
D[offset_d + k] =
|
| 1468 |
+
epilogue_op.apply(accum[k], C[offset_c + k * fdc]);
|
| 1469 |
+
}
|
| 1470 |
+
}
|
| 1471 |
+
}
|
| 1472 |
+
}
|
| 1473 |
+
}
|
| 1474 |
+
}
|
| 1475 |
+
};
|
| 1476 |
+
|
| 1477 |
+
// ============ "mlx/backend/metal/kernels/steel/attn/kernels/steel_attention.h"
|
| 1478 |
+
|
| 1479 |
+
struct AttnParams {
|
| 1480 |
+
int B; ///< Batch Size
|
| 1481 |
+
int H; ///< Heads
|
| 1482 |
+
int D; ///< Head Dim
|
| 1483 |
+
|
| 1484 |
+
int qL; ///< Query Sequence Length
|
| 1485 |
+
int kL; ///< Key Sequence Length
|
| 1486 |
+
|
| 1487 |
+
int gqa_factor; ///< Group Query factor
|
| 1488 |
+
float scale; ///< Attention scale
|
| 1489 |
+
float softcapping; ///< Softcapping value (1.0 for no softcapping)
|
| 1490 |
+
|
| 1491 |
+
int NQ; ///< Number of query blocks
|
| 1492 |
+
int NK; ///< Number of key/value blocks
|
| 1493 |
+
|
| 1494 |
+
int NQ_aligned; ///< Number of full query blocks
|
| 1495 |
+
int NK_aligned; ///< Number of full key/value blocks
|
| 1496 |
+
|
| 1497 |
+
int qL_rem; ///< Remainder in last query block
|
| 1498 |
+
int kL_rem; ///< Remainder in last key/value block
|
| 1499 |
+
int qL_off; ///< Offset in query sequence start
|
| 1500 |
+
|
| 1501 |
+
int64_t Q_strides[3]; ///< Query strides (B, H, L, D = 1)
|
| 1502 |
+
int64_t K_strides[3]; ///< Key strides (B, H, L, D = 1)
|
| 1503 |
+
int64_t V_strides[3]; ///< Value strides (B, H, L, D = 1)
|
| 1504 |
+
int64_t O_strides[3]; ///< Output strides (B, H, L, D = 1)
|
| 1505 |
+
|
| 1506 |
+
// Flash Attention variable-length support
|
| 1507 |
+
int total_q_tokens; ///< Total number of query tokens (sum of all sequence lengths)
|
| 1508 |
+
int total_k_tokens; ///< Total number of key/value tokens
|
| 1509 |
+
int max_seqlen_q; ///< Maximum query sequence length
|
| 1510 |
+
int max_seqlen_k; ///< Maximum key/value sequence length
|
| 1511 |
+
};
|
| 1512 |
+
|
| 1513 |
+
struct AttnMaskParams {
|
| 1514 |
+
int64_t M_strides[3]; ///< Mask strides (B, H, qL, kL = 1)
|
| 1515 |
+
};
|
| 1516 |
+
|
| 1517 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 1518 |
+
// GEMM kernels
|
| 1519 |
+
///////////////////////////////////////////////////////////////////////////////
|
| 1520 |
+
|
| 1521 |
+
constant bool align_Q [[function_constant(200)]];
|
| 1522 |
+
constant bool align_K [[function_constant(201)]];
|
| 1523 |
+
|
| 1524 |
+
constant bool has_mask [[function_constant(300)]];
|
| 1525 |
+
constant bool do_causal [[function_constant(301)]];
|
| 1526 |
+
|
| 1527 |
+
template <typename T>
|
| 1528 |
+
struct TransformScale {
|
| 1529 |
+
T scale;
|
| 1530 |
+
METAL_FUNC TransformScale(T scale_) : scale(scale_) {}
|
| 1531 |
+
|
| 1532 |
+
METAL_FUNC T apply(T x) const {
|
| 1533 |
+
return scale * x;
|
| 1534 |
+
}
|
| 1535 |
+
};
|
| 1536 |
+
|
| 1537 |
+
struct MaxOp {
|
| 1538 |
+
template <typename T>
|
| 1539 |
+
METAL_FUNC static constexpr T apply(T x, T y) {
|
| 1540 |
+
return metal::max(x, y);
|
| 1541 |
+
}
|
| 1542 |
+
};
|
| 1543 |
+
|
| 1544 |
+
struct SumOp {
|
| 1545 |
+
template <typename T>
|
| 1546 |
+
METAL_FUNC static constexpr T apply(T x, T y) {
|
| 1547 |
+
return x + y;
|
| 1548 |
+
}
|
| 1549 |
+
};
|
| 1550 |
+
|
| 1551 |
+
struct MulOp {
|
| 1552 |
+
template <typename T>
|
| 1553 |
+
METAL_FUNC static constexpr T apply(T x, T y) {
|
| 1554 |
+
return x * y;
|
| 1555 |
+
}
|
| 1556 |
+
};
|
| 1557 |
+
|
| 1558 |
+
struct SubOp {
|
| 1559 |
+
template <typename T>
|
| 1560 |
+
METAL_FUNC static constexpr T apply(T x, T y) {
|
| 1561 |
+
return x - y;
|
| 1562 |
+
}
|
| 1563 |
+
};
|
| 1564 |
+
|
| 1565 |
+
struct ExpSubOp {
|
| 1566 |
+
template <typename T>
|
| 1567 |
+
METAL_FUNC static constexpr T apply(T x, T y) {
|
| 1568 |
+
return fast::exp2(x - y);
|
| 1569 |
+
}
|
| 1570 |
+
};
|
| 1571 |
+
|
| 1572 |
+
struct DivOp {
|
| 1573 |
+
template <typename T>
|
| 1574 |
+
METAL_FUNC static constexpr T apply(T x, T y) {
|
| 1575 |
+
return x / y;
|
| 1576 |
+
}
|
| 1577 |
+
};
|
| 1578 |
+
|
| 1579 |
+
// clang-format off
|
| 1580 |
+
template <
|
| 1581 |
+
typename T,
|
| 1582 |
+
int BQ,
|
| 1583 |
+
int BK,
|
| 1584 |
+
int BD,
|
| 1585 |
+
int WM,
|
| 1586 |
+
int WN,
|
| 1587 |
+
typename MaskType = float,
|
| 1588 |
+
typename AccumType = float>
|
| 1589 |
+
[[kernel, max_total_threads_per_threadgroup(WM * WN * 32)]] void attention(
|
| 1590 |
+
const device T* Q [[buffer(0)]],
|
| 1591 |
+
const device T* K [[buffer(1)]],
|
| 1592 |
+
const device T* V [[buffer(2)]],
|
| 1593 |
+
device T* O [[buffer(3)]],
|
| 1594 |
+
const constant AttnParams* params [[buffer(4)]],
|
| 1595 |
+
const constant AttnMaskParams* mask_params [[buffer(5), function_constant(has_mask)]],
|
| 1596 |
+
const device MaskType* mask [[buffer(6), function_constant(has_mask)]],
|
| 1597 |
+
const device int* cu_seqlens_q [[buffer(7)]], // Cumulative query sequence lengths
|
| 1598 |
+
const device int* cu_seqlens_k [[buffer(8)]], // Cumulative key sequence lengths
|
| 1599 |
+
uint simd_lane_id [[thread_index_in_simdgroup]],
|
| 1600 |
+
uint simd_group_id [[simdgroup_index_in_threadgroup]],
|
| 1601 |
+
uint3 tid [[threadgroup_position_in_grid]],
|
| 1602 |
+
uint3 lid [[thread_position_in_threadgroup]]) { // clang-format on
|
| 1603 |
+
|
| 1604 |
+
// Pacifying compiler
|
| 1605 |
+
(void)lid;
|
| 1606 |
+
|
| 1607 |
+
// Flash Attention variable-length indexing
|
| 1608 |
+
// tid.z is now the sequence index within the batch
|
| 1609 |
+
int batch_idx = tid.z;
|
| 1610 |
+
int head_idx = tid.y;
|
| 1611 |
+
int block_idx = tid.x;
|
| 1612 |
+
|
| 1613 |
+
// Get sequence boundaries from cumulative lengths
|
| 1614 |
+
int q_seq_start = cu_seqlens_q[batch_idx];
|
| 1615 |
+
int q_seq_end = cu_seqlens_q[batch_idx + 1];
|
| 1616 |
+
int k_seq_start = cu_seqlens_k[batch_idx];
|
| 1617 |
+
int k_seq_end = cu_seqlens_k[batch_idx + 1];
|
| 1618 |
+
|
| 1619 |
+
int q_seq_len = q_seq_end - q_seq_start;
|
| 1620 |
+
int k_seq_len = k_seq_end - k_seq_start;
|
| 1621 |
+
|
| 1622 |
+
// Check if this block is within the sequence
|
| 1623 |
+
if (block_idx * BQ >= q_seq_len) {
|
| 1624 |
+
return;
|
| 1625 |
+
}
|
| 1626 |
+
|
| 1627 |
+
// Calculate offsets in the packed tensor format
|
| 1628 |
+
// Q/O shape: [total_tokens, num_heads, head_dim]
|
| 1629 |
+
// K/V shape: [total_tokens, num_heads_kv, head_dim]
|
| 1630 |
+
int q_offset = q_seq_start + block_idx * BQ;
|
| 1631 |
+
int k_offset = k_seq_start;
|
| 1632 |
+
|
| 1633 |
+
ulong kv_head_idx = head_idx / params->gqa_factor;
|
| 1634 |
+
|
| 1635 |
+
// Move pointers to the correct position in packed format
|
| 1636 |
+
Q += q_offset * params->H * params->D + head_idx * params->D;
|
| 1637 |
+
K += k_offset * (params->H / params->gqa_factor) * params->D + kv_head_idx * params->D;
|
| 1638 |
+
V += k_offset * (params->H / params->gqa_factor) * params->D + kv_head_idx * params->D;
|
| 1639 |
+
O += q_offset * params->H * params->D + head_idx * params->D;
|
| 1640 |
+
|
| 1641 |
+
if (has_mask) {
|
| 1642 |
+
// Mask indexing would need to be updated based on the mask format
|
| 1643 |
+
mask += batch_idx * mask_params->M_strides[0] +
|
| 1644 |
+
head_idx * mask_params->M_strides[1];
|
| 1645 |
+
}
|
| 1646 |
+
|
| 1647 |
+
// Prepare threadgroup memory
|
| 1648 |
+
constexpr short padQ = 16 / sizeof(T);
|
| 1649 |
+
constexpr short padK = 16 / sizeof(T);
|
| 1650 |
+
constexpr short padV = 16 / sizeof(T);
|
| 1651 |
+
|
| 1652 |
+
constexpr short LDQ_tgp = BD + padQ;
|
| 1653 |
+
constexpr short LDK_tgp = BK + padK;
|
| 1654 |
+
constexpr short LDV_tgp = BD + padV;
|
| 1655 |
+
|
| 1656 |
+
constexpr short tgp_mem_0 = (BK + padK) * (BD);
|
| 1657 |
+
constexpr short tgp_mem_1 = BK * (BD + padV);
|
| 1658 |
+
constexpr short tgp_mem_s = tgp_mem_0 > tgp_mem_1 ? tgp_mem_0 : tgp_mem_1;
|
| 1659 |
+
|
| 1660 |
+
threadgroup T Q_smem[BQ * (BD + padQ)];
|
| 1661 |
+
threadgroup T KV_smem[tgp_mem_s];
|
| 1662 |
+
|
| 1663 |
+
threadgroup T* Qs = Q_smem;
|
| 1664 |
+
threadgroup T* Ks = KV_smem;
|
| 1665 |
+
threadgroup T* Vs = KV_smem;
|
| 1666 |
+
|
| 1667 |
+
// Prepare block loaders
|
| 1668 |
+
using QBlockLoader = BlockLoaderT<
|
| 1669 |
+
/* typename T = */ T,
|
| 1670 |
+
/* short BROWS = */ BQ,
|
| 1671 |
+
/* short BCOLS = */ BD,
|
| 1672 |
+
/* short kDstStrRow = */ LDQ_tgp,
|
| 1673 |
+
/* short kDstStrCol = */ 1,
|
| 1674 |
+
/* short reduction_dim = */ 1,
|
| 1675 |
+
/* short tgp_size = */ WM * WN * 32>;
|
| 1676 |
+
|
| 1677 |
+
// K is loaded in transposed
|
| 1678 |
+
using KBlockLoader = BlockLoaderT<
|
| 1679 |
+
/* typename T = */ T,
|
| 1680 |
+
/* short BROWS = */ BK,
|
| 1681 |
+
/* short BCOLS = */ BD,
|
| 1682 |
+
/* short kDstStrRow = */ 1,
|
| 1683 |
+
/* short kDstStrCol = */ LDK_tgp,
|
| 1684 |
+
/* short reduction_dim = */ 0,
|
| 1685 |
+
/* short tgp_size = */ WM * WN * 32>;
|
| 1686 |
+
|
| 1687 |
+
using VBlockLoader = BlockLoaderT<
|
| 1688 |
+
/* typename T = */ T,
|
| 1689 |
+
/* short BROWS = */ BK,
|
| 1690 |
+
/* short BCOLS = */ BD,
|
| 1691 |
+
/* short kDstStrRow = */ LDV_tgp,
|
| 1692 |
+
/* short kDstStrCol = */ 1,
|
| 1693 |
+
/* short reduction_dim = */ 0,
|
| 1694 |
+
/* short tgp_size = */ WM * WN * 32>;
|
| 1695 |
+
|
| 1696 |
+
// For packed tensors, stride between tokens is H * D
|
| 1697 |
+
int q_stride = params->H * params->D;
|
| 1698 |
+
int kv_stride = (params->H / params->gqa_factor) * params->D;
|
| 1699 |
+
|
| 1700 |
+
QBlockLoader loader_q(
|
| 1701 |
+
Q, q_stride, Qs, simd_group_id, simd_lane_id);
|
| 1702 |
+
KBlockLoader loader_k(
|
| 1703 |
+
K, kv_stride, Ks, simd_group_id, simd_lane_id);
|
| 1704 |
+
VBlockLoader loader_v(
|
| 1705 |
+
V, kv_stride, Vs, simd_group_id, simd_lane_id);
|
| 1706 |
+
|
| 1707 |
+
// Apply softcapping adjustment to scale if needed
|
| 1708 |
+
float adjusted_scale = params->scale;
|
| 1709 |
+
if (params->softcapping != 1.0f) {
|
| 1710 |
+
adjusted_scale = params->scale / params->softcapping;
|
| 1711 |
+
}
|
| 1712 |
+
TransformScale<T> ts(static_cast<T>(adjusted_scale * 1.44269504089));
|
| 1713 |
+
|
| 1714 |
+
// Prepare MMA tiles
|
| 1715 |
+
constexpr short kFragSize = 8; // MMAFrag size
|
| 1716 |
+
using MMAFrag_acc_t = BaseMMAFrag<AccumType, kFragSize, kFragSize>;
|
| 1717 |
+
|
| 1718 |
+
constexpr int kNWarps = WM * WN;
|
| 1719 |
+
static_assert(
|
| 1720 |
+
BQ >= (kNWarps * kFragSize) && BQ % (kNWarps * kFragSize) == 0,
|
| 1721 |
+
"Each simdgroup must host atleast 1 simdgroup matrix along Q sequence.");
|
| 1722 |
+
|
| 1723 |
+
// Q seq frags per warp
|
| 1724 |
+
constexpr int TQ = BQ / (kNWarps * kFragSize);
|
| 1725 |
+
// KV sequence frags (all warps load the same frags)
|
| 1726 |
+
constexpr int TK = BK / kFragSize;
|
| 1727 |
+
// HeadDim frags (all warps load the same frags)
|
| 1728 |
+
constexpr int TD = BD / kFragSize;
|
| 1729 |
+
|
| 1730 |
+
static_assert(TQ == 1, "Check TQ");
|
| 1731 |
+
|
| 1732 |
+
MMATile<AccumType, TQ, 1, MMAFrag_acc_t> Qtile;
|
| 1733 |
+
MMATile<AccumType, 1, TK, MMAFrag_acc_t> Ktile;
|
| 1734 |
+
MMATile<AccumType, TQ, TK, MMAFrag_acc_t> Stile;
|
| 1735 |
+
MMATile<AccumType, 1, 1, MMAFrag_acc_t> Vtile;
|
| 1736 |
+
MMATile<AccumType, TQ, TD, MMAFrag_acc_t> Otile;
|
| 1737 |
+
|
| 1738 |
+
Otile.clear();
|
| 1739 |
+
|
| 1740 |
+
// Prepare mma tile offsets
|
| 1741 |
+
const short2 simd_coord = MMAFrag_acc_t::get_coord(simd_lane_id);
|
| 1742 |
+
const short sm = simd_coord.y;
|
| 1743 |
+
const short sn = simd_coord.x;
|
| 1744 |
+
const short tm = kFragSize * TQ * simd_group_id;
|
| 1745 |
+
|
| 1746 |
+
const short Qs_offset = (tm + sm) * LDQ_tgp + sn;
|
| 1747 |
+
const short Ks_offset = sm * LDK_tgp + sn;
|
| 1748 |
+
const short Vs_offset = sm * LDV_tgp + sn;
|
| 1749 |
+
|
| 1750 |
+
constexpr short Qs_tile_stride = kFragSize;
|
| 1751 |
+
constexpr short Ks_tile_stride = kFragSize * LDK_tgp;
|
| 1752 |
+
|
| 1753 |
+
threadgroup_barrier(mem_flags::mem_threadgroup);
|
| 1754 |
+
|
| 1755 |
+
// Load Q blocks apply scale
|
| 1756 |
+
int q_block_end = min(block_idx * BQ + BQ, q_seq_len);
|
| 1757 |
+
int q_block_size = q_block_end - block_idx * BQ;
|
| 1758 |
+
|
| 1759 |
+
if (q_block_size < BQ) {
|
| 1760 |
+
loader_q.load_safe(short2(BD, q_block_size));
|
| 1761 |
+
} else {
|
| 1762 |
+
loader_q.load_unsafe();
|
| 1763 |
+
}
|
| 1764 |
+
loader_q.apply_inplace_op(ts);
|
| 1765 |
+
|
| 1766 |
+
// Init row reduction variables
|
| 1767 |
+
constexpr short kRowsPT = decltype(Stile)::kRowsPerThread;
|
| 1768 |
+
|
| 1769 |
+
AccumType max_score[kRowsPT];
|
| 1770 |
+
AccumType sum_score[kRowsPT] = {0};
|
| 1771 |
+
|
| 1772 |
+
// Init to -Inf
|
| 1773 |
+
STEEL_PRAGMA_UNROLL
|
| 1774 |
+
for (short i = 0; i < kRowsPT; ++i) {
|
| 1775 |
+
max_score[i] = Limits<AccumType>::min;
|
| 1776 |
+
}
|
| 1777 |
+
|
| 1778 |
+
// Calculate number of K blocks for this sequence
|
| 1779 |
+
int kb_lim = (k_seq_len + BK - 1) / BK;
|
| 1780 |
+
|
| 1781 |
+
if (do_causal) {
|
| 1782 |
+
// For causal mask, limit to blocks that could affect this query block
|
| 1783 |
+
// Use sequence-local positions, not global offsets
|
| 1784 |
+
int q_block_start_in_seq = block_idx * BQ;
|
| 1785 |
+
int q_block_end_in_seq = q_block_start_in_seq + q_block_size;
|
| 1786 |
+
kb_lim = min(kb_lim, (q_block_end_in_seq + BK - 1) / BK);
|
| 1787 |
+
}
|
| 1788 |
+
|
| 1789 |
+
// Loop over KV seq length
|
| 1790 |
+
for (int kb = 0; kb < kb_lim; kb++) {
|
| 1791 |
+
// Load K block and apply scale
|
| 1792 |
+
threadgroup_barrier(mem_flags::mem_threadgroup);
|
| 1793 |
+
|
| 1794 |
+
int k_block_end = min(kb * BK + BK, k_seq_len);
|
| 1795 |
+
int k_block_size = k_block_end - kb * BK;
|
| 1796 |
+
|
| 1797 |
+
if (k_block_size < BK) {
|
| 1798 |
+
loader_k.load_safe(short2(BD, k_block_size));
|
| 1799 |
+
} else {
|
| 1800 |
+
loader_k.load_unsafe();
|
| 1801 |
+
}
|
| 1802 |
+
|
| 1803 |
+
// Do S = Q @ K.T
|
| 1804 |
+
Stile.clear();
|
| 1805 |
+
|
| 1806 |
+
threadgroup_barrier(mem_flags::mem_threadgroup);
|
| 1807 |
+
|
| 1808 |
+
STEEL_PRAGMA_UNROLL
|
| 1809 |
+
for (short dd = 0; dd < TD; dd++) {
|
| 1810 |
+
simdgroup_barrier(mem_flags::mem_none);
|
| 1811 |
+
|
| 1812 |
+
Qtile.template load<T, 1, 1, LDQ_tgp, 1>(
|
| 1813 |
+
&Qs[Qs_offset + dd * Qs_tile_stride]);
|
| 1814 |
+
Ktile.template load<T, 1, 1, LDK_tgp, 1>(
|
| 1815 |
+
&Ks[Ks_offset + dd * Ks_tile_stride]);
|
| 1816 |
+
|
| 1817 |
+
simdgroup_barrier(mem_flags::mem_none);
|
| 1818 |
+
|
| 1819 |
+
tile_matmad(Stile, Qtile, Ktile, Stile);
|
| 1820 |
+
}
|
| 1821 |
+
|
| 1822 |
+
// Mask out length sequence
|
| 1823 |
+
if (k_block_size < BK) {
|
| 1824 |
+
using stile_t = decltype(Stile);
|
| 1825 |
+
using selem_t = typename stile_t::elem_type;
|
| 1826 |
+
constexpr auto neg_inf = -metal::numeric_limits<selem_t>::infinity();
|
| 1827 |
+
|
| 1828 |
+
STEEL_PRAGMA_UNROLL
|
| 1829 |
+
for (short i = 0; i < stile_t::kTileRows; i++) {
|
| 1830 |
+
STEEL_PRAGMA_UNROLL
|
| 1831 |
+
for (short j = 0; j < stile_t::kTileCols; j++) {
|
| 1832 |
+
short col_pos = sn + (j * stile_t::kFragCols);
|
| 1833 |
+
STEEL_PRAGMA_UNROLL
|
| 1834 |
+
for (short jj = 0; jj < stile_t::MMAFrag_t::kElemCols; jj++) {
|
| 1835 |
+
if ((col_pos + jj) >= k_block_size) {
|
| 1836 |
+
Stile.frag_at(i, j)[jj] = neg_inf;
|
| 1837 |
+
}
|
| 1838 |
+
}
|
| 1839 |
+
}
|
| 1840 |
+
}
|
| 1841 |
+
}
|
| 1842 |
+
|
| 1843 |
+
// Mask out if causal
|
| 1844 |
+
if (do_causal) {
|
| 1845 |
+
using stile_t = decltype(Stile);
|
| 1846 |
+
using selem_t = typename stile_t::elem_type;
|
| 1847 |
+
constexpr auto neg_inf = -metal::numeric_limits<selem_t>::infinity();
|
| 1848 |
+
|
| 1849 |
+
STEEL_PRAGMA_UNROLL
|
| 1850 |
+
for (short i = 0; i < stile_t::kTileRows; i++) {
|
| 1851 |
+
// Use sequence-local positions for causal mask
|
| 1852 |
+
const int row_pos_in_seq = block_idx * BQ + tm + sm + (i * stile_t::kFragRows);
|
| 1853 |
+
STEEL_PRAGMA_UNROLL
|
| 1854 |
+
for (short j = 0; j < stile_t::kTileCols; j++) {
|
| 1855 |
+
const int col_pos_in_seq = kb * BK + sn + (j * stile_t::kFragCols);
|
| 1856 |
+
STEEL_PRAGMA_UNROLL
|
| 1857 |
+
for (short jj = 0; jj < stile_t::MMAFrag_t::kElemCols; jj++) {
|
| 1858 |
+
if (row_pos_in_seq < (col_pos_in_seq + jj)) {
|
| 1859 |
+
Stile.frag_at(i, j)[jj] = neg_inf;
|
| 1860 |
+
}
|
| 1861 |
+
}
|
| 1862 |
+
}
|
| 1863 |
+
}
|
| 1864 |
+
}
|
| 1865 |
+
|
| 1866 |
+
// Other masking as needed
|
| 1867 |
+
if (has_mask) {
|
| 1868 |
+
using stile_t = decltype(Stile);
|
| 1869 |
+
using selem_t = typename stile_t::elem_type;
|
| 1870 |
+
constexpr auto neg_inf = -metal::numeric_limits<selem_t>::infinity();
|
| 1871 |
+
|
| 1872 |
+
constexpr bool is_bool = is_same_v<MaskType, bool>;
|
| 1873 |
+
using melem_t = typename metal::conditional_t<is_bool, bool, selem_t>;
|
| 1874 |
+
|
| 1875 |
+
using MMAFrag_mask_t = BaseMMAFrag<melem_t, kFragSize, kFragSize>;
|
| 1876 |
+
using frag_t = typename MMAFrag_mask_t::frag_type;
|
| 1877 |
+
|
| 1878 |
+
STEEL_PRAGMA_UNROLL
|
| 1879 |
+
for (short i = 0; i < stile_t::kTileRows; i++) {
|
| 1880 |
+
// Use sequence-local positions
|
| 1881 |
+
const int row_pos_in_seq = block_idx * BQ + tm + sm + (i * stile_t::kFragRows);
|
| 1882 |
+
STEEL_PRAGMA_UNROLL
|
| 1883 |
+
for (short j = 0; j < stile_t::kTileCols; j++) {
|
| 1884 |
+
const int col_pos_in_seq = kb * BK + sn + (j * stile_t::kFragCols);
|
| 1885 |
+
|
| 1886 |
+
frag_t mfrag;
|
| 1887 |
+
|
| 1888 |
+
MMAFrag_mask_t::load_safe(
|
| 1889 |
+
mfrag,
|
| 1890 |
+
mask,
|
| 1891 |
+
int(mask_params->M_strides[2]),
|
| 1892 |
+
Int<1>{},
|
| 1893 |
+
q_seq_len,
|
| 1894 |
+
k_seq_len,
|
| 1895 |
+
row_pos_in_seq, // Already sequence-local
|
| 1896 |
+
col_pos_in_seq); // Already sequence-local
|
| 1897 |
+
|
| 1898 |
+
STEEL_PRAGMA_UNROLL
|
| 1899 |
+
for (short jj = 0; jj < stile_t::MMAFrag_t::kElemsPerFrag; jj++) {
|
| 1900 |
+
if constexpr (is_bool) {
|
| 1901 |
+
Stile.frag_at(i, j)[jj] =
|
| 1902 |
+
mfrag[jj] ? Stile.frag_at(i, j)[jj] : neg_inf;
|
| 1903 |
+
} else {
|
| 1904 |
+
Stile.frag_at(i, j)[jj] += 1.44269504089 * selem_t(mfrag[jj]);
|
| 1905 |
+
}
|
| 1906 |
+
}
|
| 1907 |
+
}
|
| 1908 |
+
}
|
| 1909 |
+
}
|
| 1910 |
+
|
| 1911 |
+
// Apply softcapping if needed (tanh(score) * softcapping)
|
| 1912 |
+
if (params->softcapping != 1.0f) {
|
| 1913 |
+
using stile_t = decltype(Stile);
|
| 1914 |
+
using selem_t = typename stile_t::elem_type;
|
| 1915 |
+
const selem_t softcapping_val = static_cast<selem_t>(params->softcapping);
|
| 1916 |
+
|
| 1917 |
+
STEEL_PRAGMA_UNROLL
|
| 1918 |
+
for (short i = 0; i < stile_t::kTileRows; i++) {
|
| 1919 |
+
STEEL_PRAGMA_UNROLL
|
| 1920 |
+
for (short j = 0; j < stile_t::kTileCols; j++) {
|
| 1921 |
+
STEEL_PRAGMA_UNROLL
|
| 1922 |
+
for (short jj = 0; jj < stile_t::MMAFrag_t::kElemsPerFrag; jj++) {
|
| 1923 |
+
Stile.frag_at(i, j)[jj] = metal::tanh(Stile.frag_at(i, j)[jj]) * softcapping_val;
|
| 1924 |
+
}
|
| 1925 |
+
}
|
| 1926 |
+
}
|
| 1927 |
+
}
|
| 1928 |
+
|
| 1929 |
+
threadgroup_barrier(mem_flags::mem_threadgroup);
|
| 1930 |
+
|
| 1931 |
+
// Load V blocks
|
| 1932 |
+
if (k_block_size < BK) {
|
| 1933 |
+
loader_v.load_safe(short2(BD, k_block_size));
|
| 1934 |
+
} else {
|
| 1935 |
+
loader_v.load_unsafe();
|
| 1936 |
+
}
|
| 1937 |
+
|
| 1938 |
+
// Do softmax
|
| 1939 |
+
|
| 1940 |
+
// Temp variables
|
| 1941 |
+
AccumType new_max[kRowsPT];
|
| 1942 |
+
AccumType factor[kRowsPT];
|
| 1943 |
+
STEEL_PRAGMA_UNROLL
|
| 1944 |
+
for (short i = 0; i < kRowsPT; ++i) {
|
| 1945 |
+
new_max[i] = max_score[i];
|
| 1946 |
+
}
|
| 1947 |
+
|
| 1948 |
+
// Row max
|
| 1949 |
+
Stile.template row_reduce<MaxOp>(new_max);
|
| 1950 |
+
|
| 1951 |
+
// exp(Si - rowmax(Si))
|
| 1952 |
+
Stile.template row_bin_op<ExpSubOp>(new_max);
|
| 1953 |
+
|
| 1954 |
+
// Factor exp(rowmax(Si) - rowmax(Si-1))
|
| 1955 |
+
STEEL_PRAGMA_UNROLL
|
| 1956 |
+
for (short i = 0; i < kRowsPT; ++i) {
|
| 1957 |
+
factor[i] = fast::exp2(max_score[i] - new_max[i]);
|
| 1958 |
+
}
|
| 1959 |
+
|
| 1960 |
+
// Save max for next iteration
|
| 1961 |
+
STEEL_PRAGMA_UNROLL
|
| 1962 |
+
for (short i = 0; i < kRowsPT; ++i) {
|
| 1963 |
+
max_score[i] = new_max[i];
|
| 1964 |
+
}
|
| 1965 |
+
|
| 1966 |
+
// Row Sum
|
| 1967 |
+
AccumType sum_score_tmp[kRowsPT] = {0};
|
| 1968 |
+
Stile.template row_reduce<SumOp>(sum_score_tmp);
|
| 1969 |
+
|
| 1970 |
+
// Update norm
|
| 1971 |
+
STEEL_PRAGMA_UNROLL
|
| 1972 |
+
for (short i = 0; i < kRowsPT; ++i) {
|
| 1973 |
+
sum_score[i] = sum_score[i] * factor[i] + sum_score_tmp[i];
|
| 1974 |
+
}
|
| 1975 |
+
|
| 1976 |
+
// Update O
|
| 1977 |
+
Otile.template row_bin_op<MulOp>(factor);
|
| 1978 |
+
|
| 1979 |
+
// Load V into registers
|
| 1980 |
+
threadgroup_barrier(mem_flags::mem_threadgroup);
|
| 1981 |
+
|
| 1982 |
+
STEEL_PRAGMA_UNROLL
|
| 1983 |
+
for (short iq = 0; iq < TQ; iq++) {
|
| 1984 |
+
STEEL_PRAGMA_UNROLL
|
| 1985 |
+
for (short id = 0; id < TD; id++) {
|
| 1986 |
+
STEEL_PRAGMA_UNROLL
|
| 1987 |
+
for (short ik = 0; ik < TK; ik++) {
|
| 1988 |
+
if constexpr (BD == 128) {
|
| 1989 |
+
simdgroup_barrier(mem_flags::mem_none);
|
| 1990 |
+
}
|
| 1991 |
+
|
| 1992 |
+
const short kk = ik * kFragSize;
|
| 1993 |
+
const short dd = id * kFragSize;
|
| 1994 |
+
|
| 1995 |
+
Vtile.template load<T, 1, 1, LDV_tgp, 1>(
|
| 1996 |
+
&Vs[Vs_offset + kk * LDV_tgp + dd]);
|
| 1997 |
+
|
| 1998 |
+
if constexpr (BD == 128) {
|
| 1999 |
+
simdgroup_barrier(mem_flags::mem_none);
|
| 2000 |
+
}
|
| 2001 |
+
|
| 2002 |
+
MMAFrag_acc_t::mma(
|
| 2003 |
+
Otile.frag_at(iq, id),
|
| 2004 |
+
Stile.frag_at(iq, ik),
|
| 2005 |
+
Vtile.frag_at(0, 0),
|
| 2006 |
+
Otile.frag_at(iq, id));
|
| 2007 |
+
}
|
| 2008 |
+
}
|
| 2009 |
+
}
|
| 2010 |
+
|
| 2011 |
+
// Prepare for next iteration
|
| 2012 |
+
loader_k.next();
|
| 2013 |
+
loader_v.next();
|
| 2014 |
+
}
|
| 2015 |
+
|
| 2016 |
+
// Normalize output
|
| 2017 |
+
Otile.template row_bin_op<DivOp>(sum_score);
|
| 2018 |
+
threadgroup_barrier(mem_flags::mem_none);
|
| 2019 |
+
|
| 2020 |
+
// Store results
|
| 2021 |
+
// O is already pointing to the correct block position from earlier adjustment
|
| 2022 |
+
// Just need to offset within the block for this thread's tile
|
| 2023 |
+
device T* O_tile = O + (tm + sm) * params->H * params->D + sn;
|
| 2024 |
+
|
| 2025 |
+
if (q_block_size < BQ) {
|
| 2026 |
+
// Only store if this thread's tile is within the valid range
|
| 2027 |
+
if ((tm + sm) < q_block_size && sn < BD) {
|
| 2028 |
+
auto dst_tile_dims = short2(BD - sn, q_block_size - (tm + sm));
|
| 2029 |
+
Otile.template store_safe<T, 1, 1>(O_tile, params->H * params->D, dst_tile_dims);
|
| 2030 |
+
}
|
| 2031 |
+
} else {
|
| 2032 |
+
Otile.template store<T, 1, 1>(O_tile, params->H * params->D);
|
| 2033 |
+
}
|
| 2034 |
+
}
|
| 2035 |
+
|
| 2036 |
+
// clang-format off
|
| 2037 |
+
|
| 2038 |
+
// SDPA full instantiations
|
| 2039 |
+
|
| 2040 |
+
// Instantiate a templated kernel.
|
| 2041 |
+
// Extra args are used as template parameters:
|
| 2042 |
+
// e.g. instantiate_kernel(binary_int, binary, a, b) ->
|
| 2043 |
+
// [[host_name(binary_int)]] [kernel] binary<a, b>
|
| 2044 |
+
#define instantiate_kernel(name, func, ...) \
|
| 2045 |
+
template [[host_name( \
|
| 2046 |
+
name)]] [[kernel]] decltype(func<__VA_ARGS__>) func<__VA_ARGS__>;
|
| 2047 |
+
|
| 2048 |
+
#define instantiate_attn(tname, dtype, bq, bk, bd, wm, wn, mname, mtype) \
|
| 2049 |
+
instantiate_kernel( \
|
| 2050 |
+
"steel_attention_" #tname "_bq" #bq "_bk" #bk "_bd" #bd \
|
| 2051 |
+
"_wm" #wm "_wn" #wn "_mask" #mname, \
|
| 2052 |
+
attention, dtype, bq, bk, bd, wm, wn, mtype, float)
|
| 2053 |
+
|
| 2054 |
+
#define instantiate_attn_shapes_helper(iname, itype, mname, mtype) \
|
| 2055 |
+
instantiate_attn(iname, itype, 16, 8, 256, 2, 1, mname, mtype) \
|
| 2056 |
+
instantiate_attn(iname, itype, 32, 16, 128, 4, 1, mname, mtype) \
|
| 2057 |
+
instantiate_attn(iname, itype, 32, 32, 96, 4, 1, mname, mtype) \
|
| 2058 |
+
instantiate_attn(iname, itype, 32, 32, 80, 4, 1, mname, mtype) \
|
| 2059 |
+
instantiate_attn(iname, itype, 32, 32, 72, 4, 1, mname, mtype) \
|
| 2060 |
+
instantiate_attn(iname, itype, 32, 32, 64, 4, 1, mname, mtype) \
|
| 2061 |
+
instantiate_attn(iname, itype, 32, 32, 32, 4, 1, mname, mtype)
|
| 2062 |
+
|
| 2063 |
+
#define instantiate_attn_mask_helper(iname, itype) \
|
| 2064 |
+
instantiate_attn_shapes_helper(iname, itype, iname, itype) \
|
| 2065 |
+
instantiate_attn_shapes_helper(iname, itype, bool_, bool)
|
| 2066 |
+
|
| 2067 |
+
instantiate_attn_mask_helper(float16, half);
|
| 2068 |
+
instantiate_attn_mask_helper(bfloat16, bfloat16_t);
|
| 2069 |
+
instantiate_attn_mask_helper(float32, float);
|
| 2070 |
+
|
sdpa-metal/scaled_dot_product_attention.mm
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <ATen/mps/MPSDevice.h>
|
| 2 |
+
#include <ATen/mps/MPSStream.h>
|
| 3 |
+
#include <torch/torch.h>
|
| 4 |
+
|
| 5 |
+
#import <Foundation/Foundation.h>
|
| 6 |
+
#import <Metal/Metal.h>
|
| 7 |
+
#include <algorithm>
|
| 8 |
+
#include <dlfcn.h>
|
| 9 |
+
#include <string>
|
| 10 |
+
#include <vector>
|
| 11 |
+
|
| 12 |
+
static inline id<MTLBuffer> getMTLBufferStorage(const torch::Tensor &tensor) {
|
| 13 |
+
return __builtin_bit_cast(id<MTLBuffer>, tensor.storage().data());
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
static std::string getModuleDirectory() {
|
| 17 |
+
Dl_info dl_info;
|
| 18 |
+
if (dladdr((void *)getModuleDirectory, &dl_info)) {
|
| 19 |
+
std::string path(dl_info.dli_fname);
|
| 20 |
+
size_t pos = path.find_last_of('/');
|
| 21 |
+
if (pos != std::string::npos) {
|
| 22 |
+
return path.substr(0, pos);
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
return ".";
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// Helper function to get dtype string
|
| 29 |
+
static std::string getDtypeString(torch::ScalarType dtype) {
|
| 30 |
+
switch (dtype) {
|
| 31 |
+
case torch::kFloat:
|
| 32 |
+
return "float32";
|
| 33 |
+
case torch::kHalf:
|
| 34 |
+
return "float16";
|
| 35 |
+
case torch::kBFloat16:
|
| 36 |
+
return "bfloat16";
|
| 37 |
+
default:
|
| 38 |
+
TORCH_CHECK(false, "Unsupported dtype for SDPA: ", dtype);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// Helper function to get dtype string for kernel names
|
| 43 |
+
static std::string getKernelDtypeString(torch::ScalarType dtype) {
|
| 44 |
+
switch (dtype) {
|
| 45 |
+
case torch::kFloat:
|
| 46 |
+
return "float32"; // Match the instantiation names
|
| 47 |
+
case torch::kHalf:
|
| 48 |
+
return "float16";
|
| 49 |
+
case torch::kBFloat16:
|
| 50 |
+
return "bfloat16";
|
| 51 |
+
default:
|
| 52 |
+
TORCH_CHECK(false, "Unsupported dtype for SDPA: ", dtype);
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
// Parameters structure matching Flash Attention's AttnParams
|
| 58 |
+
struct AttnParams {
|
| 59 |
+
int32_t B; // batch size
|
| 60 |
+
int32_t H; // number of heads
|
| 61 |
+
int32_t D; // head dimension
|
| 62 |
+
int32_t qL; // query sequence length (per sequence)
|
| 63 |
+
int32_t kL; // key sequence length (per sequence)
|
| 64 |
+
int32_t gqa_factor; // grouped query attention factor
|
| 65 |
+
float scale; // attention scale
|
| 66 |
+
float softcapping; // softcapping value (1.0 for no softcapping)
|
| 67 |
+
int32_t NQ; // number of query blocks
|
| 68 |
+
int32_t NK; // number of key blocks
|
| 69 |
+
int32_t NQ_aligned; // aligned query blocks
|
| 70 |
+
int32_t NK_aligned; // aligned key blocks
|
| 71 |
+
int32_t qL_rem; // remainder query length
|
| 72 |
+
int32_t kL_rem; // remainder key length
|
| 73 |
+
int32_t qL_off; // query offset
|
| 74 |
+
int64_t Q_strides[3]; // query tensor strides
|
| 75 |
+
int64_t K_strides[3]; // key tensor strides
|
| 76 |
+
int64_t V_strides[3]; // value tensor strides
|
| 77 |
+
int64_t O_strides[3]; // output tensor strides
|
| 78 |
+
|
| 79 |
+
// Flash Attention variable-length support
|
| 80 |
+
int32_t total_q_tokens; // Total number of query tokens
|
| 81 |
+
int32_t total_k_tokens; // Total number of key/value tokens
|
| 82 |
+
int32_t max_seqlen_q; // Maximum query sequence length
|
| 83 |
+
int32_t max_seqlen_k; // Maximum key/value sequence length
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
// Forward declarations for kernel implementations
|
| 87 |
+
void call_flash_attention_varlen(
|
| 88 |
+
id<MTLDevice> device,
|
| 89 |
+
id<MTLCommandBuffer> cmdBuf,
|
| 90 |
+
id<MTLLibrary> lib,
|
| 91 |
+
torch::Tensor &out,
|
| 92 |
+
torch::Tensor &query,
|
| 93 |
+
torch::Tensor &key,
|
| 94 |
+
torch::Tensor &value,
|
| 95 |
+
torch::Tensor &cu_seqlens_q,
|
| 96 |
+
torch::Tensor &cu_seqlens_k,
|
| 97 |
+
int64_t max_seqlen_q,
|
| 98 |
+
int64_t max_seqlen_k,
|
| 99 |
+
bool do_causal,
|
| 100 |
+
double scale,
|
| 101 |
+
double softcapping);
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
void flash_attention_varlen(
|
| 105 |
+
torch::Tensor &out, // [total_q_tokens, num_heads, head_size]
|
| 106 |
+
torch::Tensor &query, // [total_q_tokens, num_heads, head_size]
|
| 107 |
+
torch::Tensor &key, // [total_k_tokens, num_heads_kv, head_size]
|
| 108 |
+
torch::Tensor &value, // [total_k_tokens, num_heads_kv, head_size]
|
| 109 |
+
torch::Tensor &cu_seqlens_q, // [batch_size + 1]
|
| 110 |
+
torch::Tensor &cu_seqlens_k, // [batch_size + 1]
|
| 111 |
+
int64_t max_seqlen_q, // Maximum query sequence length
|
| 112 |
+
int64_t max_seqlen_k, // Maximum key sequence length
|
| 113 |
+
bool do_causal, // Whether to use causal mask
|
| 114 |
+
double scale, // Attention scale
|
| 115 |
+
double softcapping) { // Softcapping value
|
| 116 |
+
|
| 117 |
+
try {
|
| 118 |
+
// Get device and stream
|
| 119 |
+
id<MTLDevice> device = at::mps::MPSDevice::getInstance()->device();
|
| 120 |
+
at::mps::MPSStream *stream = at::mps::getCurrentMPSStream();
|
| 121 |
+
TORCH_CHECK(stream, "Failed to get current MPS stream");
|
| 122 |
+
|
| 123 |
+
// Get dimensions from Flash Attention format
|
| 124 |
+
int64_t total_q_tokens = query.size(0);
|
| 125 |
+
int64_t num_heads = query.size(1);
|
| 126 |
+
int64_t head_dim = query.size(2);
|
| 127 |
+
int64_t num_heads_kv = key.size(1);
|
| 128 |
+
int64_t batch_size = cu_seqlens_q.size(0) - 1; // cu_seqlens has batch_size + 1 elements
|
| 129 |
+
|
| 130 |
+
// Check if we support this head dimension
|
| 131 |
+
std::vector<int> supported_head_dims = {32, 64, 72, 80, 96, 128, 256};
|
| 132 |
+
bool supported_head_dim = std::find(supported_head_dims.begin(),
|
| 133 |
+
supported_head_dims.end(),
|
| 134 |
+
head_dim) != supported_head_dims.end();
|
| 135 |
+
|
| 136 |
+
TORCH_CHECK(supported_head_dim, "Head dimension ", head_dim, " is not supported");
|
| 137 |
+
TORCH_CHECK(cu_seqlens_q.size(0) == cu_seqlens_k.size(0),
|
| 138 |
+
"cu_seqlens_q and cu_seqlens_k must have the same size");
|
| 139 |
+
|
| 140 |
+
// Load Metal library
|
| 141 |
+
static id<MTLLibrary> lib = nil;
|
| 142 |
+
if (!lib) {
|
| 143 |
+
NSError *error = nil;
|
| 144 |
+
NSString *path = [NSString stringWithFormat:@"%s/" METALLIB_PATH,
|
| 145 |
+
getModuleDirectory().c_str()];
|
| 146 |
+
NSURL *url = [NSURL fileURLWithPath:path];
|
| 147 |
+
lib = [device newLibraryWithURL:url error:&error];
|
| 148 |
+
TORCH_CHECK(lib, "Failed to load Metal library: ",
|
| 149 |
+
error ? error.localizedDescription.UTF8String : "unknown error");
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
// Get command buffer
|
| 153 |
+
id<MTLCommandBuffer> cmdBuf = stream->commandBuffer();
|
| 154 |
+
TORCH_CHECK(cmdBuf, "Failed to get MPS command buffer");
|
| 155 |
+
|
| 156 |
+
// For variable-length Flash Attention, always use the full attention kernel
|
| 157 |
+
|
| 158 |
+
// Call the Flash Attention kernel
|
| 159 |
+
call_flash_attention_varlen(device, cmdBuf, lib, out, query, key, value,
|
| 160 |
+
cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k,
|
| 161 |
+
do_causal, scale, softcapping);
|
| 162 |
+
} catch (const std::exception& e) {
|
| 163 |
+
throw;
|
| 164 |
+
} catch (...) {
|
| 165 |
+
throw;
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
// Implementation of Flash Attention variable-length kernel
|
| 170 |
+
void call_flash_attention_varlen(
|
| 171 |
+
id<MTLDevice> device,
|
| 172 |
+
id<MTLCommandBuffer> cmdBuf,
|
| 173 |
+
id<MTLLibrary> lib,
|
| 174 |
+
torch::Tensor &out,
|
| 175 |
+
torch::Tensor &query,
|
| 176 |
+
torch::Tensor &key,
|
| 177 |
+
torch::Tensor &value,
|
| 178 |
+
torch::Tensor &cu_seqlens_q,
|
| 179 |
+
torch::Tensor &cu_seqlens_k,
|
| 180 |
+
int64_t max_seqlen_q,
|
| 181 |
+
int64_t max_seqlen_k,
|
| 182 |
+
bool do_causal,
|
| 183 |
+
double scale,
|
| 184 |
+
double softcapping) {
|
| 185 |
+
|
| 186 |
+
// Get dimensions
|
| 187 |
+
int64_t total_q_tokens = query.size(0);
|
| 188 |
+
int64_t num_heads = query.size(1);
|
| 189 |
+
int64_t head_dim = query.size(2);
|
| 190 |
+
int64_t num_heads_kv = key.size(1);
|
| 191 |
+
int64_t batch_size = cu_seqlens_q.size(0) - 1;
|
| 192 |
+
|
| 193 |
+
// Grouped Query Attention factor
|
| 194 |
+
int32_t gqa_factor = num_heads / num_heads_kv;
|
| 195 |
+
|
| 196 |
+
// Block sizes based on head dimension
|
| 197 |
+
const int BQ = (head_dim == 256) ? 16 : 32; // Use BQ=16 for head_dim=256
|
| 198 |
+
const int bk = (head_dim == 256) ? 8 : ((head_dim >= 128) ? 16 : 32); // Use bk=8 for head_dim=256
|
| 199 |
+
const int WM = (head_dim == 256) ? 2 : 4; // Use WM=2 for head_dim=256
|
| 200 |
+
const int WN = 1;
|
| 201 |
+
|
| 202 |
+
// Setup parameters
|
| 203 |
+
AttnParams params = {}; // Zero-initialize all fields
|
| 204 |
+
params.B = batch_size;
|
| 205 |
+
params.H = num_heads;
|
| 206 |
+
params.D = head_dim;
|
| 207 |
+
params.gqa_factor = gqa_factor;
|
| 208 |
+
params.scale = static_cast<float>(scale);
|
| 209 |
+
params.softcapping = static_cast<float>(softcapping);
|
| 210 |
+
params.total_q_tokens = total_q_tokens;
|
| 211 |
+
params.total_k_tokens = key.size(0);
|
| 212 |
+
params.max_seqlen_q = max_seqlen_q;
|
| 213 |
+
params.max_seqlen_k = max_seqlen_k;
|
| 214 |
+
|
| 215 |
+
// Initialize fields that might be checked but aren't used in Flash Attention
|
| 216 |
+
params.qL = 0; // Not used in variable-length attention
|
| 217 |
+
params.kL = 0; // Not used in variable-length attention
|
| 218 |
+
params.NQ = 0; // Not used
|
| 219 |
+
params.NK = 0; // Not used
|
| 220 |
+
params.NQ_aligned = 0;
|
| 221 |
+
params.NK_aligned = 0;
|
| 222 |
+
params.qL_rem = 0;
|
| 223 |
+
params.kL_rem = 0;
|
| 224 |
+
params.qL_off = 0;
|
| 225 |
+
|
| 226 |
+
// Strides are not used for packed tensors (contiguous)
|
| 227 |
+
params.Q_strides[0] = 0;
|
| 228 |
+
params.Q_strides[1] = 0;
|
| 229 |
+
params.Q_strides[2] = 0;
|
| 230 |
+
params.K_strides[0] = 0;
|
| 231 |
+
params.K_strides[1] = 0;
|
| 232 |
+
params.K_strides[2] = 0;
|
| 233 |
+
params.V_strides[0] = 0;
|
| 234 |
+
params.V_strides[1] = 0;
|
| 235 |
+
params.V_strides[2] = 0;
|
| 236 |
+
params.O_strides[0] = 0;
|
| 237 |
+
params.O_strides[1] = 0;
|
| 238 |
+
params.O_strides[2] = 0;
|
| 239 |
+
|
| 240 |
+
// For variable-length attention, we'll process each sequence separately
|
| 241 |
+
// The kernel will handle the cu_seqlens internally
|
| 242 |
+
|
| 243 |
+
bool has_mask = false; // Masks are not supported in Flash Attention
|
| 244 |
+
|
| 245 |
+
// Setup function constants
|
| 246 |
+
MTLFunctionConstantValues *constants = [MTLFunctionConstantValues new];
|
| 247 |
+
[constants setConstantValue:&has_mask type:MTLDataTypeBool atIndex:300];
|
| 248 |
+
[constants setConstantValue:&do_causal type:MTLDataTypeBool atIndex:301];
|
| 249 |
+
|
| 250 |
+
// Construct kernel name based on data type and head dimension
|
| 251 |
+
std::string kernel_name = "steel_attention_";
|
| 252 |
+
kernel_name += getKernelDtypeString(query.scalar_type());
|
| 253 |
+
kernel_name += "_bq" + std::to_string(BQ);
|
| 254 |
+
kernel_name += "_bk" + std::to_string(bk);
|
| 255 |
+
kernel_name += "_bd" + std::to_string(head_dim);
|
| 256 |
+
kernel_name += "_wm" + std::to_string(WM) + "_wn" + std::to_string(WN);
|
| 257 |
+
kernel_name += "_maskbool_"; // Always use bool for mask type (no masks supported)
|
| 258 |
+
|
| 259 |
+
// Get kernel function
|
| 260 |
+
NSError *error = nil;
|
| 261 |
+
id<MTLFunction> function = [lib newFunctionWithName:[NSString stringWithUTF8String:kernel_name.c_str()]
|
| 262 |
+
constantValues:constants
|
| 263 |
+
error:&error];
|
| 264 |
+
TORCH_CHECK(function, "Failed to get Metal function: ", kernel_name,
|
| 265 |
+
" Error: ", error ? error.localizedDescription.UTF8String : "unknown");
|
| 266 |
+
|
| 267 |
+
// Create compute pipeline
|
| 268 |
+
id<MTLComputePipelineState> pipeline = [device newComputePipelineStateWithFunction:function error:&error];
|
| 269 |
+
TORCH_CHECK(pipeline, "Failed to create compute pipeline: ",
|
| 270 |
+
error ? error.localizedDescription.UTF8String : "unknown");
|
| 271 |
+
|
| 272 |
+
// Setup command encoder with dispatch sync
|
| 273 |
+
at::mps::MPSStream *stream = at::mps::getCurrentMPSStream();
|
| 274 |
+
dispatch_queue_t q = stream->queue();
|
| 275 |
+
dispatch_sync(q, ^{
|
| 276 |
+
id<MTLComputeCommandEncoder> encoder = [cmdBuf computeCommandEncoder];
|
| 277 |
+
TORCH_CHECK(encoder, "Failed to create compute encoder");
|
| 278 |
+
|
| 279 |
+
[encoder setComputePipelineState:pipeline];
|
| 280 |
+
|
| 281 |
+
// Set buffers
|
| 282 |
+
int buffer_idx = 0;
|
| 283 |
+
|
| 284 |
+
// Query buffer - index 0
|
| 285 |
+
[encoder setBuffer:getMTLBufferStorage(query)
|
| 286 |
+
offset:query.storage_offset() * query.element_size()
|
| 287 |
+
atIndex:buffer_idx++];
|
| 288 |
+
|
| 289 |
+
// Key buffer - index 1
|
| 290 |
+
[encoder setBuffer:getMTLBufferStorage(key)
|
| 291 |
+
offset:key.storage_offset() * key.element_size()
|
| 292 |
+
atIndex:buffer_idx++];
|
| 293 |
+
|
| 294 |
+
// Value buffer - index 2
|
| 295 |
+
[encoder setBuffer:getMTLBufferStorage(value)
|
| 296 |
+
offset:value.storage_offset() * value.element_size()
|
| 297 |
+
atIndex:buffer_idx++];
|
| 298 |
+
|
| 299 |
+
// Output buffer - index 3
|
| 300 |
+
[encoder setBuffer:getMTLBufferStorage(out)
|
| 301 |
+
offset:out.storage_offset() * out.element_size()
|
| 302 |
+
atIndex:buffer_idx++];
|
| 303 |
+
|
| 304 |
+
// Parameters - index 4
|
| 305 |
+
[encoder setBytes:¶ms length:sizeof(AttnParams) atIndex:buffer_idx++];
|
| 306 |
+
|
| 307 |
+
// Skip mask parameters - indices 5 and 6 (masks not supported)
|
| 308 |
+
buffer_idx += 2;
|
| 309 |
+
|
| 310 |
+
// Set cu_seqlens buffers - indices 7 and 8
|
| 311 |
+
[encoder setBuffer:getMTLBufferStorage(cu_seqlens_q)
|
| 312 |
+
offset:cu_seqlens_q.storage_offset() * cu_seqlens_q.element_size()
|
| 313 |
+
atIndex:7];
|
| 314 |
+
[encoder setBuffer:getMTLBufferStorage(cu_seqlens_k)
|
| 315 |
+
offset:cu_seqlens_k.storage_offset() * cu_seqlens_k.element_size()
|
| 316 |
+
atIndex:8];
|
| 317 |
+
|
| 318 |
+
// Calculate grid dimensions
|
| 319 |
+
// We need to process each sequence independently
|
| 320 |
+
int64_t max_blocks_q = (max_seqlen_q + BQ - 1) / BQ;
|
| 321 |
+
|
| 322 |
+
MTLSize gridSize = MTLSizeMake(max_blocks_q, num_heads, batch_size);
|
| 323 |
+
MTLSize threadgroupSize = MTLSizeMake(32, WM, WN);
|
| 324 |
+
|
| 325 |
+
[encoder dispatchThreadgroups:gridSize threadsPerThreadgroup:threadgroupSize];
|
| 326 |
+
[encoder endEncoding];
|
| 327 |
+
|
| 328 |
+
stream->synchronize(at::mps::SyncType::COMMIT);
|
| 329 |
+
});
|
| 330 |
+
}
|
tests/__init__.py
ADDED
|
File without changes
|
tests/test_flash_attention.py
ADDED
|
@@ -0,0 +1,1132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import pytest
|
| 3 |
+
import sdpa_flash
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def create_cu_seqlens(seq_lengths):
|
| 7 |
+
"""Create cumulative sequence lengths tensor."""
|
| 8 |
+
cu_seqlens = [0]
|
| 9 |
+
for length in seq_lengths:
|
| 10 |
+
cu_seqlens.append(cu_seqlens[-1] + length)
|
| 11 |
+
return torch.tensor(cu_seqlens, dtype=torch.int32, device="mps")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 15 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 16 |
+
def test_flash_attention_single_sequence(dtype, head_dim):
|
| 17 |
+
"""Test Flash Attention with a single sequence."""
|
| 18 |
+
torch.manual_seed(42)
|
| 19 |
+
|
| 20 |
+
# Single sequence
|
| 21 |
+
seq_len = 32
|
| 22 |
+
num_heads = 4
|
| 23 |
+
|
| 24 |
+
# Create cumulative sequence lengths
|
| 25 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 26 |
+
|
| 27 |
+
# Create input tensors in Flash Attention format
|
| 28 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 29 |
+
key = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 30 |
+
value = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 31 |
+
|
| 32 |
+
# Scale factor
|
| 33 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 34 |
+
|
| 35 |
+
# Call Flash Attention
|
| 36 |
+
out = torch.empty_like(query)
|
| 37 |
+
sdpa_flash.flash_attention_varlen(
|
| 38 |
+
out=out,
|
| 39 |
+
query=query,
|
| 40 |
+
key=key,
|
| 41 |
+
value=value,
|
| 42 |
+
cu_seqlens_q=cu_seqlens,
|
| 43 |
+
cu_seqlens_k=cu_seqlens,
|
| 44 |
+
max_seqlen_q=seq_len,
|
| 45 |
+
max_seqlen_k=seq_len,
|
| 46 |
+
do_causal=False,
|
| 47 |
+
scale=scale,
|
| 48 |
+
softcapping=1.0,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Compute ground truth
|
| 52 |
+
# Flash Attention computes attention separately for each head
|
| 53 |
+
expected = torch.zeros_like(out)
|
| 54 |
+
for h in range(num_heads):
|
| 55 |
+
q_h = query[:, h, :] # [seq_len, head_dim]
|
| 56 |
+
k_h = key[:, h, :]
|
| 57 |
+
v_h = value[:, h, :]
|
| 58 |
+
|
| 59 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 60 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 61 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 62 |
+
|
| 63 |
+
# Check results (higher tolerance for bfloat16 and float16)
|
| 64 |
+
if dtype == torch.bfloat16:
|
| 65 |
+
# Higher tolerance for head_dim=128 with bfloat16
|
| 66 |
+
rtol, atol = (2e-2, 2e-2) if head_dim >= 96 else (1e-2, 1e-2)
|
| 67 |
+
elif dtype == torch.float16:
|
| 68 |
+
rtol, atol = 2e-3, 2e-3
|
| 69 |
+
else:
|
| 70 |
+
rtol, atol = 1e-3, 1e-3
|
| 71 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 75 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 76 |
+
def test_flash_attention_variable_lengths(dtype, head_dim):
|
| 77 |
+
"""Test Flash Attention with variable-length sequences."""
|
| 78 |
+
torch.manual_seed(42)
|
| 79 |
+
|
| 80 |
+
# Variable sequence lengths
|
| 81 |
+
seq_lengths_q = [8, 16, 12]
|
| 82 |
+
seq_lengths_k = [10, 20, 15]
|
| 83 |
+
batch_size = len(seq_lengths_q)
|
| 84 |
+
num_heads = 4
|
| 85 |
+
|
| 86 |
+
# Create cumulative sequence lengths
|
| 87 |
+
cu_seqlens_q = create_cu_seqlens(seq_lengths_q)
|
| 88 |
+
cu_seqlens_k = create_cu_seqlens(seq_lengths_k)
|
| 89 |
+
|
| 90 |
+
total_q = sum(seq_lengths_q)
|
| 91 |
+
total_k = sum(seq_lengths_k)
|
| 92 |
+
max_seqlen_q = max(seq_lengths_q)
|
| 93 |
+
max_seqlen_k = max(seq_lengths_k)
|
| 94 |
+
|
| 95 |
+
# Create input tensors
|
| 96 |
+
query = torch.randn(total_q, num_heads, head_dim, dtype=dtype, device="mps")
|
| 97 |
+
key = torch.randn(total_k, num_heads, head_dim, dtype=dtype, device="mps")
|
| 98 |
+
value = torch.randn(total_k, num_heads, head_dim, dtype=dtype, device="mps")
|
| 99 |
+
|
| 100 |
+
# Scale factor
|
| 101 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 102 |
+
|
| 103 |
+
# Call Flash Attention
|
| 104 |
+
out = torch.empty_like(query)
|
| 105 |
+
sdpa_flash.flash_attention_varlen(
|
| 106 |
+
out=out,
|
| 107 |
+
query=query,
|
| 108 |
+
key=key,
|
| 109 |
+
value=value,
|
| 110 |
+
cu_seqlens_q=cu_seqlens_q,
|
| 111 |
+
cu_seqlens_k=cu_seqlens_k,
|
| 112 |
+
max_seqlen_q=max_seqlen_q,
|
| 113 |
+
max_seqlen_k=max_seqlen_k,
|
| 114 |
+
do_causal=False,
|
| 115 |
+
scale=scale,
|
| 116 |
+
softcapping=1.0,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Compute ground truth for each sequence
|
| 120 |
+
expected = torch.zeros_like(out)
|
| 121 |
+
for i in range(batch_size):
|
| 122 |
+
q_start, q_end = cu_seqlens_q[i].item(), cu_seqlens_q[i+1].item()
|
| 123 |
+
k_start, k_end = cu_seqlens_k[i].item(), cu_seqlens_k[i+1].item()
|
| 124 |
+
|
| 125 |
+
q_i = query[q_start:q_end]
|
| 126 |
+
k_i = key[k_start:k_end]
|
| 127 |
+
v_i = value[k_start:k_end]
|
| 128 |
+
|
| 129 |
+
# Compute attention for each head separately
|
| 130 |
+
for h in range(num_heads):
|
| 131 |
+
q_h = q_i[:, h, :] # [seq_len, head_dim]
|
| 132 |
+
k_h = k_i[:, h, :]
|
| 133 |
+
v_h = v_i[:, h, :]
|
| 134 |
+
|
| 135 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 136 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 137 |
+
expected[q_start:q_end, h, :] = torch.matmul(attn_weights, v_h)
|
| 138 |
+
|
| 139 |
+
# Check results (higher tolerance for bfloat16 and float16)
|
| 140 |
+
if dtype == torch.bfloat16:
|
| 141 |
+
# Higher tolerance for bfloat16 with variable length sequences
|
| 142 |
+
rtol, atol = 2e-2, 2e-2
|
| 143 |
+
elif dtype == torch.float16:
|
| 144 |
+
rtol, atol = 2e-3, 2e-3
|
| 145 |
+
else:
|
| 146 |
+
rtol, atol = 1e-3, 1e-3
|
| 147 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 151 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 152 |
+
def test_flash_attention_causal(dtype, head_dim):
|
| 153 |
+
"""Test Flash Attention with causal masking."""
|
| 154 |
+
torch.manual_seed(42)
|
| 155 |
+
|
| 156 |
+
# Test dimensions
|
| 157 |
+
seq_lengths = [16, 24]
|
| 158 |
+
batch_size = len(seq_lengths)
|
| 159 |
+
num_heads = 4
|
| 160 |
+
|
| 161 |
+
# Create cumulative sequence lengths
|
| 162 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 163 |
+
total_tokens = sum(seq_lengths)
|
| 164 |
+
max_seqlen = max(seq_lengths)
|
| 165 |
+
|
| 166 |
+
# Create input tensors
|
| 167 |
+
query = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 168 |
+
key = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 169 |
+
value = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 170 |
+
|
| 171 |
+
# Scale factor
|
| 172 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 173 |
+
|
| 174 |
+
# Call Flash Attention with causal mask
|
| 175 |
+
out = torch.empty_like(query)
|
| 176 |
+
sdpa_flash.flash_attention_varlen(
|
| 177 |
+
out=out,
|
| 178 |
+
query=query,
|
| 179 |
+
key=key,
|
| 180 |
+
value=value,
|
| 181 |
+
cu_seqlens_q=cu_seqlens,
|
| 182 |
+
cu_seqlens_k=cu_seqlens,
|
| 183 |
+
max_seqlen_q=max_seqlen,
|
| 184 |
+
max_seqlen_k=max_seqlen,
|
| 185 |
+
do_causal=True,
|
| 186 |
+
scale=scale,
|
| 187 |
+
softcapping=1.0,
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
# Compute ground truth with causal mask
|
| 191 |
+
expected = torch.zeros_like(out)
|
| 192 |
+
for i in range(batch_size):
|
| 193 |
+
start, end = cu_seqlens[i].item(), cu_seqlens[i+1].item()
|
| 194 |
+
seq_len = end - start
|
| 195 |
+
|
| 196 |
+
q_i = query[start:end]
|
| 197 |
+
k_i = key[start:end]
|
| 198 |
+
v_i = value[start:end]
|
| 199 |
+
|
| 200 |
+
# Compute attention for each head separately
|
| 201 |
+
for h in range(num_heads):
|
| 202 |
+
q_h = q_i[:, h, :] # [seq_len, head_dim]
|
| 203 |
+
k_h = k_i[:, h, :]
|
| 204 |
+
v_h = v_i[:, h, :]
|
| 205 |
+
|
| 206 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 207 |
+
|
| 208 |
+
# Apply causal mask
|
| 209 |
+
causal_mask = torch.triu(torch.ones(seq_len, seq_len, device="mps"), diagonal=1).bool()
|
| 210 |
+
scores.masked_fill_(causal_mask, float("-inf"))
|
| 211 |
+
|
| 212 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 213 |
+
expected[start:end, h, :] = torch.matmul(attn_weights, v_h)
|
| 214 |
+
|
| 215 |
+
# Check results (higher tolerance for bfloat16 and float16)
|
| 216 |
+
if dtype == torch.bfloat16:
|
| 217 |
+
# Higher tolerance for head_dim=128 with bfloat16
|
| 218 |
+
rtol, atol = (2e-2, 2e-2) if head_dim >= 96 else (1e-2, 1e-2)
|
| 219 |
+
elif dtype == torch.float16:
|
| 220 |
+
rtol, atol = 2e-3, 2e-3
|
| 221 |
+
else:
|
| 222 |
+
rtol, atol = 1e-3, 1e-3
|
| 223 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 227 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 228 |
+
def test_flash_attention_gqa(dtype, head_dim):
|
| 229 |
+
"""Test Flash Attention with Grouped Query Attention."""
|
| 230 |
+
torch.manual_seed(42)
|
| 231 |
+
|
| 232 |
+
# Test dimensions
|
| 233 |
+
seq_len = 32
|
| 234 |
+
num_heads = 8
|
| 235 |
+
num_kv_heads = 2 # GQA with 4:1 ratio
|
| 236 |
+
|
| 237 |
+
# Create cumulative sequence lengths
|
| 238 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 239 |
+
|
| 240 |
+
# Create input tensors
|
| 241 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 242 |
+
key = torch.randn(seq_len, num_kv_heads, head_dim, dtype=dtype, device="mps")
|
| 243 |
+
value = torch.randn(seq_len, num_kv_heads, head_dim, dtype=dtype, device="mps")
|
| 244 |
+
|
| 245 |
+
# Scale factor
|
| 246 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 247 |
+
|
| 248 |
+
# Call Flash Attention
|
| 249 |
+
out = torch.empty_like(query)
|
| 250 |
+
sdpa_flash.flash_attention_varlen(
|
| 251 |
+
out=out,
|
| 252 |
+
query=query,
|
| 253 |
+
key=key,
|
| 254 |
+
value=value,
|
| 255 |
+
cu_seqlens_q=cu_seqlens,
|
| 256 |
+
cu_seqlens_k=cu_seqlens,
|
| 257 |
+
max_seqlen_q=seq_len,
|
| 258 |
+
max_seqlen_k=seq_len,
|
| 259 |
+
do_causal=False,
|
| 260 |
+
scale=scale,
|
| 261 |
+
softcapping=1.0,
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# Compute ground truth with GQA
|
| 265 |
+
# Each query head attends to its corresponding kv head (with repetition)
|
| 266 |
+
expected = torch.zeros_like(query)
|
| 267 |
+
gqa_factor = num_heads // num_kv_heads
|
| 268 |
+
|
| 269 |
+
for h in range(num_heads):
|
| 270 |
+
kv_h = h // gqa_factor
|
| 271 |
+
q_h = query[:, h, :] # [seq_len, head_dim]
|
| 272 |
+
k_h = key[:, kv_h, :]
|
| 273 |
+
v_h = value[:, kv_h, :]
|
| 274 |
+
|
| 275 |
+
scores = torch.matmul(q_h, k_h.transpose(-2, -1)) * scale
|
| 276 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 277 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 278 |
+
|
| 279 |
+
# Check results (higher tolerance for bfloat16 and float16)
|
| 280 |
+
if dtype == torch.bfloat16:
|
| 281 |
+
# Higher tolerance for head_dim=128 with bfloat16
|
| 282 |
+
rtol, atol = (2e-2, 2e-2) if head_dim >= 96 else (1e-2, 1e-2)
|
| 283 |
+
elif dtype == torch.float16:
|
| 284 |
+
rtol, atol = 2e-3, 2e-3
|
| 285 |
+
else:
|
| 286 |
+
rtol, atol = 1e-3, 1e-3
|
| 287 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 291 |
+
def test_flash_attention_head_dimensions(head_dim):
|
| 292 |
+
"""Test Flash Attention with different supported head dimensions."""
|
| 293 |
+
torch.manual_seed(42)
|
| 294 |
+
|
| 295 |
+
# Test dimensions
|
| 296 |
+
seq_len = 16
|
| 297 |
+
num_heads = 4
|
| 298 |
+
|
| 299 |
+
# Create cumulative sequence lengths
|
| 300 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 301 |
+
|
| 302 |
+
# Create input tensors
|
| 303 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=torch.float32, device="mps")
|
| 304 |
+
key = torch.randn(seq_len, num_heads, head_dim, dtype=torch.float32, device="mps")
|
| 305 |
+
value = torch.randn(seq_len, num_heads, head_dim, dtype=torch.float32, device="mps")
|
| 306 |
+
|
| 307 |
+
# Scale factor
|
| 308 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 309 |
+
|
| 310 |
+
# Call Flash Attention
|
| 311 |
+
out = torch.empty_like(query)
|
| 312 |
+
sdpa_flash.flash_attention_varlen(
|
| 313 |
+
out=out,
|
| 314 |
+
query=query,
|
| 315 |
+
key=key,
|
| 316 |
+
value=value,
|
| 317 |
+
cu_seqlens_q=cu_seqlens,
|
| 318 |
+
cu_seqlens_k=cu_seqlens,
|
| 319 |
+
max_seqlen_q=seq_len,
|
| 320 |
+
max_seqlen_k=seq_len,
|
| 321 |
+
do_causal=False,
|
| 322 |
+
scale=scale,
|
| 323 |
+
softcapping=1.0,
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
# Basic check that output is not zeros
|
| 327 |
+
assert out.abs().max().item() > 0
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 331 |
+
def test_flash_attention_large_head_dim(dtype):
|
| 332 |
+
"""Test Flash Attention with head_dim=128 specifically."""
|
| 333 |
+
torch.manual_seed(42)
|
| 334 |
+
|
| 335 |
+
# Test dimensions with head_dim=128
|
| 336 |
+
seq_lengths = [32, 64]
|
| 337 |
+
batch_size = len(seq_lengths)
|
| 338 |
+
num_heads = 8
|
| 339 |
+
head_dim = 128
|
| 340 |
+
|
| 341 |
+
# Create cumulative sequence lengths
|
| 342 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 343 |
+
total_tokens = sum(seq_lengths)
|
| 344 |
+
max_seqlen = max(seq_lengths)
|
| 345 |
+
|
| 346 |
+
# Create input tensors
|
| 347 |
+
query = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 348 |
+
key = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 349 |
+
value = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 350 |
+
|
| 351 |
+
# Scale factor
|
| 352 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 353 |
+
|
| 354 |
+
# Call Flash Attention
|
| 355 |
+
out = torch.empty_like(query)
|
| 356 |
+
sdpa_flash.flash_attention_varlen(
|
| 357 |
+
out=out,
|
| 358 |
+
query=query,
|
| 359 |
+
key=key,
|
| 360 |
+
value=value,
|
| 361 |
+
cu_seqlens_q=cu_seqlens,
|
| 362 |
+
cu_seqlens_k=cu_seqlens,
|
| 363 |
+
max_seqlen_q=max_seqlen,
|
| 364 |
+
max_seqlen_k=max_seqlen,
|
| 365 |
+
do_causal=False,
|
| 366 |
+
scale=scale,
|
| 367 |
+
softcapping=1.0,
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
# Compute ground truth
|
| 371 |
+
expected = torch.zeros_like(out)
|
| 372 |
+
for i in range(batch_size):
|
| 373 |
+
start, end = cu_seqlens[i].item(), cu_seqlens[i+1].item()
|
| 374 |
+
|
| 375 |
+
q_i = query[start:end]
|
| 376 |
+
k_i = key[start:end]
|
| 377 |
+
v_i = value[start:end]
|
| 378 |
+
|
| 379 |
+
# Compute attention for each head separately
|
| 380 |
+
for h in range(num_heads):
|
| 381 |
+
q_h = q_i[:, h, :] # [seq_len, head_dim]
|
| 382 |
+
k_h = k_i[:, h, :]
|
| 383 |
+
v_h = v_i[:, h, :]
|
| 384 |
+
|
| 385 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 386 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 387 |
+
expected[start:end, h, :] = torch.matmul(attn_weights, v_h)
|
| 388 |
+
|
| 389 |
+
# Check results (higher tolerance for bfloat16 with head_dim=128)
|
| 390 |
+
if dtype == torch.bfloat16:
|
| 391 |
+
# bfloat16 with head_dim=128 has known precision issues
|
| 392 |
+
rtol, atol = 2e-2, 2e-2
|
| 393 |
+
elif dtype == torch.float16:
|
| 394 |
+
rtol, atol = 2e-3, 2e-3
|
| 395 |
+
else:
|
| 396 |
+
rtol, atol = 1e-3, 1e-3
|
| 397 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 401 |
+
def test_flash_attention_large_head_dim_causal(dtype):
|
| 402 |
+
"""Test Flash Attention with head_dim=128 and causal masking."""
|
| 403 |
+
torch.manual_seed(42)
|
| 404 |
+
|
| 405 |
+
# Test dimensions
|
| 406 |
+
seq_len = 48
|
| 407 |
+
num_heads = 4
|
| 408 |
+
head_dim = 128
|
| 409 |
+
|
| 410 |
+
# Create cumulative sequence lengths
|
| 411 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 412 |
+
|
| 413 |
+
# Create input tensors
|
| 414 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 415 |
+
key = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 416 |
+
value = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 417 |
+
|
| 418 |
+
# Scale factor
|
| 419 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 420 |
+
|
| 421 |
+
# Call Flash Attention with causal mask
|
| 422 |
+
out = torch.empty_like(query)
|
| 423 |
+
sdpa_flash.flash_attention_varlen(
|
| 424 |
+
out=out,
|
| 425 |
+
query=query,
|
| 426 |
+
key=key,
|
| 427 |
+
value=value,
|
| 428 |
+
cu_seqlens_q=cu_seqlens,
|
| 429 |
+
cu_seqlens_k=cu_seqlens,
|
| 430 |
+
max_seqlen_q=seq_len,
|
| 431 |
+
max_seqlen_k=seq_len,
|
| 432 |
+
do_causal=True,
|
| 433 |
+
scale=scale,
|
| 434 |
+
softcapping=1.0,
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
# Compute ground truth with causal mask
|
| 438 |
+
expected = torch.zeros_like(out)
|
| 439 |
+
|
| 440 |
+
for h in range(num_heads):
|
| 441 |
+
q_h = query[:, h, :] # [seq_len, head_dim]
|
| 442 |
+
k_h = key[:, h, :]
|
| 443 |
+
v_h = value[:, h, :]
|
| 444 |
+
|
| 445 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 446 |
+
|
| 447 |
+
# Apply causal mask
|
| 448 |
+
causal_mask = torch.triu(torch.ones(seq_len, seq_len, device="mps"), diagonal=1).bool()
|
| 449 |
+
scores.masked_fill_(causal_mask, float("-inf"))
|
| 450 |
+
|
| 451 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 452 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 453 |
+
|
| 454 |
+
# Check results (higher tolerance for bfloat16 with head_dim=128)
|
| 455 |
+
if dtype == torch.bfloat16:
|
| 456 |
+
# bfloat16 with head_dim=128 has known precision issues
|
| 457 |
+
rtol, atol = 2e-2, 2e-2
|
| 458 |
+
elif dtype == torch.float16:
|
| 459 |
+
rtol, atol = 2e-3, 2e-3
|
| 460 |
+
else:
|
| 461 |
+
rtol, atol = 1e-3, 1e-3
|
| 462 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def test_flash_attention_large_head_dim_gqa():
|
| 466 |
+
"""Test Flash Attention with head_dim=128 and GQA."""
|
| 467 |
+
torch.manual_seed(42)
|
| 468 |
+
|
| 469 |
+
# Test dimensions
|
| 470 |
+
seq_len = 32
|
| 471 |
+
num_heads = 16
|
| 472 |
+
num_kv_heads = 4 # GQA with 4:1 ratio
|
| 473 |
+
head_dim = 128
|
| 474 |
+
|
| 475 |
+
# Create cumulative sequence lengths
|
| 476 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 477 |
+
|
| 478 |
+
# Create input tensors
|
| 479 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=torch.float32, device="mps")
|
| 480 |
+
key = torch.randn(seq_len, num_kv_heads, head_dim, dtype=torch.float32, device="mps")
|
| 481 |
+
value = torch.randn(seq_len, num_kv_heads, head_dim, dtype=torch.float32, device="mps")
|
| 482 |
+
|
| 483 |
+
# Scale factor
|
| 484 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 485 |
+
|
| 486 |
+
# Call Flash Attention
|
| 487 |
+
out = torch.empty_like(query)
|
| 488 |
+
sdpa_flash.flash_attention_varlen(
|
| 489 |
+
out=out,
|
| 490 |
+
query=query,
|
| 491 |
+
key=key,
|
| 492 |
+
value=value,
|
| 493 |
+
cu_seqlens_q=cu_seqlens,
|
| 494 |
+
cu_seqlens_k=cu_seqlens,
|
| 495 |
+
max_seqlen_q=seq_len,
|
| 496 |
+
max_seqlen_k=seq_len,
|
| 497 |
+
do_causal=False,
|
| 498 |
+
scale=scale,
|
| 499 |
+
softcapping=1.0,
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
+
# Compute ground truth with GQA
|
| 503 |
+
expected = torch.zeros_like(query)
|
| 504 |
+
gqa_factor = num_heads // num_kv_heads
|
| 505 |
+
|
| 506 |
+
for h in range(num_heads):
|
| 507 |
+
kv_h = h // gqa_factor
|
| 508 |
+
q_h = query[:, h, :] # [seq_len, head_dim]
|
| 509 |
+
k_h = key[:, kv_h, :]
|
| 510 |
+
v_h = value[:, kv_h, :]
|
| 511 |
+
|
| 512 |
+
scores = torch.matmul(q_h, k_h.transpose(-2, -1)) * scale
|
| 513 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 514 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 515 |
+
|
| 516 |
+
# Check results
|
| 517 |
+
torch.testing.assert_close(out, expected, rtol=1e-3, atol=1e-3)
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
def test_flash_attention_edge_cases():
|
| 521 |
+
"""Test Flash Attention edge cases."""
|
| 522 |
+
torch.manual_seed(42)
|
| 523 |
+
|
| 524 |
+
# Test 1: Single token sequence
|
| 525 |
+
query = torch.randn(1, 1, 64, device="mps")
|
| 526 |
+
key = torch.randn(1, 1, 64, device="mps")
|
| 527 |
+
value = torch.randn(1, 1, 64, device="mps")
|
| 528 |
+
cu_seqlens = create_cu_seqlens([1])
|
| 529 |
+
out = torch.empty_like(query)
|
| 530 |
+
|
| 531 |
+
sdpa_flash.flash_attention_varlen(
|
| 532 |
+
out=out,
|
| 533 |
+
query=query,
|
| 534 |
+
key=key,
|
| 535 |
+
value=value,
|
| 536 |
+
cu_seqlens_q=cu_seqlens,
|
| 537 |
+
cu_seqlens_k=cu_seqlens,
|
| 538 |
+
max_seqlen_q=1,
|
| 539 |
+
max_seqlen_k=1,
|
| 540 |
+
do_causal=False,
|
| 541 |
+
scale=0.125,
|
| 542 |
+
softcapping=1.0,
|
| 543 |
+
)
|
| 544 |
+
|
| 545 |
+
# With single token, output should equal value
|
| 546 |
+
torch.testing.assert_close(out, value, rtol=1e-5, atol=1e-5)
|
| 547 |
+
|
| 548 |
+
# Test 2: Empty sequence in batch
|
| 549 |
+
seq_lengths = [8, 0, 12] # Middle sequence is empty
|
| 550 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 551 |
+
total_tokens = sum(seq_lengths)
|
| 552 |
+
|
| 553 |
+
query = torch.randn(total_tokens, 4, 64, device="mps")
|
| 554 |
+
key = torch.randn(total_tokens, 4, 64, device="mps")
|
| 555 |
+
value = torch.randn(total_tokens, 4, 64, device="mps")
|
| 556 |
+
out = torch.empty_like(query)
|
| 557 |
+
|
| 558 |
+
# This should handle empty sequences gracefully
|
| 559 |
+
sdpa_flash.flash_attention_varlen(
|
| 560 |
+
out=out,
|
| 561 |
+
query=query,
|
| 562 |
+
key=key,
|
| 563 |
+
value=value,
|
| 564 |
+
cu_seqlens_q=cu_seqlens,
|
| 565 |
+
cu_seqlens_k=cu_seqlens,
|
| 566 |
+
max_seqlen_q=max(seq_lengths) if seq_lengths else 0,
|
| 567 |
+
max_seqlen_k=max(seq_lengths) if seq_lengths else 0,
|
| 568 |
+
do_causal=False,
|
| 569 |
+
scale=0.125,
|
| 570 |
+
softcapping=1.0,
|
| 571 |
+
)
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
def test_flash_attention_unsupported_cases():
|
| 575 |
+
"""Test that unsupported cases raise appropriate errors."""
|
| 576 |
+
|
| 577 |
+
# Test 1: Unsupported head dimension
|
| 578 |
+
query = torch.randn(16, 4, 48, device="mps") # head_dim = 48 (not supported)
|
| 579 |
+
key = torch.randn(16, 4, 48, device="mps")
|
| 580 |
+
value = torch.randn(16, 4, 48, device="mps")
|
| 581 |
+
cu_seqlens = create_cu_seqlens([16])
|
| 582 |
+
out = torch.empty_like(query)
|
| 583 |
+
|
| 584 |
+
with pytest.raises(RuntimeError, match="Head dimension .* is not supported"):
|
| 585 |
+
sdpa_flash.flash_attention_varlen(
|
| 586 |
+
out=out,
|
| 587 |
+
query=query,
|
| 588 |
+
key=key,
|
| 589 |
+
value=value,
|
| 590 |
+
cu_seqlens_q=cu_seqlens,
|
| 591 |
+
cu_seqlens_k=cu_seqlens,
|
| 592 |
+
max_seqlen_q=16,
|
| 593 |
+
max_seqlen_k=16,
|
| 594 |
+
do_causal=False,
|
| 595 |
+
scale=0.144,
|
| 596 |
+
softcapping=1.0,
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
+
# Test 2: Calling function with wrong number of arguments
|
| 600 |
+
query = torch.randn(16, 4, 64, device="mps")
|
| 601 |
+
key = torch.randn(16, 4, 64, device="mps")
|
| 602 |
+
value = torch.randn(16, 4, 64, device="mps")
|
| 603 |
+
mask = torch.randn(1, 1, 16, 16, device="mps")
|
| 604 |
+
cu_seqlens = create_cu_seqlens([16])
|
| 605 |
+
out = torch.empty_like(query)
|
| 606 |
+
|
| 607 |
+
# The function signature no longer accepts mask parameter
|
| 608 |
+
with pytest.raises(TypeError):
|
| 609 |
+
sdpa_flash.flash_attention_varlen(
|
| 610 |
+
out=out,
|
| 611 |
+
query=query,
|
| 612 |
+
key=key,
|
| 613 |
+
value=value,
|
| 614 |
+
cu_seqlens_q=cu_seqlens,
|
| 615 |
+
cu_seqlens_k=cu_seqlens,
|
| 616 |
+
max_seqlen_q=16,
|
| 617 |
+
max_seqlen_k=16,
|
| 618 |
+
mask=mask, # This parameter doesn't exist anymore
|
| 619 |
+
do_causal=False,
|
| 620 |
+
scale=0.125,
|
| 621 |
+
softcapping=1.0,
|
| 622 |
+
)
|
| 623 |
+
|
| 624 |
+
# Test 3: Wrong dtype for cu_seqlens (should be int32)
|
| 625 |
+
cu_seqlens_wrong = torch.tensor([0, 16], dtype=torch.int64, device="mps")
|
| 626 |
+
|
| 627 |
+
# This will silently fail (output will be unchanged)
|
| 628 |
+
# We can detect this by initializing output to a known value
|
| 629 |
+
out = torch.full_like(query, -999.0)
|
| 630 |
+
sdpa_flash.flash_attention_varlen(
|
| 631 |
+
out=out,
|
| 632 |
+
query=query,
|
| 633 |
+
key=key,
|
| 634 |
+
value=value,
|
| 635 |
+
cu_seqlens_q=cu_seqlens_wrong,
|
| 636 |
+
cu_seqlens_k=cu_seqlens_wrong,
|
| 637 |
+
max_seqlen_q=16,
|
| 638 |
+
max_seqlen_k=16,
|
| 639 |
+
do_causal=False,
|
| 640 |
+
scale=0.125,
|
| 641 |
+
softcapping=1.0,
|
| 642 |
+
)
|
| 643 |
+
|
| 644 |
+
# Check that output wasn't modified (kernel didn't run)
|
| 645 |
+
assert (out == -999.0).all(), "cu_seqlens with wrong dtype should cause kernel to not run"
|
| 646 |
+
|
| 647 |
+
|
| 648 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 649 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 650 |
+
def test_flash_attention_small_sequences(dtype, head_dim):
|
| 651 |
+
"""Test Flash Attention with small sequence lengths (2-8)."""
|
| 652 |
+
torch.manual_seed(42)
|
| 653 |
+
|
| 654 |
+
# Test different small sequence lengths
|
| 655 |
+
for seq_len in [2, 4, 6, 8]:
|
| 656 |
+
num_heads = 4
|
| 657 |
+
|
| 658 |
+
# Create cumulative sequence lengths
|
| 659 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 660 |
+
|
| 661 |
+
# Create input tensors
|
| 662 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 663 |
+
key = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 664 |
+
value = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 665 |
+
|
| 666 |
+
# Scale factor
|
| 667 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 668 |
+
|
| 669 |
+
# Call Flash Attention
|
| 670 |
+
out = torch.empty_like(query)
|
| 671 |
+
sdpa_flash.flash_attention_varlen(
|
| 672 |
+
out=out,
|
| 673 |
+
query=query,
|
| 674 |
+
key=key,
|
| 675 |
+
value=value,
|
| 676 |
+
cu_seqlens_q=cu_seqlens,
|
| 677 |
+
cu_seqlens_k=cu_seqlens,
|
| 678 |
+
max_seqlen_q=seq_len,
|
| 679 |
+
max_seqlen_k=seq_len,
|
| 680 |
+
do_causal=False,
|
| 681 |
+
scale=scale,
|
| 682 |
+
softcapping=1.0,
|
| 683 |
+
)
|
| 684 |
+
|
| 685 |
+
# Compute ground truth
|
| 686 |
+
expected = torch.zeros_like(out)
|
| 687 |
+
for h in range(num_heads):
|
| 688 |
+
q_h = query[:, h, :] # [seq_len, head_dim]
|
| 689 |
+
k_h = key[:, h, :]
|
| 690 |
+
v_h = value[:, h, :]
|
| 691 |
+
|
| 692 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 693 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 694 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 695 |
+
|
| 696 |
+
# Check results (higher tolerance for bfloat16)
|
| 697 |
+
if dtype == torch.bfloat16:
|
| 698 |
+
rtol, atol = 2e-2, 2e-2
|
| 699 |
+
elif dtype == torch.float16:
|
| 700 |
+
rtol, atol = 2e-3, 2e-3
|
| 701 |
+
else:
|
| 702 |
+
rtol, atol = 1e-3, 1e-3
|
| 703 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 704 |
+
|
| 705 |
+
|
| 706 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 707 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 708 |
+
def test_flash_attention_cross_attention(dtype, head_dim):
|
| 709 |
+
"""Test Flash Attention with different q_seq and k_seq (cross-attention)."""
|
| 710 |
+
torch.manual_seed(42)
|
| 711 |
+
|
| 712 |
+
# Test various q_seq, k_seq combinations
|
| 713 |
+
test_cases = [
|
| 714 |
+
(16, 32), # q_seq < k_seq
|
| 715 |
+
(32, 16), # q_seq > k_seq
|
| 716 |
+
(8, 128), # large difference
|
| 717 |
+
(1, 64), # single query token
|
| 718 |
+
]
|
| 719 |
+
|
| 720 |
+
for q_seq, k_seq in test_cases:
|
| 721 |
+
num_heads = 4
|
| 722 |
+
|
| 723 |
+
# Create cumulative sequence lengths
|
| 724 |
+
cu_seqlens_q = create_cu_seqlens([q_seq])
|
| 725 |
+
cu_seqlens_k = create_cu_seqlens([k_seq])
|
| 726 |
+
|
| 727 |
+
# Create input tensors
|
| 728 |
+
query = torch.randn(q_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 729 |
+
key = torch.randn(k_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 730 |
+
value = torch.randn(k_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 731 |
+
|
| 732 |
+
# Scale factor
|
| 733 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 734 |
+
|
| 735 |
+
# Call Flash Attention
|
| 736 |
+
out = torch.empty_like(query)
|
| 737 |
+
sdpa_flash.flash_attention_varlen(
|
| 738 |
+
out=out,
|
| 739 |
+
query=query,
|
| 740 |
+
key=key,
|
| 741 |
+
value=value,
|
| 742 |
+
cu_seqlens_q=cu_seqlens_q,
|
| 743 |
+
cu_seqlens_k=cu_seqlens_k,
|
| 744 |
+
max_seqlen_q=q_seq,
|
| 745 |
+
max_seqlen_k=k_seq,
|
| 746 |
+
do_causal=False,
|
| 747 |
+
scale=scale,
|
| 748 |
+
softcapping=1.0,
|
| 749 |
+
)
|
| 750 |
+
|
| 751 |
+
# Compute ground truth
|
| 752 |
+
expected = torch.zeros_like(out)
|
| 753 |
+
for h in range(num_heads):
|
| 754 |
+
q_h = query[:, h, :] # [q_seq, head_dim]
|
| 755 |
+
k_h = key[:, h, :] # [k_seq, head_dim]
|
| 756 |
+
v_h = value[:, h, :] # [k_seq, head_dim]
|
| 757 |
+
|
| 758 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 759 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 760 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 761 |
+
|
| 762 |
+
# Check results (higher tolerance for bfloat16)
|
| 763 |
+
if dtype == torch.bfloat16:
|
| 764 |
+
rtol, atol = 2e-2, 2e-2
|
| 765 |
+
elif dtype == torch.float16:
|
| 766 |
+
rtol, atol = 2e-3, 2e-3
|
| 767 |
+
else:
|
| 768 |
+
rtol, atol = 1e-3, 1e-3
|
| 769 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 770 |
+
|
| 771 |
+
|
| 772 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 773 |
+
def test_flash_attention_large_sequences(dtype):
|
| 774 |
+
"""Test Flash Attention with large k_seq (>= 1024)."""
|
| 775 |
+
torch.manual_seed(42)
|
| 776 |
+
|
| 777 |
+
# Test dimensions - large k_seq to test 2-pass algorithms
|
| 778 |
+
q_seq = 32
|
| 779 |
+
k_seq = 2048 # Large k_seq
|
| 780 |
+
num_heads = 4
|
| 781 |
+
head_dim = 64 # Use smaller head_dim to avoid memory issues
|
| 782 |
+
|
| 783 |
+
# Create cumulative sequence lengths
|
| 784 |
+
cu_seqlens_q = create_cu_seqlens([q_seq])
|
| 785 |
+
cu_seqlens_k = create_cu_seqlens([k_seq])
|
| 786 |
+
|
| 787 |
+
# Create input tensors
|
| 788 |
+
query = torch.randn(q_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 789 |
+
key = torch.randn(k_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 790 |
+
value = torch.randn(k_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 791 |
+
|
| 792 |
+
# Scale factor
|
| 793 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 794 |
+
|
| 795 |
+
# Call Flash Attention
|
| 796 |
+
out = torch.empty_like(query)
|
| 797 |
+
sdpa_flash.flash_attention_varlen(
|
| 798 |
+
out=out,
|
| 799 |
+
query=query,
|
| 800 |
+
key=key,
|
| 801 |
+
value=value,
|
| 802 |
+
cu_seqlens_q=cu_seqlens_q,
|
| 803 |
+
cu_seqlens_k=cu_seqlens_k,
|
| 804 |
+
max_seqlen_q=q_seq,
|
| 805 |
+
max_seqlen_k=k_seq,
|
| 806 |
+
do_causal=False,
|
| 807 |
+
scale=scale,
|
| 808 |
+
softcapping=1.0,
|
| 809 |
+
)
|
| 810 |
+
|
| 811 |
+
# Compute ground truth
|
| 812 |
+
expected = torch.zeros_like(out)
|
| 813 |
+
for h in range(num_heads):
|
| 814 |
+
q_h = query[:, h, :] # [q_seq, head_dim]
|
| 815 |
+
k_h = key[:, h, :] # [k_seq, head_dim]
|
| 816 |
+
v_h = value[:, h, :] # [k_seq, head_dim]
|
| 817 |
+
|
| 818 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 819 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 820 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 821 |
+
|
| 822 |
+
# Check results (higher tolerance for large sequences)
|
| 823 |
+
if dtype == torch.bfloat16:
|
| 824 |
+
rtol, atol = 3e-2, 3e-2
|
| 825 |
+
elif dtype == torch.float16:
|
| 826 |
+
rtol, atol = 5e-3, 5e-3
|
| 827 |
+
else:
|
| 828 |
+
rtol, atol = 2e-3, 2e-3
|
| 829 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 830 |
+
|
| 831 |
+
|
| 832 |
+
@pytest.mark.parametrize("gqa_ratio", [2, 4, 8])
|
| 833 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128])
|
| 834 |
+
def test_flash_attention_gqa_ratios(gqa_ratio, head_dim):
|
| 835 |
+
"""Test Flash Attention with different GQA ratios."""
|
| 836 |
+
torch.manual_seed(42)
|
| 837 |
+
|
| 838 |
+
# Test dimensions
|
| 839 |
+
seq_len = 32
|
| 840 |
+
num_heads = 16
|
| 841 |
+
num_kv_heads = num_heads // gqa_ratio
|
| 842 |
+
dtype = torch.float32
|
| 843 |
+
|
| 844 |
+
# Create cumulative sequence lengths
|
| 845 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 846 |
+
|
| 847 |
+
# Create input tensors
|
| 848 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 849 |
+
key = torch.randn(seq_len, num_kv_heads, head_dim, dtype=dtype, device="mps")
|
| 850 |
+
value = torch.randn(seq_len, num_kv_heads, head_dim, dtype=dtype, device="mps")
|
| 851 |
+
|
| 852 |
+
# Scale factor
|
| 853 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 854 |
+
|
| 855 |
+
# Call Flash Attention
|
| 856 |
+
out = torch.empty_like(query)
|
| 857 |
+
sdpa_flash.flash_attention_varlen(
|
| 858 |
+
out=out,
|
| 859 |
+
query=query,
|
| 860 |
+
key=key,
|
| 861 |
+
value=value,
|
| 862 |
+
cu_seqlens_q=cu_seqlens,
|
| 863 |
+
cu_seqlens_k=cu_seqlens,
|
| 864 |
+
max_seqlen_q=seq_len,
|
| 865 |
+
max_seqlen_k=seq_len,
|
| 866 |
+
do_causal=False,
|
| 867 |
+
scale=scale,
|
| 868 |
+
softcapping=1.0,
|
| 869 |
+
)
|
| 870 |
+
|
| 871 |
+
# Compute ground truth with GQA
|
| 872 |
+
expected = torch.zeros_like(query)
|
| 873 |
+
gqa_factor = num_heads // num_kv_heads
|
| 874 |
+
|
| 875 |
+
for h in range(num_heads):
|
| 876 |
+
kv_h = h // gqa_factor
|
| 877 |
+
q_h = query[:, h, :] # [seq_len, head_dim]
|
| 878 |
+
k_h = key[:, kv_h, :]
|
| 879 |
+
v_h = value[:, kv_h, :]
|
| 880 |
+
|
| 881 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 882 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 883 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 884 |
+
|
| 885 |
+
# Check results
|
| 886 |
+
torch.testing.assert_close(out, expected, rtol=1e-3, atol=1e-3)
|
| 887 |
+
|
| 888 |
+
|
| 889 |
+
def test_flash_attention_single_query_token():
|
| 890 |
+
"""Test Flash Attention with single query token (q_seq = 1)."""
|
| 891 |
+
torch.manual_seed(42)
|
| 892 |
+
|
| 893 |
+
# Test dimensions - single query token
|
| 894 |
+
q_seq = 1
|
| 895 |
+
k_seq = 64
|
| 896 |
+
num_heads = 8
|
| 897 |
+
head_dim = 64
|
| 898 |
+
dtype = torch.float32
|
| 899 |
+
|
| 900 |
+
# Create cumulative sequence lengths
|
| 901 |
+
cu_seqlens_q = create_cu_seqlens([q_seq])
|
| 902 |
+
cu_seqlens_k = create_cu_seqlens([k_seq])
|
| 903 |
+
|
| 904 |
+
# Create input tensors
|
| 905 |
+
query = torch.randn(q_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 906 |
+
key = torch.randn(k_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 907 |
+
value = torch.randn(k_seq, num_heads, head_dim, dtype=dtype, device="mps")
|
| 908 |
+
|
| 909 |
+
# Scale factor
|
| 910 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 911 |
+
|
| 912 |
+
# Call Flash Attention
|
| 913 |
+
out = torch.empty_like(query)
|
| 914 |
+
sdpa_flash.flash_attention_varlen(
|
| 915 |
+
out=out,
|
| 916 |
+
query=query,
|
| 917 |
+
key=key,
|
| 918 |
+
value=value,
|
| 919 |
+
cu_seqlens_q=cu_seqlens_q,
|
| 920 |
+
cu_seqlens_k=cu_seqlens_k,
|
| 921 |
+
max_seqlen_q=q_seq,
|
| 922 |
+
max_seqlen_k=k_seq,
|
| 923 |
+
do_causal=False,
|
| 924 |
+
scale=scale,
|
| 925 |
+
softcapping=1.0,
|
| 926 |
+
)
|
| 927 |
+
|
| 928 |
+
# With single token, output should be weighted average of values
|
| 929 |
+
expected = torch.zeros_like(out)
|
| 930 |
+
for h in range(num_heads):
|
| 931 |
+
q_h = query[:, h, :] # [1, head_dim]
|
| 932 |
+
k_h = key[:, h, :] # [k_seq, head_dim]
|
| 933 |
+
v_h = value[:, h, :] # [k_seq, head_dim]
|
| 934 |
+
|
| 935 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 936 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 937 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 938 |
+
|
| 939 |
+
torch.testing.assert_close(out, expected, rtol=1e-3, atol=1e-3)
|
| 940 |
+
|
| 941 |
+
|
| 942 |
+
def test_flash_attn_varlen_func():
|
| 943 |
+
"""Test the flash_attn_varlen_func compatibility function."""
|
| 944 |
+
torch.manual_seed(42)
|
| 945 |
+
|
| 946 |
+
# Test dimensions
|
| 947 |
+
seq_lengths = [8, 12]
|
| 948 |
+
num_heads = 4
|
| 949 |
+
head_dim = 64
|
| 950 |
+
|
| 951 |
+
# Create cumulative sequence lengths
|
| 952 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 953 |
+
total_tokens = sum(seq_lengths)
|
| 954 |
+
max_seqlen = max(seq_lengths)
|
| 955 |
+
|
| 956 |
+
# Create input tensors
|
| 957 |
+
q = torch.randn(total_tokens, num_heads, head_dim, device="mps")
|
| 958 |
+
k = torch.randn(total_tokens, num_heads, head_dim, device="mps")
|
| 959 |
+
v = torch.randn(total_tokens, num_heads, head_dim, device="mps")
|
| 960 |
+
|
| 961 |
+
# Call the compatibility function
|
| 962 |
+
out = sdpa_flash.flash_attn_varlen_func(
|
| 963 |
+
q=q,
|
| 964 |
+
k=k,
|
| 965 |
+
v=v,
|
| 966 |
+
cu_seqlens_q=cu_seqlens,
|
| 967 |
+
cu_seqlens_k=cu_seqlens,
|
| 968 |
+
max_seqlen_q=max_seqlen,
|
| 969 |
+
max_seqlen_k=max_seqlen,
|
| 970 |
+
dropout_p=0.0,
|
| 971 |
+
softmax_scale=None, # Will use 1/sqrt(head_dim)
|
| 972 |
+
causal=False,
|
| 973 |
+
)
|
| 974 |
+
|
| 975 |
+
# Check that output has correct shape and is not zeros
|
| 976 |
+
assert out.shape == q.shape
|
| 977 |
+
assert out.abs().max().item() > 0
|
| 978 |
+
|
| 979 |
+
# Test with causal
|
| 980 |
+
out_causal = sdpa_flash.flash_attn_varlen_func(
|
| 981 |
+
q=q,
|
| 982 |
+
k=k,
|
| 983 |
+
v=v,
|
| 984 |
+
cu_seqlens_q=cu_seqlens,
|
| 985 |
+
cu_seqlens_k=cu_seqlens,
|
| 986 |
+
max_seqlen_q=max_seqlen,
|
| 987 |
+
max_seqlen_k=max_seqlen,
|
| 988 |
+
dropout_p=0.0,
|
| 989 |
+
softmax_scale=0.125,
|
| 990 |
+
causal=True,
|
| 991 |
+
)
|
| 992 |
+
|
| 993 |
+
assert out_causal.shape == q.shape
|
| 994 |
+
assert out_causal.abs().max().item() > 0
|
| 995 |
+
|
| 996 |
+
|
| 997 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 998 |
+
@pytest.mark.parametrize("head_dim", [32, 64, 72, 80, 96, 128, 256])
|
| 999 |
+
def test_flash_attention_softcapping(dtype, head_dim):
|
| 1000 |
+
"""Test Flash Attention with softcapping."""
|
| 1001 |
+
torch.manual_seed(42)
|
| 1002 |
+
|
| 1003 |
+
# Test dimensions
|
| 1004 |
+
seq_lengths = [32, 24]
|
| 1005 |
+
num_heads = 4
|
| 1006 |
+
softcapping = 50.0
|
| 1007 |
+
|
| 1008 |
+
# Create cumulative sequence lengths
|
| 1009 |
+
cu_seqlens = create_cu_seqlens(seq_lengths)
|
| 1010 |
+
total_tokens = sum(seq_lengths)
|
| 1011 |
+
max_seqlen = max(seq_lengths)
|
| 1012 |
+
|
| 1013 |
+
# Create input tensors
|
| 1014 |
+
query = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 1015 |
+
key = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 1016 |
+
value = torch.randn(total_tokens, num_heads, head_dim, dtype=dtype, device="mps")
|
| 1017 |
+
|
| 1018 |
+
# Scale factor
|
| 1019 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 1020 |
+
|
| 1021 |
+
# Call Flash Attention with softcapping
|
| 1022 |
+
out = torch.empty_like(query)
|
| 1023 |
+
sdpa_flash.flash_attention_varlen(
|
| 1024 |
+
out=out,
|
| 1025 |
+
query=query,
|
| 1026 |
+
key=key,
|
| 1027 |
+
value=value,
|
| 1028 |
+
cu_seqlens_q=cu_seqlens,
|
| 1029 |
+
cu_seqlens_k=cu_seqlens,
|
| 1030 |
+
max_seqlen_q=max_seqlen,
|
| 1031 |
+
max_seqlen_k=max_seqlen,
|
| 1032 |
+
do_causal=False,
|
| 1033 |
+
scale=scale,
|
| 1034 |
+
softcapping=softcapping,
|
| 1035 |
+
)
|
| 1036 |
+
|
| 1037 |
+
# Compute ground truth with softcapping
|
| 1038 |
+
# The kernel applies: softmax(tanh(qk^T*scale/cap)*cap)v
|
| 1039 |
+
expected = torch.zeros_like(query)
|
| 1040 |
+
|
| 1041 |
+
for i, (start, end) in enumerate(zip(cu_seqlens[:-1], cu_seqlens[1:])):
|
| 1042 |
+
q_seq = query[start:end]
|
| 1043 |
+
k_seq = key[start:end]
|
| 1044 |
+
v_seq = value[start:end]
|
| 1045 |
+
|
| 1046 |
+
for h in range(num_heads):
|
| 1047 |
+
q_h = q_seq[:, h, :]
|
| 1048 |
+
k_h = k_seq[:, h, :]
|
| 1049 |
+
v_h = v_seq[:, h, :]
|
| 1050 |
+
|
| 1051 |
+
# Apply softcapping formula
|
| 1052 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * (scale / softcapping)
|
| 1053 |
+
scores = torch.tanh(scores) * softcapping
|
| 1054 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 1055 |
+
expected[start:end, h, :] = torch.matmul(attn_weights, v_h)
|
| 1056 |
+
|
| 1057 |
+
# Check results (higher tolerance for bfloat16 and softcapping)
|
| 1058 |
+
if dtype == torch.bfloat16:
|
| 1059 |
+
rtol, atol = 3e-2, 3e-2
|
| 1060 |
+
elif dtype == torch.float16:
|
| 1061 |
+
rtol, atol = 2e-2, 2e-2
|
| 1062 |
+
else:
|
| 1063 |
+
rtol, atol = 1e-2, 1e-2
|
| 1064 |
+
torch.testing.assert_close(out, expected, rtol=rtol, atol=atol)
|
| 1065 |
+
|
| 1066 |
+
|
| 1067 |
+
@pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16])
|
| 1068 |
+
def test_flash_attention_softcapping_edge_cases(dtype):
|
| 1069 |
+
"""Test Flash Attention softcapping with edge cases."""
|
| 1070 |
+
torch.manual_seed(42)
|
| 1071 |
+
|
| 1072 |
+
# Test with softcapping = 1.0 (no softcapping)
|
| 1073 |
+
seq_len = 16
|
| 1074 |
+
num_heads = 2
|
| 1075 |
+
head_dim = 64
|
| 1076 |
+
|
| 1077 |
+
cu_seqlens = create_cu_seqlens([seq_len])
|
| 1078 |
+
query = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 1079 |
+
key = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 1080 |
+
value = torch.randn(seq_len, num_heads, head_dim, dtype=dtype, device="mps")
|
| 1081 |
+
|
| 1082 |
+
scale = 1.0 / (head_dim ** 0.5)
|
| 1083 |
+
|
| 1084 |
+
# With softcapping = 1.0 (no effect)
|
| 1085 |
+
out_no_cap = torch.empty_like(query)
|
| 1086 |
+
sdpa_flash.flash_attention_varlen(
|
| 1087 |
+
out=out_no_cap,
|
| 1088 |
+
query=query,
|
| 1089 |
+
key=key,
|
| 1090 |
+
value=value,
|
| 1091 |
+
cu_seqlens_q=cu_seqlens,
|
| 1092 |
+
cu_seqlens_k=cu_seqlens,
|
| 1093 |
+
max_seqlen_q=seq_len,
|
| 1094 |
+
max_seqlen_k=seq_len,
|
| 1095 |
+
do_causal=False,
|
| 1096 |
+
scale=scale,
|
| 1097 |
+
softcapping=1.0,
|
| 1098 |
+
)
|
| 1099 |
+
|
| 1100 |
+
# Regular computation without softcapping
|
| 1101 |
+
expected = torch.zeros_like(query)
|
| 1102 |
+
for h in range(num_heads):
|
| 1103 |
+
q_h = query[:, h, :]
|
| 1104 |
+
k_h = key[:, h, :]
|
| 1105 |
+
v_h = value[:, h, :]
|
| 1106 |
+
|
| 1107 |
+
scores = torch.matmul(q_h, k_h.transpose(-1, -2)) * scale
|
| 1108 |
+
attn_weights = torch.softmax(scores, dim=-1)
|
| 1109 |
+
expected[:, h, :] = torch.matmul(attn_weights, v_h)
|
| 1110 |
+
|
| 1111 |
+
# Should be identical when softcapping = 1.0
|
| 1112 |
+
rtol, atol = (2e-2, 2e-2) if dtype != torch.float32 else (1e-3, 1e-3)
|
| 1113 |
+
torch.testing.assert_close(out_no_cap, expected, rtol=rtol, atol=atol)
|
| 1114 |
+
|
| 1115 |
+
# Test with very large softcapping value
|
| 1116 |
+
out_large_cap = torch.empty_like(query)
|
| 1117 |
+
sdpa_flash.flash_attention_varlen(
|
| 1118 |
+
out=out_large_cap,
|
| 1119 |
+
query=query,
|
| 1120 |
+
key=key,
|
| 1121 |
+
value=value,
|
| 1122 |
+
cu_seqlens_q=cu_seqlens,
|
| 1123 |
+
cu_seqlens_k=cu_seqlens,
|
| 1124 |
+
max_seqlen_q=seq_len,
|
| 1125 |
+
max_seqlen_k=seq_len,
|
| 1126 |
+
do_causal=False,
|
| 1127 |
+
scale=scale,
|
| 1128 |
+
softcapping=1000.0,
|
| 1129 |
+
)
|
| 1130 |
+
|
| 1131 |
+
# With very large softcapping, should be close to no softcapping
|
| 1132 |
+
torch.testing.assert_close(out_large_cap, expected, rtol=rtol, atol=atol)
|
torch-ext/sdpa_flash/__init__.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ._custom_ops import (
|
| 2 |
+
flash_attention_varlen,
|
| 3 |
+
flash_attn_varlen_func,
|
| 4 |
+
)
|
| 5 |
+
from ._ops import ops
|
| 6 |
+
|
| 7 |
+
__all__ = [
|
| 8 |
+
"flash_attention_varlen",
|
| 9 |
+
"flash_attn_varlen_func",
|
| 10 |
+
"ops",
|
| 11 |
+
]
|
torch-ext/sdpa_flash/_custom_ops.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Optional
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
from ._ops import ops
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def flash_attention_varlen(
|
| 9 |
+
out: torch.Tensor,
|
| 10 |
+
query: torch.Tensor,
|
| 11 |
+
key: torch.Tensor,
|
| 12 |
+
value: torch.Tensor,
|
| 13 |
+
cu_seqlens_q: torch.Tensor,
|
| 14 |
+
cu_seqlens_k: torch.Tensor,
|
| 15 |
+
max_seqlen_q: int,
|
| 16 |
+
max_seqlen_k: int,
|
| 17 |
+
do_causal: bool = False,
|
| 18 |
+
scale: Optional[float] = None,
|
| 19 |
+
softcapping: float = 1.0,
|
| 20 |
+
) -> None:
|
| 21 |
+
"""
|
| 22 |
+
Flash Attention with variable-length sequences.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
out: Output tensor of shape [total_q_tokens, num_heads, head_dim]
|
| 26 |
+
query: Query tensor of shape [total_q_tokens, num_heads, head_dim]
|
| 27 |
+
key: Key tensor of shape [total_k_tokens, num_heads_kv, head_dim]
|
| 28 |
+
value: Value tensor of shape [total_k_tokens, num_heads_kv, head_dim]
|
| 29 |
+
cu_seqlens_q: Cumulative sequence lengths for queries, shape [batch_size + 1], dtype must be torch.int32
|
| 30 |
+
cu_seqlens_k: Cumulative sequence lengths for keys, shape [batch_size + 1], dtype must be torch.int32
|
| 31 |
+
max_seqlen_q: Maximum sequence length in the query batch
|
| 32 |
+
max_seqlen_k: Maximum sequence length in the key batch
|
| 33 |
+
do_causal: Whether to apply causal masking
|
| 34 |
+
scale: Attention scale factor (default: 1/sqrt(head_dim))
|
| 35 |
+
softcapping: Softcapping value (default: 1.0, must be 1.0 for this implementation)
|
| 36 |
+
|
| 37 |
+
Note:
|
| 38 |
+
- cu_seqlens_q and cu_seqlens_k must have dtype torch.int32 for Metal compatibility
|
| 39 |
+
- Supported head dimensions: 32, 64, 72, 80, 96, 128
|
| 40 |
+
- Masks are not supported
|
| 41 |
+
"""
|
| 42 |
+
if scale is None:
|
| 43 |
+
scale = query.shape[-1] ** -0.5
|
| 44 |
+
|
| 45 |
+
ops.flash_attention_varlen(
|
| 46 |
+
out,
|
| 47 |
+
query,
|
| 48 |
+
key,
|
| 49 |
+
value,
|
| 50 |
+
cu_seqlens_q,
|
| 51 |
+
cu_seqlens_k,
|
| 52 |
+
max_seqlen_q,
|
| 53 |
+
max_seqlen_k,
|
| 54 |
+
do_causal,
|
| 55 |
+
scale,
|
| 56 |
+
softcapping,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def flash_attn_varlen_func(
|
| 60 |
+
q: torch.Tensor,
|
| 61 |
+
k: torch.Tensor,
|
| 62 |
+
v: torch.Tensor,
|
| 63 |
+
cu_seqlens_q: torch.Tensor,
|
| 64 |
+
cu_seqlens_k: torch.Tensor,
|
| 65 |
+
max_seqlen_q: int,
|
| 66 |
+
max_seqlen_k: int,
|
| 67 |
+
dropout_p: float = 0.0,
|
| 68 |
+
softmax_scale: Optional[float] = None,
|
| 69 |
+
causal: bool = False,
|
| 70 |
+
window_size: tuple = (-1, -1),
|
| 71 |
+
alibi_slopes: Optional[torch.Tensor] = None,
|
| 72 |
+
deterministic: bool = False,
|
| 73 |
+
return_attn_probs: bool = False,
|
| 74 |
+
) -> torch.Tensor:
|
| 75 |
+
"""
|
| 76 |
+
Flash Attention function with API compatible with the original Flash Attention.
|
| 77 |
+
|
| 78 |
+
Note: This implementation does not support:
|
| 79 |
+
- dropout
|
| 80 |
+
- window attention
|
| 81 |
+
- alibi slopes
|
| 82 |
+
- returning attention probabilities
|
| 83 |
+
"""
|
| 84 |
+
if dropout_p > 0:
|
| 85 |
+
raise NotImplementedError("Dropout is not supported in this implementation")
|
| 86 |
+
if window_size != (-1, -1):
|
| 87 |
+
raise NotImplementedError("Window attention is not supported")
|
| 88 |
+
if alibi_slopes is not None:
|
| 89 |
+
raise NotImplementedError("ALiBi is not supported")
|
| 90 |
+
if return_attn_probs:
|
| 91 |
+
raise NotImplementedError("Returning attention probabilities is not supported")
|
| 92 |
+
|
| 93 |
+
# Create output tensor
|
| 94 |
+
out = torch.empty_like(q)
|
| 95 |
+
|
| 96 |
+
# Call the kernel
|
| 97 |
+
flash_attention_varlen(
|
| 98 |
+
out=out,
|
| 99 |
+
query=q,
|
| 100 |
+
key=k,
|
| 101 |
+
value=v,
|
| 102 |
+
cu_seqlens_q=cu_seqlens_q,
|
| 103 |
+
cu_seqlens_k=cu_seqlens_k,
|
| 104 |
+
max_seqlen_q=max_seqlen_q,
|
| 105 |
+
max_seqlen_k=max_seqlen_k,
|
| 106 |
+
do_causal=causal,
|
| 107 |
+
scale=softmax_scale,
|
| 108 |
+
softcapping=1.0,
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
return out
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
__all__ = [
|
| 115 |
+
"flash_attention_varlen",
|
| 116 |
+
"flash_attn_varlen_func",
|
| 117 |
+
]
|
torch-ext/torch_binding.cpp
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <torch/library.h>
|
| 2 |
+
|
| 3 |
+
#include "registration.h"
|
| 4 |
+
#include "torch_binding.h"
|
| 5 |
+
|
| 6 |
+
TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
| 7 |
+
ops.def("flash_attention_varlen(Tensor! out, Tensor query, Tensor key, Tensor value, Tensor cu_seqlens_q, Tensor cu_seqlens_k, int max_seqlen_q, int max_seqlen_k, bool do_causal, float scale, float softcapping) -> ()");
|
| 8 |
+
ops.impl("flash_attention_varlen", torch::kMPS, flash_attention_varlen);
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
REGISTER_EXTENSION(TORCH_EXTENSION_NAME)
|
torch-ext/torch_binding.h
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/torch.h>
|
| 4 |
+
|
| 5 |
+
void flash_attention_varlen(
|
| 6 |
+
torch::Tensor &out,
|
| 7 |
+
torch::Tensor &query,
|
| 8 |
+
torch::Tensor &key,
|
| 9 |
+
torch::Tensor &value,
|
| 10 |
+
torch::Tensor &cu_seqlens_q,
|
| 11 |
+
torch::Tensor &cu_seqlens_k,
|
| 12 |
+
int64_t max_seqlen_q,
|
| 13 |
+
int64_t max_seqlen_k,
|
| 14 |
+
bool do_causal,
|
| 15 |
+
double scale,
|
| 16 |
+
double softcapping);
|