Download examples/visualizer_code/source/tests/test_scale4d_teacher_export.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/tests/test_scale4d_teacher_export.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/tests/test_scale4d_teacher_export.py
-
curl -L -o test_scale4d_teacher_export.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/tests/test_scale4d_teacher_export.py
1.71 kB
| import hashlib | |
| import torch | |
| from safetensors.torch import load_file | |
| from d4rt_scripts import export_scale4d_teacher as exporter | |
| def test_export_uses_saved_teacher_tensors(tmp_path, monkeypatch): | |
| checkpoint = tmp_path / "checkpoint.pt" | |
| checkpoint.write_bytes(b"verified source checkpoint") | |
| source_hash = hashlib.sha256(checkpoint.read_bytes()).hexdigest() | |
| payload = { | |
| "format_version": 1, | |
| "global_step": 40000, | |
| "run_config": { | |
| "trainer": {"type": "mv_trainer_stable_ucpe_flywheel_v02.MVTrainerConfig"} | |
| }, | |
| "modules": { | |
| "encoder": {"weight": torch.ones(2, 3)}, | |
| "decoder": {"weight": torch.ones(3, 4)}, | |
| "teacher_encoder": {"weight": torch.full((2, 3), 2.0)}, | |
| "teacher_decoder": {"weight": torch.full((3, 4), 3.0)}, | |
| "ema_state": { | |
| "updates": torch.tensor(40000), | |
| "decay": torch.tensor(0.99999, dtype=torch.float64), | |
| }, | |
| }, | |
| } | |
| monkeypatch.setattr(exporter, "_read_weights", lambda path: payload) | |
| monkeypatch.setattr( | |
| exporter, | |
| "recorded_model_config", | |
| lambda config: ({"load_pretrained": False}, {}), | |
| ) | |
| output = tmp_path / "teacher" | |
| receipt = exporter.export_teacher(checkpoint, output, source_hash) | |
| state = load_file(str(output / "model.safetensors")) | |
| torch.testing.assert_close( | |
| state["encoder.weight"], payload["modules"]["teacher_encoder"]["weight"] | |
| ) | |
| torch.testing.assert_close( | |
| state["decoder.weight"], payload["modules"]["teacher_decoder"]["weight"] | |
| ) | |
| assert receipt["weight_role"] == "ema_teacher" | |
| assert receipt["ema_updates"] == 40000 | |