Skip to content

Studio: size FLUX.2-klein 9B LoRA training from its own weights, not the 4B family number - #8263

Merged
danielhanchen merged 4 commits into
mainfrom
klein9b-dense-size
Aug 9, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
klein9b-dense-size

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What this fixes

base_precision="auto" is what /training/diffusion/info recommends 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-klein is a family with two transformer sizes under a single entry:

checkpoint transformer bf16 text encoder
FLUX.2-klein-4B / base-4B 8.1 GB Qwen3-4B, 8.0 GB
FLUX.2-klein-9B / base-9B 18.2 GB Qwen3-8B, 16.4 GB

(18.16 GB read from transformer/diffusion_pytorch_model.safetensors.index.json on unsloth/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 -> bf16 and free > dense * 1.15 -> int8, evaluated against 8.1 GB:

free= 12.0 GB -> auto picks int8  | with the true 9B size it would pick nf4
free= 16.0 GB -> auto picks bf16  | with the true 9B size it would pick nf4
free= 20.0 GB -> auto picks bf16  | with the true 9B size it would pick nf4
free= 24.0 GB -> auto picks bf16  | with the true 9B size it would pick int8

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_GB listed only FLUX.2-klein-9B, so FLUX.2-klein-base-9B was 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.py

  • test_auto_sizes_flux2_klein_9b_off_its_own_weights (parametrised over black-forest-labs/FLUX.2-klein-9B, -base-9B and both unsloth/ mirrors): at 20 GB free, auto must resolve to nf4. Fails on main with bf16.
  • test_auto_still_picks_bf16_for_the_klein_4b_default: the same 20 GB against the 4B default still reaches bf16, so the narrowing applies only to the variant it has a size for.
  • test_dense_bf16_gb_keeps_every_single_size_family_where_it_was and test_dense_bf16_gb_survives_a_broken_lookup.

tests/test_diffusion_auto_policy.py

  • test_klein_base_9b_is_sized_like_the_9b_not_the_4b, including the unsloth/ mirror through canonical_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 into Flux2KleinPipeline with 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.

danielhanchen and others added 2 commits August 9, 2026 14:15
…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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +499 to +501
components = family_bf16_components_gb(fam, base_model) if fam is not None else None
if components:
return float(components[0])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 7a41f150cd

ℹ️ 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 merged commit 5a8e4ef into main Aug 9, 2026
40 of 44 checks passed
@danielhanchen
danielhanchen deleted the klein9b-dense-size branch August 9, 2026 15:41
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