Download code/tt_centerpoint/tests/test_reference_cpu.py from changh95/centerpoint-p150: direct link, hf CLI and curl.
- Browser
- Download file 5.99 kB
-
https://huggingface.co/changh95/centerpoint-p150/resolve/main/code/tt_centerpoint/tests/test_reference_cpu.py
- Command line
-
hf download hf://changh95/centerpoint-p150/code/tt_centerpoint/tests/test_reference_cpu.py
-
curl -L -o test_reference_cpu.py https://huggingface.co/changh95/centerpoint-p150/resolve/main/code/tt_centerpoint/tests/test_reference_cpu.py
5.99 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """The CPU reference (``reference/``: ONNX initializers by node, BN folded where the port folds it, torch fp32) vs | |
| ONNX Runtime on the deployed ONNX as Autoware binds it (PLAN.md 0.3 item 1; BUNDLE_CONVENTIONS.md section 10): | |
| - before the data-dependent selection: every module tap (``pfn.layer0``, ``pfn``, ``canvas``, | |
| ``backbone.block0-2``, ``neck.deblock0-2``, ``head.shared``, the six head maps) at PCC >= 0.9999 against the same | |
| tensor of the deployed graph (ORT exposes it as an extra graph output), and every per-layer tap too; | |
| - after it (decode, sort, NMS, remapper): set-based, i.e. the same detections (same label, centre within 1 cm, | |
| score within 1e-4), and the same counts at every stage; | |
| - the reference also reproduces the research goldens of the shipped PandaSet sample (``public_data`` expected | |
| outputs: cells, labels, scores) and the encoder on the full 40,000-row tensor (the padded rows Autoware feeds). | |
| Runs in the research venv (onnxruntime + torch, CPU only; keep threads <= 4 on the shared host):: | |
| cd bundles/centerpoint-p150 && OMP_NUM_THREADS=4 PYTHONPATH=code \\ | |
| /home/ubuntu/experiments/tt-models/tools/research-venv/bin/python -m pytest -q -p no:cacheprovider \\ | |
| code/tt_centerpoint/tests/test_reference_cpu.py | |
| Skipped where onnxruntime or the weights are absent. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import numpy as np | |
| import pytest | |
| pytest.importorskip("onnxruntime") | |
| pytest.importorskip("onnx") | |
| torch = pytest.importorskip("torch") | |
| from tt_centerpoint.reference.model import MODULE_TAPS # noqa: E402 | |
| from tt_centerpoint.reference.pipeline import CenterPointReference # noqa: E402 | |
| from tt_centerpoint.tests import _data # noqa: E402 | |
| from tt_centerpoint.ttaw.golden import TapRegistry # noqa: E402 | |
| from tt_centerpoint.ttaw.metrics import match_detections, pcc # noqa: E402 | |
| PCC_MIN = 0.9999 # PLAN.md 0.3 item 1 | |
| THREADS = 4 | |
| CASES = [("base", "test_pcd.npz"), ("tiny", "test_pcd.npz"), ("base", "pandaset_019_f40.npz"), | |
| ("tiny", "pandaset_019_f40.npz")] | |
| def references(): | |
| w = _data.weights_dir() | |
| torch.set_num_threads(THREADS) | |
| out = {} | |
| for variant in ("base", "tiny"): | |
| out[variant] = (CenterPointReference(w, variant, backend="torch", threads=THREADS), | |
| CenterPointReference(w, variant, backend="ort", threads=THREADS, intermediates=True)) | |
| return out | |
| _RUNS = {} | |
| def _runs(references, variant, sample): | |
| key = (variant, sample) | |
| if key not in _RUNS: | |
| rt, ro = references[variant] | |
| tt, to = TapRegistry(), TapRegistry() # every tap (module and per layer) | |
| a = rt.run(_data.sample_path(sample), taps=tt) | |
| b = ro.run(_data.sample_path(sample), taps=to) | |
| _RUNS.clear() # keep one case in memory (base taps are ~1 GB) | |
| _RUNS[key] = (a, b, tt, to) | |
| return _RUNS[key] | |
| def test_taps_match_onnxruntime(references, variant, sample): | |
| a, b, tt, to = _runs(references, variant, sample) | |
| assert np.array_equal(a.prepared.features, b.prepared.features) # identical voxel tensors | |
| names = to.names() | |
| assert set(MODULE_TAPS) <= set(names) and "pfn.layer0" in names | |
| worst = {} | |
| for name in names: | |
| x, y = tt[name], to[name] | |
| assert x.shape == y.shape, name | |
| worst[name] = pcc(x, y) | |
| print(variant, sample, json.dumps({k: round(v, 10) for k, v in sorted(worst.items(), key=lambda kv: kv[1])[:5]})) | |
| bad = {k: v for k, v in worst.items() if not v >= PCC_MIN} | |
| assert not bad, bad | |
| def test_detections_match_onnxruntime_set_based(references, variant, sample): | |
| a, b, _, _ = _runs(references, variant, sample) | |
| assert a.counts == b.counts | |
| da, db = a.output, b.output | |
| m = match_detections(da.boxes, da.label_ids, da.scores, db.boxes, db.label_ids, db.scores, max_dist=0.01) | |
| assert m.recall == 1.0 and m.precision == 1.0, m.to_dict() | |
| assert max(m.score_errors, default=0.0) < 1e-4 | |
| assert len(da) == len(db) and len(da) > 0 | |
| def test_pandaset_sample_reproduces_research_golden(references, variant): | |
| """research/centerpoint/public_data/sample_ship/pandaset_019_f40/expected_<variant>.json (ORT, seed 0).""" | |
| gold = json.loads((_data.research_dir() / "public_data" / "sample_ship" / "pandaset_019_f40" / | |
| f"expected_{variant}.json").read_text()) | |
| a, _, _, _ = _runs(references, variant, "pandaset_019_f40.npz") | |
| assert a.prepared.num_pillars == gold["num_pillars"] and a.counts == gold["counts"] | |
| objs = a.objects | |
| labels = [a.output.labels[int(v)] for v in objs.label] | |
| assert labels == [o["label"] for o in gold["objects"]] | |
| assert objs.extra["cell"].tolist() == [o["cell"] for o in gold["objects"]] | |
| np.testing.assert_allclose(objs.existence_probability, [o["score"] for o in gold["objects"]], atol=2e-5) | |
| np.testing.assert_allclose(objs.x, [o["x"] for o in gold["objects"]], atol=1e-3) | |
| def test_encoder_on_the_full_40000_rows(references, variant): | |
| """Autoware feeds all 40,000 rows; the reference computes the kept pillars only. The padded rows are all-zero | |
| inputs whose output is the same constant row in both (max over the padded slots included).""" | |
| rt, ro = references[variant] | |
| prep = rt.pre(_data.SAMPLES / "test_pcd.npz") | |
| pf_ort = ro.net.encoder(prep.features, prep.num_pillars) | |
| with torch.no_grad(): | |
| pf_t = rt.net.pfn(prep.features).numpy() | |
| n = prep.num_pillars | |
| assert pf_ort.shape == (40000, 32) and pcc(pf_t[:n], pf_ort[:n]) >= PCC_MIN | |
| np.testing.assert_allclose(pf_t[n:], pf_ort[n:], atol=1e-5) | |
| assert np.allclose(pf_ort[n:], pf_ort[n]) # one constant row | |