#!/usr/bin/env python3 """Time the logits step alone (compute_logits = lm_head GEMM + logits processor) and the embedding lookup of a served LFM3 checkpoint at decode shape (1 token) and batch 8, eager, XPU-synchronized median. python3 tests/head_timing.py MODEL_DIR (inside the image, VLLM_ALLOW_INSECURE_SERIALIZATION=1) """ import sys, time import torch from vllm import LLM def bench(m): out = {} for n in (1, 8): h = torch.randn(n, m.config.hidden_size, dtype=torch.bfloat16, device="xpu") ids = torch.randint(0, m.config.vocab_size, (n,), device="xpu") for name, fn in {"compute_logits": lambda: m.compute_logits(h), "embed": lambda: m.model.embed_input_ids(ids)}.items(): for _ in range(20): fn() ts = [] for _ in range(200): torch.xpu.synchronize(); t0 = time.perf_counter(); fn(); torch.xpu.synchronize() ts.append(time.perf_counter() - t0) out[f"{name} n={n}"] = f"{sorted(ts)[len(ts) // 2] * 1e3:.3f} ms" return out if __name__ == "__main__": llm = LLM(sys.argv[1], enforce_eager=True, gpu_memory_utilization=0.3, max_model_len=4096) with torch.no_grad(): print(llm.apply_model(bench)[0])