{"record":{"id":"4747f41275a65237","repo":"sgl-project/sglang","slug":"kv-canary-canary-buf-slot-stride-must-hold-at-lea-4747f4","errorCode":null,"errorMessage":"kv-canary: canary_buf slot stride must hold at least 4 int64 fields, got {slot_stride_i64}","messagePattern":"kv-canary: canary_buf slot stride must hold at least 4 int64 fields, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/kv_canary/write_ref.py","lineNumber":76,"sourceCode":"    positions_host = positions.detach().to(device=work_device, dtype=torch.int64)\n    out_cache_loc_host = out_cache_loc.detach().to(\n        device=work_device, dtype=torch.int64\n    )\n\n    total_entries = int(write_offsets_host[active_reqs].item())\n    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\"","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/kv_canary/write_ref.py#L58-L94","documentation":"The torch reference write kernel views canary_buf as int64 rows and requires at least 4 int64 fields per slot (the canary record schema). A smaller row stride means the buffer layout does not match what the write reference (and the CUDA kernel it must match byte-for-byte) expects.","triggerScenarios":"Calling launch_canary_write_kernel_torch_reference with a canary_buf whose per-slot byte stride is < 32 bytes (< 4 int64 fields).","commonSituations":"Hand-allocating canary_buf with a custom/narrower stride; schema changes to the canary record without updating all allocators.","solutions":["Allocate canary_buf with >= 4 int64 fields (32 bytes) per slot, ideally via the library allocator","Assert buf.view(torch.int64).shape[1] >= 4 in your test setup","Keep verify and write buffers allocated from the same helper so strides stay consistent"],"exampleFix":"# before\ncanary_buf = torch.empty(num_slots, 2 * 8, dtype=torch.uint8, device=dev)\n# after\ncanary_buf = torch.empty(num_slots, 4 * 8, dtype=torch.uint8, device=dev)","handlingStrategy":"validation","validationCode":"assert canary_buf.view(torch.int64).shape[1] >= 4","typeGuard":"def canary_buf_stride_ok(buf: torch.Tensor) -> bool:\n    return buf.view(torch.int64).shape[1] >= 4","tryCatchPattern":null,"preventionTips":["Allocate buffers with the canonical 4-int64 slot schema","Share one allocator between CUDA and reference paths"],"tags":["kv-canary","buffer-layout","reference-implementation"],"backgroundTag":"buffer-stride-validation","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}