{"record":{"id":"219a5070795c6e06","repo":"sgl-project/sglang","slug":"fa4-does-not-support-updating-kv-cache-in-place-219a50","errorCode":null,"errorMessage":"FA4 does not support updating KV cache in-place.","messagePattern":"FA4 does not support updating KV cache in-place\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/flash_attention_v4_sm120.py","lineNumber":242,"sourceCode":"    num_splits: int = 0,\n    pack_gqa: Optional[bool] = None,\n    sm_margin: int = 0,\n    sinks: Optional[torch.Tensor] = None,\n    score_mod: Optional[Callable] = None,\n    aux_tensors: Optional[list] = None,\n    sfq: Optional[torch.Tensor] = None,\n    sfk: Optional[torch.Tensor] = None,\n    sfv: Optional[torch.Tensor] = None,\n    rel_bias: Optional[torch.Tensor] = None,\n    rel_bias_prep_cache: Optional[dict] = None,\n    return_softmax_lse: bool = False,\n    out: Optional[torch.Tensor] = None,\n    max_seqlen_k: Optional[int] = None,\n    **_: object,\n):\n    _validate_out_contract(out)\n    if k is not None or v is not None:\n        raise NotImplementedError(\"FA4 does not support updating KV cache in-place.\")\n    if rotary_cos is not None or rotary_sin is not None or rotary_seqlens is not None:\n        raise NotImplementedError(\"FA4 path does not support rotary embedding.\")\n    if cache_batch_idx is not None or cache_leftpad is not None:\n        raise NotImplementedError(\n            \"FA4 path does not support non-consecutive batch indices or left padding.\"\n        )\n    if isinstance(cache_seqlens, int):\n        cache_seqlens = torch.full(\n            (k_cache.shape[0],), cache_seqlens, dtype=torch.int32, device=k_cache.device\n        )\n\n    forward_arch = get_forward_arch(q.device) if get_forward_arch is not None else None\n    if (\n        forward_arch is not None\n        and not return_softmax_lse\n        and softcap in (None, 0.0)\n        and all(\n            value is None","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/flash_attention_v4_sm120.py#L224-L260","documentation":"The sm120 FA4 kvcache path mirrors the FA4 contract: it does not support in-place KV cache updates, so passing k or v tensors raises NotImplementedError.","triggerScenarios":"Calling flash_attn_with_kvcache(k=..., v=..., ...) in flash_attention_v4_sm120 while also giving k_cache/v_cache.","commonSituations":"Shared attention-backend code that always passes new k/v (FA2-style) regardless of backend.","solutions":["Pre-write k/v into the paged cache and call with k=None, v=None","Rely on cache_seqlens for valid lengths","Use FA2/FA3 backend when append semantics are needed"],"exampleFix":"# before\nout = fa.flash_attn_with_kvcache(q, k, v, kc, vc, cache_seqlens=s)\n# after\nout = fa.flash_attn_with_kvcache(q, None, None, kc, vc, cache_seqlens=s)","handlingStrategy":"validation","validationCode":"assert k is None and v is None, 'write k/v into cache before FA4 sm120 call'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep backend-specific argument adapters","Write cache updates in the memory pool layer, not the kernel call"],"tags":["flash-attention","fa4","sm120","kv-cache","unsupported-operation"],"backgroundTag":"unsupported-kv-cache-update","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}