File size: 2,561 Bytes
9c890d3 | 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 | """Export lossless SCALE4D inference weights from a native WDM checkpoint."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
from safetensors.torch import save_file
from evaluation.ucpe_checkpoint import recorded_model_config
from evaluation.ucpe_inputs import sha256_file
from nets.ucpe_cd50_stable.model import _flatten_weights, _read_weights
def export_checkpoint(checkpoint: Path, output: Path) -> dict:
payload = _read_weights(checkpoint)
if payload.get("format_version") != 1:
raise ValueError("Expected native WDM checkpoint format 1")
recorded = payload["run_config"]
encoder, decoder = recorded_model_config(recorded)
encoder["checkpoint_path"] = ""
step = int(payload["global_step"])
config = {
"format_version": 1,
"model_type": "scale4d",
"checkpoint_step": step,
"encoder": encoder,
"decoder": decoder,
"ucpe_transition": recorded.get("ucpe_transition"),
"camera_condition_max_diameter_m": recorded["trainer"].get(
"camera_condition_max_diameter_m"
),
}
state = {
name: tensor.contiguous() for name, tensor in _flatten_weights(payload).items()
}
output.mkdir(parents=True, exist_ok=True)
if any((output / name).exists() for name in ("model.safetensors", "config.json")):
raise FileExistsError("The destination already contains an inference export")
weights = output / "model.safetensors"
save_file(
state, str(weights), metadata={"format": "pt", "checkpoint_step": str(step)}
)
(output / "config.json").write_text(json.dumps(config, indent=2) + "\n")
receipt = {
"format_version": 1,
"checkpoint_step": step,
"source_sha256": sha256_file(checkpoint),
"weights_sha256": sha256_file(weights),
"weights_bytes": weights.stat().st_size,
"tensor_count": len(state),
"parameter_count": sum(tensor.numel() for tensor in state.values()),
"dtypes": sorted({str(tensor.dtype) for tensor in state.values()}),
}
(output / "release.json").write_text(json.dumps(receipt, indent=2) + "\n")
return receipt
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--checkpoint", type=Path, required=True)
parser.add_argument("--output-dir", type=Path, required=True)
args = parser.parse_args()
print(json.dumps(export_checkpoint(args.checkpoint, args.output_dir), indent=2))
if __name__ == "__main__":
main()
|