Repository navigation
pd: index an empty paged KV span as long (#47) - #284
Merged
Merged
Conversation
PagedKV.physical() built the slot index with torch.as_tensor(slots); for an empty span ([] slots) that is a float32 tensor and table indexing raises IndexError. Pin dtype=torch.long so an empty span_index()/write_span() is a no-op. Adds a CPU regression test. Refs #47 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
PagedKV.physical()built the slot index withtorch.as_tensor(slots, device=...). For an empty span (span_index(slot, n, n), e.g. nothing left to import after a fully cached prefix)slotsis[], which becomes a float32 tensor, and indexing the page table raisesIndexError: tensors used as indices must be long, int, byte or bool tensors.Changes
From #254 (branch
perf/47-chunk-copy), only the one-line fix inopt_prime/opt_prime/accel/pd/paged.pyphysical():torch.as_tensor(slots, dtype=torch.long, device=self.device).opt_prime/tests/test_pd_paged_empty_span.py: an emptyspan_index()is long and empty; an emptywrite_span()is a no-op._ChunkCopy,pd_batchingchanges, its GPU-only tests); review found it gives no gain.Verification
TORCH_DEVICE_BACKEND_AUTOLOAD=0 PYTHONPATH=$PWD python3 -m pytest -q tests/test_pd_paged_empty_span.py: before the fix, 2 failed (IndexErroratpaged.py:420). After the fix, passes.tests/test_pd_paged_empty_span.py tests/test_pd_dense_hybrid.py: 6 passed.Compatibility and limits
Behavior changes only for empty spans, which raised before. Non-empty slot lists of Python ints were already int64, so they are unaffected.
Related Issue
Refs #47
Supersedes #254 (fix only)
🤖 Generated with Claude Code