File size: 10,960 Bytes
7469b2c
 
 
 
 
3aa087b
7469b2c
 
 
3aa087b
7469b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aa087b
 
7469b2c
 
 
 
 
 
3aa087b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7469b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aa087b
 
7469b2c
 
3aa087b
 
7469b2c
 
 
 
 
 
 
 
 
 
 
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2026 Tenstorrent USA, Inc.
# SPDX-License-Identifier: Apache-2.0
"""End-to-end X-VLA inference benchmark — fixed metric harness.

A fixed harness: the model's pieces are replaced, the harness is not.

Backend selection is via the env var `XVLA_BACKEND`:
  - `torch_cpu` (default) — vanilla lerobot/HF X-VLA on CPU. Reference.
  - `ttnn`               — the TT-NN port (`tt/policy.py`).

Outputs (greppable):
    inference_speed=<frames per second, where one frame = one action step>
    accuracy=<PCC% vs cached reference action chunk; 100.0 on first run>
    peak_dram=<peak DRAM MB used during inference; 0 on torch_cpu>
"""

from __future__ import annotations

import argparse
import json
import os
import sys
import time
from pathlib import Path

import numpy as np
import torch

# Bootstrap broken lerobot imports before anything else.
_HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(_HERE))
from lerobot_bootstrap import install as _bootstrap_install  # noqa: E402

_bootstrap_install()


# =============================================================================
# Config
# =============================================================================

# Where the first run caches its action chunk (later runs report PCC against it).
REFERENCE_DIR = Path(os.environ.get("XVLA_BENCH_REFERENCE_DIR", "xvla_bench_reference"))
REFERENCE_FILE = REFERENCE_DIR / "action_chunk_seed42.npy"
SEED = 42
WARMUP_RUNS = 1
TIMED_RUNS = 3


def resolve_weights(path=None) -> Path:
    """The checkpoint directory (config.json + model.safetensors): ``path`` if given, else
    ``$XVLA_WEIGHTS_DIR``, else the HF snapshot of ``$HF_MODEL`` (default lerobot/xvla-base) at
    ``$TT_WEIGHTS_REVISION`` (a cache hit inside the served image, where tt-model pre-downloads it)."""
    path = path or os.environ.get("XVLA_WEIGHTS_DIR")
    if path:
        return Path(path)
    from huggingface_hub import hf_hub_download

    repo = os.environ.get("HF_MODEL") or "lerobot/xvla-base"
    rev = os.environ.get("TT_WEIGHTS_REVISION") or None
    cfg = hf_hub_download(repo_id=repo, filename="config.json", revision=rev)
    hf_hub_download(repo_id=repo, filename="model.safetensors", revision=rev)
    return Path(cfg).parent


# =============================================================================
# Input synthesis (mirrors xvla config: 3 cameras + state(8) + bart language)
# =============================================================================


def build_synthetic_batch(policy, device: str = "cpu") -> dict[str, torch.Tensor]:
    """Construct a deterministic synthetic input batch matching xvla schema.

    Bypasses the lerobot processor pipeline (tokenizer, normalization) by
    feeding the model exactly the tensors the policy expects post-processing.
    Determinism comes from a fixed torch generator seeded with SEED.
    """
    from lerobot.utils.constants import (
        OBS_LANGUAGE_TOKENS,
        OBS_STATE,
    )

    cfg = policy.config
    g = torch.Generator(device=device).manual_seed(SEED)

    batch = {}

    # Three camera views per config: image (3,256,256), image2 (3,256,256), image3 (3,224,224).
    # The xvla policy resizes each to `resize_imgs_with_padding` (224x224) before stacking,
    # so giving it the raw shapes from the config is correct.
    img_specs = []
    for key, feat in cfg.input_features.items():
        if hasattr(feat, "type") and str(feat.type).endswith("VISUAL"):
            img_specs.append((key, tuple(feat.shape)))
    img_specs.sort()  # deterministic order
    for key, shape in img_specs:
        batch[key] = torch.rand(1, *shape, generator=g, device=device)  # in [0,1]

    # Proprio state
    batch[OBS_STATE] = torch.randn(1, cfg.robot_state_feature.shape[0], generator=g, device=device)

    # Language tokens — bart vocab=51289. The HF config advertises
    # tokenizer_max_length=1024 but `pad_language_to=max_length` in combo
    # with that value produces a sequence (text + image tokens + 3 image
    # views' worth of aux + 30 action + 32 soft prompts) that exceeds the
    # model's own `max_len_seq=512`. Real-world xvla inference uses short
    # natural-language instructions (a few words). We fix the benchmark
    # prompt length at LANG_TOKENS = 16, which keeps the merged sequence
    # comfortably under the cap and represents a typical "pick up X" prompt.
    LANG_TOKENS = 16
    # token id 0 = <s>, 2 = </s>; 1 = <pad>. Fill: <s> + N junk + </s> + pad.
    tokens = torch.full((1, LANG_TOKENS), 1, dtype=torch.long, device=device)
    tokens[0, 0] = 0
    tokens[0, 1:8] = torch.tensor([100, 200, 300, 400, 500, 600, 700])
    tokens[0, 8] = 2
    batch[OBS_LANGUAGE_TOKENS] = tokens

    return batch


# =============================================================================
# Metrics
# =============================================================================


def pearson_correlation(a: np.ndarray, b: np.ndarray) -> float:
    a = a.astype(np.float64).ravel()
    b = b.astype(np.float64).ravel()
    a = a - a.mean()
    b = b - b.mean()
    denom = np.linalg.norm(a) * np.linalg.norm(b)
    if denom == 0:
        return 0.0
    return float(np.dot(a, b) / denom)


def peak_dram_mb_torch_cpu() -> float:
    """torch CPU has no on-device DRAM tracking; report 0."""
    return 0.0


def peak_dram_mb_ttnn() -> float:
    """Read peak DRAM allocation across all devices, in MB.

    Best-effort: ttnn API surface differs across builds. Returns 0 on failure.
    """
    try:
        import ttnn

        # Try several possible APIs; fall through silently.
        for fn_name in ("get_memory_view", "get_memory_info"):
            fn = getattr(ttnn, fn_name, None)
            if callable(fn):
                info = fn()
                # Heuristic: dict with peak fields, else 0.
                if isinstance(info, dict):
                    for k in ("peak_dram_bytes", "peak_dram", "dram_peak"):
                        if k in info:
                            return float(info[k]) / (1024 * 1024)
        return 0.0
    except Exception:
        return 0.0


# =============================================================================
# Backends
# =============================================================================


def load_policy_torch_cpu(weights: Path):
    from lerobot.policies.xvla.modeling_xvla import XVLAPolicy

    torch.set_grad_enabled(False)
    torch.manual_seed(SEED)
    policy = XVLAPolicy.from_pretrained(str(weights))
    policy.eval()
    return policy


def load_policy_ttnn(weights: Path):
    """Load X-VLA with the TT-NN backend.

    Imports `tt/policy.py` by file path (sibling of this benchmark dir) so
    the dispatch is immune to the editable-install of `tt-metal` elsewhere
    on the system overshadowing the `models.experimental.x_vla` namespace.

    On the very first iteration (no tt/policy.py yet) this falls back to
    torch CPU and prints a banner so the metric extraction stays valid.
    """
    import importlib.util

    policy_file = _HERE.parent / "tt" / "policy.py"
    if not policy_file.exists():
        print(f"[bench] no TT-NN backend yet (missing {policy_file}); falling back to torch_cpu.", flush=True)
        return load_policy_torch_cpu(weights)

    try:
        spec = importlib.util.spec_from_file_location("xvla_tt_policy", str(policy_file))
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        return module.load_policy_ttnn(weights)
    except (ImportError, AttributeError) as e:
        print(f"[bench] no TT-NN backend yet ({e}); falling back to torch_cpu.", flush=True)
        return load_policy_torch_cpu(weights)


# =============================================================================
# Driver
# =============================================================================


def run(backend: str, weights: Path) -> dict[str, float]:
    if backend == "torch_cpu":
        policy = load_policy_torch_cpu(weights)
        peak_fn = peak_dram_mb_torch_cpu
    elif backend == "ttnn":
        policy = load_policy_ttnn(weights)
        peak_fn = peak_dram_mb_ttnn
    else:
        raise ValueError(f"Unknown backend: {backend!r}")

    batch = build_synthetic_batch(policy, device="cpu")

    # Warmup
    for _ in range(WARMUP_RUNS):
        torch.manual_seed(SEED)
        _ = policy.predict_action_chunk(batch)

    # Timed runs (deterministic — re-seed each call so output is identical too)
    times = []
    last_actions = None
    for _ in range(TIMED_RUNS):
        torch.manual_seed(SEED)
        t0 = time.perf_counter()
        actions = policy.predict_action_chunk(batch)
        elapsed = time.perf_counter() - t0
        times.append(elapsed)
        last_actions = actions

    chunk_size = policy.config.chunk_size
    mean_time = float(np.mean(times))
    inference_speed = chunk_size / mean_time  # frames per second (action steps / sec)

    actions_np = last_actions.detach().cpu().to(torch.float32).numpy()

    # Reference handling: cache on first ever run; PCC vs cached otherwise.
    REFERENCE_DIR.mkdir(parents=True, exist_ok=True)
    if not REFERENCE_FILE.exists():
        np.save(REFERENCE_FILE, actions_np)
        print(f"[bench] cached new reference at {REFERENCE_FILE}", flush=True)
        accuracy = 100.0
    else:
        ref = np.load(REFERENCE_FILE)
        if ref.shape != actions_np.shape:
            print(
                f"[bench] reference shape {ref.shape} != current {actions_np.shape}; "
                f"treating as 0% accuracy",
                flush=True,
            )
            accuracy = 0.0
        else:
            pcc = pearson_correlation(actions_np, ref)
            # Map PCC in [-1, 1] -> percent in [0, 100]. PCC=1 -> 100, PCC<=0 -> 0.
            accuracy = max(0.0, pcc) * 100.0

    return {
        "inference_speed": inference_speed,
        "accuracy": accuracy,
        "peak_dram": peak_fn(),
        "mean_time_s": mean_time,
        "chunk_size": chunk_size,
    }


def main() -> int:
    p = argparse.ArgumentParser()
    p.add_argument(
        "--backend",
        default=os.environ.get("XVLA_BACKEND", "torch_cpu"),
        choices=["torch_cpu", "ttnn"],
    )
    p.add_argument("--weights", type=Path, default=None,
                   help="checkpoint dir (default: $XVLA_WEIGHTS_DIR, else the HF snapshot of $HF_MODEL)")
    args = p.parse_args()

    results = run(args.backend, resolve_weights(args.weights))
    # Greppable lines:
    print(f"inference_speed={results['inference_speed']:.4f}")
    print(f"accuracy={results['accuracy']:.4f}")
    print(f"peak_dram={results['peak_dram']:.2f}")
    # Extra context (not grepped):
    print(f"# backend={args.backend} mean_time_s={results['mean_time_s']:.4f} "
          f"chunk_size={results['chunk_size']}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())