RooflineAI Infra Field Notes by Forrest Xi

AI Infra · TPU v7

Beating B200 from Scratch: Porting and Optimizing Hy4 Inference on TPU v7

Hy4-preview is a 770B-parameter MoE model with sparse attention and MTP speculative decoding. We ported it from GPU to TPU v7. In benchmarks with 8K input / 1K output at concurrency 1–64, 8 TPU v7 chips delivered 3%–19% higher output throughput than 8 B200 GPUs. Weight loading dropped from 857 seconds to 68, and cold compile from 95 minutes to about 14. This post summarizes the work on all three fronts, and the tuning engine behind it, Curvature Drive.

TL;DR

  • Inference performance: across ten concurrency levels from 1 to 64 with 8000 input / 1000 output tokens, 8 TPU v7 chips delivered 3%–19% higher output throughput than 8 B200 GPUs, with 11%–28% lower time to first token (TTFT).
  • Weight loading: for the 757.88 GiB checkpoint, load time on the same software stack dropped from 857 seconds to 68.5 seconds.
  • Cold compile: startup time with an empty cache dropped from 95.1 minutes to about 14 minutes (mnbt 2048 configuration).
  • Method: the second half of the tuning was driven by Curvature Drive, a recursively self-improving tuning engine. In two weeks it raised throughput at concurrency 32 from 0.365× that of B200 to 1.046×.

Background#

Hy4-preview is an MoE model released by Tencent Hunyuan, with 770B total parameters, 49B activated parameters and 78 layers. The official weights are MXFP8-quantized and total 757.88 GiB. Its attention is DSA sparse attention: for each query, a lightweight indexer first selects the top-2048 KV entries, and attention is then computed over those entries (sparse MLA). Decoding uses MTP speculative decoding, which predicts 3 extra draft tokens per step for the main model to verify together. These two features account for most of the difficulty on the inference system side.

Our goal was to deploy this model on TPU v7 and outperform B200 at the same chip count. At the start, Hy4 was not on the supported list of the TPU inference stack (vLLM + vllm-torchtpu), so we had to begin with the port. Once the model ran, single-stream decoding reached only 1.9–2.3 tok/s, and throughput at concurrency 32 with 8K input was only 0.365× that of B200. On top of that, every startup spent 857 seconds loading weights, plus another 95 minutes compiling whenever the cache was invalidated. Inference performance, weight loading and cold compile are the three problems this work set out to solve.

Test environment#

TPU side GPU side
Hardware 2 × tpu7x-standard-4t, topology 2x2x2, 8 TPU v7 chips in total (16 TensorCores) 1 × a4-highgpu-8g, 8 × NVIDIA B200
Parallelism TP16 + expert parallelism TP8 + expert parallelism
Inference framework vLLM 0.29.0 + vllm-torchtpu (self-maintained branch) Official vLLM hy4-preview image (0.1.dev20485+g0fd2e8d50)
Libraries torch 2.13.0, torch_tpu 0.1.1.dev20260912, JAX 0.10.2, libtpu 0.0.47 torch 2.13.0+cu130, CUDA 13.0, FlashInfer 0.6.17, driver 590.48.01
Attention implementation masked-dense (when KV ≤ 9216), otherwise sparse FLASHMLA_SPARSE
Common settings MXFP8 weights, fp8 KV cache, MTP k=3 Same as left

Each TPU v7 chip has two TensorCores, and each one is a separate device, so 8 chips give 16 devices and a tensor parallel degree of 16.

Inference performance: from 2 tok/s to beating B200#

The gains in inference performance came in roughly three stages, each with a different bottleneck.

The first bottleneck was that the model was not being compiled at all. Hy4's model class lacked the @support_torch_compile decorator, so all 78 layers ran on TPU with op-by-op dispatch, and the device spent 94% of its time waiting on the host. After we added the decorator to both the main model and the MTP draft model and removed several constructs that blocked whole-graph compilation, single-stream throughput on short prompts rose from 2.14 to 48.57 tok/s. With 4000 input / 1000 output, enabling MTP and compiling the draft model brought it to 100.11 tok/s.

Next, the bottleneck moved to the prefill of sparse attention. Each of the 16 devices held a full copy of the KV, and to fetch at most 2048 distinct rows, the sparse MLA gather materialized 4.19 million rows. TTFT at 4K input was 4.37× that of B200. We solved this with the masked-dense method from upstream vllm-torchtpu: write the top-k result into a bitmap and compute masked dense attention, which is semantically equivalent to sparse attention. The upstream implementation only targeted deployments with 64 attention heads and does not activate under TP16 with 4 heads per device. We added attention sink support, made the enablement check use the head count after sharding, and refit the cost model used for path selection. After these changes, TTFT at 4K input dropped from 1338.81 ms to 729.04 ms.

Last came high concurrency. At higher concurrency, a single scheduler step contains several speculative decoding requests and a chunk of prefill at the same time; we call this a mixed step. At concurrency 32, one mixed step holds 32 speculative decoding requests. With 8K input, many of these mixed steps fell back to the sparse path, along with several data handoffs that XLA lowered into whole-block copies. The optimizations in this stage were: raising the masked-dense KV length cap from 6144 to 9216 so that mixed steps with 8K input no longer fall back to sparse, which alone raised throughput at concurrency 32 by 40.6%; computing the speculative decoding verification window as a separate segment; reusing the attention bitmap across layers and handing it off directly from top-k; enabling sequence parallelism in mixed steps; turning KV writeback into DMA that is contiguous per sequence; and having prefill attention truncate the KV loop at the causal position. Within two weeks, throughput at concurrency 32 went from 0.365× that of B200 to 1.046×.

Output throughput (tok/s), 8000 input / 1000 output; labels on the left are the concurrency. TPU numbers are from the final version, averaged over two runs per level (three at c32); B200 is one run per level.

At all ten concurrency levels, TPU output throughput is higher than B200, by 3% (c64) to 19% (c2). TTFT is 11%–28% lower, and time per output token is lower at every level as well. When citing these numbers, keep several conditions in mind: the load is fixed-length requests of 8000 input / 1000 output; the TPU recipe's maximum context is 10240 (262144 on B200), and when any request in a batch has KV beyond 9216, TPU mixed steps fall back to the sparse path; both sides use fp8 KV cache, which the TPU sparse attention kernel requires; B200 was measured only once per level. In addition, by engine statistics, TPU emits an average of 3.03 tokens per decode step versus 2.54–2.59 on B200. If this comes from a real difference in acceptance rate, the lead shown above holds. If part of it comes from the two sides handling the end-of-sequence token (EOS) differently, the lead is overstated; a rough estimate puts that part on the order of 1 percentage point (inference).

This set of methods suits DSA / sparse MLA models deployed with tensor parallelism on TPU, and pays off most for workloads that combine long inputs, speculative decoding and medium-to-high concurrency. The full story is in the first post of this series:

From 2 tok/s to Beating B200: Optimizing Hy4 Inference on TPU v7

Weight loading: from 857 seconds to 68#

Hy4 is deployed on two hosts with 16 ranks. With vLLM's default loading path, every rank reads the full 757.88 GiB checkpoint and then slices out its own share online. The weights sit on Lustre shared by both hosts, and two factors slow the reads further. First, vLLM only prefetches from network file systems when the whole checkpoint fits within 90% of available memory; Hy4 sits right around that line, and at one point our pods skipped prefetching because of it. Second, even with prefetch enabled, files are assigned by global rank, so with two hosts each host prefetches only half.

Our approach is to write the weights to disk after quantization conversion, already sliced per rank, producing one cache per rank (vLLM's sharded_state format), so that each rank reads only its own ~47 GiB. Upstream's ShardedStateLoader has no prefetching and does not account for quantization post-processing changing the model structure. We added prefetching to it and fixed 8 read-back issues.

Weight loading time (seconds), vLLM 0.29 software stack, taken from Loading weights took in the vLLM log. Later runs measured between 65 and 71 seconds.

On the same software stack, load time dropped from 857 seconds to 68.5 seconds, about 12.5× faster. This cache suits deployments with multi-host tensor parallelism, checkpoints close to or larger than single-host memory, and frequent restarts. The cost is that the cache is tied to the parallel configuration, max-model-len, KV data type, speculative decoding configuration and code version, and a change in any of them requires a rebuild. The full story is in the second post of this series:

Hy4 Weight Loading on TPU v7: From 857 Seconds to 68

Cold compile: from 95 minutes to 14#

TPU programs are compiled per input shape. At startup, vLLM compiles the main model for every token bucket, then the sampling, draft model and speculative decoding warmup programs, and only opens the service once all of them are done. Compiled results are cached, but the cache is invalidated whenever the configuration, image or version changes. One Hy4 cold compile takes 95.1 minutes.

We measured compile volume on a 4-layer proxy model and extrapolated to 78 layers, and found that the main cost was not in the compiler itself but in the graphs fed into it. In the sparse MLA kernel, a Python loop over 64-token chunks was unrolled verbatim during tracing, so program length grew linearly with the bucket; by extrapolation this accounts for 74–93% of each worker's compile volume. The attention sink was written into the program as a closure constant, so the same shape was compiled into multiple copies, one per rank. To address these two points, we changed the chunk loop to fori_loop and made the attention sink a runtime input, then adopted the upstream approach of replaying compiled artifacts across ranks, along with several compile-time optimizations.

Cold compile time (minutes), empty cache, from pod creation to service ready. The intermediate steps were measured at mnbt 8192 and the first and last rows at mnbt 2048, so the rows cannot be subtracted from one another directly.

Cold compile dropped from 95.1 minutes to about 14 minutes (two runs with mnbt 2048, at 819 seconds and 842 seconds). On GLM-5.3, toggling only the same "bucket splitting + replay" switch (the two contributions cannot be separated) cut the time from weights loaded to service ready from 959 seconds to 409 seconds. These problems are common when deploying models with custom Pallas kernels on vllm-torchtpu. A simple check is to count how many programs in the compile cache directory correspond to the same shape. The full story is in the third post of this series:

95 Minutes to 14: Cold Compile Optimization for Hy4 on TPU v7

The replay above can only reuse compiled artifacts within the same DP index. For pure data parallel deployments, or mixed tensor and data parallel deployments, we went further and relaxed replay to look up artifacts across DP ranks. On Qwen3.5 DP8, the time from weights loaded to service ready dropped from 741 seconds (single run) to 270 seconds (mean of three runs). See the fourth post of this series:

Cold Compile Optimization, Part 2: Cross-DP Compile Reuse for Data-Parallel Deployments on TPU

Later, after mixed-step sequence parallelism was turned on and mnbt raised to 8192 to cut time to first token, cold compile climbed back to 48 minutes. The root cause was that the sequence-parallel slice wrote the rank index into the traced graph, which disabled cross-rank replay; reading the rank at run time brought it down to 17.7 minutes. See the fifth post of this series:

Cold Compile Optimization, Part 3: Cross-Rank Reuse for Sequence-Parallel Buckets, 48 Minutes to 17.7

Curvature Drive: a recursively self-improving tuning engine#

The second half of the performance work (September 24 to October 8) was driven by Curvature Drive, a tuning engine we built. It is an agent engine for TPU inference tuning that can recursively self-improve (Recursive Self-Improvement, RSI): besides running the tuning, it improves its own process, evaluation methods and knowledge base at the end of each round, so the next round starts from a better position.

Curvature Drive brings together domain expertise on TPU. It has a built-in, continuously growing knowledge base, currently 280 experience cards, covering TPU hardware specs (the mapping between chips and TensorCores, HBM and VMEM capacity, ICI topology, and so on), Pallas kernel development techniques, XLA compilation and caching behavior, how to capture and read profiles, methods for breaking down device time by the nature of the bottleneck, and known pitfalls of individual models on TPU. Each card records the software environment it was validated in. When the environment changes, the card is flagged for re-review and serves only as a prior; it cannot by itself rule out a direction.

It also has an explicit exploration process. Before proposing any optimization hypothesis, the engine runs a frontier scan: it checks upstream code and issues / PRs, official documentation, and the source code and papers behind strong kernels, to first answer "how have others solved this bottleneck". It also periodically summarizes new developments in the external community. The experiment queue has an exploration quota: at least 1 in every 5 experiments is exploratory, dedicated to revisiting directions that experience says are not worth pursuing, or that were rejected under an older environment.

In execution, each optimization is split into fixed phases, and every phase has a machine-checked gate. The goal and comparison methodology are frozen first, and the noise of repeated measurements is measured; production profiles then break down the time of each scheduler step to the operator level. Before any hypothesis goes on hardware, its upper bound on gains is estimated and its falsification condition written down. Predictions must be registered before the data arrives, and a change must first be shown to actually take effect before its effect is evaluated. Adopted changes must pass three rounds of accuracy evaluation and a long-context verbatim comparison, and are baked into the image and the deployment recipe. Independent reviewer agents check the direction and look for counterexamples at key points, and the author has to respond to each item.

At the end of each round, the engine writes reusable lessons into new experience cards, and conclusions overturned by later evidence are registered as retracted rather than deleted. Changes to the procedure itself are submitted as proposals and merged after human review, then evaluated by replaying a fixed set of historical decision questions to confirm that the change makes the engine stronger rather than weaker. On 12 historical decision questions, the group using Curvature Drive scored 93.3%, against 82.5% for the group without it.

On Hy4, over two weeks, 14 versions and 38 experiments, Curvature Drive raised throughput at 8K/1K and concurrency 32 from 0.365× that of B200 to 1.046×, and 23 new experience cards entered the knowledge base at the end of this round. It also caught several problems that "looked faster". A change that gave a 7.8% speedup came in low on MMLU in two rounds; it was rolled back, re-measured over three rounds with alternating restarts, and re-adopted only after no regression was confirmed, and the long-context verbatim comparison in the same period also uncovered an indexer KV write error. A version that had already been baked in gave abnormal readings in the formal benchmark; the cause turned out to be a mismatched compile cache loaded after the pod placement changed, and the version was retracted right away.

An introduction to Curvature Drive and live progress on each task are at https://rsi.maxwell-x.dev.

Series#

  1. From 2 tok/s to Beating B200: Optimizing Hy4 Inference on TPU v7
  2. Hy4 Weight Loading on TPU v7: From 857 Seconds to 68
  3. 95 Minutes to 14: Cold Compile Optimization for Hy4 on TPU v7
  4. Cold Compile Optimization, Part 2: Cross-DP Compile Reuse for Data-Parallel Deployments on TPU
  5. Cold Compile Optimization, Part 3: Cross-Rank Reuse for Sequence-Parallel Buckets, 48 Minutes to 17.7

Data as of 2026-10-08; the cold compile part was updated on October 13 with the fifth post.