{"id":22409718,"url":"https://github.com/nanowell/q-sparse-llm","last_synced_at":"2025-06-24T22:31:40.572Z","repository":{"id":249543599,"uuid":"831802251","full_name":"nanowell/Q-Sparse-LLM","owner":"nanowell","description":"My Implementation of Q-Sparse: All Large Language Models can be Fully Sparsely-Activated","archived":false,"fork":false,"pushed_at":"2024-08-14T11:00:48.000Z","size":32,"stargazers_count":31,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-07T09:17:56.844Z","etag":null,"topics":["artificial-intelligence","deep-neural-networks","deeplearning","large-language-models","machine-learning","q-sparse","sparse-neural-networks","transformer"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nanowell.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-21T16:59:02.000Z","updated_at":"2024-11-23T20:46:00.000Z","dependencies_parsed_at":"2024-08-13T16:41:21.074Z","dependency_job_id":null,"html_url":"https://github.com/nanowell/Q-Sparse-LLM","commit_stats":null,"previous_names":["nanowell/q-sparse-llm"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nanowell/Q-Sparse-LLM","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanowell%2FQ-Sparse-LLM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanowell%2FQ-Sparse-LLM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanowell%2FQ-Sparse-LLM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanowell%2FQ-Sparse-LLM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanowell","download_url":"https://codeload.github.com/nanowell/Q-Sparse-LLM/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanowell%2FQ-Sparse-LLM/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261766603,"owners_count":23206655,"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":["artificial-intelligence","deep-neural-networks","deeplearning","large-language-models","machine-learning","q-sparse","sparse-neural-networks","transformer"],"created_at":"2024-12-05T12:09:31.866Z","updated_at":"2025-06-24T22:31:40.530Z","avatar_url":"https://github.com/nanowell.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Q-Sparse-LLM\n\nQ-Sparse-LLM is an implementation of a sparse transformer architecture designed for efficient and high-performance language modeling. This project introduces sparsity and quantization techniques to the traditional transformer architecture, aiming to reduce computational costs and memory footprint while maintaining model performance.\n![x3](https://github.com/user-attachments/assets/79bb0f9e-fb25-4eb0-8899-4e497f4e34b3)\n\n## Features\n\n- **Top-K Sparsity**: Implements a sparse activation mechanism that retains only the top K% of values in each layer.\n- **Quantized Top-K Sparsity**: Extends the sparsity mechanism with 8-bit quantization for further efficiency.\n- **ReLU²GLU Activation**: Uses a squared ReLU Gated Linear Unit for improved sparsity in feed-forward layers.\n\n## TODO:\n- **Compatibility with 1-bit LLMs**: Designed to be compatible with extremely quantized models like BitNet b1.58.\n\n## Architecture Overview\n\nThe Q-Sparse architecture is based on the Transformer architecture with modifications to enable sparsity in the activations:\n\n1. **Top-K Sparsity**: \n   - Applies a mask to keep only the top K% of activations (by magnitude).\n   - Rescales the output by its L2 norm.\n\n2. **Quantized Top-K Sparsity**:\n   - Quantizes the input to 8-bit representation before applying Top-K sparsity.\n\n3. **Squared ReLU (ReLU²GLU)**:\n   - Implements ReLU²GLU for feed-forward layers: `ReLU²GLU(X) = X · W_up^T ⊙ ReLU²(X · W_gate^T)`\n\n## Experiment: ReLU vs ReLU2GLU\n# ReLU\n![image](https://github.com/user-attachments/assets/6fb08565-6e50-4262-a755-84965d684682)\n\n# ReLU2GLU\n![image](https://github.com/user-attachments/assets/f159cf4d-fe4d-4b16-b87a-2cc3dcd14104)\n\n\n## Installation\n\n```bash\ngit clone https://github.com/nanowell/Q-Sparse-LLM.git\ncd Q-Sparse-LLM\n```\n\n## Usage\n\nHere's a basic example of how to use the Q-Sparse-LLM model:\n\n```python\nfrom q_sparse import QSparseModel\n\n# Initialize the model\nmodel = QSparseModel(\n    vocab_size=30000,\n    d_model=768,\n    nhead=12,\n    num_layers=12,\n    dim_feedforward=3072,\n    k_ratio=0.5,\n    quantized=True\n)\n\n# Use the model for inference or training\n# (Add specific usage instructions based on your implementation)\n```\n\n## Contributing\n\nContributions to Q-Sparse-LLM are welcome!\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n\n## Citation\n\nIf you use Q-Sparse-LLM in your research, please cite:\n\n```\n@software{Q-Sparse-LLM,\n  author = {nanowell},\n  title = {Q-Sparse-LLM: Quantized Sparse Language Model},\n  year = {2024},\n  url = {https://github.com/nanowell/Q-Sparse-LLM}\n}\n```\n\n## Acknowledgements\n\nThis project builds upon the work Q-Sparse paper:\n```\n@misc{wang2024qsparselargelanguagemodels,\n      title={Q-Sparse: All Large Language Models can be Fully Sparsely-Activated}, \n      author={Hongyu Wang and Shuming Ma and Ruiping Wang and Furu Wei},\n      year={2024},\n      eprint={2407.10969},\n      archivePrefix={arXiv},\n      primaryClass={cs.CL},\n      url={https://arxiv.org/abs/2407.10969}, \n}\n```\n## Contact\n\nFor questions and feedback, please open an issue in the GitHub repository or contact [zarugeos@gmail.com].\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanowell%2Fq-sparse-llm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanowell%2Fq-sparse-llm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanowell%2Fq-sparse-llm/lists"}