{"record":{"id":"47eab941e6e8de03","repo":"sgl-project/sglang","slug":"kv-canary-expected-input-tensors-are-required-whe-47eab9","errorCode":null,"errorMessage":"kv-canary: expected input tensors are required when enable_write_input_assert=True","messagePattern":"kv-canary: expected input tensors are required when enable_write_input_assert=True","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/kv_canary/write_ref.py","lineNumber":82,"sourceCode":"    if total_entries <= 0:\n        return\n\n    buf_i64 = (\n        canary_buf.detach()\n        .to(device=work_device)\n        .contiguous()\n        .view(torch.int64)\n        .clone()\n    )\n    slot_stride_i64 = int(buf_i64.shape[1])\n    if slot_stride_i64 < 4:\n        raise ValueError(\n            f\"kv-canary: canary_buf slot stride must hold at least 4 int64 fields, got {slot_stride_i64}\"\n        )\n\n    if enable_write_input_assert:\n        if expected_input_tokens is None or expected_input_positions is None:\n            raise ValueError(\n                \"kv-canary: expected input tensors are required when enable_write_input_assert=True\"\n            )\n        expected_input_tokens_host = expected_input_tokens.detach().to(\n            device=work_device, dtype=torch.int64\n        )\n        expected_input_positions_host = expected_input_positions.detach().to(\n            device=work_device, dtype=torch.int64\n        )\n    else:\n        if expected_input_tokens is not None or expected_input_positions is not None:\n            raise ValueError(\n                \"kv-canary: expected input tensors must be None when enable_write_input_assert=False\"\n            )\n        expected_input_tokens_host = None\n        expected_input_positions_host = None\n\n    violation_rows: list[list[int]] = []\n    total_slots_written = 0","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/kv_canary/write_ref.py#L64-L100","documentation":"The torch reference write path validates the optional input-assert mode: with enable_write_input_assert=True both expected_input_tokens and expected_input_positions must be supplied so the reference can emulate the kernel's cross-check. Missing tensors make the requested mode impossible.","triggerScenarios":"Calling launch_canary_write_kernel_torch_reference(..., enable_write_input_assert=True) with one or both expected tensors None.","commonSituations":"Mirroring a CUDA-path call in a parity test but forgetting the expected tensors; enabling asserts in a shared helper whose callers pass None defaults.","solutions":["Supply both expected_input_tokens and expected_input_positions","Or disable enable_write_input_assert if parity of the assert path is not being tested","Keep a single kwargs dict shared between CUDA and reference calls so they stay in sync"],"exampleFix":"# before\nlaunch_canary_write_kernel_torch_reference(ctx, plan, ..., enable_write_input_assert=True)\n# after\nlaunch_canary_write_kernel_torch_reference(ctx, plan, ..., enable_write_input_assert=True,\n    expected_input_tokens=exp_tokens, expected_input_positions=exp_positions)","handlingStrategy":"validation","validationCode":"if enable_write_input_assert:\n    assert expected_input_tokens is not None and expected_input_positions is not None","typeGuard":"def ref_assert_args_ok(flag: bool, t, p) -> bool:\n    return (flag and t is not None and p is not None) or (not flag and t is None and p is None)","tryCatchPattern":null,"preventionTips":["Pass the same kwargs dict to both CUDA and reference launches in parity tests"],"tags":["kv-canary","reference-implementation","assert-mode"],"backgroundTag":"missing-required-parameter","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}