# SPDX-License-Identifier: Apache-2.0 """Device e2e test of the SERVING path (one p150): the real server in-process on loopback HTTP, the real model. $ROOT/bin/devrun -t 2400 -- python -u -m pytest -q -s -o faulthandler_timeout=2000 code/tt_rdt2/tests/test_e2e_device.py Checks: the shipped stdlib smoke test passes against it (ETH dispatch + 12 x 10 grid -- or RDT2_DISPATCH=worker and 11 x 10 --, default engines, buckets, steps, cameras; the sample's chunk vs the stored official fp32 CPU reference within the device-gate limits; repeat bit-identical; bucket 512 == 416 within the BUCKETS gate; refusals answer 400); served (PNG over HTTP) == the Python API (arrays) == the gated device path (``host_inputs`` + ``write_inputs`` + ``replay`` + ``read_action_normalized`` + ``unnormalize_action``, what tests/test_tt_device.py measures) bit for bit. ``RDT2_E2E_JSON``: where the served response and the metrics are written. """ from __future__ import annotations import json import os import tempfile import numpy as np import pytest pytestmark = pytest.mark.device def test_serving_path_vs_stored_reference_and_gated_path(tmp_path): from tt_rdt2 import api from tt_rdt2.reference.expert import unnormalize_action from tt_rdt2.server import client as rdt2_client from tt_rdt2.server import smoke_test from tt_rdt2.tests.serving import get_json, serve_and_run dispatch = (os.environ.get("RDT2_DISPATCH") or "eth").lower() out_path = os.environ.get("RDT2_E2E_JSON") or os.path.join(tempfile.gettempdir(), f"rdt2_e2e_{dispatch}.json") def workload(srv): info = get_json(srv.base + "/info") assert info["device"]["dispatch"] == dispatch and info["device"]["grid"] == {"eth": "12x10", "worker": "11x10"}[dispatch] smoke_out = tmp_path / "smoke.json" args = ["--url", srv.base, "--wait", "60", "--out", str(smoke_out)] manifest = os.environ.get("RDT2_SMOKE_MANIFEST") if manifest: args += ["--manifest", manifest] if dispatch == "eth": assert smoke_test.main(args) == 0 body = json.loads(smoke_out.read_text()) if smoke_out.exists() else None if body is None: # worker: the smoke pins ETH unless a manifest says otherwise; post the sample directly code, body = rdt2_client.post(srv.base, rdt2_client.sample_request()) assert code == 200, body ref = json.loads(rdt2_client.SAMPLE_REFERENCE.read_text()) metrics, fails = smoke_test.judge(body["actions"], ref) assert not fails, fails s = api.load_sample() m = srv.model api_out = m(s["images"], s["instruction"], state=s["state"], seed=0) assert api_out.to_dict()["actions"] == body["actions"], "served != Python API" with m._lock: # the gated path, on the same resident traces tt = m._m h = tt.host_inputs(s["images"], s["instruction"], s["state"], api.seeded_noise(0)) L = h["bucket"] tt.write_inputs(h) tt.replay(L, blocking=False) ns = tt.read_action_normalized(L) act = unnormalize_action(ns, tt.norm)[0].float().numpy().astype(np.float32) assert act.tolist() == body["actions"], "served != gated device path" return {"dispatch": dispatch, "info": info, "boot_s": srv.boot_s, "served": body, "metrics_vs_cpu": metrics, "limits": ref["limits"], "served_equals_api": True, "served_equals_gated_path": True} result = serve_and_run(workload) metrics = result["metrics_vs_cpu"] os.makedirs(os.path.dirname(os.path.abspath(out_path)), exist_ok=True) with open(out_path, "w") as f: json.dump(result, f, indent=1) print(f"[e2e] {dispatch}: {metrics} -> {out_path}")