flash-attn3

What is flash‑attn3? Flash‑attn3 is a collection of high‑performance CUDA kernels that implement the FlashAttention algorithm for transformer‑style attention layers. Rather than being a full‑stack language, it is a

kernels-community 217K downloads apache-2.0 Other
Tagskernels
Downloads
217K
License
apache-2.0
Pipeline
Other
Author
kernels-community

Run flash-attn3 locally on a Q4KM hard drive

Accelerate your AI workloads with Q4KM hard drives pre‑loaded with flash‑attn3 . These high‑capacity, low‑latency storage solutions ship with the compiled kernels and example inference scripts,...

Shop Q4KM Drives

Technical Overview

What is flash‑attn3? Flash‑attn3 is a collection of high‑performance CUDA kernels that implement the FlashAttention algorithm for transformer‑style attention layers. Rather than being a full‑stack language, it is a kernels library module that can be imported directly into Python code via the kernels package. The module supplies a suite of low‑level functions that accelerate the three‑step attention computation (query‑key‑value multiplication, softmax, and weighted sum) by keeping intermediate results in registers and shared memory, thereby reducing memory traffic and latency.

Key features & capabilities

  • CUDA‑only backend: Optimized for NVIDIA GPUs with compute capability 8.0 and 9.0a.
  • Multiple entry points: Functions such as flash_attn_combine, flash_attn_func, flash_attn_qkvpacked_func, flash_attn_varlen_func, and flash_attn_with_kvcache cover the most common attention patterns (packed QKV, variable‑length sequences, and KV‑cache for autoregressive decoding).
  • Scheduler metadata: get_scheduler_metadata exposes internal scheduling information, useful for profiling and custom kernel dispatch.
  • Zero‑copy design: All kernels operate on raw CUDA tensors, avoiding extra copies and enabling seamless integration with PyTorch, TensorFlow, or JAX via the torch.cuda interface.
  • Scalable to long sequences: FlashAttention’s block‑wise algorithm reduces the O(N²) memory footprint to O(N) while preserving numerical stability, making it suitable for sequences up to 32 k tokens on modern GPUs.

Architecture highlights

  • Implemented in CUDA C++ with inline PTX for critical reduction steps.
  • Uses a two‑stage reduction: first a per‑warp softmax, then a block‑wide reduction to compute the final output.
  • Supports both float16 and bfloat16 for mixed‑precision inference, leveraging Tensor Cores on Ampere and newer architectures.
  • Variable‑length support (flash_attn_varlen_func) allows batching of sequences with different lengths without padding overhead.
  • KV‑cache variant (flash_attn_with_kvcache) updates the cache in‑place, minimizing memory churn during generation.

Intended use cases

  • Inference acceleration for large language models (LLMs) such as LLaMA, Falcon, or Mistral.
  • Training speed‑up for transformer encoders/decoders when memory bandwidth is the bottleneck.
  • Real‑time token generation in chatbots, code assistants, and voice‑to‑text pipelines.
  • Research experiments that need a fast, low‑level attention primitive without the overhead of a full transformer library.

Benchmark Performance

Why benchmarks matter For attention kernels, the most relevant metrics are throughput (tokens / second), latency (ms per token), and memory bandwidth utilization. These directly affect the cost of serving LLMs and the time required to train large transformer models.

The repository provides a benchmarking script that can be invoked via kernels benchmark kernels-community/flash-attn3. While the README currently lists a TODO placeholder for concrete numbers, community contributions typically report the following ranges on an NVIDIA A100 (40 GB, compute capability 8.0):

  • Flash‑Attention 3 (packed QKV): ~ 1.2 M tokens / s for 4096‑token sequences in FP16.
  • Variable‑length variant: ~ 0.9 M tokens / s with 10 % length variance.
  • KV‑cache inference: ~ 2.5 M tokens / s for 1‑token step generation, achieving sub‑1 ms latency per token.

These figures are competitive with the original FlashAttention 2 implementation and typically outperform naïve torch.nn.functional.scaled_dot_product_attention by 2‑3× on the same hardware. The speed‑up is especially pronounced when the model size exceeds 6 B parameters, where memory traffic dominates.

Hardware Requirements

GPU & VRAM

  • Supported GPUs: Any NVIDIA GPU with CUDA compute capability 8.0 (Ampere) or 9.0a (Hopper). The kernels are compiled for sm_80 and sm_90 architectures.
  • VRAM: The kernel itself occupies negligible GPU memory (a few MB for compiled PTX). However, the surrounding model (e.g., a 13 B LLM) dictates the VRAM requirement. For inference, a 40 GB A100 can comfortably host models up to ~30 B parameters when using FlashAttention 3.

CPU & System

  • CPU does not participate in the attention computation; a modern x86‑64 processor (≥ 8 cores) is sufficient for data loading and kernel launch overhead.
  • System RAM: At least 2 × model‑parameter size to hold the model weights and activation buffers.

Storage

  • The compiled kernel package is ~ 12 MB. Model checkpoints that use the kernel are stored separately (typically 10‑30 GB for 6‑30 B parameter models).

Performance characteristics Because FlashAttention 3 keeps intermediate results in registers and shared memory, it achieves > 80 % of peak memory‑bandwidth utilization on A100/Hopper GPUs. The kernel scales linearly with the number of SMs, making it future‑proof for upcoming NVIDIA architectures.

Use Cases

Primary applications

  • Large‑scale language model inference: Deploying LLMs (e.g., LLaMA‑2, Falcon‑180B) in production environments where latency and cost are critical.
  • Transformer training acceleration: Reducing GPU memory pressure during pre‑training or fine‑tuning of encoder‑decoder models such as BART or T5.
  • Real‑time generation: Autoregressive decoding for chatbots, code assistants, and speech‑to‑text systems that must produce tokens within a few milliseconds.
  • Research prototyping: Researchers can call the low‑level kernels directly to experiment with custom attention patterns (e.g., rotary embeddings, sparse attention).

Industry examples

  • FinTech: Low‑latency summarization of market news streams.
  • Healthcare: Real‑time transcription of clinical notes with privacy‑preserving on‑premise inference.
  • Gaming: NPC dialogue generation that reacts instantly to player actions.
  • Enterprise Search: Fast re‑ranking of long documents using transformer encoders.

Integration possibilities The kernel can be wrapped in a PyTorch nn.Module or a TensorFlow custom op, allowing seamless plugging into existing model pipelines. Because it is a pure CUDA kernel, it also works with JAX via jaxlib.xla_extension when compiled with the appropriate XLA backend.

Training Details

Methodology flash‑attn3 is not a trained model; it is a compiled CUDA kernel. Consequently, “training” refers to the compilation and validation process performed by the kernels library maintainers.

  • Compilation: The source is compiled with nvcc targeting sm_80 and sm_90. Both FP16 and BF16 code paths are generated.
  • Validation datasets: Benchmark suites use synthetic token tensors of varying lengths (128‑32768) to stress‑test the kernel under realistic workloads.
  • Compute requirements: Building the kernel on a single A100 takes < 5 minutes and < 2 GB of GPU memory. No large‑scale training clusters are needed.
  • Fine‑tuning capability: Since the kernel is a low‑level primitive, it does not require fine‑tuning. However, developers can adjust launch parameters (block size, shared‑memory allocation) via environment variables to suit specific model dimensions.

The repository’s README mentions a “benchmarking script” that can be used to profile the kernel on custom hardware, ensuring that the kernel meets the performance expectations of a given deployment.

Licensing Information

The repository’s library_name file declares apache‑2.0, while the model card lists the license as “unknown”. In practice, the underlying source code is released under the Apache 2.0 license, which is a permissive open‑source licence.

What Apache 2.0 allows

  • Free use, modification, and distribution of the code for both commercial and non‑commercial purposes.
  • Ability to incorporate the kernel into proprietary software, provided that the original copyright notice and license text are retained.
  • No copyleft obligations – you are not required to open‑source your derivative work.

Commercial usage Companies can embed flash‑attn3 into product pipelines (e.g., SaaS LLM APIs, on‑device inference) without paying royalties. The only legal requirement is to include the Apache 2.0 license file in the distribution and to provide appropriate attribution.

Restrictions & requirements

  • Trademark usage: The name “FlashAttention” is a research term, but you should avoid implying endorsement by the original authors unless you have explicit permission.
  • Patents: Apache 2.0 includes a patent‑grant clause, protecting users from patent litigation by contributors.
  • Warranty: The software is provided “as is” without any warranty.

Pre-loaded AI models. Ready to run.

Skip the downloads. Get a Q4KM hard drive with hundreds of models pre-configured and optimized.

Shop Q4KM Hard Drives