Files
UniversalHumanoid/tests/test_training.py
T
2026-11-03 19:46:08 +08:00

58 lines
2.0 KiB
Python

"""Checkpoint publication/iteration semantics with a tiny fake upstream runner."""
import json
import sys
from dataclasses import dataclass
from pathlib import Path
from types import SimpleNamespace
import pytest
from universal_humanoid import artifacts
from universal_humanoid.training import ProgressLogger, training_runner
@dataclass
class Agent:
upload_model: bool = False
class FakeRunner:
def __init__(self, *args):
self.current_learning_iteration = 0
def save(self, path, infos):
artifacts.write_json(Path(path), {"iter": self.current_learning_iteration, "infos": infos})
def test_save_is_atomic_committed_and_off_by_one_safe(tmp_path, monkeypatch):
monkeypatch.setitem(sys.modules, "mjlab.rl", SimpleNamespace(MjlabOnPolicyRunner=FakeRunner))
env = SimpleNamespace(unwrapped=SimpleNamespace(device="cpu"))
run = {"checkpoints": {}, "checkpoint_metadata": {}}
progress = {}
runner = training_runner(env, Agent(), tmp_path, run, progress)
runner.current_learning_iteration = 4
checkpoint = tmp_path / "model_4.pt"
runner.save(str(checkpoint))
assert run["checkpoint_metadata"][checkpoint.name]["completed_iterations"] == 5
assert run["checkpoints"][checkpoint.name] == artifacts.digest(checkpoint)
assert json.loads(checkpoint.read_text())["infos"]["universal_humanoid"]["committed"]
assert not list(tmp_path.glob("*.tmp"))
original = checkpoint.read_bytes()
runner.save(str(checkpoint)) # upstream repeats its last periodic save
assert checkpoint.read_bytes() == original
runner.current_learning_iteration = 5
with pytest.raises(ValueError, match="overwrite"):
runner.save(str(checkpoint))
def test_progress_observer_flushes_before_callback():
calls = []
inner = SimpleNamespace(
log=lambda **kw: calls.append("log"),
writer=SimpleNamespace(flush=lambda: calls.append("flush")),
)
logger = ProgressLogger(inner, lambda kw: calls.append(kw["it"]))
logger.log(it=3)
assert calls == ["log", "flush", 3]