gso-orbit-rgba / convert_gate_up.py
ysharma's picture
ysharma HF Staff
converter: split fused img_mlp.gate_up LoRA into diffusers gate_layer+proj
af9bd39 verified
Raw History Blame Contribute Delete
3.02 kB
"""Split the fused img_mlp.gate_up LoRA from an ai-toolkit (ComfyUI-style)
Qwen-Image-2.1 checkpoint into diffusers' separate gate_layer + proj modules.
Ground truth (diffusers @ 0121a91f9d, transformer_qwenimage21.py):
QwenImage21SwiGLUFeedForward has proj, out, gate_layer, all nn.Linear(bias=False),
gate_layer: Linear(4096, 12288), proj: Linear(4096, 12288), out: Linear(12288, 4096).
ai-toolkit exports gate_up.lora_B as cat([gate_B, proj_B], dim=0) -> (24576, rank).
Everything else in the checkpoint is kept verbatim (attn + img_mlp.out keys are
proven to load via QwenImage21Pipeline.load_lora_weights).
"""
import json
import torch
from huggingface_hub import hf_hub_download, HfApi
from safetensors.torch import load_file, save_file
SRC_REPO = "ysharma/orbit-alpha-lora"
SRC_FILE = "checkpoints/steps2000res768/orbit_alpha_lora/orbit_alpha_lora.safetensors"
DST_NAME = "orbit_alpha_lora_gate_up_split.safetensors"
DST_REPO = "ysharma/orbit-alpha-lora"
RANK = 32
GATE_OUT = 12288 # mlp_ratio 3 * hidden 4096, from Qwen/Qwen-Image-2.1 transformer config
path = hf_hub_download(repo_id=SRC_REPO, filename=SRC_FILE)
sd = load_file(path)
print(f"loaded {len(sd)} tensors from {SRC_FILE}")
out = {}
n_split = 0
for key, tensor in sd.items():
if ".img_mlp.gate_up.lora_A.weight" in key:
base = key.replace(".gate_up.lora_A.weight", "")
assert tensor.shape == (RANK, 4096), f"unexpected lora_A shape {tensor.shape} for {key}"
out[f"{base}.gate_layer.lora_A.weight"] = tensor.clone()
out[f"{base}.proj.lora_A.weight"] = tensor.clone()
elif ".img_mlp.gate_up.lora_B.weight" in key:
base = key.replace(".gate_up.lora_B.weight", "")
assert tensor.shape == (2 * GATE_OUT, RANK), f"unexpected fused lora_B shape {tensor.shape} for {key}"
gate_b, proj_b = tensor.chunk(2, dim=0)
assert gate_b.shape == (GATE_OUT, RANK) and proj_b.shape == (GATE_OUT, RANK)
out[f"{base}.gate_layer.lora_B.weight"] = gate_b.contiguous()
out[f"{base}.proj.lora_B.weight"] = proj_b.contiguous()
n_split += 1
else:
out[key] = tensor
assert n_split == 32, f"expected 32 blocks with gate_up, found {n_split}"
assert not any(".gate_up." in k for k in out), "fused gate_up keys still present"
assert sum(1 for k in out if ".gate_layer.lora_B" in k) == 32
assert sum(1 for k in out if ".proj.lora_B" in k) == 32
assert len(out) == len(sd) + 2 * 32, f"tensor count changed unexpectedly: {len(sd)} -> {len(out)}"
nonzero = all(torch.any(t != 0) for k, t in out.items() if "lora_B" in k)
assert nonzero, "a lora_B tensor is all-zero"
save_file(out, "/tmp/" + DST_NAME, metadata={"format": "pt"})
api = HfApi()
api.upload_file(
path_or_fileobj="/tmp/" + DST_NAME,
path_in_repo="checkpoints/steps2000res768/" + DST_NAME,
repo_id=DST_REPO,
repo_type="model",
commit_message="Split fused img_mlp.gate_up into diffusers gate_layer + proj",
)
print("DONE: uploaded", DST_NAME, "tensors:", len(out), "splits:", n_split)