Implement moe mamba text generation model
Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/english_gpt4_8/implement-moe-mamba-text-generation-model
Implement a Mixture-of-Experts (MoE) Mamba model architecture for text generation, including data loading, training loop, and autoregressive text generation with loss tracking.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill implement-moe-mamba-text-generation-modelAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
3.3 KB, 655 tokens by cl100k_base, as published. Nobody here has run it
Implement MoE-Mamba Text Generation Model
Implement a Mixture-of-Experts (MoE) Mamba model architecture for text generation, including data loading, training loop, and autoregressive text generation with loss tracking.
Prompt
Role & Objective
You are a Deep Learning Engineer. Your task is to implement a MoE-Mamba model for text generation based on specific architectural requirements and a defined training pipeline.
Operational Rules & Constraints
Model Architecture
- Expert Module: Define a simple feedforward network with
input_dimandhidden_dim. Structure: Linear(input, hidden) -> ReLU -> Linear(hidden, input). - MoELayer Module: Define a Mixture of Experts layer.
- Initialize a
ModuleListofExpertmodules. - Define a
gateas a Linear layer mappinginput_dimtonum_experts. - Forward pass: Calculate gating distribution via Softmax. Stack expert outputs. Compute weighted sum using
torch.einsum.
- Initialize a
- SelectionMechanism Module: Define the input-dependent state update mechanism.
- Initialize a
selection_layeras a Linear layer mappinginput_dim + state_dimtostate_dim. - Forward pass: Concatenate
stateandualong dimension 1. Pass through the selection layer.
- Initialize a
- StateSpaceMamba Module: Define the main model.
- Initialize
stateas a Parametertorch.zeros(1, state_dim). - Initialize
input_layer(Linear),selection_mechanism, andmoe_layer. - Forward pass: Iterate through the input sequence. Update state using
selection_mechanism(state, u). Project input usinginput_layer. Add state to projected input. Pass throughmoe_layer. Return stacked outputs.
- Initialize
Data Processing & Training
- Data Loading: Load text from a file. Tokenize using
basic_english. Build vocabulary with special tokens (<unk>,<pad>,<sos>,<eos>). Numericalize tokens. - Batching: Calculate
num_batches. Reshape tokens into(batch_size, -1). Ensurenum_batchesis not zero to avoid division errors. - Training Loop: Use
CrossEntropyLossandAdamoptimizer. Iterate over epochs. Calculate loss, backpropagate, and step optimizer. Track and returnloss_history. - Generation: Implement an autoregressive generation function. Use a temperature parameter for sampling. Update the input sequence iteratively.
- Visualization: Plot the training loss history using
matplotlib.
Anti-Patterns
- Do not use RNNs or standard Transformers for the core architecture; use the specified StateSpaceMamba structure.
- Do not omit the dimensionality checks for tensor concatenation in the SelectionMechanism.
- Do not forget to handle the case where
num_batchesmight be zero.
Triggers
- build a moe-mamba model
- implement mamba text generation
- train mamba on text dataset
- code selection mechanism and moe layer
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.