Skip to main content

DeepSelect

DeepSelect is a high performance implementation of the TopK kernel used in DeepSeek Sparse Attention (DSA) (which is used in DeepSeek V3.2, DeepSeek V4, and DeepSeek V4.1 models) and the sampler. It achieves 2 ~ 20x speedup compared to vanilla torch.topk.

News

  • 2026.09.10: We've released DeepSelect v1.0.0

Supported Cases

TopK workloads vary widely, and the fastest algorithm & implementation highly depends on the input dtype, batch_size, vocab_size, and topk. This repository only focuses on the following cases:

Lightning Indexer Scenario

This scenario covers:

  • Input dtype: torch.bfloat16
  • batch_size: $1 \sim +\infty$ (both large and small batch sizes are optimized)
  • vocab_size: $1 \sim +\infty$ (both large and small vocabularies are optimized)
  • topk: small (must be $\le 4096$; larger values are not supported)

Recommendations:

  • Disable sorted_index unless the output has to be ordered by index or by value; enabling either one costs performance.
  • Set return_value=False when the values are not needed. This skips the value output and is faster.

Sampling Scenario

This scenario covers:

  • Input dtype: torch.float32
  • batch_size: $1 \sim +\infty$
  • vocab_size: around 128K
  • topk: small (must be $\le 4096$; larger values are not supported)

Performance

Measured with the benchmark in tests/test.py (python3 tests/test.py --perf-only), which reports the ratio against torch.topk on the same input. The metric is effective memory bandwidth: TopK does no floating-point math, so a FLOP rate would not be meaningful here.

Lightning Indexer Scenario

bfloat16, topk = 512, one subplot per batch size, on a shared 0 - 7 TB/s axis.

DeepSelect vs torch.topk, bfloat16 Lightning Indexer

Sampling Scenario

float32, vocab_size = 129280, topk = 512.

DeepSelect vs torch.topk, float32 Sampling

Installation

git clone https://github.com/deepseek-ai/DeepSelect.git
cd DeepSelect
git submodule update --init --recursive
pip install -v .

Usage

import torch
import deep_select

# input: (batch_size, vocab_size), torch.bfloat16 or torch.float32.
# Its row stride must be a multiple of `deep_select.get_stride_requirement()[0]` bytes, and its last dimension must be contiguous.
batch_size, vocab_size, topk = 4, 204800, 1024

x = torch.randn(batch_size, vocab_size, dtype=torch.bfloat16, device="cuda")

values, indices = deep_select.topk(
    x,
    topk,
    sorted_index=True,         # return each row's indices in ascending order
    indices_type=torch.int32,  # torch.int32 or torch.int64
    return_value=True,         # False skips the value output (~10% faster)
)
# values:  (batch_size, topk) of x.dtype
# indices: (batch_size, topk) of indices_type

The row stride of the input tensor (x) must be aligned to deep_select.get_stride_requirement()[0] bytes. For unaligned inputs, padding is necessary.

Both outputs are allocated by the call, and their strides are aligned to deep_select.get_stride_requirement()[1] bytes (so they may be non-contiguous). Pass output_idx= to write indices into a buffer you own, and that buffer must satisfy the same stride requirement.

For the full signature, see deep_select/interface.py.

Variable-length rows

end sets a per-row upper bound (exclusive). Rows shorter than topk are padded with value_oob_fill_value / idx_oob_fill_value:

batch_size, vocab_size = 2, 129280   # 129280 is a multiple of 256, so float32 is fine
x = torch.randn(batch_size, vocab_size, dtype=torch.float32, device="cuda")

end = torch.tensor([129280, 100000], dtype=torch.int32, device="cuda")  # (batch_size,)
values, indices = deep_select.topk(x, 1000, end=end, sorted=True,
                                   indices_type=torch.int64)

NaN handling

NaN checking is always on. With the default abort_when_nan_found=True the kernel invokes trap() and aborts. Rows whose length is <= topk are never NaN-checked.

Citation

@misc{deepselect2026,
    title={DeepSelect: High-Performance TopK Kernels for DeepSeek Sparse Attention and Sampling},
    author={Yi Qian and Shengyu Liu and Yichen Li},
    year={2026},
    publisher = {GitHub},
    howpublished = {\url{https://github.com/deepseek-ai/DeepSelect}},
}

Metadata

Release files for tokenspeed-deepselect 1.0.0.post20260910

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for tokenspeed-deepselect 1.0.0.post20260910
File
tokenspeed_deepselect-1.0.0.post20260910-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details
tokenspeed_deepselect-1.0.0.post20260910-cp313-cp313-manylinux_2_28_aarch64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ ARM64 Details
tokenspeed_deepselect-1.0.0.post20260910-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details
tokenspeed_deepselect-1.0.0.post20260910-cp312-cp312-manylinux_2_28_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ ARM64 Details
tokenspeed_deepselect-1.0.0.post20260910-cp311-cp311-manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64 Details
tokenspeed_deepselect-1.0.0.post20260910-cp311-cp311-manylinux_2_28_aarch64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ ARM64 Details
tokenspeed_deepselect-1.0.0.post20260910-cp310-cp310-manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64 Details
tokenspeed_deepselect-1.0.0.post20260910-cp310-cp310-manylinux_2_28_aarch64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ ARM64 Details

Total release size: 173.1 MB

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp313-cp313-manylinux_2_28_x86_64.whl
Size 21.7 MB
Tags CPython 3.13 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
69dbaf2e3e5616263b892a5c2113c10b05cbf45b20a13cf04a77e3a286dde0c8
BLAKE2b-256 checksum
How to use checksums
21956a7266da286e3562ba8ecc0e12ebc32bcfe873730eba7a8e6b729ff2b3cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp313-cp313-manylinux_2_28_aarch64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp313-cp313-manylinux_2_28_aarch64.whl
Size 21.6 MB
Tags CPython 3.13 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
b532a3a9d70941e257b2550769b7725ea6f39963f33b816ee5d2f05fd23cfa9a
BLAKE2b-256 checksum
How to use checksums
2e6ce0fa9d4ef3ceb6235fb54d691bf9ea342e310f1c3a747c4abfbb304082ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp312-cp312-manylinux_2_28_x86_64.whl
Size 21.7 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
aba6e2a19c0f3de34dea550f00f708c581cb925c0d9a95f3b979c547b8908546
BLAKE2b-256 checksum
How to use checksums
48796c6f534c8287c5c30297b8e2106dd8564633bfcfe692da3ff7c1c43f7dff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp312-cp312-manylinux_2_28_aarch64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp312-cp312-manylinux_2_28_aarch64.whl
Size 21.6 MB
Tags CPython 3.12 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
7e55b4562e2abd362da82802d0a51aa0ba7f0abbaf00acdeb03130ae88b4de59
BLAKE2b-256 checksum
How to use checksums
48c34a5ccac954a41469b089227c98d312d68b585bf1edbbc2e5990afbfac26e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp311-cp311-manylinux_2_28_x86_64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp311-cp311-manylinux_2_28_x86_64.whl
Size 21.7 MB
Tags CPython 3.11 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
0e37d43eb97a71b25ad1a698d2f2319ffefc499749a54456e79193070ed3424e
BLAKE2b-256 checksum
How to use checksums
c5305b4aaed2aa7f05bcce0c549a0b020c436a860bb41b09880efd151ad89d84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp311-cp311-manylinux_2_28_aarch64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp311-cp311-manylinux_2_28_aarch64.whl
Size 21.6 MB
Tags CPython 3.11 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
2eafea60faf6cca9ef9f5e878702e7d63320cdd9a8045ea827c893136f8ff726
BLAKE2b-256 checksum
How to use checksums
68cf67f030ad28c0b92a8e78c8fcb4dde450a14e6990b4229089e534520d1d9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp310-cp310-manylinux_2_28_x86_64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp310-cp310-manylinux_2_28_x86_64.whl
Size 21.7 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
4ede74a69ecf9dbd2cf200649d20a8baeba702fb2cc2ff5bd82524aaa68e8ac3
BLAKE2b-256 checksum
How to use checksums
e17614f7020bf44d28c8cb137757d1fe53d2309c4b14399991b6c6a713f24350
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokenspeed_deepselect-1.0.0.post20260910-cp310-cp310-manylinux_2_28_aarch64.whl

Download URL tokenspeed_deepselect-1.0.0.post20260910-cp310-cp310-manylinux_2_28_aarch64.whl
Size 21.6 MB
Tags CPython 3.10 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
92ce30a1f9aa0a09c8804eafef9e0764a1043a28ebbf328d59d11199de6ed8b2
BLAKE2b-256 checksum
How to use checksums
4693ad143dd1751ba2c17c5acda005ebe2fc88f30a1bf5f4fd1c009e890eb8b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.0.0.post20260910 This release

8 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page