Skip to content

Contributing

TL;DR

Issues, bug fixes, and new adapter integrations are welcome. Please open a GitHub issue first for major changes so we can discuss scope before you invest time writing code. All distributed changes must pass the bit-identical correctness test (test_hsdp_correctness.py).


Dev setup

# Clone and install in editable mode
git clone https://github.com/X-Square-Robot/dmuon
cd dmuon

# Create environment (conda or venv)
conda create -n dmuon-dev python=3.11 && conda activate dmuon-dev

# Install PyTorch (adjust CUDA version as needed)
pip install "torch>=2.6" --index-url https://download.pytorch.org/whl/cu121

# Install DMuon and dev dependencies
pip install -e ".[dev]"

The [dev] extra installs test, lint, packaging, and documentation tools, including pytest, ruff, build, mkdocs-material, and mkdocstrings[python]. Release checks that need twine can install pip install -e ".[release]".


Running tests

Unit tests

python -m pytest tests/unit/ -v

Distributed tests (requires 4 GPUs)

# Bit-identical correctness — run before every PR with distributed changes
torchrun --nproc_per_node=4 tests/distributed/test_hsdp_correctness.py

Distributed tests take ~2 minutes per run. Write a small isolated unit test first to verify your logic before using the distributed harness as a debug loop (see the project memory note on smoke tests).

Native Ascend distributed tests (requires 4 NPUs)

Use a matching PyTorch, torch_npu, and CANN environment and select four visible devices. The accelerated 950DT path also requires unmodified TileLang v0.1.15 built with USE_ASCEND=ON. Pass compute_backend="torch" to dmuon.Muon to validate the reference arithmetic without TileLang. The same mandatory entry provides a native NPU/HCCL gate:

source /usr/local/Ascend/ascend-toolkit/set_env.sh
ASCEND_RT_VISIBLE_DEVICES=0,1,2,3 torchrun --standalone --nproc_per_node=4 \
    tests/distributed/test_hsdp_correctness.py --device npu --compute-backend tilelang \
    --output-dir /path/to/new-hsdp-proof

This compares the upstream model and optimizer configuration across a 1D shard-only mesh and a 2x2 HSDP mesh, using identical initialization and inputs. Every rank checks the exact loss, full model state, and optimizer state after each update. HSDP saves one canonical rank-0 checkpoint and checks two further updates after rebuilding and restoring that checkpoint. No tolerance or rank-local state override is used. Four steps per topology plus two replay steps provide a ten-call smoke check with --steps 4 --resume-after 2. The default gate uses ten steps per topology and two replay steps, for 22 distributed optimizer calls. The CUDA command above remains unchanged.

Single-test smoke check pattern

# Minimal isolated test — write this BEFORE modifying distributed logic
import torch
# ... 10-line setup without torchrun ...

Coding style

  • Formatter: ruff format (configured in pyproject.toml)
  • Linter: ruff check (configured in pyproject.toml)
  • Type hints: required on all public functions and class methods
  • Docstrings: NumPy style (configured in mkdocstrings); concise — one line summary, Args, Returns
  • No emojis in code, docstrings, or comments

Run style checks:

ruff format dmuon/ examples/
ruff check dmuon/ examples/

Run both before sending changes for review.


PR checklist

Before opening a pull request:

  • [ ] Rebase on main (git fetch origin && git rebase origin/main)
  • [ ] Unit tests pass (pytest tests/unit/)
  • [ ] For distributed changes: test_hsdp_correctness.py passes
  • [ ] For native NPU changes: the --device npu mandatory gate passes on four NPUs
  • [ ] Public API changes have updated docstrings
  • [ ] Docs updated if behavior or API surface changed
  • [ ] Bit-identical correctness maintained — no change to loss values for the same random seed across 1D shard-only, HSDP, and checkpoint-resume runs

For new features, open an issue first to align on scope.


Architecture orientation

Read Design / Architecture before touching the core ownership machinery (dmuon/api.py, dmuon/_core/, dmuon/_backends/). The key invariants are:

  • DedicatedParam._owned_data is the single source of truth for the parameter value on the owner rank
  • Hook registration order (pre-forward → post-forward → pre-backward → post-backward) must match FSDP2's hook ordering
  • Every async collective must have a matching drain path (either in the next forward's _pre_forward_wait or in wait_all_replicate_broadcasts)

Release process

DMuon follows semantic versioning: MAJOR.MINOR.PATCH.

  • Patch: bug fixes, docstring/docs updates, no API change
  • Minor: new public API symbols, new backends, new guide pages
  • Major: breaking API changes (rare; require discussion)

Documentation is deployed automatically to GitHub Pages on every push to main via GitHub Actions.


Community

Please search existing issues before opening a new one.


See also