{"id":29219702,"url":"https://github.com/dvlab-research/sparsetransformer","last_synced_at":"2025-07-03T02:06:42.514Z","repository":{"id":152991912,"uuid":"617336206","full_name":"dvlab-research/SparseTransformer","owner":"dvlab-research","description":"A fast and memory-efficient libarary for sparse transformer with varying token numbers (e.g., 3D point cloud).","archived":false,"fork":false,"pushed_at":"2023-09-06T07:16:14.000Z","size":243,"stargazers_count":163,"open_issues_count":5,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-20T13:33:20.264Z","etag":null,"topics":["3d-point-cloud","cuda","sparse-transformer","transformer"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dvlab-research.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2023-03-22T07:13:32.000Z","updated_at":"2025-03-14T05:52:39.000Z","dependencies_parsed_at":"2024-01-16T02:46:27.300Z","dependency_job_id":"b4e84df7-2516-40db-ae04-305dccf182a6","html_url":"https://github.com/dvlab-research/SparseTransformer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dvlab-research/SparseTransformer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlab-research%2FSparseTransformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlab-research%2FSparseTransformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlab-research%2FSparseTransformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlab-research%2FSparseTransformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dvlab-research","download_url":"https://codeload.github.com/dvlab-research/SparseTransformer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvlab-research%2FSparseTransformer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263245317,"owners_count":23436514,"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":["3d-point-cloud","cuda","sparse-transformer","transformer"],"created_at":"2025-07-03T02:06:41.128Z","updated_at":"2025-07-03T02:06:42.492Z","avatar_url":"https://github.com/dvlab-research.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SpTr: PyTorch Spatially Sparse Transformer Library\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"figs/sparse_transformer.png\"/\u003e\n\u003c/div\u003e\n\n**SparseTransformer (SpTr)** provides a **fast**, **memory-efficient**, and **easy-to-use** implementation for sparse transformer with **varying token numbers** (e.g., window transformer for 3D point cloud).\n\n**SpTr** has been used by the following works:\n\n* **Spherical Transformer for LiDAR-based 3D Recognition (CVPR 2023)**: [\\[Paper\\]](https://arxiv.org/pdf/2303.12766.pdf) [\\[Code\\]](https://github.com/dvlab-research/SphereFormer)\n\n* **Stratified Transformer for 3D Point Cloud Segmentation (CVPR 2022)**: [\\[Paper\\]](https://openaccess.thecvf.com/content/CVPR2022/papers/Lai_Stratified_Transformer_for_3D_Point_Cloud_Segmentation_CVPR_2022_paper.pdf) [\\[Code\\]](https://github.com/dvlab-research/Stratified-Transformer)\n\n## Installation\n### Install Dependency\n```\npip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html\npip install torch_scatter==2.0.9\npip install torch_geometric==1.7.2\n```\n\n### Compile sptr\n```\npython3 setup.py install\n```\n\n\n## Usage\nSpTr can be easily used in most current transformer-based 3D point cloud networks, with only several minor modifications. First, define the attention module `sptr.VarLengthMultiheadSA`. Then, wrap the input features and indices into `sptr.SparseTrTensor`, and forward it into the module. That's all. A simple example is as follows. For more complex usage, you can refer to the code of above works (e.g., SphereFormer, StratifiedFormer).\n### Example\n```\nimport sptr\n\n# Define module\ndim = 48\nnum_heads = 3\nindice_key = 'sptr_0'\nwindow_size = np.array([0.4, 0.4, 0.4])  # can also be integers for voxel-based methods\nshift_win = False  # whether to adopt shifted window\nself.attn = sptr.VarLengthMultiheadSA(\n    dim, \n    num_heads, \n    indice_key, \n    window_size, \n    shift_win\n)\n\n# Wrap the input features and indices into SparseTrTensor. Note: indices can be either intergers for voxel-based methods or floats (i.e., xyz) for point-based methods\n# feats: [N, C], indices: [N, 4] with batch indices in the 0-th column\ninput_tensor = sptr.SparseTrTensor(feats, indices, spatial_shape=None, batch_size=None)\noutput_tensor = self.attn(input_tensor)\n\n# Extract features from output tensor\noutput_feats = output_tensor.query_feats\n```\n\n## Authors\n\nXin Lai (a Ph.D student at CSE CUHK, xinlai@cse.cuhk.edu.hk) - Initial CUDA implementation, maintainance.\n\nFanbin Lu (a Ph.D student at CSE CUHK) - Improve CUDA implementation, maintainance.\n\nYukang Chen (a Ph.D student at CSE CUHK) - Maintainance. \n\n\n## Cite\n\nIf you find this project useful, please consider citing\n```\n@inproceedings{lai2023spherical,\n  title={Spherical Transformer for LiDAR-based 3D Recognition},\n  author={Lai, Xin and Chen, Yukang and Lu, Fanbin and Liu, Jianhui and Jia, Jiaya},\n  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},\n  year={2023}\n}\n```\n```\n@inproceedings{lai2022stratified,\n  title={Stratified transformer for 3d point cloud segmentation},\n  author={Lai, Xin and Liu, Jianhui and Jiang, Li and Wang, Liwei and Zhao, Hengshuang and Liu, Shu and Qi, Xiaojuan and Jia, Jiaya},\n  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},\n  pages={8500--8509},\n  year={2022}\n}\n```\n\n## License\n\nThis project is licensed under the Apache license 2.0 License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvlab-research%2Fsparsetransformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdvlab-research%2Fsparsetransformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvlab-research%2Fsparsetransformer/lists"}