flashinfer.mla.cake_dsv4_nvfp4_rope_quantize_insert¶
- flashinfer.mla.cake_dsv4_nvfp4_rope_quantize_insert(q: Tensor, kv: Tensor, cache: Tensor, slot_mapping: Tensor, positions: Tensor, cos_sin_cache: Tensor, *, q_head_padded: int = 0, apply_q_rope: bool = True, q_inplace: bool = False) Tensor¶
Rotate
q, then rotate, quantize and insertkvinto the DeepSeek-V4 NVFP4 cache.One launch replaces the torch-level GPT-J RoPE of the query and latent KV, the head padding of the query and
flashinfer.mla.nvfp4_quantize_append_sparse_mla_cache(): the cache bytes are identical to appending the BF16-rounded roped row, andq_outis the fp32 rotation of each live head rounded to BF16 with zero-filled padded heads. Rows>= slot_mapping.numel()(data-parallel padding) get theirq_outrows but are not inserted; negative and out-of-range slots are skipped.- Parameters:
q (torch.Tensor) – Contiguous CUDA BF16
[num_tokens, num_heads, 512]queries (448 NoPE dims, then 64 RoPE dims), head-major.kv (torch.Tensor) – Contiguous CUDA BF16
[num_tokens, 512]latent KV rows, one per query token (unrotated).cache (torch.Tensor) – Destination opaque uint8 NVFP4 paged cache: 3-D
[num_pages, page_size, 384], HND[num_pages, 1, page_size, 384]or NHD[num_pages, page_size, 1, 384], any positive page size, 16-byte-multiple page stride (padded pools accepted). Only the addressed data rows and scale slots are written.slot_mapping (torch.Tensor) – Contiguous 1-D CUDA int32 or int64 tensor with at most
num_tokensentries;slot_mapping[i] = page_id * page_size + entry_idfor rowi. Negative and out-of-range entries are padding and skipped; a valid slot must not repeat inside one call.positions (torch.Tensor) – Contiguous 1-D CUDA int64
[num_tokens]positions indexingcos_sin_cache(positions[i] < cos_sin_cache.shape[0]is a caller precondition; it is not checked on the device). int32 is rejected rather than converted, so the hot path stays free of hidden launches.cos_sin_cache (torch.Tensor) – Contiguous CUDA fp32
[max_position, 64]table,cos[32] | sin[32]per position (pairpof the 64 RoPE dims uses columnp).q_head_padded (int) – Padded head count of
q_out: one of 8, 16, 32, 64, 128 and at leastnum_heads;0(default) writes the cache only – identical tocake_dsv4_nvfp4_kv_rope_quantize_insert()withcompress_ratio=1– and returns an empty[num_tokens, 0, 512]tensor.apply_q_rope (bool) – Rotate the live query heads (default).
Falsecopies them unchanged (padded heads are still zero-filled).q_inplace (bool) – Rotate the 64 RoPE dims of
qin place and returnqitself (no padded copy; the NoPE dims are neither read nor written). Requiresq_head_paddedto be0ornum_heads(no head padding). This is the fast path for serving geometries whose local head count is already a supported padded count (every power-of-two tensor-parallel split of DeepSeek-V4). Withapply_q_rope=Falseonly the cache is written andqis returned untouched.
- Returns:
q_out– a new BF16[num_tokens, q_head_padded, 512]tensor (torch.empty, fully written), orqitself withq_inplace. The cache is updated in place.num_tokens == 0returns the empty tensor (orq) without a launch.- Return type:
torch.Tensor