{"id":21606105,"url":"https://github.com/bwconrad/soft-moe","last_synced_at":"2025-04-11T04:04:36.540Z","repository":{"id":188788525,"uuid":"679378691","full_name":"bwconrad/soft-moe","owner":"bwconrad","description":"PyTorch implementation of \"From Sparse to Soft Mixtures of Experts\"","archived":false,"fork":false,"pushed_at":"2023-08-22T20:01:27.000Z","size":352,"stargazers_count":53,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-11T04:04:30.333Z","etag":null,"topics":["computer-vision","machine-learning","mixture-of-experts","pytorch","transformer","vision-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/bwconrad.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":"2023-08-16T17:48:22.000Z","updated_at":"2025-03-21T08:08:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"ab6330c5-9396-4881-a0bc-7533e4f73dc8","html_url":"https://github.com/bwconrad/soft-moe","commit_stats":null,"previous_names":["bwconrad/soft-moe"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwconrad%2Fsoft-moe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwconrad%2Fsoft-moe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwconrad%2Fsoft-moe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwconrad%2Fsoft-moe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bwconrad","download_url":"https://codeload.github.com/bwconrad/soft-moe/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248339286,"owners_count":21087215,"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":["computer-vision","machine-learning","mixture-of-experts","pytorch","transformer","vision-transformer"],"created_at":"2024-11-24T20:19:11.132Z","updated_at":"2025-04-11T04:04:36.473Z","avatar_url":"https://github.com/bwconrad.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Soft Mixture of Experts\n\nPyTorch implementation of Soft Mixture of Experts (Soft-MoE) from [\"From Sparse to Soft Mixtures of Experts\"](https://arxiv.org/abs/2308.00951v1).\nThis implementation extends the [`timm`](https://github.com/huggingface/pytorch-image-models) library's `VisionTransformer` class to support Soft-MoE MLP layers.\n\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/bwconrad/soft-moe/main/assets/fig.png\" width=\"100%\" style={text-align: center;}/\u003e\n\u003c/p\u003e\n\n\n## Installation\n\n```\npip install soft-moe\n```\n\nOr install the entire repo with:\n\n```\ngit clone https://github.com/bwconrad/soft-moe\ncd soft-moe/\npip install -r requirements.txt\n```\n\n## Usage\n\n### Initializing a Soft Mixture of Experts Vision Transformer\n\n```python\nimport torch\nfrom soft_moe import SoftMoEVisionTransformer\n\nnet = SoftMoEVisionTransformer(\n    num_experts=128,\n    slots_per_expert=1,\n    moe_layer_index=6, \n    img_size=224,\n    patch_size=32,\n    num_classes=1000,\n    embed_dim=768,\n    depth=12,\n    num_heads=12,\n    mlp_ratio=4,\n)\n\nimg = torch.randn(1, 3, 224, 224)\npreds = net(img)\n```\n\nFunctions are also available to initialize default network configurations:\n\n```python\nfrom soft_moe import (soft_moe_vit_base, soft_moe_vit_huge,\n                      soft_moe_vit_large, soft_moe_vit_small,\n                      soft_moe_vit_tiny)\n\nnet = soft_moe_vit_tiny()\nnet = soft_moe_vit_small()\nnet = soft_moe_vit_base()\nnet = soft_moe_vit_large()\nnet = soft_moe_vit_huge()\n\nnet = soft_moe_vit_tiny(num_experts=64, slots_per_expert=2, img_size=128)\n```\n\n#### Setting the Mixture of Expert Layers\n\nThe `moe_layer_index` argument sets at which layer indices to use MoE MLP layers instead of regular MLP layers.\nWhen an `int` is given, all layers starting from that depth index will be MoE layers.\n\n```python\nnet = SoftMoEVisionTransformer(\n    moe_layer_index=6, # Blocks 6-12\n    depth=12,\n)\n```\n\nWhen a `list` is given, all specified layers will be MoE layers.\n\n```python\nnet = SoftMoEVisionTransformer(\n    moe_layer_index=[0, 2, 4], # Blocks 0, 2 and 4\n    depth=12,\n)\n```\n\n- __Note__: `moe_layer_index` uses __0-index__ convention.\n\n### Creating a Soft Mixture of Experts Layer \n\nThe `SoftMoELayerWrapper` class can be used to make any network layer, that takes a tensor of shape `[batch, length, dim]`, into a Soft Mixture of Experts layer.\n\n```python \nimport torch\nimport torch.nn as nn\n\nfrom soft_moe import SoftMoELayerWrapper\n\nx = torch.rand(1, 16, 128)\n\nlayer = SoftMoELayerWrapper(\n    dim=128,\n    slots_per_expert=2,\n    num_experts=32,\n    layer=nn.Linear,\n    # nn.Linear arguments\n    in_features=128,\n    out_features=32,\n)\ny = layer(x)\n\nlayer = SoftMoELayerWrapper(\n    dim=128,\n    slots_per_expert=1,\n    num_experts=16,\n    layer=nn.TransformerEncoderLayer,\n    # nn.TransformerEncoderLayer arguments\n    d_model=128,\n    nhead=8,\n)\ny = layer(x)\n```\n\n- __Note__: If the name of a layer argument overlaps with one of other arguments (e.g. `dim`) you can pass a partial function to `layer`.\n    - e.g. `layer=partial(MyCustomLayer, dim=128)`\n\n## Citation\n```bibtex\n@article{puigcerver2023sparse,\n  title={From Sparse to Soft Mixtures of Experts},\n  author={Puigcerver, Joan and Riquelme, Carlos and Mustafa, Basil and Houlsby, Neil},\n  journal={arXiv preprint arXiv:2308.00951},\n  year={2023}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwconrad%2Fsoft-moe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbwconrad%2Fsoft-moe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwconrad%2Fsoft-moe/lists"}