|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +"""L1 transitions, invalid accesses and exact Case4 address regression.""" |
| 4 | + |
| 5 | +import importlib.util |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +spec = importlib.util.spec_from_file_location( |
| 12 | + "kv_guard_subject", Path(__file__).parents[3] / "vllm_ascend/debug/kv_guard.py" |
| 13 | +) |
| 14 | +assert spec is not None and spec.loader is not None |
| 15 | +subject = importlib.util.module_from_spec(spec) |
| 16 | +sys.modules[spec.name] = subject |
| 17 | +spec.loader.exec_module(subject) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def guard(): |
| 22 | + events = [] |
| 23 | + guard = subject.KVGuard(lambda event, **fields: events.append({"event": event, **fields})) |
| 24 | + guard.configure({0: 128}) |
| 25 | + guard.events = events |
| 26 | + return guard |
| 27 | + |
| 28 | + |
| 29 | +def bind(guard, request="r", computed=0, epoch="1", pool="pool", ids=(1,)): |
| 30 | + refs = [ |
| 31 | + {"pool_id": pool, "alloc_epoch": epoch, "group_id": 0, "block_id": block, "logical_block_index": i} |
| 32 | + for i, block in enumerate(ids) |
| 33 | + ] |
| 34 | + guard.bind({"requests": [{"request_id": request, "num_computed_tokens": computed, "block_refs": refs}]}) |
| 35 | + |
| 36 | + |
| 37 | +def access(guard, slots=(), length=0, ids=(1,), request_ids=("r",), phase="forward"): |
| 38 | + guard.observe( |
| 39 | + [{"group_id": 0, "block_size": 128, "slots": list(slots), "block_table": [list(ids)], "seq_lens": [length]}], |
| 40 | + list(request_ids), |
| 41 | + phase, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def violations(guard): |
| 46 | + return [e for e in guard.events if e["event"] == "guard.violation"] |
| 47 | + |
| 48 | + |
| 49 | +def test_partial_fill_seal_and_read(guard): |
| 50 | + bind(guard) |
| 51 | + access(guard, range(128, 147), 19) |
| 52 | + assert guard.blocks[0, 1].state == subject.BlockState.PARTIAL |
| 53 | + assert guard.blocks[0, 1].valid_end == 19 |
| 54 | + access(guard, range(147, 256), 128) |
| 55 | + assert guard.blocks[0, 1].state == subject.BlockState.SEALED_FILL |
| 56 | + assert not violations(guard) |
| 57 | + |
| 58 | + |
| 59 | +def test_unknown_read_and_skipped_write(guard): |
| 60 | + bind(guard) |
| 61 | + access(guard, (), 1) |
| 62 | + assert violations(guard)[-1]["reason"] == "read_unwritten_range" |
| 63 | + access(guard, [130]) |
| 64 | + assert violations(guard)[-1]["reason"] == "noncontiguous_write" |
| 65 | + assert guard.blocks[0, 1].state == subject.BlockState.INVALID |
| 66 | + |
| 67 | + |
| 68 | +def test_case4_loaded_prefix_dummy_write_then_dirty_read_without_tensors(guard): |
| 69 | + bind(guard) |
| 70 | + guard.load({"request_id": "r", "local_block_ids": [[1]], "num_computed_tokens": 19}) |
| 71 | + assert guard.blocks[0, 1].state == subject.BlockState.SEALED_LOAD |
| 72 | + access(guard, [142], 1, request_ids=(), phase="dummy") |
| 73 | + dirty_write = violations(guard)[0] |
| 74 | + assert dirty_write["reason"] == "dummy_write_to_live_block" |
| 75 | + assert dirty_write["block_id"] == 1 and dirty_write["offsets"] == [14] |
| 76 | + assert dirty_write["valid_end"] == 19 and dirty_write["owner_request_ids"] == ["r"] |
| 77 | + access(guard, (), 19) |
| 78 | + assert violations(guard)[-1]["reason"] == "read_invalid_block" |
| 79 | + |
| 80 | + |
| 81 | +def test_loaded_tail_allows_append_but_not_overwrite(guard): |
| 82 | + bind(guard, computed=19) |
| 83 | + access(guard, [147], 20) |
| 84 | + assert not violations(guard) |
| 85 | + access(guard, [142], 20) |
| 86 | + assert violations(guard)[0]["reason"] == "overwrite_valid_prefix" |
| 87 | + |
| 88 | + |
| 89 | +def test_reallocation_epoch_clears_old_validity_and_taint(guard): |
| 90 | + bind(guard, computed=19) |
| 91 | + access(guard, [142], phase="dummy", request_ids=()) |
| 92 | + bind(guard, request="new", epoch="2") |
| 93 | + assert guard.blocks[0, 1].state == subject.BlockState.UNKNOWN |
| 94 | + assert guard.blocks[0, 1].owners == {"new"} |
| 95 | + guard.events.clear() |
| 96 | + access(guard, [128], 1, request_ids=("new",)) |
| 97 | + assert not violations(guard) |
| 98 | + |
| 99 | + |
| 100 | +def test_shared_prefix_and_finished_request(guard): |
| 101 | + bind(guard, computed=128) |
| 102 | + bind(guard, request="shared", computed=128) |
| 103 | + access(guard, (), 128, request_ids=("shared",)) |
| 104 | + assert not violations(guard) |
| 105 | + guard.bind({"finished_request_ids": ["r"], "requests": []}) |
| 106 | + assert guard.blocks[0, 1].owners == {"shared"} |
| 107 | + access(guard, (), 128) |
| 108 | + assert violations(guard)[-1]["reason"] == "unowned_read" |
| 109 | + |
| 110 | + |
| 111 | +def test_cache_lookup_does_not_revive_tainted_same_epoch(guard): |
| 112 | + bind(guard, computed=19) |
| 113 | + access(guard, [142], phase="dummy", request_ids=()) |
| 114 | + bind(guard, request="prefix-hit", computed=19) |
| 115 | + assert guard.blocks[0, 1].state == subject.BlockState.INVALID |
| 116 | + |
| 117 | + |
| 118 | +def test_load_before_scheduler_binding_and_failed_load(guard): |
| 119 | + guard.load({"request_id": "r", "local_block_ids": [[1]], "num_computed_tokens": 19}) |
| 120 | + bind(guard) |
| 121 | + assert not guard.pending_loads |
| 122 | + assert guard.blocks[0, 1].valid_end == 19 |
| 123 | + guard.fail_load("r") |
| 124 | + access(guard, (), 19) |
| 125 | + assert violations(guard)[-1]["reason"] == "read_invalid_block" |
| 126 | + |
| 127 | + |
| 128 | +def test_pending_load_never_applied_to_different_request(guard): |
| 129 | + guard.load({"request_id": "old", "local_block_ids": [[1]], "num_computed_tokens": 19}) |
| 130 | + bind(guard, request="new", epoch="2") |
| 131 | + assert guard.blocks[0, 1].state == subject.BlockState.UNKNOWN |
| 132 | + guard.bind({"finished_request_ids": ["old"]}) |
| 133 | + assert not guard.pending_loads |
| 134 | + |
| 135 | + |
| 136 | +def test_no_per_token_state_log_and_negative_padding(guard): |
| 137 | + bind(guard) |
| 138 | + for offset in range(64): |
| 139 | + access(guard, [128 + offset, -1], offset + 1) |
| 140 | + assert not violations(guard) |
| 141 | + assert len([e for e in guard.events if e["event"] == "guard.state"]) == 1 |
| 142 | + |
| 143 | + |
| 144 | +def test_null_block_write_is_not_padding_and_taints_later_read(guard): |
| 145 | + access(guard, [-1, 0, 14], ids=(0,), request_ids=(), phase="dummy") |
| 146 | + assert violations(guard)[0]["reason"] == "write_null_block" |
| 147 | + assert violations(guard)[0]["offsets"] == [0, 14] |
| 148 | + access(guard, (), 1, ids=(0,)) |
| 149 | + assert violations(guard)[-1]["reason"] == "read_tainted_null_block" |
| 150 | + guard.clear(0, 0) |
| 151 | + assert not guard.tainted_nulls |
| 152 | + |
| 153 | + |
| 154 | +def test_nonlinear_cache_checks_reserved_destinations_without_append_rules(guard): |
| 155 | + bind(guard, computed=19) |
| 156 | + guard.observe( |
| 157 | + [{"group_id": 0, "block_size": 128, "write_mode": "reserved_only", "slots": [142, 0, -1]}], ["r"], "forward" |
| 158 | + ) |
| 159 | + assert [v["reason"] for v in violations(guard)] == ["write_null_block"] |
| 160 | + assert guard.blocks[0, 1].valid_end == 19 |
| 161 | + |
| 162 | + |
| 163 | +def test_group_pool_and_allocation_isolation(guard): |
| 164 | + guard.configure({0: 128, 1: 128}) |
| 165 | + bind(guard, computed=19) |
| 166 | + other = {"pool_id": "pool", "alloc_epoch": "1", "group_id": 1, "block_id": 1, "logical_block_index": 0} |
| 167 | + guard.bind({"requests": [{"request_id": "r2", "block_refs": [other], "num_computed_tokens": 7}]}) |
| 168 | + assert guard.blocks[0, 1].valid_end == 19 |
| 169 | + assert guard.blocks[1, 1].valid_end == 7 |
| 170 | + bind(guard, pool="new-pool") |
| 171 | + assert guard.blocks[0, 1].state == subject.BlockState.UNKNOWN |
| 172 | + assert guard.blocks[1, 1].valid_end == 7 |
| 173 | + |
| 174 | + |
| 175 | +def test_copy_clear_and_read_invalid_destination(guard): |
| 176 | + bind(guard, computed=19) |
| 177 | + bind(guard, request="dest", ids=(2,)) |
| 178 | + guard.copy(0, 1, 2, "dest") |
| 179 | + assert guard.blocks[0, 2].state == subject.BlockState.SEALED_LOAD |
| 180 | + access(guard, (), 19, ids=(2,), request_ids=("dest",)) |
| 181 | + assert not violations(guard) |
| 182 | + guard.clear(0, 1) |
| 183 | + guard.copy(0, 1, 2, "dest") |
| 184 | + assert violations(guard)[-1]["reason"] == "invalid_copy_source_or_destination" |
| 185 | + access(guard, (), 19, ids=(2,), request_ids=("dest",)) |
| 186 | + assert violations(guard)[-1]["reason"] == "read_invalid_block" |
| 187 | + |
| 188 | + |
| 189 | +def test_recycling_a_live_block_is_a_lifecycle_violation(guard): |
| 190 | + bind(guard, computed=19) |
| 191 | + bind(guard, request="new", epoch="2") |
| 192 | + assert violations(guard)[0]["reason"] == "recycle_live_block" |
0 commit comments