← 开源
lucidrains

mixture-of-experts

A Pytorch implementation of Sparsely-Gated Mixture of Experts, for massively increasing the parameter count of language models

Model DevelopmentArchitecturePython
在 GitHub 打开
增长势头
+024 小时新增 Star0.0%
875
Star
71
Fork
+2
本周
1
贡献者
创建于 2020-07-13 · 更新于 2026-10-13 · 今日第 12989 名
主要开发者

lucidrains/mixture-of-experts 排名第 12,989,有 875 个星标。

同类别比较

lucidrains/mixture-of-experts 排名第 12,989,此处有 8 个同类条目;sbi-dev/sbi 排名第 12,990。

同类别比较
名称排名指标
EpistasisLab/pmlb12,973
语言:
Python
星标数:
876
分叉数:
144
简介:
PMLB: A large, curated repository of benchmark datasets for evaluating supervised machine learning algorithms.
WooooDyy/AgentGym-RL12,975
语言:
Python
星标数:
876
分叉数:
89
简介:
Code and implementations for the paper "AgentGym-RL: Training LLM Agents for Long-Horizon Decision Making through Multi-Turn Reinforcement Learning" by Zhiheng Xi et al.
brownvc/R3GAN12,976
语言:
Python
星标数:
876
分叉数:
45
简介:
Code for NeurIPS 2024 paper - The GAN is dead; long live the GAN! A Modern Baseline GAN - by Huang et al.
polarisZhao/PFLD-pytorch12,984
语言:
Python
星标数:
876
分叉数:
205
简介:
PFLD pytorch Implementation
sbi-dev/sbi12,990
语言:
Python
星标数:
875
分叉数:
272
简介:
sbi is a Python package for simulation-based inference, designed to meet the needs of both researchers and practitioners. Whether you need fine-grained control or an easy-to-use interface, sbi has you covered.
allenai/XNOR-Net12,995
语言:
Lua
星标数:
874
分叉数:
235
简介:
ImageNet classification using binary Convolutional Neural Networks
jeffreyyihuang/two-stream-action-recognition12,998
语言:
Python
星标数:
874
分叉数:
246
简介:
Using two stream architecture to implement a classic action recognition method on UCF101 dataset
Newbeeer/Poisson_flow13,001
语言:
Python
星标数:
873
分叉数:
56
简介:
Code for NeurIPS 2022 Paper, "Poisson Flow Generative Models" (PFGM)
关键参数与规格
关键参数与规格
参数值
语言Python
类别Model Development
子类别Architecture
星标数875
分叉数71
贡献者1
创建日期2020-07-13
更新日期2026-10-13
1 天星标数0
7 天星标数2
排名12,989
上次排名12,883
排名变化-106
7 天星标排名8,407
上次 7 天星标排名8,485
7 天星标排名变化78
总星标排名12,378
上次总星标排名12,355
总星标排名变化-23
国家USA
README 摘录

Sparsely Gated Mixture of Experts - Pytorch A Pytorch implementation of Sparsely Gated Mixture of Experts , for massively increasing the capacity (parameter count) of a language model while keeping the computation constant. It will mostly be a line-by-line transcription of the tensorflow implementation here , with a few enhancements. Update: You should now use ST Mixture of Experts Install Usage The above should suffice for a single machine, but if you want a heirarchical mixture of experts (2 levels), as

used in the GShard paper, please follow the instructions below 1 billion parameters If you want some more sophisticated network for the experts, you can define your own and pass it into the class as Citation

常见问题

记录了多少星标和分叉?

lucidrains/mixture-of-experts 有 875 个星标和 71 个分叉。

仓库排名是多少?

lucidrains/mixture-of-experts 排名第 12,989。

记录的语言和类别是什么?

lucidrains/mixture-of-experts 有 875 个星标,语言为 Python,类别为 Model Development。

哪个仓库排名最接近?

lucidrains/mixture-of-experts 与 sbi-dev/sbi 比较,后者排名第 12,990,有 875 个星标。

README

Sparsely Gated Mixture of Experts - Pytorch

A Pytorch implementation of Sparsely Gated Mixture of Experts, for massively increasing the capacity (parameter count) of a language model while keeping the computation constant.

It will mostly be a line-by-line transcription of the tensorflow implementation here, with a few enhancements.

Update: You should now use ST Mixture of Experts

Install

$ pip install mixture_of_experts

Usage

import torch
from torch import nn
from mixture_of_experts import MoE

moe = MoE(
    dim = 512,
    num_experts = 16,               # increase the experts (# parameters) of your model without increasing computation
    hidden_dim = 512 * 4,           # size of hidden dimension in each expert, defaults to 4 * dimension
    activation = nn.LeakyReLU,      # use your preferred activation, will default to GELU
    second_policy_train = 'random', # in top_2 gating, policy for whether to use a second-place expert
    second_policy_eval = 'random',  # all (always) | none (never) | threshold (if gate value > the given threshold) | random (if gate value > threshold * random_uniform(0, 1))
    second_threshold_train = 0.2,
    second_threshold_eval = 0.2,
    capacity_factor_train = 1.25,   # experts have fixed capacity per batch. we need some extra capacity in case gating is not perfectly balanced.
    capacity_factor_eval = 2.,      # capacity_factor_* should be set to a value >=1
    loss_coef = 1e-2                # multiplier on the auxiliary expert balancing auxiliary loss
)

inputs = torch.randn(4, 1024, 512)
out, aux_loss = moe(inputs) # (4, 1024, 512), (1,)

The above should suffice for a single machine, but if you want a heirarchical mixture of experts (2 levels), as used in the GShard paper, please follow the instructions below

import torch
from mixture_of_experts import HeirarchicalMoE

moe = HeirarchicalMoE(
    dim = 512,
    num_experts = (4, 4),       # 4 gates on the first layer, then 4 experts on the second, equaling 16 experts
)

inputs = torch.randn(4, 1024, 512)
out, aux_loss = moe(inputs) # (4, 1024, 512), (1,)

1 billion parameters

import torch
from mixture_of_experts import HeirarchicalMoE

moe = HeirarchicalMoE(
    dim = 512,
    num_experts = (22, 22)
).cuda()

inputs = torch.randn(1, 1024, 512).cuda()
out, aux_loss = moe(inputs)

total_params = sum(p.numel() for p in moe.parameters())
print(f'number of parameters - {total_params}')

If you want some more sophisticated network for the experts, you can define your own and pass it into the MoE class as experts

import torch
from torch import nn
from mixture_of_experts import MoE

# a 3 layered MLP as the experts

class Experts(nn.Module):
    def __init__(self, dim, num_experts = 16):
        super().__init__()
        self.w1 = nn.Parameter(torch.randn(num_experts, dim, dim * 4))
        self.w2 = nn.Parameter(torch.randn(num_experts, dim * 4, dim * 4))
        self.w3 = nn.Parameter(torch.randn(num_experts, dim * 4, dim))
        self.act = nn.LeakyReLU(inplace = True)

    def forward(self, x):
        hidden1 = self.act(torch.einsum('end,edh->enh', x, self.w1))
        hidden2 = self.act(torch.einsum('end,edh->enh', hidden1, self.w2))
        out = torch.einsum('end,edh->enh', hidden2, self.w3)
        return out

experts = Experts(512, num_experts = 16)

moe = MoE(
    dim = 512,
    num_experts = 16,
    experts = experts
)

inputs = torch.randn(4, 1024, 512)
out, aux_loss = moe(inputs) # (4, 1024, 512), (1,)

Citation

@misc{shazeer2017outrageously,
    title   = {Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer},
    author  = {Noam Shazeer and Azalia Mirhoseini and Krzysztof Maziarz and Andy Davis and Quoc Le and Geoffrey Hinton and Jeff Dean},
    year    = {2017},
    eprint  = {1701.06538},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG}
}
@misc{lepikhin2020gshard,
    title   = {GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding},
    author  = {Dmitry Lepikhin and HyoukJoong Lee and Yuanzhong Xu and Dehao Chen and Orhan Firat and Yanping Huang and Maxim Krikun and Noam Shazeer and Zhifeng Chen},
    year    = {2020},
    eprint  = {2006.16668},
    archivePrefix = {arXiv},
    primaryClass = {cs.CL}
}
排名相邻
同类项目