Merged MOSS Image SFT and v2 lora at 6 steps and UniPC FlowMatch Scheduler
| A | B | C | D | |
|---|---|---|---|---|
| 1 |
|
|
|
|
| 2 |
| |||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 | ||||
| 11 | ||||
| 12 | ||||
Runtime: App: SD.Next| Version: 2ffc5e4e| Pipeline: MOSSImagePipeline| Operations: txt2img
Parameters: Steps: 4| Size: 1024x1024| Sampler: Default| Scheduler: MOSSImageFlowMatchScheduler| Seed: 1| CFG scale: 1| CFG rescale: 0| Model: MOSS-Image-SFT-4step-v2
185HÂ Time: 1m 11.25s | wall 71.21 process 71.21 pipeline 66.52 decode 4.67 preview 3.17 te 1.67 gc 0.66 sync 0.62 vae 0.45 | GPU 27922 MB 44% | RAM 38.09 GB 61%
Turbo LORA Merge done by script since SD.Next doesnot support MOSS Loras yet
Script
vi /docker/sdnext/app-volume/merge_moss.py |
import os, sys, glob, shutil
from safetensors import safe_open
from safetensors.torch import save_file
DIFF = "/mnt/models/Diffusers/models--SD-Next--MOSS-Image-SFT/snapshots/fcd84d2b48fce769407c9e108be7b4b474e0fb40"
TSRC = glob.glob("/mnt/models/huggingface/models--SD-Next--MOSS-Image-SFT/snapshots/*/transformer")[0]
LORA = "/mnt/models/Lora/moss_image_sft_4_steps_lora_v2_600_avg_rank_72_bf16.safetensors"
DST = "/mnt/models/Diffusers/MOSS-Image-SFT-4step-merged"
SCALE = 1.0
WRITE = "--write" in sys.argv
mods = {}
with safe_open(LORA, "pt") as f:
for k in f.keys():
if k.endswith(".alpha"):
name, kind = k[:-6], "alpha"
elif k.endswith(".lora_down.weight"):
name, kind = k[:-17], "down"
elif k.endswith(".lora_up.weight"):
name, kind = k[:-15], "up"
else:
print("unknown key:", k)
continue
name = name.removeprefix("diffusion_model.")
mods.setdefault(name, {})[kind] = f.get_tensor(k)
print("LoRA modules:", len(mods))
shards = sorted(glob.glob(os.path.join(TSRC, "*.safetensors")))
keys = {}
for s in shards:
with safe_open(s, "pt") as f:
for k in f.keys():
keys[k] = s
print("transformer dir:", TSRC)
print("shards:", len(shards), "tensors:", len(keys))
missing = [n for n in mods if n + ".weight" not in keys]
print("matched:", len(mods) - len(missing), "missing:", len(missing))
if missing:
print("first missing:", missing[:10])
print("transformer keys with 'layers.0.':")
print("\n".join([k for k in keys if "layers.0." in k][:40]))
sys.exit(1)
if not WRITE:
print("Dry run OK. Re-run with --write to merge.")
sys.exit(0)
os.makedirs(os.path.join(DST, "transformer"), exist_ok=True)
for name in os.listdir(DIFF):
d = os.path.join(DST, name)
if not os.path.lexists(d):
os.symlink(os.path.realpath(os.path.join(DIFF, name)), d)
for name in os.listdir(TSRC):
if not name.endswith(".safetensors"):
shutil.copy(os.path.join(TSRC, name), os.path.join(DST, "transformer", name))
for s in shards:
tensors = {}
with safe_open(s, "pt") as f:
for k in f.keys():
tensors[k] = f.get_tensor(k)
n = 0
for k in list(tensors):
if not k.endswith(".weight") or k[:-7] not in mods:
continue
m = mods[k[:-7]]
down, up = m["down"].float(), m["up"].float()
rank = down.shape[0]
alpha = float(m["alpha"]) if "alpha" in m else float(rank)
delta = (up @ down) * (alpha / rank) * SCALE
w = tensors[k]
assert delta.shape == w.shape, (k, delta.shape, w.shape)
tensors[k] = (w.float() + delta).to(w.dtype)
n += 1
out = os.path.join(DST, "transformer", os.path.basename(s))
save_file(tensors, out, metadata={"format": "pt"})
print("wrote", out, "merged", n)
print("done:", DST) |
docker exec -it sdnext-ipex bash |
/mnt/python/venv/bin/python3 merge_moss.py /mnt/python/venv/bin/python3 merge_moss.py --write |
LoRA modules: 252 transformer dir: /mnt/models/huggingface/models--SD-Next--MOSS-Image-SFT/snapshots/13c25c73df159fece877da99300792ae0b787510/transformer shards: 3 tensors: 463 matched: 252 missing: 0 wrote /mnt/models/Diffusers/MOSS-Image-SFT-4step-v2/transformer/diffusion_pytorch_model-00001-of-00003.safetensors merged 118 wrote /mnt/models/Diffusers/MOSS-Image-SFT-4step-v2/transformer/diffusion_pytorch_model-00002-of-00003.safetensors merged 132 wrote /mnt/models/Diffusers/MOSS-Image-SFT-4step-v2/transformer/diffusion_pytorch_model-00003-of-00003.safetensors merged 2 done: /mnt/models/Diffusers/MOSS-Image-SFT-4step-v2 |