{"id":18819493,"url":"https://github.com/shi-labs/natten","last_synced_at":"2026-02-08T19:04:17.430Z","repository":{"id":62591591,"uuid":"547878629","full_name":"SHI-Labs/NATTEN","owner":"SHI-Labs","description":"Neighborhood Attention Extension. Bringing attention to a neighborhood near you!","archived":false,"fork":false,"pushed_at":"2025-03-18T01:40:42.000Z","size":13440,"stargazers_count":493,"open_issues_count":26,"forks_count":41,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-05-13T21:06:15.559Z","etag":null,"topics":["cuda","neighborhood-attention","pytorch"],"latest_commit_sha":null,"homepage":"https://shi-labs.com/natten/","language":"Cuda","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SHI-Labs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-10-08T13:29:47.000Z","updated_at":"2025-05-11T01:35:58.000Z","dependencies_parsed_at":"2023-09-22T03:00:54.932Z","dependency_job_id":"476a68d6-b959-4d69-a3d3-6be7d79c740b","html_url":"https://github.com/SHI-Labs/NATTEN","commit_stats":{"total_commits":76,"total_committers":12,"mean_commits":6.333333333333333,"dds":0.368421052631579,"last_synced_commit":"93f063d17a51e59716281f4fb8bd2f9139bdd4aa"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SHI-Labs%2FNATTEN","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SHI-Labs%2FNATTEN/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SHI-Labs%2FNATTEN/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SHI-Labs%2FNATTEN/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SHI-Labs","download_url":"https://codeload.github.com/SHI-Labs/NATTEN/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254227612,"owners_count":22035669,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cuda","neighborhood-attention","pytorch"],"created_at":"2024-11-08T00:23:19.751Z","updated_at":"2026-02-08T19:04:17.374Z","avatar_url":"https://github.com/SHI-Labs.png","language":"Cuda","funding_links":[],"categories":[],"sub_categories":[],"readme":"![NATTENLogo](assets/natten_dark.png#gh-dark-mode-only) ![NATTENLogo](assets/natten_light.png#gh-light-mode-only)\n\n\u003ca href=\"https://www.shi-labs.com/natten/\"\u003e\u003cimg src=\"https://img.shields.io/badge/pip%20install%20natten-read%20more-%23C209C1\" /\u003e\u003c/a\u003e\n| \u003ca href=\"docs/\"\u003e\u003cimg src=\"https://img.shields.io/badge/Documentation-B31942\" /\u003e\u003c/a\u003e\n| \u003ca href=\"https://arxiv.org/abs/2403.04690\"\u003e\u003cimg src=\"https://img.shields.io/badge/arXiv-2403.04690-orange\" /\u003e\u003c/a\u003e\n\n*Neighborhood Attention Extension*\n\nBringing attention to a neighborhood near you!\n\n\u003cdiv align=\"center\"\u003e\n\u003cpicture\u003e\n  \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"docs/assets/neighborhood_attn_2d_vis_dark.png\"\u003e\n  \u003cimg alt=\"Visualization of neighborhood attention in 2D.\" src=\"docs/assets/neighborhood_attn_2d_vis_light.png\" width=\"384\" /\u003e\n\u003c/picture\u003e\n\u003cpicture\u003e\n  \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"docs/assets/dilated_neighborhood_attn_2d_vis_dark.png\"\u003e\n  \u003cimg alt=\"Visualization of dilated neighborhood attention in 2D.\" src=\"docs/assets/dilated_neighborhood_attn_2d_vis_light.png\" width=\"384\" /\u003e\n\u003c/picture\u003e\n\u003c/div\u003e\n\nNATTEN is an open-source project dedicated to providing fast implementations for\n[Neighborhood Attention](https://openaccess.thecvf.com/content/CVPR2023/html/Hassani_Neighborhood_Attention_Transformer_CVPR_2023_paper.html),\na sliding window self-attention mechanism.\n\nIf you're not familiar with neighborhood attention, please refer to \n[our papers](https://github.com/SHI-Labs/Neighborhood-Attention-Transformer), or watch our \n[YouTube video](https://www.youtube.com/watch?v=Ya4BfioxIHA) from CVPR 2023.\n\nTo read more about our GEMM-based and fused neighborhood attention kernels, please refer to\nour new preprint, [Faster Neighborhood Attention](https://arxiv.org/abs/2403.04690).\n\n## New: Fused Neighborhood Attention now supports backpropagation!\n\nWe've released the Fused Neighborhood Attention (FNA) backward kernel and interface, which means you can now\ntrain models based on neighborhood attention faster and more efficiently.\n\nFNA can be seen as a generalization of methods such as [Flash Attention](https://github.com/Dao-AILab/flash-attention/) and\n[FMHA](https://github.com/facebookresearch/xformers/) from back-to-back matrix multiplication to\nback-to-back tensor-tensor contraction, and comes with neighborhood attention masking built in.\nThis accelerates neighborhood attention, a multi-dimensional sliding window attention pattern,\nby never storing the attention tensor to global memory, which aside from reducing global memory footprint also reduces\nthe memory bandwidth bottleneck.\n\n\u003cdiv align=\"center\"\u003e\n\u003cpicture\u003e\n  \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"assets/fna-chart-dark.png\"\u003e\n  \u003cimg alt=\"Op-level average speedup.\" src=\"assets/fna-chart-light.png\" /\u003e\n\u003c/picture\u003e\n\u003c/div\u003e\n\nWe highly recommend referring to [FNA quick start](docs/fna/fna-quickstart.md) or \nthe [Fused vs unfused NA](docs/fna/fused-vs-unfused.md) guide before\nstarting to use FNA, since the interface, memory layout, and feature set can differ from\nall unfused ops in NATTEN.\n\n## Getting started\n \nNATTEN supports PyTorch version 2.0 and later, and Python versions 3.8 and above. \nPython 3.12 is only supported with torch \u003e= 2.2.0.\n\nOlder NATTEN releases supported python \u003e= 3.7 and torch \u003e= 1.8.\n\nPlease refer to [install instructions](docs/install.md) to find out whether your operating system and hardware accelerator is\ncompatible with NATTEN.\n\n## Feature availability\n\n| Problem space | CPU backend | CUDA backend     |\n| -----------   | ----------- | ---------------- |\n| 1D            | naive       | naive, gemm, fna |\n| 2D            | naive       | naive, gemm, fna |\n| 3D            | naive       | naive, fna       |\n\n### CPU\n\n| Problem space | CPU Backend | Causal masking     | Varying parameters | Relative positional bias | Autograd support         |\n| -----------   | ----------- | ------------------ | ------------------ | ------------------------ | ------------------------ |\n| 1D            | naive       | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Forward and reverse mode |\n| 2D            | naive       | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Forward and reverse mode |\n| 3D            | naive       | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Forward and reverse mode |\n\nNotes:\n* Forward mode autograd does not support relative positional biases and causal masking yet.\n* Relative positional biases are not yet supported when any axis has causal masking enabled.\n\n### CUDA\n\n| Problem space | CUDA Backend | Causal masking     | Varying parameters | Relative positional bias | Autograd support         | Min. Arch |\n| -----------   | -----------  | ------------------ | ------------------ | ------------------------ | ------------------------ | --------- |\n| 1D            | naive        | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Forward and reverse mode | SM35      |\n| 2D            | naive        | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Forward and reverse mode | SM35      |\n| 3D            | naive        | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Forward and reverse mode | SM35      |\n| 1D            | gemm         | -                  | -                  | :white_check_mark:       | Forward and reverse mode | SM70      |\n| 2D            | gemm         | -                  | -                  | :white_check_mark:       | Forward and reverse mode | SM70      |\n| 1D            | fna          | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Reverse mode             | SM50      |\n| 2D            | fna          | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Reverse mode             | SM50      |\n| 3D            | fna          | :white_check_mark: | :white_check_mark: | :white_check_mark:       | Reverse mode             | SM50      |\n\nNotes: \n* FP16 kernels are only available on SM50 and above*, and BF16 requires SM80 and above.\n  * Naive FP16 kernels are only available on **SM60** and above.\n  * FNA FP16 kernels are only available on SM50 and above.\n* GEMM backend on SM70 and SM75 can only do FP16.\n* Tiled only implements 1/3 of the ops, is only implemented for 2D problems, and requires head dim = 32.\n* Forward mode autograd does not support relative positional biases and causal masking yet.\n* Relative positional biases are not yet supported when any axis has causal masking enabled.\n* Relative positional biases are not supported in FNA during backward pass.\n\nFeatures that will likely no longer be worked on or improved:\n* Relative positional biases\n  * There's just better alternatives that don't involve explicitly biasing the attention weight matrix, and they will be more\n  performant on top of providing similar or better accuracy levels.\n* GEMM-based kernels\n  * Since FNA covers more features than our unfused GEMM-based kernels, and we know it to be a better solution\n    (please refer to Faster Neighborhood Attention for details), we do not plan to extend or improve these kernels.\n  * This includes support for varying parameters, causal masking, and 3-D problems.\n\n## License\nNATTEN is released under the [MIT License](LICENSE).\n\n## Citation\n```bibtex\n@inproceedings{hassani2024faster,\n  title        = {Faster Neighborhood Attention: Reducing the O(n^2) Cost of Self Attention at the Threadblock Level},\n  author       = {Ali Hassani and Wen-Mei Hwu and Humphrey Shi},\n  year         = 2024,\n  booktitle    = {Advances in Neural Information Processing Systems},\n}\n@inproceedings{hassani2023neighborhood,\n  title        = {Neighborhood Attention Transformer},\n  author       = {Ali Hassani and Steven Walton and Jiachen Li and Shen Li and Humphrey Shi},\n  year         = 2023,\n  booktitle    = {IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)}\n}\n@misc{hassani2022dilated,\n  title        = {Dilated Neighborhood Attention Transformer},\n  author       = {Ali Hassani and Humphrey Shi},\n  year         = 2022,\n  url          = {https://arxiv.org/abs/2209.15001},\n  eprint       = {2209.15001},\n  archiveprefix = {arXiv},\n  primaryclass = {cs.CV}\n}\n```\n\n## Acknowledgements\nWe thank NVIDIA, and the [CUTLASS project](https://github.com/NVIDIA/cutlass/) and team for their efforts in\ncreating and open-sourcing CUTLASS. We would also like to thank Haicheng Wu for his valuable feedback and comments which led to\nthe creation of GEMM-based NA.\nWe also thank Meta and the [xFormers](https://github.com/facebookresearch/xformers/) team\nfor their FMHA kernel, which is what our Fused Neighborhood Attention kernel is based on.\nWe thank the [PyTorch](https://github.com/pytorch/pytorch/) project and team.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshi-labs%2Fnatten","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshi-labs%2Fnatten","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshi-labs%2Fnatten/lists"}