block.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Token blocks."""
  2. from typing import List
  3. from aphrodite.common.utils import Device
  4. DEFAULT_LAST_ACCESSED_TIME = -1
  5. class PhysicalTokenBlock:
  6. """Represents the state of a block in the KV cache."""
  7. def __init__(
  8. self,
  9. device: Device,
  10. block_number: int,
  11. block_size: int,
  12. block_hash: int,
  13. num_hashed_tokens: int,
  14. ) -> None:
  15. self.device = device
  16. self.block_number = block_number
  17. self.block_size = block_size
  18. self.block_hash = block_hash
  19. self.num_hashed_tokens = num_hashed_tokens
  20. self.ref_count = 0
  21. self.last_accessed = DEFAULT_LAST_ACCESSED_TIME
  22. self.computed = False
  23. def __repr__(self) -> str:
  24. return (f'PhysicalTokenBlock(device={self.device}, '
  25. f'block_number={self.block_number}, '
  26. f'num_hashed_tokens={self.num_hashed_tokens}, '
  27. f'ref_count={self.ref_count}, '
  28. f'last_accessed={self.last_accessed}, '
  29. f'computed={self.computed})')
  30. # Mapping: logical block number -> physical block.
  31. BlockTable = List[PhysicalTokenBlock]