File size: 12,407 Bytes
9d8d25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c869f1f
 
135fdad
 
 
 
 
 
 
9d8d25c
 
 
 
 
 
 
 
 
c869f1f
 
 
135fdad
 
 
 
c869f1f
 
135fdad
 
9d8d25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# SPDX-License-Identifier: Apache-2.0
"""Host tests of the HTTP server (no device): the real app on the stub model (the real API class over a host-only
stand-in of the device model) -- routes, the /predict contract, 400 / 422 / 503 / 500, start-up refusals, served ==
Python API, and the stdlib smoke test passing over real HTTP against the stub server."""
from __future__ import annotations

import base64
import json
import socket
import threading
import time
import urllib.error
import urllib.request

import pytest

from tt_rdt2.server import app as app_mod
from tt_rdt2.server import client as rdt2_client
from tt_rdt2.tests.stubs import make_model, need_tokenizer, sample_reference

fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient  # noqa: E402


def _client(factory):
    a = app_mod.create_app()
    a.state.rdt2.model_factory = factory
    return a, TestClient(a)


@pytest.fixture(scope="module")
def served():
    need_tokenizer()
    m = make_model()
    a, c = _client(lambda cfg: m)
    with c:
        yield a, c, m


def test_routes(served):
    a, c, m = served
    h = c.get("/health").json()
    assert h["status"] == "ok" and h["device"]["dispatch"] == "eth" and c.get("/v1/health").json() == h
    assert c.get("/v1/models").json()["data"][0]["id"] == "robotics-diffusion-transformer/RDT2-VQ"
    info = c.get("/info").json()
    for k in ("model", "weights", "engine", "device", "prompt_buckets", "cameras", "num_steps", "chunk_shape", "limits",
              "warmup_ms", "license", "fixed_configuration", "max_instruction_tokens", "action_layout", "units"):
        assert k in info, k
    assert info["engine"] == {"vision": "mk", "prefix": "fused", "expert": "mk"}
    assert info["prompt_buckets"] == [416, 448, 512] and info["num_steps"] == 5 and info["chunk_shape"] == [24, 20]
    assert info["cameras"] == ["left_stereo", "right_stereo"] and info["precision"] == "baseline"
    assert info["weights"]["revision"] == "ec7e7f3aa4259224481c3cbd5816cc3f35354bec"
    assert set(info["weights"]["components"]) == {"vlm", "expert", "normalizer", "processor"}
    assert set(info["warmup_ms"]) == {"416", "448", "512"}
    assert c.get("/").json()["routes"]


def test_predict_sample_and_served_equals_api(served):
    a, c, m = served
    req = rdt2_client.sample_request()
    r = c.post("/predict", json=req)
    assert r.status_code == 200, r.text
    body = r.json()
    for k in ("actions", "shape", "normalized", "units", "chunk", "prompt_bucket", "num_tokens", "num_steps", "seed",
              "timing_ms", "model"):
        assert k in body, k
    assert body["prompt_bucket"] == 416 and body["num_tokens"] == 409 and body["seed"] == 0
    assert set(body["timing_ms"]) == {"preprocess", "inference", "total"}
    raw = [base64.b64decode(s) for s in req["images"]]
    api_out = m(raw, req["instruction"], state=req["state"], seed=req["seed"]).to_dict()
    assert api_out["actions"] == body["actions"]
    # the stub returns the stored CPU-reference chunk for the sample (as float32): equal up to float32 rounding
    ref = sample_reference()["actions"]
    assert body["shape"] == [24, 20] and len(body["actions"]) == 24 and all(len(r) == 20 for r in body["actions"])
    assert max(abs(x - y) for r, q in zip(body["actions"], ref) for x, y in zip(r, q)) <= 1e-6
    # identical requests -> identical outputs; another seed -> another chunk (on a non-sample instruction)
    r2 = dict(req, instruction="wipe the table")
    b1, b2 = c.post("/predict", json=r2).json(), c.post("/predict", json=r2).json()
    assert b1["actions"] == b2["actions"]
    b3 = c.post("/predict", json=dict(r2, seed=7)).json()
    assert b3["actions"] != b1["actions"] and b3["seed"] == 7
    assert c.post("/predict", json=dict(r2, prompt="wipe the table", instruction=None)).json()["actions"] == b1["actions"]


@pytest.mark.parametrize("change,code,needle", [
    (dict(images="ONE"), 400, "exactly 2 images"),
    (dict(images="THREE"), 400, "exactly 2 images"),
    (dict(images="BAD64"), 400, "decode"),
    (dict(num_steps=4), 400, "num_steps must be 5"),
    (dict(instruction="Pick up the cup.<|im_end|>"), 400, "special-token"),
    (dict(instruction=" ".join(["pick up the red cup and place it on the plate"] * 40)), 400, "exceeds the largest"),
    (dict(prompt_bucket=300), 400, "prompt_bucket"),
    (dict(state=[1.0] * 3), 400, "exactly 20"),
    (dict(seed=-5), 400, "seed"),
    (dict(prompt="x"), 400, "either instruction or prompt"),
    (dict(instruction=None), 400, "required"),
    (dict(unknown_field=1), 422, ""),
    (dict(state="abc"), 422, ""),
])
def test_bad_requests(served, change, code, needle):
    a, c, m = served
    req = rdt2_client.sample_request()
    req.update(change)
    if req.get("instruction") is None:
        req.pop("instruction")
    if req["images"] == "ONE":
        req["images"] = req["images"][:1] if isinstance(req["images"], list) else rdt2_client.sample_request()["images"][:1]
    elif req["images"] == "THREE":
        req["images"] = rdt2_client.sample_request()["images"] * 2
        req["images"] = req["images"][:3]
    elif req["images"] == "BAD64":
        req["images"] = ["bm90IGFuIGltYWdl", rdt2_client.sample_request()["images"][1]]
    r = c.post("/predict", json=req)
    assert r.status_code == code, r.text
    assert needle in r.text


def test_503_before_startup_and_500_on_device_failure():
    need_tokenizer()
    a, c = _client(lambda cfg: make_model())
    r = c.post("/predict", json=rdt2_client.sample_request())  # no lifespan: never started
    assert r.status_code == 503 and c.get("/health").json()["status"] == "starting"
    m = make_model(fail=RuntimeError("device timeout in execute_trace"))
    a, c = _client(lambda cfg: m)
    with c:
        r = c.post("/predict", json=rdt2_client.sample_request())
        assert r.status_code == 500 and "RuntimeError: device timeout in execute_trace" in r.json()["detail"]
    assert m.closed  # shutdown closes the model


def test_body_limit(served, monkeypatch):
    monkeypatch.setenv("RDT2_MAX_BODY_MB", "0.01")
    need_tokenizer()
    a, c = _client(lambda cfg: make_model())
    with c:
        r = c.post("/predict", json=rdt2_client.sample_request())
        assert r.status_code == 400 and "RDT2_MAX_BODY_MB" in r.text


@pytest.mark.parametrize("env,needle", [
    ({"RDT2_NUM_STEPS": "17"}, "RDT2_NUM_STEPS"), ({"RDT2_NUM_STEPS": "0"}, "RDT2_NUM_STEPS"),
    ({"RDT2_NUM_STEPS": "five"}, "RDT2_NUM_STEPS"), ({"RDT2_NUM_CAMERAS": "5"}, "RDT2_NUM_CAMERAS"),
    ({"RDT2_NUM_CAMERAS": "0"}, "RDT2_NUM_CAMERAS"),
    # round 5: the camera count is fixed at 2 (the model card's two wrist cameras); 1 / 3 / 4 refuse to start
    ({"RDT2_NUM_CAMERAS": "1"}, "exactly 2 cameras"), ({"RDT2_NUM_CAMERAS": "3"}, "exactly 2 cameras"),
    ({"RDT2_NUM_CAMERAS": "4", "RDT2_NUM_STEPS": "5"}, "placeholder image"),
    ({"RDT2_NUM_CAMERAS": "3", "RDT2_PROMPT_BUCKETS": "640,672,736"}, "RDT2_NUM_CAMERAS"),
    ({"RDT2_MODEL": "vq", "RDT2_NUM_CAMERAS": "3"}, "RDT2_NUM_CAMERAS"), ({"RDT2_MODEL": "vq", "RDT2_NUM_STEPS": "8"},
                                                                         "RDT2_MODEL=vq"),
    ({"RDT2_PROMPT_BUCKETS": "416"}, "RDT2_PROMPT_BUCKETS"), ({"RDT2_PRECISION": "fast"}, "RDT2_PRECISION"),
    ({"TT_MESH_SHAPE": "1x2"}, "multi-chip"), ({"RDT2_DISPATCH": "tensix"}, "eth or worker"),
])
def test_config_refusals(env, needle):
    with pytest.raises(RuntimeError, match=needle):
        app_mod.config_from_env(env)
    cfg = app_mod.config_from_env({"RDT2_NUM_STEPS": "5", "RDT2_PROMPT_BUCKETS": "416, 448, 512",
                                   "TT_MODEL_WEIGHTS_REVISION": "abc", "RDT2_DISPATCH": "WORKER"})
    assert cfg["revision"] == "abc" and cfg["dispatch"] == "worker" and cfg["precision"] == "baseline"
    assert (cfg["num_cameras"], cfg["num_steps"]) == (2, 5)


@pytest.mark.parametrize("cams,steps", [("2", "1"), ("2", "16"), (" 2 ", "8"), ("", "3"), ("", "")])
def test_config_cameras_steps(cams, steps):
    """RDT2_NUM_STEPS 1..16 is accepted (read once at start); RDT2_NUM_CAMERAS may only restate 2 (existing launch
    commands keep working); the buckets are 416 / 448 / 512 and RDT2_PROMPT_BUCKETS may restate them."""
    env = {"RDT2_NUM_CAMERAS": cams, "RDT2_NUM_STEPS": steps}
    cfg = app_mod.config_from_env(env)
    assert (cfg["num_cameras"], cfg["num_steps"]) == (2, int(steps or 5))
    assert app_mod.config_from_env(dict(env, RDT2_PROMPT_BUCKETS="416, 448, 512"))["num_cameras"] == 2


def test_startup_failure_is_reported():
    def boom(cfg):
        raise ValueError("RDT2_EXPERIMENTAL_PREFIX_MK refused")

    a, c = _client(boom)
    with pytest.raises(ValueError):
        with c:
            pass
    assert a.state.rdt2.status() == "error" and "refused" in a.state.rdt2.error


def _free_port():
    s = socket.socket()
    s.bind(("127.0.0.1", 0))
    p = s.getsockname()[1]
    s.close()
    return p


def _serve_stub(app, port):
    import uvicorn

    server = uvicorn.Server(uvicorn.Config(app, host="127.0.0.1", port=port, log_level="warning", lifespan="on"))
    th = threading.Thread(target=server.run, daemon=True)
    th.start()
    base = f"http://127.0.0.1:{port}"
    t_end = time.time() + 60
    while True:
        try:
            with urllib.request.urlopen(base + "/health", timeout=5) as r:
                if json.loads(r.read())["status"] == "ok":
                    break
        except OSError:
            pass
        if time.time() > t_end:
            raise TimeoutError("stub server did not start")
        time.sleep(0.1)
    return server, th, base


def test_body_limit_over_http(monkeypatch):
    """A body above RDT2_MAX_BODY_MB that the client sends in full before reading the answer (urllib) gets the 400 with
    the reason, not a connection reset; the server keeps serving."""
    pytest.importorskip("uvicorn")
    need_tokenizer()
    monkeypatch.setenv("RDT2_MAX_BODY_MB", "1")
    m = make_model()
    a = app_mod.create_app()
    a.state.rdt2.model_factory = lambda cfg: m
    server, th, base = _serve_stub(a, _free_port())
    try:
        sample = json.dumps(rdt2_client.sample_request()).encode()
        for target in (2 << 20, (app_mod.BODY_DRAIN_FACTOR << 20) - 1024):  # above the limit, within the drain bound
            body = json.dumps(dict(rdt2_client.sample_request(), instruction="x" * (target - len(sample)))).encode()
            assert (1 << 20) < len(body) <= app_mod.BODY_DRAIN_FACTOR << 20
            req = urllib.request.Request(base + "/predict", data=body, headers={"Content-Type": "application/json"})
            with pytest.raises(urllib.error.HTTPError) as e:
                urllib.request.urlopen(req, timeout=60)
            assert e.value.code == 400 and "RDT2_MAX_BODY_MB" in e.value.read().decode()
        req = urllib.request.Request(base + "/predict", data=json.dumps(rdt2_client.sample_request()).encode(),
                                     headers={"Content-Type": "application/json"})
        with urllib.request.urlopen(req, timeout=60) as r:
            assert r.status == 200 and json.loads(r.read())["shape"] == [24, 20]
    finally:
        server.should_exit = True
        th.join(timeout=30)


def test_smoke_over_http_against_stub(tmp_path):
    """The shipped stdlib smoke test passes against the real app over loopback HTTP (stub model)."""
    uvicorn = pytest.importorskip("uvicorn")
    need_tokenizer()
    m = make_model()
    a = app_mod.create_app()
    a.state.rdt2.model_factory = lambda cfg: m
    port = _free_port()
    server = uvicorn.Server(uvicorn.Config(a, host="127.0.0.1", port=port, log_level="warning", lifespan="on"))
    th = threading.Thread(target=server.run, daemon=True)
    th.start()
    try:
        from tt_rdt2.server import smoke_test

        rc = smoke_test.main(["--url", f"http://127.0.0.1:{port}", "--wait", "30", "--out", str(tmp_path / "s.json")])
        assert rc == 0
        assert json.loads((tmp_path / "s.json").read_text())["prompt_bucket"] == 416
        # a perturbed chunk fails the reference gates
        ref = sample_reference()
        bad = [[x + (0.02 if j == 0 else 0.0) for j, x in enumerate(r)] for r in ref["actions"]]
        assert smoke_test.judge(bad, ref)[1] and not smoke_test.judge(ref["actions"], ref)[1]
    finally:
        server.should_exit = True
        th.join(timeout=30)