|
| 1 | +import re |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +from invokeai.backend.patches.layers.base_layer_patch import BaseLayerPatch |
| 7 | +from invokeai.backend.patches.layers.utils import any_lora_layer_from_state_dict |
| 8 | +from invokeai.backend.patches.lora_conversions.flux_lora_constants import FLUX_LORA_TRANSFORMER_PREFIX |
| 9 | +from invokeai.backend.patches.model_patch_raw import ModelPatchRaw |
| 10 | + |
| 11 | +# A regex pattern that matches all of the transformer keys in the xlabs FLUX LoRA format. |
| 12 | +# Example keys: |
| 13 | +# double_blocks.0.processor.qkv_lora1.down.weight |
| 14 | +# double_blocks.0.processor.qkv_lora1.up.weight |
| 15 | +# double_blocks.0.processor.proj_lora1.down.weight |
| 16 | +# double_blocks.0.processor.proj_lora1.up.weight |
| 17 | +# double_blocks.0.processor.qkv_lora2.down.weight |
| 18 | +# double_blocks.0.processor.proj_lora2.up.weight |
| 19 | +FLUX_XLABS_KEY_REGEX = r"double_blocks\.(\d+)\.processor\.(qkv|proj)_lora([12])\.(down|up)\.weight" |
| 20 | + |
| 21 | + |
| 22 | +def is_state_dict_likely_in_flux_xlabs_format(state_dict: dict[str | int, Any]) -> bool: |
| 23 | + """Checks if the provided state dict is likely in the xlabs FLUX LoRA format. |
| 24 | +
|
| 25 | + The xlabs format is characterized by keys matching the pattern: |
| 26 | + double_blocks.{block_idx}.processor.{qkv|proj}_lora{1|2}.{down|up}.weight |
| 27 | +
|
| 28 | + Where: |
| 29 | + - lora1 corresponds to the image attention stream (img_attn) |
| 30 | + - lora2 corresponds to the text attention stream (txt_attn) |
| 31 | + """ |
| 32 | + if not state_dict: |
| 33 | + return False |
| 34 | + |
| 35 | + # Check that all keys match the xlabs pattern |
| 36 | + for key in state_dict.keys(): |
| 37 | + if not isinstance(key, str): |
| 38 | + continue |
| 39 | + if not re.match(FLUX_XLABS_KEY_REGEX, key): |
| 40 | + return False |
| 41 | + |
| 42 | + # Ensure we have at least some valid keys |
| 43 | + return any(isinstance(k, str) and re.match(FLUX_XLABS_KEY_REGEX, k) for k in state_dict.keys()) |
| 44 | + |
| 45 | + |
| 46 | +def lora_model_from_flux_xlabs_state_dict(state_dict: Dict[str, torch.Tensor]) -> ModelPatchRaw: |
| 47 | + """Converts an xlabs FLUX LoRA state dict to the InvokeAI ModelPatchRaw format. |
| 48 | +
|
| 49 | + The xlabs format uses: |
| 50 | + - lora1 for image attention stream (img_attn) |
| 51 | + - lora2 for text attention stream (txt_attn) |
| 52 | + - qkv for query/key/value projection |
| 53 | + - proj for output projection |
| 54 | +
|
| 55 | + Key mapping: |
| 56 | + - double_blocks.X.processor.qkv_lora1 -> double_blocks.X.img_attn.qkv |
| 57 | + - double_blocks.X.processor.proj_lora1 -> double_blocks.X.img_attn.proj |
| 58 | + - double_blocks.X.processor.qkv_lora2 -> double_blocks.X.txt_attn.qkv |
| 59 | + - double_blocks.X.processor.proj_lora2 -> double_blocks.X.txt_attn.proj |
| 60 | + """ |
| 61 | + # Group keys by layer (without the .down.weight/.up.weight suffix) |
| 62 | + grouped_state_dict: dict[str, dict[str, torch.Tensor]] = {} |
| 63 | + |
| 64 | + for key, value in state_dict.items(): |
| 65 | + match = re.match(FLUX_XLABS_KEY_REGEX, key) |
| 66 | + if not match: |
| 67 | + raise ValueError(f"Key '{key}' does not match the expected pattern for xlabs FLUX LoRA weights.") |
| 68 | + |
| 69 | + block_idx = match.group(1) |
| 70 | + component = match.group(2) # qkv or proj |
| 71 | + lora_stream = match.group(3) # 1 or 2 |
| 72 | + direction = match.group(4) # down or up |
| 73 | + |
| 74 | + # Map lora1 -> img_attn, lora2 -> txt_attn |
| 75 | + attn_type = "img_attn" if lora_stream == "1" else "txt_attn" |
| 76 | + |
| 77 | + # Create the InvokeAI-style layer key |
| 78 | + layer_key = f"double_blocks.{block_idx}.{attn_type}.{component}" |
| 79 | + |
| 80 | + if layer_key not in grouped_state_dict: |
| 81 | + grouped_state_dict[layer_key] = {} |
| 82 | + |
| 83 | + # Map down/up to lora_down/lora_up |
| 84 | + param_name = f"lora_{direction}.weight" |
| 85 | + grouped_state_dict[layer_key][param_name] = value |
| 86 | + |
| 87 | + # Create LoRA layers |
| 88 | + layers: dict[str, BaseLayerPatch] = {} |
| 89 | + for layer_key, layer_state_dict in grouped_state_dict.items(): |
| 90 | + layers[FLUX_LORA_TRANSFORMER_PREFIX + layer_key] = any_lora_layer_from_state_dict(layer_state_dict) |
| 91 | + |
| 92 | + return ModelPatchRaw(layers=layers) |
0 commit comments