agent-management
Create, manage, and orchestrate AI agents using the AI Maestro CLI. Use when the user asks to "create agent", "list agents",…
Train Mixture of Experts (MoE) models using DeepSpeed or HuggingFace. Use when training large-scale models with limited compute (5× cost reduction vs dense models), implementing sparse architectures like Mixtral 8x7B or DeepSeek-V3, or scaling model capacity without proportional
Claude Code
$ npx -y skills add davila7/claude-code-templates --skill emerging-techniques-moe-training --agent claude-code
Codex
$ npx skills add davila7/claude-code-templates --skill emerging-techniques-moe-training -a codex -y
Cursor
$ npx skills add davila7/claude-code-templates --skill emerging-techniques-moe-training -a cursor -y
Gemini CLI
$ npx skills add davila7/claude-code-templates --skill emerging-techniques-moe-training -a gemini-cli -y
GitHub Copilot
$ npx skills add davila7/claude-code-templates --skill emerging-techniques-moe-training -a github-copilot -y
OpenCode
$ npx skills add davila7/claude-code-templates --skill emerging-techniques-moe-training -a opencode -y
Windsurf
$ npx skills add davila7/claude-code-templates --skill emerging-techniques-moe-training -a windsurf -y
name: emerging-techniques-moe-training description: Train Mixture of Experts (MoE) models using DeepSpeed or HuggingFace. Use when training large-scale models with limited compute (5× cost reduction vs dense models), implementing sparse architectures like Mixtral 8x7B or DeepSeek-V3, or scaling model capacity without proportional compute increase. Covers MoE architectures, routing mechanisms, load balancing, expert parallelism, and inference optimization. license: MIT metadata: version: 1.0.0 author: Orchestra Research tags: [Emerging Techniques, MoE, Mixture Of Experts, Sparse Models, DeepSpeed, Expert Parallelism, Mixtral, DeepSeek, Routing, Load Balancing, Efficient Training] dependencies: [deepspeed, transformers, torch, accelerate]
Use MoE Training when you need to:
**Notable MoE Models**: Mixtral 8x7B (Mistral AI), DeepSeek-V3, Switch Transformers (Google), GLaM (Google), NLLB-MoE (Meta)
# DeepSpeed with MoE support pip install deepspeed>=0.6.0 # Megatron-DeepSpeed for large-scale training git clone https://github.com/microsoft/Megatron-DeepSpeed cd Megatron-DeepSpeed pip install -r requirements.txt # Alternative: HuggingFace Transformers pip install transformers accelerate
import torch
import torch.nn as nn
class MoELayer(nn.Module):
"""Sparse Mixture of Experts layer."""
def __init__(self, hidden_size, num_experts=8, top_k=2):
super().__init__()
self.num_experts = num_experts
self.top_k = top_k
# Expert networks (FFN)
self.experts = nn.ModuleList([
nn.Sequential(
nn.Linear(hidden_size, 4 * hidden_size),
nn.GELU(),
nn.Linear(4 * hidden_size, hidden_size)
)
for _ in range(num_experts)
])
# Gating network (router)
self.gate = nn.Linear(hidden_size, num_experts)
def forward(self, x):
# x shape: (batch_size, seq_len, hidden_size)
batch_size, seq_len, hidden_size = x.shape
# Flatten for routing
x_flat = x.view(-1, hidden_size) # (batch_size * seq_len, hidden_size)
# Compute gate scores
gate_logits = self.gate(x_flat) # (batch_size * seq_len, num_experts)
# Top-k routing
gate_scores = torch.softmax(gate_logits, dim=-1)
topk_scores, topk_indices = torch.topk(gate_scores, self.top_k, dim=-1)
# Normalize top-k scores
topk_scores = topk_scores / topk_scores.sum(dim=-1, keepdim=True)
# Dispatch and combine expert outputs
output = torch.zeros_like(x_flat)
for i in range(self.top_k):
expert_idx = topk_indices[:, i]
expert_scores = topk_scores[:, i].unsqueeze(-1)
# Route tokens to experts
for expert_id in range(self.num_experts):
mask = (expert_idx == expert_id)
if mask.any():
expert_input = x_flat[mask]
expert_output = self.experts[expert_id](expert_input)
output[mask] += expert_scores[mask] * expert_output
# Reshape back
return output.view(batch_size, seq_len, hidden_size)# Training script with MoE deepspeed pretrain_gpt_moe.py \ --num-layers 24 \ --hidden-size 1024 \ --num-attention-heads 16 \ --seq-length 2048 \ --max-position-embeddings 2048 \ --micro-batch-size 4 \ --global-batch-size 256 \ --train-iters 500000 \ --lr 0.0001 \ --min-lr 0.00001 \ --lr-decay-style cosine \ --num-experts 128 \ --moe-expert-parallel-size 4 \ --moe-loss-coeff 0.01 \ --moe-train-capacity-factor 1.25 \ --moe-eval-capacity-factor 2.0 \ --fp16 \ --deepspeed_config ds_config.json
**Key Components:**
Input Token
↓
Router (Gate Network)
↓
Top-k Expert Selection (e.g., 2 out of 8)
↓
Expert 1 (weight: 0.6) + Expert 5 (weight: 0.4)
↓
Weighted Combination
↓
Output**Top-1 Routing (Switch Transformer):**
# Simplest routing: one expert per token gate_logits = router(x) # (batch, seq_len, num_experts) expert_idx = torch.argmax(gate_logits, dim=-1) # Hard routing
**Top-2 Routing (Mixtral):**
# Top-2: two experts per token
gate_scores = torch.softmax(router(x), dim=-1)
top2_scores, top2_indices = torch.topk(gate_scores, k=2, dim=-1)
# Normalize scores
top2_scores = top2_scores / top2_scores.sum(dim=-1, keepdim=True)
# Combine expert outputs
output = (top2_scores[:, :, 0:1] * expert_outputs[top2_indices[:, :, 0]] +
top2_scores[:, :, 1:2] * expert_outputs[top2_indices[:, :, 1]])**Expert Choice Routing:**
# Experts choose top-k tokens (instead of tokens choosing experts) # Guarantees perfect load balancing expert_scores = router(x).transpose(-1, -2) # (batch, num_experts, seq_len) topk_tokens = torch.topk(expert_scores, k=capacity_per_expert, dim=-1)
**Auxiliary Loss:**
def load_balancing_loss(gate_logits, expert_indices, num_experts):
"""Encourage uniformOpen-source registry and CLI for Claude Code components: agents, commands, skills, hooks, MCPs, settings and mods
Create, manage, and orchestrate AI agents using the AI Maestro CLI. Use when the user asks to "create agent", "list agents",…
Send and receive cryptographically signed messages between AI agents using the Agent Messaging Protocol (AMP). Use when the user…
Search auto-generated codebase documentation for function signatures, API docs, class definitions, and code comments. Use when…
Query the code graph database to understand component relationships, dependencies, and change impact. Use when the user asks to…
Search conversation history and semantic memory to recall previous discussions, decisions, and context. Use when the user asks to…
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…
Ask which skill or flow fits your situation. A router over the skills in this repo.