How philipturner/metal-flash-attention works
Project type and purpose
• This is a single‐headed attention library (FlashAttention) implemented in Swift, targeting Apple silicon via Metal.
• It is delivered as a Swift Package. There is no “frontend/UI”—it’s a low‐level compute library for transformer attention kernels (forward and backward).
File structure
Top‐level:
• Package.swift / README.md / LICENSE
• Sources/FlashAttention – the core library, subdivided into Attention, GEMM, Utilities
• Tests/FlashAttentionTests – unit tests against Swift, MSL kernels, MTLContext
• Documentation/… – design notes, C++ reference port, archived Swift tests
Key patterns:
• Each “Descriptor” subdirectory holds parameter‐packing and pipeline‐cache logic
• Each “Kernel” subdirectory holds JIT‐compiled multi‐line Swift strings that become Metal Shaders
• Utilities provides MTLContext, address‐space helpers, and cross‐cutting types
Extract architecture hints from the README
• The library JIT‐compiles Metal shaders at runtime (no pre‐compiled .metallib).
• Two major algorithmic domains: Attention and GEMM, each with descriptors, kernels, and precision management.
Detail & labeling
– Annotate key data structures (AttentionDescriptorParameters, GEMMDescriptor, MTLBuffers)