Repository navigation
Prevent FP16 overflow in grouped multi-LoRA expert projections - #16414
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…, 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.
[skip ci]
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
## 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.
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, andUNSLOTH_MULTI_LORA_COMPILE_STACKS=0disables the preparation compiler.Validation:
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:
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.