Skip to content

Studio FLUX.3 manual cast: keep LoRA adapters at the fp32 compute dtype and out of the bf16 fold - #17201

Merged
danielhanchen merged 2 commits into
mainfrom
dh/flux3-manual-cast-lora-fp32
Nov 9, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/flux3-manual-cast-lora-fp32

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What happened

#17173 loads FLUX.3 on fp16-only cards (T4 and other pre-Ampere GPUs) with bf16 weight storage and float32 compute per layer: each plain Linear becomes a manual-cast twin that upcasts its weight per call. That covers the base GEMMs, but not LoRA:

  • PEFT places every injected adapter at its base layer's weight dtype (_move_adapter_to_device_of_base_layer). On a manual-cast layer that is the stored bf16, so lora_A / lora_B were bf16, PEFT cast the activation down to bf16, and both adapter GEMMs ran in bf16 on exactly the cards that have no native bf16. fp32 adapter files were also rounded through bf16 on load.
  • The LoRA fold (diffusion_lora_fuse) computes the update in fp32 and rounds the merged weight back to the weight dtype. With bf16 storage, small updates that the former fp32 storage kept are rounded away (a weight of 1.0 plus an update of 1/1024 stayed 1.0), and the layer is then marked merged so the adapter no longer runs separately.

What this changes

  • On a manual-cast base layer, adapters are placed at the layer's compute dtype (float32) instead of its storage dtype. This hooks PEFT's adapter placement, so it applies before the state dict is copied in (fp32 file values are kept exactly) and on PEFT's low_cpu_mem_usage path. Every other layer keeps PEFT's own placement.
  • The fold plan declines manual-cast layers, so those LoRAs stay unfused and exact rather than folded lossily into bf16.
  • UNSLOTH_STUDIO_MANUAL_CAST_LORA=0 restores the previous adapter placement and fold.

Base storage is unchanged: the bf16 weights stay bf16, so the host and VRAM savings from #17173 hold. The adapters add only their own fp32 bytes.

Evidence

CPU, tiny 3-layer model (bf16 storage + manual cast, PEFT 0.19.1 injection, the same path diffusers load_lora_adapter takes), tests/test_diffusion_manual_cast_lora.py:

main this PR
adapter dtype bf16 fp32
adapter values vs file (fp32) rounded exact
GEMM dtypes in forward fp32 base, bf16 adapter all fp32
output vs fp32-stored reference differs equal (1e-6)
fold on manual-cast layers folds into bf16 declined

On main, 3 of the 6 new tests fail (test_adapter_keeps_fp32_values_and_gemms[False], [True], test_fuse_declines_manual_cast_layers); on this branch all 6 pass. The kill switch test and a plain bf16 model (no manual cast) keep the previous behavior on both.

Tesla T4 (sm 7.5, torch 2.14.1, diffusers 0.42.1, PEFT 0.19.1), random tiny-config FLUX.3 transformer (hidden 1536, 2 + 4 blocks, 1024 image + 128 text tokens), rank 32 LoRA on every block Linear, loaded with load_lora_adapter. Same card and process for every arm, 3 x 10 forwards each:

arm adapter dtype GEMM dtypes max abs diff vs fp32 storage ms / forward (median)
fp32 storage (before #17173) fp32 108 fp32 0 153.1
main (manual cast) bf16 60 fp32, 48 bf16 5.3e-05 166.6 (repeat 170.2)
this PR fp32 108 fp32 0.0 (bit-equal) 165.0

The T4 renders finite output on main as well, and the timing difference sits inside the main-vs-main repeat spread, so this is a precision fix rather than a speed fix: with the change, LoRA output on manual cast is bit-equal to the fp32-stored model, and adapter memory is the only extra cost (0.88 to 0.91 GB peak here, against 1.58 GB for fp32 storage).

torch.compile(fullgraph=True) over a manual-cast model with an fp32 adapter: 0 graph breaks, 1 graph, no recompiles (same as main).

Existing tests test_diffusion_flux3_manual_cast.py, test_diffusion_lora_fuse.py, test_diffusion_lora_files.py, test_diffusion_lora.py, test_diffusion_lora_incremental_remerge.py, test_diffusion_lora_quant_merge.py: same results as main (the four moss loader failures in test_diffusion_lora_files.py / test_diffusion_lora.py fail on main too).

…pe and out of the bf16 fold

#17173 stores the FLUX.3 weights as bf16 on fp16-only cards and upcasts each
Linear per call. PEFT places every injected adapter at its base layer's weight
dtype, so a LoRA loaded onto those layers ran its A / B GEMMs in bf16 and
rounded fp32 adapter files through bf16. The LoRA fold also rounded the merged
update into the bf16 storage, dropping updates the former fp32 storage kept.

Adapters on a manual-cast base now go to its compute dtype, and the fold
declines manual-cast layers. UNSLOTH_STUDIO_MANUAL_CAST_LORA=0 restores the
previous placement and fold.
@danielhanchen danielhanchen self-assigned this Nov 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T03:13:10.910838Z 93c4b7b 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. More of your lovely PRs please.

Reviewed commit: 93c4b7be81

ℹ️ 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 Nov 9, 2026
…uff-unsloth wheel (#17211)

## Summary

The Backend CI step that installs the pinned `ruff-unsloth` wheel
retries 3 times with 5 s and 10 s gaps. Between 05:19 and 05:40 UTC
today, GitHub served intermittent 504s for the release asset, and every
attempt of an unlucky job landed inside one burst. That turned `Repo
tests (CPU, fsdp2 / gloo-cp / gloo-pp / gloo-tp / rest /
studio-install)` red on #17188, #17191, #17197, #17201 and #17202, while
sibling jobs in the same runs installed the wheel fine a minute later.

This raises the loop to 6 attempts with exponential backoff (10, 20, 40,
80, 160 s, about 5 minutes total). A healthy download still never
sleeps, and a lasting outage still fails the job with the same
`::error::` line, so the fixed-point formatter guard keeps failing
loudly in CI when the wheel is truly unavailable.

## Changes

- `.github/workflows/studio-backend-ci.yml`: 6 attempts, `sleep $((10 <<
(attempt - 1)))`.
- `tests/test_ruff_unsloth_install_retries.py`: runs the step's own
`run:` script under bash with `pip` and `sleep` faked on `PATH`, and
checks that
- 5 failed downloads in a row still install, after at least 300 s of
backoff,
- a download that never recovers still fails the job, in under 10
minutes,
  - a healthy download does not wait at all.

## Testing

- `pytest tests/test_ruff_unsloth_install_retries.py`: 3 passed.
- Against the old step from `main`, the burst test fails with
`ruff-unsloth install failed after 3 attempts`.
- The other tests that read `studio-backend-ci.yml` pass.
@danielhanchen
danielhanchen merged commit 1185624 into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the dh/flux3-manual-cast-lora-fp32 branch November 9, 2026 11:54
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