{"record":{"id":"a389dfbcb5d17f6b","repo":"sgl-project/sglang","slug":"kv-canary-canary-buf-slot-stride-must-hold-at-lea","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/verify_ref.py","lineNumber":92,"sourceCode":"        # Skip SGLang's padded-token dummy KV slot so unfilled req_to_token entries (zero-initialized) do not\n        # produce spurious chain_hash / position violations.\n        if s != consts.TOKEN_TO_KV_SLOT_PADDING:\n            kept_slots.append(s)\n            kept_expected_positions.append(int(expected_positions_host[k].item()))\n            kept_expected_input_ids.append(int(expected_input_ids_host[k].item()))\n            kept_prev_slots.append(int(prev_slot_indices_host[k].item()))\n    active = len(kept_slots)\n    if active <= 0:\n        return\n    slot_indices_list: list[int] = kept_slots\n    expected_positions_list: list[int] = kept_expected_positions\n    expected_input_ids_list: list[int] = kept_expected_input_ids\n    prev_slot_indices_list: list[int] = kept_prev_slots\n\n    buf_i64 = canary_buf.detach().to(device=work_device).contiguous().view(torch.int64)\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    violation_rows: list[list[int]] = []\n\n    for k in range(active):\n        slot_idx = slot_indices_list[k]\n        expected_position = expected_positions_list[k]\n        expected_input_id = expected_input_ids_list[k]\n        prev_slot = prev_slot_indices_list[k]\n\n        stored_token = int(buf_i64[slot_idx, consts.CANARY_FIELD_TOKEN].item())\n        stored_position = int(buf_i64[slot_idx, consts.CANARY_FIELD_POSITION].item())\n        stored_chain_hash = int(buf_i64[slot_idx, consts.CANARY_FIELD_PREV_HASH].item())\n        stored_real_kv_hash = int(\n            buf_i64[slot_idx, consts.CANARY_FIELD_REAL_KV_HASH].item()\n        )\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/kv_canary/verify_ref.py#L74-L110","documentation":"The torch reference implementation of the canary verify kernel views canary_buf as int64 and uses each row's stride as the slot record layout; it needs at least 4 int64 fields per slot (the canary record schema). A narrower buffer means the buffer was allocated with the wrong slot stride.","triggerScenarios":"Calling launch_canary_verify_kernel_torch_reference with a canary_buf whose int64 row stride (shape[1] after .view(torch.int64)) is < 4, e.g. a buffer allocated with fewer than 32 bytes per slot.","commonSituations":"Allocating canary_buf manually with a custom stride instead of via the provided allocator; porting the canary to a different slot schema and forgetting the reference's 4-field minimum.","solutions":["Allocate canary_buf with the canonical slot stride (>= 4 int64 = 32 bytes per slot); prefer the library's buffer allocator helper","If you changed the record schema, ensure buf.view(torch.int64).shape[1] >= 4 before calling","Add a unit assert on the stride in your test harness"],"exampleFix":"# before\ncanary_buf = torch.empty(num_slots, 16, dtype=torch.uint8, device=dev)  # 2 int64 fields\n# after\ncanary_buf = torch.empty(num_slots, 4 * 8, dtype=torch.uint8, device=dev)  # 4 int64 fields/slot","handlingStrategy":"validation","validationCode":"stride = canary_buf.view(torch.int64).shape[1]\nassert stride >= 4, f\"canary_buf stride {stride} < 4 int64 fields\"","typeGuard":"def canary_buf_stride_ok(buf: torch.Tensor) -> bool:\n    return buf.view(torch.int64).shape[1] >= 4","tryCatchPattern":null,"preventionTips":["Allocate canary_buf via the library allocator, not manually","Pin slot stride to 4+ int64 fields in a constant shared by all tests"],"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"}