|
| 1 | +import os |
| 2 | +from typing import Dict, List |
| 3 | + |
| 4 | +import mlx.core as mx |
| 5 | + |
| 6 | +# Cache for compiled kernels |
| 7 | +_KERNELS: Dict[str, object] = {} |
| 8 | + |
| 9 | + |
| 10 | +def _get_metal_source(filename): |
| 11 | + path = os.path.join(os.path.dirname(__file__), filename) |
| 12 | + with open(path, "r") as f: |
| 13 | + return f.read() |
| 14 | + |
| 15 | + |
| 16 | +def _type_to_string(dtype: mx.Dtype) -> str: |
| 17 | + if dtype == mx.float32: |
| 18 | + return "float" |
| 19 | + elif dtype == mx.float16: |
| 20 | + return "half" |
| 21 | + elif dtype == mx.bfloat16: |
| 22 | + # Metal 3.1+ supports bfloat, typically via bfloat16_t or using half |
| 23 | + # For now we map to bfloat16_t assuming compiler support |
| 24 | + return "bfloat16_t" |
| 25 | + else: |
| 26 | + raise ValueError(f"Unsupported dtype for paged attention: {dtype}") |
| 27 | + |
| 28 | + |
| 29 | +def _get_kernel( |
| 30 | + name: str, |
| 31 | + filename: str, |
| 32 | + input_names: List[str], |
| 33 | + output_names: List[str], |
| 34 | + dtype: mx.Dtype = mx.float32, |
| 35 | +): |
| 36 | + type_str = _type_to_string(dtype) |
| 37 | + kernel_key = f"{name}_{type_str}" |
| 38 | + |
| 39 | + if kernel_key not in _KERNELS: |
| 40 | + source = _get_metal_source(filename) |
| 41 | + # Simple template substitution |
| 42 | + source = source.replace("{{T}}", type_str) |
| 43 | + |
| 44 | + header = """ |
| 45 | +#include <metal_stdlib> |
| 46 | +using namespace metal; |
| 47 | +""" |
| 48 | + _KERNELS[kernel_key] = mx.fast.metal_kernel( |
| 49 | + name=name, # Internal name for MLX JIT cache (not used for dispatch if we hold the object) |
| 50 | + input_names=input_names, |
| 51 | + output_names=output_names, |
| 52 | + source=source, |
| 53 | + header=header, |
| 54 | + ) |
| 55 | + return _KERNELS[kernel_key] |
| 56 | + |
| 57 | + |
| 58 | +def reshape_and_cache( |
| 59 | + key: mx.array, # (batch, num_kv_heads, 1, head_dim) |
| 60 | + value: mx.array, # (batch, num_kv_heads, 1, head_dim) |
| 61 | + key_cache: mx.array, # (num_layers, num_blocks, num_kv_heads, block_size, head_dim) |
| 62 | + value_cache: mx.array, |
| 63 | + block_tables: mx.array, # (batch, max_blocks) |
| 64 | + context_lengths: mx.array, # (batch,) |
| 65 | + block_size: int, |
| 66 | + layer_idx: int, |
| 67 | +): |
| 68 | + """ |
| 69 | + Writes new keys and values into the Paged KV Cache using a custom Metal kernel. |
| 70 | + NOTE: This performs an in-place update on key_cache/value_cache buffers. |
| 71 | + """ |
| 72 | + batch_size = key.shape[0] |
| 73 | + num_kv_heads = key.shape[1] |
| 74 | + head_dim = key.shape[3] |
| 75 | + num_layers = key_cache.shape[0] |
| 76 | + num_blocks = key_cache.shape[1] |
| 77 | + |
| 78 | + dtype = key.dtype |
| 79 | + if key_cache.dtype != dtype: |
| 80 | + raise ValueError(f"Key cache dtype {key_cache.dtype} does not match key dtype {dtype}") |
| 81 | + |
| 82 | + # 1. Prepare inputs |
| 83 | + indices = context_lengths - 1 |
| 84 | + block_indices_in_table = indices // block_size |
| 85 | + offsets = indices % block_size |
| 86 | + |
| 87 | + batch_indices = mx.arange(batch_size) |
| 88 | + physical_block_numbers = block_tables[batch_indices, block_indices_in_table] |
| 89 | + |
| 90 | + slot_mapping = physical_block_numbers.astype(mx.int64) * block_size + offsets.astype(mx.int64) |
| 91 | + |
| 92 | + # 2. Prepare Constants |
| 93 | + key_stride = num_kv_heads * head_dim |
| 94 | + value_stride = num_kv_heads * head_dim |
| 95 | + |
| 96 | + def mk_int(val): |
| 97 | + return mx.array(val, dtype=mx.int32) |
| 98 | + |
| 99 | + c_key_stride = mk_int(key_stride) |
| 100 | + c_val_stride = mk_int(value_stride) |
| 101 | + c_num_kv = mk_int(num_kv_heads) |
| 102 | + c_head_dim = mk_int(head_dim) |
| 103 | + c_block_size = mk_int(block_size) |
| 104 | + c_layer_idx = mk_int(layer_idx) |
| 105 | + c_num_layers = mk_int(num_layers) |
| 106 | + c_num_blocks = mk_int(num_blocks) |
| 107 | + |
| 108 | + # Inputs list |
| 109 | + inputs = [ |
| 110 | + key, |
| 111 | + value, |
| 112 | + key_cache, |
| 113 | + value_cache, |
| 114 | + slot_mapping, |
| 115 | + c_key_stride, |
| 116 | + c_val_stride, |
| 117 | + c_num_kv, |
| 118 | + c_head_dim, |
| 119 | + c_block_size, |
| 120 | + c_layer_idx, |
| 121 | + c_num_layers, |
| 122 | + c_num_blocks, |
| 123 | + ] |
| 124 | + |
| 125 | + # Input names (just for declaration) |
| 126 | + input_names = [ |
| 127 | + "key", |
| 128 | + "value", |
| 129 | + "key_cache", |
| 130 | + "value_cache", |
| 131 | + "slot_mapping", |
| 132 | + "key_stride", |
| 133 | + "value_stride", |
| 134 | + "num_kv_heads", |
| 135 | + "head_dim", |
| 136 | + "block_size", |
| 137 | + "layer_idx", |
| 138 | + "num_layers", |
| 139 | + "num_blocks", |
| 140 | + ] |
| 141 | + |
| 142 | + # 3. Get and Launch Kernel |
| 143 | + kernel = _get_kernel( |
| 144 | + name="reshape_and_cache_kernel", |
| 145 | + filename="reshape_and_cache.metal", |
| 146 | + input_names=input_names, |
| 147 | + output_names=["dummy_out"], |
| 148 | + dtype=dtype, |
| 149 | + ) |
| 150 | + |
| 151 | + grid = (num_kv_heads * head_dim, batch_size, 1) |
| 152 | + thread_group = (min(1024, num_kv_heads * head_dim), 1, 1) |
| 153 | + |
| 154 | + # Execute |
| 155 | + outputs = kernel( |
| 156 | + inputs=inputs, |
| 157 | + grid=grid, |
| 158 | + threadgroup=thread_group, |
| 159 | + output_shapes=[(1,)], |
| 160 | + output_dtypes=[mx.float32], # Dummy output dtype usually doesn't matter |
| 161 | + verbose=False, |
| 162 | + ) |
| 163 | + |
| 164 | + mx.eval(outputs) |
| 165 | + |
| 166 | + return key_cache, value_cache |
| 167 | + |
| 168 | + |
| 169 | +def paged_attention( |
| 170 | + queries: mx.array, |
| 171 | + key_cache: mx.array, |
| 172 | + value_cache: mx.array, |
| 173 | + block_tables: mx.array, |
| 174 | + context_lengths: mx.array, |
| 175 | + block_size: int, |
| 176 | + scale: float, |
| 177 | + num_kv_heads: int, |
| 178 | + layer_idx: int, |
| 179 | +) -> mx.array: |
| 180 | + """ |
| 181 | + Paged Attention using Metal Kernel. |
| 182 | + """ |
| 183 | + batch_size = queries.shape[0] |
| 184 | + num_heads = queries.shape[1] |
| 185 | + dtype = queries.dtype |
| 186 | + |
| 187 | + if queries.ndim == 4: |
| 188 | + if queries.shape[2] != 1: |
| 189 | + pass |
| 190 | + queries = queries.squeeze(2) |
| 191 | + |
| 192 | + head_dim = queries.shape[2] |
| 193 | + num_layers = key_cache.shape[0] |
| 194 | + num_total_blocks = key_cache.shape[1] |
| 195 | + max_blocks = block_tables.shape[1] |
| 196 | + |
| 197 | + # Prepare Constants |
| 198 | + def mk_int(val): |
| 199 | + return mx.array(val, dtype=mx.int32) |
| 200 | + |
| 201 | + c_num_heads = mk_int(num_heads) |
| 202 | + c_num_kv_heads = mk_int(num_kv_heads) |
| 203 | + c_head_dim = mk_int(head_dim) |
| 204 | + c_block_size = mk_int(block_size) |
| 205 | + c_max_blocks = mk_int(max_blocks) |
| 206 | + c_layer_idx = mk_int(layer_idx) |
| 207 | + c_num_layers = mk_int(num_layers) |
| 208 | + c_num_total_blocks = mk_int(num_total_blocks) |
| 209 | + c_scale = mx.array(scale, dtype=mx.float32) |
| 210 | + |
| 211 | + inputs = [ |
| 212 | + queries, |
| 213 | + key_cache, |
| 214 | + value_cache, |
| 215 | + block_tables, |
| 216 | + context_lengths, |
| 217 | + c_num_heads, |
| 218 | + c_num_kv_heads, |
| 219 | + c_head_dim, |
| 220 | + c_block_size, |
| 221 | + c_max_blocks, |
| 222 | + c_layer_idx, |
| 223 | + c_num_layers, |
| 224 | + c_num_total_blocks, |
| 225 | + c_scale, |
| 226 | + ] |
| 227 | + |
| 228 | + input_names = [ |
| 229 | + "queries", |
| 230 | + "key_cache", |
| 231 | + "value_cache", |
| 232 | + "block_tables", |
| 233 | + "context_lengths", |
| 234 | + "num_heads", |
| 235 | + "num_kv_heads", |
| 236 | + "head_dim", |
| 237 | + "block_size", |
| 238 | + "max_blocks", |
| 239 | + "layer_idx", |
| 240 | + "num_layers", |
| 241 | + "num_total_blocks", |
| 242 | + "scale", |
| 243 | + ] |
| 244 | + |
| 245 | + kernel = _get_kernel( |
| 246 | + name="paged_attention_kernel", |
| 247 | + filename="paged_attention_kernel.metal", |
| 248 | + input_names=input_names, |
| 249 | + output_names=["output"], |
| 250 | + dtype=dtype, # This will generate paged_attention_kernel_half etc. |
| 251 | + ) |
| 252 | + |
| 253 | + grid = (num_heads * 32, batch_size, 1) |
| 254 | + thread_group = (32, 1, 1) |
| 255 | + |
| 256 | + outputs = kernel( |
| 257 | + inputs=inputs, |
| 258 | + grid=grid, |
| 259 | + threadgroup=thread_group, |
| 260 | + output_shapes=[(batch_size, num_heads, head_dim)], |
| 261 | + output_dtypes=[dtype], # Output matches input dtype |
| 262 | + verbose=False, |
| 263 | + ) |
| 264 | + |
| 265 | + out = outputs[0] |
| 266 | + return out[:, :, None, :] |
0 commit comments