vllm-npu 0.4.2__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. vllm/__init__.py +23 -0
  2. vllm/_custom_ops.py +251 -0
  3. vllm/attention/__init__.py +13 -0
  4. vllm/attention/backends/__init__.py +0 -0
  5. vllm/attention/backends/abstract.py +127 -0
  6. vllm/attention/backends/flash_attn.py +271 -0
  7. vllm/attention/backends/flashinfer.py +220 -0
  8. vllm/attention/backends/rocm_flash_attn.py +374 -0
  9. vllm/attention/backends/torch_sdpa.py +250 -0
  10. vllm/attention/backends/xformers.py +393 -0
  11. vllm/attention/layer.py +56 -0
  12. vllm/attention/ops/__init__.py +0 -0
  13. vllm/attention/ops/paged_attn.py +216 -0
  14. vllm/attention/ops/prefix_prefill.py +792 -0
  15. vllm/attention/ops/triton_flash_attention.py +810 -0
  16. vllm/attention/selector.py +91 -0
  17. vllm/block.py +84 -0
  18. vllm/config.py +1225 -0
  19. vllm/core/__init__.py +0 -0
  20. vllm/core/block/__init__.py +0 -0
  21. vllm/core/block/block_table.py +295 -0
  22. vllm/core/block/common.py +199 -0
  23. vllm/core/block/cpu_gpu_block_allocator.py +228 -0
  24. vllm/core/block/interfaces.py +205 -0
  25. vllm/core/block/naive_block.py +318 -0
  26. vllm/core/block/prefix_caching_block.py +606 -0
  27. vllm/core/block_manager_v1.py +625 -0
  28. vllm/core/block_manager_v2.py +258 -0
  29. vllm/core/evictor_v1.py +105 -0
  30. vllm/core/evictor_v2.py +127 -0
  31. vllm/core/interfaces.py +113 -0
  32. vllm/core/policy.py +45 -0
  33. vllm/core/scheduler.py +1163 -0
  34. vllm/distributed/__init__.py +3 -0
  35. vllm/distributed/communication_op.py +237 -0
  36. vllm/distributed/device_communicators/__init__.py +0 -0
  37. vllm/distributed/device_communicators/custom_all_reduce.py +274 -0
  38. vllm/distributed/device_communicators/pynccl.py +287 -0
  39. vllm/distributed/device_communicators/pynccl_utils.py +66 -0
  40. vllm/distributed/parallel_state.py +339 -0
  41. vllm/distributed/utils.py +136 -0
  42. vllm/engine/__init__.py +0 -0
  43. vllm/engine/arg_utils.py +649 -0
  44. vllm/engine/async_llm_engine.py +737 -0
  45. vllm/engine/llm_engine.py +784 -0
  46. vllm/engine/metrics.py +368 -0
  47. vllm/engine/output_processor/__init__.py +0 -0
  48. vllm/engine/output_processor/interfaces.py +76 -0
  49. vllm/engine/output_processor/multi_step.py +142 -0
  50. vllm/engine/output_processor/single_step.py +284 -0
  51. vllm/engine/output_processor/stop_checker.py +101 -0
  52. vllm/engine/output_processor/util.py +19 -0
  53. vllm/entrypoints/__init__.py +0 -0
  54. vllm/entrypoints/api_server.py +119 -0
  55. vllm/entrypoints/llm.py +259 -0
  56. vllm/entrypoints/openai/__init__.py +0 -0
  57. vllm/entrypoints/openai/api_server.py +186 -0
  58. vllm/entrypoints/openai/cli_args.py +115 -0
  59. vllm/entrypoints/openai/protocol.py +460 -0
  60. vllm/entrypoints/openai/serving_chat.py +392 -0
  61. vllm/entrypoints/openai/serving_completion.py +347 -0
  62. vllm/entrypoints/openai/serving_engine.py +234 -0
  63. vllm/envs.py +217 -0
  64. vllm/executor/__init__.py +0 -0
  65. vllm/executor/cpu_executor.py +152 -0
  66. vllm/executor/distributed_gpu_executor.py +115 -0
  67. vllm/executor/executor_base.py +115 -0
  68. vllm/executor/gpu_executor.py +150 -0
  69. vllm/executor/multiproc_worker_utils.py +263 -0
  70. vllm/executor/neuron_executor.py +91 -0
  71. vllm/executor/ray_gpu_executor.py +327 -0
  72. vllm/executor/ray_utils.py +119 -0
  73. vllm/logger.py +153 -0
  74. vllm/logging/__init__.py +5 -0
  75. vllm/logging/formatter.py +15 -0
  76. vllm/lora/__init__.py +0 -0
  77. vllm/lora/fully_sharded_layers.py +262 -0
  78. vllm/lora/layers.py +1181 -0
  79. vllm/lora/lora.py +167 -0
  80. vllm/lora/models.py +645 -0
  81. vllm/lora/punica.py +213 -0
  82. vllm/lora/request.py +32 -0
  83. vllm/lora/utils.py +98 -0
  84. vllm/lora/worker_manager.py +251 -0
  85. vllm/model_executor/__init__.py +7 -0
  86. vllm/model_executor/guided_decoding/__init__.py +25 -0
  87. vllm/model_executor/guided_decoding/lm_format_enforcer_decoding.py +70 -0
  88. vllm/model_executor/guided_decoding/outlines_decoding.py +130 -0
  89. vllm/model_executor/guided_decoding/outlines_logits_processors.py +184 -0
  90. vllm/model_executor/layers/__init__.py +0 -0
  91. vllm/model_executor/layers/activation.py +173 -0
  92. vllm/model_executor/layers/fused_moe/__init__.py +7 -0
  93. vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
  94. vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
  95. vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
  96. vllm/model_executor/layers/fused_moe/configs/E=16,N=2688,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
  97. vllm/model_executor/layers/fused_moe/configs/E=16,N=2688,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
  98. vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
  99. vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
  100. vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
  101. vllm/model_executor/layers/fused_moe/configs/E=8,N=2048,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
  102. vllm/model_executor/layers/fused_moe/configs/E=8,N=2048,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
  103. vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
  104. vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
  105. vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_H100_80GB_HBM3,dtype=float8.json +140 -0
  106. vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
  107. vllm/model_executor/layers/fused_moe/configs/E=8,N=4096,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
  108. vllm/model_executor/layers/fused_moe/configs/E=8,N=4096,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
  109. vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
  110. vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_H100_80GB_HBM3,dtype=float8.json +146 -0
  111. vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
  112. vllm/model_executor/layers/fused_moe/fused_moe.py +479 -0
  113. vllm/model_executor/layers/layernorm.py +71 -0
  114. vllm/model_executor/layers/linear.py +709 -0
  115. vllm/model_executor/layers/logits_processor.py +115 -0
  116. vllm/model_executor/layers/ops/__init__.py +0 -0
  117. vllm/model_executor/layers/ops/rand.py +157 -0
  118. vllm/model_executor/layers/ops/sample.py +406 -0
  119. vllm/model_executor/layers/quantization/__init__.py +35 -0
  120. vllm/model_executor/layers/quantization/aqlm.py +376 -0
  121. vllm/model_executor/layers/quantization/awq.py +175 -0
  122. vllm/model_executor/layers/quantization/base_config.py +97 -0
  123. vllm/model_executor/layers/quantization/fp8.py +265 -0
  124. vllm/model_executor/layers/quantization/gptq.py +224 -0
  125. vllm/model_executor/layers/quantization/gptq_marlin.py +438 -0
  126. vllm/model_executor/layers/quantization/marlin.py +227 -0
  127. vllm/model_executor/layers/quantization/schema.py +84 -0
  128. vllm/model_executor/layers/quantization/squeezellm.py +137 -0
  129. vllm/model_executor/layers/rejection_sampler.py +405 -0
  130. vllm/model_executor/layers/rotary_embedding.py +525 -0
  131. vllm/model_executor/layers/sampler.py +1051 -0
  132. vllm/model_executor/layers/vocab_parallel_embedding.py +155 -0
  133. vllm/model_executor/model_loader/__init__.py +30 -0
  134. vllm/model_executor/model_loader/loader.py +362 -0
  135. vllm/model_executor/model_loader/neuron.py +136 -0
  136. vllm/model_executor/model_loader/tensorizer.py +368 -0
  137. vllm/model_executor/model_loader/utils.py +41 -0
  138. vllm/model_executor/model_loader/weight_utils.py +372 -0
  139. vllm/model_executor/models/__init__.py +119 -0
  140. vllm/model_executor/models/baichuan.py +410 -0
  141. vllm/model_executor/models/bloom.py +327 -0
  142. vllm/model_executor/models/chatglm.py +386 -0
  143. vllm/model_executor/models/commandr.py +373 -0
  144. vllm/model_executor/models/dbrx.py +413 -0
  145. vllm/model_executor/models/decilm.py +122 -0
  146. vllm/model_executor/models/deepseek.py +438 -0
  147. vllm/model_executor/models/falcon.py +444 -0
  148. vllm/model_executor/models/gemma.py +393 -0
  149. vllm/model_executor/models/gpt2.py +266 -0
  150. vllm/model_executor/models/gpt_bigcode.py +274 -0
  151. vllm/model_executor/models/gpt_j.py +281 -0
  152. vllm/model_executor/models/gpt_neox.py +295 -0
  153. vllm/model_executor/models/internlm2.py +323 -0
  154. vllm/model_executor/models/jais.py +333 -0
  155. vllm/model_executor/models/llama.py +442 -0
  156. vllm/model_executor/models/llava.py +239 -0
  157. vllm/model_executor/models/minicpm.py +531 -0
  158. vllm/model_executor/models/mixtral.py +583 -0
  159. vllm/model_executor/models/mixtral_quant.py +404 -0
  160. vllm/model_executor/models/mpt.py +295 -0
  161. vllm/model_executor/models/olmo.py +356 -0
  162. vllm/model_executor/models/opt.py +349 -0
  163. vllm/model_executor/models/orion.py +319 -0
  164. vllm/model_executor/models/phi.py +300 -0
  165. vllm/model_executor/models/qwen.py +284 -0
  166. vllm/model_executor/models/qwen2.py +367 -0
  167. vllm/model_executor/models/qwen2_moe.py +447 -0
  168. vllm/model_executor/models/stablelm.py +301 -0
  169. vllm/model_executor/models/starcoder2.py +302 -0
  170. vllm/model_executor/models/xverse.py +366 -0
  171. vllm/model_executor/sampling_metadata.py +588 -0
  172. vllm/model_executor/utils.py +35 -0
  173. vllm/outputs.py +150 -0
  174. vllm/py.typed +2 -0
  175. vllm/sampling_params.py +340 -0
  176. vllm/sequence.py +766 -0
  177. vllm/spec_decode/__init__.py +0 -0
  178. vllm/spec_decode/batch_expansion.py +397 -0
  179. vllm/spec_decode/interfaces.py +73 -0
  180. vllm/spec_decode/metrics.py +191 -0
  181. vllm/spec_decode/multi_step_worker.py +203 -0
  182. vllm/spec_decode/ngram_worker.py +176 -0
  183. vllm/spec_decode/spec_decode_worker.py +472 -0
  184. vllm/spec_decode/top1_proposer.py +200 -0
  185. vllm/spec_decode/util.py +228 -0
  186. vllm/test_utils.py +41 -0
  187. vllm/transformers_utils/__init__.py +0 -0
  188. vllm/transformers_utils/config.py +58 -0
  189. vllm/transformers_utils/configs/__init__.py +16 -0
  190. vllm/transformers_utils/configs/chatglm.py +68 -0
  191. vllm/transformers_utils/configs/dbrx.py +278 -0
  192. vllm/transformers_utils/configs/falcon.py +87 -0
  193. vllm/transformers_utils/configs/jais.py +236 -0
  194. vllm/transformers_utils/configs/mpt.py +178 -0
  195. vllm/transformers_utils/detokenizer.py +313 -0
  196. vllm/transformers_utils/tokenizer.py +149 -0
  197. vllm/transformers_utils/tokenizer_group/__init__.py +33 -0
  198. vllm/transformers_utils/tokenizer_group/base_tokenizer_group.py +55 -0
  199. vllm/transformers_utils/tokenizer_group/ray_tokenizer_group.py +169 -0
  200. vllm/transformers_utils/tokenizer_group/tokenizer_group.py +78 -0
  201. vllm/transformers_utils/tokenizers/__init__.py +5 -0
  202. vllm/transformers_utils/tokenizers/baichuan.py +255 -0
  203. vllm/usage/__init__.py +0 -0
  204. vllm/usage/usage_lib.py +209 -0
  205. vllm/utils.py +677 -0
  206. vllm/worker/__init__.py +0 -0
  207. vllm/worker/cache_engine.py +105 -0
  208. vllm/worker/cpu_model_runner.py +346 -0
  209. vllm/worker/cpu_worker.py +321 -0
  210. vllm/worker/model_runner.py +1168 -0
  211. vllm/worker/neuron_model_runner.py +196 -0
  212. vllm/worker/neuron_worker.py +98 -0
  213. vllm/worker/worker.py +345 -0
  214. vllm/worker/worker_base.py +146 -0
  215. vllm_npu-0.4.2.dist-info/LICENSE +201 -0
  216. vllm_npu-0.4.2.dist-info/METADATA +173 -0
  217. vllm_npu-0.4.2.dist-info/RECORD +219 -0
  218. vllm_npu-0.4.2.dist-info/WHEEL +5 -0
  219. vllm_npu-0.4.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,228 @@
1
+ from typing import Dict, FrozenSet, List, Optional
2
+
3
+ from vllm.core.block.interfaces import (Block, BlockAllocator, BlockId,
4
+ DeviceAwareBlockAllocator)
5
+ from vllm.core.block.naive_block import NaiveBlock, NaiveBlockAllocator
6
+ from vllm.core.block.prefix_caching_block import PrefixCachingBlockAllocator
7
+ from vllm.utils import Device
8
+
9
+
10
+ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator):
11
+ """A block allocator that can allocate blocks on both CPU and GPU memory.
12
+
13
+ This class implements the `DeviceAwareBlockAllocator` interface and provides
14
+ functionality for allocating and managing blocks of memory on both CPU and
15
+ GPU devices.
16
+
17
+ The `CpuGpuBlockAllocator` maintains separate memory pools for CPU and GPU
18
+ blocks, and allows for allocation, deallocation, forking, and swapping of
19
+ blocks across these memory pools.
20
+ """
21
+
22
+ @staticmethod
23
+ def create(
24
+ allocator_type: str,
25
+ num_gpu_blocks: int,
26
+ num_cpu_blocks: int,
27
+ block_size: int,
28
+ ) -> DeviceAwareBlockAllocator:
29
+ """Creates a CpuGpuBlockAllocator instance with the specified
30
+ configuration.
31
+
32
+ This static method creates and returns a CpuGpuBlockAllocator instance
33
+ based on the provided parameters. It initializes the CPU and GPU block
34
+ allocators with the specified number of blocks, block size, and
35
+ allocator type.
36
+
37
+ Args:
38
+ allocator_type (str): The type of block allocator to use for CPU
39
+ and GPU blocks. Currently supported values are "naive" and
40
+ "prefix_caching".
41
+ num_gpu_blocks (int): The number of blocks to allocate for GPU
42
+ memory.
43
+ num_cpu_blocks (int): The number of blocks to allocate for CPU
44
+ memory.
45
+ block_size (int): The size of each block in number of tokens.
46
+
47
+ Returns:
48
+ DeviceAwareBlockAllocator: A CpuGpuBlockAllocator instance with the
49
+ specified configuration.
50
+
51
+ Notes:
52
+ - The block IDs are assigned contiguously, with GPU block IDs coming
53
+ before CPU block IDs.
54
+ """
55
+ block_ids = list(range(num_gpu_blocks + num_cpu_blocks))
56
+ gpu_block_ids = block_ids[:num_gpu_blocks]
57
+ cpu_block_ids = block_ids[num_gpu_blocks:]
58
+
59
+ if allocator_type == "naive":
60
+ gpu_allocator: BlockAllocator = NaiveBlockAllocator(
61
+ create_block=NaiveBlock, # type: ignore
62
+ num_blocks=num_gpu_blocks,
63
+ block_size=block_size,
64
+ block_ids=gpu_block_ids,
65
+ )
66
+
67
+ cpu_allocator: BlockAllocator = NaiveBlockAllocator(
68
+ create_block=NaiveBlock, # type: ignore
69
+ num_blocks=num_cpu_blocks,
70
+ block_size=block_size,
71
+ block_ids=cpu_block_ids,
72
+ )
73
+ elif allocator_type == "prefix_caching":
74
+ gpu_allocator = PrefixCachingBlockAllocator(
75
+ num_blocks=num_gpu_blocks,
76
+ block_size=block_size,
77
+ block_ids=gpu_block_ids,
78
+ )
79
+
80
+ cpu_allocator = PrefixCachingBlockAllocator(
81
+ num_blocks=num_cpu_blocks,
82
+ block_size=block_size,
83
+ block_ids=cpu_block_ids,
84
+ )
85
+ else:
86
+ raise ValueError(f"Unknown allocator type {allocator_type=}")
87
+
88
+ return CpuGpuBlockAllocator(
89
+ cpu_block_allocator=cpu_allocator,
90
+ gpu_block_allocator=gpu_allocator,
91
+ )
92
+
93
+ def __init__(
94
+ self,
95
+ cpu_block_allocator: BlockAllocator,
96
+ gpu_block_allocator: BlockAllocator,
97
+ ):
98
+ assert not (
99
+ cpu_block_allocator.all_block_ids
100
+ & gpu_block_allocator.all_block_ids
101
+ ), "cpu and gpu block allocators can't have intersection of block ids"
102
+
103
+ self._allocators = {
104
+ Device.CPU: cpu_block_allocator,
105
+ Device.GPU: gpu_block_allocator,
106
+ }
107
+
108
+ self._block_ids_to_allocator: Dict[int, BlockAllocator] = {}
109
+ for _, allocator in self._allocators.items():
110
+ for block_id in allocator.all_block_ids:
111
+ self._block_ids_to_allocator[block_id] = allocator
112
+
113
+ def allocate_mutable(self, prev_block: Optional[Block],
114
+ device: Device) -> Block:
115
+ """Allocates a new mutable block on the specified device.
116
+
117
+ Args:
118
+ prev_block (Optional[Block]): The previous block to in the sequence.
119
+ Used for prefix hashing.
120
+ device (Device): The device on which to allocate the new block.
121
+
122
+ Returns:
123
+ Block: The newly allocated mutable block.
124
+ """
125
+ return self._allocators[device].allocate_mutable(prev_block)
126
+
127
+ def allocate_immutable(self, prev_block: Optional[Block],
128
+ token_ids: List[int], device: Device) -> Block:
129
+ """Allocates a new immutable block with the provided token IDs on the
130
+ specified device.
131
+
132
+ Args:
133
+ prev_block (Optional[Block]): The previous block in the sequence.
134
+ Used for prefix hashing.
135
+ token_ids (List[int]): The list of token IDs to be stored in the new
136
+ block.
137
+ device (Device): The device on which to allocate the new block.
138
+
139
+ Returns:
140
+ Block: The newly allocated immutable block containing the provided
141
+ token IDs.
142
+ """
143
+ return self._allocators[device].allocate_immutable(
144
+ prev_block, token_ids)
145
+
146
+ def free(self, block: Block) -> None:
147
+ """Frees the memory occupied by the given block.
148
+
149
+ Args:
150
+ block (Block): The block to be freed.
151
+ """
152
+ block_id = block.block_id
153
+ assert block_id is not None
154
+ allocator = self._block_ids_to_allocator[block_id]
155
+ return allocator.free(block)
156
+
157
+ def fork(self, last_block: Block) -> List[Block]:
158
+ """Creates a new sequence of blocks that shares the same underlying
159
+ memory as the original sequence.
160
+
161
+ Args:
162
+ last_block (Block): The last block in the original sequence.
163
+
164
+ Returns:
165
+ List[Block]: A new list of blocks that shares the same memory as the
166
+ original sequence.
167
+ """
168
+ block_id = last_block.block_id
169
+ assert block_id is not None
170
+ allocator = self._block_ids_to_allocator[block_id]
171
+ return allocator.fork(last_block)
172
+
173
+ def get_num_free_blocks(self, device: Device) -> int:
174
+ """Returns the number of free blocks available on the specified device.
175
+
176
+ Args:
177
+ device (Device): The device for which to query the number of free
178
+ blocks. AssertionError is raised if None is passed.
179
+
180
+ Returns:
181
+ int: The number of free blocks available on the specified device.
182
+ """
183
+ return self._allocators[device].get_num_free_blocks()
184
+
185
+ def get_num_total_blocks(self, device: Device) -> int:
186
+ return self._allocators[device].get_num_total_blocks()
187
+
188
+ def clear_copy_on_writes(self) -> Dict[int, List[int]]:
189
+ """Clears the copy-on-write (CoW) state and returns the mapping of
190
+ source to destination block IDs.
191
+
192
+ Returns:
193
+ Dict[int, List[int]]: A dictionary mapping source block IDs to lists
194
+ of destination block IDs.
195
+ """
196
+ # CoW only supported on GPU
197
+ device = Device.GPU
198
+ return self._allocators[device].clear_copy_on_writes()
199
+
200
+ def mark_blocks_as_accessed(self, block_ids: List[int],
201
+ now: float) -> None:
202
+ """Mark blocks as accessed, only use for prefix caching."""
203
+ # Prefix caching only supported on GPU.
204
+ device = Device.GPU
205
+ return self._allocators[device].mark_blocks_as_accessed(block_ids, now)
206
+
207
+ def mark_blocks_as_computed(self, block_ids: List[int]) -> None:
208
+ """Mark blocks as accessed, only use for prefix caching."""
209
+ # Prefix caching only supported on GPU.
210
+ device = Device.GPU
211
+ return self._allocators[device].mark_blocks_as_computed(block_ids)
212
+
213
+ def get_common_computed_block_ids(
214
+ self, seq_block_ids: List[List[int]]) -> List[int]:
215
+ # Prefix caching only supported on GPU.
216
+ device = Device.GPU
217
+ return self._allocators[device].get_common_computed_block_ids(
218
+ seq_block_ids)
219
+
220
+ @property
221
+ def all_block_ids(self) -> FrozenSet[int]:
222
+ return frozenset(self._block_ids_to_allocator.keys())
223
+
224
+ def promote_to_immutable_block(self, block: Block) -> BlockId:
225
+ raise NotImplementedError
226
+
227
+ def cow_block_if_not_appendable(self, block: Block) -> Optional[BlockId]:
228
+ raise NotImplementedError
@@ -0,0 +1,205 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import Dict, FrozenSet, List, Optional, Protocol
3
+
4
+ from vllm.utils import Device
5
+
6
+ BlockId = int
7
+
8
+
9
+ class Block(ABC):
10
+
11
+ @abstractmethod
12
+ def append_token_ids(self, token_ids: List[int]) -> None:
13
+ pass
14
+
15
+ @property
16
+ @abstractmethod
17
+ def block_id(self) -> Optional[int]:
18
+ pass
19
+
20
+ @block_id.setter
21
+ @abstractmethod
22
+ def block_id(self, value: Optional[int]) -> None:
23
+ """NOTE: Do not use this API outside Block."""
24
+ self._block_id = value
25
+
26
+ @property
27
+ @abstractmethod
28
+ def token_ids(self) -> List[int]:
29
+ pass
30
+
31
+ @property
32
+ @abstractmethod
33
+ def num_empty_slots(self) -> int:
34
+ pass
35
+
36
+ @property
37
+ @abstractmethod
38
+ def is_full(self) -> bool:
39
+ pass
40
+
41
+ @property
42
+ @abstractmethod
43
+ def prev_block(self) -> Optional["Block"]:
44
+ pass
45
+
46
+ @property
47
+ @abstractmethod
48
+ def computed(self) -> bool:
49
+ raise NotImplementedError
50
+
51
+ @computed.setter
52
+ @abstractmethod
53
+ def computed(self, value) -> bool:
54
+ """Should be only used by PrefixCacingAllocator"""
55
+ raise NotImplementedError
56
+
57
+ @property
58
+ @abstractmethod
59
+ def last_accessed(self) -> float:
60
+ raise NotImplementedError
61
+
62
+ @last_accessed.setter
63
+ @abstractmethod
64
+ def last_accessed(self, last_accessed_ts: float):
65
+ raise NotImplementedError
66
+
67
+ class Factory(Protocol):
68
+
69
+ @abstractmethod
70
+ def __call__(
71
+ self,
72
+ prev_block: Optional["Block"],
73
+ token_ids: List[int],
74
+ block_size: int,
75
+ allocator: "BlockAllocator",
76
+ block_id: Optional[int] = None,
77
+ ) -> "Block":
78
+ pass
79
+
80
+ @property
81
+ @abstractmethod
82
+ def content_hash(self) -> Optional[int]:
83
+ """Return the content-based hash of the current block, or None if it is
84
+ not yet defined or not supported.
85
+
86
+ For the content-based hash to be defined, the current block must be
87
+ full.
88
+ """
89
+ return None
90
+
91
+
92
+ class BlockAllocator(ABC):
93
+
94
+ @abstractmethod
95
+ def allocate_mutable(self, prev_block: Optional[Block]) -> Block:
96
+ pass
97
+
98
+ @abstractmethod
99
+ def allocate_immutable(self, prev_block: Optional[Block],
100
+ token_ids: List[int]) -> Block:
101
+ pass
102
+
103
+ @abstractmethod
104
+ def free(self, block: Block) -> None:
105
+ pass
106
+
107
+ @abstractmethod
108
+ def fork(self, last_block: Block) -> List[Block]:
109
+ pass
110
+
111
+ @abstractmethod
112
+ def get_num_total_blocks(self) -> int:
113
+ pass
114
+
115
+ @abstractmethod
116
+ def get_num_free_blocks(self) -> int:
117
+ pass
118
+
119
+ @property
120
+ @abstractmethod
121
+ def all_block_ids(self) -> FrozenSet[int]:
122
+ pass
123
+
124
+ @abstractmethod
125
+ def clear_copy_on_writes(self) -> Dict[int, List[int]]:
126
+ pass
127
+
128
+ @abstractmethod
129
+ def mark_blocks_as_accessed(self, block_ids: List[int],
130
+ now: float) -> None:
131
+ pass
132
+
133
+ @abstractmethod
134
+ def mark_blocks_as_computed(self, block_ids: List[int]) -> None:
135
+ pass
136
+
137
+ @abstractmethod
138
+ def get_common_computed_block_ids(
139
+ self, seq_block_ids: List[List[int]]) -> List[int]:
140
+ pass
141
+
142
+ @abstractmethod
143
+ def cow_block_if_not_appendable(self, block: Block) -> Optional["BlockId"]:
144
+ """NOTE: This should not be used besides Block"""
145
+ pass
146
+
147
+ @abstractmethod
148
+ def promote_to_immutable_block(self, block: Block) -> BlockId:
149
+ """NOTE: This should not be used besides Block"""
150
+ pass
151
+
152
+ class NoFreeBlocksError(ValueError):
153
+ pass
154
+
155
+
156
+ class DeviceAwareBlockAllocator(ABC):
157
+
158
+ @abstractmethod
159
+ def allocate_mutable(self, prev_block: Optional[Block],
160
+ device: Device) -> Block:
161
+ pass
162
+
163
+ @abstractmethod
164
+ def allocate_immutable(self, prev_block: Optional[Block],
165
+ token_ids: List[int], device: Device) -> Block:
166
+ pass
167
+
168
+ @abstractmethod
169
+ def get_num_free_blocks(self, device: Device) -> int:
170
+ pass
171
+
172
+ @abstractmethod
173
+ def get_num_total_blocks(self, device: Device) -> int:
174
+ pass
175
+
176
+ @abstractmethod
177
+ def free(self, block: Block) -> None:
178
+ pass
179
+
180
+ @abstractmethod
181
+ def fork(self, last_block: Block) -> List[Block]:
182
+ pass
183
+
184
+ @property
185
+ @abstractmethod
186
+ def all_block_ids(self) -> FrozenSet[int]:
187
+ pass
188
+
189
+ @abstractmethod
190
+ def clear_copy_on_writes(self) -> Dict[int, List[int]]:
191
+ pass
192
+
193
+ @abstractmethod
194
+ def mark_blocks_as_accessed(self, block_ids: List[int],
195
+ now: float) -> None:
196
+ pass
197
+
198
+ @abstractmethod
199
+ def mark_blocks_as_computed(self, block_ids: List[int]) -> None:
200
+ pass
201
+
202
+ @abstractmethod
203
+ def get_common_computed_block_ids(
204
+ self, seq_block_ids: List[List[int]]) -> List[int]:
205
+ pass