detrium.optim.muon
MuAdamW: Hybrid Matrix-Orthogonalized (Muon) + Adaptive (AdamW) Optimizer for DEIM / D-FINE / RT-DETRv2 / RT-DETRv4.
Adapts YOLO26 MuSGD & Moonshot Kimi-Muon principles to heterogeneous CNN/DETR architectures:
- 5-step quintic Newton-Schulz orthogonalization in bfloat16 Tensor Cores (batched by matrix shape).
- RMS-aligned rectangular scaling (ns_scale * sqrt(max(m, n)), default ns_scale=0.2) so Muon updates
match AdamW element-wise RMS step size (~0.2 * lr), allowing direct reuse of DEIM lr & weight_decay.
- Deterministic structural filtering protecting 1D norms/biases/affine scales, depthwise & grouped
convolutions (groups > 1), input stems, discrete embeddings, channel gates (EseModule / EffectiveSELayer /
Gateway), deformable attention offsets/weights, distillation projectors, and detection output heads.
- Supports both pure Muon on eligible matrices (Phase 1, muon_ratio=1.0, 50% state memory reduction)
and MuSGD-inspired dual-cue convex blending (Phase 2, 0 < muon_ratio < 1.0).
导出符号 · MuAdamW、zeropower_via_newtonschulz5_batched、is_muon_eligible
class MuAdamW(optim.Optimizer)
Hybrid Muon + AdamW optimizer with RMS step-size alignment and MuSGD-style update blending.
| 名称 | 类型 | 默认值 |
|---|
params | Iterable | — |
lr | float | 0.0005 |
betas | Tuple[float, float] | (0.9, 0.999) |
eps | float | 1e-08 |
weight_decay | float | 0.000125 |
momentum | float | 0.95 |
nesterov | bool | True |
ns_steps | int | 5 |
ns_scale | float | 0.2 |
muon_ratio | float | 1.0 |
include_backbone | bool | False |
min_dim | int | 16 |
exclude_patterns | Optional[Sequence[str]] | None |
step(self, closure=None)
is_muon_eligible(name: str, param: torch.Tensor, include_backbone: bool=False, min_dim: int=16, exclude_patterns: Sequence[str]=DEFAULT_EXCLUDE_PATTERNS, conv_groups: int=1, is_channel_gate: bool=False) -> Tuple[bool, str]
Deterministically classify whether a parameter tensor should receive Muon orthogonalized updates.
| 名称 | 类型 | 默认值 |
|---|
name | str | — |
param | torch.Tensor | — |
include_backbone | bool | False |
min_dim | int | 16 |
exclude_patterns | Sequence[str] | DEFAULT_EXCLUDE_PATTERNS |
conv_groups | int | 1 |
is_channel_gate | bool | False |
zeropower_via_newtonschulz5_batched(G: torch.Tensor, steps: int=5, eps: float=1e-12) -> torch.Tensor
Batched 5-step quintic Newton-Schulz iteration approximating the semi-orthogonal polar factor
of a batch of 2D matrices G with shape (B, m, n).
| 名称 | 类型 | 默认值 |
|---|
G | torch.Tensor | — |
steps | int | 5 |
eps | float | 1e-12 |