Download tests/test_package.py from koreashin/simcheongi: direct link, hf CLI and curl.
- Browser
- Download file 12.6 kB
-
https://huggingface.co/koreashin/simcheongi/resolve/main/tests/test_package.py
- Command line
-
hf download hf://koreashin/simcheongi/tests/test_package.py
-
curl -L -o test_package.py https://huggingface.co/koreashin/simcheongi/resolve/main/tests/test_package.py
12.6 kB
| #!/usr/bin/env python | |
| """CPU self-tests of the package with a TINY randomly initialised model (no download; ~1-3 min on CPU). | |
| python tests/test_package.py (or: pytest -q tests/test_package.py) | |
| 1 save_pretrained -> load_model round trip: identical tensors and identical plans | |
| 2 loss decreases when over-fitting 2 fixed batches (12 AdamW steps) | |
| 3 LoRA: adapter at init == base output bit for bit; only 'lora.*' parameters train (base tensors unchanged); | |
| save_adapter -> load_adapter reproduces the trained output; merge() == adapter output (fp32 rounding) and the merged | |
| model has exactly the base keys | |
| 4 geometry: route_ahead / command rules / ego history on a straight and a curved route | |
| 5 data: make_fake_dataset -> BCDataset item shapes, policy.plan on raw inputs | |
| 6 RequestBuilder input rules (model-free): any increasing clock (epoch seconds, negative origin) gives the same | |
| arrays, a clock relative to now raises, wrapped yaw == unwrapped yaw, a route that starts far behind the robot warns | |
| and still gives the route ahead of the robot, a short moving history warns, train / eval hash splits agree | |
| """ | |
| import copy | |
| import json | |
| import math | |
| import os | |
| import sys | |
| import tempfile | |
| import warnings | |
| import numpy as np | |
| import torch | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from simcheongi import configs as C # noqa: E402 | |
| from simcheongi import data as Dt # noqa: E402 | |
| from simcheongi import geometry as G # noqa: E402 | |
| from simcheongi import hub, lora # noqa: E402 | |
| from simcheongi import losses as LS # noqa: E402 | |
| from simcheongi import model as M # noqa: E402 | |
| from simcheongi.car_mode import CarStudent # noqa: E402 | |
| from simcheongi.policy import RequestBuilder, SimcheongiPolicy # noqa: E402 | |
| torch.set_num_threads(min(8, os.cpu_count() or 1)) | |
| TMP = tempfile.mkdtemp(prefix="simcheongi_test_") | |
| def tiny_dir(): | |
| d = os.path.join(TMP, "tiny_model") | |
| if not os.path.exists(os.path.join(d, "model.safetensors")): | |
| torch.manual_seed(0) | |
| cfg = C.tiny("M100") | |
| m = CarStudent(cfg) | |
| with torch.no_grad(): # non-zero adaLN so the planner reads the scene | |
| for pl in m.planner.layers: | |
| pl.adaln_modulation[1].weight.normal_(0, 0.02) | |
| base = {"format": hub.FORMAT, "name": "tiny-test"} | |
| hub.save_pretrained(m, d, base) | |
| return d | |
| def plan_of(model, b, seed=7): | |
| with torch.no_grad(): | |
| sc = model.scene(b, route_mode="route") | |
| noise = torch.randn(b["cur"].shape[0], C.N_FUT, 3, generator=torch.Generator().manual_seed(seed)) | |
| return model.sample(sc, b, noise=noise) | |
| def test_roundtrip(): | |
| d = tiny_dir() | |
| m1 = hub.load_model(d) | |
| d2 = os.path.join(TMP, "tiny_model_copy") | |
| hub.save_pretrained(m1, d2) | |
| m2 = hub.load_model(d2) | |
| s1, s2 = m1.state_dict(), m2.state_dict() | |
| assert sorted(s1) == sorted(s2) and all(torch.equal(s1[k], s2[k]) for k in s1) | |
| b = Dt.fake_batch(2, seed=1) | |
| assert torch.equal(plan_of(m1, b), plan_of(m2, b)) | |
| assert hub.sha256_file(os.path.join(d, "model.safetensors")) == hub.sha256_file(os.path.join(d2, "model.safetensors")) | |
| def test_loss_decreases(): | |
| M.set_matmul_precision() | |
| torch.manual_seed(0) | |
| m = hub.load_model(tiny_dir(), eval_mode=False) | |
| m.train() | |
| batches = [Dt.fake_batch(2, seed=s) for s in (3, 4)] | |
| before = np.mean([LS.fixed_loss(m, b) for b in batches]) | |
| opt = torch.optim.AdamW([p for p in m.parameters() if p.requires_grad], lr=3e-4) | |
| for i in range(12): | |
| loss, _ = LS.compute_loss(m, batches[i % 2]) | |
| opt.zero_grad() | |
| loss.backward() | |
| opt.step() | |
| after = np.mean([LS.fixed_loss(m, b) for b in batches]) | |
| print(f" fixed loss {before:.4f} -> {after:.4f}") | |
| assert after < before | |
| def test_lora(): | |
| M.set_matmul_precision() | |
| d = tiny_dir() | |
| base = hub.load_model(d) | |
| m = hub.load_model(d) | |
| b = Dt.fake_batch(2, seed=5) | |
| ref = plan_of(base, b) | |
| meta = lora.attach(m, rank=4, alpha=8, freeze_base=False) # same requires_grad state as `base` | |
| assert meta["n_targets"] > 0 | |
| assert torch.equal(plan_of(m, b), ref), "adapter at init must equal the base bit for bit" | |
| lora.detach(m) | |
| assert sorted(m.state_dict()) == sorted(base.state_dict()) | |
| meta = lora.attach(m, rank=4, alpha=8, freeze_base=True) | |
| tr = [n for n, p in m.named_parameters() if p.requires_grad] | |
| assert tr and all(n.startswith("lora.") for n in tr) | |
| b0 = {k: v.clone() for k, v in lora.base_state_dict(m).items()} | |
| m.train() | |
| opt = torch.optim.AdamW([p for p in m.parameters() if p.requires_grad], lr=1e-3) | |
| for i in range(4): | |
| loss, _ = LS.compute_loss(m, Dt.fake_batch(2, seed=10 + i)) | |
| opt.zero_grad() | |
| loss.backward() | |
| opt.step() | |
| m.eval() | |
| assert all(torch.equal(b0[k], v) for k, v in lora.base_state_dict(m).items()), "base weights must not change" | |
| out = plan_of(m, b) | |
| assert not torch.equal(out, ref) | |
| ad = os.path.join(TMP, "adapter") | |
| lora.save_adapter(m, ad) | |
| m2 = hub.load_model(d) | |
| lora.load_adapter(m2, ad, freeze_base=True) | |
| assert torch.equal(plan_of(m2, b), out), "load_adapter must reproduce the trained adapter" | |
| lora.merge(m2) | |
| assert sorted(m2.state_dict()) == sorted(base.state_dict()) | |
| diff = float((plan_of(m2, b) - out).abs().max()) | |
| print(f" merge max |diff| {diff:.2e} m") | |
| assert diff < 1e-3 | |
| def test_geometry(): | |
| route = np.c_[np.linspace(0, 20, 81), np.zeros(81)] | |
| ts = np.round(np.arange(0, 2.01, 0.1), 1) | |
| H = np.c_[ts, 0.3 * ts, np.zeros_like(ts), np.zeros_like(ts)] | |
| Hp, v, w, a = G.pad_and_rates(H) | |
| assert len(Hp) == 31 and abs(v[-1] - 0.3) < 1e-9 | |
| hist = G.ego_request(Hp, v[-1], a) | |
| assert hist["history"].shape == (16, 3) and np.allclose(hist["history"][-1], 0) | |
| assert abs(hist["ego_velocity"][0] - 0.3) < 1e-6 | |
| r = G.Route(route) | |
| s, _ = r.project(0.6, 0.0, -1e9, 1.0) | |
| c0, c1 = G.nav_command(r, s, hist) | |
| assert (c0, c1) == ("straight", "straight") | |
| ra, rv = G.route_ahead(r, s, 0.6, 0.0, 0.0) | |
| assert ra.shape == (41, 2) and rv.all() and abs(ra[-1, 0] - 10.0) < 1e-5 | |
| th = np.linspace(0, math.pi / 2, 60) # left quarter circle, radius 3 m | |
| rl = G.Route(np.c_[3 * np.sin(th), 3 - 3 * np.cos(th)]) | |
| c0, c1 = G.nav_command(rl, 0.0, hist) | |
| assert c1 == "left", (c0, c1) | |
| hr = G.hist_rig(Hp, v, w) | |
| assert hr.shape == (16, 6) and abs(hr[-1, 1]) < 1e-12 and abs(hr[0, 0] + 1.5) < 1e-9 | |
| def test_data_and_policy(): | |
| root = Dt.make_fake_dataset(os.path.join(TMP, "fake_bc"), n_eps=2, n_dec=4, val_eps=1) | |
| ds = Dt.BCDataset(root, split="train") | |
| it = ds[1] | |
| assert tuple(it["cur"].shape) == (3, 704, 1280) and tuple(it["hist"].shape) == (3, 3, 288, 544) | |
| assert tuple(it["lidar"].shape) == (2, 360) and tuple(it["plan"].shape) == (50, 3) | |
| b = Dt.collate([ds[0], ds[1]]) | |
| m = hub.load_model(tiny_dir()) | |
| with torch.no_grad(): | |
| loss, parts = LS.compute_loss(m, b) | |
| assert torch.isfinite(loss) | |
| pol = SimcheongiPolicy(m, device="cpu") | |
| frames = [np.full((720, 1280, 3), 100 + 10 * k, np.uint8) for k in range(4)] | |
| ts = np.round(np.arange(0, 2.01, 0.1), 1) | |
| H = np.c_[ts, 0.3 * np.maximum(0.0, ts - 0.3), np.zeros_like(ts), np.zeros_like(ts)] # drive start: at rest first | |
| lid = np.zeros(400, np.float32) | |
| out = pol.plan(frames, lid, H, np.c_[np.linspace(0, 20, 81), np.zeros(81)]) | |
| assert out.trajectory.shape == (50, 3) and np.isfinite(out.trajectory).all() | |
| out2 = pol.plan(frames, lid, H, None, command="left", session="no_route") | |
| assert out2.command_used == "left" | |
| def _drive(heading=0.0, n=60, v=0.4, x0=0.0, y0=0.0): | |
| """10 Hz pose rows (t from 0, x, y, yaw unwrapped): standing 0.3 s, then v m/s along `heading` with a heading wobble.""" | |
| rows = [] | |
| for k in range(n): | |
| s = v * 0.1 * max(0, k - 3) | |
| yaw = heading + 0.05 * math.sin(0.3 * k) | |
| rows.append((round(0.1 * k, 1), x0 + s * math.cos(heading) - 0.02 * math.sin(0.2 * k) * math.sin(heading), | |
| y0 + s * math.sin(heading) + 0.02 * math.sin(0.2 * k) * math.cos(heading), yaw)) | |
| return np.asarray(rows) | |
| def _route(heading=0.0, back=1.0, length=30.0, x0=0.0, y0=0.0): | |
| s = np.linspace(-back, length, int(round((length + back) / 0.25)) + 1) | |
| return np.c_[x0 + s * math.cos(heading), y0 + s * math.sin(heading)] | |
| KEYS6 = ("history", "history_velocity", "history_acceleration", "x_hist_rig", "x_route_ahead", "x_route_ahead_valid", | |
| "x_cmd_c1") | |
| def _builds(H_all, route, times=(1.5, 2.0, 2.5, 3.5, 5.0), off=0.0, wrap=False, rows=None): | |
| rb, out = RequestBuilder(), [] | |
| lid = np.zeros(400, np.float32) | |
| for t in times: | |
| k = int(round(t / 0.1)) | |
| H = H_all[: k + 1].copy() if rows is None else H_all[max(0, k + 1 - rows): k + 1].copy() | |
| H[:, 0] += off | |
| if wrap: | |
| H[:, 3] = np.mod(H[:, 3] + math.pi, 2 * math.pi) - math.pi | |
| with warnings.catch_warnings(record=True) as w: | |
| warnings.simplefilter("always") | |
| _, a, _ = rb.build(None, lid, H, route, session="s") | |
| out.append((a, [str(x.message) for x in w])) | |
| return out | |
| def _maxdiff(A, B): | |
| return max(float(np.abs(np.asarray(a[k], np.float64) - np.asarray(b[k], np.float64)).max()) | |
| for (a, _), (b, _) in zip(A, B) for k in KEYS6) | |
| def test_request_builder_rules(): | |
| H, route = _drive(), _route() | |
| ref = _builds(H, route) | |
| assert not any(w for _, w in ref), [w for _, w in ref] | |
| assert [list(a["x_frame_ticks"]) for a, _ in ref][:2] == [[0, 5, 10, 15], [5, 10, 15, 20]] | |
| for off in (-50.0, 37.3, 1763540000.0): # any increasing clock: same arrays, shifted ticks | |
| o = _builds(H, route, off=off) | |
| assert _maxdiff(ref, o) < 1e-5, (off, _maxdiff(ref, o)) | |
| d = round(off / 0.1) | |
| assert all(list(np.asarray(b["x_frame_ticks"]) - d) == list(a["x_frame_ticks"]) for (a, _), (b, _) in zip(ref, o)) | |
| rb = RequestBuilder() # clock relative to now: rejected | |
| lid = np.zeros(400, np.float32) | |
| for i, t in enumerate((2.0, 2.5)): | |
| Hr = H[: int(round(t / 0.1)) + 1].copy() | |
| Hr[:, 0] -= Hr[-1, 0] | |
| if i == 0: | |
| rb.build(None, lid, Hr, route, session="rel") | |
| else: | |
| try: | |
| rb.build(None, lid, Hr, route, session="rel") | |
| raise AssertionError("relative clock must raise") | |
| except ValueError: | |
| pass | |
| Hp, rp = _drive(heading=math.pi), _route(heading=math.pi) # heading crosses +-pi: wrapped yaw == unwrapped | |
| a_un, a_wr = _builds(Hp, rp), _builds(Hp, rp, wrap=True) | |
| assert np.ptp(np.mod(Hp[:, 3] + math.pi, 2 * math.pi)) > math.pi # the wrapped input really jumps | |
| assert _maxdiff(a_un, a_wr) < 1e-9, _maxdiff(a_un, a_wr) | |
| far = _route(back=30.0) # route starting 30 m behind the robot | |
| o = _builds(H, far, times=(2.0, 2.5)) | |
| assert o[0][1] and "route starts" in o[0][1][0], o[0][1] | |
| assert _maxdiff(ref[1:2], o[:1]) < 1e-6, _maxdiff(ref[1:2], o[:1]) # = the route cut at the robot | |
| o = _builds(H, route, times=(3.0,), rows=16) # short window while moving: warned | |
| assert any("padded as STANDING STILL" in w for w in o[0][1]), o[0][1] | |
| o = _builds(H, route, times=(4.0,), rows=31) # >= 31 rows: no warning | |
| assert not o[0][1], o[0][1] | |
| ids = [f"ep{i:04d}" for i in range(200)] # train.py and eval_offline.py: the same val split | |
| from simcheongi import training as TR # noqa: PLC0415 | |
| a = TR.build_parser().parse_args(["--data", "x", "--out", "y", "--val-frac", "0.1", "--seed", "123"]) | |
| assert a.split_seed == 0 | |
| va = {i for i in ids if Dt.hash_split(i, a.val_frac, a.split_seed) == "val"} | |
| assert va and va == {i for i in ids if Dt.hash_split(i, 0.1, 0) == "val"} | |
| if __name__ == "__main__": | |
| tests = [test_roundtrip, test_loss_decreases, test_lora, test_geometry, test_data_and_policy, | |
| test_request_builder_rules] | |
| ok = True | |
| for t in tests: | |
| try: | |
| t() | |
| print(f"PASS {t.__name__}", flush=True) | |
| except Exception as e: # noqa: BLE001 | |
| ok = False | |
| print(f"FAIL {t.__name__}: {type(e).__name__}: {e}", flush=True) | |
| import traceback | |
| traceback.print_exc() | |
| print("ALL OK" if ok else "SOME FAILED") | |
| sys.exit(0 if ok else 1) | |