Download project/scripts/audit_cave_adapter_precision.py from xiaojie06/CaveWorld3D: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/xiaojie06/CaveWorld3D/resolve/main/project/scripts/audit_cave_adapter_precision.py
- Command line
-
hf download hf://xiaojie06/CaveWorld3D/project/scripts/audit_cave_adapter_precision.py
-
curl -L -o audit_cave_adapter_precision.py https://huggingface.co/xiaojie06/CaveWorld3D/resolve/main/project/scripts/audit_cave_adapter_precision.py
2.57 kB
| """Measure which Voyager weights change when a trained low-rank update is merged into BF16.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import torch | |
| from safetensors import safe_open | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--base", type=Path, required=True) | |
| parser.add_argument("--adapter", type=Path, required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--scale", type=float, default=1.0) | |
| args = parser.parse_args() | |
| base = torch.load(args.base, mmap=True, map_location="cpu", weights_only=True)[ | |
| "module" | |
| ] | |
| rng = torch.Generator().manual_seed(3901) | |
| layers = [] | |
| with safe_open(args.adapter, framework="pt", device="cpu") as saved: | |
| keys = set(saved.keys()) | |
| for name in sorted(base): | |
| if not name.endswith(".weight"): | |
| continue | |
| key = "Hunyuan_video_I2V_lora_" + name[:-7].replace(".", "_") | |
| if key + ".lora_down.weight" not in keys: | |
| continue | |
| weight = base[name] | |
| if weight.dtype != torch.bfloat16: | |
| raise ValueError(f"Expected a BF16 released weight at {name}") | |
| up = saved.get_tensor(key + ".lora_up.weight") | |
| down = saved.get_tensor(key + ".lora_down.weight") | |
| rows = torch.randperm(weight.shape[0], generator=rng)[:64] | |
| cols = torch.randperm(weight.shape[1], generator=rng)[:64] | |
| original = weight[rows[:, None], cols] | |
| delta = args.scale * (up[rows].float() @ down[:, cols].float()) | |
| merged = (original.float() + delta).bfloat16() | |
| changed = int((merged != original).sum()) | |
| layers.append(dict(layer=name, sampled=delta.numel(), changed=changed)) | |
| if 3 * len(layers) != len(keys): | |
| raise ValueError( | |
| "Some adapter tensors were not matched to the released weights" | |
| ) | |
| total = sum(x["sampled"] for x in layers) | |
| changed = sum(x["changed"] for x in layers) | |
| result = dict( | |
| adapter=args.adapter.name, | |
| scale=args.scale, | |
| seed=3901, | |
| dtype="bfloat16", | |
| sampled=total, | |
| changed=changed, | |
| unchanged_fraction=1 - changed / total, | |
| layers=layers, | |
| ) | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| args.output.write_text(json.dumps(result, indent=2) + "\n") | |
| print(len(layers), total, result["unchanged_fraction"], flush=True) | |
| if __name__ == "__main__": | |
| main() | |