TPU MLIR lowering fails with illegal `tpu.reshape` on TPU7x
Summary
XLA compilation fails during TPU MLIR lowering with failed to legalize operation 'tpu.reshape' when compiling a large MoE model on TPU7x. The error is size-dependent: precompilation succeeds for num_tokens <= 1024 but fails at num_tokens >= 2048.
Environment
- JAX version: 0.9.2
- Hardware: TPU7x-8 (8 chips, 96 GB HBM/device)
- OS: Linux 6.8.0-1015-gcp (Ubuntu)
- Python: 3.12
Model & Configuration
- Model:
Qwen/Qwen3-VL-235B-A22B-Thinking-FP8(Qwen3 MoE, FP8 block-quantized checkpoint) - Architecture:
Qwen3VLMoeForConditionalGeneration - Key model params: 128 experts, 8 experts/token, hidden_size=4096, moe_intermediate_size=1536, 94 layers
- Serving config: tensor_parallel_size=8, max_model_len=16384, chunked_prefill enabled
- MoE backend: Tested with both GMM_EP and GMM_TP — same error with both
- Quantization: FP8 compressed-tensors with block quantization (also reproduces with BF16)
Error
jax.errors.JaxRuntimeError: INTERNAL: failed to legalize operation 'tpu.reshape':
%470 = "tpu.reshape"(%469) : (vector<2x16xbf16>) -> vector<16x2xbf16>
at location: loc(unknown)
The MLIR operation involved:
%470 = "tpu.reshape"(%469) : (vector<2x16xbf16>) -> vector<16x2xbf16>
... additional diagnostics were skipped.
The error occurs during XLA precompilation of the model's forward pass (not during weight loading or inference). The JAX trace and HLO graph construction succeed — the failure is specifically in the MLIR lowering to TPU instructions.
Full Stack Trace
File "tpu_inference/worker/tpu_worker.py", line 421, in compile_or_warm_up_model
self.model_runner.capture_model()
File "tpu_inference/runner/tpu_runner.py", line 635, in capture_model
self.compilation_manager.capture_model()
File "tpu_inference/runner/compilation_manager.py", line 96, in capture_model
self._precompile_backbone_text_only()
File "tpu_inference/runner/compilation_manager.py", line 347, in _precompile_backbone_text_only
self._precompile_backbone_helper(...)
File "tpu_inference/runner/compilation_manager.py", line 250, in _precompile_backbone_helper
self._run_compilation(...)
File "tpu_inference/runner/compilation_manager.py", line 83, in _run_compilation
result = fn(*args)
File "tpu_inference/runner/compilation_manager.py", line 239, in model_fn_wrapper
kv_caches, hidden_states, _ = self.runner.model_fn(...)
jax.errors.JaxRuntimeError: INTERNAL: failed to legalize operation 'tpu.reshape':
%470 = "tpu.reshape"(%469) : (vector<2x16xbf16>) -> vector<16x2xbf16>
Analysis
- The
vector<2x16xbf16> -> vector<16x2xbf16>is a tile-level transpose at the TPU register level - Dimensions likely correspond to:
2= K+V or gate+up fusion,16= num_experts / TP = 128 / 8 - Running with
--dtype bfloat16produces the same error — this is NOT FP8-specific - Error reproduces with both GMM_EP and GMM_TP MoE backends — not MoE-backend-specific
- Error is size-dependent: precompilation of backbone with
num_tokens= 16, 32, 64, 128, 256, 512, 1024 all succeed; fails atnum_tokens = 2048 - The
FUSED_MOEbackend (Pallas kernel) may work as a workaround but is not supported for FP8 quantization
Steps to Reproduce
pip install vllm # with TPU support
pip install jax==0.9.2 jaxlib==0.9.2
vllm serve Qwen/Qwen3-VL-235B-A22B-Thinking-FP8 \
--tensor-parallel-size 8 \
--max-model-len 16384 --max-num-batched-tokens 16384 \
--enable-chunked-prefill \
--gpu-memory-utilization 0.92 \
--max-num-seqs 32 \
--host 0.0.0.0 --port 8000 \
--trust-remote-code
Size-Dependent Compilation Results
Precompiling backbone with different num_tokens values:
| num_tokens | Result | Compilation Time |
|---|---|---|
| 16 | ✅ Success | 57s |
| 32 | ✅ Success | 58s |
| 64 | ✅ Success | 58s |
| 128 | ✅ Success | 60s |
| 256 | ✅ Success | 62s |
| 512 | ✅ Success | 75s |
| 1024 | ✅ Success | 75s |
| 2048 | ❌ tpu.reshape failure |
41s (failed) |
Setting --max-num-batched-tokens 1024 avoids the error but severely limits prefill throughput for long-context workloads.
Note that 2x16 -> 16x2 reshape is not the same as a transpose. If you truly intended to perform a transpose, please do that. Running vllm is not an acceptable repro for us, as the error comes from a kernel buried deep in the project. We can try to write a small test that causes teh same issue but I can't explain why 2048 is a magic number in the context of vllm.
Thank you Adam for the guidance. After investigation, I guess I identified the root cause — this is not a JAX/compiler bug.
The issue is in my forked codebase (tpu-inference): when an FP8 Pallas grouped matmul kernel (gmm_v2) and a sparse core gather-reduce kernel (sc_gather_reduce) are compiled together in the same XLA computation on TPU 7x-8, the compiler generates the illegal tpu.reshape due to incompatible memory layouts between the two kernels.
The size-dependent behavior (num_tokens >= 2048) was because sc_gather_reduce only activates above a threshold.
Fix: skip the sparse core gather-reduce path when using FP8 weights.
Closing — apologies for the noise. EOF
Correction: Root cause is in tpu-inference's gather_reduce.py, not libtpu
The fix is commit fe442ba2 in https://github.com/vllm-project/tpu-inference/pull/2138 ("fix broken gather sc kernel"), which updates MLIR layout attributes (StridedLayoutAttr → #tpu.tiled), pack format (compressed → interleaved), and removes an illegal vector.shape_cast that was the source of the vector<2x16xbf16> -> vector<16x2xbf16> reshape.
This is not a JAX or libtpu bug.
Sounds good. This kind of reshape will actually be supported in next release.