Repository navigation
Studio: size FLUX.2-klein 9B LoRA training from its own weights, not the 4B family number - #8263
Conversation
…B family number base_precision="auto" is what /info recommends on any bf16-capable GPU, so it is the Train tab's default. It sizes the dense load from _FamilySpec.dense_bf16_gb, which is one number per family, and flux.2-klein carries two transformer sizes: the 4B default (8.1 GB) and the 9B / base-9B pair (18.2 GB measured from the checkpoint index). A 9B run was therefore planned as a 4B, so on a 16-24 GB GPU "auto" resolved to bf16 and the dense load OOMed before the first step; the int8 band has the same problem, since int8 still materialises the full bf16 transformer before quantize_ shrinks it. Read the size from the per-base overrides the inference auto-policy already keeps, rather than adding a second table that can drift, and fall back to the family number for every single-size family. The lookup never raises: a sizing miss must not fail a run that would otherwise train. The override table itself only listed FLUX.2-klein-9B, so klein-base-9B (the undistilled 9B, and the variant upstream points fine-tuning at) was sized as a 4B on the inference side too. Add it. Tests: 9B ids of both variants and both mirrors resolve auto to nf4 at 20 GB free while the 4B default still reaches bf16, every single-size family keeps its existing number, and a broken lookup falls back instead of raising.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 61c4a3408c
ℹ️ 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".
| components = family_bf16_components_gb(fam, base_model) if fam is not None else None | ||
| if components: | ||
| return float(components[0]) |
There was a problem hiding this comment.
Preserve the training size when no base override exists
When base_model has no exact per-base override—including the normal FLUX.2-klein 4B base—family_bf16_components_gb falls through to the family table rather than returning None, so this now replaces the training spec's 8.1 GB estimate with 7.8 GB. On a GPU reporting roughly 11.7–12.15 GB free, that smaller value crosses the dense_gb * 1.5 threshold and changes auto from int8 to bf16, removing the intended training headroom and potentially causing an OOM. Use the family spec unless the canonical base matched an explicit per-base override.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed and fixed in 685bbde. family_bf16_components_gb falls through to the family table on a per-base miss, so every base without an override was being re-sized by the shared table: klein's own 4B default went from this spec's 8.1 GB to the table's 7.8, which moves the bf16 band edge from 12.15 GB to 11.70 and hands a 12 GB card a dense load. The lookup now goes through a new base_repo_bf16_components_gb, which returns the override or None and never the family entry underneath it, so a base the table does not name keeps spec.dense_bf16_gb exactly. Added a test pinning the 4B band edge at 12 GB free (int8, not bf16) and one asserting equality, not approximate equality, for every base without an override.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
What this fixes
base_precision="auto"is what/training/diffusion/inforecommends on any bf16-capable GPU, and the Train tab seeds its selector from that, so it is the default a user gets. It picks the dense mode from_FamilySpec.dense_bf16_gb, which is one number per family.flux.2-kleinis a family with two transformer sizes under a single entry:(18.16 GB read from
transformer/diffusion_pytorch_model.safetensors.index.jsononunsloth/FLUX.2-klein-base-9B; the encoder index reports 16.38 GB.)So a 9B run was planned as a 4B. The policy bands are
free > dense * 1.5 -> bf16andfree > dense * 1.15 -> int8, evaluated against 8.1 GB:On a 16-24 GB card the default therefore starts an 18.2 GB dense load and OOMs before step 1. The int8 band is no safer: int8 still materialises the full bf16 transformer before
quantize_shrinks it.The change
_dense_bf16_gb(spec, base_model)resolves the size of the base the run actually trains from. It reads the per-base overrides the inference auto-policy already keeps, rather than adding a second table that can drift out of sync, and falls back to the family number for every base without an override (which is every single-size family). It never raises: a sizing lookup must not be able to fail a run that would otherwise train._BASE_REPO_BF16_GBlisted onlyFLUX.2-klein-9B, soFLUX.2-klein-base-9Bwas sized as a 4B on the inference side too. That is the undistilled 9B, and the variant upstream (BFL, ai-toolkit) points fine-tuning at, so it is the likelier of the two to be loaded. Added.No behaviour change for any other family: the shared table and the specs already carry the same figures, which a test now pins.
Regression tests
tests/test_diffusion_base_precision.pytest_auto_sizes_flux2_klein_9b_off_its_own_weights(parametrised overblack-forest-labs/FLUX.2-klein-9B,-base-9Band bothunsloth/mirrors): at 20 GB free,automust resolve tonf4. Fails on main withbf16.test_auto_still_picks_bf16_for_the_klein_4b_default: the same 20 GB against the 4B default still reachesbf16, so the narrowing applies only to the variant it has a size for.test_dense_bf16_gb_keeps_every_single_size_family_where_it_wasandtest_dense_bf16_gb_survives_a_broken_lookup.tests/test_diffusion_auto_policy.pytest_klein_base_9b_is_sized_like_the_9b_not_the_4b, including theunsloth/mirror throughcanonical_base.7 of these fail on main, all pass with the change. Full CPU suite for the area:
pytest tests/ -k "diffusion or training or vram"gives 3092 passed, 3 skipped.What was checked but is NOT broken
The report that prompted this also said the trained adapter has no effect at inference. That does not reproduce. A full run of the prebuilt "Dog (DreamBooth subject)" recipe on
unsloth/FLUX.2-klein-base-9B(5 images, 512px, rank 16, lr 1e-4, 500 steps, nf4 base) attaches 56 LoRA modules for 19,922,944 trainable parameters, which is exactly the expected 8 double blocks x (to_q, to_k, to_v, to_out.0) plus 24 single blocks x to_qkv_mlp_proj for the 9B; loss moves from a 0.70 mean over the first 50 steps to 0.59 over the last 50; and the exported adapter reloads intoFlux2KleinPipelinewith all 56 layers found. Same seed, same prompt, LoRA off vs on: MAE 75.8/255 and RMSE 91.8 on "a photo of sks dog in a bucket", MAE 60.4 on "a photo of sks dog", and the subject in the LoRA-on images is visibly the corgi from the dataset. The target-module list and the adapter key namespace are both correct for this transformer.