{"id":25326766,"url":"https://github.com/opensparsellms/linear-moe","last_synced_at":"2025-10-29T03:31:27.431Z","repository":{"id":277087759,"uuid":"850955027","full_name":"OpenSparseLLMs/Linear-MoE","owner":"OpenSparseLLMs","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-12T03:16:55.000Z","size":1828,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T04:26:09.473Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/OpenSparseLLMs.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-09-02T06:44:08.000Z","updated_at":"2025-02-12T04:01:28.000Z","dependencies_parsed_at":"2025-02-12T04:26:18.871Z","dependency_job_id":"d82766cc-97ea-42ea-a4d1-933a4c40d8ff","html_url":"https://github.com/OpenSparseLLMs/Linear-MoE","commit_stats":null,"previous_names":["opensparsellms/linear-moe"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenSparseLLMs%2FLinear-MoE","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenSparseLLMs%2FLinear-MoE/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenSparseLLMs%2FLinear-MoE/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenSparseLLMs%2FLinear-MoE/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenSparseLLMs","download_url":"https://codeload.github.com/OpenSparseLLMs/Linear-MoE/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238766088,"owners_count":19526848,"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":[],"created_at":"2025-02-14T01:53:55.553Z","updated_at":"2025-10-29T03:31:27.426Z","avatar_url":"https://github.com/OpenSparseLLMs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# Linear-MoE\n[![arXiv](https://img.shields.io/badge/Arxiv-2503.05447-b31b1b.svg?logo=arXiv)](https://arxiv.org/abs/2503.05447)\n[![stars](https://img.shields.io/github/stars/OpenSparseLLMs/Linear-MoE)](https://github.com/OpenSparseLLMs/Linear-MoE/stargazers)\n\n\u003c/div\u003e\n\nThis repo aims to provide a **production-ready library** for modeling and training Linear-MoE models, non-invasively built on the latest [Megatron-Core](https://github.com/NVIDIA/Megatron-LM). **Contributions through pull requests are highly encouraged!**\n\n\u003c!-- It supports state-of-the-art open-source Mixture of Experts (MoE) models, seamlessly integrated with advanced linear sequence modeling techniques such as Linear Attention, State Space Modeling, and Linear RNN. LInear-MoE is still under development, **Contributions through pull requests are highly encouraged!** --\u003e\n\n\n## News\n- **$\\texttt{[2025-04]}$:** 🥳 Linear-MoE paper has been accepted by SCOPE Workshop@ICLR 2025 as an Oral Paper and wins the Outstanding Paper Honorable Mention Award.\n- **$\\texttt{[2025-03]}$:** 🥳 Add [MoM](https://arxiv.org/abs/2502.13685) (with Gated DeltaNet) and [MoM](https://arxiv.org/abs/2502.13685) (with GLA) into Linear-MoE codebase.\n- **$\\texttt{[2025-03]}$:** 🥳 Linear-MoE [Technical Report](https://arxiv.org/abs/2503.05447) is released.\n\n\n# Model Matrix\n\n|   Linear Sequence Modeling  |  Instance  |  Qwen2 MoE (@Alibaba)  |    Deepseek v2 MoE (@Deepseek)       |    Mixtral MoE (@Mistral AI)   | Llama3 (@Meta)   |\n| :---: | :---: | :---: | :---: | :---: | :---: |\n| Linear Attention (LA) |       [Basic Linear Attention](https://arxiv.org/abs/2006.16236) \u003cbr\u003e (@Idiap@EPFL)  | ✅ |    ✅    |   ✅   | ✅   |\n|  |       [Lightning Attention](https://arxiv.org/abs/2405.17381) \u003cbr\u003e (@Shanghai AI Lab)       | ✅ |          ✅          |     ✅      | ✅   |\n|  |       [Retention](https://arxiv.org/abs/2307.08621) \u003cbr\u003e (@MSRA@THU)       | ✅ |          ✅          |     ✅      | ✅   |\n|  |         [GLA](https://arxiv.org/abs/2312.06635)  \u003cbr\u003e (@MIT@IBM)         | ✅ |     ✅      |    ✅       | ✅   |\n|  |           [Delta Net](https://arxiv.org/abs/2102.11174) \u003cbr\u003e (@MIT)            | ✅ |    ✅    |     ✅      | ✅   |\n|  |           [GSA](https://arxiv.org/abs/2409.07146) \u003cbr\u003e (@SUDA@MIT)      | ✅ |    ✅    |     ✅      | ✅   |\n|  | [Based](https://arxiv.org/abs/2402.18668) \u003cbr\u003e (@Stanford) | ✅ |      ✅      |      ✅     |  ✅   |\n|  |            [Rebased](https://arxiv.org/abs/2402.10644) \u003cbr\u003e (@Tinkoff)            | ✅ |  ✅  |      ✅     | ✅   |\n|  |            [LASP-2](https://arxiv.org/abs/2502.07563) \u003cbr\u003e (@Shanghai AI Lab)            | ✅ |  ✅  |      ✅     | ✅   |\n|  |            [Gated DeltaNet](https://arxiv.org/abs/2412.06464) \u003cbr\u003e (@MIT@NVIDIA)            | ✅ |  ✅  |      ✅     | ✅   |\n|  |            [🔥MoM (with GLA)](https://arxiv.org/abs/2502.13685) \u003cbr\u003e (@Shanghai AI Lab)            | ✅ |  ✅  |      ✅     | ✅   |\n|  |            [🔥MoM (with Gated DeltaNet)](https://arxiv.org/abs/2502.13685) \u003cbr\u003e (@Shanghai AI Lab)            | ✅ |  ✅  |      ✅     | ✅   |\n| State Space Modeling (SSM) |             [Mamba2](https://arxiv.org/abs/2405.21060) \u003cbr\u003e (@Princeton@CMU) | ✅ | ✅  |   ✅   | ✅   |\n| Linear RNN |             [RWKV6](https://arxiv.org/abs/2404.05892) \u003cbr\u003e (@RWKV)              |  ✅  |   ✅   |    ✅    | ✅   |\n|  |             [HGRN2](https://arxiv.org/abs/2404.07904) \u003cbr\u003e (@TapTap@Shanghai AI Lab)             | ✅ |   ✅   |   ✅   |  ✅   |\n| Softmax Attention |             [Softmax Attention](https://arxiv.org/abs/1706.03762) \u003cbr\u003e (@Google)             | ✅ |   ✅   |   ✅   |  ✅   |\n|  |             [FlashAttention-2](https://arxiv.org/abs/2307.08691) \u003cbr\u003e (@Princeton@Stanford)             | ✅ |   ✅   |   ✅   |  ✅   |\n\n\n# Overview\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./images/linear-moe-fig2.png\" width=\"90%\" /\u003e\n\u003c/p\u003e\n\u003cdiv align=\"center\"\u003e\nLinear-MoE Architecture\n\u003c/div\u003e\n\n\u0026nbsp;\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./images/linear-moe-fig1.png\" width=\"70%\" /\u003e\n\u003c/p\u003e\n\u003cdiv align=\"center\"\u003e\nLinear-MoE System: Modeling and Training\n\u003c/div\u003e\n\n# Installation\n\nYour environment should satify the following requirements:\n\n- [PyTorch](https://pytorch.org/) \u003e= 2.0\n- [Triton](https://github.com/openai/triton) \u003e=2.2\n\n## Virtualenv\n\n```bash\n# create a conda env, install PyTorch\nconda create -n linear-moe python=3.11\nconda activate linear-moe\nconda install pytorch pytorch-cuda=12.1 -c pytorch -c nvidia\n\n# (if needed) Apex\ngit clone https://github.com/NVIDIA/apex.git\npip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings \"--build-option=--cpp_ext\" --config-settings \"--build-option=--cuda_ext\" ./\n\n# (if needed) FlashAttention\nMAX_JOBS=8 pip install flash-attn --no-build-isolation\n\n# (if needed) dropout_layer_norm in FlashAttention\ngit clone https://github.com/Dao-AILab/flash-attention.git\ncd flash-attention/csrc/layer_norm \u0026 pip install .\n\n# Transformer Engine\npip install git+https://github.com/NVIDIA/TransformerEngine.git@stable\n\n# Linear-MoE \ngit clone --recurse-submodules https://github.com/OpenSparseLLMs/Linear-MoE.git\n\n# requirements\npip install -r requirements.txt\n```\n\n\n## Container\nWe recommend using the latest release of [NGC's PyTorch container](https://ngc.nvidia.com/catalog/containers/nvidia:pytorch) with DGX nodes, which already have relatively new versions of CUDA, cuDNN, NCCL, PyTorch, Triton, Apex, TransformerEngine, etc., installed.\n\nOn the top of NGC's PyTorch container, you can setup Linear-MoE with:\n```bash\n# Linear-MoE \ngit clone --recurse-submodules https://github.com/OpenSparseLLMs/Linear-MoE.git\n\n# requirements\npip install -r requirements.txt\n```\n\n\n# Usage\n\n## Pretraining or Finetuning\n\n\u003c!-- **Key Features related to pretraining in Linear-MoE**\n- Multiple linear sequence modeling options (Linear Attention, SSM, Linear RNN)\n- Flexible MoE configurations\n- Multi-node distributed training\n- Mixed precision training\n- Gradient checkpointing\n- Token dropping for efficient MoE training --\u003e\n\nTo pretrain or finetune a Linear-MoE model, you can:\n\n1. Open `examples`, choose the model you are going to pretrain or finetune, e.g. `linear_moe_qwen2`.\n\n2. Edit `run_pretrain_qwen.sh` or `run_finetune_qwen.sh` to set your configurations including:\n- Model size (e.g., 0.5B, 1.5B, 7B)\n- Batch size\n- Learning rate\n- Model architecture (e.g., LSM modules, number of experts)\n- Distributed training settings (TP, PP, CP, EP sizes)\n- ...\n\n3. **Start pretraining or finetuning** by: `sh run_pretrain_qwen.sh` or `sh run_finetune_qwen.sh`.\n\nFor example, to train a A0.3B (hybrid) `linear-moe-qwen2` model with `LA_MOUDLE=hgrn2`, you can config `run_pretrain_qwen.sh` as:\n```bash\nENV=dsw\nMODEL_SIZE=A0.3B\nBATCH_SIZE=2\nGLOBAL_BATCH_SIZE=4\nLR=1e-4\nMIN_LR=1e-5\nSEQ_LEN=2048\nPAD_LEN=2048\nPR=bf16\nTP=1\nPP=1\nCP=1\nEP=1\nAC=sel\nDO=true\nFL=false\nFU=false\nSP=false\nTE=false\nMB=false\nUSE_GEMM=false\nTOKEN_DROPPING=false\nTRAIN_CAPACITY_FACTOR=1.25\nEVAL_CAPACITY_FACTOR=2.0\nSAVE_INTERVAL=100000\nDATASET_PATH=xxx/qwen-datasets/wudao_qwenbpe_text_document\nPRETRAIN_CHECKPOINT_PATH=xxx/qwen-ckpts/Qwen2-0.5B\nTRAIN_TOKENS=15000000000\nWARMUP_TOKENS=10000\nOUTPUT_BASEPATH=./output\n\nLA_MODULE=\"hgrn2\"\nBASE_MODEL=\"qwen2\"\n\n# for linear attention and linear RNN models\n# pure linear\n# LAYER_TYPE_LIST=\"LLLLLLLLLLLL\"\n# hybrid model\nLAYER_TYPE_LIST=\"LLLNLLLNLLLN\"\n\n# for SSM models (Mamba2), MLP layers are fixed behind mamba or attention layers. \n# M: mamba layer, *: attention layer\n# pure mamba2\n# HYBRID_OVERRIDE_PATTERN=\"MMMMMMMMMMMM\"\n# hybrid mamba2\n# HYBRID_OVERRIDE_PATTERN=\"MMM*MMM*MMM*\"\n\n# Linear Attention \u0026 Linear RNN\nlinear_moe_options=\" \\\n        --use-la-module \\\n        --la-module ${LA_MODULE} \\\n        --la-mode fused_chunk \\\n        --base-model ${BASE_MODEL} \\\n        --la-feature-map swish \\\n        --la-output-norm rmsnorm \\\n        --la-gate-fn swish \\\n        --layer-type-list ${LAYER_TYPE_LIST} \\\n        \"\n\n# # SSM\n# linear_moe_options=\" \\\n#         --use-la-module \\\n#         --la-module ${LA_MODULE} \\\n#         --base-model ${BASE_MODEL} \\\n#         \"\n```\n\n\n## Evaluation\n\nWe use [EleutherAI/lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) for benchmark evaluation. See [eval/README.md](eval/README.md) for detailed instruction.\n\n\n# Acknowledgement\nWe built this repo upon [alibaba/PAI-Megatron-Patch](https://github.com/alibaba/Pai-Megatron-Patch), using [Megatron-Core](https://github.com/NVIDIA/Megatron-LM) as the backend training engine. We integrate the triton-implemented linear attention kernels from [fla-org/flash-linear-attention](https://github.com/fla-org/flash-linear-attention), and Mamba2 kernel from [state-spaces/mamba](https://github.com/state-spaces/mamba) to maximize the hardware efficiency.\n\n# Citation\nIf you find this repo useful, please consider citing our work:\n```bib\n@article{sun2025linear-moe,\n  title={Linear-MoE: Linear Sequence Modeling Meets Mixture-of-Experts},\n  author={Sun, Weigao and Lan, Disen and Zhu, Tong and Qu, Xiaoye and Cheng, Yu},\n  journal={arXiv preprint arXiv:2503.05447},\n  year={2025}\n}\n\n@software{sun2024linear-moe,\n  title  = {Linear-MoE: A Production-Ready Library for Modeling and Training Linear-MoE Models},\n  author = {Sun, Weigao and Lan, Disen and Zhu, Tong and Du, Jusen},\n  url    = {https://github.com/OpenSparseLLMs/Linear-MoE},\n  year   = {2024}\n}\n\n@article{du2025mom,\n  title={MoM: Linear Sequence Modeling with Mixture-of-Memories},\n  author={Du, Jusen and Sun, Weigao and Lan, Disen and Hu, Jiaxi and Cheng, Yu},\n  journal={arXiv preprint arXiv:2502.13685},\n  year={2025}\n}\n\n@article{sun2025lasp2,\n  title={LASP-2: Rethinking Sequence Parallelism for Linear Attention and Its Hybrid},\n  author={Sun, Weigao and Lan, Disen and Zhong, Yiran and Qu, Xiaoye and Cheng, Yu},\n  journal={arXiv preprint arXiv:2502.07563},\n  year={2025}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopensparsellms%2Flinear-moe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopensparsellms%2Flinear-moe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopensparsellms%2Flinear-moe/lists"}