File size: 3,936 Bytes
d02c2a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Merge a list of LoRAs into a single checkpoint."""

import argparse
import json
import os
import sys

import torch
from safetensors.torch import load_file, save_file
from safetensors import safe_open


DTYPE_MAP = {
    "float32": torch.float32,
    "fp32": torch.float32,
    "float16": torch.float16,
    "fp16": torch.float16,
    "bfloat16": torch.bfloat16,
    "bf16": torch.bfloat16,
}


def log(message: str) -> None:
    print(message, flush=True)


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Merge a list of LoRAs into a single checkpoint."
    )
    parser.add_argument(
        "--loras",
        required=True,
        help='JSON list of {"path": "...", "strength": 1.0} entries.',
    )
    parser.add_argument("--output", required=True, help="Output .safetensors path.")
    parser.add_argument(
        "--save_dtype",
        default="bfloat16",
        choices=list(DTYPE_MAP.keys()),
        help="Dtype of the saved tensors (merging is always done in float32).",
    )
    parser.add_argument(
        "--device",
        default="cpu",
        help="Device to merge on (cpu, cuda, cuda:1, mps). Output is always saved from CPU.",
    )
    args = parser.parse_args()

    try:
        loras = json.loads(args.loras)
    except json.JSONDecodeError as e:
        print(f"Failed to parse --loras JSON: {e}", file=sys.stderr, flush=True)
        return 2

    if not isinstance(loras, list) or len(loras) == 0:
        print("--loras must be a non-empty JSON list.", file=sys.stderr, flush=True)
        return 2

    device = torch.device(args.device)
    save_dtype = DTYPE_MAP[args.save_dtype]

    log(f"Merging {len(loras)} LoRA(s) on {device}, saving as {args.save_dtype}.")

    merged: dict[str, torch.Tensor] = {}

    metadata = {}

    for i, entry in enumerate(loras):
        if not isinstance(entry, dict) or "path" not in entry:
            print(
                f"LoRA entry {i} must be an object with a 'path' field.",
                file=sys.stderr,
                flush=True,
            )
            return 2

        path = entry["path"]
        strength = float(entry.get("strength", 1.0))

        if not os.path.isfile(path):
            print(f"LoRA file not found: {path}", file=sys.stderr, flush=True)
            return 2

        log(f"[{i + 1}/{len(loras)}] Loading {path} (strength={strength})")
        state_dict = load_file(path, device=str(device))

        for key, tensor in state_dict.items():
            scaled = tensor.to(torch.float32) * strength
            if key in merged:
                merged[key].add_(scaled)
            else:
                merged[key] = scaled
        del state_dict

        if i == 0:
            # For the first LoRA, also copy over all non-tensor metadata (e.g. base model info)
            with safe_open(path, framework="pt") as f:
                metadata_to_keep = [
                    "version",
                    "format",
                    "ss_base_model_version",
                    "software",
                ]
                orig_metadata = f.metadata()
                for meta_key in metadata_to_keep:
                    if meta_key in orig_metadata:
                        metadata[meta_key] = orig_metadata[meta_key]

    log(f"Casting to {args.save_dtype} and moving to CPU")
    final = {k: v.to(save_dtype).cpu().contiguous() for k, v in merged.items()}
    merged.clear()

    log(f"Saving merged checkpoint to {args.output}")
    save_file(final, args.output, metadata=metadata)

    print(
        json.dumps(
            {
                "ok": True,
                "output": args.output,
                "num_loras": len(loras),
                "num_keys": len(final),
                "save_dtype": args.save_dtype,
                "device": str(device),
            }
        ),
        flush=True,
    )
    return 0


if __name__ == "__main__":
    sys.exit(main())