Skip to content

SFT: keep the router aux loss off for single-expert configs (Jamba) - #15010

Merged
danielhanchen merged 1 commit into
mainfrom
dh/s3-fsdp2-sweep-fix
Oct 23, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/s3-fsdp2-sweep-fix

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Single-expert Jamba configs (for example ai21labs/AI21-Jamba-Reasoning-3B, num_experts = 1) crashed at the first SFT step with IndexError on one GPU, DDP and FSDP2 alike: TRL >= 1.7 turns on the router aux loss whenever output_router_logits is set, but Jamba builds a plain MLP for a single expert, returns no router logits, and load_balancing_loss_func indexed an empty tuple.

The existing guard already turns the aux loss off for dense configs whose expert counts are all 0 (granite-4.0-h-350m). It now also treats a count of 1 as dense: a single expert has no routing choice, so its aux loss is a constant with no gradient. Real MoE configs (2 or more experts) keep their aux loss.

Tests

  • tests/version_compat/test_dense_router_aux_loss_off.py gains a single-expert case: it fails on main (1 of 8) and passes here (8 of 8).
  • Kaggle T4x2, tiny Jamba copy of the 3B config, fp16: trains on 1 GPU and under native FSDP2 in LoRA, QLoRA and full finetuning; main raises IndexError on every arm.

…mba)

AI21-Jamba-Reasoning-3B has num_experts = 1, so every layer is a plain JambaMLP and the
model returns no router logits. TRL >= 1.7 still enables the aux loss from
output_router_logits, and load_balancing_loss_func indexes the empty tuple at the first
step (IndexError). Treat an expert count of 1 like 0.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T17:47:47.917444Z cd83878 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: cd83878278

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Oct 23, 2026
…#15018)

`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[8]`
fails on `main`, for example in `Repo tests (CPU, rest)` on #15009 and
#15010:

```
studio/backend/tests/test_pretokenized_dataset.py:313: read_text()
```

#14884 added a read of `core/training/trainer.py` with the platform
default encoding. This passes `encoding = "utf-8"`.

## Verification

- On `main`: `-k checked_in_file_reads` gives 1 failed, 15 passed.
- On this branch: 16 passed.
- `studio/backend/tests/test_pretokenized_dataset.py`: 56 passed.
@danielhanchen
danielhanchen merged commit f48b996 into main Oct 23, 2026
35 of 37 checks passed
@danielhanchen
danielhanchen deleted the dh/s3-fsdp2-sweep-fix branch October 23, 2026 20:41
daruoktab pushed a commit to daruoktab/unsloth-zoo that referenced this pull request Oct 24, 2026
…Jamba / Zamba mamba mixers) (unslothai#2046)

## Problem

Training Jamba through Unsloth with the compiler on fails before the
first step whenever the mamba fast kernels are missing (the common case:
no `mamba_ssm` / `causal_conv1d`, or no `kernels` hub entry):

```
RuntimeError: Direct module loading failed for unsloth_compiled_module_jamba: name '_mixer' is not defined
```

`_force_slow_path_on_raising_mamba_mixers` in
`temporary_patches/misc.py` replaces `JambaMambaMixer.forward` and
`ZambaMambaMixer.forward` with

```python
def _slow_only(self, *a, __orig = _mixer.forward, **kw):
```

`create_standalone_class` reads the class attribute, sees a function not
named `forward`, treats it as a renamed temporary-patch forward and
emits it verbatim at module scope. The default expression
`_mixer.forward` names a local of the patch, so the generated module
raises `NameError` at import. `_unwrap_undecorated_method` could not
recover the real method because it only walked `__wrapped__` and closure
cells, and this wrapper binds the original through a keyword-only
default. `UNSLOTH_COMPILE_DISABLE=1` trains, which is how it was
spotted.

## Change

`_unwrap_undecorated_method` in `unsloth_zoo/compiler.py` also walks a
single function-valued default (positional or keyword-only), using the
same single-candidate rule as closure cells. Defaults are consulted only
when no closure cell holds a function, so every wrapper the closure walk
already resolved keeps its answer. The compiler now emits the real
`JambaMambaMixer.forward` (on transformers 5.x, the one behind
`@force_accelerate_hooks`), exactly as when the slow-path patch is not
active.

The slow-path guard is not lost: the patch runs again after compilation
and wraps the compiled class, so `use_fast_kernels` is still cleared at
forward time.

## Evidence

Tiny random Jamba (hidden 128, 2 layers, AI21-Jamba-Reasoning-3B
tokenizer, random weights), `FastLanguageModel` LoRA r=8, 2 SFT steps,
transformers 5.17.0, TRL 1.15.0, same host and seed for both arms.
BEFORE is `origin/main` 0f53c4d.

| arm | num_experts | compiler | result |
|---|---|---|---|
| base | 4 | on | `NameError: name '_mixer' is not defined` |
| head | 4 | on | trains, losses 11.0428, 11.0056 |
| base | 1 | on | `NameError: name '_mixer' is not defined` |
| head | 1 | on | trains, losses 11.0710, 11.0419 |
| head | 1 | off (`UNSLOTH_COMPILE_DISABLE=1`) | trains, losses 11.0711,
11.0424 |

The `num_experts = 1` rows use the Unsloth side fix for the
single-expert router aux loss (unslothai/unsloth#15010); without it both
arms stop later at `load_balancing_loss_func`, which is unrelated to
this change. On head the generated `unsloth_compiled_module_jamba.py`
has no `_mixer` or `_slow_only`, the mixer forward is
`JambaMambaMixer_forward`, and the live mixer class is the compiled one
with the slow-path guard re-applied.

Generator scan: `create_standalone_class` on every `nn.Module` class
with its own `forward` in jamba, zamba, zamba2, falcon_h1,
granitemoehybrid, nemotron_h, bamba, mamba, mamba2 and falcon_mamba,
after the slow-path patch, base vs head. On transformers 5.17.0 (89
classes) and 4.57.6 (79 classes, no nemotron_h there) the only outputs
that change are `JambaMambaMixer` and `ZambaMambaMixer`, which go from
containing `_mixer.forward` to self-contained. Every other class
generates byte-identical source.

## Tests

New `tests/test_compiler_default_bound_forward_wrapper.py` (CPU, no
GPU):

- runs the real `_force_slow_path_on_raising_mamba_mixers` (extracted by
AST) on fake Jamba and Zamba mixers, with a plain forward and with a
bare-closure decorated forward like `@force_accelerate_hooks`; the
generated class must compile, contain the real body and no `_mixer` /
`_slow_only` / `__orig`, match eager output, and still get the guard
when the patch re-runs on it;
- the same on the installed transformers' `JambaMambaMixer` and
`ZambaMambaMixer`;
- negative controls: ambiguous defaults stay untouched, a closure keeps
precedence over defaults, and a self-contained renamed temporary patch
still ships as is.

Base: 7 failed, 3 passed (the 3 negative controls). Head: 10 passed on
transformers 5.17.0 and on 4.57.6, together with
`test_compiler_decorated_forward.py`,
`test_compiler_inherited_forward_alias.py` and
`test_mamba_ssm_pre_ampere_fallback.py` (122 passed on both).

## Not covered

- A real Jamba or Zamba checkpoint; only tiny random weights were
trained.
- Zamba end to end training; Zamba was checked at the generator level
only.
- A host with mamba fast kernels installed (there the slow-path patch
does not fire and this path is not reached).
- transformers versions between 4.57.6 and 5.17.0 were not swept.
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