Download bbflow_base/scripts/audit_condition_branch_weights.py from hanghang214/data_transfer: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/scripts/audit_condition_branch_weights.py
- Command line
-
hf download hf://datasets/hanghang214/data_transfer/bbflow_base/scripts/audit_condition_branch_weights.py
-
curl -L -o audit_condition_branch_weights.py https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/scripts/audit_condition_branch_weights.py
2.14 kB
| from pathlib import Path | |
| import argparse | |
| import json | |
| import torch | |
| def tensor_norm(x): | |
| x = x.detach().float() | |
| return { | |
| "shape": list(x.shape), | |
| "l2": float(x.norm().cpu()), | |
| "mean_abs": float(x.abs().mean().cpu()), | |
| "max_abs": float(x.abs().max().cpu()), | |
| } | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--ckpt", required=True) | |
| ap.add_argument("--out", required=True) | |
| args = ap.parse_args() | |
| ckpt = torch.load(args.ckpt, map_location="cpu", weights_only=False) | |
| sd = ckpt.get("state_dict", ckpt) | |
| groups = { | |
| "unsigned_node_z_node_adapter": ["z_node_adapter"], | |
| "signed_node_disp_node_adapter": ["disp_node_adapter"], | |
| "unsigned_edge_z_edge_adapter": ["z_edge_adapter"], | |
| "signed_edge_disp_edge_adapter": ["disp_edge_adapter"], | |
| "unsigned_film_mlp": ["film_mlp"], | |
| } | |
| report = {} | |
| for group, tokens in groups.items(): | |
| items = {} | |
| for k, v in sd.items(): | |
| if not torch.is_tensor(v): | |
| continue | |
| if all(t in k for t in tokens): | |
| items[k] = tensor_norm(v) | |
| report[group] = items | |
| summary = {} | |
| for group, items in report.items(): | |
| total_l2_sq = 0.0 | |
| last_layer_l2_sq = 0.0 | |
| n = 0 | |
| for k, stats in items.items(): | |
| total_l2_sq += stats["l2"] ** 2 | |
| n += 1 | |
| if any(s in k for s in ["2.weight", "2.bias", "film_mlp.2.weight", "film_mlp.2.bias"]): | |
| last_layer_l2_sq += stats["l2"] ** 2 | |
| summary[group] = { | |
| "num_tensors": n, | |
| "total_l2": total_l2_sq ** 0.5, | |
| "last_layer_l2": last_layer_l2_sq ** 0.5, | |
| } | |
| out = Path(args.out) | |
| out.parent.mkdir(parents=True, exist_ok=True) | |
| out.write_text(json.dumps({"summary": summary, "details": report}, indent=2), encoding="utf-8") | |
| print("===== condition branch weight summary =====") | |
| print(json.dumps(summary, indent=2)) | |
| print("\nSaved:", out) | |
| if __name__ == "__main__": | |
| main() |