Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(flux2): read the Klein Base claim as a word, not a substring
Alexander Eichhorn2da10f013c4d
diff --git a/invokeai/backend/model_manager/configs/main.py b/invokeai/backend/model_manager/configs/main.pyindex 660c10523e..34ad1b758b 100644--- a/invokeai/backend/model_manager/configs/main.py+++ b/invokeai/backend/model_manager/configs/main.py@@ -405,14 +405,37 @@ def _has_flux2_diffusers_transformer_keys(state_dict: dict[str | int, Any]) -> breturn False-def _filename_suggests_base(name: str) -> bool:- """Check if a model name/filename suggests it is a Base (undistilled) variant.+# Letters, not substrings: `_filename_suggests_base` used to test `"base" in name`, which fires on+# "database" and "basement". Digits stay adjacent on purpose -- "base9b" is a name, "database" is+# not a claim.+_SUGGESTS_BASE = re.compile(r"(?<![a-z])base(?![a-z])")+_SUGGESTS_DISTILLED = re.compile(r"(?<![a-z])distill")+# "undistilled" and "non-distilled" are the Base claim spelled out, so they have to be read before+# the word they contain. A lookbehind cannot do this: it has to span an optional separator.+_DENIES_DISTILLED = re.compile(r"(?<![a-z])(?:non|not|un)[-_ ]?distill")+- Klein 9B Base and Klein 9B have identical architectures and cannot be distinguished- from the state dict. We use the filename as a heuristic: filenames containing "base"- (e.g. "flux-2-klein-base-9b", "FLUX.2-klein-base-9B") indicate the undistilled model.+def _filename_suggests_base(name: str) -> bool:+ """Whether a model's name says it is the Base (undistilled) variant.++ Klein 9B Base and Klein 9B have identical architectures and identical keys, so the name is the+ only thing left to read. Getting it wrong is not cosmetic: the two ship different default step+ counts (28 against 4), so a distilled model identified as Base generates at seven times the cost+ and a Base model identified as distilled generates at four steps it was never trained for.++ Read in order of how specific the claim is: a denial of distillation, then a claim of it, then+ the bare word "base". A name carrying both "distilled" and "base" is naming its ancestry rather+ than its variant, which is how the community repacks are named -- and which *source* the same+ weights arrive from decides the name: installed by path+ `Winnougan/Klein9b-Distilled-Base-INT8-Convrot` is `flux-2-klein-9b-int8-convrot` and identifies+ correctly, installed by repo id it is the repo's name and used not to."""- return "base" in name.lower()+ lowered = name.lower()+ if _DENIES_DISTILLED.search(lowered):+ return True+ if _SUGGESTS_DISTILLED.search(lowered):+ return False+ return bool(_SUGGESTS_BASE.search(lowered))def _get_flux2_variant(state_dict: dict[str | int, Any]) -> Flux2VariantType | None:diff --git a/tests/backend/model_manager/configs/test_flux2_base_variant_naming.py b/tests/backend/model_manager/configs/test_flux2_base_variant_naming.pynew file mode 100644index 0000000000..8356accb42--- /dev/null+++ b/tests/backend/model_manager/configs/test_flux2_base_variant_naming.py@@ -0,0 +1,70 @@+"""Which FLUX.2 Klein variant a file is identified as when only its name can say.++Klein 9B and Klein 9B Base ship identical architectures and identical keys, so the state dict+cannot tell them apart and the name is the whole decision. It is not a cosmetic one: the two carry+different default step counts (4 against 28), so identifying a distilled model as Base generates at+seven times the cost, and the reverse generates at four steps the model was never trained for.+"""++from types import SimpleNamespace++import pytest+import torch++from invokeai.backend.model_manager.configs.main import Main_Checkpoint_Flux2_Config+from invokeai.backend.model_manager.taxonomy import Flux2VariantType++# `context_in_dim` is what names the size: 3 x Qwen3-8B's hidden 4096 for the 9B, 3 x Qwen3-4B's+# 2560 for the 4B. Nothing in either file says which of the two 9B releases it is.+_CONTEXT_DIM = {Flux2VariantType.Klein9B: 12288, Flux2VariantType.Klein4B: 7680}+++def _variant_of(name: str, size: Flux2VariantType) -> Flux2VariantType:+ mod = SimpleNamespace(+ name=name,+ load_state_dict=lambda: {"txt_in.weight": torch.zeros(1, _CONTEXT_DIM[size])},+ )+ return Main_Checkpoint_Flux2_Config._get_variant_or_raise(mod)+++ "name",+ [+ "flux-2-klein-9b-base",+ "FLUX.2-klein-base-9B",+ "flux2_klein_9b_BASE_fp8",+ # A digit is not a word boundary the way a letter is: this is still the word "base".+ "flux2-klein-base9b",+ # The claim spelled out. It contains "distilled", so reading that word first gets it backwards.+ "flux-2-klein-9b-undistilled",+ "flux-2-klein-9b-base-non-distilled",+ ],+)+def test_a_name_that_says_base_identifies_the_undistilled_release(name: str) -> None:+ assert _variant_of(name, Flux2VariantType.Klein9B) is Flux2VariantType.Klein9BBase+++ "name",+ [+ # The case that reached users. Installed by path this repack is+ # `flux-2-klein-9b-int8-convrot` and identified correctly; installed by repo id the name+ # becomes the repo's, which says what the weights were distilled *from*.+ "Klein9b-Distilled-Base-INT8-Convrot",+ "flux-2-klein-9b-distilled",+ # A substring is not a word. Neither of these says anything about the variant.+ "klein-9b-database-export",+ "klein-9b-basement-run",+ # Names its method, not its variant.+ "klein-9b-distillation-run",+ ],+)+def test_a_name_that_does_not_claim_the_base_variant_stays_distilled(name: str) -> None:+ assert _variant_of(name, Flux2VariantType.Klein9B) is Flux2VariantType.Klein9B+++def test_the_4b_pair_is_decided_the_same_way() -> None:+ """Both sizes have a Base release and both read the same name, so a fix to one that misses the+ other would leave half the problem in place."""+ assert _variant_of("flux-2-klein-4b-base", Flux2VariantType.Klein4B) is Flux2VariantType.Klein4BBase+ assert _variant_of("Klein4b-Distilled-Base", Flux2VariantType.Klein4B) is Flux2VariantType.Klein4B