SpargeAttention Documentation

repository·main·Indexed 21 days ago

https://github.com/thu-ml/spargeattn

A universal, training-free sparse attention mechanism designed to accelerate inference for language, image, and video models. It provides a plug-and-play API, including spas_sage2_attn_meansim_topk_cuda as a drop-in replacement for torch.nn.functional.scaled_dot_product_attention, and supports models such as Wan, LTX-Video, HunyuanVideo, and CogVideoX-2B.

Tokens
1.3K
Snippets
6
Records
7
Agent score
27%

What's inside SpargeAttention

  1. Install SpargeAttention

    main

    To install SpargeAttention, ensure your environment meets the base requirements and then use pip or setup.py to install the package. Parallel compilation via ninja is recommended.

    # Base environment requirements:
    # python>=3.9, torch>=2.3.0
    # CUDA: >=12.8 (Blackwell), >=12.4 (Ada fp8), >=12.3 (Hopper fp8), >=12.0 (Ampere)
    
    # Installation steps:
    pip install ninja
    python setup.py install
    # or
    pip install -e .
  2. Run inference with SpargeAttn plug-and-play scripts

    main

    You can run pre-configured inference scripts for various video models (Wan, LTX-Video, HunyuanVideo, CogVideoX-2B) that have the SpargeAttn API integrated. These scripts should be executed as modules from the repository root.

    Execution Command:

    python -m inference_examples.<script_name> [--flags]

    Output Directory Structure: Videos are saved to inference_examples/videos/<model>/<run_dir>/. The <run_dir> name depends on the selected mode:

    • --mode full: original/
    • --mode cdfthreshd: cdfthreshd-<value>/
    • --mode topk: topk-<value>/
    python -m inference_examples.wan_infer \
      --model wan2_2-14b \
      --mode topk --value 0.4 \
      --start 0 --end 1
  3. Use the Plug-and-Play API for Sparse Attention

    main

    The recommended way to use SpargeAttention is via the spas_sage2_attn_meansim_topk_cuda API. This is a drop-in replacement for torch.nn.functional.scaled_dot_product_attention.

    You can adjust the topk parameter to balance attention accuracy (higher topk) and sparsity (lower topk).

    from spas_sage_attn import spas_sage2_attn_meansim_topk_cuda
    
    # Replace torch.nn.functional.scaled_dot_product_attention with this:
    attn_output = spas_sage2_attn_meansim_topk_cuda(q, k, v, topk=0.5, is_causal=False)
  4. Customize your Block-Sparse Mask

    main

    If you need to compute attention using a specific block-sparse mask per attention head, use the block_sparse_sage2_attn_cuda API.

    Mask Requirements:

    • The mask_id parameter must have the shape (batch_size, num_heads, ⌈seq_len / 128⌉, ⌈seq_len // 64⌉).
    • The mask must consist of 0 and 1 values.
    • The current fixed block size is 128×64.
    from spas_sage_attn import block_sparse_sage2_attn_cuda
    
    # mask_id shape: (batch_size, num_heads, ceil(seq_len / 128), ceil(seq_len / 64))
    attn_output = block_sparse_sage2_attn_cuda(q, k, v, mask_id=None)
  5. Configure inference flags for SpargeAttn scripts

    main

    When running the inference scripts, use the following flags to control model selection and SpargeAttn behavior:

    FlagDescription
    --model {wan2_1-1_3b, wan2_1-14b, wan2_2-14b}Selects the Wan model (only available in wan_infer.py)
    --mode {full, cdfthreshd, topk}Sets the attention mode: full (baseline SDPA), cdfthreshd (uses spas_sage2_attn_meansim_cuda), or topk (uses spas_sage2_attn_meansim_topk_cuda)
    --value <float>Sets the threshold value for cdfthreshd and topk modes
    --start <int> --end <int>Defines a slice $[start, end)$ of prompts from evaluate/datasets/video/prompts.txt
    # Example for Wan
    python -m inference_examples.wan_infer \
      --model wan2_2-14b \
      --mode topk --value 0.4 \
      --start 0 --end 1
    
    # Example for LTX-Video
    python -m inference_examples.ltx_infer --mode topk --value 0.5
  6. Reference of available SpargeAttention APIs

    main

    SpargeAttention provides several APIs based on different versions of SageAttention. The sage2 versions are highly recommended for better performance.

    - spas_sage2_attn_meansim_topk_cuda: SpargeAttn based on SageAttention2 (RECOMMENDED)
    - spas_sage2_attn_meansim_cuda: SpargeAttn based on SageAttention2 (NOT RECOMMENDED)
    - spas_sage_attn_meansim_topk_cuda: SpargeAttn based on SageAttention (RECOMMENDED)
    - spas_sage_attn_meansim_cuda: SpargeAttn based on SageAttention (NOT RECOMMENDED)