ckpt_io: also load AR-branch LoRA safetensors
Browse files- scripts/ckpt_io.py +10 -8
scripts/ckpt_io.py
CHANGED
|
@@ -1,18 +1,20 @@
|
|
| 1 |
"""Load our checkpoints from either the original .pt files or the .safetensors releases, returning the same dict layout the scripts expect.
|
| 2 |
-
head
|
| 3 |
-
|
|
|
|
| 4 |
import torch
|
| 5 |
-
|
| 6 |
def load_ckpt(path, map_location="cpu"):
|
| 7 |
if not str(path).endswith(".safetensors"): return torch.load(path, map_location=map_location, weights_only=False)
|
| 8 |
from safetensors.torch import load_file; from safetensors import safe_open
|
| 9 |
t=load_file(path, device=str(map_location))
|
| 10 |
with safe_open(path, "pt") as f: meta=f.metadata() or {}
|
| 11 |
if any(k.endswith(".lora_A") for k in t):
|
| 12 |
-
|
| 13 |
-
lora=[]
|
| 14 |
for L in layers:
|
| 15 |
-
for blk,proj in
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
return {"model":t, "cfg":{"instnorm": meta.get("input","").find("instnorm=true")>=0}}
|
|
|
|
| 1 |
"""Load our checkpoints from either the original .pt files or the .safetensors releases, returning the same dict layout the scripts expect.
|
| 2 |
+
head : {"model": state_dict, "cfg": {...}}
|
| 3 |
+
NAR LoRA: {"lora": [A0,B0,...] (layer-major: nar_self_attn q,k,v,o then nar_mlp gate,up,down), "io": {"vae2llm": sd, "llm2vae": sd}, "rank": int}
|
| 4 |
+
AR LoRA : {"lora": [A0,B0,...] (layer-major: self_attn q,k,v,o then mlp gate,up,down), "rank": int}"""
|
| 5 |
import torch
|
| 6 |
+
def _mods(prefix): return [(f"{prefix}self_attn",n) for n in ("q_proj","k_proj","v_proj","o_proj")]+[(f"{prefix}mlp",n) for n in ("gate_proj","up_proj","down_proj")]
|
| 7 |
def load_ckpt(path, map_location="cpu"):
|
| 8 |
if not str(path).endswith(".safetensors"): return torch.load(path, map_location=map_location, weights_only=False)
|
| 9 |
from safetensors.torch import load_file; from safetensors import safe_open
|
| 10 |
t=load_file(path, device=str(map_location))
|
| 11 |
with safe_open(path, "pt") as f: meta=f.metadata() or {}
|
| 12 |
if any(k.endswith(".lora_A") for k in t):
|
| 13 |
+
prefix="nar_" if any(".nar_self_attn." in k for k in t) else ""
|
| 14 |
+
layers=sorted({int(k.split(".")[1]) for k in t if k.startswith("layers.")}); lora=[]
|
| 15 |
for L in layers:
|
| 16 |
+
for blk,proj in _mods(prefix): lora+=[t[f"layers.{L}.{blk}.{proj}.lora_A"], t[f"layers.{L}.{blk}.{proj}.lora_B"]]
|
| 17 |
+
out={"lora":lora, "rank":int(meta.get("rank", lora[0].shape[0]))}
|
| 18 |
+
if prefix: out["io"]={m:{k.split(".",1)[1]:v for k,v in t.items() if k.startswith(m+".")} for m in ("vae2llm","llm2vae")}
|
| 19 |
+
return out
|
| 20 |
return {"model":t, "cfg":{"instnorm": meta.get("input","").find("instnorm=true")>=0}}
|