Skip to content

Add GPT-OSS-20B model support#1195

Open
CarlG0123 wants to merge 6 commits intoTransformerLensOrg:devfrom
CarlG0123:feat/gpt-oss-support
Open

Add GPT-OSS-20B model support#1195
CarlG0123 wants to merge 6 commits intoTransformerLensOrg:devfrom
CarlG0123:feat/gpt-oss-support

Conversation

@CarlG0123
Copy link

@CarlG0123 CarlG0123 commented Mar 7, 2026

Summary

  • Adds support for OpenAI's GPT-OSS-20B, a 20B-parameter MoE model with 32 experts
  • Implements custom MoE architecture (GPT-OSS-specific activation, router with bias, softmax-after-top-k routing)
  • Includes a direct safetensors loader that dequantizes MXFP4 weights on CPU, enabling the model to run on consumer hardware (tested on MacBook Pro with 38.7GB RAM)

Key architecture differences from Mixtral

  • Custom GLU activation: gate.clamp(max=7) * sigmoid(gate * 1.702) * (up.clamp(-7, 7) + 1) (not standard SiLU)
  • Router has bias and applies softmax after top-k selection (Mixtral applies before)
  • Expert projections have biases (Mixtral does not)
  • Merged expert weights with interleaved gate/up columns

Files changed

File Description
transformer_lens/components/mlps/gpt_oss_moe.py GptOssExpert and GptOssMoE classes
transformer_lens/pretrained/weight_conversions/openai.py HF model → TransformerLens weight converter
transformer_lens/loading_from_pretrained.py Model registration, config extraction, weight dispatch
transformer_lens/factories/mlp_factory.py Factory routing for GptOssForCausalLM
transformer_lens/pretrained/weight_conversions/__init__.py Export convert_gpt_oss_weights
run_gpt_oss.py Standalone loader using direct safetensors + MXFP4 dequantization

Usage

# Via TransformerLens from_pretrained (requires ~80GB RAM for HF model path)
model = HookedTransformer.from_pretrained("openai/gpt-oss-20b")

# Via direct safetensors loader (works on <40GB RAM machines)
python run_gpt_oss.py              # all 24 layers
python run_gpt_oss.py --layers 12  # fewer layers for less memory

Test plan

  • 1-layer model: forward pass produces valid logits (no NaN)
  • 3-layer model: loads and runs inference correctly
  • Full 24-layer model: loads all layers (~10 min), runs inference on MacBook Pro (38.7GB RAM)
  • Imports don't break existing models (tested GPT-2 config loading)
  • CI tests pass (no existing tests modified)

🤖 Generated with Claude Code

Carl Gross and others added 2 commits March 6, 2026 19:22
Adds support for loading and running OpenAI's GPT-OSS-20B model, which uses
a unique MoE architecture with 32 experts, custom GLU activation, and MXFP4
quantized weights on HuggingFace.

New files:
- gpt_oss_moe.py: GptOssExpert (custom activation) and GptOssMoE (routing)
- openai.py: Weight converter for HF model -> TransformerLens state dict
- run_gpt_oss.py: Direct safetensors loader that dequantizes MXFP4 on CPU,
  enabling the model to run on machines with <40GB RAM via swap

Modified files:
- loading_from_pretrained.py: Register model, config extraction, weight dispatch
- mlp_factory.py: Route GptOssForCausalLM to GptOssMoE
- weight_conversions/__init__.py: Export convert_gpt_oss_weights

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Register the new model in the incompatible_models list (too large for
Colab) and update the model count from 231 to 232.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@CarlG0123 CarlG0123 changed the base branch from main to dev March 7, 2026 03:22
@CarlG0123 CarlG0123 force-pushed the feat/gpt-oss-support branch 2 times, most recently from c12d182 to 9d72d1b Compare March 7, 2026 03:23
Carl Gross and others added 3 commits March 6, 2026 19:45
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Convert the standalone script to a Jupyter notebook to match
the existing demo format. Adds sections for inference, caching,
expert routing analysis, logit lens, attention patterns, and
activation patching.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant