native-sparse-attention-pytorch


Namenative-sparse-attention-pytorch JSON
Version 0.1.24 PyPI version JSON
download
home_pageNone
SummaryNative Sparse Attention
upload_time2025-03-20 14:38:57
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License Copyright (c) 2025 Phil Wang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords artificial intelligence deep learning efficient attention
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <img src="./fig2.png" width="450px"></img>

## Native Sparse Attention

Implementation of the sparse attention pattern proposed by the Deepseek team in their [Native Sparse Attention](https://arxiv.org/abs/2502.11089) paper

This will be my last open sourced project under Meta

## Appreciation

- Phil Tillet for democratizing CUDA kernel hacking with <a href="https://triton-lang.org/main/index.html">Triton</a>

- [Flex Attention](https://pytorch.org/blog/flexattention/) for allowing for rapid prototyping

- <a href="https://github.com/Mr-Grin">@Mr-Grin</a> for the code review and pointing out an inaccuracy with the implementation

## Install

```bash
$ pip install native-sparse-attention-pytorch
```

## Usage

```python
import torch
from native_sparse_attention_pytorch import SparseAttention

attn = SparseAttention(
    dim = 512,
    dim_head = 64,
    heads = 8,
    sliding_window_size = 2,
    compress_block_size = 4,
    selection_block_size = 4,
    num_selected_blocks = 2
)

tokens = torch.randn(2, 31, 512)

attended = attn(tokens)

assert tokens.shape == attended.shape
```

## Example

Enwik8 language modeling

```bash
$ pip install .[examples]
```

Then

```bash
$ python train.py
```

To record some of your experiments, just invoke `wandb login` first before modifying the training script

## Citations

```bibtex
@inproceedings{Yuan2025NativeSA,
    title   = {Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention},
    author  = {Jingyang Yuan and Huazuo Gao and Damai Dai and Junyu Luo and Liang Zhao and Zhengyan Zhang and Zhenda Xie and Y. X. Wei and Lean Wang and Zhiping Xiao and Yuqing Wang and Chong Ruan and Ming Zhang and Wenfeng Liang and Wangding Zeng},
    year    = {2025},
    url     = {https://api.semanticscholar.org/CorpusID:276408911}
}
```

```bibtex
@inproceedings{Keles2022OnTC,
    title   = {On The Computational Complexity of Self-Attention},
    author  = {Feyza Duman Keles and Pruthuvi Maheshakya Wijewardena and Chinmay Hegde},
    booktitle = {International Conference on Algorithmic Learning Theory},
    year    = {2022},
    url     = {https://api.semanticscholar.org/CorpusID:252198880}
}
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "native-sparse-attention-pytorch",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "artificial intelligence, deep learning, efficient attention",
    "author": null,
    "author_email": "Phil Wang <lucidrains@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/a1/dc/dcc9233899387d3885db59280c054ee2450d2f27e666b98efebd150f9cc6/native_sparse_attention_pytorch-0.1.24.tar.gz",
    "platform": null,
    "description": "<img src=\"./fig2.png\" width=\"450px\"></img>\n\n## Native Sparse Attention\n\nImplementation of the sparse attention pattern proposed by the Deepseek team in their [Native Sparse Attention](https://arxiv.org/abs/2502.11089) paper\n\nThis will be my last open sourced project under Meta\n\n## Appreciation\n\n- Phil Tillet for democratizing CUDA kernel hacking with <a href=\"https://triton-lang.org/main/index.html\">Triton</a>\n\n- [Flex Attention](https://pytorch.org/blog/flexattention/) for allowing for rapid prototyping\n\n- <a href=\"https://github.com/Mr-Grin\">@Mr-Grin</a> for the code review and pointing out an inaccuracy with the implementation\n\n## Install\n\n```bash\n$ pip install native-sparse-attention-pytorch\n```\n\n## Usage\n\n```python\nimport torch\nfrom native_sparse_attention_pytorch import SparseAttention\n\nattn = SparseAttention(\n    dim = 512,\n    dim_head = 64,\n    heads = 8,\n    sliding_window_size = 2,\n    compress_block_size = 4,\n    selection_block_size = 4,\n    num_selected_blocks = 2\n)\n\ntokens = torch.randn(2, 31, 512)\n\nattended = attn(tokens)\n\nassert tokens.shape == attended.shape\n```\n\n## Example\n\nEnwik8 language modeling\n\n```bash\n$ pip install .[examples]\n```\n\nThen\n\n```bash\n$ python train.py\n```\n\nTo record some of your experiments, just invoke `wandb login` first before modifying the training script\n\n## Citations\n\n```bibtex\n@inproceedings{Yuan2025NativeSA,\n    title   = {Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention},\n    author  = {Jingyang Yuan and Huazuo Gao and Damai Dai and Junyu Luo and Liang Zhao and Zhengyan Zhang and Zhenda Xie and Y. X. Wei and Lean Wang and Zhiping Xiao and Yuqing Wang and Chong Ruan and Ming Zhang and Wenfeng Liang and Wangding Zeng},\n    year    = {2025},\n    url     = {https://api.semanticscholar.org/CorpusID:276408911}\n}\n```\n\n```bibtex\n@inproceedings{Keles2022OnTC,\n    title   = {On The Computational Complexity of Self-Attention},\n    author  = {Feyza Duman Keles and Pruthuvi Maheshakya Wijewardena and Chinmay Hegde},\n    booktitle = {International Conference on Algorithmic Learning Theory},\n    year    = {2022},\n    url     = {https://api.semanticscholar.org/CorpusID:252198880}\n}\n```\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 Phil Wang\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "Native Sparse Attention",
    "version": "0.1.24",
    "project_urls": {
        "Homepage": "https://pypi.org/project/native-sparse-attention-pytorch/",
        "Repository": "https://github.com/lucidrains/native-sparse-attention-pytorch"
    },
    "split_keywords": [
        "artificial intelligence",
        " deep learning",
        " efficient attention"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9312dc7dd37bb8ac9d59ec966c31379b89c843831b79c83365281d4e0847fa38",
                "md5": "f31196a7ac0e8689d7eb2640499ea1d7",
                "sha256": "2fb8c6db845301c7a3ae23165edac1f457fbf90d53958534bb3c68cde3e563f9"
            },
            "downloads": -1,
            "filename": "native_sparse_attention_pytorch-0.1.24-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f31196a7ac0e8689d7eb2640499ea1d7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 27121,
            "upload_time": "2025-03-20T14:38:55",
            "upload_time_iso_8601": "2025-03-20T14:38:55.343009Z",
            "url": "https://files.pythonhosted.org/packages/93/12/dc7dd37bb8ac9d59ec966c31379b89c843831b79c83365281d4e0847fa38/native_sparse_attention_pytorch-0.1.24-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a1dcdcc9233899387d3885db59280c054ee2450d2f27e666b98efebd150f9cc6",
                "md5": "1811faeb20f8f47c5e74d60220a373b4",
                "sha256": "746c32dae04b06085c87af54bdfba33c5cfa5bb480cda3d28ce8e3823c239d79"
            },
            "downloads": -1,
            "filename": "native_sparse_attention_pytorch-0.1.24.tar.gz",
            "has_sig": false,
            "md5_digest": "1811faeb20f8f47c5e74d60220a373b4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 36834445,
            "upload_time": "2025-03-20T14:38:57",
            "upload_time_iso_8601": "2025-03-20T14:38:57.243241Z",
            "url": "https://files.pythonhosted.org/packages/a1/dc/dcc9233899387d3885db59280c054ee2450d2f27e666b98efebd150f9cc6/native_sparse_attention_pytorch-0.1.24.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-03-20 14:38:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lucidrains",
    "github_project": "native-sparse-attention-pytorch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "native-sparse-attention-pytorch"
}
        
Elapsed time: 0.54193s