Skip to content

Prevent FP16 overflow in grouped multi-LoRA expert projections - #16414

Merged
danielhanchen merged 8 commits into
mainfrom
dh/multi-lora-scale-overflow
Nov 3, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
dh/multi-lora-scale-overflow

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Grouped multi-adapter expert projections can overflow FP16 when LoRA amplification is folded into the second factor, even when the final projection is finite. Keep the original scaled-factor calculation whenever it fits, and use a separate output scale only for the affected expert groups.

This also preserves attenuation and finite gradients for large scales with small factors. Factor preparation uses FP32 scale arithmetic, preserves masks needed by retained backward, and is compiled once on supported NVIDIA configurations to avoid adding repeated eager launch overhead. Accumulation uses native addcmul_. CPU, Windows and ROCm retain eager preparation, and UNSLOTH_MULTI_LORA_COMPILE_STACKS=0 disables the preparation compiler.

Validation:

  • Twelve numerical cases cover both factor orientations, positive/negative attenuation, amplification, unequal adapter scales and a no-adapter row. Six fail on the merge base and pass here; six preserve existing behavior. Together with cold fullgraph checks, 16 local tests pass.
  • Actual grouped FP16/BF16 forward and backward match an independent reference on Colab L4. Twenty distinct training batches match baseline A/A exactly in losses, gradients and final parameters. Fullgraph forward/backward and retained backward pass; graph count stays at two across 32 layer instances.
  • Torch 2.6 and 2.15 GPU checks pass. The floor compiler's differences are at most one FP16 spacing and are closer to the independent FP64 reference; latest matches eager exactly.
  • Linux, macOS and Windows each run all 12 scoped cases successfully.
  • Windows ROCm on Radeon 8060S: CONFIRMED. Base has six failures/six passes; corrected head and integration candidate pass all twelve cases.
  • Linux ROCm on Radeon 8060S: CONFIRMED on the exact current-head source, verified by Git blob and SHA-256 hashes. Base has six failures/six passes; current head passes all twelve cases. Shared GPU correctness evidence only.
  • Current switchboard: functional NO_REGRESSION for regression smoke, SFT, inference smoke, auth, settings and crawl. The only subsequent commit changes no files. Catalog/crawl differences remain in unchanged UI code; the hub pixel difference disappears on confirmation. UI impact is NO.

Exclusive T4 measurements use the same card, fresh processes/caches and three ABBA rounds. These measure the complete grouped-expert path, not full-model token throughput:

Routed rows Base median Corrected median Base run spread Base / corrected peak allocated
1 2.803 ms 2.837 ms 0.072 ms 27.76 / 24.96 MiB
256 3.669 ms 3.640 ms 0.209 ms 29.77 / 27.99 MiB

Steady latency is within baseline noise; no speedup is claimed. Paired bootstrap 95% intervals for the latency delta span zero at both sizes. Preparation compilation adds about 4.7 seconds on a cold cache, measured separately from steady execution. No model weights were needed for the targeted numerical probes.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 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-11-03T07:13:30.034004Z 16f860a 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.

danielhanchen added a commit that referenced this pull request Nov 1, 2026
…, not at collection (#16430)

## Summary

`(Python 3.13, g-r)` fails on several unrelated PRs (#16413, #16414,
#16415):

```
test_hub_source_env_reset_between_tests.py::test_the_next_test_starts_on_the_source_it_inherited
E   AssertionError: assert 'huggingface' == None
E    +  where 'huggingface' = get('UNSLOTH_STUDIO_HUB_SOURCE')
```

The test reads `_INHERITED = os.environ.get(SOURCE_ENV)` at module
import, during collection. Collection goes on after that. Any module
collected later that imports the app (for example
`tests/test_liveness_reports_*.py` with a module-level `from main import
...`) runs `apply_hub_settings()`, and that always writes
`UNSLOTH_STUDIO_HUB_SOURCE`. The conftest fixture
`_restore_hub_settings_env` then correctly restores the value each test
started with (`huggingface`), but the test compares it against the stale
collection-time value (`None`). The fixture is right. The test's
baseline is taken too early, and whether it fails depends on which files
share the shard. #16236 made it compare against the inherited source,
which made it sensitive to this.

## Change

The first test records the source env and the "other" source at the
moment it starts. That is exactly what the fixture saves and restores.
The second test compares against those, and fails with a clear message
if it runs without the first one. Nothing else changes.

## Testing

- **Repro of the CI failure:** run this file plus a later-collected
stand-in module that sets `UNSLOTH_STUDIO_HUB_SOURCE=huggingface` at
import, the way the app import does.
  - main: fails with the same `assert 'huggingface' == None`;
  - this branch: 3 passed.
- This file alone: 2 passed. With `UNSLOTH_STUDIO_HUB_SOURCE=modelscope`
in the environment (the case #16236 was for): 2 passed.
- Sabotage: with the conftest restore loop disabled, the second test
fails again. The test still catches a missing restore.
@danielhanchen danielhanchen self-assigned this Nov 3, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 33e93807f1

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 4d22d3df5c

ℹ️ 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".

@unslothai unslothai deleted a comment from danielhanchen Nov 3, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: e3c365454d

ℹ️ 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".

@unslothai unslothai deleted a comment from danielhanchen Nov 3, 2026
@unslothai unslothai deleted a comment from danielhanchen Nov 3, 2026
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Nov 3, 2026
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Nov 3, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: f1a979f99d

ℹ️ 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".

@unslothai unslothai deleted a comment from danielhanchen Nov 3, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 16f860a92a

ℹ️ 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 danielhanchen changed the title Multi-LoRA grouped experts: apply the LoRA scale after the second GEMM Prevent FP16 overflow in grouped multi-LoRA expert projections Nov 3, 2026
@danielhanchen
danielhanchen merged commit 9a8b229 into main Nov 3, 2026
@danielhanchen
danielhanchen deleted the dh/multi-lora-scale-overflow branch November 3, 2026 08:16
danielhanchen added a commit that referenced this pull request Nov 3, 2026
## Summary

Since #16414 (`9a8b229714`), `Repo tests (CPU, rest-2)` has been failing
on unrelated PRs. It reproduces on current main, and it hit #16540 and
#16509 in runs created after #16531 merged:

```
tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[2]
E   AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding, so they break on Windows as soon as that file gains a non-ASCII byte. Pass encoding = "utf-8": ['tests/test_multi_lora_scale_overflow_cpu.py:20: read_text()']
```

The new test from #16414 parses `unsloth/kernels/multi_lora.py` with a
bare `read_text()`. That is the same pattern #16531 fixed in
`test_multi_lora_cold_compile_cpu.py`. The guard is right: on Windows, a
bare read uses the locale code page, so this test would break as soon as
the kernel file gains a non-ASCII character. The kernel change in #16414
is fine.

## Change

`tests/test_multi_lora_scale_overflow_cpu.py` line 20: `read_text()`
becomes `read_text(encoding = "utf-8")`.

## Testing

- `tests/test_source_read_encoding.py`:
  - main: 1 failed, 15 passed (the case above);
  - this branch: passes.
- This branch, `tests/test_source_read_encoding.py` plus
`tests/test_multi_lora_scale_overflow_cpu.py`: 28 passed.
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