Instructions to use BiliSakura/ProMoE-diffusers with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use BiliSakura/ProMoE-diffusers with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("BiliSakura/ProMoE-diffusers", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
| import math | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from .modeling_promoe_common import ( | |
| Attention, | |
| FinalLayer, | |
| LabelEmbedder, | |
| Mlp, | |
| MoeMLP_DiffMoE as MoeMLP, | |
| PatchEmbed, | |
| TimestepEmbedder, | |
| get_2d_sincos_pos_embed, | |
| modulate, | |
| ) | |
| class MoEGate(nn.Module): | |
| def __init__(self, embed_dim, num_experts=16, num_experts_per_tok=2, aux_loss_alpha=0.01): | |
| super().__init__() | |
| self.top_k = num_experts_per_tok | |
| self.n_routed_experts = num_experts | |
| self.scoring_func = "softmax" | |
| self.alpha = aux_loss_alpha | |
| self.seq_aux = False | |
| self.norm_topk_prob = False | |
| self.gating_dim = embed_dim | |
| self.weight = nn.Parameter(torch.empty((self.n_routed_experts, self.gating_dim))) | |
| self.reset_parameters() | |
| def reset_parameters(self): | |
| nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5)) | |
| def forward(self, hidden_states): | |
| bsz, seq_len, h = hidden_states.shape | |
| hidden_states = hidden_states.view(-1, h) | |
| logits = F.linear(hidden_states, self.weight, None) | |
| if self.scoring_func != "softmax": | |
| raise NotImplementedError(f"Unsupported gating scoring function: {self.scoring_func}") | |
| scores = logits.softmax(dim=-1) | |
| topk_weight, topk_idx = torch.topk(scores, k=self.top_k, dim=-1, sorted=False) | |
| if self.top_k > 1 and self.norm_topk_prob: | |
| topk_weight = topk_weight / (topk_weight.sum(dim=-1, keepdim=True) + 1e-20) | |
| if self.training and self.alpha > 0.0: | |
| scores_for_aux = scores | |
| topk_idx_for_aux_loss = topk_idx.view(bsz, -1) | |
| if self.seq_aux: | |
| scores_for_seq_aux = scores_for_aux.view(bsz, seq_len, -1) | |
| ce = torch.zeros(bsz, self.n_routed_experts, device=hidden_states.device) | |
| ce.scatter_add_( | |
| 1, | |
| topk_idx_for_aux_loss, | |
| torch.ones(bsz, seq_len * self.top_k, device=hidden_states.device), | |
| ).div_(seq_len * self.top_k / self.n_routed_experts) | |
| aux_loss = (ce * scores_for_seq_aux.mean(dim=1)).sum(dim=1).mean() * self.alpha | |
| else: | |
| mask_ce = F.one_hot(topk_idx_for_aux_loss.view(-1), num_classes=self.n_routed_experts) | |
| ce = mask_ce.float().mean(0) | |
| pi = scores_for_aux.mean(0) | |
| fi = ce * self.n_routed_experts | |
| aux_loss = (pi * fi).sum() * self.alpha | |
| else: | |
| aux_loss = None | |
| return topk_idx, topk_weight, aux_loss | |
| class AddAuxiliaryLoss(torch.autograd.Function): | |
| def forward(ctx, x, loss): | |
| ctx.dtype = loss.dtype | |
| ctx.required_aux_loss = loss.requires_grad | |
| return x | |
| def backward(ctx, grad_output): | |
| grad_loss = torch.ones(1, dtype=ctx.dtype, device=grad_output.device) if ctx.required_aux_loss else None | |
| return grad_output, grad_loss | |
| class SparseMoEBlock(nn.Module): | |
| def __init__( | |
| self, | |
| experts, | |
| hidden_dim, | |
| mlp_ratio=4, | |
| num_experts=16, | |
| num_experts_per_tok=2, | |
| pretraining_tp=2, | |
| n_shared_experts=2, | |
| ): | |
| super().__init__() | |
| self.top_k = num_experts_per_tok | |
| self.experts = nn.ModuleList(experts) | |
| self.gate = MoEGate(embed_dim=hidden_dim, num_experts=num_experts, num_experts_per_tok=num_experts_per_tok) | |
| self.n_shared_experts = n_shared_experts | |
| if self.n_shared_experts > 0: | |
| intermediate_size = hidden_dim * self.n_shared_experts | |
| self.shared_experts = MoeMLP( | |
| hidden_size=hidden_dim, | |
| intermediate_size=intermediate_size, | |
| pretraining_tp=pretraining_tp, | |
| ) | |
| def forward(self, hidden_states): | |
| identity = hidden_states | |
| orig_shape = hidden_states.shape | |
| topk_idx, topk_weight, aux_loss = self.gate(hidden_states) | |
| hidden_states = hidden_states.view(-1, hidden_states.shape[-1]) | |
| flat_topk_idx = topk_idx.view(-1) | |
| if self.training: | |
| hidden_states = hidden_states.repeat_interleave(self.top_k, dim=0) | |
| y = torch.empty_like(hidden_states, dtype=hidden_states.dtype) | |
| for i, expert in enumerate(self.experts): | |
| y[flat_topk_idx == i] = expert(hidden_states[flat_topk_idx == i]).float() | |
| y = (y.view(*topk_weight.shape, -1) * topk_weight.unsqueeze(-1)).sum(dim=1) | |
| y = y.view(*orig_shape) | |
| y = AddAuxiliaryLoss.apply(y, aux_loss) | |
| else: | |
| y = self.moe_infer(hidden_states, flat_topk_idx, topk_weight.view(-1, 1)).view(*orig_shape) | |
| if self.n_shared_experts > 0: | |
| y = y + self.shared_experts(identity) | |
| return y | |
| def moe_infer(self, x, flat_expert_indices, flat_expert_weights): | |
| expert_cache = torch.zeros_like(x) | |
| idxs = flat_expert_indices.argsort() | |
| tokens_per_expert = flat_expert_indices.bincount().cpu().numpy().cumsum(0) | |
| token_idxs = idxs // self.top_k | |
| for i, end_idx in enumerate(tokens_per_expert): | |
| start_idx = 0 if i == 0 else tokens_per_expert[i - 1] | |
| if start_idx == end_idx: | |
| continue | |
| expert = self.experts[i] | |
| exp_token_idx = token_idxs[start_idx:end_idx] | |
| expert_tokens = x[exp_token_idx] | |
| expert_out = expert(expert_tokens) | |
| expert_out.mul_(flat_expert_weights[idxs[start_idx:end_idx]]) | |
| expert_cache = expert_cache.to(expert_out.dtype) | |
| expert_cache.scatter_reduce_( | |
| 0, | |
| exp_token_idx.view(-1, 1).repeat(1, x.shape[-1]), | |
| expert_out, | |
| reduce="sum", | |
| ) | |
| return expert_cache | |
| class DiTBlock(nn.Module): | |
| def __init__( | |
| self, | |
| hidden_size, | |
| num_heads, | |
| mlp_ratio=4, | |
| pretraining_tp=2, | |
| use_swiglu=False, | |
| MoE_config=None, | |
| use_moe=True, | |
| **block_kwargs, | |
| ): | |
| super().__init__() | |
| self.norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) | |
| self.attn = Attention(hidden_size, num_heads=num_heads, qkv_bias=True, **block_kwargs) | |
| self.norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) | |
| mlp_hidden_dim = int(hidden_size * mlp_ratio) | |
| self.use_moe = use_moe | |
| if use_moe: | |
| if not use_swiglu: | |
| approx_gelu = lambda: nn.GELU(approximate="tanh") | |
| experts = [ | |
| Mlp(in_features=hidden_size, hidden_features=mlp_hidden_dim, act_layer=approx_gelu, drop=0) | |
| for _ in range(MoE_config.num_experts) | |
| ] | |
| else: | |
| experts = [ | |
| MoeMLP( | |
| hidden_size=hidden_size, | |
| intermediate_size=mlp_hidden_dim, | |
| pretraining_tp=pretraining_tp, | |
| ) | |
| for _ in range(MoE_config.num_experts) | |
| ] | |
| self.mlp = SparseMoEBlock( | |
| experts=experts, | |
| hidden_dim=hidden_size, | |
| num_experts=MoE_config.num_experts, | |
| num_experts_per_tok=MoE_config.capacity, | |
| n_shared_experts=MoE_config.n_shared_experts, | |
| ) | |
| else: | |
| if not use_swiglu: | |
| approx_gelu = lambda: nn.GELU(approximate="tanh") | |
| self.mlp = Mlp(in_features=hidden_size, hidden_features=mlp_hidden_dim, act_layer=approx_gelu, drop=0) | |
| else: | |
| self.mlp = MoeMLP(hidden_size=hidden_size, intermediate_size=mlp_hidden_dim) | |
| self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 6 * hidden_size, bias=True)) | |
| def forward(self, x, c): | |
| shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.adaLN_modulation(c).chunk(6, dim=1) | |
| x = x + gate_msa.unsqueeze(1) * self.attn(modulate(self.norm1(x), shift_msa, scale_msa)) | |
| x = x + gate_mlp.unsqueeze(1) * self.mlp(modulate(self.norm2(x), shift_mlp, scale_mlp)) | |
| return x | |
| class DiT(nn.Module): | |
| def __init__( | |
| self, | |
| input_size=32, | |
| patch_size=2, | |
| in_channels=4, | |
| hidden_size=1152, | |
| depth=28, | |
| num_heads=16, | |
| mlp_ratio=4, | |
| qk_norm=False, | |
| class_dropout_prob=0.1, | |
| num_classes=1000, | |
| pretraining_tp=1, | |
| learn_sigma=True, | |
| use_swiglu=False, | |
| MoE_config=None, | |
| ): | |
| super().__init__() | |
| self.learn_sigma = learn_sigma | |
| self.in_channels = in_channels | |
| self.out_channels = in_channels * 2 if learn_sigma else in_channels | |
| self.patch_size = patch_size | |
| self.num_heads = num_heads | |
| self.MoE_config = MoE_config | |
| use_moe_flag = [i % 2 == 1 for i in range(depth)] if self.MoE_config.interleave else [True] * depth | |
| self.x_embedder = PatchEmbed(input_size, patch_size, in_channels, hidden_size, bias=True) | |
| self.t_embedder = TimestepEmbedder(hidden_size) | |
| self.y_embedder = LabelEmbedder(num_classes, hidden_size, class_dropout_prob) | |
| num_patches = self.x_embedder.num_patches | |
| self.pos_embed = nn.Parameter(torch.zeros(1, num_patches, hidden_size), requires_grad=False) | |
| self.blocks = nn.ModuleList( | |
| [ | |
| DiTBlock( | |
| hidden_size, | |
| num_heads, | |
| mlp_ratio=mlp_ratio, | |
| qk_norm=qk_norm, | |
| use_swiglu=use_swiglu, | |
| pretraining_tp=pretraining_tp, | |
| MoE_config=MoE_config, | |
| use_moe=use_moe_flag[i], | |
| ) | |
| for i in range(depth) | |
| ] | |
| ) | |
| self.final_layer = FinalLayer(hidden_size, patch_size, self.out_channels) | |
| self.initialize_weights() | |
| def initialize_weights(self): | |
| def _basic_init(module): | |
| if isinstance(module, nn.Linear): | |
| torch.nn.init.xavier_uniform_(module.weight) | |
| if module.bias is not None: | |
| nn.init.constant_(module.bias, 0) | |
| self.apply(_basic_init) | |
| pos_embed = get_2d_sincos_pos_embed(self.pos_embed.shape[-1], int(self.x_embedder.num_patches**0.5)) | |
| self.pos_embed.data.copy_(torch.from_numpy(pos_embed).float().unsqueeze(0)) | |
| w = self.x_embedder.proj.weight.data | |
| nn.init.xavier_uniform_(w.view([w.shape[0], -1])) | |
| nn.init.constant_(self.x_embedder.proj.bias, 0) | |
| nn.init.normal_(self.y_embedder.embedding_table.weight, std=0.02) | |
| nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02) | |
| nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02) | |
| for block in self.blocks: | |
| nn.init.constant_(block.adaLN_modulation[-1].weight, 0) | |
| nn.init.constant_(block.adaLN_modulation[-1].bias, 0) | |
| nn.init.constant_(self.final_layer.adaLN_modulation[-1].weight, 0) | |
| nn.init.constant_(self.final_layer.adaLN_modulation[-1].bias, 0) | |
| nn.init.constant_(self.final_layer.linear.weight, 0) | |
| nn.init.constant_(self.final_layer.linear.bias, 0) | |
| def unpatchify(self, x): | |
| c = self.out_channels | |
| p = self.x_embedder.patch_size[0] | |
| h = w = int(x.shape[1] ** 0.5) | |
| x = x.reshape(shape=(x.shape[0], h, w, p, p, c)) | |
| x = torch.einsum("nhwpqc->nchpwq", x) | |
| return x.reshape(shape=(x.shape[0], c, h * p, h * p)) | |
| def forward(self, x, t, context, **kwargs): | |
| y = context | |
| if len(x.shape) != 4: | |
| x = x.squeeze(2) | |
| x = self.x_embedder(x) + self.pos_embed | |
| t = self.t_embedder(t) | |
| y = self.y_embedder(y, self.training) | |
| c = t + y | |
| for block in self.blocks: | |
| x = block(x, c) | |
| x = self.final_layer(x, c) | |
| return self.unpatchify(x) | |