Fused CPU attention kernels (~4x performance increase) #2973
+529
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This introduces fused CPU attention kernels for optimized CPU inference. This removes the necessity to materialize the attention matrices, thereby dramatically improving throughput.
On an M3 Max with Llama 3.2 3b at 4-bit quantization, I am measuring a 4x increase in decode T/s. This is faster than llama.cpp, even with llama.cpp CPU FlashAttention enabled.
These kernels are loosely based on the work in FlashAttention and CPU implementations in vLLM and ggml, but have been modified for higher performance.
Algorithm
run_flash_attn_cpu
Choose execution path
S_q == 1
, invoke a specialized “single-Q” routineCompute attention
FLASH_ATTN_POOL
) with macOS QoS hintsFLASH_ATTN_POOL.install(...)
to isolate flash-attention tasksD
and calls(batch, head, query_pos)
row to a Rayon workerAssemble result
(B, S_q, H, D)