Skip to content

Instantly share code, notes, and snippets.

@force_accelerate_hooks("conv1d")
def forward(
self,
hidden_states: torch.Tensor, # [B,T,H] = [B,T,4096]
cache_params: Cache | None = None,
attention_mask: torch.Tensor | None = None, # typically [B,T] for GDN padding mask
**kwargs: Unpack[TransformersKwargs],
):
# Part A — Input and cache
# Zero out padded tokens, if any; the shape is unchanged.
# operatorx/runners/tpu/backends/tokamax.py
from tokamax._src.ops.experimental.routing_moe.layer import modular
def _scores(logits, scoring): # like moe.py::_scores
return {"softmax": lambda l: jax.nn.softmax(l, -1),
"sigmoid": jax.nn.sigmoid,
"sqrtsoftplus": lambda l: jnp.sqrt(jax.nn.softplus(l))}[scoring](logits)
def _route(x, p, rt, K): # router GEMM + topk
logits = (x @ p["gate"]).astype(jnp.float32)
Qwen3.8: 2002 vs 1995 tok/s.
Serving benchmark (160 concurrent, 480 prompts, 8192 in / 1024 out):
┌────────┬──────────────┬───────────┬────────────┬───────────────────────────┐
│ run │ output tok/s │ mean TPOT │ median ITL │ job │
├────────┼──────────────┼───────────┼────────────┼───────────────────────────┤
│ base 1 │ 2320 │ 59.40 ms │ 38.54 ms │ j-4d7d0008-937a-4c78-a380 │
├────────┼──────────────┼───────────┼────────────┼───────────────────────────┤
│ base 2 │ 2283 │ 60.31 ms │ 38.71 ms │ j-2cbe646a-2fab-4e2e-8706 │
├────────┼──────────────┼───────────┼────────────┼───────────────────────────┤
│ PR 1 │ 2264 │ 60.62 ms │ 38.01 ms │ j-91678913-4e5e-4677-bac1 │
Fused MoE kernel time (rank 0, same kernel fused_ep_moe_v2_fp4w_g16_c128_nb1):
┌────────────────┬───────┬──────────┬──────────┬───────┬───────┐
│ │ calls │ mean │ median │ p10 │ p90 │
├────────────────┼───────┼──────────┼──────────┼───────┼───────┤
│ base 74fe44178 │ 2148 │ 141.0 µs │ 141.9 µs │ 126.7 │ 152.2 │
├────────────────┼───────┼──────────┼──────────┼───────┼───────┤
│ PR c261b63e7 │ 2220 │ 137.7 µs │ 139.1 µs │ 124.2 │ 149.5 │
└────────────────┴───────┴──────────┴──────────┴───────┴───────┘
On DeepSeek-V4-Pro, PR 5 doesn't change accuracy or speed.
MMLU-Pro (full set): the same within noise. The fused MoE kernel was used in both runs ("Fused EP MoE armed", block-512 W4A8).
┌─────────────┬───────────────────────────┬───────────────────────────┐
│ │ base 74fe44178 │ PR c261b63e7 │
├─────────────┼───────────────────────────┼───────────────────────────┤
│ exact_match │ 0.7862 ± 0.0036 │ 0.7857 ± 0.0036 │
├─────────────┼───────────────────────────┼───────────────────────────┤
│ job │ j-e9694b8c-fd87-4ae1-9c88 │ j-d7f3cca3-a2a1-49aa-90f4 │
Both MMLU runs have finished, and they scored exactly the same:
┌─────────────┬───────────┬───────────────────────────┬─────────────────┐
│ Run │ Commit │ Job │ MMLU-Pro │
├─────────────┼───────────┼───────────────────────────┼─────────────────┤
│ base (main) │ aba4aad88 │ j-379f0157-2fa7-48da-963b │ 0.7852 ± 0.0036 │
├─────────────┼───────────┼───────────────────────────┼─────────────────┤
│ PR (#3076) │ e21aafe44 │ j-93ad129f-7358-47bb-b96b │ 0.7852 ± 0.0036 │
└─────────────┴───────────┴───────────────────────────┴─────────────────┘
Steps:
1. Find the MoE kernel and check its name.
- base: fused_ep_moe_v2_fp4w_silu_and_mul_with_clamp_g24_c128_nb1
- PR: fused_ep_moe_v2_fp4w_silu_and_mul_with_clamp_w13s201_w2s201_g24_c128_nb1
2. Find its HLO
2.1.base:
```
%copy.5160.refs_15_ = f32[24,14,6144]{2,1,0} copy(f32[24,14,6144]{2,0,1} %bitcast.2781) 9.8 µs
f32[24,14,6144]{2,1,0} %copy.5160.refs_15_
┌───────────────┬───────────┬───────────┬─────────┬─────────┬───────────┐
│ │ without │ without │ with │ with │ change │
│ │ PR, run 1 │ PR, run 2 │ PR, run │ PR, run │ (avg) │
│ │ │ │ 1 │ 2 │ │
├───────────────┼───────────┼───────────┼─────────┼─────────┼───────────┤
│ output tokens │ 2301 │ 2323 │ 2343 │ 2366 │ +1.8% │
│ per second │ │ │ │ │ │
├───────────────┼───────────┼───────────┼─────────┼─────────┼───────────┤
│ mean time per │ 60.12 ms │ 59.33 ms │ 58.66 │ 58.09 │ −1.35 ms │
│ output token │ │ │ ms │ ms │ (−2.3%) │
┌────────────────────────┬────────────────┬────────┐
│ Code │ MMLU-Pro runs │ Mean │
├────────────────────────┼────────────────┼────────┤
│ base (PR 3, 11a653a10) │ 0.7860, 0.7885 │ 0.7873 │
├────────────────────────┼────────────────┼────────┤
│ PR 4 (4de631839) │ 0.7832, 0.7876 │ 0.7854 │
└────────────────────────┴────────────────┴────────┘