Soft Mixture of Experts
PyTorch implementation of Soft Mixture of Experts (Soft-MoE) from "From Sparse to Soft Mixtures of Experts".
This implementation extends the timm library's VisionTransformer class to support Soft-MoE MLP layers.
Installation
pip install soft-moe
Or install the entire repo with:
git clone https://github.com/bwconrad/soft-moe
cd soft-moe/
pip install -r requirements.txt
Usage
Initializing a Soft Mixture of Experts Vision Transformer
import torch
from soft_moe import SoftMoEVisionTransformer
net = SoftMoEVisionTransformer(
num_experts=128,
slots_per_expert=1,
moe_layer_index=6,
img_size=224,
patch_size=32,
num_classes=1000,
embed_dim=768,
depth=12,
num_heads=12,
mlp_ratio=4,
)
img = torch.randn(1, 3, 224, 224)
preds = net(img)
Functions are also available to initialize default network configurations:
from soft_moe import (soft_moe_vit_base, soft_moe_vit_huge,
soft_moe_vit_large, soft_moe_vit_small,
soft_moe_vit_tiny)
net = soft_moe_vit_tiny()
net = soft_moe_vit_small()
net = soft_moe_vit_base()
net = soft_moe_vit_large()
net = soft_moe_vit_huge()
net = soft_moe_vit_tiny(num_experts=64, slots_per_expert=2, img_size=128)
Setting the Mixture of Expert Layers
The moe_layer_index argument sets at which layer indices to use MoE MLP layers instead of regular MLP layers.
When an int is given, all layers starting from that depth index will be MoE layers.
net = SoftMoEVisionTransformer(
moe_layer_index=6, # Blocks 6-12
depth=12,
)
When a list is given, all specified layers will be MoE layers.
net = SoftMoEVisionTransformer(
moe_layer_index=[0, 2, 4], # Blocks 0, 2 and 4
depth=12,
)
- Note:
moe_layer_indexuses 0-index convention.
Creating a Soft Mixture of Experts Layer
The 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.
import torch
import torch.nn as nn
from soft_moe import SoftMoELayerWrapper
x = torch.rand(1, 16, 128)
layer = SoftMoELayerWrapper(
dim=128,
slots_per_expert=2,
num_experts=32,
layer=nn.Linear,
# nn.Linear arguments
in_features=128,
out_features=32,
)
y = layer(x)
layer = SoftMoELayerWrapper(
dim=128,
slots_per_expert=1,
num_experts=16,
layer=nn.TransformerEncoderLayer,
# nn.TransformerEncoderLayer arguments
d_model=128,
nhead=8,
)
y = layer(x)
- Note: If the name of a layer argument overlaps with one of other arguments (e.g.
dim) you can pass a partial function tolayer.- e.g.
layer=partial(MyCustomLayer, dim=128)
- e.g.
Citation
@article{puigcerver2023sparse,
title={From Sparse to Soft Mixtures of Experts},
author={Puigcerver, Joan and Riquelme, Carlos and Mustafa, Basil and Houlsby, Neil},
journal={arXiv preprint arXiv:2308.00951},
year={2023}
}
Metadata
Release files for soft-moe 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| soft_moe-0.0.1.tar.gz | 13.8 kB | Without provenance | Details for soft_moe-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| soft_moe-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for soft_moe-0.0.1-py3-none-any.whl |
Total release size: 28.2 kB
Release files / soft_moe-0.0.1.tar.gz
| Download URL | soft_moe-0.0.1.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6d2aaace545cf301d1c596f5bad2e296ce9ae7b8d76fb373d20124e5de9a286f
|
|
BLAKE2b-256 checksum How to use checksums |
24802a0615570c6fa8020d92039ed0bc3a702db275c15c57d366d28fea264c59
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / soft_moe-0.0.1-py3-none-any.whl
| Download URL | soft_moe-0.0.1-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
573f82e0e883d0a3bdcb05592a64c7cf15a959fcd62afa1b79dda0359945945f
|
|
BLAKE2b-256 checksum How to use checksums |
ca01edd5b1efc5f08c32ad1c97802e4df5abdd57339e474ed0cad71b9d9571cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Security for soft-moe 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.