Status

NATTEN — Neighborhood Attention kernels

CUDA kernels for NATTEN (Neighborhood Attention Extension, SHI-Labs/NATTEN), packaged for the kernels library. Vendored from NATTEN v0.21.7 (MIT license).

Neighborhood attention restricts each token's attention to a sliding local window over 1-D, 2-D, or 3-D token layouts, with optional stride, dilation, and per-dimension causal masking. This kernel ships NATTEN's fused neighborhood attention (FNA) and FMHA kernels along with the thin functional frontend (na1d, na2d, na3d, attention, merge_attentions) and its backend/config selection logic.

Usage

import torch
from kernels import get_kernel

natten = get_kernel("kernels-community/natten", version=1)

# 2-D neighborhood attention over a 32x32 token layout:
# [batch, *token_layout, heads, head_dim]
q = torch.randn(1, 32, 32, 8, 64, device="cuda", dtype=torch.bfloat16)
k = torch.randn_like(q)
v = torch.randn_like(q)

out = natten.functional.na2d(q, k, v, kernel_size=(7, 7))

# 3-D, with stride/dilation/causal masking per dim:
q3 = torch.randn(1, 8, 16, 16, 8, 64, device="cuda", dtype=torch.bfloat16)
k3, v3 = torch.randn_like(q3), torch.randn_like(q3)
out3 = natten.functional.na3d(
    q3, k3, v3, kernel_size=(3, 5, 5), dilation=(1, 2, 2), is_causal=(True, False, False)
)

All functions are differentiable and torch.compile-compatible (the underlying ops are registered via TORCH_LIBRARY with fake/meta implementations).

Backend / architecture support

Backend Compute capabilities Notes
Reference CUDA kernels 7.0 – 12.0 correctness fallback, all dims
CUTLASS 2.X FNA / FMHA (cutlass-fna) 7.0 – 12.0 (SM80 tensor-core paths) default on pre-Hopper
Hopper FNA / FMHA (hopper-fna) 9.0a H100/H200
Blackwell FNA / FMHA (blackwell-fna) 10.0a B200/GB200; requires CUDA ≥ 12.8, so cu126 builds fall back to cutlass-fna / reference on these GPUs
Flex Attention backend (flex-fna) any pure PyTorch, via torch.nn.attention.flex_attention

Known gaps vs. upstream wheels:

  • No SM103 (B300 / Blackwell Ultra) binaries — kernel-builder does not currently target 10.3a.
  • SM120 (RTX 50 series) gets the CUTLASS 2.X and reference paths only, same as upstream (NATTEN has no SM120-specific fused kernels).
  • No ROCm or CPU support (upstream dropped these in v0.20).

The backend is chosen automatically per GPU; pass backend="..." to na1d/na2d/na3d to override.

Developing

The ~144 kernel instantiation TUs under csrc/autogen/ are committed codegen output. To regenerate (e.g. when bumping the vendored NATTEN version), run:

scripts/regen.sh                 # re-runs autogen + rewrites build.toml src lists

Build and test with kernel-builder:

nix run .#build-and-copy -L
nix run .#ci-test -L

Credits

All kernels and the Python frontend are by Ali Hassani and the NATTEN contributors (SHI-Labs/NATTEN, MIT). This repository only repackages them for the Kernel Hub: pybind11 bindings are replaced with TORCH_LIBRARY registrations (Python limited API), and build-time codegen is committed statically.

If you use NATTEN, please cite:

@inproceedings{hassani2023neighborhood,
  title   = {Neighborhood Attention Transformer},
  author  = {Ali Hassani and Steven Walton and Jiachen Li and Shen Li and Humphrey Shi},
  year    = 2023,
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
}
@misc{hassani2024faster,
  title   = {Faster Neighborhood Attention: Reducing the O(n^2) Cost of Self Attention at the Threadblock Level},
  author  = {Ali Hassani and Wen-Mei Hwu and Humphrey Shi},
  year    = 2024,
  eprint  = {2403.04690},
  archivePrefix = {arXiv},
}
Downloads last month
-
kernel
mit
arxiv: 2403.04690
Supported hardwares new
CUDA
8.09.010.012.09.0a10.0a
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64aarch64
Kernel Builder
ce92bd7