Getting Nemotron-3.5-Lightning running on four TPU chips

David Cardozo · Code on GitHub

I got to know Nemotron-3-Nano while co-authoring Opening the Black Box, a SciPy paper on how the model chooses which tool to call. For #tpu-sprint, I used my compute credits from the TPU Builders community to try serving Nano’s newer sibling, Nemotron-3.5-Lightning, on four TPU chips.

I figured getting Lightning to run well would take some care. Each request has its own Transformer attention cache and Mamba-2 state. In a mixture-of-experts layer, the model routes each token to a few of 128 small expert networks, so two tokens in a row can need different weights. Before I could measure how fast it ran, I had to get a token back.

I gave tpu-agent-suite a one-line brief.

Onboard nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16 to TPU v6e (4 chips), benchmark, serve.

Sixteen seconds into the first attempt, vLLM had found the model and started a worker on each of the four TPU chips. Then it stopped. On TPU, vLLM’s generic mixture-of-experts (MoE) configuration check rejected Nemotron’s non-gated experts (is_act_and_mul=False). At each expert layer a token picks 6 of 128 experts. Each expert has a single up projection followed by squared ReLU, with no second “gate” half to multiply against.

That check turned out to be the first of three places where the TPU path assumed gated experts. Later I found the weight loader padding expert weights as if they came in two halves, and the grouped matrix-multiply kernel expecting to split them. Before patching vLLM, I wanted a second implementation I could trust, something to test the vLLM changes against. So I built one with the suite’s agents and reviewed every patch.

The first server generated seven tokens a second

The onboarding agent split the experts across four chips, 32 experts per chip, and put a small FastAPI server in front of the model. A known-answer check came back with “Paris.” On the first streaming benchmark, with a 1,024-token prompt and 128 output tokens, the median request took 218 ms to produce its first token. Each subsequent token took about 143 ms. At that pace the full response took more than 18 seconds.

That split between the first token and the rest told me what to profile. Processing the prompt happens once. The decode step runs over and over, once for every subsequent token. Saving one millisecond there would be worth about 127 ms on this request.

An XProf capture of eager decoding counted 2,357 execution launches per output token. It also found about 298 calls to aten::item per token, where the host wanted a scalar back from the device. Before writing a faster matrix multiply, I wanted to know what would happen if the host handed the TPU a whole decode step at once.

We preallocated the attention and Mamba caches and compiled the step with torch.compile(backend="tpu", dynamic=False, fullgraph=True). Fixed cache shapes let the executable accept the next token without rebuilding the graph as the sequence grew. The first compiled version brought streaming time per output token down to 65.86 ms. The next target was now visible inside that graph. The expert code, carried over from the eager version, was gathering and transposing weights for all 32 local experts, even though a token needed only six of the layer’s 128. Computing all 32 local experts densely, with no gather, brought it to 18.61 ms. A Pallas kernel that reads only the selected experts came next.

EagerXProf trace of one TPU chip over 236 ms during eager decoding: short, fragmented launches for about one token, followed by an idle gap.
CompiledXProf trace of one TPU chip over the same 236 ms with a compiled decode step: 13 back-to-back compiled steps.
The same 236 ms on one chip. Eager PyTorch (top) spends it on about one token across hundreds of separate launches, then sits idle. A later compiled build with all 32 local experts (bottom, the third point in the chart below) produces 13.
Median milliseconds per output token for six builds. Eager PyTorch 143.00, first compiled step 65.86, “Dense local experts, no gather” 18.61, sparse Pallas kernel 6.22, sharded experts 3.76, fully sharded decoder 1.92.
Farther left means a shorter wait between tokens. Each point measures a whole build. The 3.76 ms run combined expert sharding with host thread pinning and pipelined token readback.

What four chips could tell me about one slow token

A Pallas kernel for the routed experts and a change in how we split their weights brought decode into the single-digit milliseconds. Originally, each chip owned whole experts. In the newer layout, each chip owned a slice of every expert. All four chips could work on the same selected experts and then sum their partial results.

At that point the average hid a puzzle. Short bursts of generation had a rhythm. In most groups of three decode steps, two were quick and one was slow. Was Python late submitting the next token? I lined up the start and end of the compiled module on all four chips.

Each column is the same decode step across four chips. In this intermediate build, step 3 took 4.80–5.23 ms across the chips. Two steps later, all four finished in about 3.01 ms.

Over the 11 groups after the initial group, the slow steps averaged 4.86 ms and the other steps averaged 3.19 ms. The gaps before those groups’ slow and other steps averaged 7.69 and 7.68 microseconds, respectively. A difference of a hundredth of a microsecond between launches couldn’t explain an extra 1.68 milliseconds inside the module. On a slow step, one chip spent longer reading or using weights, and the others waited at a reduction.

The pattern disappeared after we also sharded the dense layers. On a fresh machine, a separate run recorded zero slow steps across 1,270 steady-state steps under our original detection rule. The trace located the delay on the device. What made one chip periodically slow down remains an open question.

Why I stopped trusting “Paris”

“Paris” told me the server could answer one question. I wanted to know whether my decoder computed the same thing as the reference. I started by asking whether the reference agreed with itself.

We ran the Hugging Face model in two modes in our four-chip test harness, with the custom Pallas decode kernel disabled. One mode processed the sequence in chunks. The other advanced its Mamba state one token at a time. Given the same token history, they picked the same next token at 754 of 769 positions. At the first MoE layer, they picked exactly the same six experts at only 411 of those positions.

I had initially put the difference down to BF16 rounding. On CPU we repeated the check on a 625-token prompt with the checkpoint’s first Mamba layer and the router that follows it. For the FP64 run, the test replaced the implementation’s hard-coded FP32 casts with FP64 casts. The two paths still chose the same six experts at just 55.8% of decode positions, exactly what we saw in FP32. In Transformers 5.24.1, the chunked Mamba path clamped the learned time step, dt, to a minimum of 0.001. The recurrent path applied no such floor. The checkpoint’s configured lower limit was zero.

We restored the checkpoint setting on each of the 23 Mamba mixers before running the reference and the served model.

blk.mixer.time_step_limit = tuple(model.config.time_step_limit)

With that setting, first-layer router agreement reached 100% in the CPU FP64 test. The same test passed at the upstream commit that made the Nemotron-H mixer read the configured limit directly. Builds containing Transformers #48989 already use it.

Back on TPU, the two paths picked exactly the same six experts at 748 of 769 positions (97.27%) in the first routed layer. That layer showed the largest gain. I checked the other 22 to see how far it carried.

Each pair of marks represents one routed layer. A match means the chunked and recurrent paths selected the same set of six experts. Each run used six prompts and 769 evaluated positions, with both paths following that run’s reference tokens. Across the 23 layers, mean agreement rose from 61.11% to 81.35%.

The reference fooled me a second time on three prompts of nearly 30,000 tokens each. We scored 256 next-token predictions per prompt against native Hugging Face with matmul precision set to HIGHEST. The custom decoder matched 732 of 768 and the native BF16 comparison run matched 748. It looked as though the port had lost accuracy on long inputs. When we checked the errors by position, they spiked every 2,048 tokens, exactly the chunk size our reference harness used to feed the prompt.

The fault was in our code for running Hugging Face across four chips. Three pipeline stages counted cached tokens from layers they didn’t own. On later full chunks, those stages supplied no mask, so their attention fell back to a default that kept only the first 2,048 cached positions. We built the mask from the actual token positions, then ran the reference against itself at chunk sizes of 2,048 and 4,096. The boundary spikes disappeared. With the corrected reference, the same custom decoder matched 747 of 768 predictions and native BF16 matched 746. Before I score a decoder against a chunked reference now, I change the reference’s chunk size and check its answers at the boundaries.

I also wanted a test a reader could interpret without inspecting logits. On the same 100 GSM8K math questions, using greedy decoding with thinking disabled, the optimized path and native Hugging Face each scored 93/100. Both got 92 questions right. Each solved one the other missed, and their full generated token sequences matched on only 44 questions.

Of 100 paired GSM8K questions, 44 had identical generated token sequences, 53 had different sequences ending in the same number, and 3 had different final numbers.
The two decoders returned the same final number on 97 questions. Of the three where their answers differed, each decoder got one right and both missed the third.

A different wait: the first token

An 8,192-token prompt exposed a different wait. The server spent 1,464 ms getting to the first token. XProf showed the same expert down-projection weights being transposed once per 256-token chunk. Across 32 chunks and 23 MoE layers, one chip did 736 transposes for a single prompt. Reading and writing those copies accounted for about 518.6 GB of memory traffic on that chip.

XProf trace of two expert chunks from an 8,192-token prefill. Each chunk shows a convert step, a copy.3 transpose taking about half the chunk, and a fusion step.
Two expert chunks from an 8,192-token prefill on one chip. Each spends about 0.62 ms in copy.3, a transpose of the same down-projection weights, 736 times per prompt (456.8 ms, 31% of prefill).

Moving the transpose outside the chunk loop let each layer reuse one copy across all 32 chunks. That one edit took median time to first token from 1,442 to 1,001 ms, the largest individual prefill saving we measured. Larger chunks cut the number of passes over the weights. Later builds split the dense layers across chips and grouped prompt tokens by the experts they selected. The final build returned the first token in 164 ms, about 9× faster than the first 8K build.

Median time to first token on an 8,192-token prompt, in milliseconds. Initial build 1,463.62, last-token logits 1,441.56, transpose once per layer 1,001.13, 2,048-token chunks 700.13, sharded dense layers 420.55, compiled segments + grouped experts 253.50, final build 163.83.
Each point is the median of five warm requests with 8,192 input tokens. The three runs marked with an asterisk requested 16 output tokens. The other four requested 1,024. Timing ends when the first token arrives.

What one user actually sees

The final custom decoder kept BF16 weights and sharded the experts, dense projections, and vocabulary output across the four chips. It also handled the prompt and the next token differently. During prefill, it grouped the work for all prompt tokens assigned to the same expert. During decode, the Pallas kernel needed to handle the experts selected by just one token.

We reran the warm streaming benchmark on a fresh four-chip v6e VM. The custom server completed five requests at each shape. The original working vLLM fork completed 32 at the longer shape on the same four-chip v6e configuration. Both served one request at a time with BF16 weights. The table gives medians with compilation and startup outside the timed requests.

The custom rows used our streaming harness with a fixed prompt and an in-process token callback. The vLLM row used vllm bench serve with random prompts over HTTP.
Server Prompt → output tokens Time to first token Time per subsequent token End-to-end latency
Custom 1,024 → 128 52.42 ms 1.92 ms 296.23 ms
Custom 8,192 → 1,024 163.83 ms 1.91 ms 2.12 s
Initial vLLM fork 8,192 → 1,024 269.25 ms 4.74 ms 5.12 s

For the longer request, the custom server started streaming in about a sixth of a second. Our harness measured about 524 tokens/s during decode for that one user. The vLLM figure gave me a starting point for a different question. Where would its time go when sixteen requests shared one decode step?

Back to vLLM

Anurag Prakash’s open Mamba-2 PR, #3422, supplied the TPU implementation for the recurrent layers. With that work and my three patches for non-gated experts, the fork could load the checkpoint. The patches covered the vLLM configuration check (#3911), expert weight padding (#3910), and the squared-ReLU expert kernel (#3909). Once vLLM core allows non-gated experts on TPU, vllm-torchtpu can drop the configuration workaround.

With 16 requests in flight, each Mamba layer copied recurrent state one sequence at a time. XProf showed 46 compiled loop instructions for those copies, one gather and one scatter in each of 23 layers. I batched the state reads and writes across requests in draft PR #1 on my fork, removing all 46 loops. In a matched serving benchmark, aggregate output rose from 883 to 1,179 tokens/s, though a lone request in a large bucket waited about 35 ms longer for its first token. In a separate greedy replay of 4,096 tokens, every generated token and its logged probability matched the previous implementation.

BeforeXProf trace of a 13 ms window of vLLM decode with 16 requests. Mamba state copies are highlighted: 46 while loops spread across the step.
AfterThe same 13 ms window after batching the state copy. Only thin highlighted slivers remain.
The same 13 ms window of decode with 16 requests, before (top) and after (bottom), with the Mamba state copy highlighted. Before: 46 while loops copying state one request at a time (5.5 ms per step). After: two batched operations per layer (0.71 ms). The captures used different prompts, so compare the highlighted work, not the step length.
Follow request B in slot 5. Both versions bring B’s saved state into the working batch, update it with B’s current token, and write it back to slot 5. The animation illustrates three requests. The final chart uses the measured 16-request concurrency benchmark with 128 requests in total, 8,192 input tokens, 1,024 output tokens, and a 2,048-token scheduler budget.

Why one token fetched twelve experts

With a single request in flight, a smaller mystery showed up. The token needed six experts, yet a layer typically read twelve. The decode bucket held 16 rows. The 15 padding rows all selected the same six experts, with only occasional overlap with the real token’s six. Across 322 layer and step measurements, padding raised the mean from 6.00 to 11.64 distinct experts. On the vectorized build, enabling TPU_MOE_SKIP_PADDED_TOKENS=1 reduced the median gap between streamed tokens from 4.84 to 4.42 ms, a 0.42 ms saving at one request.

At 16 concurrent requests all 16 rows were real, so skipping padding had nothing to remove. Each MoE layer selected 47.19 distinct experts on average. Over the 23 layers that amounts to an estimated 5.98 GB of BF16 expert weights per chip per decode step. XProf timed the expert matrix multiplies at 4.07 ms of an 8.11 ms step. Dividing the weight bytes by the kernel time gives about 1.47 TB/s, roughly 90% of peak memory bandwidth while those kernels ran.

At that bandwidth, reducing the bytes fetched per expert offers more headroom than tuning the same BF16 matrix multiply. It also changes how I read published GPU numbers for this model. The runs targeted the same prompt and output lengths with very different setups.

Aggregate output tokens/s · median time between tokens. Both benchmarks targeted 8,192 input and 1,024 output tokens. The TPU runs used 32, 128, and 256 requests, respectively. Lambda’s published GPU benchmark used 512 at each level.
Concurrent requests 4× TPU v6e, BF16 (vLLM fork, after these fixes) 1× B200, NVFP4 1× H100, W4A16 fallback
1 212 tok/s · 4.42 ms 361 tok/s · 2.6 ms 326 tok/s · 2.9 ms
16 1,179 tok/s · 9.62 ms 2,132 tok/s · 5.8 ms 1,438 tok/s · 9.9 ms
64 1,787 tok/s · 20.37 ms 3,556 tok/s · 15.9 ms 2,635 tok/s · 23.2 ms

NVFP4 stores four-bit weight values plus scales, while our TPU run fetched 16-bit BF16 weights. I’d match weight format, prompt and output lengths, and concurrency before drawing a hardware conclusion from tokens/s.

When to isolate a compile cache and when to reuse it

Comparing precision settings. On torch_tpu==0.3.0, one arm of an A/B test silently ran the other arm’s compiled program. The disk cache key and the in-process compiler guards both missed the TPU matmul precision setting. To repeat the test on that version, we started each arm in a fresh process with its own TORCHINDUCTOR_CACHE_DIR. Upstream tracked this in torch_tpu #6393 and fixed both cache checks.

Restarting the same server. Our vLLM server took 588 s to start cold. We kept its compilation caches on disk, cleared the in-memory cache, and restarted the same configuration. It was ready in 71 s. Compilation itself fell from 426 s to 6.8 s. Those are startup costs, paid before the first timed request.

What I'd do on day one

If I repeated this project, I’d save the first streaming request and a device trace as soon as the model could generate. I’d also keep an independent reference close enough to instrument. On day one I’d compare its two Mamba paths on the same token history, then double the prefill chunk size and inspect the boundary tokens. A server could answer “Paris” with either bug still in place.