Skip to content

pd: index an empty paged KV span as long (#47) - #284

Merged
gspark-etri merged 1 commit into
optimus-rmfrom
fix/47-paged-empty-span
Oct 30, 2026
Merged

gspark-etri merged 1 commit into
optimus-rmfrom
fix/47-paged-empty-span

Conversation

@gspark-etri

Copy link
Copy Markdown
Collaborator

Background

PagedKV.physical() built the slot index with torch.as_tensor(slots, device=...). For an empty span (span_index(slot, n, n), e.g. nothing left to import after a fully cached prefix) slots is [], which becomes a float32 tensor, and indexing the page table raises IndexError: 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 in opt_prime/opt_prime/accel/pd/paged.py physical(): torch.as_tensor(slots, dtype=torch.long, device=self.device).

  • New CPU test opt_prime/tests/test_pd_paged_empty_span.py: an empty span_index() is long and empty; an empty write_span() is a no-op.
  • Not taken from Overlap GPU prefill token chunks with bounded KV copies #254: the chunk-copy feature (_ChunkCopy, pd_batching changes, 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 (IndexError at paged.py:420). After the fix, passes.
  • tests/test_pd_paged_empty_span.py tests/test_pd_dense_hybrid.py: 6 passed.
  • Full suite: CI.

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

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>
@gspark-etri
gspark-etri merged commit ef272c2 into optimus-rm Oct 30, 2026
1 check passed
@gspark-etri
gspark-etri deleted the fix/47-paged-empty-span branch October 30, 2026 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant