| """Re-key a MOSS-Image PEFT LoRA for ComfyUI's native MOSS loader.""" | |
| import argparse | |
| import hashlib | |
| from pathlib import Path | |
| import torch | |
| from safetensors import safe_open | |
| from safetensors.torch import load_file, save_file | |
| SOURCE_PREFIX = "transformer." | |
| COMFY_PREFIX = "diffusion_model." | |
| SUFFIXES = (".lora_A.weight", ".lora_B.weight") | |
| def parse_args(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--input", required=True, type=Path) | |
| parser.add_argument("--output", required=True, type=Path) | |
| parser.add_argument("--base-model", default="OpenMOSS-Team/MOSS-Image-SFT") | |
| parser.add_argument("--trigger", default="") | |
| parser.add_argument( | |
| "--recommended-strength", | |
| default="1.0", | |
| help="Recommended model strength value or range recorded in metadata.", | |
| ) | |
| return parser.parse_args() | |
| def sha256(path): | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def module_name(key): | |
| for suffix in SUFFIXES: | |
| if key.endswith(suffix): | |
| return key[: -len(suffix)], suffix | |
| raise RuntimeError(f"Unsupported LoRA tensor key: {key}") | |
| def main(): | |
| options = parse_args() | |
| source = options.input.resolve() | |
| output = options.output.resolve() | |
| temporary = output.with_name(output.name + ".building") | |
| if not source.is_file(): | |
| raise FileNotFoundError(source) | |
| if output.exists() or temporary.exists(): | |
| raise FileExistsError(f"Output already exists: {output} / {temporary}") | |
| with safe_open(source, framework="pt", device="cpu") as opened: | |
| source_metadata = opened.metadata() or {} | |
| source_tensors = load_file(str(source), device="cpu") | |
| converted = {} | |
| pairs = {} | |
| for key, tensor in source_tensors.items(): | |
| if not key.startswith(SOURCE_PREFIX): | |
| raise RuntimeError(f"Unexpected source namespace: {key}") | |
| target = COMFY_PREFIX + key[len(SOURCE_PREFIX):] | |
| if target in converted: | |
| raise RuntimeError(f"Converted key collision: {target}") | |
| module, suffix = module_name(target) | |
| pairs.setdefault(module, set()).add(suffix) | |
| converted[target] = tensor.contiguous() | |
| incomplete = {module: suffixes for module, suffixes in pairs.items() if set(SUFFIXES) != suffixes} | |
| if incomplete: | |
| raise RuntimeError(f"Incomplete LoRA A/B pairs: {list(incomplete)[:5]}") | |
| metadata = { | |
| **source_metadata, | |
| "format": "comfyui-native-moss-image-lora", | |
| "conversion": "namespace-only: transformer. -> diffusion_model.", | |
| "source_file": source.name, | |
| "source_sha256": sha256(source), | |
| "base_model": options.base_model, | |
| "trigger": options.trigger, | |
| "recommended_strength_model": str(options.recommended_strength), | |
| "recommended_strength_clip": "0", | |
| } | |
| output.parent.mkdir(parents=True, exist_ok=True) | |
| save_file(converted, str(temporary), metadata=metadata) | |
| temporary.replace(output) | |
| reloaded = load_file(str(output), device="cpu") | |
| if set(reloaded) != set(converted): | |
| raise RuntimeError("Saved key set differs from converted key set") | |
| for target, tensor in converted.items(): | |
| if not torch.equal(tensor, reloaded[target]): | |
| raise RuntimeError(f"Tensor changed during conversion: {target}") | |
| print(f"output={output}") | |
| print(f"source_tensors={len(source_tensors)}") | |
| print(f"converted_tensors={len(converted)}") | |
| print(f"target_modules={len(pairs)}") | |
| print(f"sha256={sha256(output)}") | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 3.72 kB
- Xet hash:
- ca337c6ce7930be2acc502151c7d2f03b6f9c40039988a1dac8420ddd16fdf5f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.