Skip to content

misc: Customize kv lens buffer size for sparse attention #1383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions flashinfer/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def __init__(
self,
float_workspace_buffer: torch.Tensor,
backend: str = "auto",
kv_lens_buffer_size: int = 32768,
) -> None:
r"""Constructs of :class:`BlockSparseAttentionWrapper`.

Expand All @@ -124,6 +125,8 @@ def __init__(
The implementation backend, could be ``auto``/``fa2`` or ``fa3``. Defaults to ``auto``.
If set to ``auto``, the function will automatically choose the backend based on the
device architecture and kernel availability.
kv_lens_buffer_size : int
The size of the kv lens buffer (num_kv_heads * MB), defaults to 32768.
"""
Comment on lines +128 to 130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The description for kv_lens_buffer_size states the size is (num_kv_heads * MB), but for BlockSparseAttentionWrapper, the required size for _kv_lens_buffer is MB (number of block rows) and for _vector_sparse_indptr_buffer it's MB + 1. The (num_kv_heads * MB) seems to be from VariableBlockSparseAttentionWrapper and is misleading here.

Also, the parameter is used for both _kv_lens_buffer and _vector_sparse_indptr_buffer, so the docstring could be more precise by mentioning both.

Suggested change
kv_lens_buffer_size : int
The size of the kv lens buffer (num_kv_heads * MB), defaults to 32768.
"""
kv_lens_buffer_size : int
The buffer size for KV lens and sparse indptr (>= MB). Defaults to 32768.

self._float_workspace_buffer = float_workspace_buffer
self.device = float_workspace_buffer.device
Expand All @@ -138,11 +141,11 @@ def __init__(
)
# NOTE(Zihao): assume maximum batch size is 32768
self._vector_sparse_indptr_buffer = torch.empty(
(32768,), dtype=torch.int32, device=self.device
(kv_lens_buffer_size,), dtype=torch.int32, device=self.device
)

self._kv_lens_buffer = torch.empty(
(32768,), dtype=torch.int32, device=self.device
(kv_lens_buffer_size,), dtype=torch.int32, device=self.device
)
self._pin_memory_int_workspace_buffer = torch.empty(
self._int_workspace_buffer.shape,
Expand Down