Enhanced Mixture of Experts implementation with optimized Straight-Through Estimator router
Project description
DenseMixer
DenseMixer enhances Mixture of Experts (MoE) models in HuggingFace Transformers with more precise router gradients for improved post-training.
- 📖 Blog & Theory
- 🧪 Reproducible experiments: see experiments folder
Supported Models
Installation
Basic Installation
pip install densemixer
Auto-Patching Setup (Recommended)
To enable DenseMixer automatically for all Python scripts (without needing to add any import):
densemixer setup
export DENSEMIXER_ENABLED=1
This will append the necessary auto-import logic to your usercustomize.py in your user site-packages (if not already present). Any Python process with DENSEMIXER_ENABLED=1 will auto-load DenseMixer and patch transformers models.
Note: DenseMixer is disabled by default for safety. You must explicitly set DENSEMIXER_ENABLED=1 to enable it.
To disable, either unset the environment variable or manually remove the relevant lines from your usercustomize.py in your user site-packages.
Usage
Basic Usage
from transformers import Qwen3MoeForCausalLM
model = Qwen3MoeForCausalLM.from_pretrained("Qwen/Qwen3-MoE-30B-A3B")
When DenseMixer patches are applied, you'll see messages like:
INFO - densemixer - DenseMixer: Using custom forward method for Qwen3-MoE
INFO - densemixer - DenseMixer: Using custom forward method for Qwen2-MoE
INFO - densemixer - DenseMixer: Using custom forward method for OLMoE
Configuration
DenseMixer can be controlled with environment variables:
| Variable | Default | Description |
|---|---|---|
DENSEMIXER_ENABLED |
0 |
Master switch to enable/disable DenseMixer |
DENSEMIXER_QWEN3 |
1 |
Enable for Qwen3-MoE models (when DENSEMIXER_ENABLED=1) |
DENSEMIXER_QWEN2 |
1 |
Enable for Qwen2-MoE models (when DENSEMIXER_ENABLED=1) |
DENSEMIXER_OLMOE |
1 |
Enable for OLMoE models (when DENSEMIXER_ENABLED=1) |
Examples
Enable DenseMixer for all models:
export DENSEMIXER_ENABLED=1
python your_script.py
Enable only for Qwen3-MoE:
export DENSEMIXER_ENABLED=1
export DENSEMIXER_QWEN3=1
export DENSEMIXER_QWEN2=0
export DENSEMIXER_OLMOE=0
python your_script.py
Disable DenseMixer completely (default behavior):
# No environment variables needed - DenseMixer is disabled by default
python your_script.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file densemixer-1.0.0.tar.gz.
File metadata
- Download URL: densemixer-1.0.0.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d5009b5b86a3f92ffbfee1d40fdc03f35d6730d27859aee73af45ad1d96ce6a
|
|
| MD5 |
bfa4caba21feaa1c0d560737b6d1cc2f
|
|
| BLAKE2b-256 |
2286c0ce44cd4339ccf1de1043eb5369abbcdf7852466cadb91d4eea592a8403
|
File details
Details for the file densemixer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: densemixer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6862b3ce53c0ff3bce793279f732d5536f015b06b107be0f39770e37ba814815
|
|
| MD5 |
e0798540e3dc5c32a79b48a84a84b19f
|
|
| BLAKE2b-256 |
664a6fc96fdfd8e0d697ba2de01258bc1eee1049c76d17ab54af80cefc7cb9d6
|