commit_hash
string
pr_url
string
pr_date
string
timeline_extracted_at
string
analysis_extracted_at
string
models
list
perf_command
string
has_serving
bool
has_latency
bool
has_throughput
bool
uses_lm_eval
bool
commit_subject
string
commit_message
string
commit_date
string
files_changed
list
stats
dict
diff_text
string
apis
list
affected_paths
list
repo
string
hardware
string
lm_eval_command
string
fc542144c4477ffec1d3de6fa43e54f8fb5351e8
https://github.com/vllm-project/vllm/pull/12563
2025-01-31
2025-09-07 17:46:50
2025-09-07 17:46:50
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --num-prompts 100
true
false
false
true
[Feature] Fix guided decoding blocking bitmask memcpy (#12563)
[Feature] Fix guided decoding blocking bitmask memcpy (#12563) **[Guided decoding performance optimization]** Sending the guided decoding bitmask in xgrammar to the GPU (`self.token_bitmask.to(scores.device)`) is a blocking operation that prevents the CPU from pre-launching the sampler kernels. The CPU waits until decode is complete, then copies the bitmask over. This PR changes the operation to async via setting `non-blocking=True`. (Current) The CPU is blocked on a `cudaStreamSynchronize` and only pre-empts the sampling kernels after bitmask application. Below is the Nsys profile for one decode phase from Llama 3.1 8B. ![image](https://github.com/user-attachments/assets/8997eae1-b822-4f52-beb8-ef19a7c6b824) With the optimization, this is no longer the case: ![image](https://github.com/user-attachments/assets/6d5ea83f-f169-4f98-a8c1-41c719b3e1e7) ---------
2025-01-31T15:37:30-08:00
[ "vllm/model_executor/guided_decoding/xgrammar_decoding.py" ]
{ "commit_year": 2025, "num_edited_lines": 4, "num_files": 1, "num_hunks": 1, "num_non_test_edited_lines": 4, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/guided_decoding/xgrammar_decoding.py b/vllm/model_executor/guided_decoding/xgrammar_decoding.py index 2d8594cb8..ee30ce96f 100644 --- a/vllm/model_executor/guided_decoding/xgrammar_decoding.py +++ b/vllm/model_executor/guided_decoding/xgrammar_decoding.py @@ -307,8 +307,8 @@ class XGrammarLogitsProcessor: # Note: In this method, if the tensors have different dimensions # on CPU device fails, but on GPU it runs without error. Hence the # unsqueeze above for scores, to match the token bitmask shape - xgr.apply_token_bitmask_inplace(scores, - self.token_bitmask.to(scores.device)) + xgr.apply_token_bitmask_inplace( + scores, self.token_bitmask.to(scores.device, non_blocking=True)) if device_type != "cuda": scores = scores.to(dtype).to(device_type).squeeze()
[ "None" ]
[ "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/llm.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
fa63e710c7fbaae3a445f669d3b5ba6b9a4ef412
https://github.com/vllm-project/vllm/pull/12094
2025-01-26
2025-09-07 17:46:54
2025-09-07 17:46:54
[ "meta-llama/Meta-Llama-3-8B" ]
VLLM_USE_V1=1 python benchmarks/benchmark_latency.py --model meta-llama/Meta-Llama-3-8B --tensor-parallel-size 1 --input-len 1000 --batch-size 32
false
true
false
true
[V1][Perf] Reduce scheduling overhead in model runner after cuda sync (#12094)
[V1][Perf] Reduce scheduling overhead in model runner after cuda sync (#12094)
2025-01-26T00:42:37-08:00
[ "vllm/v1/outputs.py", "vllm/v1/sample/sampler.py", "vllm/v1/worker/gpu_model_runner.py" ]
{ "commit_year": 2025, "num_edited_lines": 34, "num_files": 3, "num_hunks": 6, "num_non_test_edited_lines": 34, "num_non_test_files": 3, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/v1/outputs.py b/vllm/v1/outputs.py index acc3a944e..32aee44e3 100644 --- a/vllm/v1/outputs.py +++ b/vllm/v1/outputs.py @@ -8,7 +8,7 @@ import torch class SamplerOutput: # [num_reqs] - sampled_token_ids: List[int] + sampled_token_ids: torch.Tensor # [num_reqs, max_num_logprobs + 1] logprob_token_ids: Optional[torch.Tensor] diff --git a/vllm/v1/sample/sampler.py b/vllm/v1/sample/sampler.py index 7cd42ca21..9ad665a64 100644 --- a/vllm/v1/sample/sampler.py +++ b/vllm/v1/sample/sampler.py @@ -50,9 +50,8 @@ class Sampler(nn.Module): # Use int32 to reduce the tensor size. sampled = sampled.to(torch.int32) - # NOTE: CPU-GPU synchronization happens here. sampler_output = SamplerOutput( - sampled_token_ids=sampled.tolist(), + sampled_token_ids=sampled, logprob_token_ids=topk_indices, logprobs=topk_logprobs, prompt_logprob_token_ids=None, diff --git a/vllm/v1/worker/gpu_model_runner.py b/vllm/v1/worker/gpu_model_runner.py index 4b3c325de..6339f1f03 100644 --- a/vllm/v1/worker/gpu_model_runner.py +++ b/vllm/v1/worker/gpu_model_runner.py @@ -775,10 +775,10 @@ class GPUModelRunner: sampling_metadata=sampling_metadata, ) - sampled_token_ids = sampler_output.sampled_token_ids # TODO(woosuk): The following loop can be slow since it iterates over # the requests one by one. Optimize. num_reqs = self.input_batch.num_reqs + request_seq_lens: List[Tuple[int, CachedRequestState, int]] = [] for i, req_id in enumerate(self.input_batch.req_ids[:num_reqs]): assert req_id is not None req_state = self.requests[req_id] @@ -787,10 +787,10 @@ class GPUModelRunner: assert seq_len <= req_state.num_tokens if seq_len == req_state.num_tokens: # Append the sampled token to the output token ids. - token_id = sampled_token_ids[i] - self.input_batch.token_ids_cpu[i, seq_len] = token_id self.input_batch.num_tokens[i] += 1 - req_state.output_token_ids.append(token_id) + # OPTIMIZATION: Priming the state updates for later updates. + req_state.output_token_ids.append(0) + request_seq_lens.append((i, req_state, seq_len)) else: # Ignore the sampled token from the partial request. # Rewind the generator state as if the token was not sampled. @@ -799,6 +799,21 @@ class GPUModelRunner: # This relies on cuda-specific torch-internal impl details generator.set_offset(generator.get_offset() - 4) + # num_reqs entries should be non-None + assert all( + req_id is not None for req_id in + self.input_batch.req_ids[:num_reqs]), "req_ids contains None" + req_ids = cast(List[str], self.input_batch.req_ids[:num_reqs]) + + # NOTE: GPU -> CPU Sync happens here. + # Move as many CPU operations as possible before this sync point. + sampled_token_ids = sampler_output.sampled_token_ids.tolist() + # Update with the actual token ids + for i, req_state, seq_len in request_seq_lens: + token_id = sampled_token_ids[i] + self.input_batch.token_ids_cpu[i, seq_len] = token_id + req_state.output_token_ids[-1] = token_id + if sampler_output.logprob_token_ids is None: logprob_token_ids = None else: @@ -808,12 +823,6 @@ class GPUModelRunner: else: logprobs = sampler_output.logprobs.cpu() - # num_reqs entries should be non-None - assert all( - req_id is not None for req_id in - self.input_batch.req_ids[:num_reqs]), "req_ids contains None" - req_ids = cast(List[str], self.input_batch.req_ids[:num_reqs]) - model_runner_output = ModelRunnerOutput( req_ids=req_ids, req_id_to_index=self.input_batch.req_id_to_index,
[ "vllm.v1.outputs.SamplerOutput", "vllm.v1.sample.sampler.Sampler.forward", "vllm.v1.worker.GPUModelRunner.execute_model" ]
[ "vllm/v1/worker/gpu_model_runner.py", "vllm/v1/sample/sampler.py", "vllm/model_executor/layers/sampler.py", "vllm/v1/sample/tpu/sampler.py", "vllm/outputs.py", "vllm/v1/outputs.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Meta-Llama-3-8B,dtype=auto --tasks gsm8k --batch_size auto --limit 100
6dd94dbe94c1820a1e224cba65efcf0befa97995
https://github.com/vllm-project/vllm/pull/12380
2025-01-24
2025-09-07 17:46:57
2025-09-07 17:46:57
[ "meta-llama/Meta-Llama-3-8B" ]
python benchmarks/benchmark_latency.py --model meta-llama/Meta-Llama-3-8B --load-format dummy
false
true
false
true
[perf] fix perf regression from #12253 (#12380)
[perf] fix perf regression from #12253 (#12380)
2025-01-24T11:34:27+08:00
[ "vllm/worker/model_runner.py" ]
{ "commit_year": 2025, "num_edited_lines": 5, "num_files": 1, "num_hunks": 2, "num_non_test_edited_lines": 5, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/worker/model_runner.py b/vllm/worker/model_runner.py index cf2f1c6b3..bf1a40d48 100644 --- a/vllm/worker/model_runner.py +++ b/vllm/worker/model_runner.py @@ -455,7 +455,6 @@ class ModelInputForGPUBuilder(ModelRunnerInputBuilderBase[ModelInputForGPU]): self.enable_prompt_adapter = (self.runner.prompt_adapter_config is not None) self.multi_modal_input_mapper = self.runner.multi_modal_input_mapper - self.decode_only = True # Attention metadata inputs. if self.attn_backend is not None: @@ -477,6 +476,10 @@ class ModelInputForGPUBuilder(ModelRunnerInputBuilderBase[ModelInputForGPU]): finished_requests_ids: Optional[List[str]] = None) -> None: self.finished_requests_ids = finished_requests_ids + # if the current batch is decode-only. + # will be set to False if there is any non-decode request. + self.decode_only = True + # Intermediate data (data in CPU before going to GPU) for # the current sequence group. self.inter_data_list: List[
[ "vllm.worker.model_runner.ModelInputForGPUBuilder.__init__" ]
[ "vllm/worker/model_runner.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/openai/serving_completion.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Meta-Llama-3-8B,dtype=auto --tasks gsm8k --batch_size auto --limit 100
310aca88c984983189a57f1b72e3b1dde89fb92f
https://github.com/vllm-project/vllm/pull/11870
2025-01-09
2025-09-07 17:47:12
2025-09-07 17:47:12
[ "meta-llama/Meta-Llama-3-70B" ]
python benchmarks/benchmark_latency.py --model meta-llama/Meta-Llama-3-70B --load-format dummy --enforce-eager -tp 4
false
true
false
true
[perf]fix current stream (#11870)
[perf]fix current stream (#11870)
2025-01-09T07:18:21Z
[ "vllm/distributed/device_communicators/pynccl.py", "vllm/distributed/parallel_state.py", "vllm/utils.py", "vllm/worker/multi_step_model_runner.py" ]
{ "commit_year": 2025, "num_edited_lines": 61, "num_files": 4, "num_hunks": 14, "num_non_test_edited_lines": 61, "num_non_test_files": 4, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/distributed/device_communicators/pynccl.py b/vllm/distributed/device_communicators/pynccl.py index fda4d007c..efc599871 100644 --- a/vllm/distributed/device_communicators/pynccl.py +++ b/vllm/distributed/device_communicators/pynccl.py @@ -10,6 +10,7 @@ from vllm.distributed.device_communicators.pynccl_wrapper import ( ncclRedOpTypeEnum, ncclUniqueId) from vllm.distributed.utils import StatelessProcessGroup from vllm.logger import init_logger +from vllm.utils import current_stream logger = init_logger(__name__) @@ -96,7 +97,7 @@ class PyNcclCommunicator: self.comm: ncclComm_t = self.nccl.ncclCommInitRank( self.world_size, self.unique_id, self.rank) - stream = torch.cuda.current_stream() + stream = current_stream() # A small all_reduce for warmup. data = torch.zeros(1, device=device) self.all_reduce(data) @@ -119,7 +120,7 @@ class PyNcclCommunicator: out_tensor = torch.empty_like(in_tensor) if stream is None: - stream = torch.cuda.current_stream() + stream = current_stream() self.nccl.ncclAllReduce(buffer_type(in_tensor.data_ptr()), buffer_type(out_tensor.data_ptr()), in_tensor.numel(), @@ -141,7 +142,7 @@ class PyNcclCommunicator: f"this nccl communicator is created to work on {self.device}, " f"but the input tensor is on {input_tensor.device}") if stream is None: - stream = torch.cuda.current_stream() + stream = current_stream() self.nccl.ncclAllGather( buffer_type(input_tensor.data_ptr()), buffer_type(output_tensor.data_ptr()), input_tensor.numel(), @@ -162,7 +163,7 @@ class PyNcclCommunicator: f"this nccl communicator is created to work on {self.device}, " f"but the input tensor is on {input_tensor.device}") if stream is None: - stream = torch.cuda.current_stream() + stream = current_stream() self.nccl.ncclReduceScatter( buffer_type(input_tensor.data_ptr()), buffer_type(output_tensor.data_ptr()), output_tensor.numel(), @@ -177,7 +178,7 @@ class PyNcclCommunicator: f"this nccl communicator is created to work on {self.device}, " f"but the input tensor is on {tensor.device}") if stream is None: - stream = torch.cuda.current_stream() + stream = current_stream() self.nccl.ncclSend(buffer_type(tensor.data_ptr()), tensor.numel(), ncclDataTypeEnum.from_torch(tensor.dtype), dst, self.comm, cudaStream_t(stream.cuda_stream)) @@ -189,7 +190,7 @@ class PyNcclCommunicator: f"this nccl communicator is created to work on {self.device}, " f"but the input tensor is on {tensor.device}") if stream is None: - stream = torch.cuda.current_stream() + stream = current_stream() self.nccl.ncclRecv(buffer_type(tensor.data_ptr()), tensor.numel(), ncclDataTypeEnum.from_torch(tensor.dtype), src, self.comm, cudaStream_t(stream.cuda_stream)) @@ -201,7 +202,7 @@ class PyNcclCommunicator: f"this nccl communicator is created to work on {self.device}, " f"but the input tensor is on {tensor.device}") if stream is None: - stream = torch.cuda.current_stream() + stream = current_stream() if src == self.rank: sendbuff = buffer_type(tensor.data_ptr()) # NCCL requires the sender also to have a receive buffer diff --git a/vllm/distributed/parallel_state.py b/vllm/distributed/parallel_state.py index a837c1dc5..be7f16ef5 100644 --- a/vllm/distributed/parallel_state.py +++ b/vllm/distributed/parallel_state.py @@ -357,10 +357,7 @@ class GroupCoordinator: return out pynccl_comm = self.pynccl_comm assert pynccl_comm is not None - # TODO: pynccl should not use `stream=` - # it can just always use the current stream. - out = pynccl_comm.all_reduce(input_, - stream=torch.cuda.current_stream()) + out = pynccl_comm.all_reduce(input_) if out is None: # fall back to the default all-reduce using PyTorch. # this usually happens during testing. diff --git a/vllm/utils.py b/vllm/utils.py index a92b77efd..0b0905e67 100644 --- a/vllm/utils.py +++ b/vllm/utils.py @@ -944,6 +944,39 @@ def find_nccl_library() -> str: return so_file +prev_set_stream = torch.cuda.set_stream + +_current_stream = None + + +def _patched_set_stream(stream: torch.cuda.Stream) -> None: + global _current_stream + _current_stream = stream + prev_set_stream(stream) + + +torch.cuda.set_stream = _patched_set_stream + + +def current_stream() -> torch.cuda.Stream: + """ + replace `torch.cuda.current_stream()` with `vllm.utils.current_stream()`. + it turns out that `torch.cuda.current_stream()` is quite expensive, + as it will construct a new stream object at each call. + here we patch `torch.cuda.set_stream` to keep track of the current stream + directly, so that we can avoid calling `torch.cuda.current_stream()`. + + the underlying hypothesis is that we do not call `torch._C._cuda_setStream` + from C/C++ code. + """ + global _current_stream + if _current_stream is None: + # when this function is called before any stream is set, + # we return the default stream. + _current_stream = torch.cuda.current_stream() + return _current_stream + + def enable_trace_function_call_for_thread(vllm_config: "VllmConfig") -> None: """Set up function tracing for the current thread, if enabled via the VLLM_TRACE_FUNCTION environment variable diff --git a/vllm/worker/multi_step_model_runner.py b/vllm/worker/multi_step_model_runner.py index a2c2cebf8..acce92349 100644 --- a/vllm/worker/multi_step_model_runner.py +++ b/vllm/worker/multi_step_model_runner.py @@ -14,7 +14,7 @@ from vllm.model_executor.layers.sampler import (PromptLogprobs, SampleLogprobs, get_pythonized_sample_results) from vllm.sequence import (CompletionSequenceGroupOutput, IntermediateTensors, Logprob, SequenceGroupMetadata, SequenceOutput) -from vllm.utils import PyObjectCache, async_tensor_h2d +from vllm.utils import PyObjectCache, async_tensor_h2d, current_stream from vllm.worker.model_runner import (GPUModelRunnerBase, ModelInputForGPUWithSamplingMetadata) from vllm.worker.model_runner_base import ( @@ -498,7 +498,7 @@ class MultiStepModelRunner(GPUModelRunnerBase[StatefulModelInput]): # appended sampler output from last iteration # - also maybe pythonize if CPU is ahead of GPU - current_stream = torch.cuda.current_stream() + stream = current_stream() if not model_input.is_first_multi_step: # Explicitly block on the previous step's forward to make sure we # don't clobber any GPU tensors still in use. @@ -541,7 +541,7 @@ class MultiStepModelRunner(GPUModelRunnerBase[StatefulModelInput]): num_steps=1) # record the event for the current step so that the next step can sync - model_input.record_step_event(current_stream) + model_input.record_step_event(stream) if get_pp_group().is_last_rank and self.is_driver_worker: assert isinstance(output, list) @@ -552,7 +552,7 @@ class MultiStepModelRunner(GPUModelRunnerBase[StatefulModelInput]): # event for the pythonization so that we only pythonize if the # tensors are ready. May be able to be combined with the step event output_ready_event = torch.cuda.Event() - output_ready_event.record(current_stream) + output_ready_event.record(stream) if self.parallel_config.pipeline_parallel_size > 1: output[0].sampled_token_ids_cpu = output[ 0].sampled_token_ids.cpu()
[ "vllm.distributed.device_communicators.pynccl.PyNcclCommunicator.all_reduce", "vllm.utils.current_stream", "vllm.worker.multi_step_model_runner.MultiStepModelRunner.execute_model" ]
[ "vllm/distributed/device_communicators/pynccl.py", "vllm/distributed/parallel_state.py", "vllm/worker/multi_step_model_runner.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Meta-Llama-3-70B,dtype=auto --tasks gsm8k --batch_size auto --limit 100
b55ed6ef8ab0dce7fb0f79ff292dafdb4d22610c
https://github.com/vllm-project/vllm/pull/11692
2025-01-02
2025-09-07 17:47:18
2025-09-07 17:47:18
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm
true
false
false
true
[V1][Minor] Optimize token_ids_cpu copy (#11692)
[V1][Minor] Optimize token_ids_cpu copy (#11692)
2025-01-02T12:04:58-07:00
[ "vllm/v1/worker/gpu_input_batch.py", "vllm/v1/worker/gpu_model_runner.py" ]
{ "commit_year": 2025, "num_edited_lines": 14, "num_files": 2, "num_hunks": 4, "num_non_test_edited_lines": 14, "num_non_test_files": 2, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/v1/worker/gpu_input_batch.py b/vllm/v1/worker/gpu_input_batch.py index e79145300..f8a1427c6 100644 --- a/vllm/v1/worker/gpu_input_batch.py +++ b/vllm/v1/worker/gpu_input_batch.py @@ -66,8 +66,9 @@ class InputBatch: pin_memory=False, ) self.token_ids_cpu = self.token_ids_cpu_tensor.numpy() - self.num_computed_tokens_cpu = np.empty(max_num_reqs, dtype=np.int32) + self.num_tokens = np.zeros(max_num_reqs, dtype=np.int32) self.num_prompt_tokens = np.zeros(max_num_reqs, dtype=np.int32) + self.num_computed_tokens_cpu = np.empty(max_num_reqs, dtype=np.int32) # Attention-related. self.block_table = torch.zeros( @@ -189,6 +190,7 @@ class InputBatch: end_idx = start_idx + len(request.output_token_ids) self.token_ids_cpu[req_index, start_idx:end_idx] = request.output_token_ids + self.num_tokens[req_index] = request.num_tokens self.num_computed_tokens_cpu[req_index] = request.num_computed_tokens num_blocks = len(request.block_ids) @@ -290,14 +292,15 @@ class InputBatch: self.req_ids[last_req_index] = None self.req_id_to_index[req_id] = empty_index - # TODO(woosuk): Optimize the copy of token_ids_cpu and - # block_table_cpu. - self.token_ids_cpu[empty_index] = self.token_ids_cpu[ - last_req_index] + num_tokens = self.num_tokens[last_req_index] + self.token_ids_cpu[empty_index, :num_tokens] = self.token_ids_cpu[ + last_req_index, :num_tokens] + self.num_tokens[empty_index] = num_tokens self.num_prompt_tokens[empty_index] = \ self.num_prompt_tokens[last_req_index] self.num_computed_tokens_cpu[ empty_index] = self.num_computed_tokens_cpu[last_req_index] + # TODO(woosuk): Optimize the copy of block_table_cpu. self.block_table_cpu[empty_index] = self.block_table_cpu[ last_req_index] self.temperature_cpu[empty_index] = self.temperature_cpu[ diff --git a/vllm/v1/worker/gpu_model_runner.py b/vllm/v1/worker/gpu_model_runner.py index 995de54e8..75098b033 100644 --- a/vllm/v1/worker/gpu_model_runner.py +++ b/vllm/v1/worker/gpu_model_runner.py @@ -644,6 +644,7 @@ class GPUModelRunner: # Append the sampled token to the output token ids. token_id = sampled_token_ids[i] self.input_batch.token_ids_cpu[i, seq_len] = token_id + self.input_batch.num_tokens[i] += 1 req_state.output_token_ids.append(token_id) else: # Ignore the sampled token from the partial request.
[ "InputBatch.add_request", "InputBatch.condense", "GPUModelRunner._update_states" ]
[ "vllm/v1/worker/gpu_input_batch.py", "vllm/v1/worker/gpu_model_runner.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
3b61cb450d899dc423feb264c297d4d18d701678
https://github.com/vllm-project/vllm/pull/10989
2024-12-09
2025-09-07 17:47:34
2025-09-07 17:47:34
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_latency.py --model meta-llama/Llama-3.1-8B-Instruct --batch-size 32 --input-len 512 --output-len 128
false
true
false
true
[V1] Further reduce CPU overheads in flash-attn (#10989)
[V1] Further reduce CPU overheads in flash-attn (#10989)
2024-12-09T12:38:46-08:00
[ "csrc/cache_kernels.cu", "vllm/v1/attention/backends/flash_attn.py" ]
{ "commit_year": 2024, "num_edited_lines": 35, "num_files": 2, "num_hunks": 2, "num_non_test_edited_lines": 35, "num_non_test_files": 2, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/csrc/cache_kernels.cu b/csrc/cache_kernels.cu index 1be806bbf..8a95279f9 100644 --- a/csrc/cache_kernels.cu +++ b/csrc/cache_kernels.cu @@ -307,10 +307,20 @@ void reshape_and_cache_flash( torch::Tensor& key_cache, // [num_blocks, block_size, num_heads, head_size] torch::Tensor& value_cache, // [num_blocks, block_size, num_heads, head_size] - torch::Tensor& slot_mapping, // [num_tokens] + torch::Tensor& slot_mapping, // [num_tokens] or [num_actual_tokens] const std::string& kv_cache_dtype, const double k_scale, const double v_scale) { - int num_tokens = key.size(0); + // NOTE(woosuk): In vLLM V1, key.size(0) can be different from + // slot_mapping.size(0) because of padding for CUDA graphs. + // In vLLM V0, key.size(0) is always equal to slot_mapping.size(0) because + // both include padding. + // In vLLM V1, however, key.size(0) can be larger than slot_mapping.size(0) + // since key includes padding for CUDA graphs, while slot_mapping does not. + // In this case, slot_mapping.size(0) represents the actual number of tokens + // before padding. + // For compatibility with both cases, we use slot_mapping.size(0) as the + // number of tokens. + int num_tokens = slot_mapping.size(0); int num_heads = key.size(1); int head_size = key.size(2); int block_size = key_cache.size(1); diff --git a/vllm/v1/attention/backends/flash_attn.py b/vllm/v1/attention/backends/flash_attn.py index d37989055..251a103e6 100644 --- a/vllm/v1/attention/backends/flash_attn.py +++ b/vllm/v1/attention/backends/flash_attn.py @@ -138,14 +138,25 @@ class FlashAttentionImpl(AttentionImpl): # Profiling run. return output - num_actual_tokens = attn_metadata.num_actual_tokens + # IMPORTANT! + # NOTE(woosuk): With piece-wise CUDA graphs, this method is executed in + # eager-mode PyTorch. Thus, we need to be careful about any CPU overhead + # in this method. For example, `view` and `slice` (or `[:n]`) operations + # are surprisingly slow even in the case they do not invoke any GPU ops. + # Minimize the PyTorch ops in this method as much as possible. + # Whenever making a change in this method, please benchmark the + # performance to make sure it does not introduce any overhead. + num_actual_tokens = attn_metadata.num_actual_tokens # Reshape the input keys and values and store them in the cache. - key_cache = kv_cache[0] - value_cache = kv_cache[1] + # NOTE(woosuk): Here, key and value are padded while slot_mapping is + # not padded. However, we don't need to do key[:num_actual_tokens] and + # value[:num_actual_tokens] because the reshape_and_cache_flash op uses + # the slot_mapping's shape to determine the number of actual tokens. + key_cache, value_cache = kv_cache.unbind(0) torch.ops._C_cache_ops.reshape_and_cache_flash( - key[:num_actual_tokens], - value[:num_actual_tokens], + key, + value, key_cache, value_cache, attn_metadata.slot_mapping,
[ "vllm.v1.attention.backends.flash_attn.FlashAttentionImpl.forward", "torch.ops._C_cache_ops.reshape_and_cache_flash" ]
[ "vllm/attention/backends/flash_attn.py", "vllm/v1/attention/backends/flash_attn.py", "vllm/_custom_ops.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
98f47f2a4032f8c395268de80858c64ffcfc60fa
https://github.com/vllm-project/vllm/pull/10733
2024-11-28
2025-09-07 17:47:41
2025-09-07 17:47:41
[ "facebook/opt-125m" ]
python benchmarks/benchmark_latency.py --model facebook/opt-125m
false
true
false
true
[V1] Optimize the CPU overheads in FlashAttention custom op (#10733)
[V1] Optimize the CPU overheads in FlashAttention custom op (#10733)
2024-11-28T09:01:02-08:00
[ "vllm/v1/attention/backends/flash_attn.py" ]
{ "commit_year": 2024, "num_edited_lines": 17, "num_files": 1, "num_hunks": 4, "num_non_test_edited_lines": 17, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/v1/attention/backends/flash_attn.py b/vllm/v1/attention/backends/flash_attn.py index 5f8535eaa..e618edf7d 100644 --- a/vllm/v1/attention/backends/flash_attn.py +++ b/vllm/v1/attention/backends/flash_attn.py @@ -135,6 +135,13 @@ class FlashAttentionImpl(AttentionImpl): assert k_scale == 1.0 and v_scale == 1.0, ( "key/v_scale is not supported in FlashAttention.") + # Reshape the query, key, and value tensors. + # NOTE(woosuk): We do this outside the custom op to minimize the CPU + # overheads from the non-CUDA-graph regions. + query = query.view(-1, self.num_heads, self.head_size) + key = key.view(-1, self.num_kv_heads, self.head_size) + value = value.view(-1, self.num_kv_heads, self.head_size) + output = torch.empty_like(query) torch.ops.vllm.unified_v1_flash_attention( output, @@ -153,7 +160,7 @@ class FlashAttentionImpl(AttentionImpl): self.alibi_slopes, self.logits_soft_cap, ) - return output + return output.view(-1, self.num_heads * self.head_size) def unified_v1_flash_attention( @@ -184,11 +191,6 @@ def unified_v1_flash_attention( attn_metadata: FlashAttentionMetadata = current_metadata num_actual_tokens = attn_metadata.num_actual_tokens - # Reshape the query, key, and value tensors. - query = query.view(-1, num_heads, head_size) - key = key.view(-1, num_kv_heads, head_size) - value = value.view(-1, num_kv_heads, head_size) - # Reshape the input keys and values and store them in the cache. key_cache = kv_cache[0] value_cache = kv_cache[1] @@ -218,8 +220,7 @@ def unified_v1_flash_attention( block_table=attn_metadata.block_table, softcap=logits_soft_cap, ) - attn_output = attn_output.view(num_actual_tokens, -1) - # TODO(woosuk): Optimize this. + # TODO(woosuk): Remove this unnecessary copy. output[:num_actual_tokens].copy_(attn_output)
[ "vllm.v1.attention.backends.flash_attn.FlashAttentionImpl.forward", "vllm.v1.attention.backends.flash_attn.unified_v1_flash_attention" ]
[ "vllm/attention/backends/flash_attn.py", "vllm/v1/attention/backends/flash_attn.py", "vllm/_custom_ops.py", "csrc/torch_bindings.cpp", "csrc/cpu/torch_bindings.cpp", "csrc/rocm/torch_bindings.cpp", "csrc/moe/torch_bindings.cpp" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=facebook/opt-125m,dtype=auto --tasks gsm8k --batch_size auto --limit 100
8c1e77fb585c4f42783a3d88c1efc7c9e15fd89f
https://github.com/vllm-project/vllm/pull/10742
2024-11-28
2025-09-07 17:47:44
2025-09-07 17:47:44
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_latency.py --model meta-llama/Llama-3.1-8B-Instruct --batch-size 32 --input-len 512 --output-len 128
false
true
false
true
[Kernel] Update vllm-flash-attn version to reduce CPU overheads (#10742)
[Kernel] Update vllm-flash-attn version to reduce CPU overheads (#10742)
2024-11-28T08:31:28-08:00
[ "CMakeLists.txt" ]
{ "commit_year": 2024, "num_edited_lines": 2, "num_files": 1, "num_hunks": 1, "num_non_test_edited_lines": 2, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/CMakeLists.txt b/CMakeLists.txt index 45a3b484e..f43bf8143 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -522,7 +522,7 @@ else() FetchContent_Declare( vllm-flash-attn GIT_REPOSITORY https://github.com/vllm-project/flash-attention.git - GIT_TAG d886f88165702b3c7e7744502772cd98b06be9e1 + GIT_TAG fdf6d72b48aea41f4ae6a89139a453dae554abc8 GIT_PROGRESS TRUE # Don't share the vllm-flash-attn build between build types BINARY_DIR ${CMAKE_BINARY_DIR}/vllm-flash-attn
[ "None" ]
[ "vllm/attention/backends/flash_attn.py", "vllm/v1/attention/backends/flash_attn.py", "vllm/attention/ops/triton_flash_attention.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
6e36f4fa6ce64619b9ea94c88a157f5783a63a65
https://github.com/vllm-project/vllm/pull/7874
2024-09-02
2025-09-07 17:48:01
2025-09-07 17:48:01
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100
true
false
false
true
improve chunked prefill performance
improve chunked prefill performance [Bugfix] Fix #7592 vllm 0.5.4 enable_chunked_prefill throughput is slightly lower than 0.5.3~0.5.0. (#7874)
2024-09-02T14:20:12-07:00
[ "tests/basic_correctness/test_chunked_prefill.py", "vllm/core/scheduler.py" ]
{ "commit_year": 2024, "num_edited_lines": 18, "num_files": 2, "num_hunks": 2, "num_non_test_edited_lines": 15, "num_non_test_files": 1, "num_test_files": 1, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/basic_correctness/test_chunked_prefill.py b/tests/basic_correctness/test_chunked_prefill.py index fc6f829c3..a63ac380e 100644 --- a/tests/basic_correctness/test_chunked_prefill.py +++ b/tests/basic_correctness/test_chunked_prefill.py @@ -116,6 +116,9 @@ def test_models_with_fp8_kv_cache( pytest.skip( "#7378: CUDA illegal memory access (undiagnosed) facebook/opt-125m" ) + if ((model, kv_cache_dtype, chunked_prefill_token_size) == ( + "nm-testing/Qwen2-1.5B-Instruct-FP8-K-V", "fp8_e4m3", 4)): + pytest.skip("flakey test, see: #7874 #8051") max_num_seqs = chunked_prefill_token_size max_num_batched_tokens = chunked_prefill_token_size diff --git a/vllm/core/scheduler.py b/vllm/core/scheduler.py index 4c2f71582..81c78bda3 100644 --- a/vllm/core/scheduler.py +++ b/vllm/core/scheduler.py @@ -1027,16 +1027,21 @@ class Scheduler: # Update waiting requests. self.waiting.extendleft(running_scheduled.preempted) + # Update new running requests. - self.running.extend([s.seq_group for s in prefills.seq_groups]) - self.running.extend( - [s.seq_group for s in running_scheduled.decode_seq_groups]) - self.running.extend( - [s.seq_group for s in running_scheduled.prefill_seq_groups]) + # By default, vLLM scheduler prioritizes prefills. + # Once chunked prefill is enabled, + # the policy is changed to prioritize decode requests. self.running.extend( [s.seq_group for s in swapped_in.decode_seq_groups]) self.running.extend( [s.seq_group for s in swapped_in.prefill_seq_groups]) + self.running.extend( + [s.seq_group for s in running_scheduled.decode_seq_groups]) + self.running.extend( + [s.seq_group for s in running_scheduled.prefill_seq_groups]) + self.running.extend([s.seq_group for s in prefills.seq_groups]) + # Update swapped requests. self.swapped.extend(running_scheduled.swapped_out) return SchedulerOutputs(
[ "vllm.core.scheduler.Scheduler.schedule", "vllm.core.scheduler.SchedulerOutputs" ]
[ "vllm/core/scheduler.py", "vllm/v1/core/sched/scheduler.py", "vllm/attention/ops/chunked_prefill_paged_decode.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
e3580537a41a46b0f3cd750b86b633c1857a8c90
https://github.com/vllm-project/vllm/pull/7753
2024-08-28
2025-09-07 17:48:09
2025-09-07 17:48:09
[ "RedHatAI/Meta-Llama-3-8B-Instruct-FP8" ]
python benchmarks/benchmark_serving.py --model RedHatAI/Meta-Llama-3-8B-Instruct-FP8 --enable-prefix-caching --enable-chunked-prefill --max-num-batched-tokens 2048
true
false
false
true
[Performance] Enable chunked prefill and prefix caching together (#7753)
[Performance] Enable chunked prefill and prefix caching together (#7753)
2024-08-28T00:36:31-07:00
[ "tests/basic_correctness/test_chunked_prefill.py", "tests/core/test_block_manager.py", "tests/core/test_chunked_prefill_scheduler.py", "vllm/core/block_manager_v1.py", "vllm/core/block_manager_v2.py", "vllm/core/embedding_model_block_manager.py", "vllm/core/interfaces.py", "vllm/core/scheduler.py", "vllm/worker/model_runner.py" ]
{ "commit_year": 2024, "num_edited_lines": 252, "num_files": 9, "num_hunks": 12, "num_non_test_edited_lines": 107, "num_non_test_files": 6, "num_test_files": 3, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/basic_correctness/test_chunked_prefill.py b/tests/basic_correctness/test_chunked_prefill.py index 1211e6ba5..fc6f829c3 100644 --- a/tests/basic_correctness/test_chunked_prefill.py +++ b/tests/basic_correctness/test_chunked_prefill.py @@ -6,6 +6,7 @@ prefill requests are chunked. Run `pytest tests/models/test_chunked_prefill.py`. """ +from contextlib import nullcontext import pytest @@ -156,3 +157,68 @@ def test_models_with_fp8_kv_cache( name_0="no_chunked_prefill", name_1="chunked_prefill", ) + + +@pytest.mark.parametrize("max_tokens", [16]) +@pytest.mark.parametrize("enforce_eager", [False]) +@pytest.mark.parametrize("chunk_size", [30, 32]) +@pytest.mark.parametrize("use_v2_block_manager", [False, True]) +# NOTE: Increasing this in this suite will fail CI because we currently cannot +# reset distributed env properly. Use a value > 1 just when you test. +@pytest.mark.parametrize("tensor_parallel_size", [1]) +def test_with_prefix_caching( + vllm_runner, + max_tokens: int, + enforce_eager: bool, + chunk_size: int, + use_v2_block_manager: bool, + tensor_parallel_size: int, +) -> None: + """ + Checks exact match decode with and without prefix caching + with chunked prefill enabled. + """ + model = "meta-llama/Llama-2-7b-chat-hf" + # The common prompt has 142 tokens with Llama-2 tokenizer. + common_prompt = "You are a helpful AI assistant " * 20 + unique_prompts = [ + "Question", # Warmup + "Question", # Fully cached + "Another question", # Partial cached + ] + full_prompts = [f"{common_prompt}\n{p}" for p in unique_prompts] + + max_num_batched_tokens = max_num_seqs = chunk_size + outputs = {} # type: ignore + check_result = True + for enable in (True, False): + with vllm_runner( + model, + dtype="half", + max_num_batched_tokens=max_num_batched_tokens, + enable_chunked_prefill=True, + enable_prefix_caching=enable, + tensor_parallel_size=tensor_parallel_size, + use_v2_block_manager=use_v2_block_manager, + enforce_eager=enforce_eager, + max_num_seqs=max_num_seqs, + ) as vllm_model: + # It should fail when prefix caching is enable and chunk + # size is not a multiple of block size (16). + should_fail = chunk_size % 16 != 0 and enable + check_result &= not should_fail + outputs[enable] = [] + # Send the request one-by-one to ensure the cache is populated. + with pytest.raises(ValueError) if should_fail else nullcontext(): + for prompt in full_prompts: + outputs[enable] += vllm_model.generate_greedy([prompt], + max_tokens) + + # Check results only if we did not expect a failure. + if check_result: + check_outputs_equal( + outputs_0_lst=outputs[False], + outputs_1_lst=outputs[True], + name_0="w/o prefix caching", + name_1="with prefix caching", + ) diff --git a/tests/core/test_block_manager.py b/tests/core/test_block_manager.py index cd306b9e4..2ee9f2082 100644 --- a/tests/core/test_block_manager.py +++ b/tests/core/test_block_manager.py @@ -595,3 +595,43 @@ def test_sliding_window_multi_seq(): # assert all blocks are free now assert block_manager.get_num_free_gpu_blocks() == num_gpu_blocks + + +def test_mark_blocks_as_computed_with_prefix_cache_and_chunked_prefill(): + """When prefix cache and chunked prefill are enabled, the block manager + should only mark a chunk of blocks as computed instead of all blocks. + """ + + block_size = 4 + num_cpu_blocks = 0 + num_gpu_blocks = 16 + block_manager = BlockSpaceManagerV1(block_size, + num_gpu_blocks, + num_cpu_blocks, + watermark=0, + enable_caching=True) + + # Set prompt size to have num_gpu_blocks - 1 full blocks. + prompt_length = block_size * num_gpu_blocks - 1 + + # Allocate (reserve) all blocks. + _, seq_group = create_dummy_prompt("0", + prompt_length, + block_size=block_size) + block_manager.allocate(seq_group) + assert seq_group.seqs[0].n_blocks == num_gpu_blocks + + # 1st chunk: Compute 2 and half blocks. Should mark 2 blocks as computed. + token_chunk_size = int(block_size * 2.5) + block_manager.mark_blocks_as_computed(seq_group, token_chunk_size) + computed_blocks = block_manager.get_all_computed_blocks(seq_group.seqs[0]) + assert len(computed_blocks) == 2 + + # Actual computed tokens. + seq_group.seqs[0].data.update_num_computed_tokens(token_chunk_size) + + # 2nd chunk: Complete 3rd block and additional 4 blocks. + token_chunk_size = int(block_size * 4.5) + block_manager.mark_blocks_as_computed(seq_group, token_chunk_size) + computed_blocks = block_manager.get_all_computed_blocks(seq_group.seqs[0]) + assert len(computed_blocks) == 7 diff --git a/tests/core/test_chunked_prefill_scheduler.py b/tests/core/test_chunked_prefill_scheduler.py index 6d9c2f3eb..2f6ea632a 100644 --- a/tests/core/test_chunked_prefill_scheduler.py +++ b/tests/core/test_chunked_prefill_scheduler.py @@ -562,3 +562,42 @@ def test_chunked_prefill_max_seqs(): assert len(get_sequence_groups(out)) == max_seqs assert not running[0].is_prefill() assert not running[1].is_prefill() + + +def test_perfix_caching(): + """Verify allocating full blocks when prefix caching is enabled.""" + block_size = 4 + max_seqs = 10 + max_model_len = 80 + max_num_batched_tokens = 64 + scheduler_config = SchedulerConfig(max_num_batched_tokens, + max_seqs, + max_model_len, + enable_chunked_prefill=True) + cache_config = CacheConfig(block_size, + 1.0, + 1, + "auto", + enable_prefix_caching=True) + cache_config.num_cpu_blocks = 0 + cache_config.num_gpu_blocks = 32 + scheduler = Scheduler(scheduler_config, cache_config, None) + running: List[SequenceGroup] = [] + + # Add seq groups to scheduler. + for i in range(2): + _, seq_group = create_dummy_prompt(str(i), + block_size=block_size, + prompt_length=50) + scheduler.add_seq_group(seq_group) + running.append(seq_group) + + seq_group_meta, out = schedule_and_update_computed_tokens(scheduler) + assert set(get_sequence_groups(out)) == set(running) + assert seq_group_meta[0].token_chunk_size == 50 + # Verify it is chunked. Note that although the budget is 64-50=14, + # we only allocate full blocks for prefix caching, so only 4*(14//4)=12 + # tokens are allocated. + assert seq_group_meta[1].token_chunk_size == 12 + assert out.num_prefill_groups == 2 + assert out.num_batched_tokens == 62 diff --git a/vllm/core/block_manager_v1.py b/vllm/core/block_manager_v1.py index 666723313..24ab9eb66 100644 --- a/vllm/core/block_manager_v1.py +++ b/vllm/core/block_manager_v1.py @@ -681,14 +681,20 @@ class BlockSpaceManagerV1(BlockSpaceManager): for block in block_table: block.last_accessed = access_time - def compute_full_blocks_in_seq(self, seq: Sequence): + def compute_full_blocks_in_seq(self, seq: Sequence, token_chunk_size: int): if seq.seq_id not in self.block_tables: return - max_full_block = seq.get_len() // self.block_size - 1 + + # When chunked prefill is enabled, the computed full blocks + # should be calculated based on the number of computed tokens. + max_computed_tokens = (seq.data.get_num_computed_tokens() + + token_chunk_size) + computed_full_blocks = max_computed_tokens // self.block_size + block_table = self.block_tables[seq.seq_id] - if max_full_block == -1: + if computed_full_blocks == 0: return - for i in reversed(range(max_full_block)): + for i in reversed(range(computed_full_blocks)): if block_table[i].computed: break block_table[i].computed = True @@ -718,10 +724,11 @@ class BlockSpaceManagerV1(BlockSpaceManager): ids_list = [self.get_all_computed_blocks(seq) for seq in seqs] return commonprefix([ids for ids in ids_list if ids != []]) - def mark_blocks_as_computed(self, seq_group: SequenceGroup): + def mark_blocks_as_computed(self, seq_group: SequenceGroup, + token_chunk_size: int): if self.enable_caching: for seq in seq_group.get_seqs(): - self.compute_full_blocks_in_seq(seq) + self.compute_full_blocks_in_seq(seq, token_chunk_size) def get_prefix_cache_hit_rate(self, device: Device) -> float: if device == Device.GPU: diff --git a/vllm/core/block_manager_v2.py b/vllm/core/block_manager_v2.py index 7d2db43cb..b06385b06 100644 --- a/vllm/core/block_manager_v2.py +++ b/vllm/core/block_manager_v2.py @@ -290,7 +290,8 @@ class BlockSpaceManagerV2(BlockSpaceManager): self._last_access_blocks_tracker.update_last_access( seq.seq_id, now) - def mark_blocks_as_computed(self, seq_group: SequenceGroup): + def mark_blocks_as_computed(self, seq_group: SequenceGroup, + token_chunk_size: int): # If prefix caching is enabled, mark immutable blocks as computed # right after they have been scheduled (for prefill). This assumes # the scheduler is synchronous so blocks are actually computed when diff --git a/vllm/core/embedding_model_block_manager.py b/vllm/core/embedding_model_block_manager.py index f16f66e99..c47d7d8df 100644 --- a/vllm/core/embedding_model_block_manager.py +++ b/vllm/core/embedding_model_block_manager.py @@ -80,7 +80,8 @@ class EmbeddingModelBlockSpaceManager(BlockSpaceManager): seq_group: List[Sequence]) -> List[int]: return [] - def mark_blocks_as_computed(self, seq_group: SequenceGroup): + def mark_blocks_as_computed(self, seq_group: SequenceGroup, + token_chunk_size: int): pass def get_prefix_cache_hit_rate(self, device: Device) -> float: diff --git a/vllm/core/interfaces.py b/vllm/core/interfaces.py index becd0d2e7..96f8dd851 100644 --- a/vllm/core/interfaces.py +++ b/vllm/core/interfaces.py @@ -115,7 +115,8 @@ class BlockSpaceManager(ABC): pass @abstractmethod - def mark_blocks_as_computed(self, seq_group: SequenceGroup): + def mark_blocks_as_computed(self, seq_group: SequenceGroup, + token_chunk_size: int): pass @abstractmethod diff --git a/vllm/core/scheduler.py b/vllm/core/scheduler.py index fbc53afa3..51fde6e4e 100644 --- a/vllm/core/scheduler.py +++ b/vllm/core/scheduler.py @@ -1226,7 +1226,8 @@ class Scheduler: # will crash the vLLM instance / will not retry. for scheduled_seq_group in scheduler_outputs.scheduled_seq_groups: self.block_manager.mark_blocks_as_computed( - scheduled_seq_group.seq_group) + scheduled_seq_group.seq_group, + scheduled_seq_group.token_chunk_size) self._seq_group_metadata_cache[self.next_cache_id].reset() @@ -1457,10 +1458,27 @@ class Scheduler: for seq in seqs: num_new_tokens += seq.get_num_new_tokens() assert num_new_tokens > 0 - # Chunk if a running request cannot fit in. - # If number of seq > 1, it means it is doing beam search in a - # decode phase. Do not chunk in that case. + # Chunk if a running request cannot fit in the given budget. + # If number of seq > 1, it means it is doing beam search + # in a decode phase. Do not chunk. if enable_chunking and len(seqs) == 1: - num_new_tokens = min(num_new_tokens, - budget.remaining_token_budget()) + remaining_token_budget = budget.remaining_token_budget() + if self.cache_config.enable_prefix_caching: + # When prefix caching is enabled, we always allocate + # the number of new tokens that is dividable by the block size + # to avoid partial block matching. + block_size = self.cache_config.block_size + reminder = budget.token_budget % block_size + if reminder != 0: + raise ValueError("When enabling chunked prefill and " + "prefix caching, max_num_batched_tokens " + "(chunk size) must be dividable by " + "block size, but got chunk_size " + f"({budget.token_budget}) % block_size " + f"({block_size}) = {reminder}") + if remaining_token_budget < num_new_tokens: + num_new_tokens = (remaining_token_budget // + block_size) * block_size + else: + num_new_tokens = min(num_new_tokens, remaining_token_budget) return num_new_tokens diff --git a/vllm/worker/model_runner.py b/vllm/worker/model_runner.py index f556e4ea1..2b287a5d2 100644 --- a/vllm/worker/model_runner.py +++ b/vllm/worker/model_runner.py @@ -501,23 +501,48 @@ class ModelInputForGPUBuilder(ModelRunnerInputBuilderBase[ModelInputForGPU]): and self.sliding_window is None and inter_data.is_prompt) inter_data.prefix_cache_hit = prefix_cache_hit - if self.chunked_prefill_enabled and prefix_cache_hit: - raise RuntimeError( - "chunked prefill cannot be used with prefix caching now.") - - # If prefix cache is hit, advance context length to bypass - # hit blocks. Accordingly, input tokens, position and query length - # have to be updated. - if prefix_cache_hit: - assert computed_block_nums is not None - context_len = len(computed_block_nums) * self.block_size + + if not prefix_cache_hit: + return + + assert computed_block_nums is not None + # The cache hit prompt tokens in this sequence. Note that + # this may be larger than the sequence length if chunked + # prefill is enabled. + prefix_cache_len = len(computed_block_nums) * self.block_size + # The number of so far computed prompt tokens in this sequence. + context_len = inter_data.context_lens[seq_idx] + # The total number of prompt tokens in this sequence. + # When chunked prefill is enabled, this is the token number of + # computed chunks + current chunk. + seq_len = inter_data.seq_lens[seq_idx] + if prefix_cache_len <= context_len: + # We already passed the cache hit region, + # so do normal computation. + pass + elif context_len < prefix_cache_len < seq_len: + # Partial hit. Compute the missing part. + uncomputed_start = prefix_cache_len - context_len inter_data.input_tokens[seq_idx] = inter_data.input_tokens[ - seq_idx][context_len:] + seq_idx][uncomputed_start:] inter_data.input_positions[seq_idx] = inter_data.input_positions[ - seq_idx][context_len:] + seq_idx][uncomputed_start:] + context_len = prefix_cache_len + inter_data.context_lens[seq_idx] = context_len inter_data.query_lens[ seq_idx] = inter_data.seq_lens[seq_idx] - context_len + elif seq_len <= prefix_cache_len: + # Full hit. Only compute the last token to avoid + # erroneous behavior. FIXME: Ideally we should directly + # mark all tokens as computed in the scheduler and do not + # schedule this sequence, so this case should not happen. + inter_data.input_tokens[seq_idx] = inter_data.input_tokens[ + seq_idx][-1:] + inter_data.input_positions[seq_idx] = inter_data.input_positions[ + seq_idx][-1:] + inter_data.query_lens[seq_idx] = 1 + inter_data.context_lens[seq_idx] = inter_data.seq_lens[seq_idx] - 1 def _compute_for_sliding_window(self, inter_data: InterDataForSeqGroup, seq_idx: int,
[ "ModelRunner.generate_greedy", "Scheduler.schedule", "BlockSpaceManager.mark_blocks_as_computed" ]
[ "vllm/worker/model_runner.py", "vllm/core/scheduler.py", "vllm/v1/core/sched/scheduler.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=RedHatAI/Meta-Llama-3-8B-Instruct-FP8,dtype=auto --tasks gsm8k --batch_size auto --limit 100
2deb029d115dadd012ce5ea70487a207cb025493
https://github.com/vllm-project/vllm/pull/7822
2024-08-26
2025-09-07 17:48:12
2025-09-07 17:48:12
[ "RedHatAI/Meta-Llama-3-8B-Instruct-FP8" ]
python benchmarks/benchmark_prefix_caching.py --model RedHatAI/Meta-Llama-3-8B-Instruct-FP8 --output-len 200 --enable-prefix-caching
true
false
false
true
[Performance][BlockManagerV2] Mark prefix cache block as computed after schedule (#7822)
[Performance][BlockManagerV2] Mark prefix cache block as computed after schedule (#7822)
2024-08-26T11:24:53-07:00
[ "tests/core/block/test_prefix_caching_block.py", "vllm/core/block/prefix_caching_block.py", "vllm/core/block_manager_v2.py" ]
{ "commit_year": 2024, "num_edited_lines": 63, "num_files": 3, "num_hunks": 6, "num_non_test_edited_lines": 32, "num_non_test_files": 2, "num_test_files": 1, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/core/block/test_prefix_caching_block.py b/tests/core/block/test_prefix_caching_block.py index c2226870c..25be2dd13 100644 --- a/tests/core/block/test_prefix_caching_block.py +++ b/tests/core/block/test_prefix_caching_block.py @@ -708,6 +708,37 @@ class TestPrefixCachingBlockAllocator: token_ids=token_ids) assert allocator.get_prefix_cache_hit_rate() > 0.99 + # Test case for marking cache hit blocks as computed right after + # a batch of prefill sequences are scheduled. + @staticmethod + def test_touch_block(): + block_size = 16 + common_blocks = 4 + allocator = PrefixCachingBlockAllocator(num_blocks=8, + block_size=block_size) + + common_token_ids = list(range(block_size * common_blocks)) + + # Mimic the behavior of allocating the same block chain + # (i.e., common prefix) for a batch of 3 different prefill sequences. + for _ in range(3): + blocks = TestPrefixCachingBlockAllocator.create_immutable_chain( + block_size=block_size, + token_ids=common_token_ids, + allocator=allocator, + ) + block_ids = [block.block_id for block in blocks] + # The allocated blocks should be marked as touched + # but not computed. + computed_block_ids = allocator.get_computed_block_ids( + [], block_ids, skip_last_block_id=False) + assert len(computed_block_ids) == 0 + + allocator.mark_blocks_as_computed([]) + computed_block_ids = allocator.get_computed_block_ids( + [], block_ids, skip_last_block_id=False) + assert len(computed_block_ids) == common_blocks + @staticmethod def create_immutable_chain( block_size: int, diff --git a/vllm/core/block/prefix_caching_block.py b/vllm/core/block/prefix_caching_block.py index 432a6651a..a87e814cf 100644 --- a/vllm/core/block/prefix_caching_block.py +++ b/vllm/core/block/prefix_caching_block.py @@ -1,6 +1,6 @@ """Token blocks.""" from os.path import commonprefix -from typing import Dict, FrozenSet, Iterable, List, Optional, Tuple +from typing import Dict, FrozenSet, Iterable, List, Optional, Set, Tuple from vllm.core.block.common import (CacheMetricData, CopyOnWriteTracker, get_all_blocks_recursively) @@ -73,6 +73,11 @@ class PrefixCachingBlockAllocator(BlockAllocator): # prefix hash will be in this dict, even if they have refcount 0. self._cached_blocks: Dict[PrefixHash, BlockId] = {} + # A list of immutable block IDs that have been touched by scheduler + # and should be marked as computed after an entire batch of sequences + # are scheduled. + self._touched_blocks: Set[BlockId] = set() + # Used to track status of each physical block id self._block_tracker: Dict[BlockId, BlockTracker] = {} for block_id in block_ids: @@ -438,10 +443,14 @@ class PrefixCachingBlockAllocator(BlockAllocator): assert self._refcounter.get(block.block_id) > 0 if block.content_hash not in self._cached_blocks: - # No cached content hash => Set this block as cached - # (Note that this block is not computed yet => - # Will be computed after free()) + # No cached content hash => Set this block as cached. + # Note that this block cannot be marked as computed yet + # because other sequences in the same batch cannot reuse + # this block. self._cached_blocks[block.content_hash] = block.block_id + # Mark this block as touched so that it can be marked as + # computed after the entire batch of sequences are scheduled. + self._touched_blocks.add(block.block_id) return block.block_id # Reuse the cached content hash @@ -507,7 +516,10 @@ class PrefixCachingBlockAllocator(BlockAllocator): "Mark block as accessed which is not belonged to GPU") def mark_blocks_as_computed(self, block_ids: List[int]) -> None: - raise NotImplementedError("Marking as computed is incremental") + # Mark all touched blocks as computed. + for block_id in self._touched_blocks: + self._block_tracker[block_id].computed = True + self._touched_blocks.clear() def _track_block_id(self, block_id: Optional[BlockId], computed: bool) -> None: diff --git a/vllm/core/block_manager_v2.py b/vllm/core/block_manager_v2.py index b7d9451f1..7d4919a0d 100644 --- a/vllm/core/block_manager_v2.py +++ b/vllm/core/block_manager_v2.py @@ -287,11 +287,11 @@ class BlockSpaceManagerV2(BlockSpaceManager): seq.seq_id, now) def mark_blocks_as_computed(self, seq_group: SequenceGroup): - # The only need for mark block as computed is for prefix caching, - # while currently we could determine whether one block is computed - # or not by check whether it has content hash. - # So this function is useless for block_v2. - pass + # If prefix caching is enabled, mark immutable blocks as computed + # right after they have been scheduled (for prefill). This assumes + # the scheduler is synchronous so blocks are actually computed when + # scheduling the next batch. + self.block_allocator.mark_blocks_as_computed([]) def get_common_computed_block_ids( self, seqs: List[Sequence]) -> GenericSequence[int]:
[ "PrefixCachingBlockAllocator.mark_blocks_as_computed", "BlockSpaceManagerV2.mark_blocks_as_computed" ]
[ "vllm/core/block/prefix_caching_block.py", "vllm/core/block_manager.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=RedHatAI/Meta-Llama-3-8B-Instruct-FP8,dtype=auto --tasks gsm8k --batch_size auto --limit 100
fc7b8d1eefcbe837a56b7c080509417fe5167e6c
https://github.com/vllm-project/vllm/pull/7364
2024-08-09
2025-09-07 17:48:14
2025-09-07 17:48:14
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100
true
false
false
true
[Performance] e2e overheads reduction: Small followup diff (#7364)
[Performance] e2e overheads reduction: Small followup diff (#7364)
2024-08-09T15:49:36Z
[ "vllm/core/block_manager_v1.py", "vllm/sequence.py" ]
{ "commit_year": 2024, "num_edited_lines": 7, "num_files": 2, "num_hunks": 2, "num_non_test_edited_lines": 7, "num_non_test_files": 2, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/core/block_manager_v1.py b/vllm/core/block_manager_v1.py index 622aca66a..ad26d3c51 100644 --- a/vllm/core/block_manager_v1.py +++ b/vllm/core/block_manager_v1.py @@ -336,9 +336,9 @@ class BlockSpaceManagerV1(BlockSpaceManager): # Assign the self-attention block tables for each sequence. if len(wait_seqs) == 1: - self.block_tables[wait_seqs[0].seq_id] = block_table + self.block_tables[seq.seq_id] = block_table else: - for seq in seq_group.get_seqs(status=SequenceStatus.WAITING): + for seq in wait_seqs: self.block_tables[seq.seq_id] = block_table.copy() # Allocate encoder sequence diff --git a/vllm/sequence.py b/vllm/sequence.py index ba477efc5..fd2dc9656 100644 --- a/vllm/sequence.py +++ b/vllm/sequence.py @@ -655,6 +655,9 @@ class SequenceGroup: return [seq for seq in self.seqs if not seq.is_finished()] def get_finished_seqs(self) -> List[Sequence]: + if self.is_single_seq: + return self.seqs if self.seqs[0].is_finished() else [] + return [seq for seq in self.seqs if seq.is_finished()] def update_num_computed_tokens(self, num_new_computed_tokens: int):
[ "BlockSpaceManagerV1", "SequenceGroup.get_finished_seqs" ]
[ "vllm/sequence.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
660470e5a36b8e52083615ad7c85e9b4fd4c72ce
https://github.com/vllm-project/vllm/pull/7193
2024-08-06
2025-09-07 17:48:19
2025-09-07 17:48:19
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --tensor-parallel-size 1 --enable-prefix-caching --use-v2-block-manager
true
false
false
true
[Core] Optimize evictor-v2 performance (#7193)
[Core] Optimize evictor-v2 performance (#7193)
2024-08-06T12:34:25-07:00
[ "vllm/core/evictor_v2.py" ]
{ "commit_year": 2024, "num_edited_lines": 6, "num_files": 1, "num_hunks": 2, "num_non_test_edited_lines": 6, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/core/evictor_v2.py b/vllm/core/evictor_v2.py index 3dd12e2e2..5b1a208b7 100644 --- a/vllm/core/evictor_v2.py +++ b/vllm/core/evictor_v2.py @@ -91,8 +91,9 @@ class LRUEvictor(Evictor): # at the start of OrderedDict. Loop through all these blocks to # find the one with maximum number of hashed tokens. for _id, block in self.free_table.items(): - if evicted_block.last_accessed > block.last_accessed or ( - evicted_block.last_accessed == block.last_accessed and + if evicted_block.last_accessed < block.last_accessed: + break + if (evicted_block.last_accessed == block.last_accessed and evicted_block.num_hashed_tokens < block.num_hashed_tokens): evicted_block = block evicted_block_id = _id @@ -109,6 +110,7 @@ class LRUEvictor(Evictor): def update(self, block_id: int, last_accessed: float): self.free_table[block_id].last_accessed = last_accessed + self.free_table.move_to_end(block_id) def remove(self, block_id: int): if block_id not in self.free_table:
[ "None" ]
[ "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/api_server.py", "vllm/entrypoints/openai/api_server.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
89a84b0bb7b30706a02836234a94493ea8f780bf
https://github.com/vllm-project/vllm/pull/6779
2024-07-26
2025-09-07 17:48:26
2025-09-07 17:48:26
[ "Qwen/Qwen1.5-0.5B" ]
python benchmarks/benchmark_serving.py --model Qwen/Qwen1.5-0.5B --backend vllm --num-prompts 2048 --input-len 1024
true
false
false
true
[Core] Use array to speedup padding (#6779)
[Core] Use array to speedup padding (#6779)
2024-07-25T21:31:31-07:00
[ "vllm/model_executor/layers/sampler.py", "vllm/model_executor/sampling_metadata.py", "vllm/sequence.py" ]
{ "commit_year": 2024, "num_edited_lines": 46, "num_files": 3, "num_hunks": 9, "num_non_test_edited_lines": 46, "num_non_test_files": 3, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/layers/sampler.py b/vllm/model_executor/layers/sampler.py index 5c376797a..121458f81 100644 --- a/vllm/model_executor/layers/sampler.py +++ b/vllm/model_executor/layers/sampler.py @@ -220,7 +220,7 @@ def _apply_min_tokens_penalty( seqs_to_penalize: List[int] = [] for j, seq_id in enumerate(seq_ids): seq_data = seq_group.seq_data[seq_id] - if len(seq_data.output_token_ids) < min_tokens: + if len(seq_data.output_token_ids_array) < min_tokens: seqs_to_penalize.append(j) if seqs_to_penalize: diff --git a/vllm/model_executor/sampling_metadata.py b/vllm/model_executor/sampling_metadata.py index 390b5d173..27b37a9d5 100644 --- a/vllm/model_executor/sampling_metadata.py +++ b/vllm/model_executor/sampling_metadata.py @@ -1,4 +1,5 @@ import random +from array import array from dataclasses import dataclass from typing import Dict, List, Optional, Tuple @@ -329,8 +330,8 @@ class SamplingTensors: user-defined seed for each sequence. extra_entropy: extra entropy to use when generating seeds. """ - prompt_tokens: List[List[int]] = [] - output_tokens: List[List[int]] = [] + prompt_tokens: List[array] = [] + output_tokens: List[array] = [] top_ks: List[int] = [] temperatures: List[float] = [] top_ps: List[float] = [] @@ -432,13 +433,15 @@ class SamplingTensors: if (seq_group.is_prompt and sampling_params.prompt_logprobs is not None): prefill_len = len(seq_group.prompt_logprob_indices) - prompt_tokens.extend([] for _ in range(prefill_len)) - output_tokens.extend([] for _ in range(prefill_len)) + prompt_tokens.extend( + array('l') for _ in range(prefill_len)) + output_tokens.extend( + array('l') for _ in range(prefill_len)) if seq_group.do_sample: for seq_id in seq_ids: seq_data = seq_group.seq_data[seq_id] - prompt_tokens.append(list(seq_data.prompt_token_ids)) - output_tokens.append(list(seq_data.output_token_ids)) + prompt_tokens.append(seq_data.prompt_token_ids_array) + output_tokens.append(seq_data.output_token_ids_array) sampling_tensors = SamplingTensors.from_lists( temperatures, top_ps, top_ks, min_ps, presence_penalties, @@ -454,9 +457,9 @@ class SamplingTensors: frequency_penalties: List[float], repetition_penalties: List[float], sampling_seeds: List[int], sample_indices: List[int], - prompt_tokens: List[List[int]], - output_tokens: List[List[int]], vocab_size: int, - extra_seeds_to_generate: int, device: torch.device, + prompt_tokens: List[array], output_tokens: List[array], + vocab_size: int, extra_seeds_to_generate: int, + device: torch.device, dtype: torch.dtype) -> "SamplingTensors": # Note that the performance will be very bad without # pinned memory. diff --git a/vllm/sequence.py b/vllm/sequence.py index 0cd4c7e71..72821ecea 100644 --- a/vllm/sequence.py +++ b/vllm/sequence.py @@ -3,6 +3,7 @@ import copy import enum import math from abc import ABC, abstractmethod +from array import array from collections import defaultdict from dataclasses import dataclass, field from typing import (TYPE_CHECKING, Dict, List, Mapping, Optional, Set, Tuple, @@ -119,10 +120,10 @@ class SequenceData: prompt_token_ids: List[int], output_token_ids: Optional[List[int]] = None, ) -> None: - self._prompt_token_ids: List[int] = list(prompt_token_ids) + self._prompt_token_ids = array('l', prompt_token_ids) self._prompt_token_ids_tuple: Tuple[int, ...] = tuple(prompt_token_ids) - self._output_token_ids: List[int] = ( - list(output_token_ids) if output_token_ids is not None else []) + self._output_token_ids = array( + 'l', output_token_ids if output_token_ids is not None else []) self.cumulative_logprob = 0.0 # The number of tokens that are computed (that run against the model). @@ -132,8 +133,8 @@ class SequenceData: self._update_cached_all_tokens() def _update_cached_all_tokens(self): - self._cached_all_token_ids: List[int] = (self._prompt_token_ids + - self._output_token_ids) + self._cached_all_token_ids: List[int] = list(self._prompt_token_ids + + self._output_token_ids) @property def prompt_token_ids(self) -> Tuple[int, ...]: @@ -141,19 +142,27 @@ class SequenceData: @prompt_token_ids.setter def prompt_token_ids(self, new_prompt_token_ids) -> None: - self._prompt_token_ids = list(new_prompt_token_ids) + self._prompt_token_ids = array('l', new_prompt_token_ids) self._prompt_token_ids_tuple = tuple(new_prompt_token_ids) self._update_cached_all_tokens() + @property + def prompt_token_ids_array(self) -> array: + return self._prompt_token_ids + @property def output_token_ids(self) -> Tuple[int, ...]: return tuple(self._output_token_ids) @output_token_ids.setter def output_token_ids(self, new_output_token_ids) -> None: - self._output_token_ids = list(new_output_token_ids) + self._output_token_ids = array('l', new_output_token_ids) self._update_cached_all_tokens() + @property + def output_token_ids_array(self) -> array: + return self._output_token_ids + def append_token_id(self, token_id: int, logprob: float) -> None: self._output_token_ids.append(token_id) self._cached_all_token_ids.append(token_id)
[ "vllm.sequence.SequenceData.prompt_token_ids_array", "vllm.sequence.SequenceData.output_token_ids_array", "vllm.model_executor.sampling_metadata.SamplingTensors.from_lists" ]
[ "vllm/sequence.py", "vllm/model_executor/sampling_metadata.py", "vllm/v1/sample/sampler.py", "vllm/model_executor/layers/sampler.py", "vllm/v1/sample/tpu/sampler.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=Qwen/Qwen1.5-0.5B,dtype=auto --trust_remote_code --tasks gsm8k --batch_size auto --limit 100
9ed82e7074a18e25680ab106fc846364ad97bc00
https://github.com/vllm-project/vllm/pull/6520
2024-07-19
2025-09-07 17:48:29
2025-09-07 17:48:29
[ "meta-llama/Llama-2-7b-hf" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-2-7b-hf --backend vllm --num-prompts 100
true
false
false
true
[Misc] Small perf improvements (#6520)
[Misc] Small perf improvements (#6520)
2024-07-19T12:10:56-07:00
[ "tests/core/block/test_block_manager_v2.py", "tests/core/block/test_cpu_gpu_block_allocator.py", "vllm/core/block/block_table.py", "vllm/core/block/prefix_caching_block.py", "vllm/model_executor/models/__init__.py", "vllm/sequence.py", "vllm/utils.py" ]
{ "commit_year": 2024, "num_edited_lines": 69, "num_files": 7, "num_hunks": 11, "num_non_test_edited_lines": 50, "num_non_test_files": 5, "num_test_files": 2, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/core/block/test_block_manager_v2.py b/tests/core/block/test_block_manager_v2.py index d0ca09c4b..d7863a9ae 100644 --- a/tests/core/block/test_block_manager_v2.py +++ b/tests/core/block/test_block_manager_v2.py @@ -249,10 +249,13 @@ def test_append_slots(block_size, prompt_len, num_slots_to_append, # Expect consumed blocks to be new blocks required to support the new slots. expected_consumed_blocks = len( - chunk_list( - list( - range(prompt_len + num_slots_to_append + num_lookahead_slots)), - block_size)) - len(chunk_list(list(range(prompt_len)), block_size)) + list( + chunk_list( + list( + range(prompt_len + num_slots_to_append + + num_lookahead_slots)), + block_size))) - len( + list(chunk_list(list(range(prompt_len)), block_size))) assert num_consumed_blocks == expected_consumed_blocks diff --git a/tests/core/block/test_cpu_gpu_block_allocator.py b/tests/core/block/test_cpu_gpu_block_allocator.py index 15b76d909..a9e38d404 100644 --- a/tests/core/block/test_cpu_gpu_block_allocator.py +++ b/tests/core/block/test_cpu_gpu_block_allocator.py @@ -58,10 +58,10 @@ def test_allocate_immutable_block(num_cpu_blocks: int, num_gpu_blocks: int, unique_token_ids = list( range((num_cpu_blocks + num_gpu_blocks) * block_size)) - gpu_token_ids = chunk_list(unique_token_ids[:num_gpu_blocks * block_size], - block_size) - cpu_token_ids = chunk_list(unique_token_ids[num_gpu_blocks * block_size:], - block_size) + gpu_token_ids = list( + chunk_list(unique_token_ids[:num_gpu_blocks * block_size], block_size)) + cpu_token_ids = list( + chunk_list(unique_token_ids[num_gpu_blocks * block_size:], block_size)) assert allocator.get_num_free_blocks(Device.CPU) == num_cpu_blocks assert allocator.get_num_free_blocks(Device.GPU) == num_gpu_blocks diff --git a/vllm/core/block/block_table.py b/vllm/core/block/block_table.py index 49e63c231..06b816eb3 100644 --- a/vllm/core/block/block_table.py +++ b/vllm/core/block/block_table.py @@ -1,3 +1,4 @@ +import math from typing import List, Optional from vllm.core.block.common import BlockList @@ -337,10 +338,17 @@ class BlockTable: This is required for the scheduler to determine whether a sequence can continue generation, or if it must be preempted. """ + # Math below is equivalent to: + # all_token_ids = token_ids + [-1] * num_lookahead_slots + # token_blocks = self._chunk_token_blocks_for_append(all_token_ids) + # return len(token_blocks) - all_token_ids = token_ids + [-1] * num_lookahead_slots - token_blocks = self._chunk_token_blocks_for_append(all_token_ids) - return len(token_blocks) + num_token_ids = len(token_ids) + num_lookahead_slots + first_chunk_size = self._block_size - (self._num_full_slots % + self._block_size) + num_token_blocks = (1 + math.ceil( + (num_token_ids - first_chunk_size) / self._block_size)) + return num_token_blocks def _chunk_token_blocks_for_append( self, token_ids: List[int]) -> List[List[int]]: @@ -351,6 +359,7 @@ class BlockTable: """ first_chunk_size = self._block_size - (self._num_full_slots % self._block_size) - token_blocks = [token_ids[:first_chunk_size]] + chunk_list( - token_ids[first_chunk_size:], self._block_size) + token_blocks = [token_ids[:first_chunk_size]] + token_blocks.extend( + chunk_list(token_ids[first_chunk_size:], self._block_size)) return token_blocks diff --git a/vllm/core/block/prefix_caching_block.py b/vllm/core/block/prefix_caching_block.py index f272e23ee..d102ad404 100644 --- a/vllm/core/block/prefix_caching_block.py +++ b/vllm/core/block/prefix_caching_block.py @@ -552,9 +552,12 @@ class PrefixCachingBlockAllocator(BlockAllocator): # runner. # It returns a list of int although type annotation says list of string. + if len(computed_seq_block_ids) == 1: + return computed_seq_block_ids[0] + return commonprefix([ ids for ids in computed_seq_block_ids # type: ignore - if ids != [] + if ids ]) def get_num_blocks_touched(self, diff --git a/vllm/model_executor/models/__init__.py b/vllm/model_executor/models/__init__.py index 87508a116..aa5a70757 100644 --- a/vllm/model_executor/models/__init__.py +++ b/vllm/model_executor/models/__init__.py @@ -1,3 +1,4 @@ +import functools import importlib from typing import Dict, List, Optional, Type @@ -98,6 +99,14 @@ _ROCM_PARTIALLY_SUPPORTED_MODELS: Dict[str, str] = { class ModelRegistry: + @staticmethod + @functools.lru_cache(maxsize=128) + def _get_model(model_arch: str): + module_name, model_cls_name = _MODELS[model_arch] + module = importlib.import_module( + f"vllm.model_executor.models.{module_name}") + return getattr(module, model_cls_name, None) + @staticmethod def load_model_cls(model_arch: str) -> Optional[Type[nn.Module]]: if model_arch in _OOT_MODELS: @@ -114,10 +123,7 @@ class ModelRegistry: "Model architecture %s is partially supported by ROCm: %s", model_arch, _ROCM_PARTIALLY_SUPPORTED_MODELS[model_arch]) - module_name, model_cls_name = _MODELS[model_arch] - module = importlib.import_module( - f"vllm.model_executor.models.{module_name}") - return getattr(module, model_cls_name, None) + return ModelRegistry._get_model(model_arch) @staticmethod def get_supported_archs() -> List[str]: diff --git a/vllm/sequence.py b/vllm/sequence.py index 1cebf68d4..6c12a01bd 100644 --- a/vllm/sequence.py +++ b/vllm/sequence.py @@ -457,24 +457,25 @@ class SequenceGroup: self.prompt_adapter_request = prompt_adapter_request self.encoder_seq = encoder_seq self.trace_headers = trace_headers + self._first_seq = next(iter(self.seqs_dict.values())) @property def prompt(self) -> Optional[str]: # All sequences in the group should have the same prompt. # We use the prompt of an arbitrary sequence. - return next(iter(self.seqs_dict.values())).prompt + return self._first_seq.prompt @property def prompt_token_ids(self) -> List[int]: # All sequences in the group should have the same prompt. # We use the prompt of an arbitrary sequence. - return next(iter(self.seqs_dict.values())).prompt_token_ids + return self._first_seq.prompt_token_ids @property def multi_modal_data(self) -> "MultiModalDataDict": # All sequences in the group should have the same multi-modal data. # We use the multi-modal data of an arbitrary sequence. - return next(iter(self.seqs_dict.values())).multi_modal_data + return self._first_seq.multi_modal_data @property def lora_int_id(self) -> int: diff --git a/vllm/utils.py b/vllm/utils.py index f3025a68d..f906d8258 100644 --- a/vllm/utils.py +++ b/vllm/utils.py @@ -415,9 +415,10 @@ def init_kmp_env(): os.environ['KMP_REDUCTION_BARRIER_PATTERN'] = "dist,dist" -def chunk_list(lst: List[T], chunk_size: int) -> List[List[T]]: +def chunk_list(lst: List[T], chunk_size: int): """Yield successive chunk_size chunks from lst.""" - return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)] + for i in range(0, len(lst), chunk_size): + yield lst[i:i + chunk_size] def cdiv(a: int, b: int) -> int:
[ "BlockTable.get_num_token_blocks", "ModelRegistry.load_model_cls", "SequenceGroup.prompt", "SequenceGroup.prompt_token_ids", "SequenceGroup.multi_modal_data" ]
[]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-2-7b-hf,dtype=auto --tasks gsm8k --batch_size auto --limit 100
3476ed0809ec91a3457da0cb90543133a4f4b519
https://github.com/vllm-project/vllm/pull/5602
2024-07-02
2025-09-07 17:48:40
2025-09-07 17:48:40
[ "meta-llama/Llama-2-7b-hf" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-2-7b-hf --dtype float16 --num-prompts 300 --seed 0
true
false
false
true
[Core] Optimize block_manager_v2 vs block_manager_v1 (to make V2 default) (#5602)
[Core] Optimize block_manager_v2 vs block_manager_v1 (to make V2 default) (#5602)
2024-07-01T20:10:37-07:00
[ "benchmarks/benchmark_latency.py", "tests/conftest.py", "tests/core/block/test_block_table.py", "tests/core/block/test_cpu_gpu_block_allocator.py", "tests/core/block/test_naive_block.py", "tests/core/block/test_prefix_caching_block.py", "tests/spec_decode/test_batch_expansion.py", "vllm/core/block/block_table.py", "vllm/core/block/common.py", "vllm/core/block/cpu_gpu_block_allocator.py", "vllm/core/block/interfaces.py", "vllm/core/block/naive_block.py", "vllm/core/block/prefix_caching_block.py", "vllm/core/block_manager_v2.py", "vllm/engine/llm_engine.py", "vllm/entrypoints/openai/serving_completion.py", "vllm/model_executor/sampling_metadata.py", "vllm/outputs.py", "vllm/sequence.py" ]
{ "commit_year": 2024, "num_edited_lines": 1721, "num_files": 19, "num_hunks": 107, "num_non_test_edited_lines": 1570, "num_non_test_files": 13, "num_test_files": 6, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/benchmarks/benchmark_latency.py b/benchmarks/benchmark_latency.py index a46ee1581..8d0554b0f 100644 --- a/benchmarks/benchmark_latency.py +++ b/benchmarks/benchmark_latency.py @@ -46,6 +46,7 @@ def main(args: argparse.Namespace): load_format=args.load_format, distributed_executor_backend=args.distributed_executor_backend, otlp_traces_endpoint=args.otlp_traces_endpoint, + enable_prefix_caching=args.enable_prefix_caching, ) sampling_params = SamplingParams( @@ -220,6 +221,9 @@ if __name__ == '__main__': action='store_true', help='If True, the prefill requests can be chunked based on the ' 'max_num_batched_tokens') + parser.add_argument("--enable-prefix-caching", + action='store_true', + help="Enable automatic prefix caching") parser.add_argument('--use-v2-block-manager', action='store_true') parser.add_argument( "--ray-workers-use-nsight", diff --git a/tests/conftest.py b/tests/conftest.py index 0bd24905e..ac802d03b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -474,7 +474,7 @@ class VllmRunner: req_sample_output_strs: List[str] = [] for sample in req_output.outputs: output_str = sample.text - output_ids = sample.token_ids + output_ids = list(sample.token_ids) req_sample_output_ids.append(prompt_ids + output_ids) req_sample_output_strs.append(prompt_str + output_str) outputs.append((req_sample_output_ids, req_sample_output_strs)) diff --git a/tests/core/block/test_block_table.py b/tests/core/block/test_block_table.py index 496774c8d..e2391a568 100644 --- a/tests/core/block/test_block_table.py +++ b/tests/core/block/test_block_table.py @@ -373,8 +373,9 @@ def test_cow(block_size: int, sequence_len: int, append_len: int, block_size) - (sequence_len // block_size) original_block_table.allocate(token_ids=token_ids, device=Device.GPU) - original_block_ids = original_block_table.physical_block_ids + original_block_ids = original_block_table.physical_block_ids[:] + print("original_block_ids = {}".format(original_block_ids)) forked_block_table = original_block_table.fork() # Expect no additional allocation (copy on _write_). @@ -457,7 +458,7 @@ def test_cow_lookahead_simple(block_size: int, sequence_len: int, # Allocate lookahead slots. original_block_table.ensure_num_empty_slots(lookahead_slots) - original_block_ids = original_block_table.physical_block_ids + original_block_ids = original_block_table.physical_block_ids[:] forked_block_table = original_block_table.fork() diff --git a/tests/core/block/test_cpu_gpu_block_allocator.py b/tests/core/block/test_cpu_gpu_block_allocator.py index 44a5be6c1..15b76d909 100644 --- a/tests/core/block/test_cpu_gpu_block_allocator.py +++ b/tests/core/block/test_cpu_gpu_block_allocator.py @@ -8,8 +8,8 @@ from vllm.utils import Device, chunk_list @pytest.mark.parametrize("num_gpu_blocks", [1024]) @pytest.mark.parametrize("block_size", [16]) @pytest.mark.parametrize("allocator_type", ["naive", "prefix_caching"]) -def test_allocate_mutable(num_cpu_blocks: int, num_gpu_blocks: int, - block_size: int, allocator_type: str): +def test_allocate_mutable_block(num_cpu_blocks: int, num_gpu_blocks: int, + block_size: int, allocator_type: str): allocator = CpuGpuBlockAllocator.create( allocator_type=allocator_type, num_gpu_blocks=num_gpu_blocks, @@ -21,14 +21,14 @@ def test_allocate_mutable(num_cpu_blocks: int, num_gpu_blocks: int, assert allocator.get_num_free_blocks(Device.GPU) == num_gpu_blocks cpu_blocks = [ - allocator.allocate_mutable(prev_block=None, device=Device.CPU) + allocator.allocate_mutable_block(prev_block=None, device=Device.CPU) for _ in range(num_cpu_blocks) ] assert allocator.get_num_free_blocks(Device.CPU) == 0 assert allocator.get_num_free_blocks(Device.GPU) == num_gpu_blocks gpu_blocks = [ - allocator.allocate_mutable(prev_block=None, device=Device.GPU) + allocator.allocate_mutable_block(prev_block=None, device=Device.GPU) for _ in range(num_gpu_blocks) ] assert allocator.get_num_free_blocks(Device.CPU) == 0 @@ -47,8 +47,8 @@ def test_allocate_mutable(num_cpu_blocks: int, num_gpu_blocks: int, @pytest.mark.parametrize("num_gpu_blocks", [1024]) @pytest.mark.parametrize("block_size", [2]) @pytest.mark.parametrize("allocator_type", ["naive", "prefix_caching"]) -def test_allocate_immutable(num_cpu_blocks: int, num_gpu_blocks: int, - block_size: int, allocator_type: str): +def test_allocate_immutable_block(num_cpu_blocks: int, num_gpu_blocks: int, + block_size: int, allocator_type: str): allocator = CpuGpuBlockAllocator.create( allocator_type=allocator_type, num_gpu_blocks=num_gpu_blocks, @@ -67,18 +67,18 @@ def test_allocate_immutable(num_cpu_blocks: int, num_gpu_blocks: int, assert allocator.get_num_free_blocks(Device.GPU) == num_gpu_blocks cpu_blocks = [ - allocator.allocate_immutable(prev_block=None, - token_ids=token_ids, - device=Device.CPU) + allocator.allocate_immutable_block(prev_block=None, + token_ids=token_ids, + device=Device.CPU) for token_ids in cpu_token_ids ] assert allocator.get_num_free_blocks(Device.CPU) == 0 assert allocator.get_num_free_blocks(Device.GPU) == num_gpu_blocks gpu_blocks = [ - allocator.allocate_immutable(prev_block=None, - token_ids=token_ids, - device=Device.GPU) + allocator.allocate_immutable_block(prev_block=None, + token_ids=token_ids, + device=Device.GPU) for token_ids in gpu_token_ids ] assert allocator.get_num_free_blocks(Device.CPU) == 0 diff --git a/tests/core/block/test_naive_block.py b/tests/core/block/test_naive_block.py index edcdc0c7d..9821ac41b 100644 --- a/tests/core/block/test_naive_block.py +++ b/tests/core/block/test_naive_block.py @@ -14,11 +14,11 @@ class TestNaiveBlockAllocator: prev_block: Optional[Block], token_ids: List[int]): if allocate_type == "immutable": - allocate_block = lambda: allocator.allocate_immutable( + allocate_block = lambda: allocator.allocate_immutable_block( prev_block=prev_block, token_ids=token_ids) elif allocate_type == "mutable": - allocate_block = lambda: allocator.allocate_mutable(prev_block= - prev_block) + allocate_block = lambda: allocator.allocate_mutable_block( + prev_block=prev_block) else: raise ValueError() diff --git a/tests/core/block/test_prefix_caching_block.py b/tests/core/block/test_prefix_caching_block.py index fcf32cbe9..95858268a 100644 --- a/tests/core/block/test_prefix_caching_block.py +++ b/tests/core/block/test_prefix_caching_block.py @@ -26,11 +26,10 @@ class TestPrefixCachingBlock: token_ids = list(range(num_to_fill)) mock_allocator = MagicMock(spec=PrefixCachingBlockAllocator) - block_with_prev = PrefixCachingBlock( - prev_block=None, - token_ids=token_ids, - block_size=block_size, - prefix_caching_allocator=mock_allocator) + block_with_prev = PrefixCachingBlock(prev_block=None, + token_ids=token_ids, + block_size=block_size, + allocator=mock_allocator) if is_curr_block_full: # Expect hash since block is full. @@ -71,7 +70,7 @@ class TestPrefixCachingBlock: prev_block=previous_block, token_ids=token_ids, block_size=block_size, - prefix_caching_allocator=mock_allocator, + allocator=mock_allocator, ) if is_curr_block_full and prev_block_has_hash: @@ -138,7 +137,7 @@ class TestPrefixCachingBlock: prev_block=prev_block, token_ids=[], block_size=block_size, - prefix_caching_allocator=allocator, + allocator=allocator, ) tokens_to_append = token_ids[block_number * @@ -159,11 +158,11 @@ class TestPrefixCachingBlockAllocator: prev_block: Optional[Block], token_ids: List[int]): if allocate_type == "immutable": - allocate_block = lambda: allocator.allocate_immutable( + allocate_block = lambda: allocator.allocate_immutable_block( prev_block=prev_block, token_ids=token_ids) elif allocate_type == "mutable": - allocate_block = lambda: allocator.allocate_mutable(prev_block= - prev_block) + allocate_block = lambda: allocator.allocate_mutable_block( + prev_block=prev_block) else: raise ValueError() @@ -233,12 +232,13 @@ class TestPrefixCachingBlockAllocator: # Expect allocation with unseen hash to fail. with pytest.raises(BlockAllocator.NoFreeBlocksError): - allocator.allocate_immutable(prev_block=chain[-1], - token_ids=list(range(block_size))) + allocator.allocate_immutable_block(prev_block=chain[-1], + token_ids=list( + range(block_size))) # Expect mutable allocation to fail. with pytest.raises(BlockAllocator.NoFreeBlocksError): - allocator.allocate_mutable(prev_block=chain[-1]) + allocator.allocate_mutable_block(prev_block=chain[-1]) # Expect allocation of exact same chain to pass. second_chain = TestPrefixCachingBlockAllocator.create_immutable_chain( @@ -270,7 +270,7 @@ class TestPrefixCachingBlockAllocator: # Expect mutable allocation to fail. with pytest.raises(BlockAllocator.NoFreeBlocksError): - allocator.allocate_mutable(prev_block=None) + allocator.allocate_mutable_block(prev_block=None) block_to_free = chain[-1] @@ -280,11 +280,11 @@ class TestPrefixCachingBlockAllocator: allocator.free(block_to_free) assert block_to_free.block_id is None, i - new_block = allocator.allocate_mutable(prev_block=None) + new_block = allocator.allocate_mutable_block(prev_block=None) assert new_block.block_id == block_id, i with pytest.raises(BlockAllocator.NoFreeBlocksError): - allocator.allocate_mutable(prev_block=None) + allocator.allocate_mutable_block(prev_block=None) block_to_free = new_block @@ -376,7 +376,6 @@ class TestPrefixCachingBlockAllocator: # Create token ids that will exhaust all blocks. token_ids = list(range(num_blocks_to_consume * block_size)) - blocks = list(range(num_blocks_to_consume)) first_chain = TestPrefixCachingBlockAllocator.create_immutable_chain( block_size=block_size, @@ -384,9 +383,6 @@ class TestPrefixCachingBlockAllocator: allocator=allocator, ) - # mark all blocks in first chain as computed - allocator.mark_blocks_as_computed(blocks) - # After zero_point, second_chain's token_ids would be set -1, which # make it different from here comparing with first_chain zero_point = random.randint(1, len(token_ids) - 1) @@ -424,15 +420,16 @@ class TestPrefixCachingBlockAllocator: block_size=block_size) token_ids = list(range(block_size)) - block = allocator.allocate_immutable(prev_block=None, - token_ids=token_ids) + block = allocator.allocate_immutable_block(prev_block=None, + token_ids=token_ids) assert allocator._refcounter.get(block.block_id) == 1 - m = allocator.allocate_mutable(prev_block=None) + m = allocator.allocate_mutable_block(prev_block=None) block_id = m.block_id for i in range(block_size): m.append_token_ids([i]) + # After block get promoted to immutable from mutable, if there is # already same content hash block, then it shall be released into # hashless_allocator @@ -452,48 +449,79 @@ class TestPrefixCachingBlockAllocator: all_blocks_list = [i for i in range(num_blocks)] zero_ref = {i: 0 for i in range(num_blocks)} + one_ref = {i: 1 for i in range(num_blocks)} allocator = PrefixCachingBlockAllocator(num_blocks=num_blocks, block_size=block_size) token_ids = list(range(num_blocks * block_size)) - # now we have num_blocks free blocks in hashless allocator - # with internal tracking list _blocks _cached_blocks and evictor - # empty and block's ref shall be 0 + # Verify initial/pre-alloc state + + # Ensure all blocks are free inside hashless allocator assert list(allocator._hashless_allocator._free_block_indices ) == all_blocks_list - assert len(allocator._blocks.keys()) == 0 + # Ensure no tracked blocks + assert len(allocator._block_tracker.keys()) == num_blocks + for block_id in range(num_blocks): + assert not allocator._block_tracker[block_id].active + # Ensure no cached blocks assert len(allocator._cached_blocks.values()) == 0 + # Ensure no evicted blocks assert len(allocator.evictor.free_table.keys()) == 0 + # Ensure 0s ref counts for all blocks assert allocator._refcounter._refcounts == zero_ref # Allocate immutable chains with only one block residuled in new_block = [] for i in range(num_blocks): - block = allocator.allocate_immutable( + block = allocator.allocate_immutable_block( prev_block=None, token_ids=token_ids[block_size * i:block_size * (i + 1)]) new_block.append(block) + # Verify post-alloc state + + # Ensure no blocks are free inside hashless allocator + assert (len(allocator._hashless_allocator._free_block_indices) == 0) + # Ensure all blocks are tracked + assert len(allocator._block_tracker.keys()) == num_blocks + for block_id in range(num_blocks): + assert allocator._block_tracker[block_id].active + # Ensure all blocks are cached (all promoted) + assert len(allocator._cached_blocks.values()) == num_blocks + # Ensure no evicted blocks + assert len(allocator.evictor.free_table.keys()) == 0 + # Ensure 1s ref counts for all blocks + assert allocator._refcounter._refcounts == one_ref + # Free all blocks, and now all blocks shall be in the evictor - # there shall be no tracking data left in _blocks + # there shall be no tracking data left in _block_tracker # all blocks shall be tracked in _cached_blocks # all blocks' ref shall be zero for block in new_block: allocator.free(block) - assert len(allocator._blocks.keys()) == 0 + # Verify post-free state + + # Ensure no tracked blocks + assert len(allocator._block_tracker.keys()) == num_blocks + for block_id in range(num_blocks): + assert not allocator._block_tracker[block_id].active + # Ensure no blocks in hashless allocator (all promoted) assert len(allocator._hashless_allocator._free_block_indices) == 0 + # Ensure all blocks are cached assert list(allocator._cached_blocks.values()) == all_blocks_list + # Ensure all blocks are inside the evictor assert list(allocator.evictor.free_table.keys()) == all_blocks_list + # Ensure 0s refcounts assert allocator._refcounter._refcounts == zero_ref # Allocate a mutable block, and the first block shall be evicted # and set its content hash into None, ref to 1 - mutable = allocator.allocate_mutable(prev_block=None) + mutable = allocator.allocate_mutable_block(prev_block=None) assert mutable.block_id == 0 assert mutable.content_hash is None - assert 0 in allocator._blocks + assert allocator._block_tracker[0].active assert allocator._refcounter.get(0) == 1 assert 0 not in allocator._cached_blocks assert 0 not in allocator.evictor @@ -502,27 +530,27 @@ class TestPrefixCachingBlockAllocator: # hashless allocator allocator.free(mutable) - assert len(allocator._blocks.keys()) == 0 + assert not allocator._block_tracker[0].active assert allocator._refcounter._refcounts == zero_ref assert 0 not in allocator._cached_blocks assert 0 not in allocator.evictor assert 0 in allocator._hashless_allocator._free_block_indices - # when allocate immutable with first block_size tokens, we + # When allocate immutable with first block_size tokens, we # shall get free block from hashless allocator, thus no block left # in hashless - block = allocator.allocate_immutable(prev_block=None, - token_ids=token_ids[:block_size]) + block = allocator.allocate_immutable_block( + prev_block=None, token_ids=token_ids[:block_size]) assert block.block_id == 0 assert len(allocator._hashless_allocator._free_block_indices) == 0 - assert 0 in allocator._blocks + assert allocator._block_tracker[0].active assert 0 in allocator._cached_blocks.values() assert allocator._refcounter.get(0) == 1 assert 0 not in allocator.evictor # allocate mutable block again, it shall be popped from evictor - mutable = allocator.allocate_mutable(prev_block=None) + mutable = allocator.allocate_mutable_block(prev_block=None) assert len(allocator._hashless_allocator._free_block_indices) == 0 assert mutable.block_id not in allocator.evictor.free_table assert allocator._refcounter.get(mutable.block_id) == 1 @@ -619,7 +647,7 @@ class TestPrefixCachingBlockAllocator: block_token_ids = token_ids[block_number * block_size:(block_number + 1) * block_size] - prev_block = allocator.allocate_immutable( + prev_block = allocator.allocate_immutable_block( prev_block=prev_block, token_ids=block_token_ids) blocks.append(prev_block) diff --git a/tests/spec_decode/test_batch_expansion.py b/tests/spec_decode/test_batch_expansion.py index 42dd90422..c350a2c55 100644 --- a/tests/spec_decode/test_batch_expansion.py +++ b/tests/spec_decode/test_batch_expansion.py @@ -90,10 +90,10 @@ def test_create_single_target_seq_group_metadata(k: int): assert output.request_id == input_seq_group_metadata.request_id assert len(output.seq_data) == 1 - assert output.seq_data[target_seq_id].get_prompt_token_ids( - ) == prompt_tokens - assert output.seq_data[target_seq_id].get_output_token_ids( - ) == prev_output_tokens + token_ids + assert output.seq_data[target_seq_id].get_prompt_token_ids() == tuple( + prompt_tokens) + assert output.seq_data[target_seq_id].get_output_token_ids() == tuple( + prev_output_tokens + token_ids) assert len(output.block_tables) == 1 assert output.block_tables[ diff --git a/vllm/core/block/block_table.py b/vllm/core/block/block_table.py index d705f3d91..49e63c231 100644 --- a/vllm/core/block/block_table.py +++ b/vllm/core/block/block_table.py @@ -1,5 +1,6 @@ from typing import List, Optional +from vllm.core.block.common import BlockList from vllm.core.block.interfaces import Block, DeviceAwareBlockAllocator from vllm.utils import Device, cdiv, chunk_list @@ -47,12 +48,10 @@ class BlockTable: self._allocator = block_allocator if _blocks is None: _blocks = [] - self._blocks: List[Block] = _blocks + self._blocks: BlockList = BlockList(_blocks) self._max_block_sliding_window = max_block_sliding_window - # Use helper method instead of directly calculating, as blocks - # may not be allocated. - self._num_full_slots = len(self._get_all_token_ids()) + self._num_full_slots = self._get_num_token_ids() @staticmethod def get_num_required_blocks(token_ids: List[int], block_size: int) -> int: @@ -88,11 +87,18 @@ class BlockTable: """ assert not self._is_allocated assert token_ids - self._blocks = self._allocate_blocks_for_token_ids(prev_block=None, - token_ids=token_ids, - device=device) + blocks = self._allocate_blocks_for_token_ids(prev_block=None, + token_ids=token_ids, + device=device) + self.update(blocks) self._num_full_slots = len(token_ids) + def update(self, blocks: List[Block]) -> None: + """Resets the table to the newly provided blocks + (with their corresponding block ids) + """ + self._blocks.update(blocks) + def append_token_ids(self, token_ids: List[int], num_lookahead_slots: int = 0, @@ -140,11 +146,11 @@ class BlockTable: num_lookahead_slots) # Update the blocks with the new tokens - blocks = self._blocks[self._num_full_slots // self._block_size:] + first_block_idx = self._num_full_slots // self._block_size token_blocks = self._chunk_token_blocks_for_append(token_ids) - for block, token_block in zip(blocks, token_blocks): - block.append_token_ids(token_block) + for i, token_block in enumerate(token_blocks): + self._blocks.append_token_ids(first_block_idx + i, token_block) self._num_full_slots += len(token_ids) @@ -174,8 +180,8 @@ class BlockTable: for _ in range(blocks_to_allocate): assert len(self._blocks) > 0 self._blocks.append( - self._allocator.allocate_mutable(prev_block=self._blocks[-1], - device=device)) + self._allocator.allocate_mutable_block( + prev_block=self._blocks[-1], device=device)) def fork(self) -> "BlockTable": """Creates a new BlockTable instance with a copy of the blocks from the @@ -209,12 +215,12 @@ class BlockTable: is set to `None`. """ assert self._is_allocated - for block in self._blocks: + for block in self.blocks: self._allocator.free(block) - self._blocks = [] + self._blocks.reset() @property - def physical_block_ids(self) -> List[Optional[int]]: + def physical_block_ids(self) -> List[int]: """Returns a list of physical block indices for the blocks in the BlockTable. @@ -228,7 +234,7 @@ class BlockTable: BlockTable. """ assert self._is_allocated - return [block.block_id for block in self._blocks] + return self._blocks.ids() def get_unseen_token_ids(self, sequence_token_ids: List[int]) -> List[int]: """Get the number of "unseen" tokens in the sequence. @@ -253,17 +259,31 @@ class BlockTable: token_ids: List[int], device: Device) -> List[Block]: blocks: List[Block] = [] - for block_token_ids in chunk_list(token_ids, self._block_size): - if len(block_token_ids) == self._block_size: - # If the block is full, create an immutable block. - prev_block = self._allocator.allocate_immutable( - prev_block, token_ids=block_token_ids, device=device) + + block_token_ids = [] + tail_token_ids = [] + for cur_token_ids in chunk_list(token_ids, self._block_size): + if len(cur_token_ids) == self._block_size: + block_token_ids.append(cur_token_ids) else: - # Else, partially fill a mutable block with token ids. - prev_block = self._allocator.allocate_mutable( - prev_block=prev_block, device=device) - prev_block.append_token_ids(block_token_ids) - blocks.append(prev_block) + tail_token_ids.append(cur_token_ids) + + if block_token_ids: + blocks.extend( + self._allocator.allocate_immutable_blocks( + prev_block, block_token_ids=block_token_ids, + device=device)) + prev_block = blocks[-1] + + if tail_token_ids: + assert len(tail_token_ids) == 1 + cur_token_ids = tail_token_ids[0] + + block = self._allocator.allocate_mutable_block( + prev_block=prev_block, device=device) + block.append_token_ids(cur_token_ids) + + blocks.append(block) return blocks @@ -274,18 +294,25 @@ class BlockTable: if not self._is_allocated: return token_ids - for block in self._blocks: + for block in self.blocks: token_ids.extend(block.token_ids) return token_ids + def _get_num_token_ids(self) -> int: + res = 0 + for block in self.blocks: + res += len(block.token_ids) + + return res + @property def _is_allocated(self) -> bool: return len(self._blocks) > 0 @property - def blocks(self) -> Optional[List[Block]]: - return self._blocks + def blocks(self) -> List[Block]: + return self._blocks.list() @property def _num_empty_slots(self) -> int: diff --git a/vllm/core/block/common.py b/vllm/core/block/common.py index d2787d696..1e808e21b 100644 --- a/vllm/core/block/common.py +++ b/vllm/core/block/common.py @@ -1,4 +1,5 @@ -from typing import Dict, Iterable, List, Optional, Protocol, Tuple +from collections import deque +from typing import Deque, Dict, Iterable, List, Optional, Protocol, Tuple from vllm.core.block.interfaces import Block, BlockAllocator @@ -95,64 +96,40 @@ class CopyOnWriteTracker: The CopyOnWriteTracker class maintains a mapping of source block indices to their corresponding copy-on-write destination block indices. It works in - conjunction with a RefCounter and a BlockAllocator to handle reference - counting and block allocation. + conjunction with a RefCounter. Args: refcounter (RefCounter): The reference counter used to track block reference counts. - allocator (BlockAllocator): The block allocator used to allocate and - free blocks. """ - def __init__( - self, - refcounter: RefCounterProtocol, - allocator: BlockAllocator, - ): + def __init__(self, refcounter: RefCounterProtocol): self._copy_on_writes: List[Tuple[BlockId, BlockId]] = [] self._refcounter = refcounter - self._allocator = allocator - - def cow_block_if_not_appendable(self, block: Block) -> Optional[BlockId]: - """Performs a copy-on-write operation on the given block if it is not - appendable. - - This method checks the reference count of the given block. If the - reference count is greater than 1, indicating that the block is shared, - a copy-on-write operation is performed. The original block is freed, - and a new block is allocated with the same content. The new block index - is returned. - - Args: - block (Block): The block to check for copy-on-write. - Returns: - Optional[BlockId]: The block index of the new block if a copy-on - -write operation was performed, or the original block index if - no copy-on-write was necessary. + def is_appendable(self, block: Block) -> bool: + """Checks if the block is shared or not. If shared, then it cannot + be appended and needs to be duplicated via copy-on-write """ block_id = block.block_id if block_id is None: - return block_id + return True refcount = self._refcounter.get(block_id) - assert refcount != 0 - if refcount > 1: - src_block_id = block_id - # Decrement refcount of the old block. - self._allocator.free(block) - - # Allocate a fresh new block. - block_id = self._allocator.allocate_mutable( - prev_block=block.prev_block).block_id + return refcount <= 1 - # Track src/dst copy. - assert src_block_id is not None - assert block_id is not None - self._copy_on_writes.append((src_block_id, block_id)) - - return block_id + def record_cow(self, src_block_id: Optional[BlockId], + trg_block_id: Optional[BlockId]) -> None: + """Records a copy-on-write operation from source to target block id + Args: + src_block_id (BlockId): The source block id from which to copy + the data + trg_block_id (BlockId): The target block id to which the data + is copied + """ + assert src_block_id is not None + assert trg_block_id is not None + self._copy_on_writes.append((src_block_id, trg_block_id)) def clear_cows(self) -> List[Tuple[BlockId, BlockId]]: """Clears the copy-on-write tracking information and returns the current @@ -172,6 +149,139 @@ class CopyOnWriteTracker: return cows +class BlockPool: + """Used to pre-allocate block objects, in order to avoid excessive python + object allocations/deallocations. + The pool starts from "pool_size" objects and will increase to more objects + if necessary + + Note that multiple block objects may point to the same physical block id, + which is why this pool is needed, so that it will be easier to support + prefix caching and more complicated sharing of physical blocks. + """ + + def __init__(self, block_size: int, create_block: Block.Factory, + allocator: BlockAllocator, pool_size: int): + self._block_size = block_size + self._create_block = create_block + self._allocator = allocator + self._pool_size = pool_size + assert self._pool_size >= 0 + + self._free_ids: Deque[int] = deque(range(self._pool_size)) + self._pool = [] + for i in range(self._pool_size): + self._pool.append( + self._create_block(prev_block=None, + token_ids=[], + block_size=self._block_size, + allocator=self._allocator, + block_id=None)) + + def increase_pool(self): + """Doubles the internal pool size + """ + cur_pool_size = self._pool_size + new_pool_size = cur_pool_size * 2 + self._pool_size = new_pool_size + + self._free_ids += deque(range(cur_pool_size, new_pool_size)) + + for i in range(cur_pool_size, new_pool_size): + self._pool.append( + self._create_block(prev_block=None, + token_ids=[], + block_size=self._block_size, + allocator=self._allocator, + block_id=None)) + + def init_block(self, prev_block: Optional[Block], token_ids: List[int], + block_size: int, physical_block_id: Optional[int]) -> Block: + if len(self._free_ids) == 0: + self.increase_pool() + assert len(self._free_ids) > 0 + + pool_id = self._free_ids.popleft() + + block = self._pool[pool_id] + block.__init__( # type: ignore[misc] + prev_block=prev_block, + token_ids=token_ids, + block_size=block_size, + allocator=block._allocator, # type: ignore[attr-defined] + block_id=physical_block_id) + block.pool_id = pool_id # type: ignore[attr-defined] + return block + + def free_block(self, block: Block) -> None: + self._free_ids.appendleft(block.pool_id) # type: ignore[attr-defined] + + +class BlockList: + """This class is an optimization to allow fast-access to physical + block ids. It maintains a block id list that is updated with the + block list and this avoids the need to reconstruct the block id + list on every iteration of the block manager + """ + + def __init__(self, blocks: List[Block]): + self._blocks: List[Block] = [] + self._block_ids: List[int] = [] + + self.update(blocks) + + def _add_block_id(self, block_id: Optional[BlockId]) -> None: + assert block_id is not None + self._block_ids.append(block_id) + + def _update_block_id(self, block_index: int, + new_block_id: Optional[BlockId]) -> None: + assert new_block_id is not None + self._block_ids[block_index] = new_block_id + + def update(self, blocks: List[Block]): + self._blocks = blocks + + # Cache block ids for fast query + self._block_ids = [] + for block in self._blocks: + self._add_block_id(block.block_id) + + def append_token_ids(self, block_index: int, token_ids: List[int]) -> None: + block = self._blocks[block_index] + prev_block_id = block.block_id + + block.append_token_ids(token_ids) + + # CoW or promotion may update the internal block_id + if prev_block_id != block.block_id: + self._update_block_id(block_index, block.block_id) + + def append(self, new_block: Block): + self._blocks.append(new_block) + self._add_block_id(new_block.block_id) + + def __len__(self) -> int: + return len(self._blocks) + + def __getitem__(self, block_index: int) -> Block: + return self._blocks[block_index] + + def __setitem__(self, block_index: int, new_block: Block) -> None: + self._blocks[block_index] = new_block + self._update_block_id(block_index, new_block.block_id) + + def reset(self): + self._blocks = [] + self._block_ids = [] + + def list(self) -> List[Block]: + return self._blocks + + def ids(self) -> List[int]: + return self._block_ids + + def get_all_blocks_recursively(last_block: Block) -> List[Block]: """Retrieves all the blocks in a sequence starting from the last block. diff --git a/vllm/core/block/cpu_gpu_block_allocator.py b/vllm/core/block/cpu_gpu_block_allocator.py index 255aae9d1..5287cd9c1 100644 --- a/vllm/core/block/cpu_gpu_block_allocator.py +++ b/vllm/core/block/cpu_gpu_block_allocator.py @@ -113,11 +113,11 @@ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): def allocate_or_get_null_block(self) -> Block: if self._null_block is None: self._null_block = NullBlock( - self.allocate_mutable(None, Device.GPU)) + self.allocate_mutable_block(None, Device.GPU)) return self._null_block - def allocate_mutable(self, prev_block: Optional[Block], - device: Device) -> Block: + def allocate_mutable_block(self, prev_block: Optional[Block], + device: Device) -> Block: """Allocates a new mutable block on the specified device. Args: @@ -128,10 +128,31 @@ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): Returns: Block: The newly allocated mutable block. """ - return self._allocators[device].allocate_mutable(prev_block) + return self._allocators[device].allocate_mutable_block(prev_block) - def allocate_immutable(self, prev_block: Optional[Block], - token_ids: List[int], device: Device) -> Block: + def allocate_immutable_blocks(self, prev_block: Optional[Block], + block_token_ids: List[List[int]], + device: Optional[Device]) -> List[Block]: + """Allocates a new group of immutable blocks with the provided block + token IDs on the specified device. + + Args: + prev_block (Optional[Block]): The previous block in the sequence. + Used for prefix hashing. + block_token_ids (List[int]): The list of block token IDs to be + stored in the new blocks. + device (Device): The device on which to allocate the new block. + + Returns: + List[Block]: The newly allocated list of immutable blocks + containing the provided block token IDs. + """ + return self._allocators[device].allocate_immutable_blocks( + prev_block, block_token_ids) + + def allocate_immutable_block(self, prev_block: Optional[Block], + token_ids: List[int], + device: Device) -> Block: """Allocates a new immutable block with the provided token IDs on the specified device. @@ -146,7 +167,7 @@ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): Block: The newly allocated immutable block containing the provided token IDs. """ - return self._allocators[device].allocate_immutable( + return self._allocators[device].allocate_immutable_block( prev_block, token_ids) def free(self, block: Block) -> None: @@ -161,7 +182,7 @@ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): block_id = block.block_id assert block_id is not None allocator = self._block_ids_to_allocator[block_id] - return allocator.free(block) + allocator.free(block) def fork(self, last_block: Block) -> List[Block]: """Creates a new sequence of blocks that shares the same underlying @@ -210,8 +231,8 @@ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): """ return self._allocators[device].get_physical_block_id(absolute_id) - def swap(self, blocks: List[Block], source_device: Device, - dest_device: Device) -> Dict[int, int]: + def swap(self, blocks: List[Block], src_device: Device, + dst_device: Device) -> Dict[int, int]: """Execute the swap for the given blocks from source_device on to dest_device, save the current swap mapping and append them to the accumulated `self._swap_mapping` for each @@ -219,23 +240,23 @@ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): Args: blocks: List of blocks to be swapped. - source_device (Device): Device to swap the 'blocks' from. - dest_device (Device): Device to swap the 'blocks' to. + src_device (Device): Device to swap the 'blocks' from. + dst_device (Device): Device to swap the 'blocks' to. Returns: Dict[int, int]: Swap mapping from source_device on to dest_device. """ - source_block_ids = [block.block_id for block in blocks] - self._allocators[source_device].swap_out(blocks) - self._allocators[dest_device].swap_in(blocks) - dest_block_ids = [block.block_id for block in blocks] + src_block_ids = [block.block_id for block in blocks] + self._allocators[src_device].swap_out(blocks) + self._allocators[dst_device].swap_in(blocks) + dst_block_ids = [block.block_id for block in blocks] current_swap_mapping: Dict[int, int] = {} - for src, dest in zip(source_block_ids, dest_block_ids): - if src is not None and dest is not None: - self._swap_mapping[src] = dest - current_swap_mapping[src] = dest + for src_block_id, dst_block_id in zip(src_block_ids, dst_block_ids): + if src_block_id is not None and dst_block_id is not None: + self._swap_mapping[src_block_id] = dst_block_id + current_swap_mapping[src_block_id] = dst_block_id return current_swap_mapping def get_num_blocks_touched(self, @@ -283,23 +304,25 @@ class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): device = Device.GPU return self._allocators[device].mark_blocks_as_computed(block_ids) + def get_computed_block_ids(self, prev_computed_block_ids: List[int], + block_ids: List[int], + skip_last_block_id: bool) -> List[int]: + # Prefix caching only supported on GPU. + device = Device.GPU + return self._allocators[device].get_computed_block_ids( + prev_computed_block_ids, block_ids, skip_last_block_id) + def get_common_computed_block_ids( - self, seq_block_ids: List[List[int]]) -> List[int]: + self, computed_seq_block_ids: List[List[int]]) -> List[int]: # Prefix caching only supported on GPU. device = Device.GPU return self._allocators[device].get_common_computed_block_ids( - seq_block_ids) + computed_seq_block_ids) @property def all_block_ids(self) -> FrozenSet[int]: return frozenset(self._block_ids_to_allocator.keys()) - def promote_to_immutable_block(self, block: Block) -> BlockId: - raise NotImplementedError - - def cow_block_if_not_appendable(self, block: Block) -> Optional[BlockId]: - raise NotImplementedError - def get_and_reset_swaps(self) -> List[Tuple[int, int]]: """Returns and clears the mapping of source to destination block IDs. Will be called after every swapping operations for now, and after every @@ -341,6 +364,11 @@ class NullBlock(Block): def token_ids(self) -> List[BlockId]: return self._proxy.token_ids + @property + def num_tokens_total(self) -> int: + raise NotImplementedError( + "num_tokens_total is not used for null block") + @property def num_empty_slots(self) -> BlockId: return self._proxy.num_empty_slots diff --git a/vllm/core/block/interfaces.py b/vllm/core/block/interfaces.py index 4b20856a1..ab39832bc 100644 --- a/vllm/core/block/interfaces.py +++ b/vllm/core/block/interfaces.py @@ -28,6 +28,13 @@ class Block(ABC): def token_ids(self) -> List[int]: pass + @property + @abstractmethod + def num_tokens_total(self) -> int: + """The number of tokens till the current block (inclusive) + """ + pass + @property @abstractmethod def num_empty_slots(self) -> int: @@ -92,12 +99,18 @@ class Block(ABC): class BlockAllocator(ABC): @abstractmethod - def allocate_mutable(self, prev_block: Optional[Block]) -> Block: + def allocate_mutable_block(self, prev_block: Optional[Block]) -> Block: pass @abstractmethod - def allocate_immutable(self, prev_block: Optional[Block], - token_ids: List[int]) -> Block: + def allocate_immutable_block(self, prev_block: Optional[Block], + token_ids: List[int]) -> Block: + pass + + @abstractmethod + def allocate_immutable_blocks( + self, prev_block: Optional[Block], + block_token_ids: List[List[int]]) -> List[Block]: pass @abstractmethod @@ -146,13 +159,19 @@ class BlockAllocator(ABC): def mark_blocks_as_computed(self, block_ids: List[int]) -> None: pass + @abstractmethod + def get_computed_block_ids(self, prev_computed_block_ids: List[int], + block_ids: List[int], + skip_last_block_id: bool) -> List[int]: + pass + @abstractmethod def get_common_computed_block_ids( - self, seq_block_ids: List[List[int]]) -> List[int]: + self, computed_seq_block_ids: List[List[int]]) -> List[int]: pass @abstractmethod - def cow_block_if_not_appendable(self, block: Block) -> Optional["BlockId"]: + def cow_block_if_not_appendable(self, block: Block) -> BlockId: """NOTE: This should not be used besides Block""" pass @@ -174,13 +193,20 @@ class BlockAllocator(ABC): class DeviceAwareBlockAllocator(ABC): @abstractmethod - def allocate_mutable(self, prev_block: Optional[Block], - device: Device) -> Block: + def allocate_mutable_block(self, prev_block: Optional[Block], + device: Device) -> Block: + pass + + @abstractmethod + def allocate_immutable_block(self, prev_block: Optional[Block], + token_ids: List[int], + device: Device) -> Block: pass @abstractmethod - def allocate_immutable(self, prev_block: Optional[Block], - token_ids: List[int], device: Device) -> Block: + def allocate_immutable_blocks(self, prev_block: Optional[Block], + block_token_ids: List[List[int]], + device: Device) -> List[Block]: pass @abstractmethod @@ -217,9 +243,15 @@ class DeviceAwareBlockAllocator(ABC): def mark_blocks_as_computed(self, block_ids: List[int]) -> None: pass + @abstractmethod + def get_computed_block_ids(self, prev_computed_block_ids: List[int], + block_ids: List[int], + skip_last_block_id: bool) -> List[int]: + pass + @abstractmethod def get_common_computed_block_ids( - self, seq_block_ids: List[List[int]]) -> List[int]: + self, computed_seq_block_ids: List[List[int]]) -> List[int]: pass @abstractmethod @@ -230,8 +262,8 @@ class DeviceAwareBlockAllocator(ABC): pass @abstractmethod - def swap(self, blocks: List[Block], source_device: Device, - dest_device: Device) -> Dict[int, int]: + def swap(self, blocks: List[Block], src_device: Device, + dst_device: Device) -> Dict[int, int]: pass @abstractmethod diff --git a/vllm/core/block/naive_block.py b/vllm/core/block/naive_block.py index 50f27bab3..0c1e88314 100644 --- a/vllm/core/block/naive_block.py +++ b/vllm/core/block/naive_block.py @@ -1,6 +1,7 @@ -from typing import FrozenSet, Iterable, List, Optional, Set, Tuple +from collections import deque +from typing import Deque, FrozenSet, Iterable, List, Optional, Tuple -from vllm.core.block.common import (CopyOnWriteTracker, RefCounter, +from vllm.core.block.common import (BlockPool, CopyOnWriteTracker, RefCounter, get_all_blocks_recursively) from vllm.core.block.interfaces import Block, BlockAllocator, BlockId, Device from vllm.utils import cdiv @@ -31,28 +32,39 @@ class NaiveBlockAllocator(BlockAllocator): num_blocks: int, block_size: int, block_ids: Optional[Iterable[int]] = None, + block_pool: Optional[BlockPool] = None, ): if block_ids is None: block_ids = range(num_blocks) - self._free_block_indices: Set[BlockId] = set(block_ids) + self._free_block_indices: Deque[BlockId] = deque(block_ids) self._all_block_indices = frozenset(block_ids) assert len(self._all_block_indices) == num_blocks self._refcounter = RefCounter( all_block_indices=self._free_block_indices) - self._create_block = create_block self._block_size = block_size self._cow_tracker = CopyOnWriteTracker( - refcounter=self._refcounter.as_readonly(), - allocator=self, - ) - - def allocate_immutable(self, - prev_block: Optional[Block], - token_ids: List[int], - device: Optional[Device] = None) -> Block: + refcounter=self._refcounter.as_readonly()) + + if block_pool is None: + extra_factor = 4 + # Pre-allocate "num_blocks * extra_factor" block objects. + # The "* extra_factor" is a buffer to allow more block objects + # than physical blocks + self._block_pool = BlockPool(self._block_size, create_block, self, + num_blocks * extra_factor) + else: + # In this case, the block pool is provided by the caller, + # which means that there is most likely a need to share + # a block pool between allocators + self._block_pool = block_pool + + def allocate_immutable_block(self, + prev_block: Optional[Block], + token_ids: List[int], + device: Optional[Device] = None) -> Block: """Allocates a new immutable block with the given token IDs, linked to the previous block. @@ -66,13 +78,36 @@ class NaiveBlockAllocator(BlockAllocator): Block: The newly allocated immutable block. """ assert device is None - block = self.allocate_mutable(prev_block=prev_block) + block = self.allocate_mutable_block(prev_block=prev_block) block.append_token_ids(token_ids) return block - def allocate_mutable(self, - prev_block: Optional[Block], - device: Optional[Device] = None) -> Block: + def allocate_immutable_blocks( + self, + prev_block: Optional[Block], + block_token_ids: List[List[int]], + device: Optional[Device] = None) -> List[Block]: + assert device is None + num_blocks = len(block_token_ids) + + block_ids = [] + for i in range(num_blocks): + block_ids.append(self._allocate_block_id()) + + blocks = [] + for i in range(num_blocks): + prev_block = self._block_pool.init_block( + prev_block=prev_block, + token_ids=block_token_ids[i], + block_size=self._block_size, + physical_block_id=block_ids[i]) + blocks.append(prev_block) + + return blocks + + def allocate_mutable_block(self, + prev_block: Optional[Block], + device: Optional[Device] = None) -> Block: """Allocates a new mutable block, linked to the previous block. Args: @@ -84,20 +119,39 @@ class NaiveBlockAllocator(BlockAllocator): Block: The newly allocated mutable block. """ assert device is None - block_id = self._allocate_new_block_id() - return self._create_block( - prev_block=prev_block, - token_ids=[], - block_id=block_id, - block_size=self._block_size, - allocator=self, - ) - - def free(self, block: Block) -> None: - assert block.block_id is not None - self._free_block_id(block.block_id) + block_id = self._allocate_block_id() + block = self._block_pool.init_block(prev_block=prev_block, + token_ids=[], + block_size=self._block_size, + physical_block_id=block_id) + return block + + def _allocate_block_id(self) -> BlockId: + if not self._free_block_indices: + raise BlockAllocator.NoFreeBlocksError() + + block_id = self._free_block_indices.popleft() + self._refcounter.incr(block_id) + return block_id + + def _free_block_id(self, block: Block) -> None: + block_id = block.block_id + assert block_id is not None + + refcount = self._refcounter.decr(block_id) + if refcount == 0: + self._free_block_indices.appendleft(block_id) + block.block_id = None + def free(self, block: Block, keep_block_object: bool = False) -> None: + # Release the physical block id + self._free_block_id(block) + + # Release the block object + if not keep_block_object: + self._block_pool.free_block(block) + def fork(self, last_block: Block) -> List[Block]: """Creates a new sequence of blocks that shares the same underlying memory as the original sequence. @@ -120,14 +174,13 @@ class NaiveBlockAllocator(BlockAllocator): refcount = self._refcounter.incr(block.block_id) assert refcount != 1, "can't fork free'd block" - forked_blocks.append( - self._create_block( - prev_block=prev_block, - token_ids=block.token_ids, - block_id=block.block_id, - block_size=self._block_size, - allocator=self, - )) + forked_block = self._block_pool.init_block( + prev_block=prev_block, + token_ids=block.token_ids, + block_size=self._block_size, + physical_block_id=block.block_id) + + forked_blocks.append(forked_block) prev_block = forked_blocks[-1] return forked_blocks @@ -138,20 +191,6 @@ class NaiveBlockAllocator(BlockAllocator): def get_num_total_blocks(self) -> int: return len(self._all_block_indices) - def _allocate_new_block_id(self) -> BlockId: - if not self._free_block_indices: - raise BlockAllocator.NoFreeBlocksError() - - block_id = next(iter(self._free_block_indices)) - self._refcounter.incr(block_id) - self._free_block_indices.remove(block_id) - return block_id - - def _free_block_id(self, block_id: BlockId) -> None: - refcount = self._refcounter.decr(block_id) - if refcount == 0: - self._free_block_indices.add(block_id) - def get_physical_block_id(self, absolute_id: int) -> int: """Returns the zero-offset block id on certain block allocator given the absolute block id. @@ -173,7 +212,7 @@ class NaiveBlockAllocator(BlockAllocator): def all_block_ids(self) -> FrozenSet[int]: return self._all_block_indices - def cow_block_if_not_appendable(self, block: Block) -> Optional[BlockId]: + def cow_block_if_not_appendable(self, block: Block) -> BlockId: """Performs a copy-on-write operation on the given block if it is not appendable. @@ -181,11 +220,22 @@ class NaiveBlockAllocator(BlockAllocator): block (Block): The block to check for copy-on-write. Returns: - Optional[BlockId]: The block index of the new block if a copy-on - -write operation was performed, or the original block index if + BlockId: The block index of the new block if a copy-on-write + operation was performed, or the original block index if no copy-on-write was necessary. """ - return self._cow_tracker.cow_block_if_not_appendable(block) + src_block_id = block.block_id + assert src_block_id is not None + + if self._cow_tracker.is_appendable(block): + return src_block_id + + self._free_block_id(block) + trg_block_id = self._allocate_block_id() + + self._cow_tracker.record_cow(src_block_id, trg_block_id) + + return trg_block_id def clear_copy_on_writes(self) -> List[Tuple[BlockId, BlockId]]: """Returns the copy-on-write source->destination mapping and clears it. @@ -213,8 +263,15 @@ class NaiveBlockAllocator(BlockAllocator): """ pass + def get_computed_block_ids(self, prev_computed_block_ids: List[int], + block_ids: List[int], + skip_last_block_id: bool) -> List[int]: + """No prefix caching here => return empty list + """ + return [] + def get_common_computed_block_ids( - self, seq_block_ids: List[List[int]]) -> List[int]: + self, computed_seq_block_ids: List[List[int]]) -> List[int]: """Determine blocks that can be skipped in prefill. Since the naive allocator does not support prefix caching, always return @@ -223,7 +280,7 @@ class NaiveBlockAllocator(BlockAllocator): return [] def promote_to_immutable_block(self, block: Block) -> BlockId: - raise NotImplementedError + raise NotImplementedError("There is no promotion for naive blocks") def get_num_blocks_touched(self, blocks: List[Block], @@ -263,17 +320,27 @@ class NaiveBlockAllocator(BlockAllocator): def swap_out(self, blocks: List[Block]) -> None: for block in blocks: - self.free(block) + self._free_block_id(block) def swap_in(self, blocks: List[Block]) -> None: for block in blocks: + # Here we allocate either immutable or mutable block and then + # extract its block_id. Note that the block object is released + # and the block_id is assigned to "block" to allow reusing the + # existing "block" object if block.is_full: - alloc = self.allocate_immutable(block.prev_block, - block.token_ids) + tmp_block = self.allocate_immutable_block( + prev_block=block.prev_block, token_ids=block.token_ids) else: - alloc = self.allocate_mutable(block.prev_block) - alloc.append_token_ids(block.token_ids) - block.block_id = alloc.block_id + tmp_block = self.allocate_mutable_block( + prev_block=block.prev_block) + tmp_block.append_token_ids(block.token_ids) + + block_id = tmp_block.block_id + tmp_block.block_id = None + self._block_pool.free_block(tmp_block) + + block.block_id = block_id # Assign block_id class NaiveBlock(Block): @@ -315,11 +382,12 @@ class NaiveBlock(Block): self._append_token_ids_no_cow(token_ids) def append_token_ids(self, token_ids: List[int]) -> None: - """Appends the given token IDs to the block, instructing the allocator - to perform a copy-on-write if necessary. + """Appends the given token IDs to the block and performs a + copy-on-write if necessary. Args: - token_ids (List[int]): The token IDs to be appended to the block. + token_ids (Optional[List[int]]): The token IDs to be appended + to the block. """ self._append_token_ids_no_cow(token_ids) @@ -328,7 +396,16 @@ class NaiveBlock(Block): self._cow_target)) def _append_token_ids_no_cow(self, token_ids: List[int]) -> None: - assert self.num_empty_slots >= len(token_ids) + """Appends the given token IDs to the block + + Args: + token_ids (List[int]): The token IDs to be appended to the block. + """ + if len(token_ids) == 0: + return + + assert len(token_ids) <= self.num_empty_slots + self._token_ids.extend(token_ids) @property @@ -361,12 +438,17 @@ class NaiveBlock(Block): @property def num_empty_slots(self) -> int: - return self._block_size - len(self._token_ids) + return self._block_size - len(self.token_ids) @property def token_ids(self) -> List[int]: return self._token_ids + @property + def num_tokens_total(self) -> int: + raise NotImplementedError( + "num_tokens_total is not used for naive block") + @property def block_size(self) -> int: return self._block_size diff --git a/vllm/core/block/prefix_caching_block.py b/vllm/core/block/prefix_caching_block.py index 2df7d74e4..f272e23ee 100644 --- a/vllm/core/block/prefix_caching_block.py +++ b/vllm/core/block/prefix_caching_block.py @@ -1,13 +1,13 @@ """Token blocks.""" -from itertools import takewhile from os.path import commonprefix from typing import Dict, FrozenSet, Iterable, List, Optional, Tuple from vllm.core.block.common import (CopyOnWriteTracker, get_all_blocks_recursively) from vllm.core.block.interfaces import Block, BlockAllocator, BlockId, Device -from vllm.core.block.naive_block import NaiveBlock, NaiveBlockAllocator +from vllm.core.block.naive_block import (BlockPool, NaiveBlock, + NaiveBlockAllocator) from vllm.core.evictor_v2 import EvictionPolicy, Evictor, make_evictor from vllm.utils import cdiv @@ -19,6 +19,30 @@ PrefixHash = int _DEFAULT_LAST_ACCESSED_TIME = -1 +class BlockTracker: + """Used to track the status of a block inside the prefix caching allocator + """ + __slots__ = ("active", "last_accessed", "computed") + + def reset(self): + self.last_accessed: float = _DEFAULT_LAST_ACCESSED_TIME + self.computed: bool = False + + def __init__(self): + self.active: bool = False + self.reset() + + def enable(self): + assert not self.active + self.active = True + self.reset() + + def disable(self): + assert self.active + self.active = False + self.reset() + + class PrefixCachingBlockAllocator(BlockAllocator): """A block allocator that implements prefix caching. @@ -41,12 +65,26 @@ class PrefixCachingBlockAllocator(BlockAllocator): block_ids: Optional[Iterable[int]] = None, eviction_policy: EvictionPolicy = EvictionPolicy.LRU, ): + if block_ids is None: + block_ids = range(num_blocks) + + self._block_size = block_size + # A mapping of prefix hash to block index. All blocks which have a # prefix hash will be in this dict, even if they have refcount 0. self._cached_blocks: Dict[PrefixHash, BlockId] = {} - # A mapping of blockId to Block to track those cached blocks - self._blocks: Dict[BlockId, Block] = {} + # Used to track status of each physical block id + self._block_tracker: Dict[BlockId, BlockTracker] = {} + for block_id in block_ids: + self._block_tracker[block_id] = BlockTracker() + + # Pre-allocate "num_blocks * extra_factor" block objects. + # The "* extra_factor" is a buffer to allow more block objects + # than physical blocks + extra_factor = 4 + self._block_pool = BlockPool(self._block_size, self._create_block, + self, num_blocks * extra_factor) # An allocator for blocks that do not have prefix hashes. self._hashless_allocator = NaiveBlockAllocator( @@ -54,10 +92,9 @@ class PrefixCachingBlockAllocator(BlockAllocator): num_blocks=num_blocks, block_size=block_size, block_ids=block_ids, + block_pool=self._block_pool, # Share block pool here ) - self._block_size = block_size - # Evitor used to maintain how we want to handle those computed blocks # if we find memory pressure is high. self.evictor: Evictor = make_evictor(eviction_policy) @@ -68,9 +105,7 @@ class PrefixCachingBlockAllocator(BlockAllocator): self._refcounter = self._hashless_allocator.refcounter self._cow_tracker = CopyOnWriteTracker( - refcounter=self._refcounter.as_readonly(), - allocator=self, - ) + refcounter=self._refcounter.as_readonly()) # Implements Block.Factory. def _create_block( @@ -90,14 +125,14 @@ class PrefixCachingBlockAllocator(BlockAllocator): token_ids=token_ids, block_size=block_size, block_id=block_id, - prefix_caching_allocator=allocator, + allocator=allocator, computed=computed, ) - def allocate_immutable(self, - prev_block: Optional[Block], - token_ids: List[int], - device: Optional[Device] = None) -> Block: + def allocate_immutable_block(self, + prev_block: Optional[Block], + token_ids: List[int], + device: Optional[Device] = None) -> Block: """Allocates an immutable block with the given token IDs, reusing cached blocks if possible. @@ -111,29 +146,41 @@ class PrefixCachingBlockAllocator(BlockAllocator): assert device is None assert_prefix_caching_block_or_none(prev_block) - block = self._create_block( - prev_block=prev_block, - token_ids=token_ids, - block_size=self._block_size, - allocator=self, - ) + # First, try to create a block that points to cached data + block = self._block_pool.init_block(prev_block=prev_block, + token_ids=token_ids, + block_size=self._block_size, + physical_block_id=None) assert block.content_hash is not None cached_block_id = self._cached_blocks.get(block.content_hash, None) if cached_block_id is not None: block.block_id = cached_block_id - self._incr_refcount_cached_block(block, block.block_id) + self._incr_refcount_cached_block(block) return block + self._block_pool.free_block(block) - block = self.allocate_mutable(prev_block) + # No cached block => Allocate a new block + block = self.allocate_mutable_block(prev_block) block.append_token_ids(token_ids) - assert block.content_hash is not None - return block - def allocate_mutable(self, - prev_block: Optional[Block], - device: Optional[Device] = None) -> Block: + def allocate_immutable_blocks( + self, + prev_block: Optional[Block], + block_token_ids: List[List[int]], + device: Optional[Device] = None) -> List[Block]: + blocks = [] + for token_ids in block_token_ids: + prev_block = self.allocate_immutable_block(prev_block=prev_block, + token_ids=token_ids, + device=device) + blocks.append(prev_block) + return blocks + + def allocate_mutable_block(self, + prev_block: Optional[Block], + device: Optional[Device] = None) -> Block: """Allocates a mutable block. If there are no free blocks, this will evict unused cached blocks. @@ -147,116 +194,154 @@ class PrefixCachingBlockAllocator(BlockAllocator): assert device is None assert_prefix_caching_block_or_none(prev_block) - try: - block = self._hashless_allocator.allocate_mutable( - prev_block=prev_block) - - assert block.block_id not in self._blocks - assert block.block_id is not None - self._blocks[block.block_id] = block - return block - except BlockAllocator.NoFreeBlocksError: - # We must check the unused cached blocks before raising OOM. - pass - - # If the evictor has blocks available for eviction, evict a block - # and return it. - if self.evictor.num_blocks > 0: - # here we get an evicted block, which is only added - # into evictor if its ref counter is 0 - # and since its content would be changed, we need - # to remove it from _cached_blocks's tracking list - block_id, content_hash_to_evict = self.evictor.evict() - - _block_id = self._cached_blocks[content_hash_to_evict] - assert self._refcounter.get(_block_id) == 0 - assert _block_id == block_id - - self._cached_blocks.pop(content_hash_to_evict) - - self._refcounter.incr(block_id) - - # Now this block is pop from evictor and ready to write - # with new content which most probably different with - # original content. So need to tell worker to recompute - # its kvcache - block = self._create_block( - prev_block=prev_block, - token_ids=[], - block_size=self._block_size, - allocator=self, - block_id=block_id, - computed=False, - ) - assert block.content_hash is None - - assert block.block_id not in self._blocks - assert block.block_id is not None - self._blocks[block.block_id] = block - return block - - # No block available in hashless allocator, nor in unused cache blocks. - raise BlockAllocator.NoFreeBlocksError() + block_id = self._allocate_block_id() + block = self._block_pool.init_block(prev_block=prev_block, + token_ids=[], + block_size=self._block_size, + physical_block_id=block_id) + assert not block.computed + assert block.content_hash is None + return block - def _incr_refcount_cached_block(self, block: Block, - block_id: BlockId) -> None: - # now _incr_refcount_cached_block comes from two place - # allocate_immutable/promote_to_immutable_block where hit - # _cached_blocks hash key. - # In both cases, it means that already exists a already - # computed block which shared with block now + def _incr_refcount_cached_block(self, block: Block) -> None: + # Set this block to be "computed" since it is pointing to a + # cached block id (which was already computed) block.computed = True + block_id = block.block_id + assert block_id is not None + refcount = self._refcounter.incr(block_id) if refcount == 1: - # if block get referred, then it shall not be in evictor - # and put it into _blocks for tracking + # In case a cached block was evicted, restore its tracking if block_id in self.evictor: self.evictor.remove(block_id) - self._blocks[block_id] = block - def free(self, block: Block) -> None: - """Decrement the refcount of the block. If the decremented refcount is - zero, store the block in the freelist. + self._track_block_id(block_id, computed=True) - If the block has a content hash (meaning it is immutable), then we will - keep the block around in case future allocations require it. - """ - assert (block.block_id - is not None), "freeing unallocated block is undefined" + def _decr_refcount_cached_block(self, block: Block) -> None: + # Ensure this is immutable/cached block + assert block.content_hash is not None + + block_id = block.block_id + assert block_id is not None + + refcount = self._refcounter.decr(block_id) + if refcount > 0: + block.block_id = None + return + else: + assert refcount == 0 - self._free_block_id_for_block(block.block_id, block) + # No longer used + assert block.content_hash in self._cached_blocks + + # Add the cached block to the evictor + # (This keeps the cached block around so it can be reused) + self.evictor.add(block_id, block.content_hash, block.num_tokens_total, + self._block_tracker[block_id].last_accessed) + + # Stop tracking the block + self._untrack_block_id(block_id) block.block_id = None - def _free_block_id_for_block(self, block_id: BlockId, - block: Block) -> None: - assert isinstance(block, PrefixCachingBlock) - - # if we comes from promote_to_immutable_block, it means that - # block.content_hash is never None. - # However we need to release the same content block, so that - # physical block could get reused. - if block.block_id != block_id or block.content_hash is None: - refcount = self._refcounter.get(block_id) - # We have fork case where block would get more than one ref, - # so we cannot free it from tracking if ref cnt large than 1 - assert block.block_id is not None - refcount = self._refcounter.get(block.block_id) - if refcount == 1: - del self._blocks[block.block_id] - - return self._hashless_allocator.free(block) + def _decr_refcount_hashless_block(self, block: Block) -> None: + block_id = block.block_id + assert block_id is not None - refcount = self._refcounter.decr(block_id) + # We may have a fork case where block is shared, + # in which case, we cannot remove it from tracking + refcount = self._refcounter.get(block_id) + if refcount == 1: + self._untrack_block_id(block_id) - # If no longer used, add the block to the evictor. - if refcount == 0: - assert block.content_hash in self._cached_blocks - assert block.block_id is not None - del self._blocks[block.block_id] - self.evictor.add(block.block_id, block.content_hash, - block.num_tokens_total, block.last_accessed) + # Decrement refcount of the block_id, but do not free the block object + # itself (will be handled by the caller) + self._hashless_allocator.free(block, keep_block_object=True) + + def _allocate_block_id(self) -> BlockId: + """First tries to allocate a block id from the hashless allocator, + and if there are no blocks, then tries to evict an unused cached block. + """ + hashless_block_id = self._maybe_allocate_hashless_block_id() + if hashless_block_id is not None: + return hashless_block_id + + evicted_block_id = self._maybe_allocate_evicted_block_id() + if evicted_block_id is not None: + return evicted_block_id + + # No block available in hashless allocator, nor in unused cache blocks. + raise BlockAllocator.NoFreeBlocksError() + + def _maybe_allocate_hashless_block_id(self) -> Optional[BlockId]: + try: + # Allocate mutable block and extract its block_id + block = self._hashless_allocator.allocate_mutable_block( + prev_block=None) + block_id = block.block_id + self._block_pool.free_block(block) + + self._track_block_id(block_id, computed=False) + return block_id + except BlockAllocator.NoFreeBlocksError: + return None + + def _maybe_allocate_evicted_block_id(self) -> Optional[BlockId]: + if self.evictor.num_blocks == 0: + return None + + # Here we get an evicted block, which is only added + # into evictor if its ref counter is 0 + # and since its content would be changed, we need + # to remove it from _cached_blocks's tracking list + block_id, content_hash_to_evict = self.evictor.evict() + + # Sanity checks + assert content_hash_to_evict in self._cached_blocks + _block_id = self._cached_blocks[content_hash_to_evict] + assert self._refcounter.get(_block_id) == 0 + assert _block_id == block_id + + self._cached_blocks.pop(content_hash_to_evict) + + self._refcounter.incr(block_id) + self._track_block_id(block_id, computed=False) + + return block_id + + def _free_block_id(self, block: Block) -> None: + """Decrements the refcount of the block. The block may be in two + possible states: (1) immutable/cached or (2) mutable/hashless. + In the first case, the refcount is decremented directly and the block + may be possibly added to the evictor. In other case, hashless + allocator free(..) with keep_block_object=True is called to only free + the block id (since the block object may be reused by the caller) + """ + block_id = block.block_id + assert block_id is not None, "Freeing unallocated block is undefined" + + if block.content_hash is not None: + # Immutable: This type of block is always cached, and we want to + # keep it in the evictor for future reuse + self._decr_refcount_cached_block(block) + else: + # Mutable: This type of block is not cached, so we release it + # directly to the hashless allocator + self._decr_refcount_hashless_block(block) + + assert block.block_id is None + + def free(self, block: Block, keep_block_object: bool = False) -> None: + """Release the block (look at free_block_id(..) docs) + """ + # Release the physical block index + self._free_block_id(block) + + # Release the block object to the pool + if not keep_block_object: + self._block_pool.free_block(block) def fork(self, last_block: Block) -> List[Block]: """Creates a new sequence of blocks that shares the same underlying @@ -274,17 +359,20 @@ class PrefixCachingBlockAllocator(BlockAllocator): forked_blocks: List[Block] = [] prev_block = None for block in source_blocks: - refcount = self._refcounter.incr(block.block_id) - assert refcount != 1, "can't fork free'd block" - - forked_blocks.append( - self._create_block( - prev_block=prev_block, - token_ids=block.token_ids, - block_id=block.block_id, - block_size=self._block_size, - allocator=self, - )) + block_id = block.block_id + assert block_id is not None + + refcount = self._refcounter.incr(block_id) + assert refcount != 1, "can't fork free'd block_id = {}".format( + block_id) + + forked_block = self._block_pool.init_block( + prev_block=prev_block, + token_ids=block.token_ids, + block_size=self._block_size, + physical_block_id=block_id) + + forked_blocks.append(forked_block) prev_block = forked_blocks[-1] return forked_blocks @@ -329,7 +417,7 @@ class PrefixCachingBlockAllocator(BlockAllocator): Note that if we already have a cached block with the same content, we will replace the newly-promoted block's mapping with the existing cached - block. + block id. Args: block: The mutable block to be promoted. @@ -338,23 +426,30 @@ class PrefixCachingBlockAllocator(BlockAllocator): BlockId: Either the original block index, or the block index of the previously cached block matching the same content. """ + # Ensure block can be promoted assert block.content_hash is not None assert block.block_id is not None assert self._refcounter.get(block.block_id) > 0 - # If the content hash does not have a corresponding cached block, - # set this block as the cached block. if block.content_hash not in self._cached_blocks: + # No cached content hash => Set this block as cached + # (Note that this block is not computed yet => + # Will be computed after free()) self._cached_blocks[block.content_hash] = block.block_id - else: - self._free_block_id_for_block( - self._cached_blocks[block.content_hash], block) - self._incr_refcount_cached_block( - block, self._cached_blocks[block.content_hash]) + return block.block_id - return self._cached_blocks[block.content_hash] + # Reuse the cached content hash + self._decr_refcount_hashless_block(block) + block.block_id = self._cached_blocks[block.content_hash] - def cow_block_if_not_appendable(self, block: Block) -> Optional[BlockId]: + # Increment refcount of the cached block and (possibly) restore + # it from the evictor. + # Note that in this case, the block is marked as computed + self._incr_refcount_cached_block(block) + + return block.block_id + + def cow_block_if_not_appendable(self, block: Block) -> BlockId: """Performs a copy-on-write operation on the given block if it is not appendable. @@ -362,11 +457,22 @@ class PrefixCachingBlockAllocator(BlockAllocator): block (Block): The block to check for copy-on-write. Returns: - Optional[BlockId]: The block index of the new block if a copy-on - -write operation was performed, or the original block index if + BlockId: The block index of the new block if a copy-on-write + operation was performed, or the original block index if no copy-on-write was necessary. """ - return self._cow_tracker.cow_block_if_not_appendable(block) + src_block_id = block.block_id + assert src_block_id is not None + + if self._cow_tracker.is_appendable(block): + return src_block_id + + self._free_block_id(block) + trg_block_id = self._allocate_block_id() + + self._cow_tracker.record_cow(src_block_id, trg_block_id) + + return trg_block_id def clear_copy_on_writes(self) -> List[Tuple[BlockId, BlockId]]: """Returns the copy-on-write source->destination mapping and clears it. @@ -386,8 +492,8 @@ class PrefixCachingBlockAllocator(BlockAllocator): """ for block_id in block_ids: - if block_id in self._blocks: - self._blocks[block_id].last_accessed = now + if self._block_tracker[block_id].active: + self._block_tracker[block_id].last_accessed = now elif block_id in self.evictor: self.evictor.update(block_id, now) else: @@ -395,25 +501,46 @@ class PrefixCachingBlockAllocator(BlockAllocator): "Mark block as accessed which is not belonged to GPU") def mark_blocks_as_computed(self, block_ids: List[int]) -> None: - """Mark blocks as computed, used in prefix caching.""" + raise NotImplementedError("Marking as computed is incremental") - for block_id in block_ids: - if block_id in self._blocks: - # only those full block is valid for prefix caching - if self._blocks[block_id].is_full: - self._blocks[block_id].computed = True - elif block_id not in self.evictor: - raise ValueError(f"Mark {block_id=} as computed which " - "is not belonged to GPU") + def _track_block_id(self, block_id: Optional[BlockId], + computed: bool) -> None: + assert block_id is not None + self._block_tracker[block_id].enable() + self._block_tracker[block_id].computed = computed + + def _untrack_block_id(self, block_id: Optional[BlockId]) -> None: + assert block_id is not None + self._block_tracker[block_id].disable() def block_is_computed(self, block_id: int) -> bool: - if block_id in self._blocks: - return self._blocks[block_id].computed + if self._block_tracker[block_id].active: + return self._block_tracker[block_id].computed else: return block_id in self.evictor + def get_computed_block_ids(self, + prev_computed_block_ids: List[int], + block_ids: List[int], + skip_last_block_id: bool = True) -> List[int]: + prev_prefix_size = len(prev_computed_block_ids) + cur_size = len(block_ids) + if skip_last_block_id: + cur_size -= 1 + + # Sanity checks + assert cur_size >= 0 + assert prev_prefix_size <= cur_size + + ret = prev_computed_block_ids + for i in range(prev_prefix_size, cur_size): + block_id = block_ids[i] + if self.block_is_computed(block_id): + ret.append(block_id) + return ret + def get_common_computed_block_ids( - self, seq_block_ids: List[List[int]]) -> List[int]: + self, computed_seq_block_ids: List[List[int]]) -> List[int]: """Return the block ids that are common for a given sequence group. Only those blocks that are immutable and already be marked @@ -424,14 +551,9 @@ class PrefixCachingBlockAllocator(BlockAllocator): # prompt is cached. This would cause erroneous behavior in model # runner. - ids_list = [ - list( - takewhile(lambda block_id: self.block_is_computed(block_id), - seq[:-1])) for seq in seq_block_ids - ] # It returns a list of int although type annotation says list of string. return commonprefix([ - ids for ids in ids_list # type: ignore + ids for ids in computed_seq_block_ids # type: ignore if ids != [] ]) @@ -473,10 +595,10 @@ class PrefixCachingBlockAllocator(BlockAllocator): blocks: List of blocks to be swapped out. """ for block in blocks: - self.free(block) + self._free_block_id(block) def swap_in(self, blocks: List[Block]) -> None: - """Execute the swap int actions. Change the block id from + """Execute the swap in actions. Change the block id from old allocator to current allocator for each block to finish the block table update. @@ -484,13 +606,22 @@ class PrefixCachingBlockAllocator(BlockAllocator): blocks: List of blocks to be swapped in. """ for block in blocks: + # Here we allocate either immutable or mutable block and then + # extract its block_id. Note that the block object is released + # and the block_id is assigned to "block" to allow reusing the + # existing "block" object if block.is_full: - alloc = self.allocate_immutable(block.prev_block, - block.token_ids) + tmp_block = self.allocate_immutable_block( + prev_block=block.prev_block, token_ids=block.token_ids) else: - alloc = self.allocate_mutable(block.prev_block) - alloc.append_token_ids(block.token_ids) - block.block_id = alloc.block_id + tmp_block = self.allocate_mutable_block( + prev_block=block.prev_block) + tmp_block.append_token_ids(block.token_ids) + + block_id = tmp_block.block_id + self._block_pool.free_block(tmp_block) + + block.block_id = block_id # Assign block_id class PrefixCachingBlock(Block): @@ -507,7 +638,7 @@ class PrefixCachingBlock(Block): token_ids (List[int]): The initial token IDs to be stored in the block. block_size (int): The maximum number of token IDs that can be stored in the block. - prefix_caching_allocator (BlockAllocator): The prefix + allocator (BlockAllocator): The prefix caching block allocator associated with this block. block_id (Optional[int], optional): The physical block index of this block. Defaults to None. @@ -518,31 +649,55 @@ class PrefixCachingBlock(Block): prev_block: Optional[Block], token_ids: List[int], block_size: int, - prefix_caching_allocator: BlockAllocator, + allocator: BlockAllocator, block_id: Optional[int] = None, computed: bool = False, ): - assert isinstance(prefix_caching_allocator, - PrefixCachingBlockAllocator), ( - "Currently this class is only tested with " - "PrefixCachingBlockAllocator.") + assert isinstance(allocator, PrefixCachingBlockAllocator), ( + "Currently this class is only tested with " + "PrefixCachingBlockAllocator. Got instead allocator = {}".format( + allocator)) assert_prefix_caching_block_or_none(prev_block) self._prev_block = prev_block self._cached_content_hash: Optional[int] = None - self._cached_num_tokens_total: Optional[int] = None - self._prefix_caching_allocator = prefix_caching_allocator + self._cached_num_tokens_total: int = 0 + self._allocator = allocator self._last_accessed: float = _DEFAULT_LAST_ACCESSED_TIME self._computed = computed - self._block = NaiveBlock( - prev_block=prev_block, - token_ids=token_ids, - block_size=block_size, - block_id=block_id, - allocator=prefix_caching_allocator, - _cow_target=self, - ) + # On the first time, we create the block object, and next we only + # reinitialize it + if hasattr(self, "_block"): + self._block.__init__( # type: ignore[has-type] + prev_block=prev_block, + token_ids=token_ids, + block_size=block_size, + block_id=block_id, + allocator=self._allocator) + else: + self._block = NaiveBlock(prev_block=prev_block, + token_ids=token_ids, + block_size=block_size, + block_id=block_id, + allocator=self._allocator) + + self._update_num_tokens_total() + + def _update_num_tokens_total(self): + """Incrementally computes the number of tokens that there is + till the current block (included) + """ + res = 0 + + # Add all previous blocks + if self._prev_block is not None: + res += self._prev_block.num_tokens_total + + # Add current block + res += len(self.token_ids) + + self._cached_num_tokens_total = res @property def computed(self) -> bool: @@ -564,22 +719,28 @@ class PrefixCachingBlock(Block): """Appends the given token IDs to the block and registers the block as immutable if the block becomes full. - Internally, the naive block handles CoW. - Args: token_ids (List[int]): The token IDs to be appended to the block. """ - assert token_ids + # Ensure this is mutable block (not promoted) + assert self.content_hash is None + assert not self.computed + + if len(token_ids) == 0: + return - # naive block handles CoW. + # Ensure there are input tokens + assert token_ids, "Got token_ids = {}".format(token_ids) + + # Naive block handles CoW. self._block.append_token_ids(token_ids) + self._update_num_tokens_total() # If the content hash is present, then the block can be made immutable. # Register ourselves with the allocator, potentially replacing the # physical block index. if self.content_hash is not None: - self.block_id = (self._prefix_caching_allocator. - promote_to_immutable_block(self)) + self.block_id = self._allocator.promote_to_immutable_block(self) @property def block_id(self) -> Optional[int]: @@ -599,23 +760,6 @@ class PrefixCachingBlock(Block): @property def num_tokens_total(self) -> int: - """return the total tokens so far. - - Here we iterate the block chain till to the first block, while - cache the result in local to prevent repeated computations. - """ - if self._cached_num_tokens_total is not None: - return self._cached_num_tokens_total - - _block: Optional[Block] = self - self._cached_num_tokens_total = 0 - - # TODO: current implement here take O(N^2), we expect future - # we have O(1) here - while _block is not None: - self._cached_num_tokens_total += len(_block.token_ids) - _block = _block.prev_block - return self._cached_num_tokens_total @property @@ -638,7 +782,6 @@ class PrefixCachingBlock(Block): For the content-based hash to be defined, the current block must be full. """ - # If the hash is already computed, return it. if self._cached_content_hash is not None: return self._cached_content_hash @@ -688,7 +831,129 @@ class PrefixCachingBlock(Block): return hash((is_first_block, prev_block_hash, *cur_block_token_ids)) +class ComputedBlocksTracker: + """Handles caching of per-sequence computed block ids. + When a sequence appears for the first time, it traverses all of the + blocks and detects the prefix of blocks that is computed. On the + subsequent times, it only traverses the new blocks that were added + and updates the already recorded prefix of blocks with the newly + computed blocks. + + To avoid redundant traversals, the algorithm also detects when there + is a "gap" in the computed prefix. For example, if we have blocks = + [1,2,3,4,5], and we have detected [1,2,3] as the computed prefix, then + we won't try to add more computed blocks to [1,2,3] in this sequence + iteration, and will add more computed blocks only after the sequence is + freed and reused again. + + Note that currently, for a given sequence, we also skip the last + block id for caching purposes, to avoid caching of a full sequence + """ + + def __init__(self, allocator): + self._allocator = allocator + self._cached_computed_seq_blocks: Dict[int, Tuple[List[int], + bool]] = {} + + def add_seq(self, seq_id: int) -> None: + """Start tracking seq_id + """ + assert seq_id not in self._cached_computed_seq_blocks + self._cached_computed_seq_blocks[seq_id] = ([], False) + + def remove_seq(self, seq_id: int) -> None: + """Stop tracking seq_id + """ + assert seq_id in self._cached_computed_seq_blocks + del self._cached_computed_seq_blocks[seq_id] + + def get_cached_computed_blocks_and_update( + self, seq_id: int, block_ids: List[int]) -> List[int]: + """ Look at the class documentation for details + """ + # Ensure seq_id is already tracked + assert seq_id in self._cached_computed_seq_blocks + + # Get cached data (may be empty on the first time) + prev_computed_block_ids, has_gap = self._cached_computed_seq_blocks[ + seq_id] + + if has_gap: + # When gap is detected, we do not add more computed blocks at this + # sequence iteration + return prev_computed_block_ids + + # We do not consider the last block id for caching purposes. + num_cur_blocks = len(block_ids) - 1 + assert num_cur_blocks >= 0 + + if len(prev_computed_block_ids) >= num_cur_blocks: + # Cache HIT + assert len(prev_computed_block_ids) == num_cur_blocks + return prev_computed_block_ids + + # If here, then we may possibly add more computed blocks. As a result, + # traverse the additional blocks after prev_computed_block_ids to + # detect more computed blocks and add them. + + # Incremental init for seq_id => Look only at the new blocks + computed_block_ids = self._allocator.get_computed_block_ids( # noqa: E501 + prev_computed_block_ids, + block_ids, + skip_last_block_id= + True, # We skip last block id to avoid caching of full seq + ) + + # Detect if there is a "gap" + has_gap = len(computed_block_ids) < num_cur_blocks + + # Record + self._cached_computed_seq_blocks[seq_id] = (computed_block_ids, + has_gap) + + return computed_block_ids + + +class LastAccessBlocksTracker: + """Manages the last access time of the tracked sequences, in order to allow + an efficient update of allocator's block last access times + """ + + def __init__(self, allocator): + self._allocator = allocator + self._seq_last_access: Dict[int, Optional[float]] = {} + + def add_seq(self, seq_id: int) -> None: + """Start tracking seq_id + """ + assert seq_id not in self._seq_last_access + self._seq_last_access[seq_id] = None + + def remove_seq(self, seq_id: int) -> None: + """Stop tracking seq_id + """ + assert seq_id in self._seq_last_access + del self._seq_last_access[seq_id] + + def update_last_access(self, seq_id: int, time: float) -> None: + assert seq_id in self._seq_last_access + self._seq_last_access[seq_id] = time + + def update_seq_blocks_last_access(self, seq_id: int, + block_ids: List[int]) -> None: + assert seq_id in self._seq_last_access + + ts = self._seq_last_access[seq_id] + + if ts is None: + # No last access was recorded, no need to update. + return + + self._allocator.mark_blocks_as_accessed(block_ids, ts) + + def assert_prefix_caching_block_or_none(block: Optional[Block]): if block is None: return - assert isinstance(block, PrefixCachingBlock) + assert isinstance(block, + PrefixCachingBlock), "Got block = {}".format(block) diff --git a/vllm/core/block_manager_v2.py b/vllm/core/block_manager_v2.py index 309775237..6a6eebc39 100644 --- a/vllm/core/block_manager_v2.py +++ b/vllm/core/block_manager_v2.py @@ -7,6 +7,8 @@ from typing import Tuple from vllm.core.block.block_table import BlockTable from vllm.core.block.cpu_gpu_block_allocator import CpuGpuBlockAllocator from vllm.core.block.interfaces import Block +from vllm.core.block.prefix_caching_block import (ComputedBlocksTracker, + LastAccessBlocksTracker) from vllm.core.block.utils import check_no_caching_or_swa_for_blockmgr_encdec from vllm.core.interfaces import AllocStatus, BlockSpaceManager from vllm.sequence import Sequence, SequenceGroup, SequenceStatus @@ -100,6 +102,11 @@ class BlockSpaceManagerV2(BlockSpaceManager): self.block_tables: Dict[SeqId, BlockTable] = {} self.cross_block_tables: Dict[EncoderSeqId, BlockTable] = {} + self._computed_blocks_tracker = ComputedBlocksTracker( + self.block_allocator) + self._last_access_blocks_tracker = LastAccessBlocksTracker( + self.block_allocator) + def can_allocate(self, seq_group: SequenceGroup) -> AllocStatus: # FIXME(woosuk): Here we assume that all sequences in the group share # the same prompt. This may not be true for preempted sequences. @@ -157,10 +164,18 @@ class BlockSpaceManagerV2(BlockSpaceManager): block_table: BlockTable = self._allocate_sequence(seq) self.block_tables[seq.seq_id] = block_table + # Track seq + self._computed_blocks_tracker.add_seq(seq.seq_id) + self._last_access_blocks_tracker.add_seq(seq.seq_id) + # Assign the block table for each sequence. for seq in waiting_seqs[1:]: self.block_tables[seq.seq_id] = block_table.fork() + # Track seq + self._computed_blocks_tracker.add_seq(seq.seq_id) + self._last_access_blocks_tracker.add_seq(seq.seq_id) + # Allocate cross-attention block table for encoder sequence # # NOTE: Here we assume that all sequences in the group have the same @@ -224,11 +239,23 @@ class BlockSpaceManagerV2(BlockSpaceManager): return new_cows def free(self, seq: Sequence) -> None: - if seq.seq_id not in self.block_tables: + seq_id = seq.seq_id + + if seq_id not in self.block_tables: # Already freed or haven't been scheduled yet. return - self.block_tables[seq.seq_id].free() - del self.block_tables[seq.seq_id] + + # Update seq block ids with the latest access time + self._last_access_blocks_tracker.update_seq_blocks_last_access( + seq_id, self.block_tables[seq.seq_id].physical_block_ids) + + # Untrack seq + self._last_access_blocks_tracker.remove_seq(seq_id) + self._computed_blocks_tracker.remove_seq(seq_id) + + # Free table/blocks + self.block_tables[seq_id].free() + del self.block_tables[seq_id] def free_cross(self, seq_group: SequenceGroup) -> None: request_id = seq_group.request_id @@ -239,9 +266,7 @@ class BlockSpaceManagerV2(BlockSpaceManager): del self.cross_block_tables[request_id] def get_block_table(self, seq: Sequence) -> List[int]: - assert seq.seq_id in self.block_tables block_ids = self.block_tables[seq.seq_id].physical_block_ids - assert all(b is not None for b in block_ids) return block_ids # type: ignore def get_cross_block_table(self, seq_group: SequenceGroup) -> List[int]: @@ -252,20 +277,14 @@ class BlockSpaceManagerV2(BlockSpaceManager): return block_ids # type: ignore def access_all_blocks_in_seq(self, seq: Sequence, now: float): - # Update the last accessed time of all the blocks accessed - # in this step. - # And the accessed time is only useful for prefix caching now, - # as it support internal evictor policy for which cached - # block could be refilled, to keep cached content could be reused - # at max extend. if self.enable_caching: - block_table = self.block_tables[seq.seq_id] - block_ids: List[Optional[int]] = [] - for block_id in block_table.physical_block_ids: - block_ids.append(block_id) - self.block_allocator.mark_blocks_as_accessed( - block_ids, # type: ignore - now) + # Record the latest access time for the sequence. The actual update + # of the block ids is deferred to the sequence free(..) call, since + # only during freeing of block ids, the blocks are actually added to + # the evictor (which is when the most updated time is required) + # (This avoids expensive calls to mark_blocks_as_accessed(..)) + self._last_access_blocks_tracker.update_last_access( + seq.seq_id, now) def mark_blocks_as_computed(self, seq_group: SequenceGroup): # The only need for mark block as computed is for prefix caching, @@ -285,17 +304,26 @@ class BlockSpaceManagerV2(BlockSpaceManager): This method determines which blocks can be safely skipped for all sequences in the sequence group. """ - seq_block_ids = [ - self.block_tables[seq.seq_id].physical_block_ids for seq in seqs - ] + computed_seq_block_ids = [] + for seq in seqs: + computed_seq_block_ids.append( + self._computed_blocks_tracker. + get_cached_computed_blocks_and_update( + seq.seq_id, + self.block_tables[seq.seq_id].physical_block_ids)) + # NOTE(sang): This assumes seq_block_ids doesn't contain any None. return self.block_allocator.get_common_computed_block_ids( - seq_block_ids) # type: ignore + computed_seq_block_ids) # type: ignore def fork(self, parent_seq: Sequence, child_seq: Sequence) -> None: src_block_table = self.block_tables[parent_seq.seq_id] self.block_tables[child_seq.seq_id] = src_block_table.fork() + # Track child seq + self._computed_blocks_tracker.add_seq(child_seq.seq_id) + self._last_access_blocks_tracker.add_seq(child_seq.seq_id) + def can_swap_in(self, seq_group: SequenceGroup, num_lookahead_slots: int) -> AllocStatus: """Returns the AllocStatus for the given sequence_group @@ -323,19 +351,31 @@ class BlockSpaceManagerV2(BlockSpaceManager): List[Tuple[int, int]]: The mapping of swapping block from CPU to GPU. """ - blocks = self._get_blocks_for_swap(seq_group, SequenceStatus.SWAPPED) - current_swap_mapping = self.block_allocator.swap( - blocks=blocks, source_device=Device.CPU, dest_device=Device.GPU) - - block_number_mapping = { - self.block_allocator.get_physical_block_id(Device.CPU, - cpu_block_id): - self.block_allocator.get_physical_block_id(Device.GPU, - gpu_block_id) - for cpu_block_id, gpu_block_id in current_swap_mapping.items() - } - # convert to list of tuples once here - return list(block_number_mapping.items()) + physical_block_id_mapping = [] + for seq in seq_group.get_seqs(status=SequenceStatus.SWAPPED): + blocks = self.block_tables[seq.seq_id].blocks + if len(blocks) == 0: + continue + + seq_swap_mapping = self.block_allocator.swap(blocks=blocks, + src_device=Device.CPU, + dst_device=Device.GPU) + + # Refresh the block ids of the table (post-swap) + self.block_tables[seq.seq_id].update(blocks) + + seq_physical_block_id_mapping = { + self.block_allocator.get_physical_block_id( + Device.CPU, cpu_block_id): + self.block_allocator.get_physical_block_id( + Device.GPU, gpu_block_id) + for cpu_block_id, gpu_block_id in seq_swap_mapping.items() + } + + physical_block_id_mapping.extend( + list(seq_physical_block_id_mapping.items())) + + return physical_block_id_mapping def can_swap_out(self, seq_group: SequenceGroup) -> bool: """Returns whether we can swap out the given sequence_group @@ -355,7 +395,7 @@ class BlockSpaceManagerV2(BlockSpaceManager): return True return False - def swap_out(self, sequence_group: SequenceGroup) -> List[Tuple[int, int]]: + def swap_out(self, seq_group: SequenceGroup) -> List[Tuple[int, int]]: """Returns the block id mapping (from GPU to CPU) generated by swapping out the given sequence_group with num_lookahead_slots. @@ -366,19 +406,31 @@ class BlockSpaceManagerV2(BlockSpaceManager): List[Tuple[int, int]]: The mapping of swapping block from GPU to CPU. """ - blocks = self._get_blocks_for_swap(sequence_group, - SequenceStatus.RUNNING) - current_swap_mapping = self.block_allocator.swap( - blocks=blocks, source_device=Device.GPU, dest_device=Device.CPU) - block_number_mapping = { - self.block_allocator.get_physical_block_id(Device.GPU, - gpu_block_id): - self.block_allocator.get_physical_block_id(Device.CPU, - cpu_block_id) - for gpu_block_id, cpu_block_id in current_swap_mapping.items() - } - # convert to list of tuples once here - return list(block_number_mapping.items()) + physical_block_id_mapping = [] + for seq in seq_group.get_seqs(status=SequenceStatus.RUNNING): + blocks = self.block_tables[seq.seq_id].blocks + if len(blocks) == 0: + continue + + seq_swap_mapping = self.block_allocator.swap(blocks=blocks, + src_device=Device.GPU, + dst_device=Device.CPU) + + # Refresh the block ids of the table (post-swap) + self.block_tables[seq.seq_id].update(blocks) + + seq_physical_block_id_mapping = { + self.block_allocator.get_physical_block_id( + Device.GPU, gpu_block_id): + self.block_allocator.get_physical_block_id( + Device.CPU, cpu_block_id) + for gpu_block_id, cpu_block_id in seq_swap_mapping.items() + } + + physical_block_id_mapping.extend( + list(seq_physical_block_id_mapping.items())) + + return physical_block_id_mapping def get_num_free_gpu_blocks(self) -> int: return self.block_allocator.get_num_free_blocks(Device.GPU) diff --git a/vllm/engine/llm_engine.py b/vllm/engine/llm_engine.py index 5886ebc24..c13b17471 100644 --- a/vllm/engine/llm_engine.py +++ b/vllm/engine/llm_engine.py @@ -177,7 +177,8 @@ class LLMEngine: "enforce_eager=%s, kv_cache_dtype=%s, " "quantization_param_path=%s, device_config=%s, " "decoding_config=%r, observability_config=%r, " - "seed=%d, served_model_name=%s)", + "seed=%d, served_model_name=%s, use_v2_block_manager=%s, " + "enable_prefix_caching=%s)", VLLM_VERSION, model_config.model, speculative_config, @@ -204,6 +205,8 @@ class LLMEngine: observability_config, model_config.seed, model_config.served_model_name, + scheduler_config.use_v2_block_manager, + cache_config.enable_prefix_caching, ) # TODO(woosuk): Print more configs in debug mode. diff --git a/vllm/entrypoints/openai/serving_completion.py b/vllm/entrypoints/openai/serving_completion.py index 8741893c9..1bd095655 100644 --- a/vllm/entrypoints/openai/serving_completion.py +++ b/vllm/entrypoints/openai/serving_completion.py @@ -345,7 +345,7 @@ class OpenAIServingCompletion(OpenAIServing): out_logprobs = prompt_logprobs output_text = prompt_text elif request.echo and request.max_tokens > 0: - token_ids = prompt_token_ids + output.token_ids + token_ids = prompt_token_ids + list(output.token_ids) out_logprobs = (prompt_logprobs + output.logprobs if request.logprobs is not None else None) output_text = prompt_text + output.text diff --git a/vllm/model_executor/sampling_metadata.py b/vllm/model_executor/sampling_metadata.py index f95de56f3..ad5fb1317 100644 --- a/vllm/model_executor/sampling_metadata.py +++ b/vllm/model_executor/sampling_metadata.py @@ -427,8 +427,8 @@ class SamplingTensors: if seq_group.do_sample: for seq_id in seq_ids: seq_data = seq_group.seq_data[seq_id] - prompt_tokens.append(seq_data.prompt_token_ids) - output_tokens.append(seq_data.output_token_ids) + prompt_tokens.append(list(seq_data.prompt_token_ids)) + output_tokens.append(list(seq_data.output_token_ids)) sampling_tensors = SamplingTensors.from_lists( temperatures, top_ps, top_ks, min_ps, presence_penalties, diff --git a/vllm/outputs.py b/vllm/outputs.py index 49f526b5f..4cb7f06bd 100644 --- a/vllm/outputs.py +++ b/vllm/outputs.py @@ -1,6 +1,6 @@ import time from dataclasses import dataclass -from typing import List, Optional, Union +from typing import List, Optional, Tuple, Union from vllm.lora.request import LoRARequest from vllm.sequence import (PromptLogprobs, RequestMetrics, SampleLogprobs, @@ -28,7 +28,7 @@ class CompletionOutput: index: int text: str - token_ids: List[int] + token_ids: Tuple[int, ...] cumulative_logprob: float logprobs: Optional[SampleLogprobs] finish_reason: Optional[str] = None diff --git a/vllm/sequence.py b/vllm/sequence.py index 22cb26dc0..21c558d44 100644 --- a/vllm/sequence.py +++ b/vllm/sequence.py @@ -116,41 +116,66 @@ class SequenceData: prompt_token_ids: List[int], output_token_ids: Optional[List[int]] = None, ) -> None: - if output_token_ids is None: - output_token_ids = [] + self._prompt_token_ids: List[int] = list(prompt_token_ids) + self._prompt_token_ids_tuple: Tuple[int, ...] = tuple(prompt_token_ids) + self._output_token_ids: List[int] = ( + list(output_token_ids) if output_token_ids is not None else []) - self.prompt_token_ids = prompt_token_ids - self._prompt_token_ids_tuple = tuple(prompt_token_ids) - self.output_token_ids = output_token_ids self.cumulative_logprob = 0.0 # The number of tokens that are computed (that run against the model). self._num_computed_tokens = 0 self._stage: SequenceStage = SequenceStage.PREFILL + self._update_cached_all_tokens() + + def _update_cached_all_tokens(self): + self._cached_all_token_ids: List[int] = (self._prompt_token_ids + + self._output_token_ids) + + @property + def prompt_token_ids(self) -> Tuple[int, ...]: + return self._prompt_token_ids_tuple + + @prompt_token_ids.setter + def prompt_token_ids(self, new_prompt_token_ids) -> None: + self._prompt_token_ids = list(new_prompt_token_ids) + self._prompt_token_ids_tuple = tuple(new_prompt_token_ids) + self._update_cached_all_tokens() + + @property + def output_token_ids(self) -> Tuple[int, ...]: + return tuple(self._output_token_ids) + + @output_token_ids.setter + def output_token_ids(self, new_output_token_ids) -> None: + self._output_token_ids = list(new_output_token_ids) + self._update_cached_all_tokens() + def append_token_id(self, token_id: int, logprob: float) -> None: - self.output_token_ids.append(token_id) + self._output_token_ids.append(token_id) + self._cached_all_token_ids.append(token_id) self.cumulative_logprob += logprob def get_len(self) -> int: - return len(self.output_token_ids) + len(self.prompt_token_ids) + return len(self._output_token_ids) + len(self._prompt_token_ids) def get_prompt_len(self) -> int: - return len(self.prompt_token_ids) + return len(self._prompt_token_ids) def get_output_len(self) -> int: - return len(self.output_token_ids) + return len(self._output_token_ids) def get_token_ids(self) -> List[int]: - return self.prompt_token_ids + self.output_token_ids + return self._cached_all_token_ids def get_prefix_token_ids( self, num_tokens: int ) -> Tuple[Tuple[int, ...], Optional[Tuple[int, ...]]]: """Get prefix tokens, and make the return value hashable""" - prompt_length = len(self.prompt_token_ids) + prompt_length = self.get_prompt_len() if num_tokens > prompt_length: return (self._prompt_token_ids_tuple, - tuple(self.output_token_ids[:num_tokens - prompt_length])) + tuple(self._output_token_ids[:num_tokens - prompt_length])) else: return (self._prompt_token_ids_tuple[:num_tokens], None) @@ -183,14 +208,14 @@ class SequenceData: return self.get_len() - self.get_num_computed_tokens() def get_last_token_id(self) -> int: - if not self.output_token_ids: - return self.prompt_token_ids[-1] - return self.output_token_ids[-1] + if not self._output_token_ids: + return self._prompt_token_ids[-1] + return self._output_token_ids[-1] - def get_prompt_token_ids(self) -> List[int]: + def get_prompt_token_ids(self) -> Tuple[int, ...]: return self.prompt_token_ids - def get_output_token_ids(self) -> List[int]: + def get_output_token_ids(self) -> Tuple[int, ...]: return self.output_token_ids @property @@ -199,8 +224,8 @@ class SequenceData: def __repr__(self) -> str: return (f"SequenceData(" - f"prompt_token_ids={self.prompt_token_ids}, " - f"output_token_ids={self.output_token_ids}, " + f"prompt_token_ids={self._prompt_token_ids}, " + f"output_token_ids={self._output_token_ids}, " f"cumulative_logprob={self.cumulative_logprob})") @@ -306,14 +331,14 @@ class Sequence: def get_token_ids(self) -> List[int]: return self.data.get_token_ids() - def get_prompt_token_ids(self) -> List[int]: + def get_prompt_token_ids(self) -> Tuple[int, ...]: return self.data.get_prompt_token_ids() def get_last_token_id(self) -> int: return self.data.get_last_token_id() - def get_output_token_ids(self) -> List[int]: - return self.data.output_token_ids + def get_output_token_ids(self) -> Tuple[int, ...]: + return self.data.get_output_token_ids() def get_cumulative_logprob(self) -> float: return self.data.cumulative_logprob
[ "CpuGpuBlockAllocator.allocate_mutable_block", "CpuGpuBlockAllocator.allocate_immutable_block", "PrefixCachingBlockAllocator.allocate_mutable_block", "PrefixCachingBlockAllocator.allocate_immutable_block", "PrefixCachingBlock.__init__" ]
[ "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/openai/serving_completion.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-2-7b-hf,dtype=auto --tasks gsm8k --batch_size auto --limit 100
d7740ea4dcee4ab75d7d6eef723f33cae957b288
https://github.com/vllm-project/vllm/pull/4594
2024-05-08
2025-09-07 17:49:00
2025-09-07 17:49:00
[ "meta-llama/Llama-2-7b-hf" ]
python benchmarks/benchmark_throughput.py --model meta-llama/Llama-2-7b-hf --input-len 256 --output-len 256
false
false
true
true
[Core] Optimize sampler get_logprobs (#4594)
[Core] Optimize sampler get_logprobs (#4594)
2024-05-08T08:42:28-07:00
[ "vllm/model_executor/layers/sampler.py" ]
{ "commit_year": 2024, "num_edited_lines": 117, "num_files": 1, "num_hunks": 3, "num_non_test_edited_lines": 117, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/layers/sampler.py b/vllm/model_executor/layers/sampler.py index 1f19d2053..e52e350d2 100644 --- a/vllm/model_executor/layers/sampler.py +++ b/vllm/model_executor/layers/sampler.py @@ -782,13 +782,14 @@ def _get_logprobs( top_logprobs, top_token_ids = torch.topk(logprobs, largest_num_logprobs, dim=-1) - top_logprobs = top_logprobs.cpu() - top_token_ids = top_token_ids.cpu() else: top_logprobs, top_token_ids = None, None - selected_logprobs = selected_logprobs.cpu() - ranks = ranks.cpu() + selected_logprobs = selected_logprobs.to('cpu') + ranks = ranks.to('cpu') + if top_logprobs is not None and top_token_ids is not None: + top_logprobs = top_logprobs.to('cpu') + top_token_ids = top_token_ids.to('cpu') # Find prompt/sample logprobs. prompt_logprobs_per_seq_group: List[Optional[PromptLogprobs]] = [] @@ -828,37 +829,48 @@ def _get_prompt_logprob_if_needed( # Find prompt logprobs prompt_logprobs: Optional[PromptLogprobs] = None - if (is_prompt and sampling_params.prompt_logprobs is not None): + if is_prompt and sampling_params.prompt_logprobs is not None: prompt_logprobs = [] num_logprobs = sampling_params.prompt_logprobs next_prompt_tokens = _get_next_prompt_tokens(seq_group) - for token_id in next_prompt_tokens: + # Pre-select indexes and create a list. It is faster than calling .item + # repetitively. + selected_logprob_items = selected_logprobs[ + selected_logprobs_idx:selected_logprobs_idx + + len(next_prompt_tokens)].tolist() + rank_items = ranks[selected_logprobs_idx:selected_logprobs_idx + + len(next_prompt_tokens)].tolist() + + for idx, token_id in enumerate(next_prompt_tokens): # Calculate the prompt logprob of the real prompt tokens. - # Use tuple here for performance (to use to_list()). # {token_id: (logprob, rank_from_vocab)} prompt_logprobs_dict: Dict[int, Tuple[float, int]] = { - token_id: (selected_logprobs[selected_logprobs_idx].item(), - ranks[selected_logprobs_idx].item()) + token_id: (selected_logprob_items[idx], rank_items[idx]) } # Add top K prompt logprobs along with its rank. if num_logprobs > 0: - prompt_logprobs_dict.update( - zip( - top_token_ids[top_logprob_idx, :num_logprobs].tolist(), - zip( - top_logprobs[ - top_logprob_idx, :num_logprobs].tolist(), - # This is ranks. Since top_logprob is sorted, - # we can just use a range here. - range(1, num_logprobs + 1)))) + top_ids = top_token_ids[ + top_logprob_idx, :num_logprobs].tolist() + top_probs = top_logprobs[ + top_logprob_idx, :num_logprobs].tolist() + # Top K is already sorted by rank, so we can use 1 ~ + # num_logprobs + 1 for rank. + top_ranks = range(1, num_logprobs + 1) + prompt_logprobs_dict.update({ + top_id: (top_prob, rank) + for top_id, top_prob, rank in zip(top_ids, top_probs, + top_ranks) + }) prompt_logprobs.append({ token_id: Logprob(*logprob_and_rank) for token_id, logprob_and_rank in prompt_logprobs_dict.items() }) # + 1 to go to the next prompt token. top_logprob_idx += 1 - selected_logprobs_idx += 1 + + # + len(next_prompt_tokens) to go to the next prompt. + selected_logprobs_idx += len(next_prompt_tokens) return prompt_logprobs, top_logprob_idx, selected_logprobs_idx @@ -874,47 +886,54 @@ def _get_sampled_logprob_if_needed( ): """Compute the sample logprob if needed.""" seq_ids = seq_group.seq_ids - num_logprobs = seq_group.sampling_params.logprobs - if num_logprobs is None: - num_logprobs = 0 + num_logprobs = seq_group.sampling_params.logprobs or 0 sampled_logprobs: SampleLogprobs = [] next_token_ids, parent_seq_ids = sample_result if seq_group.do_sample: assert len(next_token_ids) > 0 - for (next_token_id, parent_id) in zip(next_token_ids, parent_seq_ids): - # Calculate the sample logprob of the real sampled tokens. - # Use tuple here for performance (to use to_list()). - # token_id: (logprob, rank_from_vocab) - sampled_logprobs_dict: Dict[int, Tuple[float, int]] = { - next_token_id: - (selected_logprobs[selected_logprobs_idx].item(), - ranks[selected_logprobs_idx].item()) + # Pre-select items from tensor. tolist() is faster than repetitive + # `.item()` calls. + selected_logprob_items = selected_logprobs[ + selected_logprobs_idx:selected_logprobs_idx + + len(next_token_ids)].tolist() + rank_items = ranks[selected_logprobs_idx:selected_logprobs_idx + + len(next_token_ids)].tolist() + for idx, (next_token_id, + parent_id) in enumerate(zip(next_token_ids, parent_seq_ids)): + # Get the logprob of a sampled token. + sampled_logprobs_dict = { + next_token_id: (selected_logprob_items[idx], rank_items[idx]) } - # +1 to go to the next sampled token. Note that - # selected_logprobs can contain duplicates unlike top_logprobs - # when beam search is enabled. - selected_logprobs_idx += 1 - - # Second, add top K logprobs along with its rank. - if num_logprobs >= 0: - sampled_logprobs_dict.update( - zip( - top_token_ids[top_logprob_idx + - parent_id, :num_logprobs].tolist(), - zip( - top_logprobs[top_logprob_idx + - parent_id, :num_logprobs].tolist(), - # This is rank. Since top_logprob is sorted, we - # can just use a range here. - range(1, num_logprobs + 1)))) + # Get top K logprobs. + if num_logprobs > 0: + top_ids = top_token_ids[top_logprob_idx + + parent_id, :num_logprobs].tolist() + top_probs = top_logprobs[top_logprob_idx + + parent_id, :num_logprobs].tolist() + # Top K is already sorted by rank, so we can use 1 ~ + # num_logprobs + 1 for rank. + top_ranks = range(1, num_logprobs + 1) + sampled_logprobs_dict.update({ + top_id: (top_prob, rank) + for top_id, top_prob, rank in zip(top_ids, top_probs, + top_ranks) + }) + sampled_logprobs.append({ token_id: Logprob(*logprob_and_rank) for token_id, logprob_and_rank in sampled_logprobs_dict.items() }) - # There are len(seq_ids) number of sampled tokens for the current - # sequence group in top_logprobs. Jump to the next seq_group. + + # NOTE: This part of code is not intuitive. `selected_logprobs` include + # logprobs for the current step, which has len(next_token_ids) tokens + # per sequence group. `logprobs` includes logprobs from the previous + # steps, which has len(seq_ids) tokens per sequence group. + + # Iterate to the next sequence group in a batch. + selected_logprobs_idx += len(next_token_ids) + # Iterate to the next sequence group in a batch. top_logprob_idx += len(seq_ids) return sampled_logprobs, top_logprob_idx, selected_logprobs_idx
[ "None" ]
[ "vllm/v1/sample/sampler.py", "vllm/model_executor/layers/sampler.py", "vllm/v1/sample/tpu/sampler.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/llm.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-2-7b-hf,dtype=auto --tasks gsm8k --batch_size auto --limit 100
ad8d696a99ca1eee19f1404e16e8e82df592ff85
https://github.com/vllm-project/vllm/pull/4270
2024-04-22
2025-09-07 17:49:06
2025-09-07 17:49:06
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100
true
false
false
true
[Core] Scheduler perf fix (#4270)
[Core] Scheduler perf fix (#4270)
2024-04-22T21:11:06Z
[ "tests/core/test_scheduler.py", "vllm/core/scheduler.py" ]
{ "commit_year": 2024, "num_edited_lines": 25, "num_files": 2, "num_hunks": 13, "num_non_test_edited_lines": 7, "num_non_test_files": 1, "num_test_files": 1, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/core/test_scheduler.py b/tests/core/test_scheduler.py index 9588a1bea..a25112385 100644 --- a/tests/core/test_scheduler.py +++ b/tests/core/test_scheduler.py @@ -540,7 +540,7 @@ def test_decode_schedule_preempted(): curr_loras = None for i in range(3): _, seq_group = create_dummy_prompt(str(i), prompt_length=60) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) running.append(seq_group) scheduler.block_manager.can_append_slots = MagicMock() @@ -581,7 +581,7 @@ def test_decode_swap_beam_search(): budget = create_token_budget() for i in range(3): _, seq_group = create_dummy_prompt(str(i), prompt_length=60, best_of=2) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) running.append(seq_group) append_new_token_seq_group(60, seq_group, 1) budget.add_num_seqs(seq_group.request_id, @@ -629,7 +629,7 @@ def test_schedule_decode_blocks_to_copy_update(): running = deque() policy = PolicyFactory.get_policy(policy_name="fcfs") curr_loras = None - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) running.append(seq_group) @@ -659,7 +659,7 @@ def test_schedule_swapped_simple(): curr_loras = None blocks_to_swap_out = {} _, seq_group = create_dummy_prompt("1", prompt_length=60, best_of=2) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) scheduler._swap_out(seq_group, blocks_to_swap_out) swapped.append(seq_group) @@ -687,7 +687,7 @@ def test_schedule_swapped_max_token_budget(): blocks_to_swap_out = {} for _ in range(2): _, seq_group = create_dummy_prompt("1", prompt_length=60, best_of=2) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) scheduler._swap_out(seq_group, blocks_to_swap_out) swapped.append(seq_group) @@ -721,7 +721,7 @@ def test_schedule_swapped_max_seqs(): blocks_to_swap_out = {} for i in range(4): _, seq_group = create_dummy_prompt(str(i), prompt_length=60) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) scheduler._swap_out(seq_group, blocks_to_swap_out) swapped.append(seq_group) @@ -759,7 +759,7 @@ def test_schedule_swapped_max_loras(): lora_name=str(i), lora_int_id=i + 1, lora_local_path="abc")) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) scheduler._swap_out(seq_group, blocks_to_swap_out) swapped.append(seq_group) @@ -783,7 +783,7 @@ def test_schedule_swapped_cannot_swap_in(): blocks_to_swap_out = {} for _ in range(2): _, seq_group = create_dummy_prompt("1", prompt_length=60, best_of=2) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) scheduler._swap_out(seq_group, blocks_to_swap_out) swapped.append(seq_group) @@ -808,7 +808,7 @@ def test_schedule_swapped_blocks_to_copy(): policy = PolicyFactory.get_policy(policy_name="fcfs") curr_loras = None _, seq_group = create_dummy_prompt("1", prompt_length=60, best_of=2) - scheduler._allocate_and_set_running(seq_group, 60) + scheduler._allocate_and_set_running(seq_group) append_new_token_seq_group(60, seq_group, 1) blocks_to_swap_out = {} scheduler._swap_out(seq_group, blocks_to_swap_out) diff --git a/vllm/core/scheduler.py b/vllm/core/scheduler.py index 419855062..8d7db09bb 100644 --- a/vllm/core/scheduler.py +++ b/vllm/core/scheduler.py @@ -297,7 +297,6 @@ class Scheduler: def add_seq_group(self, seq_group: SequenceGroup) -> None: # Add sequence groups to the waiting queue. - logger.debug(f"add_seq_group {seq_group.request_id}") self.waiting.append(seq_group) def abort_seq_group(self, request_id: Union[str, Iterable[str]]) -> None: @@ -427,7 +426,6 @@ class Scheduler: swapped_out.append(seq_group) break else: - logger.debug(f"append slot for {seq_group}") self._append_slots(seq_group, blocks_to_copy) is_prefill = seq_group.is_prefill() if is_prefill: @@ -659,7 +657,7 @@ class Scheduler: if curr_loras is not None and lora_int_id > 0: curr_loras.add(lora_int_id) waiting_queue.popleft() - self._allocate_and_set_running(seq_group, num_new_tokens) + self._allocate_and_set_running(seq_group) seq_groups.append( ScheduledSequenceGroup(seq_group=seq_group, token_chunk_size=num_new_tokens)) @@ -952,8 +950,7 @@ class Scheduler: self.running = deque(seq_group for seq_group in self.running if not seq_group.is_finished()) - def _allocate_and_set_running(self, seq_group: SequenceGroup, - num_new_tokens: int) -> None: + def _allocate_and_set_running(self, seq_group: SequenceGroup) -> None: self.block_manager.allocate(seq_group) for seq in seq_group.get_seqs(status=SequenceStatus.WAITING): seq.status = SequenceStatus.RUNNING
[ "vllm.core.scheduler.Scheduler._allocate_and_set_running" ]
[ "vllm/core/scheduler.py", "vllm/v1/core/sched/scheduler.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/llm.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
9474e89ba4ecae253b585eb6b3e1d85f4e108f01
https://github.com/vllm-project/vllm/pull/3357
2024-03-20
2025-09-07 17:49:25
2025-09-07 17:49:25
[ "huggyllama/llama-7b" ]
python benchmarks/benchmark_throughput.py --model huggyllama/llama-7b --dataset-name sharegpt --num-prompts 2000
false
false
true
true
[PREFIX CACHING FOLLOW UP] A bunch of fixes to block allocator performance when automatic prefix caching is disabled (#3357)
[PREFIX CACHING FOLLOW UP] A bunch of fixes to block allocator performance when automatic prefix caching is disabled (#3357)
2024-03-20T00:11:11-07:00
[ "tests/core/test_block_manager.py", "tests/prefix_caching/test_prefix_caching.py", "vllm/core/block_manager.py", "vllm/core/evictor.py" ]
{ "commit_year": 2024, "num_edited_lines": 286, "num_files": 4, "num_hunks": 25, "num_non_test_edited_lines": 260, "num_non_test_files": 2, "num_test_files": 2, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/core/test_block_manager.py b/tests/core/test_block_manager.py index 44ac05a14..9473a33f0 100644 --- a/tests/core/test_block_manager.py +++ b/tests/core/test_block_manager.py @@ -4,7 +4,7 @@ from typing import List from vllm import SamplingParams from vllm.block import PhysicalTokenBlock -from vllm.core.block_manager import (BlockAllocator, BlockSpaceManager, +from vllm.core.block_manager import (UncachedBlockAllocator, BlockSpaceManager, AllocStatus) from vllm.utils import Device from vllm.sequence import Sequence, SequenceGroup, SequenceStatus, Logprob @@ -15,7 +15,8 @@ from .utils import create_dummy_prompt def test_block_allocator_allocate(): block_size = 4 num_cpu_blocks = 4 - cpu_allocator = BlockAllocator(Device.CPU, block_size, num_cpu_blocks) + cpu_allocator = UncachedBlockAllocator(Device.CPU, block_size, + num_cpu_blocks) # Allocate all available cpu blocks. num_free = num_cpu_blocks @@ -24,7 +25,7 @@ def test_block_allocator_allocate(): block = cpu_allocator.allocate() num_free -= 1 - assert block.block_hash not in cpu_allocator.evictor + assert block not in cpu_allocator.free_blocks assert cpu_allocator.get_num_free_blocks() == num_free with pytest.raises(ValueError): @@ -34,14 +35,15 @@ def test_block_allocator_allocate(): def test_block_allocator_free(): block_size = 4 num_cpu_blocks = 4 - cpu_allocator = BlockAllocator(Device.CPU, block_size, num_cpu_blocks) + cpu_allocator = UncachedBlockAllocator(Device.CPU, block_size, + num_cpu_blocks) # Allocate all available cpu blocks. blocks: List[PhysicalTokenBlock] = [] for _ in range(num_cpu_blocks): block = cpu_allocator.allocate() blocks.append(block) - assert block.block_hash not in cpu_allocator.evictor + assert block not in cpu_allocator.free_blocks # Free all allocated cpu blocks. num_free = 0 @@ -49,7 +51,7 @@ def test_block_allocator_free(): for block in blocks: cpu_allocator.free(block) num_free += 1 - assert block.block_hash in cpu_allocator.evictor + assert block in cpu_allocator.free_blocks assert cpu_allocator.get_num_free_blocks() == num_free with pytest.raises(ValueError): diff --git a/tests/prefix_caching/test_prefix_caching.py b/tests/prefix_caching/test_prefix_caching.py index c83551c36..cb61aac39 100644 --- a/tests/prefix_caching/test_prefix_caching.py +++ b/tests/prefix_caching/test_prefix_caching.py @@ -4,7 +4,7 @@ Run `pytest tests/prefix_caching/test_prefix_caching.py`. """ import pytest -from vllm.core.block_manager import BlockAllocator +from vllm.core.block_manager import CachedBlockAllocator from vllm.utils import Device @@ -15,10 +15,7 @@ def test_block_allocator( num_blocks: int, ): block_hash = 1 - block_allocator = BlockAllocator(Device.CPU, - block_size, - num_blocks, - enable_caching=True) + block_allocator = CachedBlockAllocator(Device.CPU, block_size, num_blocks) # Allocate two PysicalTokenBlocks with the same hash and check # that they are the same PhysicalTokenBlock @@ -45,10 +42,7 @@ def test_block_allocator( @pytest.mark.parametrize("num_blocks", [16]) def test_eviction(num_blocks: int, ): block_size = 16 - block_allocator = BlockAllocator(Device.CPU, - block_size, - num_blocks, - enable_caching=True) + block_allocator = CachedBlockAllocator(Device.CPU, block_size, num_blocks) blocks = [] for i in range(num_blocks): diff --git a/vllm/core/block_manager.py b/vllm/core/block_manager.py index 8b089a565..ad9b557fd 100644 --- a/vllm/core/block_manager.py +++ b/vllm/core/block_manager.py @@ -3,6 +3,7 @@ import enum from itertools import count, takewhile from os.path import commonprefix from typing import Dict, List, Optional, Set, Tuple +from abc import ABC, abstractmethod from vllm.block import BlockTable, PhysicalTokenBlock from vllm.sequence import Sequence, SequenceGroup, SequenceStatus @@ -10,7 +11,7 @@ from vllm.utils import Device from vllm.core.evictor import Evictor, EvictionPolicy, make_evictor -class BlockAllocator: +class BlockAllocatorBase(ABC): """Manages free physical token blocks for a device. The allocator maintains a list of free blocks and allocates a block when @@ -18,23 +19,57 @@ class BlockAllocator: the reference count becomes zero, the block is added back to the free list. """ + @abstractmethod def __init__(self, device: Device, block_size: int, num_blocks: int, - eviction_policy: EvictionPolicy = EvictionPolicy.LRU, - enable_caching: bool = False) -> None: + eviction_policy: EvictionPolicy = EvictionPolicy.LRU): + pass + + @abstractmethod + def allocate(self, + block_hash: Optional[int] = None, + num_hashed_tokens: int = 0) -> PhysicalTokenBlock: + pass + + @abstractmethod + def free(self, block: PhysicalTokenBlock) -> None: + pass + + @abstractmethod + def get_num_free_blocks(self) -> int: + pass + + @abstractmethod + def contains_block(self, block_hash: int) -> bool: + pass + + @abstractmethod + def update_hash(self, block_hash: int, block: PhysicalTokenBlock): + pass + + +class CachedBlockAllocator(BlockAllocatorBase): + """Manages free physical token blocks for a device. + + The allocator maintains a list of free blocks and allocates a block when + requested. When a block is freed, its reference count is decremented. If + the reference count becomes zero, the block is added back to the free list. + """ + + def __init__(self, + device: Device, + block_size: int, + num_blocks: int, + eviction_policy: EvictionPolicy = EvictionPolicy.LRU) -> None: self.device = device self.block_size = block_size self.num_blocks = num_blocks - self.enable_caching = enable_caching self.current_num_blocks = 0 self.cached_blocks: Dict[int, PhysicalTokenBlock] = {} - # Switch over to FIFO eviction when caching is disabled - if not self.enable_caching: - eviction_policy = EvictionPolicy.FIFO self.evictor: Evictor = make_evictor(eviction_policy) self.default_hash_ctr = count() @@ -57,13 +92,6 @@ class BlockAllocator: def allocate(self, block_hash: Optional[int] = None, num_hashed_tokens: int = 0) -> PhysicalTokenBlock: - # If caching is disabled, just allocate a new block and return it - if not self.enable_caching: - block = self.allocate_block(next(self.default_hash_ctr), - num_hashed_tokens) - block.ref_count += 1 - return block - if block_hash is None: block_hash = next(self.default_hash_ctr) if block_hash in self.evictor: @@ -90,9 +118,8 @@ class BlockAllocator: assert block.block_hash not in self.evictor self.evictor.add(block) - # If caching is enabled, remove the block from the cached_blocks - if self.enable_caching: - del self.cached_blocks[block.block_hash] + # Remove the block from the cached_blocks + del self.cached_blocks[block.block_hash] def get_num_free_blocks(self) -> int: return (self.num_blocks - self.current_num_blocks + @@ -102,14 +129,68 @@ class BlockAllocator: return block_hash in self.cached_blocks or block_hash in self.evictor def update_hash(self, block_hash: int, block: PhysicalTokenBlock): - # If caching is enabled, update the hash of block and the - # cached_blocks dictionary. - if self.enable_caching: - assert not self.contains_block(block_hash) - old_hash = block.block_hash - block.block_hash = block_hash - del self.cached_blocks[old_hash] - self.cached_blocks[block_hash] = block + # Update the hash of block and the cached_blocks dictionary. + assert not self.contains_block(block_hash) + old_hash = block.block_hash + block.block_hash = block_hash + del self.cached_blocks[old_hash] + self.cached_blocks[block_hash] = block + + +class UncachedBlockAllocator(BlockAllocatorBase): + """Manages free physical token blocks for a device. + + The allocator maintains a list of free blocks and allocates a block when + requested. When a block is freed, its reference count is decremented. If + the reference count becomes zero, the block is added back to the free list. + """ + + def __init__( + self, + device: Device, + block_size: int, + num_blocks: int, + ) -> None: + self.device = device + self.block_size = block_size + self.num_blocks = num_blocks + + # Initialize the free blocks. + self.free_blocks: BlockTable = [] + for i in range(num_blocks): + block = PhysicalTokenBlock(device=device, + block_number=i, + block_size=block_size, + block_hash=-1, + num_hashed_tokens=0) + self.free_blocks.append(block) + + def allocate(self, + block_hash: Optional[int] = None, + num_hashed_tokens: int = 0) -> PhysicalTokenBlock: + if not self.free_blocks: + raise ValueError("Out of memory! No free blocks are available.") + block = self.free_blocks.pop() + block.ref_count = 1 + return block + + def free(self, block: PhysicalTokenBlock) -> None: + if block.ref_count == 0: + raise ValueError(f"Double free! {block} is already freed.") + block.ref_count -= 1 + if block.ref_count == 0: + self.free_blocks.append(block) + + def get_num_free_blocks(self) -> int: + return len(self.free_blocks) + + def contains_block(self, block_hash: int) -> bool: + raise NotImplementedError( + "Invalid codepath for uncached block allocator.") + + def update_hash(self, block_hash: int, block: PhysicalTokenBlock): + raise NotImplementedError( + "Invalid codepath for uncached block allocator.") class AllocStatus(enum.Enum): @@ -142,6 +223,10 @@ class BlockSpaceManager: self.num_total_gpu_blocks = num_gpu_blocks self.num_total_cpu_blocks = num_cpu_blocks + if enable_caching and sliding_window is not None: + raise NotImplementedError( + "Sliding window is not allowed with prefix caching enabled!") + self.block_sliding_window = None if sliding_window is not None: assert sliding_window % block_size == 0, (sliding_window, @@ -154,14 +239,17 @@ class BlockSpaceManager: self.enable_caching = enable_caching self.watermark_blocks = int(watermark * num_gpu_blocks) - self.gpu_allocator = BlockAllocator(Device.GPU, - block_size, - num_gpu_blocks, - enable_caching=enable_caching) - self.cpu_allocator = BlockAllocator(Device.CPU, - block_size, - num_cpu_blocks, - enable_caching=enable_caching) + + if self.enable_caching: + self.gpu_allocator = CachedBlockAllocator(Device.GPU, block_size, + num_gpu_blocks) + self.cpu_allocator = CachedBlockAllocator(Device.CPU, block_size, + num_cpu_blocks) + else: + self.gpu_allocator = UncachedBlockAllocator( + Device.GPU, block_size, num_gpu_blocks) + self.cpu_allocator = UncachedBlockAllocator( + Device.CPU, block_size, num_cpu_blocks) # Mapping: seq_id -> BlockTable. self.block_tables: Dict[int, BlockTable] = {} @@ -198,10 +286,16 @@ class BlockSpaceManager: if (self.block_sliding_window is not None and logical_idx >= self.block_sliding_window): block = block_table[logical_idx % self.block_sliding_window] - else: + # Set the reference counts of the token blocks. + block.ref_count = seq_group.num_seqs() + elif self.enable_caching: block = self.gpu_allocator.allocate( seq.hash_of_block(logical_idx), seq.num_hashed_tokens_of_block(logical_idx)) + else: + block = self.gpu_allocator.allocate() + # Set the reference counts of the token blocks. + block.ref_count = seq_group.num_seqs() block_table.append(block) # Assign the block table for each sequence. @@ -220,8 +314,10 @@ class BlockSpaceManager: seq: Sequence, last_block: PhysicalTokenBlock, ) -> PhysicalTokenBlock: - # Compute a new hash for the block so that it can be shared by - # other Sequences + assert self.enable_caching + + # Compute a new hash for the block so that it can be shared by other + # Sequences new_hash = seq.hash_of_block(len(seq.logical_token_blocks) - 1) # if new_hash is already in the cached table, then free last_block @@ -254,6 +350,8 @@ class BlockSpaceManager: self, seq: Sequence, ) -> PhysicalTokenBlock: + if not self.enable_caching: + return self.gpu_allocator.allocate() block_hash: Optional[int] = None if (self._is_last_block_full(seq)): block_hash = seq.hash_of_block(len(seq.logical_token_blocks) - 1) @@ -293,10 +391,12 @@ class BlockSpaceManager: assert last_block.device == Device.GPU if last_block.ref_count == 1: # Not shared with other sequences. Appendable. - # If the last block is now complete, promote it to a full block so - # that it can be shared - new_block = self._maybe_promote_last_block(seq, last_block) - block_table[-1] = new_block + if self.enable_caching: + # If the last block is now complete, we may reuse an old block + # to save memory. + maybe_new_block = self._maybe_promote_last_block( + seq, last_block) + block_table[-1] = maybe_new_block return None else: # The last block is shared with other sequences. @@ -440,9 +540,12 @@ class BlockSpaceManager: seq: Sequence, access_time: float, ) -> None: - block_table = self.block_tables[seq.seq_id] - for block in block_table: - block.last_accessed = access_time + if self.enable_caching: + # Update the last accessed time of all the blocks accessed + # in this step. + block_table = self.block_tables[seq.seq_id] + for block in block_table: + block.last_accessed = access_time def compute_full_blocks_in_seq(self, seq: Sequence): if seq.seq_id not in self.block_tables: diff --git a/vllm/core/evictor.py b/vllm/core/evictor.py index 1d81f5a97..9f401cba3 100644 --- a/vllm/core/evictor.py +++ b/vllm/core/evictor.py @@ -1,5 +1,5 @@ import enum -from typing import Dict, List, Optional +from typing import Dict from abc import ABC, abstractmethod, abstractproperty from vllm.block import PhysicalTokenBlock @@ -10,7 +10,6 @@ class EvictionPolicy(enum.Enum): Evictor subclass. """ LRU = enum.auto() - FIFO = enum.auto() class Evictor(ABC): @@ -66,37 +65,18 @@ class LRUEvictor(Evictor): # TODO: The performance of this evict function can be optimized further. def evict(self) -> PhysicalTokenBlock: - free_blocks: List[PhysicalTokenBlock] = list(self.free_table.values()) - if len(free_blocks) == 0: + if len(self.free_table) == 0: raise ValueError("No usable cache memory left") + free_blocks = self.free_table.values() - # Find lowest timestamp - lowest_timestamp = free_blocks[0].last_accessed - for block in free_blocks: - if block.last_accessed < lowest_timestamp: - lowest_timestamp = block.last_accessed + # Get evicted block + evicted_block: PhysicalTokenBlock = next(iter(free_blocks)) - # Find all blocks with the lowest timestamp - least_recent: List[PhysicalTokenBlock] = [] for block in free_blocks: - if block.last_accessed == lowest_timestamp: - least_recent.append(block) - - # Find highest prefix count per block - highest_num_hashed_tokens = 0 - for block in least_recent: - if block.num_hashed_tokens > highest_num_hashed_tokens: - highest_num_hashed_tokens = block.num_hashed_tokens - - evicted_block: Optional[PhysicalTokenBlock] = None - - # Find the first block with the lowest timestamp - for block in least_recent: - if block.num_hashed_tokens == highest_num_hashed_tokens: + if (block.last_accessed < evicted_block.last_accessed + or block.last_accessed == evicted_block.last_accessed and + block.num_hashed_tokens > evicted_block.num_hashed_tokens): evicted_block = block - break - - assert evicted_block is not None del self.free_table[evicted_block.block_hash] @@ -119,43 +99,8 @@ class LRUEvictor(Evictor): return len(self.free_table) -class RandomEvictor(Evictor): - """Evicts in a first-in-first-out order""" - - def __init__(self): - self.free_table: Dict[int, PhysicalTokenBlock] = {} - - def __contains__(self, block_hash: int) -> bool: - return block_hash in self.free_table - - def evict(self) -> PhysicalTokenBlock: - if len(self.free_table) == 0: - raise ValueError("No usable cache memory left") - evicted_block = next(iter(self.free_table.values())) - evicted_block.computed = False - del self.free_table[evicted_block.block_hash] - return evicted_block - - def add(self, block: PhysicalTokenBlock): - self.free_table[block.block_hash] = block - - def remove(self, block_hash: int) -> PhysicalTokenBlock: - if block_hash not in self.free_table: - raise ValueError( - "Attempting to remove block that's not in the evictor") - block: PhysicalTokenBlock = self.free_table[block_hash] - del self.free_table[block_hash] - return block - - @property - def num_blocks(self) -> int: - return len(self.free_table) - - def make_evictor(eviction_policy: EvictionPolicy) -> Evictor: if eviction_policy == EvictionPolicy.LRU: return LRUEvictor() - elif eviction_policy == EvictionPolicy.FIFO: - return RandomEvictor() else: raise ValueError(f"Unknown cache eviction policy: {eviction_policy}")
[ "vllm.core.block_manager.UncachedBlockAllocator", "vllm.core.block_manager.CachedBlockAllocator", "vllm.core.block_manager.BlockSpaceManager" ]
[ "vllm/core/block_manager.py", "vllm/core/block/prefix_caching_block.py", "examples/offline_inference/automatic_prefix_caching.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=huggyllama/llama-7b,dtype=auto --trust_remote_code --tasks gsm8k --batch_size auto --limit 100
b690e34824fd5a5c4054a0c0468ebfb6aa1dd215
https://github.com/vllm-project/vllm/pull/21075
2025-08-02
2025-09-07 17:49:40
2025-09-07 17:49:40
[ "ibm-ai-platform/Bamba-9B-v2" ]
python benchmarks/benchmark_serving.py --model ibm-ai-platform/Bamba-9B-v2 --dataset-name sharegpt
true
false
false
true
[Model] Mamba2 preallocate SSM output tensor to avoid d2d copy overhead (#21075)
[Model] Mamba2 preallocate SSM output tensor to avoid d2d copy overhead (#21075)
2025-08-02T01:59:34-07:00
[ "tests/kernels/mamba/test_mamba_ssm.py", "tests/kernels/mamba/test_mamba_ssm_ssd.py", "vllm/model_executor/layers/mamba/mamba_mixer.py", "vllm/model_executor/layers/mamba/mamba_mixer2.py", "vllm/model_executor/layers/mamba/ops/mamba_ssm.py", "vllm/model_executor/layers/mamba/ops/ssd_chunk_scan.py", "vllm/model_executor/layers/mamba/ops/ssd_combined.py", "vllm/model_executor/models/phi4flash.py", "vllm/model_executor/models/plamo2.py" ]
{ "commit_year": 2025, "num_edited_lines": 262, "num_files": 9, "num_hunks": 39, "num_non_test_edited_lines": 165, "num_non_test_files": 7, "num_test_files": 2, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/kernels/mamba/test_mamba_ssm.py b/tests/kernels/mamba/test_mamba_ssm.py index 8dece26dd..4c32ae81b 100644 --- a/tests/kernels/mamba/test_mamba_ssm.py +++ b/tests/kernels/mamba/test_mamba_ssm.py @@ -365,6 +365,7 @@ def test_selective_state_update(dim, dstate, has_z, itype): batch_size = 1 state = torch.randn(batch_size, dim, dstate, dtype=itype, device=device) x = torch.randn(batch_size, dim, device=device, dtype=itype) + out = torch.empty_like(x) dt = torch.randn(batch_size, dim, device=device, dtype=itype) dt_bias = torch.rand(dim, device=device) - 4.0 A = -torch.rand(dim, dstate, device=device) - 1.0 @@ -373,16 +374,17 @@ def test_selective_state_update(dim, dstate, has_z, itype): D = torch.randn(dim, device=device) z = torch.randn_like(x) if has_z else None state_ref = state.detach().clone() - out = selective_state_update(state, - x, - dt, - A, - B, - C, - D=D, - z=z, - dt_bias=dt_bias, - dt_softplus=True) + selective_state_update(state, + x, + dt, + A, + B, + C, + D=D, + z=z, + dt_bias=dt_bias, + dt_softplus=True, + out=out) out_ref = selective_state_update_ref(state_ref, x, dt, @@ -581,6 +583,7 @@ def test_selective_state_update_with_batch_indices(with_padding, dim, dstate, ], dim=0) x = torch.randn(padded_batch_size, dim, device=device, dtype=itype) + out = torch.empty_like(x) dt = torch.randn(padded_batch_size, dim, device=device, dtype=itype) dt_bias = torch.rand(dim, device=device) - 4.0 A = -torch.rand(dim, dstate, device=device) - 1.0 @@ -590,18 +593,19 @@ def test_selective_state_update_with_batch_indices(with_padding, dim, dstate, z = torch.randn_like(x) if has_z else None state_ref = state[state_indices, :].clone() state_before = state.clone() - out = selective_state_update(state, - x, - dt, - A, - B, - C, - D=D, - z=z, - dt_bias=dt_bias, - dt_softplus=True, - state_batch_indices=padded_state_indices, - pad_slot_id=PAD_SLOT_ID) + selective_state_update(state, + x, + dt, + A, + B, + C, + D=D, + z=z, + dt_bias=dt_bias, + dt_softplus=True, + state_batch_indices=padded_state_indices, + pad_slot_id=PAD_SLOT_ID, + out=out) out_ref = selective_state_update_ref(state_ref, x[:batch_size], dt[:batch_size], @@ -665,6 +669,7 @@ def test_selective_state_update_with_heads_with_batch_indices( dtype=torch.int32, device=device) x = torch.randn(batch_size, nheads, headdim, device=device, dtype=itype) + out = torch.empty_like(x) if not tie_hdim: dt = torch.randn(batch_size, nheads, @@ -691,18 +696,19 @@ def test_selective_state_update_with_heads_with_batch_indices( C = torch.randn(batch_size, ngroups, dstate, device=device) z = torch.randn_like(x) if has_z else None state_ref = state[state_indices, :].detach().clone() - out = selective_state_update(state, - x, - dt, - A, - B, - C, - D=D, - z=z, - dt_bias=dt_bias, - dt_softplus=True, - state_batch_indices=state_indices, - pad_slot_id=PAD_SLOT_ID) + selective_state_update(state, + x, + dt, + A, + B, + C, + D=D, + z=z, + dt_bias=dt_bias, + dt_softplus=True, + state_batch_indices=state_indices, + pad_slot_id=PAD_SLOT_ID, + out=out) out_ref = selective_state_update_ref(state_ref, x, dt, diff --git a/tests/kernels/mamba/test_mamba_ssm_ssd.py b/tests/kernels/mamba/test_mamba_ssm_ssd.py index 00c1a2911..67b14a7fa 100644 --- a/tests/kernels/mamba/test_mamba_ssm_ssd.py +++ b/tests/kernels/mamba/test_mamba_ssm_ssd.py @@ -212,15 +212,16 @@ def test_mamba_chunk_scan_single_example(d_head, n_heads, seq_len_chunk_size, Y_min, final_state_min = ssd_minimal_discrete(X * dt.unsqueeze(-1), A * dt, B, C, chunk_size) - - Y, final_state = mamba_chunk_scan_combined(X, - dt, - A, - B, - C, - chunk_size, - D=None, - return_final_states=True) + Y = torch.empty_like(X) + final_state = mamba_chunk_scan_combined(X, + dt, + A, + B, + C, + chunk_size, + D=None, + return_final_states=True, + out=Y) # just test the last in sequence torch.testing.assert_close(Y[:, -1], Y_min[:, -1], atol=atol, rtol=rtol) @@ -292,7 +293,8 @@ def test_mamba_chunk_scan_cont_batch(d_head, n_heads, seq_len_chunk_size_cases, _query_start_loc_to_chunk_indices_offsets( cu_seqlens, chunk_size, cu_seqlens[-1]) - Y, new_states = mamba_chunk_scan_combined( + Y = torch.empty_like(X) + new_states = mamba_chunk_scan_combined( X, dt, A, @@ -306,6 +308,7 @@ def test_mamba_chunk_scan_cont_batch(d_head, n_heads, seq_len_chunk_size_cases, chunk_offsets=chunk_offsets, return_varlen_states=True, initial_states=states, + out=Y, ) # just test the last in sequence diff --git a/vllm/model_executor/layers/mamba/mamba_mixer.py b/vllm/model_executor/layers/mamba/mamba_mixer.py index 796c8d937..60cf3e118 100644 --- a/vllm/model_executor/layers/mamba/mamba_mixer.py +++ b/vllm/model_executor/layers/mamba/mamba_mixer.py @@ -220,7 +220,8 @@ class MambaMixer(CustomOp): has_initial_state=attn_metadata.context_lens_tensor > 0, query_start_loc=attn_metadata.query_start_loc) else: - scan_outputs = selective_state_update( + scan_outputs = torch.empty_like(hidden_states.transpose(0, 1)) + selective_state_update( mamba_cache_params.ssm_state, hidden_states.transpose(0, 1), discrete_time_step.transpose(0, 1), @@ -231,7 +232,8 @@ class MambaMixer(CustomOp): gate.transpose(0, 1), time_proj_bias, dt_softplus=True, - state_batch_indices=mamba_cache_params.state_indices_tensor) + state_batch_indices=mamba_cache_params.state_indices_tensor, + out=scan_outputs) scan_outputs = scan_outputs.transpose(0, 1) # 4. Final linear projection diff --git a/vllm/model_executor/layers/mamba/mamba_mixer2.py b/vllm/model_executor/layers/mamba/mamba_mixer2.py index 36edac237..5ac9a7f9a 100644 --- a/vllm/model_executor/layers/mamba/mamba_mixer2.py +++ b/vllm/model_executor/layers/mamba/mamba_mixer2.py @@ -541,7 +541,6 @@ class MambaMixer2(MambaBase, CustomOp): # NOTE: V0 put prefill before decode, v1 puts decode before prefill # Separate prefill and decode by splitting varlen input # Split along token dimension - # NOTE: V0 put prefill before decode, v1 puts decode before prefill if envs.VLLM_USE_V1: hidden_states_B_C_d, hidden_states_B_C_p = torch.split( hidden_states_B_C[:num_actual_tokens], @@ -583,7 +582,28 @@ class MambaMixer2(MambaBase, CustomOp): 1] if has_prefill else None) - ssd_output_list = [] + # Preallocate output tensor to avoid memcpy cost for merging prefill + # and decode outputs + preallocated_ssm_out = torch.empty( + [ + num_prefill_tokens + num_decodes, + (self.num_heads // self.tp_size) * self.head_dim + ], + dtype=hidden_states.dtype, + device=hidden_states.device, + ) + if envs.VLLM_USE_V1: + preallocated_ssm_out_d, preallocated_ssm_out_p = torch.split( + preallocated_ssm_out, + [num_decodes, num_prefill_tokens], + dim=0, + ) + else: + preallocated_ssm_out_p, preallocated_ssm_out_d = torch.split( + preallocated_ssm_out, + [num_prefill_tokens, num_decodes], + dim=0, + ) # Process prefill requests if has_prefill: @@ -623,7 +643,8 @@ class MambaMixer2(MambaBase, CustomOp): has_initial_states_p[:num_prefills, None, None, None], ssm_state[state_indices_tensor_p], 0) - scan_output, varlen_state = mamba_chunk_scan_combined( + # NOTE: final output is an in-place update of out tensor + varlen_state = mamba_chunk_scan_combined( hidden_states_p.view(1, num_prefill_tokens, self.num_heads // self.tp_size, self.head_dim), @@ -646,15 +667,14 @@ class MambaMixer2(MambaBase, CustomOp): return_final_states=False, dt_softplus=True, dt_limit=(0.0, float("inf")), + out=preallocated_ssm_out_p.view(1, num_prefill_tokens, -1, + self.head_dim), ) # update ssm states # - varlen state is a (num_prefills, nheads, headdim, dstate) tensor ssm_state[state_indices_tensor_p] = varlen_state - # - reshape - ssd_output_list.append(scan_output.view(num_prefill_tokens, -1)) - # Process decode requests if has_decode: # 2. Convolution sequence transformation @@ -684,8 +704,8 @@ class MambaMixer2(MambaBase, CustomOp): # - the hidden is reshaped into (bs, num_heads, head_dim) # - mamba_cache_params.ssm_state's slots will be selected # using state_indices_tensor_d - - hidden_states_d = selective_state_update( + # NOTE: final output is an in-place update of out tensor + selective_state_update( ssm_state, hidden_states_d, dt_d, @@ -697,26 +717,16 @@ class MambaMixer2(MambaBase, CustomOp): dt_bias=dt_bias, dt_softplus=True, state_batch_indices=state_indices_tensor_d, + out=preallocated_ssm_out_d.view(num_decodes, -1, + self.head_dim), ) - if envs.VLLM_USE_V1: - ssd_output_list.insert( - 0, - hidden_states_d.view(-1, (self.num_heads // self.tp_size) * - self.head_dim)) - else: - ssd_output_list.append( - hidden_states_d.view(-1, (self.num_heads // self.tp_size) * - self.head_dim)) - - # Merge prefill and decode outputs before passing to gated MLP - hidden_states = torch.vstack(ssd_output_list) - # 4. gated MLP # GatedRMSNorm internally applying SiLU to the gate # SiLU is applied internally before normalization, unlike standard # norm usage - hidden_states = self.norm(hidden_states, gate[:num_actual_tokens]) + hidden_states = self.norm(preallocated_ssm_out, + gate[:num_actual_tokens]) # 5. Final linear projection output[:num_actual_tokens], _ = self.out_proj(hidden_states) diff --git a/vllm/model_executor/layers/mamba/ops/mamba_ssm.py b/vllm/model_executor/layers/mamba/ops/mamba_ssm.py index 3f67fc35a..838290a9f 100644 --- a/vllm/model_executor/layers/mamba/ops/mamba_ssm.py +++ b/vllm/model_executor/layers/mamba/ops/mamba_ssm.py @@ -205,7 +205,8 @@ def selective_state_update(state, dt_bias=None, dt_softplus=False, state_batch_indices=None, - pad_slot_id=PAD_SLOT_ID): + pad_slot_id=PAD_SLOT_ID, + out=None): """ Argument: state: (batch, dim, dstate) or (batch, nheads, dim, dstate) @@ -223,10 +224,9 @@ def selective_state_update(state, for example: cache_indices = [pad_slot_id, 1, 20, pad_slot_id] in this case, the kernel will not process entries at indices 0 and 3 - Return: - out: (batch, dim) or (batch, nheads, dim) + out: Preallocated ssm output tensor. Assume same shape as x. + In-place updated. """ - has_heads = state.dim() > 3 if state.dim() == 3: state = state.unsqueeze(1) if x.dim() == 2: @@ -245,6 +245,8 @@ def selective_state_update(state, z = z.unsqueeze(1) if dt_bias is not None and dt_bias.dim() == 1: dt_bias = dt_bias.unsqueeze(0) + if out.dim() == 2: + out = out.unsqueeze(1) _, nheads, dim, dstate = state.shape batch = x.shape[0] @@ -264,7 +266,8 @@ def selective_state_update(state, assert dt_bias.shape == (nheads, dim) if state_batch_indices is not None: assert state_batch_indices.shape == (batch, ) - out = torch.empty_like(x) + assert out.shape == x.shape + grid = lambda META: (triton.cdiv(dim, META['BLOCK_SIZE_M']), batch, nheads) z_strides = ((z.stride(0), z.stride(1), z.stride(2)) if z is not None else (0, 0, 0)) @@ -328,9 +331,6 @@ def selective_state_update(state, BLOCK_SIZE_M, num_warps=num_warps, ) - if not has_heads: - out = out.squeeze(1) - return out def selective_scan_fn(u, diff --git a/vllm/model_executor/layers/mamba/ops/ssd_chunk_scan.py b/vllm/model_executor/layers/mamba/ops/ssd_chunk_scan.py index 61eff0c00..fc2b3b25f 100644 --- a/vllm/model_executor/layers/mamba/ops/ssd_chunk_scan.py +++ b/vllm/model_executor/layers/mamba/ops/ssd_chunk_scan.py @@ -454,6 +454,7 @@ def _chunk_scan_fwd( chunk_indices=None, chunk_offsets=None, initial_states=None, + out=None, ): batch, seqlen, nheads, headdim = x.shape _, _, nchunks, chunk_size = dt.shape @@ -483,20 +484,10 @@ def _chunk_scan_fwd( else: chunk_indices, chunk_offsets = None, None - # Allocates output. - out = torch.empty(batch, - seqlen, - nheads, - headdim, - device=x.device, - dtype=x.dtype) + assert out.shape == x.shape + if z is not None: - out_x = torch.empty(batch, - seqlen, - nheads, - headdim, - device=x.device, - dtype=x.dtype) + out_x = torch.empty_like(x) assert out_x.stride() == out.stride() else: out_x = None @@ -579,4 +570,4 @@ def _chunk_scan_fwd( IS_TRITON_22=TRITON_22, HAS_INITSTATES=initial_states is not None, ) - return out, out_x + return out_x diff --git a/vllm/model_executor/layers/mamba/ops/ssd_combined.py b/vllm/model_executor/layers/mamba/ops/ssd_combined.py index b121275e9..ad2853a3d 100644 --- a/vllm/model_executor/layers/mamba/ops/ssd_combined.py +++ b/vllm/model_executor/layers/mamba/ops/ssd_combined.py @@ -36,7 +36,8 @@ def _mamba_chunk_scan_combined_fwd(x, chunk_offsets=None, cu_seqlens=None, dt_softplus=False, - dt_limit=(0.0, float("inf"))): + dt_limit=(0.0, float("inf")), + out=None): batch, seqlen, nheads, headdim = x.shape _, _, ngroups, dstate = B.shape assert nheads % ngroups == 0 @@ -134,7 +135,7 @@ def _mamba_chunk_scan_combined_fwd(x, # - in each (pseudo) chunk, we detect if the previous (pseudo) chunk had # a seq_idx change, in which case we take states information from # init_states. - out, out_x = _chunk_scan_fwd( + out_x = _chunk_scan_fwd( CB, x, dt, @@ -147,9 +148,10 @@ def _mamba_chunk_scan_combined_fwd(x, chunk_indices=chunk_indices, chunk_offsets=chunk_offsets, initial_states=initial_states, + out=out, ) if cu_seqlens is None: - return out, out_x, dt, dA_cumsum, states, final_states + return out_x, dt, dA_cumsum, states, final_states else: assert batch == 1, "passing cu_seqlens to get the varlen states is only supported if batch dimension is 1" varlen_states = chunk_state_varlen( @@ -161,7 +163,7 @@ def _mamba_chunk_scan_combined_fwd(x, states.squeeze(0), initial_states=initial_states, ) - return out, out_x, dt, dA_cumsum, states, final_states, varlen_states + return out_x, dt, dA_cumsum, states, final_states, varlen_states def mamba_chunk_scan_combined(x, @@ -180,6 +182,7 @@ def mamba_chunk_scan_combined(x, cu_seqlens=None, dt_softplus=False, dt_limit=(0.0, float("inf")), + out=None, return_final_states=False, return_varlen_states=False): """ @@ -197,15 +200,14 @@ def mamba_chunk_scan_combined(x, seq_idx: (batch, seqlen) cu_seqlens: (num_sequences + 1) or None, only used if return_varlen_states is True dt_softplus: Whether to apply softplus to dt - Return: - out: (batch, seqlen, nheads, headdim) + out: Preallocated output tensor """ if not return_varlen_states: cu_seqlens = None else: assert cu_seqlens is not None, "cu_seqlens must be provided if return_varlen_states is True" - out, out_x, dt_out, dA_cumsum, states, final_states, *rest = _mamba_chunk_scan_combined_fwd( + out_x, dt_out, dA_cumsum, states, final_states, *rest = _mamba_chunk_scan_combined_fwd( x, dt, A, @@ -221,12 +223,14 @@ def mamba_chunk_scan_combined(x, chunk_offsets=chunk_offsets, cu_seqlens=cu_seqlens, dt_softplus=dt_softplus, - dt_limit=dt_limit) + dt_limit=dt_limit, + out=out) if not return_varlen_states: - return out if not return_final_states else (out, final_states) + if not return_final_states: + return + else: + return final_states else: varlen_states = rest[0] - return (out, - varlen_states) if not return_final_states else (out, - final_states, + return (varlen_states) if not return_final_states else (final_states, varlen_states) diff --git a/vllm/model_executor/models/phi4flash.py b/vllm/model_executor/models/phi4flash.py index a4ded2b7a..1a761d01f 100644 --- a/vllm/model_executor/models/phi4flash.py +++ b/vllm/model_executor/models/phi4flash.py @@ -387,7 +387,8 @@ class Phi4Mamba(nn.Module): has_initial_state=attn_metadata.context_lens_tensor > 0, query_start_loc=attn_metadata.query_start_loc) else: - scan_outputs = selective_state_update( + scan_outputs = torch.empty_like(hidden_states.transpose(0, 1)) + selective_state_update( mamba_cache_params.ssm_state, hidden_states.transpose(0, 1), discrete_time_step.transpose(0, 1), @@ -400,7 +401,8 @@ class Phi4Mamba(nn.Module): None if self.yoco_kv else gate.transpose(0, 1), time_proj_bias, dt_softplus=True, - state_batch_indices=mamba_cache_params.state_indices_tensor) + state_batch_indices=mamba_cache_params.state_indices_tensor, + out=scan_outputs) scan_outputs = scan_outputs.transpose(0, 1) # 4. Final linear projection diff --git a/vllm/model_executor/models/plamo2.py b/vllm/model_executor/models/plamo2.py index 9bc577cfe..8b1df66f0 100644 --- a/vllm/model_executor/models/plamo2.py +++ b/vllm/model_executor/models/plamo2.py @@ -257,7 +257,21 @@ class Plamo2MambaMixer(nn.Module): query_start_loc_p = (attn_metadata.query_start_loc[:num_prefills + 1] if has_prefill else None) - ssd_output_list = [] + # Preallocate output tensor to avoid memcpy cost for merging prefill + # and decode outputs + preallocated_ssm_out = torch.empty( + [ + num_prefill_tokens + num_decodes, + (self.num_heads // self.tp_size) * self.head_dim + ], + dtype=hidden_states.dtype, + device=hidden_states.device, + ) + preallocated_ssm_out_p, preallocated_ssm_out_d = torch.split( + preallocated_ssm_out, + [num_prefill_tokens, num_decodes], + dim=0, + ) # Process prefill requests if has_prefill: @@ -290,7 +304,7 @@ class Plamo2MambaMixer(nn.Module): initial_states = torch.where( mamba2_metadata.has_initial_states[:, None, None, None], mamba_cache_params.ssm_state[state_indices_tensor_p], 0) - scan_output, varlen_state = mamba_chunk_scan_combined( + varlen_state = mamba_chunk_scan_combined( hidden_states_p.view(1, num_prefill_tokens, self.num_heads // self.tp_size, self.head_dim), @@ -312,15 +326,14 @@ class Plamo2MambaMixer(nn.Module): return_final_states=False, dt_softplus=True, dt_limit=(0.0, float("inf")), + out=preallocated_ssm_out_p.view(1, num_prefill_tokens, -1, + self.head_dim), ) # update ssm states # - varlen state is a (batch, nheads, headdim, dstate) tensor mamba_cache_params.ssm_state[state_indices_tensor_p] = varlen_state - # - reshape - ssd_output_list.append(scan_output.view(num_prefill_tokens, -1)) - # Process decode requests if has_decode: # 2. Convolution sequence transformation @@ -349,8 +362,7 @@ class Plamo2MambaMixer(nn.Module): # - the hidden is reshaped into (bs, num_heads, head_dim) # - mamba_cache_params.ssm_state's slots will be selected # using state_indices_tensor_d - - hidden_states_d = selective_state_update( + selective_state_update( mamba_cache_params.ssm_state, hidden_states_d, dt, @@ -362,17 +374,13 @@ class Plamo2MambaMixer(nn.Module): dt_bias=dt_bias, dt_softplus=True, state_batch_indices=state_indices_tensor_d, + out=preallocated_ssm_out_d.view(num_decodes, -1, + self.head_dim), ) assert self.num_heads % self.tp_size == 0 - ssd_output_list.append( - hidden_states_d.view(-1, (self.num_heads // self.tp_size) * - self.head_dim)) - - # Merge prefill and decode outputs before passing to MLP - hidden_states = torch.vstack(ssd_output_list) # 4. Final linear projection - out = self.out_proj(hidden_states) + out = self.out_proj(preallocated_ssm_out) return out
[ "vllm.model_executor.layers.mamba.ops.mamba_ssm.selective_state_update", "vllm.model_executor.layers.mamba.ops.ssd_combined.mamba_chunk_scan_combined" ]
[ "vllm/model_executor/layers/mamba/mamba_mixer2.py", "vllm/model_executor/models/phi4flash.py", "vllm/model_executor/models/plamo2.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=ibm-ai-platform/Bamba-9B-v2,dtype=float16 --tasks gsm8k --batch_size auto --limit 100
58eee5f2e05b74eb2cb1a3bbda9c04df4805e4cc
https://github.com/vllm-project/vllm/pull/20000
2025-08-02
2025-09-07 17:49:44
2025-09-07 17:49:44
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100
true
false
false
true
[PERF] Use faster way of decode in tokenizer: avoid useless list-to-list conversion (#20000)
[PERF] Use faster way of decode in tokenizer: avoid useless list-to-list conversion (#20000)
2025-08-02T01:43:52-07:00
[ "vllm/transformers_utils/tokenizer.py" ]
{ "commit_year": 2025, "num_edited_lines": 7, "num_files": 1, "num_hunks": 1, "num_non_test_edited_lines": 7, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/transformers_utils/tokenizer.py b/vllm/transformers_utils/tokenizer.py index 24ddd35ab..6a31a4198 100644 --- a/vllm/transformers_utils/tokenizer.py +++ b/vllm/transformers_utils/tokenizer.py @@ -50,11 +50,12 @@ def decode_tokens( `skip_special_tokens=None` means to use the backend's default settings. """ + decode_method = getattr(tokenizer, "_decode", tokenizer.decode) if skip_special_tokens is not None: - return tokenizer.decode(token_ids, - skip_special_tokens=skip_special_tokens) + return decode_method(token_ids, + skip_special_tokens=skip_special_tokens) - return tokenizer.decode(token_ids) + return decode_method(token_ids) def encode_tokens(
[ "vllm.transformers_utils.tokenizer.decode_tokens" ]
[ "vllm/transformers_utils/tokenizer.py", "vllm/transformers_utils/tokenizer_base.py", "vllm/transformers_utils/tokenizer_group.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
a32237665df876fcb51196dc209e8aff9fd89d29
https://github.com/vllm-project/vllm/pull/21245
2025-07-22
2025-09-07 17:50:16
2025-09-07 17:50:16
[ "facebook/opt-125m" ]
vllm bench serve --dataset-name random --model facebook/opt-125m --served-model-name facebook/opt-125m --random-input-len 700 --random-output-len 1 --endpoint /v1/completions --ignore-eos --host localhost --port 8000 --request-rate 200 --num-prompts 100
true
false
false
true
[Core] Optimize update checks in LogitsProcessor (#21245)
[Core] Optimize update checks in LogitsProcessor (#21245)
2025-07-22T05:27:18-07:00
[ "vllm/v1/sample/logits_processor.py" ]
{ "commit_year": 2025, "num_edited_lines": 18, "num_files": 1, "num_hunks": 3, "num_non_test_edited_lines": 18, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/v1/sample/logits_processor.py b/vllm/v1/sample/logits_processor.py index 3a4c25964..3a06e7105 100644 --- a/vllm/v1/sample/logits_processor.py +++ b/vllm/v1/sample/logits_processor.py @@ -335,14 +335,19 @@ class LogitBiasLogitsProcessor(LogitsProcessor): if not batch_update: return + needs_update: bool = False # Process added requests. - needs_update = bool(batch_update.added) for index, params, _ in batch_update.added: if isinstance(params, SamplingParams) and (lb := params.logit_bias): self.biases[index] = lb + needs_update = True else: - self.biases.pop(index, None) + # Drop biases metadata at batch index + if self.biases.pop(index, None) is not None: + # If a new request replaces an old request which + # specified biases, we should update processor tensors + needs_update = True if self.biases: # Process removed requests. @@ -419,7 +424,6 @@ class MinTokensLogitsProcessor(LogitsProcessor): if batch_update: # Process added requests. - needs_update |= bool(batch_update.added) for index, params, output_tok_ids in batch_update.added: if (isinstance(params, SamplingParams) and (min_tokens := params.min_tokens) @@ -427,9 +431,13 @@ class MinTokensLogitsProcessor(LogitsProcessor): # Replace request metadata at batch index self.min_toks[index] = (min_tokens, output_tok_ids, params.all_stop_token_ids) + needs_update = True else: - # Drop request metadata at batch index - self.min_toks.pop(index, None) + # Drop min_toks metadata at batch index + if self.min_toks.pop(index, None) is not None: + # If a new request replaces an old request which + # specified min_toks, we should update processor tensors + needs_update = True if self.min_toks: # Process removed requests.
[ "LogitBiasLogitsProcessor.update_state", "MinTokensLogitsProcessor.update_state" ]
[ "vllm/v1/sample/logits_processor.py", "vllm/model_executor/layers/logits_processor.py", "vllm/entrypoints/openai/logits_processors.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=facebook/opt-125m,dtype=auto --tasks gsm8k --batch_size auto --limit 100
e7b204268132cb775c139574c1ff4ad7e15c8f66
https://github.com/vllm-project/vllm/pull/21334
2025-07-22
2025-09-07 17:50:20
2025-09-07 17:50:20
[ "meta-llama/Llama-4-Maverick-17B-128E-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-4-Maverick-17B-128E-Instruct --dataset-name sharegpt --num-prompts 100
true
false
false
true
Revert "[Performance] Performance improvements in non-blockwise fp8 CUTLASS MoE (#20762) (#21334)
Revert "[Performance] Performance improvements in non-blockwise fp8 CUTLASS MoE (#20762) (#21334)
2025-07-21T21:49:01-07:00
[ "benchmarks/kernels/benchmark_grouped_gemm_cutlass.py", "csrc/moe/moe_permute_unpermute_op.cu", "tests/kernels/moe/test_cutlass_moe.py", "tests/kernels/moe/test_pplx_cutlass_moe.py", "vllm/model_executor/layers/fused_moe/cutlass_moe.py", "vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py" ]
{ "commit_year": 2025, "num_edited_lines": 212, "num_files": 6, "num_hunks": 30, "num_non_test_edited_lines": 176, "num_non_test_files": 4, "num_test_files": 2, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/benchmarks/kernels/benchmark_grouped_gemm_cutlass.py b/benchmarks/kernels/benchmark_grouped_gemm_cutlass.py index a6b42406b..1d4e730f9 100644 --- a/benchmarks/kernels/benchmark_grouped_gemm_cutlass.py +++ b/benchmarks/kernels/benchmark_grouped_gemm_cutlass.py @@ -80,11 +80,6 @@ def bench_run( a, score, topk, renormalize=False ) - ab_strides1 = torch.full((num_experts,), k, device="cuda", dtype=torch.int64) - ab_strides2 = torch.full((num_experts,), n, device="cuda", dtype=torch.int64) - c_strides1 = torch.full((num_experts,), 2 * n, device="cuda", dtype=torch.int64) - c_strides2 = torch.full((num_experts,), k, device="cuda", dtype=torch.int64) - def run_triton_moe( a: torch.Tensor, w1: torch.Tensor, @@ -116,10 +111,6 @@ def bench_run( w2: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, - ab_strides1: torch.Tensor, - ab_strides2: torch.Tensor, - c_strides1: torch.Tensor, - c_strides2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, per_act_token: bool, @@ -134,10 +125,6 @@ def bench_run( topk_ids, w1_scale, w2_scale, - ab_strides1, - ab_strides2, - c_strides1, - c_strides2, per_act_token, a1_scale=None, ) @@ -149,10 +136,6 @@ def bench_run( w2_q: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, - ab_strides1: torch.Tensor, - ab_strides2: torch.Tensor, - c_strides1: torch.Tensor, - c_strides2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, ): @@ -167,10 +150,6 @@ def bench_run( topk_ids, w1_scale, w2_scale, - ab_strides1, - ab_strides2, - c_strides1, - c_strides2, per_act_token, a1_scale=None, ) @@ -215,10 +194,6 @@ def bench_run( w2_q, w1_scale, w2_scale, - ab_strides1, - ab_strides2, - c_strides1, - c_strides2, topk_weights, topk_ids, ) @@ -256,10 +231,6 @@ def bench_run( "w1_scale": w1_scale, "w2_scale": w2_scale, "per_act_token": per_act_token, - "ab_strides1": ab_strides1, - "ab_strides2": ab_strides2, - "c_strides1": c_strides1, - "c_strides2": c_strides2, # cuda graph params "cutlass_graph": cutlass_graph, "triton_graph": triton_graph, @@ -318,10 +289,6 @@ def bench_run( w2_q, w1_scale, w2_scale, - ab_strides1, - ab_strides2, - c_strides1, - c_strides2, topk_weights, topk_ids, per_act_token, @@ -330,7 +297,7 @@ def bench_run( results.append( benchmark.Timer( - stmt="run_cutlass_moe(a, a_scale, w1_q, w2_q, w1_scale, w2_scale, ab_strides1, ab_strides2, c_strides1, c_strides2, topk_weights, topk_ids, per_act_token, num_runs)", # noqa: E501 + stmt="run_cutlass_moe(a, a_scale, w1_q, w2_q, w1_scale, w2_scale, topk_weights, topk_ids, per_act_token, num_runs)", # noqa: E501 globals=globals, label=label, sub_label=sub_label, diff --git a/csrc/moe/moe_permute_unpermute_op.cu b/csrc/moe/moe_permute_unpermute_op.cu index 13aecd800..a77471a7f 100644 --- a/csrc/moe/moe_permute_unpermute_op.cu +++ b/csrc/moe/moe_permute_unpermute_op.cu @@ -160,30 +160,6 @@ __global__ void shuffleInputRowsKernel(const T* input, } } -template <typename T> -__global__ void shuffleInputRowsKernelSlow(const T* input, - const int32_t* dst2src_map, - T* output, int64_t num_src_rows, - int64_t num_dst_rows, - int64_t num_cols) { - int64_t dest_row_idx = blockIdx.x; - int64_t const source_row_idx = dst2src_map[dest_row_idx]; - - if (blockIdx.x < num_dst_rows) { - // Duplicate and permute rows - auto const* source_row_ptr = input + source_row_idx * num_cols; - auto* dest_row_ptr = output + dest_row_idx * num_cols; - - int64_t const start_offset = threadIdx.x; - int64_t const stride = blockDim.x; - - for (int elem_index = start_offset; elem_index < num_cols; - elem_index += stride) { - dest_row_ptr[elem_index] = source_row_ptr[elem_index]; - } - } -} - void shuffle_rows(const torch::Tensor& input_tensor, const torch::Tensor& dst2src_map, torch::Tensor& output_tensor) { @@ -197,24 +173,17 @@ void shuffle_rows(const torch::Tensor& input_tensor, int64_t const num_src_rows = input_tensor.size(0); int64_t const num_cols = input_tensor.size(1); - if (num_cols % (128 / sizeof(input_tensor.scalar_type()) / 8)) { - // use slow kernel if num_cols can't be aligned to 128 bits - MOE_DISPATCH(input_tensor.scalar_type(), [&] { - shuffleInputRowsKernelSlow<scalar_t><<<blocks, threads, 0, stream>>>( - reinterpret_cast<scalar_t*>(input_tensor.data_ptr()), - dst2src_map.data_ptr<int32_t>(), - reinterpret_cast<scalar_t*>(output_tensor.data_ptr()), num_src_rows, - num_dest_rows, num_cols); - }); - } else { - MOE_DISPATCH(input_tensor.scalar_type(), [&] { - shuffleInputRowsKernel<scalar_t><<<blocks, threads, 0, stream>>>( - reinterpret_cast<scalar_t*>(input_tensor.data_ptr()), - dst2src_map.data_ptr<int32_t>(), - reinterpret_cast<scalar_t*>(output_tensor.data_ptr()), num_src_rows, - num_dest_rows, num_cols); - }); - } + TORCH_CHECK(!(num_cols % (128 / sizeof(input_tensor.scalar_type()) / 8)), + "num_cols must be divisible by 128 / " + "sizeof(input_tensor.scalar_type()) / 8"); + + MOE_DISPATCH(input_tensor.scalar_type(), [&] { + shuffleInputRowsKernel<scalar_t><<<blocks, threads, 0, stream>>>( + reinterpret_cast<scalar_t*>(input_tensor.data_ptr()), + dst2src_map.data_ptr<int32_t>(), + reinterpret_cast<scalar_t*>(output_tensor.data_ptr()), num_src_rows, + num_dest_rows, num_cols); + }); } #else diff --git a/tests/kernels/moe/test_cutlass_moe.py b/tests/kernels/moe/test_cutlass_moe.py index 37727b75b..81fb3ec1d 100644 --- a/tests/kernels/moe/test_cutlass_moe.py +++ b/tests/kernels/moe/test_cutlass_moe.py @@ -207,10 +207,6 @@ def run_8_bit(moe_tensors: MOETensors8Bit, 'topk_ids': topk_ids, 'w1_scale': moe_tensors.w1_scale, 'w2_scale': moe_tensors.w2_scale, - 'ab_strides1': moe_tensors.ab_strides1, - 'ab_strides2': moe_tensors.ab_strides2, - 'c_strides1': moe_tensors.c_strides1, - 'c_strides2': moe_tensors.c_strides2, 'per_act_token': per_act_token, 'a1_scale': None #moe_tensors.a_scale } @@ -444,11 +440,6 @@ def test_run_cutlass_moe_fp8( expert_map[start:end] = list(range(num_local_experts)) expert_map = torch.tensor(expert_map, dtype=torch.int32, device="cuda") - ab_strides1 = torch.full((e, ), k, device="cuda", dtype=torch.int64) - ab_strides2 = torch.full((e, ), n, device="cuda", dtype=torch.int64) - c_strides1 = torch.full((e, ), 2 * n, device="cuda", dtype=torch.int64) - c_strides2 = torch.full((e, ), k, device="cuda", dtype=torch.int64) - activation = lambda o, i: torch.ops._C.silu_and_mul(o, i) a1q, a1q_scale = moe_kernel_quantize_input(mt.a, mt.a_scale, torch.float8_e4m3fn, @@ -457,9 +448,8 @@ def test_run_cutlass_moe_fp8( func = lambda output: run_cutlass_moe_fp8( output, a1q, mt.w1_q, mt.w2_q, topk_ids, activation, global_num_experts, expert_map, mt.w1_scale, mt.w2_scale, - a1q_scale, None, ab_strides1, ab_strides2, c_strides1, c_strides2, - workspace13, workspace2, None, mt.a.dtype, per_act_token, - per_out_channel, False) + a1q_scale, None, workspace13, workspace2, None, mt.a.dtype, + per_act_token, per_out_channel, False) workspace13.random_() output_random_workspace = torch.empty(output_shape, diff --git a/tests/kernels/moe/test_pplx_cutlass_moe.py b/tests/kernels/moe/test_pplx_cutlass_moe.py index 77adc89ea..e4f4a393d 100644 --- a/tests/kernels/moe/test_pplx_cutlass_moe.py +++ b/tests/kernels/moe/test_pplx_cutlass_moe.py @@ -75,7 +75,6 @@ def pplx_cutlass_moe( assert torch.cuda.current_device() == pgi.local_rank num_tokens, hidden_dim = a.shape - intermediate_dim = w2.shape[2] num_experts = w1.shape[0] block_size = hidden_dim # TODO support more cases device = pgi.device @@ -124,31 +123,10 @@ def pplx_cutlass_moe( num_local_experts=num_local_experts, num_dispatchers=num_dispatchers) - ab_strides1 = torch.full((num_local_experts, ), - hidden_dim, - device="cuda", - dtype=torch.int64) - ab_strides2 = torch.full((num_local_experts, ), - intermediate_dim, - device="cuda", - dtype=torch.int64) - c_strides1 = torch.full((num_local_experts, ), - 2 * intermediate_dim, - device="cuda", - dtype=torch.int64) - c_strides2 = torch.full((num_local_experts, ), - hidden_dim, - device="cuda", - dtype=torch.int64) - experts = CutlassExpertsFp8(num_local_experts, out_dtype, per_act_token, per_out_ch, - ab_strides1, - ab_strides2, - c_strides1, - c_strides2, num_dispatchers=num_dispatchers, use_batched_format=True) diff --git a/vllm/model_executor/layers/fused_moe/cutlass_moe.py b/vllm/model_executor/layers/fused_moe/cutlass_moe.py index ff49d7bb7..2585a2953 100644 --- a/vllm/model_executor/layers/fused_moe/cutlass_moe.py +++ b/vllm/model_executor/layers/fused_moe/cutlass_moe.py @@ -13,7 +13,8 @@ from vllm.model_executor.layers.fused_moe.prepare_finalize import ( MoEPrepareAndFinalizeNoEP) from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceDelegate) -from vllm.model_executor.layers.fused_moe.utils import (_fp8_quantize, +from vllm.model_executor.layers.fused_moe.utils import (_fp8_perm, + _fp8_quantize, _resize_cache, extract_required_args) from vllm.scalar_type import scalar_types @@ -34,10 +35,6 @@ def run_cutlass_moe_fp8( w2_scale: Optional[torch.Tensor], a1q_scale: Optional[torch.Tensor], a2_scale: Optional[torch.Tensor], - ab_strides1: torch.Tensor, - ab_strides2: torch.Tensor, - c_strides1: torch.Tensor, - c_strides2: torch.Tensor, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_num_tokens: Optional[torch.Tensor], @@ -156,11 +153,27 @@ def run_cutlass_moe_fp8( problem_sizes1, problem_sizes2, a_map, c_map, global_num_experts, N, K) - a1q = ops.shuffle_rows(a1q, a_map) - a1q_scale = (ops.shuffle_rows(a1q_scale, a_map) - if per_act_token else a1q_scale) + a1q = _fp8_perm(a1q, a_map) + a1q_scale = a1q_scale[a_map] if per_act_token else a1q_scale expert_offsets = expert_offsets[:-1] + ab_strides1 = torch.full((w1.size(0), ), + K, + device=device, + dtype=torch.int64) + c_strides1 = torch.full((w1.size(0), ), + 2 * N, + device=device, + dtype=torch.int64) + ab_strides2 = torch.full((w1.size(0), ), + N, + device=device, + dtype=torch.int64) + c_strides2 = torch.full((w1.size(0), ), + K, + device=device, + dtype=torch.int64) + if use_batched_format: c1 = _resize_cache(workspace13, (local_E * padded_M, N * 2)) c2 = _resize_cache(workspace2, (local_E * padded_M, N)) @@ -197,8 +210,7 @@ def run_cutlass_moe_fp8( else: # We can't do this inplace because output may point to the same tensor # as c3. - output.copy_(ops.shuffle_rows(c3, c_map).view(M * topk, K), - non_blocking=True) + output.copy_(c3[c_map].view(M * topk, K), non_blocking=True) # TODO (bnell): split class batched vs. non-batched? @@ -211,10 +223,6 @@ class CutlassExpertsFp8(mk.FusedMoEPermuteExpertsUnpermute): out_dtype: Optional[torch.dtype], per_act_token_quant: bool, per_out_ch_quant: bool, - ab_strides1: torch.Tensor, - ab_strides2: torch.Tensor, - c_strides1: torch.Tensor, - c_strides2: torch.Tensor, block_shape: Optional[list[int]] = None, num_dispatchers: Optional[int] = None, use_batched_format: bool = False, @@ -231,10 +239,6 @@ class CutlassExpertsFp8(mk.FusedMoEPermuteExpertsUnpermute): self.max_experts_per_worker = max_experts_per_worker self.num_dispatchers = num_dispatchers self.out_dtype = out_dtype - self.ab_strides1 = ab_strides1 - self.ab_strides2 = ab_strides2 - self.c_strides1 = c_strides1 - self.c_strides2 = c_strides2 self.use_batched_format = use_batched_format @property @@ -314,8 +318,7 @@ class CutlassExpertsFp8(mk.FusedMoEPermuteExpertsUnpermute): run_cutlass_moe_fp8( output, hidden_states, w1, w2, topk_ids, activation_callable, global_num_experts, expert_map, w1_scale, w2_scale, a1q_scale, - a2_scale, self.ab_strides1, self.ab_strides2, self.c_strides1, - self.c_strides2, workspace13, workspace2, expert_num_tokens, + a2_scale, workspace13, workspace2, expert_num_tokens, self.out_dtype if self.out_dtype is not None else in_dtype, self.per_act_token_quant, self.per_out_ch_quant, self.use_batched_format) @@ -329,10 +332,6 @@ def cutlass_moe_fp8( topk_ids: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, - ab_strides1: torch.Tensor, - ab_strides2: torch.Tensor, - c_strides1: torch.Tensor, - c_strides2: torch.Tensor, per_act_token: Optional[bool] = None, activation: str = "silu", a1_scale: Optional[torch.Tensor] = None, @@ -360,17 +359,6 @@ def cutlass_moe_fp8( Shape: [num_experts] or [num_experts, 2N] - w2_scale (torch.Tensor): The fp32 scale to dequantize w2_q. Shape: [num_experts] or [num_experts, K] - - ab_strides1 (torch.Tensor): The input/weight strides for the first gemm. - Shape: [num_experts] - - ab_strides2 (torch.Tensor): The input/weight strides for the second gemm. - Shape: [num_experts] - - c_strides1 (torch.Tensor): The output strides for the first gemm. - Shape: [num_experts] - - c_strides2 (torch.Tensor): The output strides for the second gemm. - Shape: [num_experts] - - per_act_token (Optional[bool]): Whether the scale is per-token or - per-tensor. - - activation (str): The activation function to use. - a1_scale (Optional[torch.Tensor]): The optional fp32 scale to quantize a. Shape: scalar or [M] - a2_scale (Optional[torch.Tensor]): The optional fp32 scale to @@ -403,10 +391,6 @@ def cutlass_moe_fp8( out_dtype=a.dtype, per_act_token_quant=per_act_token, per_out_ch_quant=per_out_ch, - ab_strides1=ab_strides1, - ab_strides2=ab_strides2, - c_strides1=c_strides1, - c_strides2=c_strides2, use_batched_format=False, ), ) diff --git a/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py b/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py index 1a31410c3..2c93977be 100644 --- a/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py +++ b/vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py @@ -859,21 +859,6 @@ class CompressedTensorsW8A8Fp8MoECutlassMethod(CompressedTensorsMoEMethod): layer.w13_weight_scale = torch.nn.Parameter(max_w13_scales, requires_grad=False) - device = layer.w13_weight.device - # ab_strides1 and c_strides2 are the same - self.ab_strides1_c_strides2 = torch.full((layer.local_num_experts, ), - layer.hidden_size, - device=device, - dtype=torch.int64) - self.ab_strides2 = torch.full((layer.local_num_experts, ), - layer.intermediate_size_per_partition, - device=device, - dtype=torch.int64) - self.c_strides1 = torch.full((layer.local_num_experts, ), - 2 * layer.intermediate_size_per_partition, - device=device, - dtype=torch.int64) - def select_gemm_impl( self, prepare_finalize: FusedMoEPrepareAndFinalize, @@ -896,10 +881,6 @@ class CompressedTensorsW8A8Fp8MoECutlassMethod(CompressedTensorsMoEMethod): moe.in_dtype, self.input_quant.strategy == QuantizationStrategy.TOKEN, self.weight_quant.strategy == QuantizationStrategy.CHANNEL, - ab_strides1=self.ab_strides1_c_strides2, - ab_strides2=self.ab_strides2, - c_strides1=self.c_strides1, - c_strides2=self.ab_strides1_c_strides2, num_dispatchers=num_dispatchers, use_batched_format=use_batched_format, ) @@ -946,8 +927,7 @@ class CompressedTensorsW8A8Fp8MoECutlassMethod(CompressedTensorsMoEMethod): num_expert_group=num_expert_group, custom_routing_function=custom_routing_function, scoring_func=scoring_func, - e_score_correction_bias=e_score_correction_bias, - indices_type=self.topk_indices_dtype) + e_score_correction_bias=e_score_correction_bias) per_act_token = ( self.input_quant.strategy == QuantizationStrategy.TOKEN) @@ -968,10 +948,6 @@ class CompressedTensorsW8A8Fp8MoECutlassMethod(CompressedTensorsMoEMethod): expert_map=None if self.disable_expert_map else expert_map, w1_scale=layer.w13_weight_scale, w2_scale=layer.w2_weight_scale, - ab_strides1=self.ab_strides1_c_strides2, - ab_strides2=self.ab_strides2, - c_strides1=self.c_strides1, - c_strides2=self.ab_strides1_c_strides2, a1_scale=layer.w13_input_scale, a2_scale=layer.w2_input_scale, )
[ "vllm.cutlass_moe_fp8", "CutlassExpertsFp8.apply", "CompressedTensorsW8A8Fp8MoECutlassMethod.select_gemm_impl" ]
[ "vllm/model_executor/layers/fused_moe/cutlass_moe.py", "vllm/model_executor/layers/quantization/compressed_tensors/compressed_tensors_moe.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-4-Maverick-17B-128E-Instruct,dtype=auto --trust_remote_code --tasks gsm8k --batch_size auto --limit 100
015069b01741e9ecb9e604c7fe87fbdfc306ebe5
https://github.com/vllm-project/vllm/pull/17515
2025-05-01
2025-09-07 17:51:10
2025-09-07 17:51:10
[ "Qwen/Qwen3-7B-Instruct" ]
python benchmarks/benchmark_serving.py --model Qwen/Qwen3-7B-Instruct --dataset-name sharegpt --request-rate 1
true
false
false
true
[Misc] Optimize the Qwen3_ReasoningParser extract_reasoning_content (#17515)
[Misc] Optimize the Qwen3_ReasoningParser extract_reasoning_content (#17515)
2025-05-01T03:29:01-07:00
[ "vllm/reasoning/qwen3_reasoning_parser.py" ]
{ "commit_year": 2025, "num_edited_lines": 53, "num_files": 1, "num_hunks": 3, "num_non_test_edited_lines": 53, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/reasoning/qwen3_reasoning_parser.py b/vllm/reasoning/qwen3_reasoning_parser.py index f588f4016..7095034b1 100644 --- a/vllm/reasoning/qwen3_reasoning_parser.py +++ b/vllm/reasoning/qwen3_reasoning_parser.py @@ -1,6 +1,5 @@ # SPDX-License-Identifier: Apache-2.0 -import re from collections.abc import Sequence from typing import Optional, Union @@ -31,9 +30,6 @@ class Qwen3ReasoningParser(ReasoningParser): self.think_start_token = "<think>" self.think_end_token = "</think>" - self.reasoning_regex = re.compile( - rf"{self.think_start_token}(.*?){self.think_end_token}", re.DOTALL) - if not self.model_tokenizer: raise ValueError( "The model tokenizer must be passed to the ReasoningParser " @@ -121,29 +117,34 @@ class Qwen3ReasoningParser(ReasoningParser): def extract_reasoning_content( self, model_output: str, request: ChatCompletionRequest ) -> tuple[Optional[str], Optional[str]]: + """ + Extract reasoning content from the model output. + + For text <think>abc</think>xyz: + - 'abc' goes to reasoning_content + - 'xyz' goes to content - # Check if the model output contains the <think> tokens. + Returns: + tuple[Optional[str], Optional[str]]: reasoning content and content + """ + + # Check if the model output contains the <think> and </think> tokens. if (self.think_start_token not in model_output or self.think_end_token not in model_output): return None, model_output - else: - # Use a regex to find the reasoning content - reasoning_content = self.reasoning_regex.findall(model_output)[0] - - # Remove the reasoning content from the model output - # Although <think> token is always at the - # beginning of the line, we cannot guarantee that the - # other models will follow this convention. - # Therefore, we need to add :start_index. - start_index = model_output.find(self.think_start_token) - if start_index != -1: - end_index = start_index + len( - f"{self.think_start_token}{reasoning_content}{self.think_end_token}" - ) - model_output = model_output[:start_index] + \ - model_output[end_index:] - - if len(model_output) == 0: - return reasoning_content, None - - return reasoning_content, model_output + # Check if the <think> is present in the model output, remove it + # if it is present. + model_output_parts = model_output.partition(self.think_start_token) + model_output = model_output_parts[2] if model_output_parts[ + 1] else model_output_parts[0] + # Check if the model output contains the </think> tokens. + # If the end token is not found, return the model output as is. + if self.think_end_token not in model_output: + return None, model_output + + # Extract reasoning content from the model output. + reasoning_content, _, content = model_output.partition( + self.think_end_token) + + final_content = content or None + return reasoning_content, final_content
[ "Qwen3ReasoningParser.extract_reasoning_content" ]
[ "vllm/reasoning/qwen3_reasoning_parser.py", "examples/online_serving/openai_chat_completion_with_reasoning.py", "examples/online_serving/openai_chat_completion_with_reasoning_streaming.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=Qwen/Qwen3-7B-Instruct,dtype=auto --trust_remote_code --tasks gsm8k --batch_size auto --limit 100
bc7c4d206bbfb56b06d218b6c2971e8ca191db36
https://github.com/vllm-project/vllm/pull/13305
2025-04-23
2025-09-07 17:51:14
2025-09-07 17:51:14
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --dtype float16 --num-prompts 300 --seed 0
true
false
false
true
[Kernel][ROCM] Upstream prefix prefill speed up for vLLM V1 (#13305)
[Kernel][ROCM] Upstream prefix prefill speed up for vLLM V1 (#13305)
2025-04-22T19:11:56-07:00
[ "tests/core/block/e2e/test_correctness.py", "vllm/attention/ops/prefix_prefill.py" ]
{ "commit_year": 2025, "num_edited_lines": 1640, "num_files": 2, "num_hunks": 4, "num_non_test_edited_lines": 1634, "num_non_test_files": 1, "num_test_files": 1, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/core/block/e2e/test_correctness.py b/tests/core/block/e2e/test_correctness.py index e9b537ed5..9e8e315d8 100644 --- a/tests/core/block/e2e/test_correctness.py +++ b/tests/core/block/e2e/test_correctness.py @@ -195,15 +195,15 @@ def test_lookahead_greedy_equality_with_preemption(baseline_llm_generator, ]) @pytest.mark.parametrize("per_test_common_llm_kwargs", [{ - "block_size": 8, + "block_size": 16, "max_num_batched_tokens": 2, "max_num_seqs": 2, }, { - "block_size": 8, + "block_size": 16, "max_num_batched_tokens": 3, "max_num_seqs": 2, }, { - "block_size": 8, + "block_size": 16, "max_num_batched_tokens": 256, "max_num_seqs": 10, }]) diff --git a/vllm/attention/ops/prefix_prefill.py b/vllm/attention/ops/prefix_prefill.py index e0478c2ae..a8c8d8409 100644 --- a/vllm/attention/ops/prefix_prefill.py +++ b/vllm/attention/ops/prefix_prefill.py @@ -16,831 +16,778 @@ NUM_WARPS = 4 if current_platform.is_rocm() else 8 # To check compatibility IS_TURING = current_platform.get_device_capability() == (7, 5) -if triton.__version__ >= "2.1.0": - - @triton.jit - def _fwd_kernel( - Q, - K, - V, - K_cache, - V_cache, - B_Loc, - sm_scale, - k_scale, - v_scale, - B_Start_Loc, - B_Seqlen, - block_size, - x, - Out, - stride_b_loc_b, - stride_b_loc_s, - stride_qbs, - stride_qh, - stride_qd, - stride_kbs, - stride_kh, - stride_kd, - stride_vbs, - stride_vh, - stride_vd, - stride_obs, - stride_oh, - stride_od, - stride_k_cache_bs, - stride_k_cache_h, - stride_k_cache_d, - stride_k_cache_bl, - stride_k_cache_x, - stride_v_cache_bs, - stride_v_cache_h, - stride_v_cache_d, - stride_v_cache_bl, - num_queries_per_kv: int, - IN_PRECISION: tl.constexpr, - BLOCK_M: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, # head size - BLOCK_DMODEL_PADDED: tl.constexpr, # head size padded to a power of 2 - BLOCK_N: tl.constexpr, - SLIDING_WINDOW: tl.constexpr, - SKIP_DECODE: tl.constexpr, - ): - - cur_batch = tl.program_id(0) - cur_head = tl.program_id(1) - start_m = tl.program_id(2) - - cur_kv_head = cur_head // num_queries_per_kv - - cur_batch_seq_len = tl.load(B_Seqlen + cur_batch) - cur_batch_in_all_start_index = tl.load(B_Start_Loc + cur_batch) - cur_batch_in_all_stop_index = tl.load(B_Start_Loc + cur_batch + 1) - cur_batch_query_len = (cur_batch_in_all_stop_index - - cur_batch_in_all_start_index) - cur_batch_ctx_len = cur_batch_seq_len - cur_batch_query_len - - if SKIP_DECODE and cur_batch_query_len == 1: - return - - # start position inside of the query - # generally, N goes over kv, while M goes over query_len - block_start_loc = BLOCK_M * start_m - - # initialize offsets - # [N]; starts at 0 - offs_n = tl.arange(0, BLOCK_N) - # [D]; starts at 0 - offs_d = tl.arange(0, BLOCK_DMODEL_PADDED) - # [M]; starts at current position in query - offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) - # [M,D] - off_q = ( - (cur_batch_in_all_start_index + offs_m[:, None]) * stride_qbs + - cur_head * stride_qh + offs_d[None, :] * stride_qd) - - dim_mask = tl.where( - tl.arange(0, BLOCK_DMODEL_PADDED) < BLOCK_DMODEL, 1, - 0).to(tl.int1) # [D] - - q = tl.load(Q + off_q, - mask=dim_mask[None, :] & - (offs_m[:, None] < cur_batch_query_len), - other=0.0) # [M,D] - - # initialize pointer to m and l - m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") # [M] - l_i = tl.zeros([BLOCK_M], dtype=tl.float32) # [M] - acc = tl.zeros([BLOCK_M, BLOCK_DMODEL_PADDED], - dtype=tl.float32) # [M,D] - - # compute query against context (no causal mask here) - for start_n in range(0, cur_batch_ctx_len, BLOCK_N): - start_n = tl.multiple_of(start_n, BLOCK_N) - # -- compute qk ---- - bn = tl.load(B_Loc + cur_batch * stride_b_loc_b + - ((start_n + offs_n) // block_size) * stride_b_loc_s, - mask=(start_n + offs_n) < cur_batch_ctx_len, - other=0) # [N] - # [D,N] - off_k = (bn[None, :] * stride_k_cache_bs + - cur_kv_head * stride_k_cache_h + - (offs_d[:, None] // x) * stride_k_cache_d + - ((start_n + offs_n[None, :]) % block_size) * - stride_k_cache_bl + - (offs_d[:, None] % x) * stride_k_cache_x) - # [N,D] - off_v = ( - bn[:, None] * stride_v_cache_bs + - cur_kv_head * stride_v_cache_h + - offs_d[None, :] * stride_v_cache_d + - (start_n + offs_n[:, None]) % block_size * stride_v_cache_bl) - k_load = tl.load(K_cache + off_k, - mask=dim_mask[:, None] & - ((start_n + offs_n[None, :]) < cur_batch_ctx_len), - other=0.0) # [D,N] - - if k_load.dtype.is_fp8(): - k = (k_load.to(tl.float32) * tl.load(k_scale)).to(q.dtype) - else: - k = k_load - - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) # [M,N] - qk = tl.dot(q, k, acc=qk, input_precision=IN_PRECISION) - qk = tl.where((start_n + offs_n[None, :]) < cur_batch_ctx_len, qk, - float("-inf")) - qk *= sm_scale - if SLIDING_WINDOW > 0: - # (cur_batch_ctx_len + offs_m[:, None]) are the positions of - # Q entries in sequence - # (start_n + offs_n[None, :]) are the positions of - # KV entries in sequence - # So the condition makes sure each entry in Q only attends - # to KV entries not more than SLIDING_WINDOW away. - # - # We can't use -inf here, because the - # sliding window may lead to the entire row being masked. - # This then makes m_ij contain -inf, which causes NaNs in - # exp(). - qk = tl.where((cur_batch_ctx_len + offs_m[:, None]) - - (start_n + offs_n[None, :]) < SLIDING_WINDOW, qk, - -10000) - - # -- compute m_ij, p, l_ij - m_ij = tl.max(qk, 1) # [M] - p = tl.exp(qk - m_ij[:, None]) # [M,N] - l_ij = tl.sum(p, 1) # [M] - # -- update m_i and l_i - m_i_new = tl.maximum(m_i, m_ij) # [M] - alpha = tl.exp(m_i - m_i_new) # [M] - beta = tl.exp(m_ij - m_i_new) # [M] - l_i_new = alpha * l_i + beta * l_ij # [M] - - # -- update output accumulator -- - # scale p - p_scale = beta / l_i_new - p = p * p_scale[:, None] - # scale acc - acc_scale = l_i / l_i_new * alpha - acc = acc * acc_scale[:, None] - # update acc - v_load = tl.load(V_cache + off_v, - mask=dim_mask[None, :] & - ((start_n + offs_n[:, None]) < cur_batch_ctx_len), - other=0.0) # [N,D] - if v_load.dtype.is_fp8(): - v = (v_load.to(tl.float32) * tl.load(v_scale)).to(q.dtype) - else: - v = v_load - p = p.to(v.dtype) - - acc = tl.dot(p, v, acc=acc, input_precision=IN_PRECISION) - # # update m_i and l_i - l_i = l_i_new - m_i = m_i_new - - off_k = (offs_n[None, :] * stride_kbs + cur_kv_head * stride_kh + - offs_d[:, None] * stride_kd) - off_v = (offs_n[:, None] * stride_vbs + cur_kv_head * stride_vh + - offs_d[None, :] * stride_vd) - k_ptrs = K + off_k - v_ptrs = V + off_v - - # block_mask is 0 when we're already past the current query length - block_mask = tl.where(block_start_loc < cur_batch_query_len, 1, 0) - - # compute query against itself (with causal mask) - for start_n in range(0, block_mask * (start_m + 1) * BLOCK_M, BLOCK_N): - start_n = tl.multiple_of(start_n, BLOCK_N) - # -- compute qk ---- - k = tl.load(k_ptrs + - (cur_batch_in_all_start_index + start_n) * stride_kbs, - mask=dim_mask[:, None] & - ((start_n + offs_n[None, :]) < cur_batch_query_len), - other=0.0) - - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - qk = tl.dot(q, k, acc=qk, input_precision=IN_PRECISION) - qk *= sm_scale - # apply causal mask - qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, - float("-inf")) - if SLIDING_WINDOW > 0: - qk = tl.where( - offs_m[:, None] - (start_n + offs_n[None, :]) - < SLIDING_WINDOW, qk, -10000) - - # -- compute m_ij, p, l_ij - m_ij = tl.max(qk, 1) - p = tl.exp(qk - m_ij[:, None]) - l_ij = tl.sum(p, 1) - # -- update m_i and l_i - m_i_new = tl.maximum(m_i, m_ij) - alpha = tl.exp(m_i - m_i_new) - beta = tl.exp(m_ij - m_i_new) - l_i_new = alpha * l_i + beta * l_ij - # -- update output accumulator -- - # scale p - p_scale = beta / l_i_new - p = p * p_scale[:, None] - # scale acc - acc_scale = l_i / l_i_new * alpha - acc = acc * acc_scale[:, None] - # update acc - v = tl.load(v_ptrs + - (cur_batch_in_all_start_index + start_n) * stride_vbs, - mask=dim_mask[None, :] & - ((start_n + offs_n[:, None]) < cur_batch_query_len), - other=0.0) - p = p.to(v.dtype) - - acc = tl.dot(p, v, acc=acc, input_precision=IN_PRECISION) - # update m_i and l_i - l_i = l_i_new - m_i = m_i_new - # initialize pointers to output - off_o = ( - (cur_batch_in_all_start_index + offs_m[:, None]) * stride_obs + - cur_head * stride_oh + offs_d[None, :] * stride_od) - out_ptrs = Out + off_o - tl.store(out_ptrs, - acc, - mask=dim_mask[None, :] & - (offs_m[:, None] < cur_batch_query_len)) + +# Here's an example autotuner config for this kernel. This config does provide +# a performance improvement, but dramatically increases first call latency in +# triton 3.2. Because of this tradeoff, it's currently commented out. +# @triton.autotune( +# configs=[ +# triton.Config({'BLOCK_M': 128, 'BLOCK_N': 64, \ +# "num_unroll_cache": 4, \ +# "num_unroll_request": 1 } | \ +# ({"kpack": 2, "waves_per_eu": 2} \ +# if current_platform.is_rocm() else {}), \ +# num_warps=4, \ +# num_stages=1) +# ], +# key=["BLOCK_SIZE", "MAX_Q_LEN", "MAX_CTX_LEN"] +# ) +@triton.jit +def _fwd_kernel(Q, + K, + V, + K_cache, + V_cache, + B_Loc, + sm_scale, + k_scale, + v_scale, + B_Start_Loc, + B_Seqlen, + x: tl.constexpr, + Out, + stride_b_loc_b, + stride_b_loc_s, + stride_qbs, + stride_qh, + stride_qd, + stride_kbs, + stride_kh, + stride_kd, + stride_vbs, + stride_vh, + stride_vd, + stride_obs, + stride_oh, + stride_od, + stride_k_cache_bs, + stride_k_cache_h, + stride_k_cache_d, + stride_k_cache_bl: tl.constexpr, + stride_k_cache_x, + stride_v_cache_bs, + stride_v_cache_h, + stride_v_cache_d, + stride_v_cache_bl, + num_queries_per_kv: tl.constexpr, + IN_PRECISION: tl.constexpr, + BLOCK_M: tl.constexpr, + BLOCK_DMODEL: tl.constexpr, + BLOCK_DMODEL_PADDED: tl.constexpr, + BLOCK_SIZE: tl.constexpr, + BLOCK_N: tl.constexpr, + SLIDING_WINDOW: tl.constexpr, + num_unroll_cache: tl.constexpr, + num_unroll_request: tl.constexpr, + SKIP_DECODE: tl.constexpr, + MAX_Q_LEN: tl.constexpr = 0, + MAX_CTX_LEN: tl.constexpr = 0): + + cur_batch = tl.program_id(0) + cur_head = tl.program_id(1) + start_m = tl.program_id(2) + + cur_kv_head = cur_head // num_queries_per_kv + + cur_batch_seq_len = tl.load(B_Seqlen + cur_batch) + cur_batch_in_all_start_index = tl.load(B_Start_Loc + cur_batch) + cur_batch_in_all_stop_index = tl.load(B_Start_Loc + cur_batch + 1) + cur_batch_query_len = (cur_batch_in_all_stop_index - + cur_batch_in_all_start_index) + cur_batch_ctx_len = cur_batch_seq_len - cur_batch_query_len + + if SKIP_DECODE and cur_batch_query_len == 1: return - @triton.jit - def _fwd_kernel_flash_attn_v2( - Q, - K, - V, - K_cache, - V_cache, - B_Loc, - sm_scale, - B_Start_Loc, - B_Seqlen, - B_Ctxlen, - block_size, - x, - Out, - stride_b_loc_b, - stride_b_loc_s, - stride_qbs, - stride_qh, - stride_qd, - stride_kbs, - stride_kh, - stride_kd, - stride_vbs, - stride_vh, - stride_vd, - stride_obs, - stride_oh, - stride_od, - stride_k_cache_bs, - stride_k_cache_h, - stride_k_cache_d, - stride_k_cache_bl, - stride_k_cache_x, - stride_v_cache_bs, - stride_v_cache_h, - stride_v_cache_d, - stride_v_cache_bl, - num_queries_per_kv: int, - BLOCK_M: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, - BLOCK_N: tl.constexpr, - ): - cur_batch = tl.program_id(0) - cur_head = tl.program_id(1) - start_m = tl.program_id(2) - - cur_kv_head = cur_head // num_queries_per_kv - - cur_batch_ctx_len = tl.load(B_Ctxlen + cur_batch) - cur_batch_seq_len = tl.load(B_Seqlen + cur_batch) - cur_batch_in_all_start_index = tl.load(B_Start_Loc + cur_batch) - - block_start_loc = BLOCK_M * start_m - - # initialize offsets - offs_n = tl.arange(0, BLOCK_N) - offs_d = tl.arange(0, BLOCK_DMODEL) - offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) - off_q = ( - (cur_batch_in_all_start_index + offs_m[:, None]) * stride_qbs + - cur_head * stride_qh + offs_d[None, :] * stride_qd) - - q = tl.load(Q + off_q, - mask=offs_m[:, None] - < cur_batch_seq_len - cur_batch_ctx_len, + # start position inside of the query + # generally, N goes over kv, while M goes over query_len + block_start_loc = BLOCK_M * start_m + + # initialize offsets + # [BLOCK_SIZE]; starts at 0 + offs_bs_n = tl.arange(0, BLOCK_SIZE) + # [N]; starts at 0 + offs_n = tl.arange(0, BLOCK_N) + # [D]; starts at 0 + offs_d = tl.arange(0, BLOCK_DMODEL_PADDED) + # [M]; starts at current position in query + offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) + # [M,D] + off_q = ((cur_batch_in_all_start_index + offs_m[:, None]) * stride_qbs + + cur_head * stride_qh + offs_d[None, :] * stride_qd) + + dim_mask = tl.where( + tl.arange(0, BLOCK_DMODEL_PADDED) < BLOCK_DMODEL, 1, + 0).to(tl.int1) # [D] + + q = tl.load(Q + off_q, + mask=dim_mask[None, :] & + (offs_m[:, None] < cur_batch_query_len), + other=0.0) # [M,D] + + # initialize pointer to m and l + m_i = tl.full([BLOCK_M], float("-inf"), dtype=tl.float32) + l_i = tl.full([BLOCK_M], 1.0, dtype=tl.float32) + acc = tl.zeros([BLOCK_M, BLOCK_DMODEL_PADDED], dtype=tl.float32) # [M,D] + + # compute query against context (no causal mask here) + for start_n in tl.range(0, cur_batch_ctx_len, BLOCK_SIZE, \ + loop_unroll_factor=num_unroll_cache): + start_n = tl.multiple_of(start_n, BLOCK_SIZE) + # -- compute qk ---- + bn = tl.load(B_Loc + cur_batch * stride_b_loc_b + + (start_n // BLOCK_SIZE) * stride_b_loc_s) + # [D,BLOCK_SIZE] + off_k = ( + bn[None, :] * stride_k_cache_bs + cur_kv_head * stride_k_cache_h + + (offs_d[:, None] // x) * stride_k_cache_d + + ((start_n + offs_bs_n[None, :]) % BLOCK_SIZE) * stride_k_cache_bl + + (offs_d[:, None] % x) * stride_k_cache_x) + + # [BLOCK_SIZE,D] + off_v = (bn[:, None] * stride_v_cache_bs + + cur_kv_head * stride_v_cache_h + + offs_d[None, :] * stride_v_cache_d + + offs_bs_n[:, None] * stride_v_cache_bl) + + if start_n + BLOCK_SIZE > cur_batch_ctx_len or \ + BLOCK_DMODEL != BLOCK_DMODEL_PADDED: + k_load = tl.load( + K_cache + off_k, + mask=dim_mask[:, None] & + ((start_n + offs_bs_n[None, :]) < cur_batch_ctx_len), + other=0.0) # [D,N] + else: + k_load = tl.load(K_cache + off_k) + + if k_load.dtype.is_fp8(): + k = (k_load.to(tl.float32) * tl.load(k_scale)).to(q.dtype) + else: + k = k_load + + qk = tl.zeros([BLOCK_M, BLOCK_SIZE], dtype=tl.float32) # [M,N] + qk = tl.dot(q, k, acc=qk, input_precision=IN_PRECISION) + qk = tl.where((start_n + offs_bs_n[None, :]) < cur_batch_ctx_len, qk, + float("-inf")) + qk *= sm_scale + if SLIDING_WINDOW > 0: + # (cur_batch_ctx_len + offs_m[:, None]) are the positions of + # Q entries in sequence + # (start_n + offs_bs_n[None, :]) are the positions of + # KV entries in sequence + # So the condition makes sure each entry in Q only attends + # to KV entries not more than SLIDING_WINDOW away. + # + # We can't use -inf here, because the + # sliding window may lead to the entire row being masked. + # This then makes m_ij contain -inf, which causes NaNs in + # exp(). + qk = tl.where((cur_batch_ctx_len + offs_m[:, None]) - + (start_n + offs_bs_n[None, :]) < SLIDING_WINDOW, qk, + -10000) + + # compute running maximum + m_ij = tl.maximum(m_i, tl.max(qk, axis=1)) + p = tl.exp(qk - m_ij[:, None]) + l_ij = tl.sum(p, axis=1) + alpha = tl.exp(m_i - m_ij) + acc = acc * alpha[:, None] + + # update acc + if start_n + BLOCK_SIZE > cur_batch_ctx_len or \ + BLOCK_DMODEL != BLOCK_DMODEL_PADDED: + v_load = tl.load( + V_cache + off_v, + mask=dim_mask[None, :] & + ((start_n + offs_bs_n[:, None]) < cur_batch_ctx_len), + other=0.0) # [N,D] + else: + v_load = tl.load(V_cache + off_v) + + if v_load.dtype.is_fp8(): + v = (v_load.to(tl.float32) * tl.load(v_scale)).to(q.dtype) + else: + v = v_load + p = p.to(v.dtype) + + acc = tl.dot(p, v, acc=acc, input_precision=IN_PRECISION) + # # update m_i and l_i + l_i = l_i * alpha + l_ij + m_i = m_ij + + off_k = (offs_n[None, :] * stride_kbs + cur_kv_head * stride_kh + + offs_d[:, None] * stride_kd) + off_v = (offs_n[:, None] * stride_vbs + cur_kv_head * stride_vh + + offs_d[None, :] * stride_vd) + k_ptrs = K + off_k + v_ptrs = V + off_v + + # block_mask is 0 when we're already past the current query length + block_mask = tl.where(block_start_loc < cur_batch_query_len, 1, 0) + + # compute query against itself (with causal mask) + for start_n in tl.range(0, \ + block_mask * (start_m + 1) * BLOCK_M, BLOCK_N, \ + loop_unroll_factor=num_unroll_request): + start_n = tl.multiple_of(start_n, BLOCK_N) + # -- compute qk ---- + k = tl.load(k_ptrs + + (cur_batch_in_all_start_index + start_n) * stride_kbs, + mask=dim_mask[:, None] & + ((start_n + offs_n[None, :]) < cur_batch_query_len), other=0.0) - # # initialize pointer to m and l - m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") - l_i = tl.zeros([BLOCK_M], dtype=tl.float32) - acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) - - for start_n in range(0, cur_batch_ctx_len, BLOCK_N): - start_n = tl.multiple_of(start_n, BLOCK_N) - # -- compute qk ---- - bn = tl.load(B_Loc + cur_batch * stride_b_loc_b + - ((start_n + offs_n) // block_size) * stride_b_loc_s, - mask=(start_n + offs_n) < cur_batch_ctx_len, - other=0) - off_k = (bn[None, :] * stride_k_cache_bs + - cur_kv_head * stride_k_cache_h + - (offs_d[:, None] // x) * stride_k_cache_d + - ((start_n + offs_n[None, :]) % block_size) * - stride_k_cache_bl + - (offs_d[:, None] % x) * stride_k_cache_x) - off_v = ( - bn[:, None] * stride_v_cache_bs + - cur_kv_head * stride_v_cache_h + - offs_d[None, :] * stride_v_cache_d + - (start_n + offs_n[:, None]) % block_size * stride_v_cache_bl) - k = tl.load(K_cache + off_k, - mask=(start_n + offs_n[None, :]) < cur_batch_ctx_len, - other=0.0) - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - qk += tl.dot(q, k) - qk = tl.where((start_n + offs_n[None, :]) < cur_batch_ctx_len, qk, - float("-inf")) - qk *= sm_scale - - # -- compute m_ij, p, l_ij - m_ij = tl.max(qk, 1) - m_i_new = tl.maximum(m_i, m_ij) - p = tl.math.exp(qk - m_i_new[:, None]) - l_ij = tl.sum(p, 1) - # -- update m_i and l_i - - alpha = tl.math.exp(m_i - m_i_new) - l_i_new = alpha * l_i + l_ij - # -- update output accumulator -- - # scale p - # scale acc - acc_scale = alpha - # acc_scale = l_i / l_i_new * alpha - acc = acc * acc_scale[:, None] - # update acc - v = tl.load(V_cache + off_v, - mask=(start_n + offs_n[:, None]) < cur_batch_ctx_len, - other=0.0) - - p = p.to(v.dtype) - acc += tl.dot(p, v) - # update m_i and l_i - l_i = l_i_new - m_i = m_i_new - - off_k = (offs_n[None, :] * stride_kbs + cur_kv_head * stride_kh + - offs_d[:, None] * stride_kd) - off_v = (offs_n[:, None] * stride_vbs + cur_kv_head * stride_vh + - offs_d[None, :] * stride_vd) - k_ptrs = K + off_k - v_ptrs = V + off_v - - block_mask = tl.where( - block_start_loc < cur_batch_seq_len - cur_batch_ctx_len, 1, 0) - - for start_n in range(0, block_mask * (start_m + 1) * BLOCK_M, BLOCK_N): - start_n = tl.multiple_of(start_n, BLOCK_N) - # -- compute qk ---- - k = tl.load(k_ptrs + - (cur_batch_in_all_start_index + start_n) * stride_kbs, - mask=(start_n + offs_n[None, :]) - < cur_batch_seq_len - cur_batch_ctx_len, - other=0.0) - - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - qk += tl.dot(q, k) - qk *= sm_scale - qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, - float("-inf")) - - # -- compute m_ij, p, l_ij - m_ij = tl.max(qk, 1) - m_i_new = tl.maximum(m_i, m_ij) - p = tl.math.exp(qk - m_i_new[:, None]) - l_ij = tl.sum(p, 1) - # -- update m_i and l_i - - alpha = tl.math.exp(m_i - m_i_new) - l_i_new = alpha * l_i + l_ij - # -- update output accumulator -- - # scale p - # scale acc - acc_scale = alpha - # acc_scale = l_i / l_i_new * alpha - acc = acc * acc_scale[:, None] - # update acc - v = tl.load(v_ptrs + - (cur_batch_in_all_start_index + start_n) * stride_vbs, - mask=(start_n + offs_n[:, None]) - < cur_batch_seq_len - cur_batch_ctx_len, - other=0.0) - - p = p.to(v.dtype) - acc += tl.dot(p, v) - # update m_i and l_i - l_i = l_i_new - m_i = m_i_new - - # acc /= l_i[:, None] - # initialize pointers to output - off_o = ( - (cur_batch_in_all_start_index + offs_m[:, None]) * stride_obs + - cur_head * stride_oh + offs_d[None, :] * stride_od) - out_ptrs = Out + off_o - tl.store(out_ptrs, - acc, - mask=offs_m[:, None] < cur_batch_seq_len - cur_batch_ctx_len) - return - - @triton.jit - def _fwd_kernel_alibi( - Q, - K, - V, - K_cache, - V_cache, - B_Loc, - sm_scale, - k_scale, - v_scale, - B_Start_Loc, - B_Seqlen, - Alibi_slopes, - block_size, - x, - Out, - stride_b_loc_b, - stride_b_loc_s, - stride_qbs, - stride_qh, - stride_qd, - stride_kbs, - stride_kh, - stride_kd, - stride_vbs, - stride_vh, - stride_vd, - stride_obs, - stride_oh, - stride_od, - stride_k_cache_bs, - stride_k_cache_h, - stride_k_cache_d, - stride_k_cache_bl, - stride_k_cache_x, - stride_v_cache_bs, - stride_v_cache_h, - stride_v_cache_d, - stride_v_cache_bl, - num_queries_per_kv: int, - IN_PRECISION: tl.constexpr, - BLOCK_M: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, # head size - BLOCK_DMODEL_PADDED: tl.constexpr, # head size padded to a power of 2 - BLOCK_N: tl.constexpr, - SKIP_DECODE: tl.constexpr, - ): - # attn_bias[] - cur_batch = tl.program_id(0) - cur_head = tl.program_id(1) - start_m = tl.program_id(2) - - cur_kv_head = cur_head // num_queries_per_kv - - # cur_batch_seq_len: the length of prompts - # cur_batch_ctx_len: the length of prefix - # cur_batch_in_all_start_index: the start id of the dim=0 - cur_batch_seq_len = tl.load(B_Seqlen + cur_batch) - cur_batch_in_all_start_index = tl.load(B_Start_Loc + cur_batch) - cur_batch_in_all_stop_index = tl.load(B_Start_Loc + cur_batch + 1) - cur_batch_query_len = (cur_batch_in_all_stop_index - - cur_batch_in_all_start_index) - cur_batch_ctx_len = cur_batch_seq_len - cur_batch_query_len - - if SKIP_DECODE and cur_batch_query_len == 1: - return - - block_start_loc = BLOCK_M * start_m - - # initialize offsets - offs_n = tl.arange(0, BLOCK_N) - offs_d = tl.arange(0, BLOCK_DMODEL_PADDED) - offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) - off_q = ( - (cur_batch_in_all_start_index + offs_m[:, None]) * stride_qbs + - cur_head * stride_qh + offs_d[None, :] * stride_qd) - - dim_mask = tl.where( - tl.arange(0, BLOCK_DMODEL_PADDED) < BLOCK_DMODEL, 1, 0).to(tl.int1) - - q = tl.load(Q + off_q, + qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) + qk = tl.dot(q, k, acc=qk, input_precision=IN_PRECISION) + qk *= sm_scale + # apply causal mask + qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, + float("-inf")) + if SLIDING_WINDOW > 0: + qk = tl.where( + offs_m[:, None] - (start_n + offs_n[None, :]) < SLIDING_WINDOW, + qk, -10000) + + # compute running maximum + m_ij = tl.maximum(m_i, tl.max(qk, axis=1)) + p = tl.exp(qk - m_ij[:, None]) + l_ij = tl.sum(p, axis=1) + alpha = tl.exp(m_i - m_ij) + acc = acc * alpha[:, None] + + # update acc + v = tl.load(v_ptrs + + (cur_batch_in_all_start_index + start_n) * stride_vbs, mask=dim_mask[None, :] & - (offs_m[:, None] < cur_batch_seq_len - cur_batch_ctx_len), + ((start_n + offs_n[:, None]) < cur_batch_query_len), + other=0.0) + p = p.to(v.dtype) + + acc = tl.dot(p, v, acc=acc, input_precision=IN_PRECISION) + # update m_i and l_i + l_i = l_i * alpha + l_ij + m_i = m_ij + + acc = acc / l_i[:, None] + + # initialize pointers to output + off_o = ((cur_batch_in_all_start_index + offs_m[:, None]) * stride_obs + + cur_head * stride_oh + offs_d[None, :] * stride_od) + out_ptrs = Out + off_o + tl.store(out_ptrs, + acc, + mask=dim_mask[None, :] & (offs_m[:, None] < cur_batch_query_len)) + return + + +@triton.jit +def _fwd_kernel_flash_attn_v2( + Q, + K, + V, + K_cache, + V_cache, + B_Loc, + sm_scale, + B_Start_Loc, + B_Seqlen, + B_Ctxlen, + block_size, + x, + Out, + stride_b_loc_b, + stride_b_loc_s, + stride_qbs, + stride_qh, + stride_qd, + stride_kbs, + stride_kh, + stride_kd, + stride_vbs, + stride_vh, + stride_vd, + stride_obs, + stride_oh, + stride_od, + stride_k_cache_bs, + stride_k_cache_h, + stride_k_cache_d, + stride_k_cache_bl, + stride_k_cache_x, + stride_v_cache_bs, + stride_v_cache_h, + stride_v_cache_d, + stride_v_cache_bl, + num_queries_per_kv: int, + BLOCK_M: tl.constexpr, + BLOCK_DMODEL: tl.constexpr, + BLOCK_N: tl.constexpr, +): + cur_batch = tl.program_id(0) + cur_head = tl.program_id(1) + start_m = tl.program_id(2) + + cur_kv_head = cur_head // num_queries_per_kv + + cur_batch_ctx_len = tl.load(B_Ctxlen + cur_batch) + cur_batch_seq_len = tl.load(B_Seqlen + cur_batch) + cur_batch_in_all_start_index = tl.load(B_Start_Loc + cur_batch) + + block_start_loc = BLOCK_M * start_m + + # initialize offsets + offs_n = tl.arange(0, BLOCK_N) + offs_d = tl.arange(0, BLOCK_DMODEL) + offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) + off_q = ((cur_batch_in_all_start_index + offs_m[:, None]) * stride_qbs + + cur_head * stride_qh + offs_d[None, :] * stride_qd) + + q = tl.load(Q + off_q, + mask=offs_m[:, None] < cur_batch_seq_len - cur_batch_ctx_len, + other=0.0) + + # # initialize pointer to m and l + m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") + l_i = tl.zeros([BLOCK_M], dtype=tl.float32) + acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) + + for start_n in range(0, cur_batch_ctx_len, BLOCK_N): + start_n = tl.multiple_of(start_n, BLOCK_N) + # -- compute qk ---- + bn = tl.load(B_Loc + cur_batch * stride_b_loc_b + + ((start_n + offs_n) // block_size) * stride_b_loc_s, + mask=(start_n + offs_n) < cur_batch_ctx_len, + other=0) + off_k = ( + bn[None, :] * stride_k_cache_bs + cur_kv_head * stride_k_cache_h + + (offs_d[:, None] // x) * stride_k_cache_d + + ((start_n + offs_n[None, :]) % block_size) * stride_k_cache_bl + + (offs_d[:, None] % x) * stride_k_cache_x) + off_v = (bn[:, None] * stride_v_cache_bs + + cur_kv_head * stride_v_cache_h + + offs_d[None, :] * stride_v_cache_d + + (start_n + offs_n[:, None]) % block_size * stride_v_cache_bl) + k = tl.load(K_cache + off_k, + mask=(start_n + offs_n[None, :]) < cur_batch_ctx_len, + other=0.0) + qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) + qk += tl.dot(q, k) + qk = tl.where((start_n + offs_n[None, :]) < cur_batch_ctx_len, qk, + float("-inf")) + qk *= sm_scale + + # -- compute m_ij, p, l_ij + m_ij = tl.max(qk, 1) + m_i_new = tl.maximum(m_i, m_ij) + p = tl.math.exp(qk - m_i_new[:, None]) + l_ij = tl.sum(p, 1) + # -- update m_i and l_i + + alpha = tl.math.exp(m_i - m_i_new) + l_i_new = alpha * l_i + l_ij + # -- update output accumulator -- + # scale p + # scale acc + acc_scale = alpha + # acc_scale = l_i / l_i_new * alpha + acc = acc * acc_scale[:, None] + # update acc + v = tl.load(V_cache + off_v, + mask=(start_n + offs_n[:, None]) < cur_batch_ctx_len, + other=0.0) + + p = p.to(v.dtype) + acc += tl.dot(p, v) + # update m_i and l_i + l_i = l_i_new + m_i = m_i_new + + off_k = (offs_n[None, :] * stride_kbs + cur_kv_head * stride_kh + + offs_d[:, None] * stride_kd) + off_v = (offs_n[:, None] * stride_vbs + cur_kv_head * stride_vh + + offs_d[None, :] * stride_vd) + k_ptrs = K + off_k + v_ptrs = V + off_v + + block_mask = tl.where( + block_start_loc < cur_batch_seq_len - cur_batch_ctx_len, 1, 0) + + for start_n in range(0, block_mask * (start_m + 1) * BLOCK_M, BLOCK_N): + start_n = tl.multiple_of(start_n, BLOCK_N) + # -- compute qk ---- + k = tl.load(k_ptrs + + (cur_batch_in_all_start_index + start_n) * stride_kbs, + mask=(start_n + offs_n[None, :]) + < cur_batch_seq_len - cur_batch_ctx_len, other=0.0) - # # initialize pointer to m and l - m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") - l_i = tl.zeros([BLOCK_M], dtype=tl.float32) - acc = tl.zeros([BLOCK_M, BLOCK_DMODEL_PADDED], dtype=tl.float32) - - alibi_slope = tl.load(Alibi_slopes + cur_head) - alibi_start_q = tl.arange( - 0, BLOCK_M) + block_start_loc + cur_batch_ctx_len - alibi_start_k = 0 - for start_n in range(0, cur_batch_ctx_len, BLOCK_N): - start_n = tl.multiple_of(start_n, BLOCK_N) - # -- compute qk ---- - bn = tl.load(B_Loc + cur_batch * stride_b_loc_b + - ((start_n + offs_n) // block_size) * stride_b_loc_s, - mask=(start_n + offs_n) < cur_batch_ctx_len, - other=0) - off_k = (bn[None, :] * stride_k_cache_bs + - cur_kv_head * stride_k_cache_h + - (offs_d[:, None] // x) * stride_k_cache_d + - ((start_n + offs_n[None, :]) % block_size) * - stride_k_cache_bl + - (offs_d[:, None] % x) * stride_k_cache_x) - off_v = ( - bn[:, None] * stride_v_cache_bs + - cur_kv_head * stride_v_cache_h + - offs_d[None, :] * stride_v_cache_d + - (start_n + offs_n[:, None]) % block_size * stride_v_cache_bl) - k_load = tl.load(K_cache + off_k, - mask=dim_mask[:, None] & - ((start_n + offs_n[None, :]) < cur_batch_ctx_len), - other=0.0) # [D,N] - - if k_load.dtype.is_fp8(): - k = (k_load.to(tl.float32) * tl.load(k_scale)).to(q.dtype) - else: - k = k_load - - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - qk = tl.dot(q, k, acc=qk, input_precision=IN_PRECISION) - qk = tl.where((start_n + offs_n[None, :]) < cur_batch_ctx_len, qk, - float("-inf")) - qk *= sm_scale - - # load alibi - alibi = (tl.arange(0, BLOCK_N)[None, :] + alibi_start_k - - alibi_start_q[:, None]) * alibi_slope - alibi = tl.where( - (alibi <= 0) & (alibi_start_q[:, None] < cur_batch_seq_len), - alibi, float("-inf")) - qk += alibi - alibi_start_k += BLOCK_N - - # -- compute m_ij, p, l_ij - m_ij = tl.max(qk, 1) - m_i_new = tl.maximum(m_i, m_ij) - p = tl.math.exp(qk - m_i_new[:, None]) - l_ij = tl.sum(p, 1) - # -- update m_i and l_i - - alpha = tl.math.exp(m_i - m_i_new) - l_i_new = alpha * l_i + l_ij - # -- update output accumulator -- - # scale p - # scale acc - acc_scale = alpha - # acc_scale = l_i / l_i_new * alpha - acc = acc * acc_scale[:, None] - # update acc - v_load = tl.load(V_cache + off_v, - mask=dim_mask[None, :] & - ((start_n + offs_n[:, None]) < cur_batch_ctx_len), - other=0.0) - if v_load.dtype.is_fp8(): - v = (v_load.to(tl.float32) * tl.load(v_scale)).to(q.dtype) - else: - v = v_load - p = p.to(v.dtype) - - acc = tl.dot(p, v, acc=acc, input_precision='ieee') - # update m_i and l_i - l_i = l_i_new - m_i = m_i_new - - off_k = (offs_n[None, :] * stride_kbs + cur_kv_head * stride_kh + - offs_d[:, None] * stride_kd) - off_v = (offs_n[:, None] * stride_vbs + cur_kv_head * stride_vh + - offs_d[None, :] * stride_vd) - k_ptrs = K + off_k - v_ptrs = V + off_v - - block_mask = tl.where( - block_start_loc < cur_batch_seq_len - cur_batch_ctx_len, 1, 0) - - # init alibi - alibi_slope = tl.load(Alibi_slopes + cur_head) - alibi_start_q = tl.arange( - 0, BLOCK_M) + block_start_loc + cur_batch_ctx_len - alibi_start_k = cur_batch_ctx_len - # # init debugger - # offset_db_q = tl.arange(0, BLOCK_M) + block_start_loc - # offset_db_k = tl.arange(0, BLOCK_N) - # calc q[BLOCK_M, BLOCK_MODEL] mul k[prefix_len: , BLOCK_DMODEL] - for start_n in range(0, block_mask * (start_m + 1) * BLOCK_M, BLOCK_N): - start_n = tl.multiple_of(start_n, BLOCK_N) - # -- compute qk ---- - k = tl.load(k_ptrs + - (cur_batch_in_all_start_index + start_n) * stride_kbs, - mask=dim_mask[:, None] & - ((start_n + offs_n[None, :]) - < cur_batch_seq_len - cur_batch_ctx_len), - other=0.0) - - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - qk = tl.dot(q, k, acc=qk, input_precision='ieee') - qk *= sm_scale - qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, - float("-inf")) - - # load alibi - alibi = (tl.arange(0, BLOCK_N)[None, :] + alibi_start_k - - alibi_start_q[:, None]) * alibi_slope - alibi = tl.where( - (alibi <= 0) & (alibi_start_q[:, None] < cur_batch_seq_len), - alibi, float("-inf")) - qk += alibi - alibi_start_k += BLOCK_N - - # -- compute m_ij, p, l_ij - m_ij = tl.max(qk, 1) - m_i_new = tl.maximum(m_i, m_ij) - p = tl.math.exp(qk - m_i_new[:, None]) - l_ij = tl.sum(p, 1) - # -- update m_i and l_i - - alpha = tl.math.exp(m_i - m_i_new) - l_i_new = alpha * l_i + l_ij - # -- update output accumulator -- - # scale p - # scale acc - acc_scale = alpha - # acc_scale = l_i / l_i_new * alpha - acc = acc * acc_scale[:, None] - # update acc - v = tl.load(v_ptrs + - (cur_batch_in_all_start_index + start_n) * stride_vbs, - mask=dim_mask[None, :] & - ((start_n + offs_n[:, None]) - < cur_batch_seq_len - cur_batch_ctx_len), - other=0.0) - p = p.to(v.dtype) - - acc = tl.dot(p, v, acc=acc, input_precision='ieee') - # update m_i and l_i - l_i = l_i_new - m_i = m_i_new - - acc = acc / l_i[:, None] - - # initialize pointers to output - off_o = ( - (cur_batch_in_all_start_index + offs_m[:, None]) * stride_obs + - cur_head * stride_oh + offs_d[None, :] * stride_od) - out_ptrs = Out + off_o - tl.store(out_ptrs, - acc, - mask=dim_mask[None, :] & - (offs_m[:, None] < cur_batch_seq_len - cur_batch_ctx_len)) + qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) + qk += tl.dot(q, k) + qk *= sm_scale + qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, + float("-inf")) + + # -- compute m_ij, p, l_ij + m_ij = tl.max(qk, 1) + m_i_new = tl.maximum(m_i, m_ij) + p = tl.math.exp(qk - m_i_new[:, None]) + l_ij = tl.sum(p, 1) + # -- update m_i and l_i + + alpha = tl.math.exp(m_i - m_i_new) + l_i_new = alpha * l_i + l_ij + # -- update output accumulator -- + # scale p + # scale acc + acc_scale = alpha + # acc_scale = l_i / l_i_new * alpha + acc = acc * acc_scale[:, None] + # update acc + v = tl.load(v_ptrs + + (cur_batch_in_all_start_index + start_n) * stride_vbs, + mask=(start_n + offs_n[:, None]) + < cur_batch_seq_len - cur_batch_ctx_len, + other=0.0) + + p = p.to(v.dtype) + acc += tl.dot(p, v) + # update m_i and l_i + l_i = l_i_new + m_i = m_i_new + + # acc /= l_i[:, None] + # initialize pointers to output + off_o = ((cur_batch_in_all_start_index + offs_m[:, None]) * stride_obs + + cur_head * stride_oh + offs_d[None, :] * stride_od) + out_ptrs = Out + off_o + tl.store(out_ptrs, + acc, + mask=offs_m[:, None] < cur_batch_seq_len - cur_batch_ctx_len) + return + + +@triton.jit +def _fwd_kernel_alibi( + Q, + K, + V, + K_cache, + V_cache, + B_Loc, + sm_scale, + k_scale, + v_scale, + B_Start_Loc, + B_Seqlen, + Alibi_slopes, + block_size, + x, + Out, + stride_b_loc_b, + stride_b_loc_s, + stride_qbs, + stride_qh, + stride_qd, + stride_kbs, + stride_kh, + stride_kd, + stride_vbs, + stride_vh, + stride_vd, + stride_obs, + stride_oh, + stride_od, + stride_k_cache_bs, + stride_k_cache_h, + stride_k_cache_d, + stride_k_cache_bl, + stride_k_cache_x, + stride_v_cache_bs, + stride_v_cache_h, + stride_v_cache_d, + stride_v_cache_bl, + num_queries_per_kv: int, + IN_PRECISION: tl.constexpr, + BLOCK_M: tl.constexpr, + BLOCK_DMODEL: tl.constexpr, # head size + BLOCK_DMODEL_PADDED: tl.constexpr, # head size padded to a power of 2 + BLOCK_N: tl.constexpr, + SKIP_DECODE: tl.constexpr, +): + # attn_bias[] + cur_batch = tl.program_id(0) + cur_head = tl.program_id(1) + start_m = tl.program_id(2) + + cur_kv_head = cur_head // num_queries_per_kv + + # cur_batch_seq_len: the length of prompts + # cur_batch_ctx_len: the length of prefix + # cur_batch_in_all_start_index: the start id of the dim=0 + cur_batch_seq_len = tl.load(B_Seqlen + cur_batch) + cur_batch_in_all_start_index = tl.load(B_Start_Loc + cur_batch) + cur_batch_in_all_stop_index = tl.load(B_Start_Loc + cur_batch + 1) + cur_batch_query_len = (cur_batch_in_all_stop_index - + cur_batch_in_all_start_index) + cur_batch_ctx_len = cur_batch_seq_len - cur_batch_query_len + + if SKIP_DECODE and cur_batch_query_len == 1: return - @torch.inference_mode() - def context_attention_fwd(q, - k, - v, - o, - kv_cache_dtype: str, - k_cache, - v_cache, - b_loc, - b_start_loc, - b_seq_len, - max_seq_len, - max_input_len, - k_scale: torch.Tensor, - v_scale: torch.Tensor, - alibi_slopes=None, - sliding_window=None, - sm_scale=None, - skip_decode=False): - - q_dtype_is_f32 = q.dtype is torch.float32 + block_start_loc = BLOCK_M * start_m + + # initialize offsets + offs_n = tl.arange(0, BLOCK_N) + offs_d = tl.arange(0, BLOCK_DMODEL_PADDED) + offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) + off_q = ((cur_batch_in_all_start_index + offs_m[:, None]) * stride_qbs + + cur_head * stride_qh + offs_d[None, :] * stride_qd) + + dim_mask = tl.where( + tl.arange(0, BLOCK_DMODEL_PADDED) < BLOCK_DMODEL, 1, 0).to(tl.int1) + + q = tl.load(Q + off_q, + mask=dim_mask[None, :] & + (offs_m[:, None] < cur_batch_seq_len - cur_batch_ctx_len), + other=0.0) + + # # initialize pointer to m and l + m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") + l_i = tl.zeros([BLOCK_M], dtype=tl.float32) + acc = tl.zeros([BLOCK_M, BLOCK_DMODEL_PADDED], dtype=tl.float32) + + alibi_slope = tl.load(Alibi_slopes + cur_head) + alibi_start_q = tl.arange(0, BLOCK_M) + block_start_loc + cur_batch_ctx_len + alibi_start_k = 0 + for start_n in range(0, cur_batch_ctx_len, BLOCK_N): + start_n = tl.multiple_of(start_n, BLOCK_N) + # -- compute qk ---- + bn = tl.load(B_Loc + cur_batch * stride_b_loc_b + + ((start_n + offs_n) // block_size) * stride_b_loc_s, + mask=(start_n + offs_n) < cur_batch_ctx_len, + other=0) + off_k = ( + bn[None, :] * stride_k_cache_bs + cur_kv_head * stride_k_cache_h + + (offs_d[:, None] // x) * stride_k_cache_d + + ((start_n + offs_n[None, :]) % block_size) * stride_k_cache_bl + + (offs_d[:, None] % x) * stride_k_cache_x) + off_v = (bn[:, None] * stride_v_cache_bs + + cur_kv_head * stride_v_cache_h + + offs_d[None, :] * stride_v_cache_d + + (start_n + offs_n[:, None]) % block_size * stride_v_cache_bl) + k_load = tl.load(K_cache + off_k, + mask=dim_mask[:, None] & + ((start_n + offs_n[None, :]) < cur_batch_ctx_len), + other=0.0) # [D,N] + + if k_load.dtype.is_fp8(): + k = (k_load.to(tl.float32) * tl.load(k_scale)).to(q.dtype) + else: + k = k_load + + qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) + qk = tl.dot(q, k, acc=qk, input_precision=IN_PRECISION) + qk = tl.where((start_n + offs_n[None, :]) < cur_batch_ctx_len, qk, + float("-inf")) + qk *= sm_scale + + # load alibi + alibi = (tl.arange(0, BLOCK_N)[None, :] + alibi_start_k - + alibi_start_q[:, None]) * alibi_slope + alibi = tl.where( + (alibi <= 0) & (alibi_start_q[:, None] < cur_batch_seq_len), alibi, + float("-inf")) + qk += alibi + alibi_start_k += BLOCK_N + + # -- compute m_ij, p, l_ij + m_ij = tl.max(qk, 1) + m_i_new = tl.maximum(m_i, m_ij) + p = tl.math.exp(qk - m_i_new[:, None]) + l_ij = tl.sum(p, 1) + # -- update m_i and l_i + + alpha = tl.math.exp(m_i - m_i_new) + l_i_new = alpha * l_i + l_ij + # -- update output accumulator -- + # scale p + # scale acc + acc_scale = alpha + # acc_scale = l_i / l_i_new * alpha + acc = acc * acc_scale[:, None] + # update acc + v_load = tl.load(V_cache + off_v, + mask=dim_mask[None, :] & + ((start_n + offs_n[:, None]) < cur_batch_ctx_len), + other=0.0) + if v_load.dtype.is_fp8(): + v = (v_load.to(tl.float32) * tl.load(v_scale)).to(q.dtype) + else: + v = v_load + p = p.to(v.dtype) + + acc = tl.dot(p, v, acc=acc, input_precision='ieee') + # update m_i and l_i + l_i = l_i_new + m_i = m_i_new + + off_k = (offs_n[None, :] * stride_kbs + cur_kv_head * stride_kh + + offs_d[:, None] * stride_kd) + off_v = (offs_n[:, None] * stride_vbs + cur_kv_head * stride_vh + + offs_d[None, :] * stride_vd) + k_ptrs = K + off_k + v_ptrs = V + off_v + + block_mask = tl.where( + block_start_loc < cur_batch_seq_len - cur_batch_ctx_len, 1, 0) + + # init alibi + alibi_slope = tl.load(Alibi_slopes + cur_head) + alibi_start_q = tl.arange(0, BLOCK_M) + block_start_loc + cur_batch_ctx_len + alibi_start_k = cur_batch_ctx_len + # # init debugger + # offset_db_q = tl.arange(0, BLOCK_M) + block_start_loc + # offset_db_k = tl.arange(0, BLOCK_N) + # calc q[BLOCK_M, BLOCK_MODEL] mul k[prefix_len: , BLOCK_DMODEL] + for start_n in range(0, block_mask * (start_m + 1) * BLOCK_M, BLOCK_N): + start_n = tl.multiple_of(start_n, BLOCK_N) + # -- compute qk ---- + k = tl.load( + k_ptrs + (cur_batch_in_all_start_index + start_n) * stride_kbs, + mask=dim_mask[:, None] & ((start_n + offs_n[None, :]) + < cur_batch_seq_len - cur_batch_ctx_len), + other=0.0) + + qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) + qk = tl.dot(q, k, acc=qk, input_precision='ieee') + qk *= sm_scale + qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, + float("-inf")) + + # load alibi + alibi = (tl.arange(0, BLOCK_N)[None, :] + alibi_start_k - + alibi_start_q[:, None]) * alibi_slope + alibi = tl.where( + (alibi <= 0) & (alibi_start_q[:, None] < cur_batch_seq_len), alibi, + float("-inf")) + qk += alibi + alibi_start_k += BLOCK_N + + # -- compute m_ij, p, l_ij + m_ij = tl.max(qk, 1) + m_i_new = tl.maximum(m_i, m_ij) + p = tl.math.exp(qk - m_i_new[:, None]) + l_ij = tl.sum(p, 1) + # -- update m_i and l_i + + alpha = tl.math.exp(m_i - m_i_new) + l_i_new = alpha * l_i + l_ij + # -- update output accumulator -- + # scale p + # scale acc + acc_scale = alpha + # acc_scale = l_i / l_i_new * alpha + acc = acc * acc_scale[:, None] + # update acc + v = tl.load( + v_ptrs + (cur_batch_in_all_start_index + start_n) * stride_vbs, + mask=dim_mask[None, :] & ((start_n + offs_n[:, None]) + < cur_batch_seq_len - cur_batch_ctx_len), + other=0.0) + p = p.to(v.dtype) + + acc = tl.dot(p, v, acc=acc, input_precision='ieee') + # update m_i and l_i + l_i = l_i_new + m_i = m_i_new + + acc = acc / l_i[:, None] + + # initialize pointers to output + off_o = ((cur_batch_in_all_start_index + offs_m[:, None]) * stride_obs + + cur_head * stride_oh + offs_d[None, :] * stride_od) + out_ptrs = Out + off_o + tl.store(out_ptrs, + acc, + mask=dim_mask[None, :] & + (offs_m[:, None] < cur_batch_seq_len - cur_batch_ctx_len)) + return + + +@torch.inference_mode() +def context_attention_fwd(q, + k, + v, + o, + kv_cache_dtype: str, + k_cache, + v_cache, + b_loc, + b_start_loc, + b_seq_len, + max_seq_len, + max_input_len, + k_scale: torch.Tensor, + v_scale: torch.Tensor, + alibi_slopes=None, + sliding_window=None, + sm_scale=None, + skip_decode=False): + + q_dtype_is_f32 = q.dtype is torch.float32 + + # Turing does have tensor core for float32 multiplication + # use ieee as fallback for triton kernels work. There is also + # warning on vllm/config.py to inform users this fallback + # implementation + IN_PRECISION = 'ieee' if IS_TURING and q_dtype_is_f32 else None + + # Conversion of FP8 Tensor from uint8 storage to + # appropriate torch.dtype for interpretation by Triton + if "fp8" in kv_cache_dtype: + assert (k_cache.dtype == torch.uint8) + assert (v_cache.dtype == torch.uint8) + + if kv_cache_dtype in ("fp8", "fp8_e4m3"): + target_dtype = current_platform.fp8_dtype() + elif kv_cache_dtype == "fp8_e5m2": + target_dtype = torch.float8_e5m2 + else: + raise ValueError("Unsupported FP8 dtype:", kv_cache_dtype) + + k_cache = k_cache.view(target_dtype) + v_cache = v_cache.view(target_dtype) + + if (k_cache.dtype == torch.uint8 + or v_cache.dtype == torch.uint8 and kv_cache_dtype == "auto"): + raise ValueError("kv_cache_dtype='auto' unsupported for\ + FP8 KV Cache prefill kernel") + + # shape constraints + Lq, Lk, Lv = q.shape[-1], k.shape[-1], v.shape[-1] + assert Lq == Lk and Lk == Lv + # round up Lk to a power of 2 - this is required for Triton block size + Lk_padded = triton.next_power_of_2(Lk) + + if sm_scale is None: + sm_scale = 1.0 / (Lq**0.5) + batch, head = b_seq_len.shape[0], q.shape[1] + num_queries_per_kv = q.shape[1] // k.shape[1] + + assert batch + 1 == len(b_start_loc) + + # 0 means "disable" + if sliding_window is None or sliding_window <= 0: + sliding_window = 0 + + if alibi_slopes is not None: # need to reduce num. blocks when using fp32 # due to increased use of GPU shared memory # if q.dtype is torch.float32: BLOCK = BASE_BLOCK // 2 if q_dtype_is_f32 else BASE_BLOCK - - # Turing does have tensor core for float32 multiplication - # use ieee as fallback for triton kernels work. There is also - # warning on vllm/config.py to inform users this fallback - # implementation - IN_PRECISION = 'ieee' if IS_TURING and q_dtype_is_f32 else None - - # Conversion of FP8 Tensor from uint8 storage to - # appropriate torch.dtype for interpretation by Triton - if "fp8" in kv_cache_dtype: - assert (k_cache.dtype == torch.uint8) - assert (v_cache.dtype == torch.uint8) - - if kv_cache_dtype in ("fp8", "fp8_e4m3"): - target_dtype = current_platform.fp8_dtype() - elif kv_cache_dtype == "fp8_e5m2": - target_dtype = torch.float8_e5m2 - else: - raise ValueError("Unsupported FP8 dtype:", kv_cache_dtype) - - k_cache = k_cache.view(target_dtype) - v_cache = v_cache.view(target_dtype) - - if (k_cache.dtype == torch.uint8 - or v_cache.dtype == torch.uint8 and kv_cache_dtype == "auto"): - raise ValueError("kv_cache_dtype='auto' unsupported for\ - FP8 KV Cache prefill kernel") - - # shape constraints - Lq, Lk, Lv = q.shape[-1], k.shape[-1], v.shape[-1] - assert Lq == Lk and Lk == Lv - # round up Lk to a power of 2 - this is required for Triton block size - Lk_padded = triton.next_power_of_2(Lk) - - if sm_scale is None: - sm_scale = 1.0 / (Lq**0.5) - batch, head = b_seq_len.shape[0], q.shape[1] - num_queries_per_kv = q.shape[1] // k.shape[1] - - assert batch + 1 == len(b_start_loc) - grid = (batch, head, triton.cdiv(max_input_len, BLOCK)) # batch, head, - - # 0 means "disable" - if sliding_window is None or sliding_window <= 0: - sliding_window = 0 - - if alibi_slopes is not None: - _fwd_kernel_alibi[grid]( - q, - k, - v, - k_cache, - v_cache, - b_loc, - sm_scale, - k_scale, - v_scale, - b_start_loc, - b_seq_len, - alibi_slopes, - v_cache.shape[3], - k_cache.shape[4], - o, - b_loc.stride(0), - b_loc.stride(1), - q.stride(0), - q.stride(1), - q.stride(2), - k.stride(0), - k.stride(1), - k.stride(2), - v.stride(0), - v.stride(1), - v.stride(2), - o.stride(0), - o.stride(1), - o.stride(2), - k_cache.stride(0), - k_cache.stride(1), - k_cache.stride(2), - k_cache.stride(3), - k_cache.stride( - 4 - ), #[num_blocks, num_kv_heads, head_size/x, block_size, x] - v_cache.stride(0), - v_cache.stride(1), - v_cache.stride(2), - v_cache.stride( - 3), #[num_blocks, num_kv_heads, head_size, block_size] - num_queries_per_kv=num_queries_per_kv, - IN_PRECISION=IN_PRECISION, - BLOCK_M=BLOCK, - BLOCK_DMODEL=Lk, - BLOCK_DMODEL_PADDED=Lk_padded, - BLOCK_N=BLOCK, - SKIP_DECODE=skip_decode, - num_warps=NUM_WARPS, - num_stages=1, - ) - return - - _fwd_kernel[grid]( + # batch, head, + grid = (batch, head, triton.cdiv(max_input_len, BLOCK)) + _fwd_kernel_alibi[grid]( q, k, v, @@ -852,6 +799,7 @@ if triton.__version__ >= "2.1.0": v_scale, b_start_loc, b_seq_len, + alibi_slopes, v_cache.shape[3], k_cache.shape[4], o, @@ -886,9 +834,69 @@ if triton.__version__ >= "2.1.0": BLOCK_DMODEL=Lk, BLOCK_DMODEL_PADDED=Lk_padded, BLOCK_N=BLOCK, - SLIDING_WINDOW=sliding_window, SKIP_DECODE=skip_decode, num_warps=NUM_WARPS, num_stages=1, ) return + + max_seq_len = 0 if max_seq_len is None else max_seq_len + extra_kargs = {} + if current_platform.is_rocm(): + extra_kargs = {"kpack": 2, "waves_per_eu": 2} + + grid = lambda META: (batch, head, + triton.cdiv(max_input_len, META["BLOCK_M"])) + _fwd_kernel[grid]( + q, + k, + v, + k_cache, + v_cache, + b_loc, + sm_scale, + k_scale, + v_scale, + b_start_loc, + b_seq_len, + k_cache.shape[4], + o, + b_loc.stride(0), + b_loc.stride(1), + q.stride(0), + q.stride(1), + q.stride(2), + k.stride(0), + k.stride(1), + k.stride(2), + v.stride(0), + v.stride(1), + v.stride(2), + o.stride(0), + o.stride(1), + o.stride(2), + k_cache.stride(0), + k_cache.stride(1), + k_cache.stride(2), + k_cache.stride(3), + k_cache.stride( + 4), #[num_blocks, num_kv_heads, head_size/x, block_size, x] + v_cache.stride(0), + v_cache.stride(1), + v_cache.stride(2), + v_cache.stride(3), #[num_blocks, num_kv_heads, head_size, block_size] + BLOCK_SIZE=v_cache.shape[3], + num_queries_per_kv=num_queries_per_kv, + IN_PRECISION=IN_PRECISION, + BLOCK_DMODEL=Lk, + BLOCK_DMODEL_PADDED=Lk_padded, + SLIDING_WINDOW=sliding_window, + SKIP_DECODE=skip_decode, + BLOCK_M=128, + BLOCK_N=64, + num_unroll_cache=4, + num_unroll_request=1, + num_warps=4, + num_stages=1, + **extra_kargs) + return
[ "vllm.attention.ops.prefix_prefill.context_attention_fwd" ]
[ "vllm/attention/ops/prefix_prefill.py", "vllm/attention/layer.py", "vllm/model_executor/layers/fused_moe/layer.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
299ebb62b269ce167eb1c71b5e39a1dc1f65ce1c
https://github.com/vllm-project/vllm/pull/16436
2025-04-21
2025-09-07 17:51:17
2025-09-07 17:51:17
[ "Qwen/Qwen2.5-1.5B-Instruct" ]
vllm bench serve --model Qwen/Qwen2.5-1.5B-Instruct --request-rate 1 --num-prompts 100 --random-input-len 1000 --random-output-len 100 --tokenizer Qwen/Qwen2.5-1.5B-Instruct --ignore-eos
true
false
false
true
[Core] Speed up decode by remove synchronizing operation in sampler (#16436)
[Core] Speed up decode by remove synchronizing operation in sampler (#16436)
2025-04-21T18:18:22Z
[ "vllm/model_executor/layers/utils.py" ]
{ "commit_year": 2025, "num_edited_lines": 13, "num_files": 1, "num_hunks": 1, "num_non_test_edited_lines": 13, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/layers/utils.py b/vllm/model_executor/layers/utils.py index a9ef97391..5e56be061 100644 --- a/vllm/model_executor/layers/utils.py +++ b/vllm/model_executor/layers/utils.py @@ -47,10 +47,15 @@ def apply_penalties(logits: torch.Tensor, prompt_tokens_tensor: torch.Tensor, output_tokens_tensor, vocab_size, num_seqs) repetition_penalties = repetition_penalties.unsqueeze(dim=1).repeat( 1, vocab_size) - logits[logits > 0] /= torch.where(prompt_mask | output_mask, - repetition_penalties, 1.0)[logits > 0] - logits[logits <= 0] *= torch.where(prompt_mask | output_mask, - repetition_penalties, 1.0)[logits <= 0] + + # If token appears in prompt or output, apply, otherwise use 1.0 for no-op. + penalties = torch.where(prompt_mask | output_mask, repetition_penalties, + 1.0) + + # If logits are positive, divide by penalty, otherwise multiply by penalty. + scaling = torch.where(logits > 0, 1.0 / penalties, penalties) + logits *= scaling + # We follow the definition in OpenAI API. # Refer to https://platform.openai.com/docs/api-reference/parameter-details logits -= frequency_penalties.unsqueeze(dim=1) * output_bin_counts
[ "vllm.model_executor.layers.utils.apply_penalties" ]
[ "vllm/v1/sample/sampler.py", "vllm/model_executor/layers/sampler.py", "vllm/v1/sample/tpu/sampler.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/openai/serving_completion.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=Qwen/Qwen2.5-1.5B-Instruct,dtype=auto --trust_remote_code --tasks gsm8k --batch_size auto --limit 100
35fad35a485eac9195c510731ba4a9d297dfd963
https://github.com/vllm-project/vllm/pull/15478
2025-03-26
2025-09-07 17:51:35
2025-09-07 17:51:35
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm
true
false
false
true
[V1][Sampler] Faster top-k only implementation (#15478)
[V1][Sampler] Faster top-k only implementation (#15478)
2025-03-26T10:56:47-07:00
[ "tests/v1/sample/test_topk_topp_sampler.py", "vllm/v1/sample/ops/topk_topp_sampler.py", "vllm/v1/sample/sampler.py" ]
{ "commit_year": 2025, "num_edited_lines": 96, "num_files": 3, "num_hunks": 7, "num_non_test_edited_lines": 59, "num_non_test_files": 2, "num_test_files": 1, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/v1/sample/test_topk_topp_sampler.py b/tests/v1/sample/test_topk_topp_sampler.py new file mode 100644 index 000000000..8a5076412 --- /dev/null +++ b/tests/v1/sample/test_topk_topp_sampler.py @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: Apache-2.0 +import torch +from torch import Generator + +from vllm.v1.sample.ops.topk_topp_sampler import apply_top_k_top_p + +DEVICE = "cuda" + +BATCH_SIZE = 1024 +VOCAB_SIZE = 128 * 1024 + + +def test_topk_impl_equivalance(): + + with torch.device(DEVICE): + generator = Generator(device=DEVICE).manual_seed(33) + + logits = torch.rand((BATCH_SIZE, VOCAB_SIZE), generator=generator) + + # Random top-k values between 1 and 9. + k = torch.randint(1, 10, (BATCH_SIZE, ), generator=generator) + + # Set k=vocab_size for ~50% of requests in the batch (top-k disabled). + k.masked_fill_( + torch.randint(0, + 2, (BATCH_SIZE, ), + generator=generator, + dtype=bool), VOCAB_SIZE) + + # Top-k only implementation + result1 = apply_top_k_top_p(logits=logits.clone(), k=k, p=None) + + # Top-p + top-k + no_op_top_p = torch.tensor([1.0]) + result2 = apply_top_k_top_p(logits=logits.clone(), k=k, p=no_op_top_p) + + assert torch.allclose(result1, result2) diff --git a/vllm/v1/sample/ops/topk_topp_sampler.py b/vllm/v1/sample/ops/topk_topp_sampler.py index 1dea71187..5dfcae08b 100644 --- a/vllm/v1/sample/ops/topk_topp_sampler.py +++ b/vllm/v1/sample/ops/topk_topp_sampler.py @@ -19,6 +19,12 @@ except ImportError: class TopKTopPSampler(nn.Module): + """ + Module that performs optional top-k and top-p filtering followed by + weighted random sampling of logits. + + Implementations may update the logits tensor in-place. + """ def __init__(self): super().__init__() @@ -84,7 +90,11 @@ class TopKTopPSampler(nn.Module): k: Optional[torch.Tensor], p: Optional[torch.Tensor], ) -> torch.Tensor: - """PyTorch-native implementation of top-k and top-p sampling.""" + """ + PyTorch-native implementation of top-k and top-p sampling. + + The logits tensor may be updated in-place. + """ logits = apply_top_k_top_p(logits, k, p) probs = logits.softmax(dim=-1, dtype=torch.float32) return random_sample(probs, generators) @@ -136,10 +146,18 @@ def apply_top_k_top_p( ) -> torch.Tensor: """Apply top-k and top-p masks to the logits. - This function sorts the logits tensor, which can be slow for large batches. + If a top-p is used, this function will sort the logits tensor, + which can be slow for large batches. + + The logits tensor may be updated in-place. """ - if k is None and p is None: - return logits + if p is None: + if k is None: + return logits + + # Avoid sorting vocab for top-k only case. + return apply_top_k_only(logits, k) + logits_sort, logits_idx = logits.sort(dim=-1, descending=False) if k is not None: @@ -153,7 +171,7 @@ def apply_top_k_top_p( if p is not None: # Apply top-p. probs_sort = logits_sort.softmax(dim=-1) - probs_sum = probs_sort.cumsum(dim=-1) + probs_sum = torch.cumsum(probs_sort, dim=-1, out=probs_sort) top_p_mask = probs_sum <= 1 - p.unsqueeze(dim=1) # at least one top_p_mask[:, -1] = False @@ -164,6 +182,31 @@ def apply_top_k_top_p( return logits +def apply_top_k_only( + logits: torch.Tensor, + k: torch.Tensor, +) -> torch.Tensor: + """ + Apply top-k mask to the logits. + + This implementation doesn't involve sorting the entire vocab. + + The logits tensor may be updated in-place. + """ + no_top_k_mask = k == logits.shape[1] + # Set non-top-k rows to 1 so that we can gather. + k = k.masked_fill(no_top_k_mask, 1) + max_top_k = k.max() + # topk.values tensor has shape [batch_size, max_top_k]. + # Convert top k to 0-based index in range [0, max_top_k). + k_index = k.sub_(1).unsqueeze(1) + top_k_mask = logits.topk(max_top_k, dim=1).values.gather(1, k_index) + # Handle non-topk rows. + top_k_mask.masked_fill_(no_top_k_mask.unsqueeze(1), -float("inf")) + logits.masked_fill_(logits < top_k_mask, -float("inf")) + return logits + + def random_sample( probs: torch.Tensor, generators: dict[int, torch.Generator], diff --git a/vllm/v1/sample/sampler.py b/vllm/v1/sample/sampler.py index 397a049dc..004f98496 100644 --- a/vllm/v1/sample/sampler.py +++ b/vllm/v1/sample/sampler.py @@ -87,6 +87,12 @@ class Sampler(nn.Module): logits: torch.Tensor, sampling_metadata: SamplingMetadata, ) -> torch.Tensor: + """Sample logits based on sampling metadata. + + The various logits processing functions called in this method + may update the logits tensor in-place. + """ + assert not (sampling_metadata.all_greedy and sampling_metadata.all_random) if sampling_metadata.all_random:
[ "vllm.v1.sample.ops.topk_topp_sampler.apply_top_k_top_p", "vllm.v1.sample.ops.topk_topp_sampler.apply_top_k_only", "vllm.v1.sample.ops.topk_topp_sampler.TopKTopPSampler.forward_native", "vllm.v1.sample.sampler.Sampler.forward" ]
[ "vllm/v1/sample/ops/topk_topp_sampler.py", "vllm/v1/sample/sampler.py", "vllm/model_executor/layers/sampler.py", "vllm/v1/sample/tpu/sampler.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
296f927f2493908984707354e3cc5d7b2e41650b
https://github.com/vllm-project/vllm/pull/14857
2025-03-21
2025-09-07 17:51:42
2025-09-07 17:51:42
[ "ibm-ai-platform/Bamba-9B" ]
python benchmarks/benchmark_serving.py --model ibm-ai-platform/Bamba-9B --dataset-name sharegpt
true
false
false
true
[Model] RE: Mamba2 Prefill Performance Tweaks: Fixing Flurry of Unnecessary Memory Copies (#14857)
[Model] RE: Mamba2 Prefill Performance Tweaks: Fixing Flurry of Unnecessary Memory Copies (#14857)
2025-03-20T19:21:08-07:00
[ "vllm/model_executor/layers/mamba/mamba_mixer2.py" ]
{ "commit_year": 2025, "num_edited_lines": 13, "num_files": 1, "num_hunks": 2, "num_non_test_edited_lines": 13, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/layers/mamba/mamba_mixer2.py b/vllm/model_executor/layers/mamba/mamba_mixer2.py index fec6d6112..d7a45bc51 100644 --- a/vllm/model_executor/layers/mamba/mamba_mixer2.py +++ b/vllm/model_executor/layers/mamba/mamba_mixer2.py @@ -470,10 +470,11 @@ class MambaMixer2(CustomOp): if has_prefill: initial_states = None - if has_initial_states is not None and any(has_initial_states): - for idx in mamba_cache_params.state_indices_tensor[ - ~has_initial_states]: - mamba_cache_params.ssm_state[idx].zero_() + if has_initial_states is not None and torch.any( + has_initial_states): + zero_init_indices = mamba_cache_params.state_indices_tensor[ + ~has_initial_states] + mamba_cache_params.ssm_state[zero_init_indices] = 0 initial_states = mamba_cache_params.ssm_state[ mamba_cache_params.state_indices_tensor] @@ -499,8 +500,8 @@ class MambaMixer2(CustomOp): # update ssm states # - varlen state is a (batch, nheads, headdim, dstate) tensor - for i, idx in enumerate(mamba_cache_params.state_indices_tensor): - mamba_cache_params.ssm_state[idx].copy_(varlen_state[i]) + mamba_cache_params.ssm_state[ + mamba_cache_params.state_indices_tensor] = varlen_state # - reshape hidden_states = scan_output.view(seq_len, -1)
[ "MambaMixer2.forward_cuda", "mamba_mixer2" ]
[ "vllm/model_executor/layers/mamba/mamba_mixer2.py", "vllm/model_executor/models/mamba2.py", "vllm/model_executor/models/mamba_cache.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=ibm-ai-platform/Bamba-9B,dtype=float16 --tasks gsm8k --batch_size auto --limit 100
22d33baca2c0c639cfd45c48e99803e56c3efa74
https://github.com/vllm-project/vllm/pull/15150
2025-03-19
2025-09-07 17:51:45
2025-09-07 17:51:45
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100
true
false
false
true
[FrontEnd][Perf] `merge_async_iterators` fast-path for single-prompt requests (#15150)
[FrontEnd][Perf] `merge_async_iterators` fast-path for single-prompt requests (#15150)
2025-03-19T21:04:41Z
[ "vllm/utils.py" ]
{ "commit_year": 2025, "num_edited_lines": 5, "num_files": 1, "num_hunks": 1, "num_non_test_edited_lines": 5, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/utils.py b/vllm/utils.py index 79787303a..9bc081890 100644 --- a/vllm/utils.py +++ b/vllm/utils.py @@ -411,6 +411,11 @@ async def merge_async_iterators( When it yields, it yields a tuple (i, item) where i is the index of the iterator that yields the item. """ + if len(iterators) == 1: + # Fast-path single iterator case. + async for item in iterators[0]: + yield 0, item + return loop = asyncio.get_running_loop()
[ "vllm.utils.merge_async_iterators" ]
[ "vllm/entrypoints/api_server.py", "vllm/entrypoints/openai/api_server.py", "vllm/entrypoints/openai/serving_completion.py", "vllm/engine/async_llm_engine.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
99abb8b650c66664cdc84d815b7f306f33bd9881
https://github.com/vllm-project/vllm/pull/14930
2025-03-18
2025-09-07 17:51:49
2025-09-07 17:51:49
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --speculative-model '[ngram]' --num-speculative-tokens 3 --dataset-name sharegpt
true
false
false
true
[V1][Spec Decode] Optimize Rejection Sampler with Triton Kernels (#14930)
[V1][Spec Decode] Optimize Rejection Sampler with Triton Kernels (#14930)
2025-03-18T14:31:54-07:00
[ "tests/v1/sample/test_rejection_sampler.py", "vllm/envs.py", "vllm/v1/outputs.py", "vllm/v1/sample/ops/utils.py", "vllm/v1/sample/rejection_sampler.py", "vllm/v1/spec_decode/metadata.py", "vllm/v1/spec_decode/utils.py", "vllm/v1/worker/gpu_model_runner.py" ]
{ "commit_year": 2025, "num_edited_lines": 1329, "num_files": 8, "num_hunks": 34, "num_non_test_edited_lines": 1098, "num_non_test_files": 7, "num_test_files": 1, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/v1/sample/test_rejection_sampler.py b/tests/v1/sample/test_rejection_sampler.py index 84139a40b..8c423e367 100644 --- a/tests/v1/sample/test_rejection_sampler.py +++ b/tests/v1/sample/test_rejection_sampler.py @@ -6,20 +6,23 @@ import torch import torch.nn.functional as F from vllm.v1.sample.metadata import SamplingMetadata -from vllm.v1.sample.rejection_sampler import INVALID_TOKEN_ID, RejectionSampler +from vllm.v1.sample.rejection_sampler import (PLACEHOLDER_TOKEN_ID, + RejectionSampler) +from vllm.v1.spec_decode.metadata import SpecDecodeMetadata -DEVICE = "cpu" +DEVICE = "cuda" @pytest.fixture -def sampler(): +def rejection_sampler(): return RejectionSampler() -def create_logits_tensor(token_ids: list[list[int]], +def create_logits_tensor(output_token_ids: list[list[int]], vocab_size: int = 100) -> torch.Tensor: """Helper function to create logits tensor that will produce desired token ids on argmax""" + token_ids = [tokens[:-1] for tokens in output_token_ids] num_total_tokens = sum(len(tokens) for tokens in token_ids) logits = torch.full((num_total_tokens, vocab_size), -100.0, device=DEVICE) start_loc = 0 @@ -31,15 +34,22 @@ def create_logits_tensor(token_ids: list[list[int]], def create_sampling_metadata( - all_greedy: bool, - generators: Optional[dict[int, Any]] = None) -> SamplingMetadata: + all_greedy: bool, + temperature: Optional[torch.Tensor] = None, + generators: Optional[dict[int, Any]] = None, +) -> SamplingMetadata: """Create a v1 sampling metadata object with all_greedy set to the given value. Either all greedy or all random sampling is used. """ generators = generators or {} + if all_greedy: + temperature = None + else: + assert temperature is not None + return SamplingMetadata( - temperature=torch.tensor([]), + temperature=temperature, all_greedy=all_greedy, all_random=not all_greedy, top_p=None, @@ -61,7 +71,7 @@ def create_sampling_metadata( ########################### Tests for Greedy Sampling ################### -def test_perfect_match(sampler): +def test_perfect_match(rejection_sampler): """Test when output tokens perfectly match speculated tokens""" spec_tokens = [[1, 2, 3]] output_tokens = [[1, 2, 3, 4]] # 4 is the bonus token @@ -70,15 +80,23 @@ def test_perfect_match(sampler): logits = create_logits_tensor(output_tokens) bonus_token_tensor = torch.tensor([output_tokens[0][-1]], device=logits.device) - - output = sampler(spec_tokens, None, bonus_token_tensor, logits, metadata) + spec_decode_metadata = SpecDecodeMetadata.make_dummy(spec_tokens, + device=logits.device) + + output = rejection_sampler( + spec_decode_metadata, + draft_probs=None, + target_logits=logits, + bonus_token_ids=bonus_token_tensor, + sampling_metadata=metadata, + ) expected = torch.tensor([[1, 2, 3, 4]], dtype=torch.int, device=logits.device) assert torch.equal(output, expected) -def test_early_mismatch(sampler): +def test_early_mismatch(rejection_sampler): """Test when there's an early mismatch in tokens""" spec_tokens = [[1, 2, 3]] output_tokens = [[1, 5, 3, 4]] # Mismatch at position 1 @@ -87,15 +105,25 @@ def test_early_mismatch(sampler): logits = create_logits_tensor(output_tokens) bonus_token_tensor = torch.tensor([output_tokens[0][-1]], device=logits.device) - - output = sampler(spec_tokens, None, bonus_token_tensor, logits, metadata) - expected = torch.tensor([[1, 5, INVALID_TOKEN_ID, INVALID_TOKEN_ID]], - dtype=torch.int, - device=logits.device) + spec_decode_metadata = SpecDecodeMetadata.make_dummy(spec_tokens, + device=logits.device) + + output = rejection_sampler( + spec_decode_metadata, + draft_probs=None, + target_logits=logits, + bonus_token_ids=bonus_token_tensor, + sampling_metadata=metadata, + ) + expected = torch.tensor( + [[1, 5, PLACEHOLDER_TOKEN_ID, PLACEHOLDER_TOKEN_ID]], + dtype=torch.int, + device=logits.device, + ) assert torch.equal(output, expected) -def test_multiple_sequences(sampler): +def test_multiple_sequences(rejection_sampler): """Test handling multiple sequences of speculated tokens""" spec_tokens = [[1, 2], [3]] output_tokens = [[1, 2, 5], [3, @@ -105,15 +133,23 @@ def test_multiple_sequences(sampler): logits = create_logits_tensor(output_tokens) bonus_token_tensor = torch.tensor( [output_tokens[0][-1], output_tokens[1][-1]], device=logits.device) - - output = sampler(spec_tokens, None, bonus_token_tensor, logits, metadata) - expected = torch.tensor([[1, 2, 5], [3, 4, INVALID_TOKEN_ID]], + spec_decode_metadata = SpecDecodeMetadata.make_dummy(spec_tokens, + device=logits.device) + + output = rejection_sampler( + spec_decode_metadata, + draft_probs=None, + target_logits=logits, + bonus_token_ids=bonus_token_tensor, + sampling_metadata=metadata, + ) + expected = torch.tensor([[1, 2, 5], [3, 4, PLACEHOLDER_TOKEN_ID]], dtype=torch.int, device=logits.device) assert torch.equal(output, expected) -def test_single_token_sequence(sampler): +def test_single_token_sequence(rejection_sampler): """Test handling sequences with single token""" spec_tokens = [[1]] output_tokens = [[1, 2]] # Single token with bonus token 2 @@ -122,13 +158,21 @@ def test_single_token_sequence(sampler): logits = create_logits_tensor(output_tokens) bonus_token_tensor = torch.tensor([output_tokens[0][-1]], device=logits.device) - - output = sampler(spec_tokens, None, bonus_token_tensor, logits, metadata) + spec_decode_metadata = SpecDecodeMetadata.make_dummy(spec_tokens, + device=logits.device) + + output = rejection_sampler( + spec_decode_metadata, + draft_probs=None, + target_logits=logits, + bonus_token_ids=bonus_token_tensor, + sampling_metadata=metadata, + ) expected = torch.tensor([[1, 2]], dtype=torch.int, device=logits.device) assert torch.equal(output, expected) -def test_empty_sequence(sampler): +def test_empty_sequence(rejection_sampler): """Test handling empty sequence of speculated tokens""" spec_tokens: list[list[int]] = [[]] output_tokens = [[5]] # Just the bonus token @@ -137,13 +181,21 @@ def test_empty_sequence(sampler): logits = create_logits_tensor(output_tokens) bonus_token_tensor = torch.tensor([output_tokens[0][-1]], device=logits.device) - - output = sampler(spec_tokens, None, bonus_token_tensor, logits, metadata) + spec_decode_metadata = SpecDecodeMetadata.make_dummy(spec_tokens, + device=logits.device) + + output = rejection_sampler( + spec_decode_metadata, + draft_probs=None, + target_logits=logits, + bonus_token_ids=bonus_token_tensor, + sampling_metadata=metadata, + ) expected = torch.tensor([[5]], dtype=torch.int, device=logits.device) assert torch.equal(output, expected) -def test_multiple_mismatches(sampler): +def test_multiple_mismatches(rejection_sampler): """Test handling multiple sequences with mismatches""" spec_tokens = [[1, 2, 3], [4, 5, 6]] output_tokens = [[1, 2, 7, 6], [4, 8, 6, @@ -153,12 +205,22 @@ def test_multiple_mismatches(sampler): logits = create_logits_tensor(output_tokens) bonus_token_tensor = torch.tensor( [output_tokens[0][-1], output_tokens[1][-1]], device=logits.device) - - output = sampler(spec_tokens, None, bonus_token_tensor, logits, metadata) - expected = torch.tensor([[1, 2, 7, INVALID_TOKEN_ID], - [4, 8, INVALID_TOKEN_ID, INVALID_TOKEN_ID]], - dtype=torch.int, - device=logits.device) + spec_decode_metadata = SpecDecodeMetadata.make_dummy(spec_tokens, + device=logits.device) + + output = rejection_sampler( + spec_decode_metadata, + draft_probs=None, + target_logits=logits, + bonus_token_ids=bonus_token_tensor, + sampling_metadata=metadata, + ) + expected = torch.tensor( + [[1, 2, 7, PLACEHOLDER_TOKEN_ID], + [4, 8, PLACEHOLDER_TOKEN_ID, PLACEHOLDER_TOKEN_ID]], + dtype=torch.int, + device=logits.device, + ) assert torch.equal(output, expected) @@ -166,18 +228,27 @@ def test_multiple_mismatches(sampler): "spec_tokens,output_tokens,expected", [ ([[1, 2]], [[1, 2, 3]], [[1, 2, 3]]), # Perfect match with bonus - ([[1]], [[2, 3]], [[2, INVALID_TOKEN_ID]]), # First mismatch + ([[1]], [[2, 3]], [[2, PLACEHOLDER_TOKEN_ID]]), # First mismatch ([[1, 2], [3, 4]], [[1, 5, 6], [3, 4, 7]], - [[1, 5, INVALID_TOKEN_ID], [3, 4, 7]]), # Mixed matches + [[1, 5, PLACEHOLDER_TOKEN_ID], [3, 4, 7]]), # Mixed matches ]) -def test_parametrized_cases(sampler, spec_tokens, output_tokens, expected): +def test_parametrized_cases(rejection_sampler, spec_tokens, output_tokens, + expected): """Parametrized test for various matching scenarios""" metadata = create_sampling_metadata(all_greedy=True) logits = create_logits_tensor(output_tokens) bonus_token_tensor = torch.tensor([tokens[-1] for tokens in output_tokens], device=logits.device) - - output = sampler(spec_tokens, None, bonus_token_tensor, logits, metadata) + spec_decode_metadata = SpecDecodeMetadata.make_dummy(spec_tokens, + device=logits.device) + + output = rejection_sampler( + spec_decode_metadata, + draft_probs=None, + target_logits=logits, + bonus_token_ids=bonus_token_tensor, + sampling_metadata=metadata, + ) expected_tensor = torch.tensor(expected, dtype=torch.int, device=logits.device) @@ -190,21 +261,31 @@ def test_parametrized_cases(sampler, spec_tokens, output_tokens, expected): @pytest.mark.parametrize("batch_size", [1, 4, 8]) @pytest.mark.parametrize("frac_seeded", [0.0, 0.5]) @pytest.mark.parametrize("n_rep", [20]) -def test_deterministic_when_seeded(sampler, k: int, vocab_size: int, - batch_size: int, frac_seeded: float, - n_rep: int): - draft_probs = torch.rand(batch_size, k, vocab_size, dtype=torch.float32) - target_probs = torch.rand(batch_size * (k + 1), - vocab_size, - dtype=torch.float32) +def test_deterministic_when_seeded( + rejection_sampler, + k: int, + vocab_size: int, + batch_size: int, + frac_seeded: float, + n_rep: int, +): + num_tokens = batch_size * k + draft_probs = torch.rand(num_tokens, + vocab_size, + dtype=torch.float32, + device=DEVICE) + draft_probs = F.softmax(draft_probs, dim=-1) + target_logits = torch.rand_like(draft_probs) bonus_token_ids = torch.randint(low=0, high=vocab_size, size=(batch_size, 1), - dtype=torch.int64) + dtype=torch.int64, + device=DEVICE) draft_token_ids = torch.randint(low=0, high=vocab_size, size=(batch_size, k), - dtype=torch.int64) + dtype=torch.int64, + device=DEVICE) seeded_mask = torch.rand(batch_size, dtype=torch.float32) <= frac_seeded @@ -215,10 +296,21 @@ def test_deterministic_when_seeded(sampler, k: int, vocab_size: int, for i in range(batch_size) if seeded_mask[i] } + temperature = torch.ones(batch_size, + dtype=torch.float32, + device=DEVICE) sampling_metadata = create_sampling_metadata(all_greedy=False, + temperature=temperature, generators=seeded_seqs) - rep_result = sampler(draft_token_ids.tolist(), draft_probs, - bonus_token_ids, target_probs, sampling_metadata) + spec_decode_metadata = SpecDecodeMetadata.make_dummy( + draft_token_ids.tolist(), device=DEVICE) + rep_result = rejection_sampler( + spec_decode_metadata, + draft_probs=draft_probs, + target_logits=target_logits, + bonus_token_ids=bonus_token_ids, + sampling_metadata=sampling_metadata, + ) results.append(rep_result) @@ -257,10 +349,10 @@ def test_rejection_sampling_approximates_target_distribution(): num_reference_probs = 100 # Prepare draft, target, and reference probability distributions - draft_probs, target_probs = (F.softmax( - torch.rand(vocab_size, dtype=torch.float32), - dim=-1, - ) for _ in range(2)) + draft_probs = F.softmax(torch.rand(vocab_size, dtype=torch.float32), + dim=-1) + target_logits = torch.rand(vocab_size, dtype=torch.float32) + target_probs = F.softmax(target_logits, dim=-1) reference_probs = F.softmax( torch.rand(num_reference_probs, vocab_size, dtype=torch.float32), dim=-1, @@ -273,7 +365,7 @@ def test_rejection_sampling_approximates_target_distribution(): for num_samples in sample_sizes: # Sample using rejection sampling. rej_sample_probs = estimate_rejection_sampling_pdf( - draft_probs, target_probs, k, vocab_size, num_samples) + draft_probs, target_logits, k, vocab_size, num_samples) rej_sample_probs = rej_sample_probs.to(DEVICE) # Average distance from reference probs. @@ -313,7 +405,7 @@ def get_ratio_first_to_last(elements: list[float]) -> float: def estimate_rejection_sampling_pdf( draft_probs: torch.Tensor, - target_probs: torch.Tensor, + target_logits: torch.Tensor, k: int, vocab_size: int, num_samples: int, @@ -323,35 +415,44 @@ def estimate_rejection_sampling_pdf( Args: draft_probs: Draft probability distribution. - target_probs: Target probability distribution. + target_logits: Target logits. num_samples: Number of samples to draw. Returns: Estimated probability distribution of the output tokens. """ - sampler = RejectionSampler() - # Repeat draft probs num_samples times. + rejection_sampler = RejectionSampler() + num_tokens = num_samples * k + # Repeat draft probs num_samples * k times. draft_probs = draft_probs.reshape(1, 1, vocab_size).repeat(num_samples, k, 1) - # Repeat target probs num_samples * (k + 1) times. - target_probs = target_probs.reshape(1, 1, vocab_size).repeat( - num_samples, k + 1, 1).reshape(num_samples * (k + 1), vocab_size) + # Repeat target probs num_tokens times. + target_logits = target_logits.reshape(1, vocab_size).repeat(num_tokens, 1) # Randomly sample draft token ids from draft probs. draft_token_ids = torch.multinomial(draft_probs[:, 0, :], num_samples=k, replacement=True).reshape( num_samples, k) + draft_probs = draft_probs.view(num_tokens, vocab_size) # Bonus tokens not used but required. bonus_token_ids = torch.zeros((1, 1), dtype=torch.int64, device=DEVICE).repeat(num_samples, 1) - sampling_metadata = create_sampling_metadata(all_greedy=False) - output_token_ids = sampler(draft_token_ids.tolist(), draft_probs, - bonus_token_ids, target_probs, - sampling_metadata) + temperature = torch.ones(num_samples, dtype=torch.float32, device=DEVICE) + sampling_metadata = create_sampling_metadata(all_greedy=False, + temperature=temperature) + spec_decode_metadata = SpecDecodeMetadata.make_dummy( + draft_token_ids.tolist(), device=bonus_token_ids.device) + output_token_ids = rejection_sampler( + spec_decode_metadata, + draft_probs=draft_probs, + target_logits=target_logits, + bonus_token_ids=bonus_token_ids, + sampling_metadata=sampling_metadata, + ) output_token_ids = output_token_ids[:, :-1].flatten() hist = torch.histogram(output_token_ids.to(dtype=torch.float, diff --git a/vllm/envs.py b/vllm/envs.py index bf214f314..b2937462a 100644 --- a/vllm/envs.py +++ b/vllm/envs.py @@ -35,7 +35,6 @@ if TYPE_CHECKING: VLLM_TRACE_FUNCTION: int = 0 VLLM_ATTENTION_BACKEND: Optional[str] = None VLLM_USE_FLASHINFER_SAMPLER: Optional[bool] = None - VLLM_USE_FLASHINFER_REJECTION_SAMPLER: bool = False VLLM_FLASHINFER_FORCE_TENSOR_CORES: bool = False VLLM_PP_LAYER_PARTITION: Optional[str] = None VLLM_CPU_KVCACHE_SPACE: int = 0 diff --git a/vllm/v1/outputs.py b/vllm/v1/outputs.py index edae654b5..6f4641717 100644 --- a/vllm/v1/outputs.py +++ b/vllm/v1/outputs.py @@ -46,7 +46,7 @@ class SamplerOutput: # [num_reqs, max_num_generated_tokens] # Different requests can have different number of generated tokens. # All requests are padded to max_num_generated_tokens. - # INVALID_TOKEN_ID (-1 by default) is used for padding. + # PLACEHOLDER_TOKEN_ID (-1 by default) is used for padding. sampled_token_ids: torch.Tensor logprobs_tensors: Optional[LogprobsTensors] diff --git a/vllm/v1/sample/ops/utils.py b/vllm/v1/sample/ops/utils.py new file mode 100644 index 000000000..a54e20603 --- /dev/null +++ b/vllm/v1/sample/ops/utils.py @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: Apache-2.0 +from typing import Union + +import torch + + +def compiled_softmax( + logits: torch.Tensor, + temperature: Union[float, torch.Tensor] = 1.0, +) -> torch.Tensor: + """Faster softmax kernel generated by torch.compile. + + Args: + logits: [n, vocab_size] + temperature: [n] or float + """ + # NOTE(woosuk): Avoid recompilation by marking the first dim as dynamic. + torch._dynamo.mark_dynamic(logits, index=0) + if isinstance(temperature, torch.Tensor): + torch._dynamo.mark_dynamic(temperature, index=0) + return _softmax(logits, temperature) + + +@torch.compile +def _softmax( + logits: torch.Tensor, + temperature: Union[float, torch.Tensor], +) -> torch.Tensor: + logits = logits / temperature + return torch.softmax(logits, dim=-1, dtype=torch.float32) diff --git a/vllm/v1/sample/rejection_sampler.py b/vllm/v1/sample/rejection_sampler.py index 5601c62e9..6284ae4b4 100644 --- a/vllm/v1/sample/rejection_sampler.py +++ b/vllm/v1/sample/rejection_sampler.py @@ -3,25 +3,32 @@ from typing import Optional import torch import torch.nn as nn -from torch.nn.utils.rnn import pad_sequence +import triton +import triton.language as tl from vllm.logger import init_logger from vllm.v1.sample.metadata import SamplingMetadata -from vllm.v1.spec_decode.utils import random_sample +from vllm.v1.sample.ops.utils import compiled_softmax +from vllm.v1.spec_decode.metadata import SpecDecodeMetadata logger = init_logger(__name__) -INVALID_TOKEN_ID = -1 + +PLACEHOLDER_TOKEN_ID: tl.constexpr = -1 +GREEDY_TEMPERATURE: tl.constexpr = -1 +# Maximum number of speculative draft tokens allowed per request in a single +# step. This value is chosen to be large enough to handle typical use cases. +MAX_SPEC_LEN = 32 class RejectionSampler(nn.Module): """ - The implementation strictly follows the algorithm described in + The implementation strictly follows the algorithm described in https://arxiv.org/abs/2211.17192. However, we want to clarify the terminology used in the implementation: - accepted tokens: tokens that are accepted based on the relationship + accepted tokens: tokens that are accepted based on the relationship between the "raw" draft and target probabilities. recovered tokens: tokens that are sampled based on the adjusted probability - distribution, which is derived from both the draft and target + distribution, which is derived from both the draft and target probabilities. bonus tokens: If all proposed tokens are accepted, the bonus token is added to the @@ -31,48 +38,42 @@ class RejectionSampler(nn.Module): sampling process. For example, we can use top_p, top_k sampling for bonus tokens, while spec decode does not support these sampling strategies. - output tokens: - Tokens are finally generated with the rejection sampler. + output tokens: + Tokens are finally generated with the rejection sampler. output tokens = accepted tokens + recovered tokens + bonus tokens """ - def __init__(self): - super().__init__() - def forward( self, - draft_token_ids: list[list[int]], + metadata: SpecDecodeMetadata, + # [num_tokens, vocab_size] draft_probs: Optional[torch.Tensor], - bonus_token_ids_tensor: torch.Tensor, # [batch_size, 1] - target_probs: torch.Tensor, # [num_total_tokens, vocab_size] + # [num_tokens, vocab_size] + target_logits: torch.Tensor, + # [batch_size, 1] + bonus_token_ids: torch.Tensor, sampling_metadata: SamplingMetadata, ) -> torch.Tensor: ''' Args: - draft_token_ids (List[List[int]]): - A 2D list of token IDs for each request in the batch. - Each request might have different number of draft tokens. - It may also contain empty lists for requests that have - no draft tokens. + metadata: + Metadata for spec decoding. draft_probs (Optional[torch.Tensor]): Probability distribution for the draft tokens. Shape is - [batch_size, max_spec_len, vocab_size]. Can be None if - probabilities are not provided, which is the case for - ngram spec decode. + [num_tokens, vocab_size]. Can be None if probabilities are + not provided, which is the case for ngram spec decode. + target_logits (torch.Tensor): + Target model's logits probability distribution. + Shape is [num_tokens, vocab_size]. Here, probabilities from + different requests are flattened into a single tensor because + this is the shape of the output logits. bonus_token_ids_tensor (torch.Tensor): - A tensor containing bonus tokens. Shape is [batch_size, 1]. - Bonus tokens are added to the end of the sequence if all - proposed tokens are accepted. We generate the bonus tokens - outside of the rejection sampler with the default sampling - strategy. It allows for more flexibility in the sampling + A tensor containing bonus tokens. Shape is [batch_size, 1]. + Bonus tokens are added to the end of the sequence if all + proposed tokens are accepted. We generate the bonus tokens + outside of the rejection sampler with the default sampling + strategy. It allows for more flexibility in the sampling process such as top_p, top_k sampling. - target_probs (torch.Tensor): - Target model probability distribution. - Shape is [num_total_tokens, vocab_size]. num_total_tokens - is the total number of tokens from all requests. Here, - probabilities from different requests are flattened into - a single tensor because this is the shape of the output - logits. sampling_metadata (SamplingMetadata): Additional metadata needed for sampling, such as temperature, top-k/top-p parameters, or other relevant information. @@ -80,268 +81,481 @@ class RejectionSampler(nn.Module): output_token_ids (torch.Tensor): A tensor containing the final output token IDs. ''' - - # NOTE: The following input preparationg can be moved - # to the model runner with a persistent manner for better - # performance. - # Convert draft token IDs to a tensor, split by sample_lens, then pad. - draft_token_ids = [ - torch.tensor(x, dtype=int, device='cpu') for x in draft_token_ids - ] - draft_token_ids_tensor = pad_sequence(draft_token_ids, - batch_first=True, - padding_value=INVALID_TOKEN_ID) - - # NOTE: CPU <-> GPU synchronization happens here. - draft_token_ids_tensor = draft_token_ids_tensor.to(target_probs.device) - - # Create one-hot tensor for draft token ids. - # This is used for ngram where we don't have draft_probs. - if draft_probs is None and not sampling_metadata.all_greedy: - vocab_size = target_probs.size(-1) - draft_probs = _create_greedy_token_probs(draft_token_ids_tensor, - vocab_size, - target_probs.device) - sample_lens = [len(x) + 1 for x in draft_token_ids] - target_probs = _convert_2d_probs(target_probs, sample_lens) - - return self.forward_native(draft_token_ids_tensor, draft_probs, - bonus_token_ids_tensor, target_probs, - sampling_metadata) - - # TODO: The following method can be optimized for better performance. - def forward_native( - self, - draft_token_ids_tensor: torch.Tensor, - # [batch_size, max_spec_len, vocab_size] - draft_probs: Optional[torch.Tensor], - bonus_token_ids_tensor: torch.Tensor, - # [batch_size, max_spec_len + 1, vocab_size] - target_probs: torch.Tensor, - sampling_metadata: SamplingMetadata, - ) -> torch.Tensor: - # Add 1 to include the 'bonus' token. - if sampling_metadata.all_greedy: - # Produce a mask that remains 1 (True) until the first - # mismatch (cumprod turns 0 after a mismatch). - target_token_ids_tensor = target_probs.argmax(dim=-1) - accept_mask = (target_token_ids_tensor[:, :-1] == - draft_token_ids_tensor).cumprod(dim=1) - - # Identify valid positions (non-padding). - valid_mask = target_token_ids_tensor != INVALID_TOKEN_ID - # Generate mask with bonus token. - generate_mask = torch.cat([ - accept_mask, - torch.zeros(accept_mask.size(0), 1, device=accept_mask.device) - ], - dim=1).to(torch.bool) & valid_mask - zeros_mask = (generate_mask == 0) - first_zero_idx = zeros_mask.float().argmax(dim=1) - # Figure out which rows actually contain at least one zero. - rows_with_zero = zeros_mask.any(dim=1) - # Use indexing to set the first zero in each of those rows to 1. - generate_mask[rows_with_zero, first_zero_idx[rows_with_zero]] = 1 - - output_token_ids = target_token_ids_tensor - output_token_ids[~generate_mask] = INVALID_TOKEN_ID - else: - # Reference: https://arxiv.org/pdf/2211.17192 - # 1. Extract the probabilities of the draft tokens. - # [batch_size, max_spec_len] - batch_size = draft_token_ids_tensor.size(0) - max_spec_len = draft_token_ids_tensor.size(1) - invalid_idx = draft_token_ids_tensor == INVALID_TOKEN_ID - draft_token_ids_tensor[invalid_idx] = 0 - assert draft_probs is not None - draft_token_probs = draft_probs.gather( - dim=-1, index=draft_token_ids_tensor.unsqueeze(-1)).squeeze(-1) - target_token_probs = target_probs.gather( - dim=-1, index=draft_token_ids_tensor.unsqueeze(-1)).squeeze(-1) - # Force the probabilities of invalid tokens to inf - # so that they are not accepted. - draft_token_probs[invalid_idx] = float('inf') - - # 2. Generate uniform samples. - # [batch_size, max_spec_len + 1] - uniform_samples = _create_uniform_samples( - sampling_metadata.generators, batch_size, max_spec_len, - target_probs.device) - - # 3. Accept or reject the samples. - # [batch_size, max_spec_len] - # If the draft token probabilities are 0, set them to the smallest - # positive normal value representable by float32. - safe_draft_probs = torch.where(draft_token_probs > 0, - draft_token_probs, - torch.finfo(torch.float32).tiny) - accepted = uniform_samples <= target_token_probs / safe_draft_probs - accept_mask = accepted.cumprod(dim=1) - # Set the token ids to the draft token ids if accepted, otherwise - # set them to INVALID_TOKEN_ID. - accepted_token_ids = (draft_token_ids_tensor * accept_mask + - INVALID_TOKEN_ID * (1 - accept_mask)) - - # 4. Adjust the distribution for the recovered tokens. - # Clamp the bonus probabilities to the smallest positive normal - # value representable by float32. - bonus_prob = torch.clamp(target_probs[:, :-1, :] - draft_probs, - min=torch.finfo(torch.float32).tiny) - normalized_bonus_prob = bonus_prob / bonus_prob.sum(dim=-1, - keepdim=True) - - # 5. Sample recovered token ids. - recovered_token_ids = random_sample( - normalized_bonus_prob, - sampling_metadata.generators).reshape(batch_size, max_spec_len) - - # 6. Get the final output token ids. - # output_token_ids = accepted_token_ids + - # recovered_token_ids + - # bonus_token_id - recovered_bonus_token_ids = torch.cat( - [recovered_token_ids, bonus_token_ids_tensor], dim=1) - # Generate mask with bonus tokens. - generate_mask = torch.cat([ - accept_mask, - torch.zeros(batch_size, 1, device=accept_mask.device) - ], - dim=1).to(torch.bool) - zeros_mask = (generate_mask == 0) - first_zero_idx = zeros_mask.float().argmax(dim=1) - output_token_ids = torch.cat([ - accepted_token_ids, - torch.full((batch_size, 1), - fill_value=INVALID_TOKEN_ID, - device=accept_mask.device) - ], - dim=1) - output_token_ids[torch.arange(batch_size), - first_zero_idx] = recovered_bonus_token_ids[ - torch.arange(batch_size), first_zero_idx] - + assert metadata.max_spec_len <= MAX_SPEC_LEN + # [num_tokens, vocab_size] + target_probs = compute_probs( + target_logits, + metadata.cu_num_draft_tokens, + sampling_metadata, + ) + + output_token_ids = rejection_sample( + metadata.draft_token_ids, + metadata.num_draft_tokens, + metadata.max_spec_len, + metadata.cu_num_draft_tokens, + draft_probs, + target_probs, + bonus_token_ids, + sampling_metadata, + ) return output_token_ids - def compute_probs(self, logits: torch.Tensor, - sampling_metadata: SamplingMetadata, - sample_lens: list[int]) -> torch.Tensor: - """ - Compute probability distribution from logits based on sampling metadata. - - This function applies temperature scaling to the logits and converts - them to probabilities using softmax. Note that division by - temperature is not performed inplace to preserve the original logits - tensor, which will be used by the original sampler to get bonus tokens. - - Args: - logits: Input logits tensor to be converted to probabilities - sampling_metadata: Metadata containing sampling parameters such - as temperature and whether greedy sampling is used - sample_lens: List of sample lengths used for repeating - temperature values - - Returns: - torch.Tensor: Probability distribution (softmax of scaled logits) - if non-greedy sampling is used, otherwise returns the - original logits - """ + @staticmethod + def parse_output( + output_token_ids: torch.Tensor, + vocab_size: int, + ) -> list[list[int]]: + output_token_ids_np = output_token_ids.cpu().numpy() + # Create mask for valid tokens. + valid_mask = ((output_token_ids_np != PLACEHOLDER_TOKEN_ID) & + (output_token_ids_np < vocab_size)) + outputs = [ + row[valid_mask[i]].tolist() + for i, row in enumerate(output_token_ids_np) + ] + return outputs + + +def rejection_sample( + # [num_tokens] + draft_token_ids: torch.Tensor, + # [batch_size] + num_draft_tokens: list[int], + max_spec_len: int, + # [batch_size] + cu_num_draft_tokens: torch.Tensor, + # [num_tokens, vocab_size] + draft_probs: Optional[torch.Tensor], + # [num_tokens, vocab_size] + target_probs: torch.Tensor, + # [batch_size, 1] + bonus_token_ids: torch.Tensor, + sampling_metadata: SamplingMetadata, +) -> torch.Tensor: + assert draft_token_ids.ndim == 1 + assert draft_probs is None or draft_probs.ndim == 2 + assert cu_num_draft_tokens.ndim == 1 + assert target_probs.ndim == 2 + + batch_size = len(num_draft_tokens) + num_tokens = draft_token_ids.shape[0] + vocab_size = target_probs.shape[-1] + device = target_probs.device + assert draft_token_ids.is_contiguous() + assert draft_probs is None or draft_probs.is_contiguous() + assert target_probs.is_contiguous() + assert bonus_token_ids.is_contiguous() + assert target_probs.shape == (num_tokens, vocab_size) + + # Create output buffer. + output_token_ids = torch.empty( + (batch_size, max_spec_len + 1), + dtype=torch.int32, # Consistent with SamplerOutput.sampled_token_ids. + device=device, + ) + output_token_ids.fill_(PLACEHOLDER_TOKEN_ID) + + if sampling_metadata.all_greedy: + is_greedy = None + else: + is_greedy = sampling_metadata.temperature == GREEDY_TEMPERATURE + if not sampling_metadata.all_random: + # Rejection sampling for greedy sampling requests. + target_argmax = target_probs.argmax(dim=-1) + rejection_greedy_sample_kernel[(batch_size, )]( + output_token_ids, + cu_num_draft_tokens, + draft_token_ids, + target_argmax, + bonus_token_ids, + is_greedy, + max_spec_len, + num_warps=1, + ) if sampling_metadata.all_greedy: - return logits - assert sampling_metadata.temperature is not None - # We should optimize the following code as - # it will cause CPU -> GPU synchronization. - temperature = torch.repeat_interleave( - sampling_metadata.temperature, - torch.tensor(sample_lens, - device=sampling_metadata.temperature.device)) - temperature = temperature.unsqueeze(dim=1) - logits = logits / temperature - return logits.softmax(dim=-1, dtype=torch.float32) - - -def _create_greedy_token_probs( - token_ids: torch.Tensor, - vocab_size: int, - out_device: torch.device, + return output_token_ids + + # Generate uniform probabilities for rejection sampling. + # [num_tokens] + uniform_probs = generate_uniform_probs( + num_tokens, + num_draft_tokens, + sampling_metadata.generators, + device, + ) + + # Sample recovered tokens for each position. + # [num_tokens] + recovered_token_ids = sample_recovered_tokens( + max_spec_len, + num_draft_tokens, + cu_num_draft_tokens, + draft_token_ids, + draft_probs, + target_probs, + sampling_metadata, + device, + ) + + # Rejection sampling for random sampling requests. + rejection_random_sample_kernel[(batch_size, )]( + output_token_ids, + cu_num_draft_tokens, + draft_token_ids, + draft_probs, + target_probs, + bonus_token_ids, + recovered_token_ids, + uniform_probs, + is_greedy, + max_spec_len, + vocab_size, + IS_NGRAM=draft_probs is None, + num_warps=1, + ) + return output_token_ids + + +def compute_probs( + logits: torch.Tensor, # [num_tokens, vocab_size] + cu_num_draft_tokens: torch.Tensor, # [batch_size] + sampling_metadata: SamplingMetadata, ) -> torch.Tensor: - batch_size, num_tokens = token_ids.shape - - token_probs = torch.zeros(batch_size, - num_tokens, - vocab_size, - dtype=torch.float, - device=out_device) - - # Ignore INVALID_TOKEN_ID. - valid_mask = (token_ids != INVALID_TOKEN_ID) - valid_indices = token_ids.clone() - valid_indices[~valid_mask] = 0 - - token_probs.scatter_(dim=2, - index=valid_indices.unsqueeze(-1), - src=valid_mask.unsqueeze(-1).float()) - - return token_probs - - -def _convert_2d_probs( - probs: torch.Tensor, # [num_total_tokens, vocab_size] - sample_lens: list[int]) -> torch.Tensor: + """Compute probability distribution from logits based on sampling metadata. + + This function applies temperature scaling to the logits and converts + them to probabilities using softmax. For greedy decoding, it returns + the original logits. + + Args: + logits: Input logits tensor to be converted to probabilities. + cu_num_draft_tokens: Cumulative number of draft tokens. + sampling_metadata: Metadata containing sampling parameters such as + temperature and whether greedy sampling is used. + + Returns: + torch.Tensor: Probability distribution (softmax of scaled logits) + if non-greedy sampling is used, otherwise returns the + original logits. """ - Converts a 2D tensor of probabilities to a 3D tensor with padding. - [num_total_tokens, vocab_size] -> - [batch_size, max_spec_len + 1, vocab_size] + assert logits.ndim == 2 + assert cu_num_draft_tokens.ndim == 1 + if sampling_metadata.all_greedy: + return logits + + num_tokens = logits.shape[0] + batch_size = cu_num_draft_tokens.shape[0] + expanded_temperature = torch.empty( + (num_tokens, 1), + dtype=torch.float32, + device=logits.device, + ) + expand_kernel[(batch_size, )]( + expanded_temperature, + sampling_metadata.temperature, + cu_num_draft_tokens, + GREEDY_TEMPERATURE, # replace_from + 1, # replace_to + MAX_NUM_TOKENS=MAX_SPEC_LEN, + num_warps=1, + ) + output_prob = compiled_softmax(logits, expanded_temperature) + return output_prob + + +def generate_uniform_probs( + num_tokens: int, + num_draft_tokens: list[int], + generators: dict[int, torch.Generator], + device: torch.device, +) -> torch.Tensor: """ - cumulative_lens = torch.cumsum(torch.tensor(sample_lens, - device=probs.device), - dim=0) - split_indices = cumulative_lens[:-1].tolist() # Exclude last index - - # Split into chunks without loops - chunks = torch.tensor_split(probs, split_indices, dim=0) - - # Pad all sequences to maximum length - padded_probs = pad_sequence(chunks, batch_first=True, padding_value=0.0) - return padded_probs - - -def _create_uniform_samples(seeded_seqs: dict[int, torch.Generator], - batch_size: int, k: int, - device: torch.device) -> torch.Tensor: + Generates a batch of uniform random samples, with optional seeding + if available. + + This method creates a tensor of shape `(num_tokens, )` filled + with uniform random values in the range [0, 1). If `generators` is provided, + the requests with their own seeds will use the provided `torch.Generator` + for reproducibility. The samples for the other requests will be generated + without a seed. + + Args: + num_tokens : int + Total number of tokens. + num_draft_tokens : List[List[int]] + Number of draft tokens per request. + generators : Optional[Dict[int, torch.Generator]] + A dictionary mapping indices in the batch to + `torch.Generator` objects. + device : torch.device + The device on which to allocate the tensor. + Returns: + uniform_rand : torch.Tensor + A tensor of shape `(num_tokens, )` containing uniform + random values in the range [0, 1). """ - Generates a batch of uniform random samples, with optional seeding - for specific sequences. - - This method creates a tensor of shape `(batch_size, k)` filled - with uniform random values in the range [0, 1). If `seeded_seqs` - is provided, the sequences corresponding to specific indices - will be generated using the provided `torch.Generator` for - reproducibility. The other sequences will be generated without - a seed. - - Args: - seeded_seqs : Optional[Dict[int, torch.Generator]] - A dictionary mapping indices in the batch to - `torch.Generator` objects. - batch_size : int - The number of sequences to generate. - k : int - The number of random samples per sequence. - device : torch.device - The device on which to allocate the tensor. - - Returns: - uniform_rand : torch.Tensor - A tensor of shape `(batch_size, k)` containing uniform - random values in the range [0, 1). - """ - - uniform_rand = torch.rand(batch_size, - k, - dtype=torch.float32, - device=device) - # Apply seeded generators only where needed - if seeded_seqs: - for idx, generator in seeded_seqs.items(): - uniform_rand[idx].uniform_(0, 1, generator=generator) - return uniform_rand + uniform_probs = torch.rand( + (num_tokens, ), + dtype=torch.float32, + device=device, + ) + start_idx = 0 + for req_idx, n in enumerate(num_draft_tokens): + # Do not generate random numbers for requests with no draft tokens. + # This can be important for reproducibility. + if n == 0: + continue + end_idx = start_idx + n + generator = generators.get(req_idx) + if generator is not None: + uniform_probs[start_idx:end_idx].uniform_(generator=generator) + start_idx = end_idx + return uniform_probs + + +def sample_recovered_tokens( + max_spec_len: int, + num_draft_tokens: list[int], + # [batch_size] + cu_num_draft_tokens: torch.Tensor, + # [num_tokens] + draft_token_ids: torch.Tensor, + # [num_tokens, vocab_size] + draft_probs: Optional[torch.Tensor], + # [num_tokens, vocab_size] + target_probs: torch.Tensor, + sampling_metadata: SamplingMetadata, + device: torch.device, +) -> torch.Tensor: + # NOTE(woosuk): Create only one distribution for each request. + batch_size = len(num_draft_tokens) + vocab_size = target_probs.shape[-1] + q = torch.empty( + (batch_size, vocab_size), + dtype=torch.float32, + device=device, + ) + q.exponential_() + for i, generator in sampling_metadata.generators.items(): + # Do not generate random numbers for requests with no draft tokens. + # This can be important for reproducibility. + if num_draft_tokens[i] > 0: + q[i].exponential_(generator=generator) + + recovered_token_ids = torch.empty_like(draft_token_ids) + sample_recovered_tokens_kernel[(batch_size, max_spec_len)]( + recovered_token_ids, + cu_num_draft_tokens, + draft_token_ids, + draft_probs, + target_probs, + q, + vocab_size, + triton.next_power_of_2(vocab_size), + IS_NGRAM=draft_probs is None, + ) + return recovered_token_ids + + +# NOTE(woosuk): Avoid specialization to prevent unnecessary recompilation. +@triton.jit(do_not_specialize=["max_spec_len"]) +def rejection_greedy_sample_kernel( + output_token_ids_ptr, # [batch_size, max_spec_len + 1] + cu_num_draft_tokens_ptr, # [batch_size] + draft_token_ids_ptr, # [num_tokens] + target_argmax_ptr, # [num_tokens] + bonus_token_ids_ptr, # [batch_size] + is_greedy_ptr, # [batch_size] or None + max_spec_len, +): + req_idx = tl.program_id(0) + # FIXME(woosuk): Because is_greedy_ptr is not None at profiling run, + # re-compilation may happen during runtime when is_greedy_ptr is None. + if is_greedy_ptr is None: + is_greedy = True + else: + is_greedy = tl.load(is_greedy_ptr + req_idx) + if not is_greedy: + # Early exit for non-greedy sampling requests. + return + + if req_idx == 0: + start_idx = 0 + else: + start_idx = tl.load(cu_num_draft_tokens_ptr + req_idx - 1) + end_idx = tl.load(cu_num_draft_tokens_ptr + req_idx) + num_draft_tokens = end_idx - start_idx + + rejected = False + for pos in range(num_draft_tokens): + if not rejected: + draft_token_id = tl.load(draft_token_ids_ptr + start_idx + pos) + target_argmax_id = tl.load(target_argmax_ptr + start_idx + pos) + tl.store(output_token_ids_ptr + req_idx * (max_spec_len + 1) + pos, + target_argmax_id) + if draft_token_id != target_argmax_id: + # Reject. + rejected = True + + if not rejected: + # If all tokens are accepted, append the bonus token. + bonus_token_id = tl.load(bonus_token_ids_ptr + req_idx) + tl.store( + output_token_ids_ptr + req_idx * (max_spec_len + 1) + + num_draft_tokens, bonus_token_id) + + +# NOTE(woosuk): Avoid specialization to prevent unnecessary recompilation. +@triton.jit(do_not_specialize=["max_spec_len"]) +def rejection_random_sample_kernel( + output_token_ids_ptr, # [batch_size, max_spec_len + 1] + cu_num_draft_tokens_ptr, # [batch_size] + draft_token_ids_ptr, # [num_tokens] + draft_probs_ptr, # [num_tokens, vocab_size] or None + target_probs_ptr, # [num_tokens, vocab_size] + bonus_token_ids_ptr, # [batch_size] + recovered_token_ids_ptr, # [num_tokens] + uniform_probs_ptr, # [num_tokens] + is_greedy_ptr, # [batch_size] + max_spec_len, + vocab_size, + IS_NGRAM: tl.constexpr, +): + req_idx = tl.program_id(0) + is_greedy = tl.load(is_greedy_ptr + req_idx) + if is_greedy: + # Early exit for greedy sampling requests. + return + + if req_idx == 0: + start_idx = 0 + else: + start_idx = tl.load(cu_num_draft_tokens_ptr + req_idx - 1) + end_idx = tl.load(cu_num_draft_tokens_ptr + req_idx) + num_draft_tokens = end_idx - start_idx + + rejected = False + for pos in range(num_draft_tokens): + if not rejected: + draft_token_id = tl.load(draft_token_ids_ptr + start_idx + pos) + if IS_NGRAM: + draft_prob = 1 + else: + draft_prob = tl.load(draft_probs_ptr + + (start_idx + pos) * vocab_size + + draft_token_id) + target_prob = tl.load(target_probs_ptr + + (start_idx + pos) * vocab_size + + draft_token_id) + uniform_prob = tl.load(uniform_probs_ptr + start_idx + pos) + # NOTE(woosuk): While the draft probability should never be 0, + # we check it to avoid NaNs. If it happens to be 0, we reject. + if draft_prob > 0 and target_prob / draft_prob >= uniform_prob: + # Accept. + token_id = draft_token_id + else: + # Reject. Use recovered token. + rejected = True + token_id = tl.load(recovered_token_ids_ptr + start_idx + pos) + tl.store(output_token_ids_ptr + req_idx * (max_spec_len + 1) + pos, + token_id) + + if not rejected: + # If all tokens are accepted, append the bonus token. + bonus_token_id = tl.load(bonus_token_ids_ptr + req_idx) + tl.store( + output_token_ids_ptr + req_idx * (max_spec_len + 1) + + num_draft_tokens, bonus_token_id) + + +# NOTE(woosuk): Avoid specialization to prevent unnecessary recompilation. +@triton.jit(do_not_specialize=["replace_from", "replace_to"]) +def expand_kernel( + output_ptr, # [num_tokens] + input_ptr, # [batch_size] + cu_num_tokens_ptr, # [batch_size] + replace_from, + replace_to, + MAX_NUM_TOKENS: tl.constexpr, +): + req_idx = tl.program_id(0) + if req_idx == 0: # noqa: SIM108 + start_idx = 0 + else: + start_idx = tl.load(cu_num_tokens_ptr + req_idx - 1) + end_idx = tl.load(cu_num_tokens_ptr + req_idx) + num_tokens = end_idx - start_idx + + src_val = tl.load(input_ptr + req_idx) + src_val = tl.where(src_val == replace_from, replace_to, src_val) + offset = tl.arange(0, MAX_NUM_TOKENS) + tl.store(output_ptr + start_idx + offset, + src_val, + mask=offset < num_tokens) + + +@triton.jit +def sample_recovered_tokens_kernel( + output_token_ids_ptr, # [num_tokens] + cu_num_draft_tokens_ptr, # [batch_size] + draft_token_ids_ptr, # [num_tokens] + draft_probs_ptr, # [num_tokens, vocab_size] or None + target_probs_ptr, # [num_tokens, vocab_size] + q_ptr, # [batch_size, vocab_size] + vocab_size, + PADDED_VOCAB_SIZE: tl.constexpr, + IS_NGRAM: tl.constexpr, +): + req_idx = tl.program_id(0) + if req_idx == 0: + start_idx = 0 + else: + start_idx = tl.load(cu_num_draft_tokens_ptr + req_idx - 1) + end_idx = tl.load(cu_num_draft_tokens_ptr + req_idx) + num_draft_tokens = end_idx - start_idx + + # Early exit for out-of-range positions. + pos = tl.program_id(1) + if pos >= num_draft_tokens: + return + + vocab_offset = tl.arange(0, PADDED_VOCAB_SIZE) + if IS_NGRAM: + draft_token_id = tl.load(draft_token_ids_ptr + start_idx + pos) + orig_prob = tl.load(target_probs_ptr + (start_idx + pos) * vocab_size + + draft_token_id) + # Temporarily zero out the probability of the draft token. + # This is essentially the same as target_prob - draft_prob, except that + # n-gram does not have draft_prob. We regard it as 1. + tl.store( + target_probs_ptr + (start_idx + pos) * vocab_size + draft_token_id, + 0) + prob = tl.load(target_probs_ptr + (start_idx + pos) * vocab_size + + vocab_offset, + mask=vocab_offset < vocab_size, + other=0) + else: + draft_prob = tl.load(draft_probs_ptr + (start_idx + pos) * vocab_size + + vocab_offset, + mask=vocab_offset < vocab_size, + other=0) + target_prob = tl.load(target_probs_ptr + + (start_idx + pos) * vocab_size + vocab_offset, + mask=vocab_offset < vocab_size, + other=0) + prob = tl.maximum(target_prob - draft_prob, 0) + # NOTE(woosuk): We don't need `prob = prob / tl.sum(prob)` here because + # `tl.argmax` will select the maximum value. + + q = tl.load(q_ptr + req_idx * vocab_size + vocab_offset, + mask=vocab_offset < vocab_size, + other=float("-inf")) + recovered_id = tl.argmax(prob / q, axis=-1) + tl.store(output_token_ids_ptr + start_idx + pos, recovered_id) + + if IS_NGRAM: + # Restore the original probability. + tl.store( + target_probs_ptr + (start_idx + pos) * vocab_size + draft_token_id, + orig_prob) diff --git a/vllm/v1/spec_decode/metadata.py b/vllm/v1/spec_decode/metadata.py new file mode 100644 index 000000000..1cf650d5f --- /dev/null +++ b/vllm/v1/spec_decode/metadata.py @@ -0,0 +1,61 @@ +# SPDX-License-Identifier: Apache-2.0 +from dataclasses import dataclass + +import numpy as np +import torch + + +@dataclass +class SpecDecodeMetadata: + + # [num_tokens] + draft_token_ids: torch.Tensor + # [batch_size] + num_draft_tokens: list[int] + # [batch_size] + cu_num_draft_tokens: torch.Tensor + # [num_tokens] + target_logits_indices: torch.Tensor + # [batch_size] + bonus_logits_indices: torch.Tensor + # [num_tokens + batch_size] + logits_indices: torch.Tensor + + def __post_init__(self): + self.max_spec_len = max(self.num_draft_tokens) + + @classmethod + def make_dummy( + cls, + draft_token_ids: list[list[int]], + device: torch.device, + ) -> "SpecDecodeMetadata": + batch_size = len(draft_token_ids) + num_draft_tokens = [len(ids) for ids in draft_token_ids] + flattened_draft_token_ids = sum(draft_token_ids, []) + num_tokens = len(flattened_draft_token_ids) + + draft_token_ids_tensor = torch.tensor(flattened_draft_token_ids, + dtype=torch.int32, + device=device) + cu_num_draft_tokens = np.cumsum(num_draft_tokens, dtype=np.int32) + cu_num_draft_tokens_tensor = torch.from_numpy(cu_num_draft_tokens).to( + device) + + target_logits_indices = torch.zeros(num_tokens, + dtype=torch.int32, + device=device) + bonus_logits_indices = torch.zeros(batch_size, + dtype=torch.int32, + device=device) + logits_indices = torch.zeros(num_tokens + batch_size, + dtype=torch.int32, + device=device) + return cls( + draft_token_ids=draft_token_ids_tensor, + num_draft_tokens=num_draft_tokens, + cu_num_draft_tokens=cu_num_draft_tokens_tensor, + target_logits_indices=target_logits_indices, + bonus_logits_indices=bonus_logits_indices, + logits_indices=logits_indices, + ) diff --git a/vllm/v1/spec_decode/utils.py b/vllm/v1/spec_decode/utils.py index 584140136..d5329ef7b 100644 --- a/vllm/v1/spec_decode/utils.py +++ b/vllm/v1/spec_decode/utils.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: Apache-2.0 -from vllm.v1.sample.ops.topk_topp_sampler import random_sample # noqa from vllm.v1.worker.gpu_input_batch import InputBatch diff --git a/vllm/v1/worker/gpu_model_runner.py b/vllm/v1/worker/gpu_model_runner.py index 66015382b..657333c6d 100644 --- a/vllm/v1/worker/gpu_model_runner.py +++ b/vllm/v1/worker/gpu_model_runner.py @@ -34,7 +34,8 @@ from vllm.v1.kv_cache_interface import (FullAttentionSpec, KVCacheConfig, from vllm.v1.outputs import (EMPTY_MODEL_RUNNER_OUTPUT, LogprobsTensors, ModelRunnerOutput) from vllm.v1.sample.metadata import SamplingMetadata -from vllm.v1.sample.rejection_sampler import INVALID_TOKEN_ID, RejectionSampler +from vllm.v1.sample.rejection_sampler import RejectionSampler +from vllm.v1.spec_decode.metadata import SpecDecodeMetadata from vllm.v1.spec_decode.ngram_proposer import NgramProposer from vllm.v1.spec_decode.utils import is_spec_decode_supported from vllm.v1.utils import bind_kv_cache @@ -149,7 +150,6 @@ class GPUModelRunner(LoRAModelRunnerMixin): self.use_spec_decode = False if self.speculative_config: self.use_spec_decode = True - self.rejection_sampler = RejectionSampler() # TODO: find a better way to check if we are using ngram. assert self.speculative_config.ngram_prompt_lookup_min, \ "Currently, only ngram spec decode is supported in V1." @@ -162,6 +162,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): self.speculative_config.ngram_prompt_lookup_min, self.speculative_config.num_speculative_tokens, ) + self.rejection_sampler = RejectionSampler() # Request states. self.requests: dict[str, CachedRequestState] = {} @@ -452,7 +453,8 @@ class GPUModelRunner(LoRAModelRunnerMixin): def _prepare_inputs( self, scheduler_output: "SchedulerOutput", - ) -> tuple[FlashAttentionMetadata, torch.Tensor]: + ) -> tuple[FlashAttentionMetadata, torch.Tensor, + Optional[SpecDecodeMetadata]]: total_num_scheduled_tokens = scheduler_output.total_num_scheduled_tokens assert total_num_scheduled_tokens > 0 num_reqs = self.input_batch.num_reqs @@ -577,22 +579,33 @@ class GPUModelRunner(LoRAModelRunnerMixin): use_spec_decode = len( scheduler_output.scheduled_spec_decode_tokens) > 0 - if use_spec_decode: - logits_indices = self._calc_spec_decode_metadata( - scheduler_output, cu_num_tokens) - else: + if not use_spec_decode: # NOTE(woosuk): Due to chunked prefills, the batch may contain # partial requests. While we should not sample any token # from these partial requests, we do so for simplicity. # We will ignore the sampled tokens from the partial requests. # TODO: Support prompt logprobs. logits_indices = attn_metadata.query_start_loc[1:] - 1 + spec_decode_metadata = None + else: + # Get the number of draft tokens for each request. + # Iterate over the dictionary rather than all requests since not all + # requests have draft tokens. + num_draft_tokens = np.zeros(num_reqs, dtype=np.int32) + for req_id, draft_token_ids in ( + scheduler_output.scheduled_spec_decode_tokens.items()): + req_idx = self.input_batch.req_id_to_index[req_id] + num_draft_tokens[req_idx] = len(draft_token_ids) + + spec_decode_metadata = self._calc_spec_decode_metadata( + num_draft_tokens, cu_num_tokens) + logits_indices = spec_decode_metadata.logits_indices # Hot-Swap lora model if self.lora_config: self.set_active_loras(self.input_batch, num_scheduled_tokens) - return attn_metadata, logits_indices + return attn_metadata, logits_indices, spec_decode_metadata def _compute_cascade_attn_prefix_len( self, @@ -732,50 +745,79 @@ class GPUModelRunner(LoRAModelRunnerMixin): def _calc_spec_decode_metadata( self, - scheduler_output: "SchedulerOutput", - cu_num_tokens: np.ndarray, - ) -> torch.Tensor: - # Get the number of spec decode tokens for each request. - num_reqs = self.input_batch.num_reqs - num_spec_decode_tokens = np.empty(num_reqs, dtype=np.int32) - for i, req_id in enumerate(self.input_batch.req_ids): - num_spec_decode_tokens[i] = len( - scheduler_output.scheduled_spec_decode_tokens.get(req_id, ())) - - # Get spec decode logits indices. - # E.g., num_scheduled_tokens: [4, 100, 3, 100, 2] - # cu_num_tokens: [4, 104, 107, 207, 209] - # num_spec_tokens_list: [3, 0, 2, 0, 1] - # num_sampled_tokens: [4, 1, 3, 1, 2] - # spec_decode_logits_indices: - # [0, 1, 2, 3, 103, 104, 105, 106, 206, 207, 208] - num_sampled_tokens = num_spec_decode_tokens + 1 - # logits_start_loc: [0, 103, 104, 206, 207] - logits_start_loc = cu_num_tokens - num_sampled_tokens - # [0, 103, 104, 206, 207] -> - # [0, 0, 0, 0, 103, 104, 104, 104, 206, 207, 207] - logits_start_loc = np.repeat(logits_start_loc, num_sampled_tokens) - # The following three lines: - # [4, 1, 3, 1, 2] -> [0, 1, 2, 3, 0, 0, 1, 2, 0, 0, 1] - # Step 1. [4, 1, 3, 1, 2] -> [4, 5, 8, 9, 11] - cu_num_sampled_tokens = np.cumsum(num_sampled_tokens) - # Step 2. [4, 5, 8, 9, 11] -> [0, 4, 5, 8, 9] - # -> [0, 0, 0, 0, 4, 5, 5, 5, 8, 9, 9] - cumsums_sampled_offsets = np.repeat( - cu_num_sampled_tokens - num_sampled_tokens, num_sampled_tokens) - # Step 3. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - # - [0, 0, 0, 0, 4, 5, 5, 5, 8, 9, 9] - # -> [0, 1, 2, 3, 0, 0, 1, 2, 0, 0, 1] - total_num_sampled_tokens = num_sampled_tokens.sum() - sampled_arange = (self.arange_np[:total_num_sampled_tokens] - - cumsums_sampled_offsets) - - # [0, 0, 0, 0, 103, 104, 104, 104, 206, 207, 207] -> - # [0, 1, 2, 3, 103, 104, 105, 106, 206, 207, 208] - spec_decode_logits_indices = logits_start_loc + sampled_arange - return torch.from_numpy(spec_decode_logits_indices).to( + num_draft_tokens: np.ndarray, + cu_num_scheduled_tokens: np.ndarray, + ) -> SpecDecodeMetadata: + # Inputs: + # cu_num_scheduled_tokens: [ 4, 104, 107, 207, 209] + # num_draft_tokens: [ 3, 0, 2, 0, 1] + # Outputs: + # cu_num_draft_tokens: [ 3, 3, 5, 5, 6] + # logits_indices: [ 0, 1, 2, 3, 103, 104, 105, 106, + # 206, 207, 208] + # target_logits_indices: [ 0, 1, 2, 5, 6, 9] + # bonus_logits_indices: [ 3, 4, 7, 8, 10] + + # Compute the logits indices. + # [4, 1, 3, 1, 2] + num_sampled_tokens = num_draft_tokens + 1 + # Step 1. [4, 5, 8, 9, 11] + cu_num_sampled_tokens = np.cumsum(num_sampled_tokens, dtype=np.int32) + total_num_sampled_tokens = cu_num_sampled_tokens[-1] + # Step 2. [0, 0, 0, 0, 4, 5, 5, 5, 8, 9, 9] + cumsums_offsets = np.repeat(cu_num_sampled_tokens - num_sampled_tokens, + num_sampled_tokens) + # Step 3. [0, 1, 2, 3, 0, 0, 1, 2, 0, 0, 1] + arange = self.arange_np[:total_num_sampled_tokens] - cumsums_offsets + # Step 4. [0, 0, 0, 0, 103, 104, 104, 104, 206, 207, 207] + logits_indices = np.repeat( + cu_num_scheduled_tokens - num_sampled_tokens, num_sampled_tokens) + # Step 5. [0, 1, 2, 3, 103, 104, 105, 106, 206, 207, 208] + logits_indices += arange + + # Compute the bonus logits indices. + bonus_logits_indices = cu_num_sampled_tokens - 1 + + # Compute the draft logits indices. + # [3, 3, 5, 5, 6] + cu_num_draft_tokens = np.cumsum(num_draft_tokens, dtype=np.int32) + total_num_draft_tokens = cu_num_draft_tokens[-1] + # [0, 0, 0, 3, 3, 5] + cumsums_offsets = np.repeat(cu_num_draft_tokens - num_draft_tokens, + num_draft_tokens) + # [0, 1, 2, 0, 1, 0] + arange = self.arange_np[:total_num_draft_tokens] - cumsums_offsets + # [0, 0, 0, 5, 5, 9] + target_logits_indices = np.repeat( + cu_num_sampled_tokens - num_sampled_tokens, num_draft_tokens) + # [0, 1, 2, 5, 6, 9] + target_logits_indices += arange + + # TODO: Optimize the CPU -> GPU copy. + cu_num_draft_tokens = torch.from_numpy(cu_num_draft_tokens).to( + self.device, non_blocking=True) + logits_indices = torch.from_numpy(logits_indices).to(self.device, + non_blocking=True) + target_logits_indices = torch.from_numpy(target_logits_indices).to( + self.device, non_blocking=True) + bonus_logits_indices = torch.from_numpy(bonus_logits_indices).to( self.device, non_blocking=True) + # Compute the draft token ids. + # draft_token_indices: [ 1, 2, 3, 105, 106, 208] + draft_token_ids = self.input_ids[logits_indices] + draft_token_ids = draft_token_ids[target_logits_indices + 1] + + metadata = SpecDecodeMetadata( + draft_token_ids=draft_token_ids, + num_draft_tokens=num_draft_tokens.tolist(), + cu_num_draft_tokens=cu_num_draft_tokens, + target_logits_indices=target_logits_indices, + bonus_logits_indices=bonus_logits_indices, + logits_indices=logits_indices, + ) + return metadata + def _execute_encoder(self, scheduler_output: "SchedulerOutput"): scheduled_encoder_inputs = scheduler_output.scheduled_encoder_inputs if not scheduled_encoder_inputs: @@ -931,7 +973,8 @@ class GPUModelRunner(LoRAModelRunnerMixin): encoder_outputs = [] # Prepare the decoder inputs. - attn_metadata, logits_indices = self._prepare_inputs(scheduler_output) + attn_metadata, logits_indices, spec_decode_metadata = ( + self._prepare_inputs(scheduler_output)) num_scheduled_tokens = scheduler_output.total_num_scheduled_tokens if (self.use_cuda_graph and num_scheduled_tokens <= self.cudagraph_batch_sizes[-1]): @@ -1006,31 +1049,29 @@ class GPUModelRunner(LoRAModelRunnerMixin): # Sample the next token and get logprobs if needed. sampling_metadata = self.input_batch.sampling_metadata - if not self.use_spec_decode: + if spec_decode_metadata is None: sampler_output = self.model.sample( logits=logits, sampling_metadata=sampling_metadata, ) else: - draft_token_ids = [ - scheduler_output.scheduled_spec_decode_tokens.get(req_id, []) - for req_id in self.input_batch.req_ids - ] - sample_lens = [len(tokens) + 1 for tokens in draft_token_ids] - recover_logits_idx = np.cumsum(sample_lens) - 1 - target_probs = self.rejection_sampler.compute_probs( - logits, sampling_metadata, sample_lens) + # TODO(woosuk): Optimize the memory usage. + bonus_logits = logits[spec_decode_metadata.bonus_logits_indices] sampler_output = self.model.sample( - logits=logits[recover_logits_idx, :], + logits=bonus_logits, sampling_metadata=sampling_metadata, ) bonus_token_ids = sampler_output.sampled_token_ids + + # TODO(woosuk): Optimize the memory usage. + target_logits = logits[spec_decode_metadata.target_logits_indices] output_token_ids = self.rejection_sampler( - draft_token_ids, + spec_decode_metadata, None, # draft_probs + target_logits, bonus_token_ids, - target_probs, - sampling_metadata) + sampling_metadata, + ) sampler_output.sampled_token_ids = output_token_ids # TODO(woosuk): The following loop can be slow since it iterates over @@ -1066,13 +1107,8 @@ class GPUModelRunner(LoRAModelRunnerMixin): valid_sampled_token_ids = sampled_token_ids.tolist() else: # Includes spec decode tokens. - valid_mask = sampled_token_ids != INVALID_TOKEN_ID - gen_lens = valid_mask.sum(dim=1).tolist() - # TODO(woosuk): Optimize this. - valid_sampled_token_ids = [ - seq.tolist() - for seq in sampled_token_ids[valid_mask].split(gen_lens) - ] + valid_sampled_token_ids = self.rejection_sampler.parse_output( + sampled_token_ids, self.input_batch.vocab_size) if not self.use_spec_decode: spec_token_ids = None @@ -1316,6 +1352,33 @@ class GPUModelRunner(LoRAModelRunnerMixin): "initializing the engine.") from e else: raise e + if self.use_spec_decode: + draft_token_ids = [[0] for _ in range(num_reqs)] + dummy_spec_decode_metadata = SpecDecodeMetadata.make_dummy( + draft_token_ids, self.device) + + num_tokens = sum(len(ids) for ids in draft_token_ids) + # draft_probs = torch.randn( + # num_tokens, logits.shape[-1], device=self.device, + # dtype=logits.dtype) + draft_probs = None + target_logits = torch.randn(num_tokens, + logits.shape[-1], + device=self.device, + dtype=logits.dtype) + # NOTE(woosuk): Here, we should use int32 because the sampler uses + # int32 for bonus_token_ids. If the dtype mismatches, re-compilation + # will occur at runtime. + bonus_token_ids = torch.zeros(num_reqs, + device=self.device, + dtype=torch.int32) + self.rejection_sampler( + dummy_spec_decode_metadata, + draft_probs, + target_logits, + bonus_token_ids, + dummy_metadata, + ) return sampler_output def profile_run(self) -> None:
[ "RejectionSampler.forward", "RejectionSampler.parse_output", "SpecDecodeMetadata.make_dummy", "compiled_softmax" ]
[ "vllm/v1/sample/rejection_sampler.py", "vllm/v1/spec_decode/metadata.py", "vllm/v1/sample/metadata.py", "vllm/v1/pool/metadata.py", "vllm/v1/sample/tpu/metadata.py", "vllm/v1/worker/gpu_model_runner.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct --tasks gsm8k --num_fewshot 5 --limit 100
fe66b34728e5d383e3d19aefc544eeee808c99fb
https://github.com/vllm-project/vllm/pull/14778
2025-03-14
2025-09-07 17:51:55
2025-09-07 17:51:55
[ "ibm-ai-platform/Bamba-9B" ]
python benchmarks/benchmark_serving.py --model ibm-ai-platform/Bamba-9B --dtype float16 --num-prompts 300 --seed 0
true
false
false
true
[Model] Mamba2 Prefill Performance Tweaks: Fixing Flurry of Unnecessary Memory Copies (#14778)
[Model] Mamba2 Prefill Performance Tweaks: Fixing Flurry of Unnecessary Memory Copies (#14778)
2025-03-14T16:36:18-04:00
[ "vllm/model_executor/layers/mamba/mamba_mixer2.py" ]
{ "commit_year": 2025, "num_edited_lines": 30, "num_files": 1, "num_hunks": 2, "num_non_test_edited_lines": 30, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/layers/mamba/mamba_mixer2.py b/vllm/model_executor/layers/mamba/mamba_mixer2.py index b53a540ed..5b19e3f35 100644 --- a/vllm/model_executor/layers/mamba/mamba_mixer2.py +++ b/vllm/model_executor/layers/mamba/mamba_mixer2.py @@ -466,10 +466,17 @@ class MambaMixer2(CustomOp): if has_prefill: initial_states = None - if has_initial_states is not None and any(has_initial_states): - for idx in mamba_cache_params.state_indices_tensor[ - ~has_initial_states]: - mamba_cache_params.ssm_state[idx].zero_() + + if has_initial_states is not None and torch.any( + has_initial_states): + + # vectorized ssm_state zero init + batched_zero_init_func = torch.vmap( + lambda idx: mamba_cache_params.ssm_state[idx].zero_()) + batched_zero_init_func( + mamba_cache_params. + state_indices_tensor[~has_initial_states].unsqueeze( + dim=-1), ) initial_states = mamba_cache_params.ssm_state[ mamba_cache_params.state_indices_tensor] @@ -493,10 +500,17 @@ class MambaMixer2(CustomOp): dt_limit=(0.0, float("inf")), ) - # update ssm states - # - varlen state is a (batch, nheads, headdim, dstate) tensor - for i, idx in enumerate(mamba_cache_params.state_indices_tensor): - mamba_cache_params.ssm_state[idx].copy_(varlen_state[i]) + # vectorized ssm state update using vmap + # the 1d state_indices_tensor needs to be unsqueezed to avoid vmap + # limitation which doesn't allow use of `item()` + # Note: the lambda capture can happen where ssm_state is initialized + # instead of here + batched_copy = torch.vmap( + lambda idx, source_state: mamba_cache_params.ssm_state[ + idx].copy_(source_state)) + batched_copy( + mamba_cache_params.state_indices_tensor.unsqueeze(dim=-1), + varlen_state) # - reshape hidden_states = scan_output.view(seq_len, -1)
[ "MambaMixer2.forward_cuda", "mamba_mixer2" ]
[ "vllm/model_executor/layers/mamba/mamba_mixer2.py", "vllm/model_executor/models/mamba2.py", "vllm/model_executor/models/mamba_cache.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=ibm-ai-platform/Bamba-9B,dtype=auto --trust_remote_code --tasks gsm8k --batch_size auto --limit 100
70b808fe1a63322bc6bf5f46a91981a8f6b8af00
https://github.com/vllm-project/vllm/pull/14377
2025-03-11
2025-09-07 17:51:59
2025-09-07 17:51:59
[ "Qwen/Qwen2-VL-7B" ]
python benchmarks/benchmark_serving.py --model Qwen/Qwen2-VL-7B --dataset-name random --request-rate 1
true
false
false
true
[Perf]:Optimize qwen2-vl to reduce cudaMemcpyAsync (#14377)
[Perf]:Optimize qwen2-vl to reduce cudaMemcpyAsync (#14377)
2025-03-11T07:39:56Z
[ "vllm/model_executor/models/qwen2_5_vl.py", "vllm/model_executor/models/qwen2_vl.py" ]
{ "commit_year": 2025, "num_edited_lines": 94, "num_files": 2, "num_hunks": 12, "num_non_test_edited_lines": 94, "num_non_test_files": 2, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/models/qwen2_5_vl.py b/vllm/model_executor/models/qwen2_5_vl.py index ef3d28c80..ae48c7794 100644 --- a/vllm/model_executor/models/qwen2_5_vl.py +++ b/vllm/model_executor/models/qwen2_5_vl.py @@ -255,10 +255,12 @@ class Qwen2_5_VisionAttention(nn.Module): return q, k, v def forward( - self, - x: torch.Tensor, - cu_seqlens: torch.Tensor, - rotary_pos_emb: torch.Tensor, + self, + x: torch.Tensor, + cu_seqlens: torch.Tensor, + rotary_pos_emb: torch.Tensor, + max_seqlen: Optional[int] = None, # Only used for Flash Attention + seqlens: Optional[list[int]] = None, # Only used for xFormers ) -> torch.Tensor: # [s, b, c] --> [s, b, head * 3 * head_dim] x, _ = self.qkv(x) @@ -285,7 +287,6 @@ class Qwen2_5_VisionAttention(nn.Module): q, k, v = (rearrange(x, "b s ... -> (b s) ...") for x in [q, k, v]) - max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max().item() output = flash_attn_varlen_func(q, k, v, @@ -321,7 +322,6 @@ class Qwen2_5_VisionAttention(nn.Module): from xformers import ops as xops from xformers.ops.fmha.attn_bias import BlockDiagonalMask - seqlens = (cu_seqlens[1:] - cu_seqlens[:-1]).tolist() attn_bias = BlockDiagonalMask.from_seqlens(q_seqlen=seqlens, kv_seqlen=None, device=q.device) @@ -364,11 +364,20 @@ class Qwen2_5_VisionBlock(nn.Module): quant_config=quant_config, prefix=f"{prefix}.mlp") - def forward(self, x: torch.Tensor, cu_seqlens: torch.Tensor, - rotary_pos_emb: torch.Tensor) -> torch.Tensor: + def forward( + self, + x: torch.Tensor, + cu_seqlens: torch.Tensor, + rotary_pos_emb: torch.Tensor, + max_seqlen: Optional[int] = None, # Only used for Flash Attention + seqlens: Optional[list[int]] = None, # Only used for xFormers + ) -> torch.Tensor: x = x + self.attn(self.norm1(x), cu_seqlens=cu_seqlens, - rotary_pos_emb=rotary_pos_emb) + rotary_pos_emb=rotary_pos_emb, + max_seqlen=max_seqlen, + seqlens=seqlens) + x = x + self.mlp(self.norm2(x)) return x @@ -528,6 +537,7 @@ class Qwen2_5_VisionTransformer(nn.Module): quant_config=quant_config, prefix=f"{prefix}.merger", ) + self.attn_backend: _Backend = get_vit_attn_backend(support_fa=True) @property def dtype(self) -> torch.dtype: @@ -633,14 +643,25 @@ class Qwen2_5_VisionTransformer(nn.Module): # transformers hidden_states = hidden_states.unsqueeze(1) + + max_seqlen = None + seqlens = None + if self.attn_backend == _Backend.FLASH_ATTN: + max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max().item() + elif self.attn_backend == _Backend.XFORMERS: + seqlens = (cu_seqlens[1:] - cu_seqlens[:-1]).tolist() for layer_num, blk in enumerate(self.blocks): if layer_num in self.fullatt_block_indexes: cu_seqlens_now = cu_seqlens else: cu_seqlens_now = cu_window_seqlens - hidden_states = blk(hidden_states, - cu_seqlens=cu_seqlens_now, - rotary_pos_emb=rotary_pos_emb) + hidden_states = blk( + hidden_states, + cu_seqlens=cu_seqlens_now, + rotary_pos_emb=rotary_pos_emb, + max_seqlen=max_seqlen, + seqlens=seqlens, + ) # For Qwen2.5-VL-3B, float16 will overflow at last block # for long visual tokens sequences. diff --git a/vllm/model_executor/models/qwen2_vl.py b/vllm/model_executor/models/qwen2_vl.py index ac3d154dd..0e9fa7183 100644 --- a/vllm/model_executor/models/qwen2_vl.py +++ b/vllm/model_executor/models/qwen2_vl.py @@ -303,10 +303,12 @@ class Qwen2VisionAttention(nn.Module): return q, k, v def forward( - self, - x: torch.Tensor, - cu_seqlens: torch.Tensor, - rotary_pos_emb: torch.Tensor, + self, + x: torch.Tensor, + cu_seqlens: torch.Tensor, + rotary_pos_emb: torch.Tensor, + max_seqlen: Optional[int] = None, # Only used for Flash Attention + seqlens: Optional[list[int]] = None, # Only used for xFormers ) -> torch.Tensor: # [s, b, c] --> [s, b, 3 * head * head_dim] @@ -329,7 +331,6 @@ class Qwen2VisionAttention(nn.Module): q, k, v = (rearrange(x, "b s ... -> (b s) ...") for x in [q, k, v]) - max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max().item() output = flash_attn_varlen_func(q, k, v, @@ -365,7 +366,6 @@ class Qwen2VisionAttention(nn.Module): from xformers import ops as xops from xformers.ops.fmha.attn_bias import BlockDiagonalMask - seqlens = (cu_seqlens[1:] - cu_seqlens[:-1]).tolist() attn_bias = BlockDiagonalMask.from_seqlens(q_seqlen=seqlens, kv_seqlen=None, device=q.device) @@ -409,11 +409,22 @@ class Qwen2VisionBlock(nn.Module): quant_config=quant_config, prefix=f"{prefix}.mlp") - def forward(self, x: torch.Tensor, cu_seqlens: torch.Tensor, - rotary_pos_emb: torch.Tensor) -> torch.Tensor: - x = x + self.attn(self.norm1(x), - cu_seqlens=cu_seqlens, - rotary_pos_emb=rotary_pos_emb) + def forward( + self, + x: torch.Tensor, + cu_seqlens: torch.Tensor, + rotary_pos_emb: torch.Tensor, + max_seqlen: Optional[int] = None, # Only used for Flash Attention + seqlens: Optional[list[int]] = None, # Only used for xFormers + ) -> torch.Tensor: + x = x + self.attn( + self.norm1(x), + cu_seqlens=cu_seqlens, + rotary_pos_emb=rotary_pos_emb, + max_seqlen=max_seqlen, + seqlens=seqlens, + ) + x = x + self.mlp(self.norm2(x)) return x @@ -570,6 +581,7 @@ class Qwen2VisionTransformer(nn.Module): quant_config=quant_config, prefix=f"{prefix}.merger", ) + self.attn_backend: _Backend = get_vit_attn_backend(support_fa=True) @property def dtype(self) -> torch.dtype: @@ -624,8 +636,21 @@ class Qwen2VisionTransformer(nn.Module): # transformers x = x.unsqueeze(1) + + max_seqlen = None + seqlens = None + if self.attn_backend == _Backend.FLASH_ATTN: + max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max().item() + elif self.attn_backend == _Backend.XFORMERS: + seqlens = (cu_seqlens[1:] - cu_seqlens[:-1]).tolist() for blk in self.blocks: - x = blk(x, cu_seqlens=cu_seqlens, rotary_pos_emb=rotary_pos_emb) + x = blk( + x, + cu_seqlens=cu_seqlens, + rotary_pos_emb=rotary_pos_emb, + max_seqlen=max_seqlen, + seqlens=seqlens, + ) # adapter x = self.merger(x)
[ "Qwen2VisionAttention.forward", "Qwen2VisionBlock.forward", "Qwen2VisionTransformer.forward", "Qwen2_5_VisionAttention.forward", "Qwen2_5_VisionTransformer.forward" ]
[ "vllm/model_executor/models/qwen2_vl.py", "vllm/model_executor/models/qwen2_5_vl.py", "vllm/multimodal/registry.py", "vllm/inputs/registry.py", "vllm/model_executor/models/registry.py", "vllm/transformers_utils/chat_templates/registry.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=Qwen/Qwen2-VL-7B,dtype=auto --trust_remote_code --tasks gsm8k --batch_size auto --limit 100
9f1710f1ace3535920c0bb6d4cc329c36289080e
https://github.com/vllm-project/vllm/pull/13897
2025-03-06
2025-09-07 17:52:14
2025-09-07 17:52:14
[ "deepseek-ai/DeepSeek-V2-Lite-Chat" ]
python benchmarks/benchmark_serving.py --model deepseek-ai/DeepSeek-V2-Lite-Chat --input-len 28000 --output-len 64
true
false
false
true
Fix mla prefill context performance (#13897)
Fix mla prefill context performance (#13897)
2025-03-06T09:35:49-08:00
[ "vllm/attention/backends/mla/common.py", "vllm/v1/attention/backends/mla/common.py" ]
{ "commit_year": 2025, "num_edited_lines": 4, "num_files": 2, "num_hunks": 2, "num_non_test_edited_lines": 4, "num_non_test_files": 2, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/attention/backends/mla/common.py b/vllm/attention/backends/mla/common.py index 8184b0732..109e8496f 100644 --- a/vllm/attention/backends/mla/common.py +++ b/vllm/attention/backends/mla/common.py @@ -1308,7 +1308,7 @@ class MLACommonImpl(MLAAttentionImpl[T], Generic[T]): ) kv_c_normed = workspace[:toks]\ - [..., :self.kv_lora_rank].unsqueeze(1) + [..., :self.kv_lora_rank] k_pe = workspace[:toks]\ [..., self.kv_lora_rank:].unsqueeze(1) diff --git a/vllm/v1/attention/backends/mla/common.py b/vllm/v1/attention/backends/mla/common.py index c98262eea..0b55854de 100644 --- a/vllm/v1/attention/backends/mla/common.py +++ b/vllm/v1/attention/backends/mla/common.py @@ -874,7 +874,7 @@ class MLACommonImpl(MLAAttentionImpl[M], Generic[M]): ) kv_c_normed = workspace[:toks]\ - [..., :self.kv_lora_rank].unsqueeze(1) + [..., :self.kv_lora_rank] k_pe = workspace[:toks]\ [..., self.kv_lora_rank:].unsqueeze(1)
[ "MLACommonImpl.forward" ]
[ "vllm/core/block/common.py", "vllm/attention/backends/mla/common.py", "vllm/v1/attention/backends/mla/common.py", "vllm/attention/ops/prefix_prefill.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=deepseek-ai/DeepSeek-V2-Lite-Chat,dtype=float16 --tasks gsm8k --batch_size auto --limit 100
9badee53decb3d432dc805336abfb0eb81dfb48f
https://github.com/vllm-project/vllm/pull/14223
2025-03-04
2025-09-07 17:52:18
2025-09-07 17:52:18
[ "meta-llama/Llama-3.2-1B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.2-1B-Instruct --dataset-path ShareGPT_V3_unfiltered_cleaned_split.json
true
false
false
true
Fix performance when `--generation-config` is not `None` (#14223)
Fix performance when `--generation-config` is not `None` (#14223)
2025-03-04T20:59:22+01:00
[ "vllm/entrypoints/llm.py", "vllm/entrypoints/openai/serving_chat.py", "vllm/entrypoints/openai/serving_completion.py", "vllm/entrypoints/openai/serving_transcription.py" ]
{ "commit_year": 2025, "num_edited_lines": 48, "num_files": 4, "num_hunks": 8, "num_non_test_edited_lines": 48, "num_non_test_files": 4, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/entrypoints/llm.py b/vllm/entrypoints/llm.py index 122e2ed86..fc585ee9e 100644 --- a/vllm/entrypoints/llm.py +++ b/vllm/entrypoints/llm.py @@ -244,6 +244,7 @@ class LLM: engine_args, usage_context=UsageContext.LLM_CLASS) self.request_counter = Counter() + self.default_sampling_params: Union[dict[str, Any], None] = None @staticmethod def get_engine_class() -> type[LLMEngine]: @@ -268,10 +269,11 @@ class LLM: tokenizer_group.tokenizer = get_cached_tokenizer(tokenizer) def get_default_sampling_params(self) -> SamplingParams: - diff_sampling_param = ( - self.llm_engine.model_config.get_diff_sampling_param()) - if diff_sampling_param: - return SamplingParams.from_optional(**diff_sampling_param) + if self.default_sampling_params is None: + self.default_sampling_params = ( + self.llm_engine.model_config.get_diff_sampling_param()) + if self.default_sampling_params: + return SamplingParams.from_optional(**self.default_sampling_params) return SamplingParams() @overload diff --git a/vllm/entrypoints/openai/serving_chat.py b/vllm/entrypoints/openai/serving_chat.py index 98e9ea0fc..f4aaee360 100644 --- a/vllm/entrypoints/openai/serving_chat.py +++ b/vllm/entrypoints/openai/serving_chat.py @@ -105,10 +105,11 @@ class OpenAIServingChat(OpenAIServing): "been registered") from e self.enable_prompt_tokens_details = enable_prompt_tokens_details - diff_sampling_param = self.model_config.get_diff_sampling_param() - if diff_sampling_param: + self.default_sampling_params = ( + self.model_config.get_diff_sampling_param()) + if self.default_sampling_params: logger.info("Overwriting default chat sampling param with: %s", - diff_sampling_param) + self.default_sampling_params) async def create_chat_completion( self, @@ -210,17 +211,14 @@ class OpenAIServingChat(OpenAIServing): sampling_params: Union[SamplingParams, BeamSearchParams] default_max_tokens = self.max_model_len - len( engine_prompt["prompt_token_ids"]) - # Build default sampling params - default_sampling_params = ( - self.model_config.get_diff_sampling_param()) if request.use_beam_search: sampling_params = request.to_beam_search_params( - default_max_tokens, default_sampling_params) + default_max_tokens, self.default_sampling_params) else: sampling_params = request.to_sampling_params( default_max_tokens, self.model_config.logits_processor_pattern, - default_sampling_params) + self.default_sampling_params) self._log_inputs(request_id, request_prompts[i], diff --git a/vllm/entrypoints/openai/serving_completion.py b/vllm/entrypoints/openai/serving_completion.py index ed09af84f..b2ad28c0a 100644 --- a/vllm/entrypoints/openai/serving_completion.py +++ b/vllm/entrypoints/openai/serving_completion.py @@ -51,11 +51,12 @@ class OpenAIServingCompletion(OpenAIServing): models=models, request_logger=request_logger, return_tokens_as_token_ids=return_tokens_as_token_ids) - diff_sampling_param = self.model_config.get_diff_sampling_param() - if diff_sampling_param: + self.default_sampling_params = ( + self.model_config.get_diff_sampling_param()) + if self.default_sampling_params: logger.info( "Overwriting default completion sampling param with: %s", - diff_sampling_param) + self.default_sampling_params) async def create_completion( self, @@ -119,17 +120,14 @@ class OpenAIServingCompletion(OpenAIServing): sampling_params: Union[SamplingParams, BeamSearchParams] default_max_tokens = self.max_model_len - len( engine_prompt["prompt_token_ids"]) - # Build default sampling params - default_sampling_params = ( - self.model_config.get_diff_sampling_param()) if request.use_beam_search: sampling_params = request.to_beam_search_params( - default_max_tokens, default_sampling_params) + default_max_tokens, self.default_sampling_params) else: sampling_params = request.to_sampling_params( default_max_tokens, self.model_config.logits_processor_pattern, - default_sampling_params) + self.default_sampling_params) request_id_item = f"{request_id}-{i}" diff --git a/vllm/entrypoints/openai/serving_transcription.py b/vllm/entrypoints/openai/serving_transcription.py index 77f016a5e..402a0bb7a 100644 --- a/vllm/entrypoints/openai/serving_transcription.py +++ b/vllm/entrypoints/openai/serving_transcription.py @@ -161,11 +161,12 @@ class OpenAIServingTranscription(OpenAIServing): request_logger=request_logger, return_tokens_as_token_ids=return_tokens_as_token_ids) - diff_sampling_param = self.model_config.get_diff_sampling_param() - if diff_sampling_param: + self.default_sampling_params = ( + self.model_config.get_diff_sampling_param()) + if self.default_sampling_params: logger.info( "Overwriting default completion sampling param with: %s", - diff_sampling_param) + self.default_sampling_params) async def _preprocess_transcription( self, @@ -273,9 +274,8 @@ class OpenAIServingTranscription(OpenAIServing): try: # TODO(rob): subtract len of tokenized prompt. default_max_tokens = self.model_config.max_model_len - default_params = self.model_config.get_diff_sampling_param() sampling_params = request.to_sampling_params( - default_max_tokens, default_params) + default_max_tokens, self.default_sampling_params) self._log_inputs( request_id,
[ "vLLM.LLM.get_default_sampling_params", "OpenAIServingChat.create_chat_completion", "OpenAIServingCompletion.create_completion", "OpenAIServingTranscription._preprocess_transcription" ]
[ "vllm/entrypoints/llm.py", "vllm/entrypoints/openai/serving_chat.py", "vllm/entrypoints/openai/serving_completion.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.2-1B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
19d98e0c7db96713f0e2201649159431177a56e2
https://github.com/vllm-project/vllm/pull/13625
2025-03-03
2025-09-07 17:52:21
2025-09-07 17:52:21
[ "deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct" ]
python benchmarks/benchmark_serving.py --model deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct --dataset-name sharegpt --num-prompts 100
true
false
false
true
[Kernel] Optimize moe intermediate_cache usage (#13625)
[Kernel] Optimize moe intermediate_cache usage (#13625)
2025-03-03T16:29:53-05:00
[ "vllm/model_executor/layers/fused_moe/fused_moe.py" ]
{ "commit_year": 2025, "num_edited_lines": 17, "num_files": 1, "num_hunks": 1, "num_non_test_edited_lines": 17, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/layers/fused_moe/fused_moe.py b/vllm/model_executor/layers/fused_moe/fused_moe.py index 00260313e..5336b3c10 100644 --- a/vllm/model_executor/layers/fused_moe/fused_moe.py +++ b/vllm/model_executor/layers/fused_moe/fused_moe.py @@ -1240,15 +1240,20 @@ def fused_experts_impl(hidden_states: torch.Tensor, config = get_config_func(M) - intermediate_cache1 = torch.empty((M, top_k_num, N), - device=hidden_states.device, - dtype=hidden_states.dtype) + # We can reuse the memory between these because by the time we need + # cache3, we're done with cache1 + cache13 = torch.empty(M * top_k_num * max(N, w2.shape[1]), + device=hidden_states.device, + dtype=hidden_states.dtype) + intermediate_cache1 = cache13[:M * top_k_num * N].view( + (M, topk_ids.shape[1], N)) + intermediate_cache3 = cache13[:M * top_k_num * w2.shape[1]].view( + (M, topk_ids.shape[1], w2.shape[1])) + + # This needs separate memory since it's used concurrently with cache1 intermediate_cache2 = torch.empty((M * top_k_num, N // 2), device=hidden_states.device, dtype=hidden_states.dtype) - intermediate_cache3 = torch.empty((M, top_k_num, w2.shape[1]), - device=hidden_states.device, - dtype=hidden_states.dtype) if hidden_states.dtype == torch.bfloat16: compute_type = tl.bfloat16
[ "torch.ops.vllm.inplace_fused_experts", "torch.ops.vllm.outplace_fused_experts" ]
[ "vllm/model_executor/layers/fused_moe/fused_moe.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/llm.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct,dtype=float16 --tasks gsm8k --batch_size auto
e206b5433109d298e53451015465b2bf8f03ef0a
https://github.com/vllm-project/vllm/pull/13837
2025-02-26
2025-09-07 17:52:25
2025-09-07 17:52:25
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100 --guided-decoding-backend xgrammar
true
false
false
true
[v0][Core] Use xgrammar shared context to avoid copy overhead for offline engine (#13837)
[v0][Core] Use xgrammar shared context to avoid copy overhead for offline engine (#13837)
2025-02-26T14:58:24+08:00
[ "vllm/model_executor/guided_decoding/xgrammar_decoding.py" ]
{ "commit_year": 2025, "num_edited_lines": 26, "num_files": 1, "num_hunks": 2, "num_non_test_edited_lines": 26, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/model_executor/guided_decoding/xgrammar_decoding.py b/vllm/model_executor/guided_decoding/xgrammar_decoding.py index 329b03a57..e6ba7f5ec 100644 --- a/vllm/model_executor/guided_decoding/xgrammar_decoding.py +++ b/vllm/model_executor/guided_decoding/xgrammar_decoding.py @@ -3,7 +3,6 @@ # noqa: UP007 from __future__ import annotations -import copy import json import re from dataclasses import dataclass, field @@ -348,5 +347,26 @@ class XGrammarLogitsProcessor: return scores def clone(self) -> XGrammarLogitsProcessor: - """Deepcopy due to per-sequence state in the matchers""" - return copy.deepcopy(self) + """Create a new instance with shared compiled grammar + but separate state""" + new_processor = XGrammarLogitsProcessor(self.config) + + # Share the compiled grammar context (immutable after compilation) + new_processor.ctx = self.ctx + + # Create fresh matchers for the new sequence + if self.ctx is not None: + new_processor.matchers = [ + xgr.GrammarMatcher(self.ctx) for _ in range(self.batch_size) + ] + + # Create a new token bitmask with the same size + if hasattr(self, 'token_bitmask') and self.token_bitmask is not None: + new_processor.token_bitmask = self.token_bitmask + + # Copy simple attributes + new_processor.batch_size = self.batch_size + # Reset prefilled state for new sequence + new_processor.prefilled = False + + return new_processor
[ "vllm.model_executor.guided_decoding.xgrammar_decoding.XGrammarLogitsProcessor.clone" ]
[ "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py", "vllm/entrypoints/llm.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
6a417b8600d4d1e57698a91b71a38446e8fc5c45
https://github.com/vllm-project/vllm/pull/13589
2025-02-20
2025-09-07 17:52:28
2025-09-07 17:52:28
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100
true
false
false
true
fix neuron performance issue (#13589)
fix neuron performance issue (#13589)
2025-02-20T10:59:36-08:00
[ "vllm/worker/neuron_worker.py" ]
{ "commit_year": 2025, "num_edited_lines": 4, "num_files": 1, "num_hunks": 2, "num_non_test_edited_lines": 4, "num_non_test_files": 1, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/vllm/worker/neuron_worker.py b/vllm/worker/neuron_worker.py index 5f0eb0019..95e7acd02 100644 --- a/vllm/worker/neuron_worker.py +++ b/vllm/worker/neuron_worker.py @@ -76,7 +76,7 @@ class NeuronWorker(LoraNotSupportedWorkerBase, LocalOrDistributedWorkerBase): # Set the number of GPU blocks to be the same as the maximum number of # sequences that can be processed in a single batch. This is equivalent # to schedule without PagedAttention. - num_gpu_blocks = self.scheduler_config.max_num_seqs + num_gpu_blocks = self.scheduler_config.max_num_seqs + 1 # Swap not yet supported with Neuron backend. num_cpu_blocks = 0 @@ -90,7 +90,7 @@ class NeuronWorker(LoraNotSupportedWorkerBase, LocalOrDistributedWorkerBase): # Different values are not tested. assert num_cpu_blocks == 0 - assert num_gpu_blocks == self.scheduler_config.max_num_seqs + assert num_gpu_blocks == self.scheduler_config.max_num_seqs + 1 self.cache_config.num_gpu_blocks = num_gpu_blocks self.cache_config.num_cpu_blocks = num_cpu_blocks
[ "NeuronWorker.determine_num_available_blocks", "NeuronWorker.initialize_cache" ]
[ "vllm/worker/neuron_worker.py", "vllm/worker/neuron_model_runner.py", "vllm/platforms/neuron.py", "examples/offline_inference/neuron.py", "vllm/model_executor/model_loader/neuron.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
4c822298981a8f7521492075ff72659985fc4c3f
https://github.com/vllm-project/vllm/pull/13365
2025-02-18
2025-09-07 17:52:34
2025-09-07 17:52:34
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_latency.py --model meta-llama/Llama-3.1-8B-Instruct --speculative-model meta-llama/Llama-3.2-1B-Instruct --num-speculative-tokens 5
false
true
false
true
[V1][Spec Decode] Optimize N-gram matching with Numba (#13365)
[V1][Spec Decode] Optimize N-gram matching with Numba (#13365)
2025-02-18T13:19:58-08:00
[ "requirements-common.txt", "vllm/v1/spec_decode/ngram_proposer.py", "vllm/v1/worker/gpu_model_runner.py" ]
{ "commit_year": 2025, "num_edited_lines": 127, "num_files": 3, "num_hunks": 5, "num_non_test_edited_lines": 127, "num_non_test_files": 3, "num_test_files": 0, "only_non_test_files": 1, "only_test_files": 0 }
diff --git a/requirements-common.txt b/requirements-common.txt index b7c94cbdb..c52980bc7 100644 --- a/requirements-common.txt +++ b/requirements-common.txt @@ -1,6 +1,7 @@ psutil sentencepiece # Required for LLaMA tokenizer. numpy < 2.0.0 +numba == 0.60.0 # v0.61 doesn't support Python 3.9. Required for N-gram speculative decoding. requests >= 2.26.0 tqdm blake3 diff --git a/vllm/v1/spec_decode/ngram_proposer.py b/vllm/v1/spec_decode/ngram_proposer.py index 9b116e00a..33289d05d 100644 --- a/vllm/v1/spec_decode/ngram_proposer.py +++ b/vllm/v1/spec_decode/ngram_proposer.py @@ -1,14 +1,12 @@ # SPDX-License-Identifier: Apache-2.0 -from typing import List, Optional +from typing import Optional import numpy as np +from numba import jit class NgramProposer: - def __init__(self): - pass - def propose( self, context_token_ids: np.ndarray, @@ -21,7 +19,7 @@ class NgramProposer: that match. Args: - context_token_ids: List of token IDs representing the + context_token_ids: Numpy array of token IDs representing the context sequence. n: Length of the n-gram to match. k: Number of tokens follow the match. If there are less @@ -41,66 +39,65 @@ class NgramProposer: followed that pattern. Here we will return [4,2,3] because we only have three tokens after the match. """ - # TODO: Use c++ to implement the _find_subarray_kmp to - # improve the efficiency - return self._find_subarray_kmp(context_token_ids, n, k) + return _find_subarray_kmp(context_token_ids, n, k) - @staticmethod - def _kmp_lps_array(pattern: List[int]) -> List[int]: - """ - Build the lps (longest proper prefix which is also suffix) - array for the pattern. - """ - lps = [0] * len(pattern) - prev_lps = 0 # length of the previous longest prefix suffix - i = 1 - while i < len(pattern): - if pattern[i] == pattern[prev_lps]: - prev_lps += 1 - lps[i] = prev_lps - i += 1 +@jit(nopython=True) +def _kmp_lps_array(pattern: np.ndarray) -> np.ndarray: + """ + Build the lps (longest proper prefix which is also suffix) + array for the pattern. + """ + lps = np.zeros(len(pattern), dtype=np.int32) + prev_lps = 0 # length of the previous longest prefix suffix + i = 1 + + while i < len(pattern): + if pattern[i] == pattern[prev_lps]: + prev_lps += 1 + lps[i] = prev_lps + i += 1 + else: + if prev_lps != 0: + prev_lps = lps[prev_lps - 1] else: - if prev_lps != 0: - prev_lps = lps[prev_lps - 1] - else: - lps[i] = 0 - i += 1 + lps[i] = 0 + i += 1 + return lps - return lps - @staticmethod - def _find_subarray_kmp( - context_token_ids: np.ndarray, - n: int, - k: int, - ) -> Optional[np.ndarray]: - context_len = context_token_ids.shape[0] - assert n > 0 +@jit(nopython=True) +def _find_subarray_kmp( + context_token_ids: np.ndarray, + n: int, + k: int, +) -> Optional[np.ndarray]: + context_len = context_token_ids.shape[0] + assert n > 0 - pattern = context_token_ids[-n:] - # Precompute lps array for Y - lps = NgramProposer._kmp_lps_array(pattern) + pattern = context_token_ids[-n:] + # Precompute lps array for Y + lps = _kmp_lps_array(pattern) - i = 0 - j = 0 - # -n because the last n tokens are used as pattern - while i < context_len - n: - if context_token_ids[i] == pattern[j]: - i += 1 - j += 1 + i = 0 + j = 0 + # -n because the last n tokens are used as pattern + while i < context_len - n: + if context_token_ids[i] == pattern[j]: + i += 1 + j += 1 - # If we have matched the entire Y - if j == n: - # Found pattern in context, gather the next K elements - return context_token_ids[i:i + k] + # If we have matched the entire Y + if j == n: + # Found pattern in context, gather the next K elements + return context_token_ids[i:i + k] + else: + # Mismatch + if j != 0: + # Use the lps array to avoid re-checking elements + j = lps[j - 1] else: - # Mismatch - if j != 0: - # Use the lps array to avoid re-checking elements - j = lps[j - 1] - else: - i += 1 + i += 1 - # Y not found - return None + # Y not found + return None diff --git a/vllm/v1/worker/gpu_model_runner.py b/vllm/v1/worker/gpu_model_runner.py index 0ecc00acc..31fe095a9 100644 --- a/vllm/v1/worker/gpu_model_runner.py +++ b/vllm/v1/worker/gpu_model_runner.py @@ -120,11 +120,20 @@ class GPUModelRunner(LoRAModelRunnerMixin): # Set up speculative decoding. self.use_spec_decode = False if self.speculative_config: + self.use_spec_decode = True + # TODO: find a better way to check if we are using ngram. assert self.speculative_config.ngram_prompt_lookup_min, \ "Currently, only ngram spec decode is supported in V1." - self.drafter = NgramProposer() - self.use_spec_decode = True + if get_pp_group().is_last_rank: + self.drafter = NgramProposer() + # Trigger Numba JIT compilation for N-gram proposer. + # This usually takes less than 1 second. + self.drafter.propose( + np.zeros(1024, dtype=np.int32), + self.speculative_config.ngram_prompt_lookup_min, + self.speculative_config.num_speculative_tokens, + ) # Request states. self.requests: Dict[str, CachedRequestState] = {}
[ "vllm.v1.spec_decode.ngram_proposer.NgramProposer.propose", "vllm.v1.worker.GPUModelRunner.__init__" ]
[ "vllm/v1/spec_decode/ngram_proposer.py", "vllm/v1/worker/gpu_model_runner.py", "vllm/engine/llm_engine.py", "vllm/v1/engine/llm_engine.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100
30172b4947c52890b808c6da3a6c7580f55cbb74
https://github.com/vllm-project/vllm/pull/13244
2025-02-18
2025-09-07 17:52:38
2025-09-07 17:52:38
[ "meta-llama/Llama-3.1-8B-Instruct" ]
python benchmarks/benchmark_serving.py --model meta-llama/Llama-3.1-8B-Instruct --backend vllm --num-prompts 100
true
false
false
true
[V1] Optimize handling of sampling metadata and req_ids list (#13244)
[V1] Optimize handling of sampling metadata and req_ids list (#13244)
2025-02-18T12:15:33-08:00
[ "tests/v1/sample/test_rejection_sampler.py", "tests/v1/sample/test_sampler.py", "tests/v1/worker/test_gpu_input_batch.py", "tests/v1/worker/test_gpu_model_runner.py", "vllm/model_executor/layers/utils.py", "vllm/v1/core/scheduler.py", "vllm/v1/sample/metadata.py", "vllm/v1/sample/ops/penalties.py", "vllm/v1/sample/ops/topk_topp_sampler.py", "vllm/v1/sample/rejection_sampler.py", "vllm/v1/sample/sampler.py", "vllm/v1/utils.py", "vllm/v1/worker/gpu_input_batch.py", "vllm/v1/worker/gpu_model_runner.py", "vllm/v1/worker/tpu_model_runner.py" ]
{ "commit_year": 2025, "num_edited_lines": 553, "num_files": 15, "num_hunks": 74, "num_non_test_edited_lines": 420, "num_non_test_files": 11, "num_test_files": 4, "only_non_test_files": 0, "only_test_files": 0 }
diff --git a/tests/v1/sample/test_rejection_sampler.py b/tests/v1/sample/test_rejection_sampler.py index 8bc33e841..3e810e525 100644 --- a/tests/v1/sample/test_rejection_sampler.py +++ b/tests/v1/sample/test_rejection_sampler.py @@ -26,17 +26,13 @@ def create_logits_tensor(token_ids: List[int], def create_sampling_metadata(spec_tokens: List[List[int]]) -> SamplingMetadata: batch_size = len(spec_tokens) return SamplingMetadata( - temperature=0.0, + temperature=torch.tensor([]), all_greedy=True, all_random=False, - rejection_sampling=True, spec_token_ids=spec_tokens, top_p=None, top_k=None, - no_top_p=False, - no_top_k=False, min_p=torch.empty(batch_size, ), - no_min_p=True, generators={}, max_num_logprobs=0, no_penalties=False, @@ -45,8 +41,7 @@ def create_sampling_metadata(spec_tokens: List[List[int]]) -> SamplingMetadata: presence_penalties=torch.tensor([]), repetition_penalties=torch.tensor([]), output_token_ids=[], - min_tokens=[], - stop_token_ids=[], + min_tokens={}, logit_bias=[None] * batch_size, ) diff --git a/tests/v1/sample/test_sampler.py b/tests/v1/sample/test_sampler.py index a4bd651f8..3f6301c54 100644 --- a/tests/v1/sample/test_sampler.py +++ b/tests/v1/sample/test_sampler.py @@ -77,25 +77,20 @@ def _create_default_sampling_metadata( temperature=torch.full((batch_size, ), 0.0), all_greedy=True, all_random=False, - rejection_sampling=False, - top_p=torch.empty(batch_size, ), - top_k=torch.empty(batch_size, ), - no_top_p=True, - no_top_k=True, - min_p=torch.empty(batch_size, ), - no_min_p=True, + top_p=None, + top_k=None, + min_p=None, generators={}, max_num_logprobs=0, prompt_token_ids=_create_prompt_tokens_tensor(prompt_token_ids, vocab_size, device), output_token_ids=output_token_ids, - spec_token_ids=[], + spec_token_ids=None, frequency_penalties=_create_penalty_tensor(batch_size, 0.0, device), presence_penalties=_create_penalty_tensor(batch_size, 0.0, device), repetition_penalties=_create_penalty_tensor(batch_size, 1.0, device), no_penalties=True, - min_tokens=[], - stop_token_ids=[], + min_tokens={}, logit_bias=[None] * batch_size, ) return fake_sampling_metadata @@ -104,10 +99,10 @@ def _create_default_sampling_metadata( def _generate_min_token_penalties_and_stop_tokens( num_output_tokens: int, batch_size: int, vocab_size: int, batch_indices_for_min_token_penalty: List[int] -) -> Tuple[List[int], List[Set[int]]]: +) -> Dict[int, Tuple[int, Set[int]]]: """ - Generates and returns a list of minimum token penalties (`min_tokens`) - and a corresponding list of stop token IDs (`stop_token_ids`) for each + Generates and returns a dict of minimum token penalties and + corresponding stop token IDs (`min_tokens`, `stop_token_ids`) for each batch. If a batch index is included in `batch_indices_for_min_token_penalty`, @@ -115,22 +110,19 @@ def _generate_min_token_penalties_and_stop_tokens( and a random set of stop token IDs is created. Otherwise, a lower `min_tokens` value is assigned, and the stop token IDs set is empty. """ - stop_token_ids: List[Set[int]] = [] - min_tokens: List[int] = [] + min_tokens: Dict[int, Tuple[int, Set[int]]] = {} for index in range(batch_size): if index in batch_indices_for_min_token_penalty: - min_tokens.append( + min_tokens[index] = ( np.random.randint(num_output_tokens + 1, - 2 * num_output_tokens)) - stop_token_ids.append( + 2 * num_output_tokens), set( np.random.randint(0, vocab_size - 1) for _ in range(np.random.randint(0, vocab_size)))) - else: - min_tokens.append(np.random.randint(0, num_output_tokens)) - stop_token_ids.append(set()) - return (min_tokens, stop_token_ids) + min_tokens[index] = (np.random.randint(0, + num_output_tokens), set()) + return min_tokens def _create_weighted_output_token_list( @@ -165,7 +157,7 @@ def _create_weighted_output_token_list( output_token_ids_for_batch.extend( [token_id for _ in range(index + 1)]) output_token_ids.append(output_token_ids_for_batch) - return (output_token_ids, sorted_token_ids_in_output) + return output_token_ids, sorted_token_ids_in_output @pytest.mark.parametrize("device", CUDA_DEVICES) @@ -182,17 +174,17 @@ def test_sampler_min_tokens_penalty(device: str, batch_size: int): NUM_OUTPUT_TOKENS, batch_size, VOCAB_SIZE, torch.device(device)) batch_indices_for_min_token_penalty = np.random.randint( 0, batch_size - 1, size=np.random.randint(0, batch_size)).tolist() - min_tokens, stop_token_ids = _generate_min_token_penalties_and_stop_tokens( + min_tokens = _generate_min_token_penalties_and_stop_tokens( NUM_OUTPUT_TOKENS, batch_size, VOCAB_SIZE, batch_indices_for_min_token_penalty) sampling_metadata.min_tokens = min_tokens - sampling_metadata.stop_token_ids = stop_token_ids sampler = Sampler() logits = sampler.apply_penalties(fake_logits, sampling_metadata) logits = logits.cpu() for batch_idx in range(batch_size): for token_id in range(VOCAB_SIZE): - if token_id in stop_token_ids[batch_idx]: + _, stop_token_ids = min_tokens.get(batch_idx, (0, set())) + if token_id in stop_token_ids: assert logits[batch_idx][token_id] == -float("inf") else: assert logits[batch_idx][token_id] != -float("inf") diff --git a/tests/v1/worker/test_gpu_input_batch.py b/tests/v1/worker/test_gpu_input_batch.py index c0ab356f5..cb3b3d21f 100644 --- a/tests/v1/worker/test_gpu_input_batch.py +++ b/tests/v1/worker/test_gpu_input_batch.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 -from typing import Dict, List, Set, Tuple +from typing import Dict, List, Optional, Set, Tuple import numpy as np import pytest @@ -41,7 +41,7 @@ def _remove_requests( for index in req_indices_to_remove: input_batch.remove_request(reqs[index].req_id) req_ids_to_remove.add(reqs[index].req_id) - return (req_ids_to_remove, req_indices_to_remove_list) + return req_ids_to_remove, req_indices_to_remove_list def _construct_expected_sampling_metadata( @@ -64,8 +64,7 @@ def _construct_expected_sampling_metadata( top_p = [0.0 for _ in range(num_reqs)] min_p = [0.0 for _ in range(num_reqs)] temperature = [0.0 for _ in range(num_reqs)] - stop_token_ids: List[Set[int]] = [set() for _ in range(num_reqs)] - min_tokens = [0 for _ in range(num_reqs)] + min_tokens = {} logit_bias = [None] * num_reqs for req in reqs: if req.req_id not in req_ids_retained: @@ -83,22 +82,21 @@ def _construct_expected_sampling_metadata( top_p[index_in_input_batch] = req.sampling_params.top_p min_p[index_in_input_batch] = req.sampling_params.min_p temperature[index_in_input_batch] = req.sampling_params.temperature - stop_token_ids[ - index_in_input_batch] = req.sampling_params.all_stop_token_ids - min_tokens[index_in_input_batch] = req.sampling_params.min_tokens + min_tokens[index_in_input_batch] = ( + req.sampling_params.min_tokens, + req.sampling_params.all_stop_token_ids) logit_bias[index_in_input_batch] = req.sampling_params.logit_bias return SamplingMetadata( temperature=torch.tensor(temperature, dtype=torch.float, device=device), all_greedy=False, all_random=True, - rejection_sampling=False, - top_p=torch.tensor(top_p, dtype=torch.float, device=device), - top_k=torch.tensor(top_k, dtype=torch.int, device=device), - no_top_p=all(x == 1.0 for x in top_p), - no_top_k=all(x == 0 for x in top_k), - min_p=torch.tensor(min_p, dtype=torch.float, device=device), - no_min_p=all(x == 0.0 for x in min_p), + top_p=None if all(x == 1.0 for x in top_p) else torch.tensor( + top_p, dtype=torch.float, device=device), + top_k=None if all(x == 0 for x in top_k) else torch.tensor( + top_k, dtype=torch.int, device=device), + min_p=None if all(x == 0.0 for x in min_p) else torch.tensor( + min_p, dtype=torch.float, device=device), generators={}, max_num_logprobs=0, prompt_token_ids=make_tensor_with_pad( @@ -117,9 +115,8 @@ def _construct_expected_sampling_metadata( dtype=torch.float, device=device), output_token_ids=output_token_ids, - spec_token_ids=[], + spec_token_ids=None, min_tokens=min_tokens, - stop_token_ids=stop_token_ids, no_penalties=(all(x == 0 for x in presence_penalties) and all(x == 0 for x in frequency_penalties) and all(x == 1 for x in repetition_penalties)), @@ -206,8 +203,7 @@ def test_sampling_metadata_in_input_batch(device: str, batch_size: int): input_batch.condense(req_indices_to_remove) # Generate the sampling metadata - sampling_metadata = input_batch.make_sampling_metadata( - req_id_output_token_ids, req_id_to_spec_token_ids={}, skip_copy=False) + sampling_metadata = input_batch._make_sampling_metadata() # Create expected output. expected_sampling_metadata = _construct_expected_sampling_metadata( @@ -216,13 +212,16 @@ def test_sampling_metadata_in_input_batch(device: str, batch_size: int): input_batch.req_id_to_index, device=torch.device(device)) + def same(t1: Optional[torch.Tensor], t2: Optional[torch.Tensor]) -> bool: + return (t1 is None + and t2 is None) or (t1 is not None and t2 is not None + and torch.allclose(t1, t2)) + # Assert the actual and expected output. assert torch.allclose(expected_sampling_metadata.temperature, sampling_metadata.temperature) - assert torch.allclose(expected_sampling_metadata.top_p, - sampling_metadata.top_p) - assert torch.allclose(expected_sampling_metadata.top_k, - sampling_metadata.top_k) + assert same(expected_sampling_metadata.top_p, sampling_metadata.top_p) + assert same(expected_sampling_metadata.top_k, sampling_metadata.top_k) assert torch.allclose( expected_sampling_metadata.frequency_penalties, sampling_metadata.frequency_penalties, @@ -240,10 +239,6 @@ def test_sampling_metadata_in_input_batch(device: str, batch_size: int): assert (expected_sampling_metadata.output_token_ids == sampling_metadata.output_token_ids) assert expected_sampling_metadata.min_tokens == sampling_metadata.min_tokens - assert expected_sampling_metadata.stop_token_ids == \ - sampling_metadata.stop_token_ids assert expected_sampling_metadata.no_penalties == \ sampling_metadata.no_penalties - assert expected_sampling_metadata.no_top_p == sampling_metadata.no_top_p - assert expected_sampling_metadata.no_top_k == sampling_metadata.no_top_k assert expected_sampling_metadata.logit_bias == sampling_metadata.logit_bias diff --git a/tests/v1/worker/test_gpu_model_runner.py b/tests/v1/worker/test_gpu_model_runner.py index c655b0fde..973efcbf8 100644 --- a/tests/v1/worker/test_gpu_model_runner.py +++ b/tests/v1/worker/test_gpu_model_runner.py @@ -5,6 +5,7 @@ from vllm.config import CacheConfig, ModelConfig, SchedulerConfig, VllmConfig from vllm.sampling_params import SamplingParams from vllm.v1.core.scheduler_output import (CachedRequestData, NewRequestData, SchedulerOutput) +from vllm.v1.sample.metadata import SamplingMetadata from vllm.v1.worker.gpu_model_runner import GPUModelRunner @@ -82,14 +83,21 @@ def _is_req_added(model_runner, req_id: str) -> bool: return req_id in model_runner.requests +def _is_sampling_metadata_changed(model_runner, + sampling_metadata_before: SamplingMetadata): + return model_runner.input_batch.sampling_metadata is not ( + sampling_metadata_before) + + def test_update_states_new_request(model_runner): req_id = "req_0" # new req scheduler_output = _schedule_new_request(req_id) - batch_changed = model_runner._update_states(scheduler_output) - assert batch_changed is True + metadata_before = model_runner.input_batch.sampling_metadata + model_runner._update_states(scheduler_output) + assert _is_sampling_metadata_changed(model_runner, metadata_before) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) @@ -117,8 +125,9 @@ def test_update_states_request_finished(model_runner): free_encoder_input_ids=[], ) - batch_changed = model_runner._update_states(scheduler_output) - assert batch_changed is True + metadata_before = model_runner.input_batch.sampling_metadata + model_runner._update_states(scheduler_output) + assert _is_sampling_metadata_changed(model_runner, metadata_before) assert not _is_req_added(model_runner, req_id) assert not _is_req_scheduled(model_runner, req_id) @@ -142,7 +151,7 @@ def test_update_states_request_resumed(model_runner): scheduled_spec_decode_tokens={}, scheduled_encoder_inputs={}, num_common_prefix_blocks=0, - finished_req_ids={}, + finished_req_ids=set(), free_encoder_input_ids=[], ) @@ -171,8 +180,9 @@ def test_update_states_request_resumed(model_runner): free_encoder_input_ids=[], ) - batch_changed = model_runner._update_states(scheduler_output) - assert batch_changed is True + metadata_before = model_runner.input_batch.sampling_metadata + model_runner._update_states(scheduler_output) + assert _is_sampling_metadata_changed(model_runner, metadata_before) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) @@ -200,8 +210,9 @@ def test_update_states_no_changes(model_runner): free_encoder_input_ids=[], ) - batch_changed = model_runner._update_states(scheduler_output) - assert batch_changed is False + metadata_before = model_runner.input_batch.sampling_metadata + model_runner._update_states(scheduler_output) + assert not _is_sampling_metadata_changed(model_runner, metadata_before) assert _is_req_added(model_runner, req_id) assert _is_req_scheduled(model_runner, req_id) @@ -233,8 +244,8 @@ def test_update_states_request_unscheduled(model_runner): free_encoder_input_ids=[], ) - batch_changed = model_runner._update_states(scheduler_output) - assert batch_changed is True + metadata_before = model_runner._update_states(scheduler_output) + assert _is_sampling_metadata_changed(model_runner, metadata_before) assert _is_req_added(model_runner, req_ids[0]) assert _is_req_scheduled(model_runner, req_ids[0]) diff --git a/vllm/model_executor/layers/utils.py b/vllm/model_executor/layers/utils.py index dfe71028c..a9ef97391 100644 --- a/vllm/model_executor/layers/utils.py +++ b/vllm/model_executor/layers/utils.py @@ -45,7 +45,7 @@ def apply_penalties(logits: torch.Tensor, prompt_tokens_tensor: torch.Tensor, vocab_size, num_seqs) output_bin_counts, output_mask = get_token_bin_counts_and_mask( output_tokens_tensor, vocab_size, num_seqs) - repetition_penalties = repetition_penalties.unsqueeze_(dim=1).repeat( + repetition_penalties = repetition_penalties.unsqueeze(dim=1).repeat( 1, vocab_size) logits[logits > 0] /= torch.where(prompt_mask | output_mask, repetition_penalties, 1.0)[logits > 0] @@ -53,6 +53,6 @@ def apply_penalties(logits: torch.Tensor, prompt_tokens_tensor: torch.Tensor, repetition_penalties, 1.0)[logits <= 0] # We follow the definition in OpenAI API. # Refer to https://platform.openai.com/docs/api-reference/parameter-details - logits -= frequency_penalties.unsqueeze_(dim=1) * output_bin_counts - logits -= presence_penalties.unsqueeze_(dim=1) * output_mask + logits -= frequency_penalties.unsqueeze(dim=1) * output_bin_counts + logits -= presence_penalties.unsqueeze(dim=1) * output_mask return logits diff --git a/vllm/v1/core/scheduler.py b/vllm/v1/core/scheduler.py index 8f1083425..535aa644c 100644 --- a/vllm/v1/core/scheduler.py +++ b/vllm/v1/core/scheduler.py @@ -195,8 +195,10 @@ class Scheduler: request.num_computed_tokens - request.num_tokens) if num_scheduled_spec_tokens > 0: + # Trim spec_token_ids list to num_scheduled_spec_tokens. + del request.spec_token_ids[num_scheduled_spec_tokens:] scheduled_spec_decode_tokens[request.request_id] = ( - request.spec_token_ids[:num_scheduled_spec_tokens]) + request.spec_token_ids) # Encoder-related. if encoder_inputs_to_schedule: @@ -567,7 +569,7 @@ class Scheduler: outputs.append( EngineCoreOutput( request_id=req_id, - new_token_ids=new_token_ids or [], + new_token_ids=new_token_ids, finish_reason=request.get_finished_reason(), new_logprobs=new_logprobs, new_prompt_logprobs_tensors=prompt_logprobs_tensors, diff --git a/vllm/v1/sample/metadata.py b/vllm/v1/sample/metadata.py index ea64181c0..2184a1866 100644 --- a/vllm/v1/sample/metadata.py +++ b/vllm/v1/sample/metadata.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 from dataclasses import dataclass -from typing import Dict, List, Optional, Set +from typing import Dict, List, Optional, Set, Tuple import torch @@ -12,15 +12,13 @@ class SamplingMetadata: temperature: torch.Tensor all_greedy: bool all_random: bool - rejection_sampling: bool - spec_token_ids: List[List[int]] - top_p: torch.Tensor - top_k: torch.Tensor - no_top_p: bool - no_top_k: bool - min_p: torch.Tensor - no_min_p: bool + # None when there are no speculated tokens. + spec_token_ids: Optional[List[List[int]]] + + top_p: Optional[torch.Tensor] + top_k: Optional[torch.Tensor] + min_p: Optional[torch.Tensor] generators: Dict[int, torch.Generator] @@ -34,7 +32,8 @@ class SamplingMetadata: repetition_penalties: torch.Tensor output_token_ids: List[List[int]] - min_tokens: List[int] - stop_token_ids: List[Set[int]] + + # req_index -> (min_tokens, stop_token_ids) + min_tokens: Dict[int, Tuple[int, Set[int]]] logit_bias: List[Optional[Dict[int, float]]] diff --git a/vllm/v1/sample/ops/penalties.py b/vllm/v1/sample/ops/penalties.py index ba368b44a..8d9f6529f 100644 --- a/vllm/v1/sample/ops/penalties.py +++ b/vllm/v1/sample/ops/penalties.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 -from typing import List, Set, Tuple +from typing import Dict, List, Set, Tuple import torch @@ -8,18 +8,17 @@ from vllm.model_executor.layers.utils import apply_penalties from vllm.utils import is_pin_memory_available, make_tensor_with_pad -def apply_min_token_penalties(logits: torch.Tensor, - output_token_ids: List[List[int]], - stop_token_ids: List[Set[int]], - min_tokens: List[int]) -> None: +def apply_min_token_penalties( + logits: torch.Tensor, output_token_ids: List[List[int]], + min_tokens: Dict[int, Tuple[int, Set[int]]]) -> None: """ Applies minimum token penalty by setting the logits of the stop tokens to -inf. """ min_tokens_logits_to_penalize: List[Tuple[int, int]] = [] - for index, min_token in enumerate(min_tokens): + for index, (min_token, stop_token_ids) in min_tokens.items(): if len(output_token_ids[index]) < min_token: - for stop_token_id in stop_token_ids[index]: + for stop_token_id in stop_token_ids: min_tokens_logits_to_penalize.append((index, stop_token_id)) if min_tokens_logits_to_penalize: logits[tuple(zip(*min_tokens_logits_to_penalize))] = -float("inf") diff --git a/vllm/v1/sample/ops/topk_topp_sampler.py b/vllm/v1/sample/ops/topk_topp_sampler.py index 27431001e..78c88ad8b 100644 --- a/vllm/v1/sample/ops/topk_topp_sampler.py +++ b/vllm/v1/sample/ops/topk_topp_sampler.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 -from typing import Dict +from typing import Dict, Optional import torch import torch.nn as nn @@ -55,13 +55,11 @@ class TopKTopPSampler(nn.Module): self, logits: torch.Tensor, generators: Dict[int, torch.Generator], - no_top_k: bool, - k: torch.Tensor, - no_top_p: bool, - p: torch.Tensor, + k: Optional[torch.Tensor], + p: Optional[torch.Tensor], ) -> torch.Tensor: """PyTorch-native implementation of top-k and top-p sampling.""" - logits = apply_top_k_top_p(logits, no_top_k, k, no_top_p, p) + logits = apply_top_k_top_p(logits, k, p) probs = logits.softmax(dim=-1, dtype=torch.float32) return random_sample(probs, generators) @@ -69,37 +67,33 @@ class TopKTopPSampler(nn.Module): self, logits: torch.Tensor, generators: Dict[int, torch.Generator], - no_top_k: bool, - k: torch.Tensor, - no_top_p: bool, - p: torch.Tensor, + k: Optional[torch.Tensor], + p: Optional[torch.Tensor], ) -> torch.Tensor: """More optimized implementation for top-k and top-p sampling.""" probs = logits.softmax(dim=-1, dtype=torch.float32) - if no_top_k and no_top_p: + if k is None and p is None: # We prefer `random_sample` over `flashinfer_sample` when sorting is # not needed. This is because `random_sample` does not require # CPU-GPU synchronization while `flashinfer_sample` does. return random_sample(probs, generators) - return flashinfer_sample(probs, no_top_k, k, no_top_p, p, generators) + return flashinfer_sample(probs, k, p, generators) def apply_top_k_top_p( logits: torch.Tensor, - no_top_k: bool, - k: torch.Tensor, - no_top_p: bool, - p: torch.Tensor, + k: Optional[torch.Tensor], + p: Optional[torch.Tensor], ) -> torch.Tensor: """Apply top-k and top-p masks to the logits. This function sorts the logits tensor, which can be slow for large batches. """ - if no_top_k and no_top_p: + if k is None and p is None: return logits logits_sort, logits_idx = logits.sort(dim=-1, descending=False) - if not no_top_k: + if k is not None: # Apply top-k. top_k_mask = logits_sort.size(1) - k.to(torch.long) # Get all the top_k values. @@ -107,7 +101,7 @@ def apply_top_k_top_p( top_k_mask = logits_sort < top_k_mask logits_sort.masked_fill_(top_k_mask, -float("inf")) - if not no_top_p: + if p is not None: # Apply top-p. probs_sort = logits_sort.softmax(dim=-1) probs_sum = probs_sort.cumsum(dim=-1) @@ -147,10 +141,8 @@ def random_sample( def flashinfer_sample( probs: torch.Tensor, - no_top_k: bool, - k: torch.Tensor, - no_top_p: bool, - p: torch.Tensor, + k: Optional[torch.Tensor], + p: Optional[torch.Tensor], generators: Dict[int, torch.Generator], ) -> torch.Tensor: """Sample from the probabilities using FlashInfer. @@ -167,7 +159,7 @@ def flashinfer_sample( does not. Call this function at the end of the forward pass to minimize the synchronization overhead. """ - assert not (no_top_k and no_top_p) + assert not (k is None and p is None) max_top_k_round = 32 batch_size = probs.shape[0] uniform_samples = torch.empty((max_top_k_round, batch_size), @@ -178,11 +170,11 @@ def flashinfer_sample( for i, generator in generators.items(): uniform_samples[:, i].uniform_(generator=generator) - if no_top_k: + if k is None: # Top-p only. next_token_ids, success = flashinfer.sampling.top_p_sampling_from_probs( probs, uniform_samples, p, deterministic=True) - elif no_top_p: + elif p is None: # Top-k only. next_token_ids, success = flashinfer.sampling.top_k_sampling_from_probs( probs, uniform_samples, k, deterministic=True) @@ -194,9 +186,9 @@ def flashinfer_sample( # NOTE: CPU-GPU synchronization happens here. if not success.all(): - if not no_top_k: + if k is not None: probs = flashinfer.sampling.top_k_renorm_prob(probs, k) - if not no_top_p: + if p is not None: probs = flashinfer.sampling.top_p_renorm_prob(probs, p) next_token_ids = flashinfer.sampling.sampling_from_probs( probs, uniform_samples[0], deterministic=True) diff --git a/vllm/v1/sample/rejection_sampler.py b/vllm/v1/sample/rejection_sampler.py index df1da8930..580ad4429 100644 --- a/vllm/v1/sample/rejection_sampler.py +++ b/vllm/v1/sample/rejection_sampler.py @@ -68,6 +68,7 @@ class RejectionSampler(nn.Module): # NOTE: The following input preparationg can be moved # to the model runner with a persistent manner for better # performance. + assert sampling_metadata.spec_token_ids is not None spec_token_ids = sampling_metadata.spec_token_ids max_spec_len = max(len(s) for s in spec_token_ids) batch_size = len(spec_token_ids) @@ -119,6 +120,7 @@ class RejectionSampler(nn.Module): logits: torch.Tensor, sampling_metadata: SamplingMetadata, ) -> SamplerOutput: + assert sampling_metadata.spec_token_ids is not None spec_lens = [len(x) for x in sampling_metadata.spec_token_ids] # Add 1 to include the 'bonus' token. sample_lens = [x + 1 for x in spec_lens] diff --git a/vllm/v1/sample/sampler.py b/vllm/v1/sample/sampler.py index ec6374d12..8e2533eef 100644 --- a/vllm/v1/sample/sampler.py +++ b/vllm/v1/sample/sampler.py @@ -26,7 +26,7 @@ class Sampler(nn.Module): logits: torch.Tensor, sampling_metadata: SamplingMetadata, ) -> SamplerOutput: - if sampling_metadata.rejection_sampling: + if sampling_metadata.spec_token_ids: if sampling_metadata.max_num_logprobs: raise NotImplementedError( "Rejection sampling does not support logprobs.") @@ -104,16 +104,14 @@ class Sampler(nn.Module): logits = self.apply_temperature(logits, sampling_metadata.temperature) # Apply min_p. - if not sampling_metadata.no_min_p: + if sampling_metadata.min_p is not None: logits = self.apply_min_p(logits, sampling_metadata.min_p) # Apply top_k and/or top_p. random_sampled = self.topk_topp_sampler( logits, sampling_metadata.generators, - sampling_metadata.no_top_k, sampling_metadata.top_k, - sampling_metadata.no_top_p, sampling_metadata.top_p, ) @@ -179,9 +177,10 @@ class Sampler(nn.Module): logits: torch.Tensor, sampling_metadata: SamplingMetadata, ) -> torch.Tensor: - apply_min_token_penalties(logits, sampling_metadata.output_token_ids, - sampling_metadata.stop_token_ids, - sampling_metadata.min_tokens) + if sampling_metadata.min_tokens: + apply_min_token_penalties(logits, + sampling_metadata.output_token_ids, + sampling_metadata.min_tokens) if not sampling_metadata.no_penalties: assert sampling_metadata.prompt_token_ids is not None logits = apply_all_penalties( diff --git a/vllm/v1/utils.py b/vllm/v1/utils.py index 5494542c1..5be465014 100644 --- a/vllm/v1/utils.py +++ b/vllm/v1/utils.py @@ -188,3 +188,14 @@ def bind_kv_cache( for layer_name, kv_cache in kv_caches.items(): # NOTE: Use list because of v0 PP virtual engine. forward_context[layer_name].kv_cache = [kv_cache] + + +def copy_slice(from_tensor: torch.Tensor, to_tensor: torch.Tensor, + length: int) -> None: + """ + Copy the first length elements of a tensor into another tensor in a + non-blocking manner. + + Used to copy pinned CPU tensor data to pre-allocated GPU tensors. + """ + to_tensor[:length].copy_(from_tensor[:length], non_blocking=True) diff --git a/vllm/v1/worker/gpu_input_batch.py b/vllm/v1/worker/gpu_input_batch.py index cb7411a44..ccafc325b 100644 --- a/vllm/v1/worker/gpu_input_batch.py +++ b/vllm/v1/worker/gpu_input_batch.py @@ -1,9 +1,8 @@ # SPDX-License-Identifier: Apache-2.0 - # Datastructures defining an input batch from dataclasses import dataclass -from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple +from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, cast import numpy as np import torch @@ -12,6 +11,7 @@ from vllm.lora.request import LoRARequest from vllm.multimodal import MultiModalKwargs from vllm.sampling_params import SamplingParams, SamplingType from vllm.v1.sample.metadata import SamplingMetadata +from vllm.v1.utils import copy_slice from vllm.v1.worker.block_table import BlockTable _SAMPLING_EPS = 1e-5 @@ -63,7 +63,7 @@ class InputBatch: self.pin_memory = pin_memory self.vocab_size = vocab_size - self.req_ids: List[Optional[str]] = [None] * max_num_reqs + self._req_ids: List[Optional[str]] = [] self.req_id_to_index: Dict[str, int] = {} # TODO(woosuk): This buffer could be too large if max_model_len is big. @@ -171,11 +171,8 @@ class InputBatch: self.repetition_penalties_cpu_tensor.numpy() self.repetition_penalties_reqs: Set[str] = set() - self.min_tokens: List[int] = [0] * max_num_reqs - self.stop_token_ids: List[Set[int]] = [ - set() for _ in range(max_num_reqs) - ] - self.prompt_token_ids: Optional[torch.Tensor] = None + # req_index -> (min_tokens, stop_token_ids) + self.min_tokens: Dict[int, Tuple[int, Set[int]]] = {} # lora related self.request_lora_mapping = np.zeros((self.max_num_reqs, ), @@ -196,6 +193,17 @@ class InputBatch: self.logit_bias: List[Optional[Dict[int, float]]] = [None] * max_num_reqs + self.req_output_token_ids: List[Optional[List[int]]] = [] + + # This is updated each time the batch constituents change. + self.sampling_metadata = self._make_sampling_metadata() + + @property + def req_ids(self) -> List[str]: + # None elements should only be present transiently + # while performing state updates to the batch. + return cast(List[str], self._req_ids) + def add_request( self, request: "CachedRequestState", @@ -206,7 +214,13 @@ class InputBatch: assert req_index < self.max_num_reqs req_id = request.req_id - self.req_ids[req_index] = req_id + if req_index == len(self._req_ids): + self._req_ids.append(req_id) + self.req_output_token_ids.append(request.output_token_ids) + else: + self._req_ids[req_index] = req_id + self.req_output_token_ids[req_index] = request.output_token_ids + self.req_id_to_index[req_id] = req_index # Copy the prompt token ids and output token ids. @@ -255,8 +269,9 @@ class InputBatch: req_index] = sampling_params.repetition_penalty if sampling_params.repetition_penalty != 1.0: self.repetition_penalties_reqs.add(req_id) - self.min_tokens[req_index] = sampling_params.min_tokens - self.stop_token_ids[req_index] = sampling_params.all_stop_token_ids + if sampling_params.min_tokens: + self.min_tokens[req_index] = (sampling_params.min_tokens, + sampling_params.all_stop_token_ids) # NOTE(woosuk): self.generators should not include the requests that # do not have their own generator. @@ -284,16 +299,20 @@ class InputBatch: self.request_lora_mapping[req_index] = 0 def remove_request(self, req_id: str) -> Optional[int]: + """This method must always be followed by a call to condense().""" + req_index = self.req_id_to_index.pop(req_id, None) if req_index is None: return None - self.req_ids[req_index] = None + self._req_ids[req_index] = None + self.req_output_token_ids[req_index] = None self.greedy_reqs.discard(req_id) self.random_reqs.discard(req_id) self.top_p_reqs.discard(req_id) self.top_k_reqs.discard(req_id) self.min_p_reqs.discard(req_id) + self.min_tokens.pop(req_index, None) self.frequency_penalties_reqs.discard(req_id) self.presence_penalties_reqs.discard(req_id) self.repetition_penalties_reqs.discard(req_id) @@ -313,33 +332,17 @@ class InputBatch: self.logit_bias[req_index] = None return req_index - def clear(self) -> None: - self.req_ids = [None] * self.max_num_reqs - self.req_id_to_index.clear() - self.greedy_reqs.clear() - self.random_reqs.clear() - self.top_p_reqs.clear() - self.top_k_reqs.clear() - self.min_p_reqs.clear() - self.frequency_penalties_reqs.clear() - self.presence_penalties_reqs.clear() - self.repetition_penalties_reqs.clear() - self.generators.clear() - self.num_logprobs.clear() - self.num_prompt_logprobs.clear() - self.request_lora_mapping.fill(0) - self.lora_id_to_lora_request.clear() - self.lora_id_to_request_ids.clear() - self.logit_bias = [None] * self.max_num_reqs - def condense(self, empty_req_indices: List[int]) -> None: - if self.num_reqs == 0: + num_reqs = self.num_reqs + if num_reqs == 0: # The batched states are empty. + self._req_ids.clear() + self.req_output_token_ids.clear() return # NOTE(woosuk): This function assumes that the empty_req_indices # is sorted in descending order. - last_req_index = self.num_reqs + len(empty_req_indices) - 1 + last_req_index = num_reqs + len(empty_req_indices) - 1 while empty_req_indices: # Find the largest non-empty index. while last_req_index in empty_req_indices: @@ -351,10 +354,13 @@ class InputBatch: break # Swap the states. - req_id = self.req_ids[last_req_index] + req_id = self._req_ids[last_req_index] + output_token_ids = self.req_output_token_ids[last_req_index] assert req_id is not None - self.req_ids[empty_index] = req_id - self.req_ids[last_req_index] = None + self._req_ids[empty_index] = req_id + self._req_ids[last_req_index] = None + self.req_output_token_ids[empty_index] = output_token_ids + self.req_output_token_ids[last_req_index] = None self.req_id_to_index[req_id] = empty_index num_tokens = self.num_tokens[last_req_index] @@ -379,13 +385,14 @@ class InputBatch: self.repetition_penalties_cpu[ empty_index] = self.repetition_penalties_cpu[last_req_index] self.min_p_cpu[empty_index] = self.min_p_cpu[last_req_index] - self.min_tokens[empty_index] = self.min_tokens[last_req_index] - self.stop_token_ids[empty_index] = self.stop_token_ids[ - last_req_index] generator = self.generators.pop(last_req_index, None) if generator is not None: self.generators[empty_index] = generator + min_token = self.min_tokens.pop(last_req_index, None) + if min_token is not None: + self.min_tokens[empty_index] = min_token + self.request_lora_mapping[empty_index] = self.request_lora_mapping[ last_req_index] @@ -394,87 +401,71 @@ class InputBatch: # Decrement last_req_index since it is now empty. last_req_index -= 1 - def make_sampling_metadata( - self, - req_id_output_token_ids: Dict[str, List[int]], - req_id_to_spec_token_ids: Dict[str, List[int]], - skip_copy: bool = False, - ) -> SamplingMetadata: - if not skip_copy: - self.temperature[:self.num_reqs].copy_( - self.temperature_cpu_tensor[:self.num_reqs], non_blocking=True) - self.top_p[:self.num_reqs].copy_( - self.top_p_cpu_tensor[:self.num_reqs], non_blocking=True) - self.top_k[:self.num_reqs].copy_( - self.top_k_cpu_tensor[:self.num_reqs], non_blocking=True) - self.min_p[:self.num_reqs].copy_( - self.min_p_cpu_tensor[:self.num_reqs], non_blocking=True) - if not self.no_penalties: - # Since syncing these tensors is expensive only copy them - # if necessary i.e. if there are requests which require - # penalties to be applied during sampling. - self.frequency_penalties[:self.num_reqs].copy_( - self.frequency_penalties_cpu_tensor[:self.num_reqs], - non_blocking=True, - ) - self.presence_penalties[:self.num_reqs].copy_( - self.presence_penalties_cpu_tensor[:self.num_reqs], - non_blocking=True, - ) - self.repetition_penalties[:self.num_reqs].copy_( - self.repetition_penalties_cpu_tensor[:self.num_reqs], - non_blocking=True, - ) - # The prompt tokens are used only for applying penalties during - # the sampling process. Hence copy these tensors only when - # there are requests which need penalties to be applied. - self.prompt_token_ids = self._make_prompt_token_ids_tensor() - - output_token_ids: List[List[int]] = [] - spec_token_ids: List[List[int]] = [] - rejection_sampling = False - for req_id in self.req_ids[:self.num_reqs]: - assert req_id is not None - # Currently we create a tensor for output_token_ids from scratch - # at each step. However, for the penalties computation what we - # need is stats about the token ids present in the output. This - # stats can be maintained incrementally instead of computing it - # from scratch at each step. - # TODO - Replace this with incremental update to output token - # statistics. - output_token_ids.append(req_id_output_token_ids[req_id]) - req_spec_token_ids = req_id_to_spec_token_ids.get(req_id, []) - spec_token_ids.append(req_spec_token_ids) - if req_spec_token_ids: - # If any of the requests require speculative decoding, set the - # flag to True. - rejection_sampling = True + # Trim lists to the batch size. + del self._req_ids[self.num_reqs:] + del self.req_output_token_ids[self.num_reqs:] + + def refresh_sampling_metadata(self): + self.sampling_metadata = self._make_sampling_metadata() + + def _make_sampling_metadata(self) -> SamplingMetadata: + num_reqs = self.num_reqs + copy_slice(self.temperature_cpu_tensor, self.temperature, num_reqs) + if not self.no_top_p: + copy_slice(self.top_p_cpu_tensor, self.top_p, num_reqs) + if not self.no_top_k: + copy_slice(self.top_k_cpu_tensor, self.top_k, num_reqs) + if not self.no_min_p: + copy_slice(self.min_p_cpu_tensor, self.min_p, num_reqs) + + if not self.no_penalties: + # Since syncing these tensors is expensive only copy them + # if necessary i.e. if there are requests which require + # penalties to be applied during sampling. + copy_slice(self.frequency_penalties_cpu_tensor, + self.frequency_penalties, num_reqs) + copy_slice(self.presence_penalties_cpu_tensor, + self.presence_penalties, num_reqs) + copy_slice(self.repetition_penalties_cpu_tensor, + self.repetition_penalties, num_reqs) + + # The prompt tokens are used only for applying penalties during + # the sampling process. Hence copy these tensors only when + # there are requests which need penalties to be applied. + prompt_token_ids = self._make_prompt_token_ids_tensor() + else: + prompt_token_ids = None return SamplingMetadata( - temperature=self.temperature[:self.num_reqs], + temperature=self.temperature[:num_reqs], all_greedy=self.all_greedy, all_random=self.all_random, - rejection_sampling=rejection_sampling, - top_p=self.top_p[:self.num_reqs], - top_k=self.top_k[:self.num_reqs], - min_p=self.min_p[:self.num_reqs], - no_min_p=self.no_min_p, - no_top_p=self.no_top_p, - no_top_k=self.no_top_k, + top_p=None if self.no_top_p else self.top_p[:num_reqs], + top_k=None if self.no_top_k else self.top_k[:num_reqs], + min_p=None if self.no_min_p else self.min_p[:num_reqs], generators=self.generators, max_num_logprobs=self.max_num_logprobs, - prompt_token_ids=self.prompt_token_ids, - frequency_penalties=self.frequency_penalties[:self.num_reqs], - presence_penalties=self.presence_penalties[:self.num_reqs], - repetition_penalties=self.repetition_penalties[:self.num_reqs], - output_token_ids=output_token_ids, - spec_token_ids=spec_token_ids, - min_tokens=self.min_tokens[:self.num_reqs], - stop_token_ids=self.stop_token_ids[:self.num_reqs], + prompt_token_ids=prompt_token_ids, + frequency_penalties=self.frequency_penalties[:num_reqs], + presence_penalties=self.presence_penalties[:num_reqs], + repetition_penalties=self.repetition_penalties[:num_reqs], + output_token_ids=cast(List[List[int]], self.req_output_token_ids), + spec_token_ids=None, + min_tokens=self.min_tokens, no_penalties=self.no_penalties, - logit_bias=self.logit_bias[:self.num_reqs], + logit_bias=self.logit_bias[:num_reqs], ) + def get_sampling_metadata( + self, + req_id_to_spec_token_ids: Dict[str, List[int]], + ) -> SamplingMetadata: + # Set the new spec token ids in the cached sampling metadata. + self.sampling_metadata.spec_token_ids = [ + req_id_to_spec_token_ids.get(req_id, []) for req_id in self.req_ids + ] if req_id_to_spec_token_ids else None + return self.sampling_metadata + def _make_prompt_token_ids_tensor(self) -> torch.Tensor: max_prompt_len = self.num_prompt_tokens[:self.num_reqs].max() prompt_token_ids_cpu_tensor = torch.empty( diff --git a/vllm/v1/worker/gpu_model_runner.py b/vllm/v1/worker/gpu_model_runner.py index 5754422cb..0ecc00acc 100644 --- a/vllm/v1/worker/gpu_model_runner.py +++ b/vllm/v1/worker/gpu_model_runner.py @@ -31,7 +31,6 @@ from vllm.v1.engine.mm_input_cache import MMInputCacheClient from vllm.v1.kv_cache_interface import (FullAttentionSpec, KVCacheConfig, KVCacheSpec) from vllm.v1.outputs import LogprobsTensors, ModelRunnerOutput -from vllm.v1.sample.metadata import SamplingMetadata from vllm.v1.sample.rejection_sampler import INVALID_TOKEN_ID from vllm.v1.spec_decode.ngram_proposer import NgramProposer from vllm.v1.utils import bind_kv_cache @@ -224,16 +223,15 @@ class GPUModelRunner(LoRAModelRunnerMixin): pin_memory=self.pin_memory) self.seq_lens_np = self.seq_lens_cpu.numpy() - def _update_states(self, scheduler_output: "SchedulerOutput") -> bool: + def _update_states(self, scheduler_output: "SchedulerOutput") -> None: """Update the cached states and the persistent batch with the scheduler output. The updated states are used by the `_prepare_inputs` function to create the input GPU tensors for the model. - Returns: - True if there is a new/resumed/paused/finished request in the batch. - If False, we can skip copying SamplingMetadata to the GPU. + The SamplingMetadata is updated and copied to the GPU if there is a + new/resumed/paused/finished request in the batch. """ # Remove finished requests from the cached states. for req_id in scheduler_output.finished_req_ids: @@ -344,9 +342,12 @@ class GPUModelRunner(LoRAModelRunnerMixin): num_new_tokens = (num_computed_tokens + len(req_data.new_token_ids) - req_state.num_tokens) - new_token_ids = (req_data.new_token_ids[-num_new_tokens:] - if num_new_tokens > 0 else []) - req_state.output_token_ids.extend(new_token_ids) + if num_new_tokens == 1: + # Avoid slicing list in most common case. + req_state.output_token_ids.append(req_data.new_token_ids[-1]) + elif num_new_tokens > 0: + req_state.output_token_ids.extend( + req_data.new_token_ids[-num_new_tokens:]) # Update the block IDs. if not req_data.resumed_from_preemption: # Append the new blocks to the existing block IDs. @@ -380,7 +381,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): self.input_batch.num_tokens_no_spec[req_index] = end_token_index # Add spec_token_ids to token_ids_cpu. spec_token_ids = scheduler_output.scheduled_spec_decode_tokens.get( - req_id, []) + req_id, ()) if spec_token_ids: start_index = end_token_index end_token_index += len(spec_token_ids) @@ -410,7 +411,8 @@ class GPUModelRunner(LoRAModelRunnerMixin): if removed_req_indices: self.input_batch.condense(removed_req_indices) - return batch_changed + if batch_changed: + self.input_batch.refresh_sampling_metadata() def _prepare_inputs( self, @@ -429,8 +431,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): # TODO: The Python loop can be slow. Optimize. num_scheduled_tokens = np.empty(num_reqs, dtype=np.int32) max_num_scheduled_tokens = 0 - for i, req_id in zip(range(num_reqs), self.input_batch.req_ids): - assert req_id is not None + for i, req_id in enumerate(self.input_batch.req_ids): num_tokens = scheduler_output.num_scheduled_tokens[req_id] num_scheduled_tokens[i] = num_tokens max_num_scheduled_tokens = max(max_num_scheduled_tokens, @@ -669,10 +670,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): def _calc_mrope_positions(self, scheduler_output: "SchedulerOutput"): mrope_pos_ptr = 0 - num_reqs = self.input_batch.num_reqs - for index, req_id in enumerate(self.input_batch.req_ids[:num_reqs]): - assert req_id is not None - + for index, req_id in enumerate(self.input_batch.req_ids): req = self.requests[req_id] assert req.mrope_positions is not None @@ -726,12 +724,11 @@ class GPUModelRunner(LoRAModelRunnerMixin): self, scheduler_output: "SchedulerOutput", cu_num_tokens: np.ndarray, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> torch.Tensor: # Get the number of spec decode tokens for each request. num_reqs = self.input_batch.num_reqs num_spec_decode_tokens = np.empty(num_reqs, dtype=np.int32) - for i, req_id in zip(range(num_reqs), self.input_batch.req_ids): - assert req_id is not None + for i, req_id in enumerate(self.input_batch.req_ids): num_spec_decode_tokens[i] = len( scheduler_output.scheduled_spec_decode_tokens.get(req_id, ())) @@ -769,22 +766,6 @@ class GPUModelRunner(LoRAModelRunnerMixin): return torch.from_numpy(spec_decode_logits_indices).to( self.device, non_blocking=True) - def _prepare_sampling( - self, - batch_changed: bool, - req_to_spec_token_ids: Dict[str, List[int]], - ) -> SamplingMetadata: - # Create the sampling metadata. - req_id_output_token_ids: Dict[str, List[int]] = \ - {req_id: req.output_token_ids \ - for req_id, req in self.requests.items()} - - sampling_metadata = self.input_batch.make_sampling_metadata( - req_id_output_token_ids, - req_to_spec_token_ids, - skip_copy=not batch_changed) - return sampling_metadata - def _execute_encoder(self, scheduler_output: "SchedulerOutput"): scheduled_encoder_inputs = scheduler_output.scheduled_encoder_inputs if not scheduled_encoder_inputs: @@ -838,9 +819,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): scheduler_output: "SchedulerOutput", ) -> List[torch.Tensor]: encoder_outputs: List[torch.Tensor] = [] - num_reqs = self.input_batch.num_reqs - for req_id in self.input_batch.req_ids[:num_reqs]: - assert req_id is not None + for req_id in self.input_batch.req_ids: num_scheduled_tokens = scheduler_output.num_scheduled_tokens[ req_id] req_state = self.requests[req_id] @@ -882,7 +861,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): scheduler_output: "SchedulerOutput", intermediate_tensors: Optional[IntermediateTensors] = None, ) -> Union[ModelRunnerOutput, torch.Tensor]: - batch_changed = self._update_states(scheduler_output) + self._update_states(scheduler_output) if self.is_multimodal_model: # Run the multimodal encoder if any. @@ -964,8 +943,8 @@ class GPUModelRunner(LoRAModelRunnerMixin): logits = self.model.compute_logits(sample_hidden_states, None) # Sample the next token and get logprobs if needed. - sampling_metadata = self._prepare_sampling( - batch_changed, scheduler_output.scheduled_spec_decode_tokens) + sampling_metadata = self.input_batch.get_sampling_metadata( + scheduler_output.scheduled_spec_decode_tokens) sampler_output = self.model.sample( logits=logits, sampling_metadata=sampling_metadata, @@ -973,14 +952,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): # TODO(woosuk): The following loop can be slow since it iterates over # the requests one by one. Optimize. - num_reqs = self.input_batch.num_reqs - req_ids: List[str] = [] - # Because `input_batch.req_ids` is a list of length `max_num_reqs`, - # we need to stop at `num_reqs`. - # FIXME(woosuk): This is hacky. Refactor. - for i, req_id in zip(range(num_reqs), self.input_batch.req_ids): - assert req_id is not None - req_ids.append(req_id) + for i, req_id in enumerate(self.input_batch.req_ids): req_state = self.requests[req_id] seq_len = (req_state.num_computed_tokens + scheduler_output.num_scheduled_tokens[req_id]) @@ -1027,7 +999,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): valid_sampled_token_ids) model_runner_output = ModelRunnerOutput( - req_ids=req_ids, + req_ids=self.input_batch.req_ids, req_id_to_index=self.input_batch.req_id_to_index, sampled_token_ids=valid_sampled_token_ids, spec_token_ids=spec_token_ids, @@ -1041,19 +1013,18 @@ class GPUModelRunner(LoRAModelRunnerMixin): sampled_token_ids: List[List[int]], ) -> List[List[int]]: # TODO(woosuk): Optimize. - num_reqs = len(sampled_token_ids) draft_token_ids: List[List[int]] = [] - for i in range(num_reqs): - if len(sampled_token_ids[i]) == 0: + for i, sampled_ids in enumerate(sampled_token_ids): + num_sampled_ids = len(sampled_ids) + if not num_sampled_ids: # Skip speculative decoding. draft_token_ids.append([]) continue # Add sampled_token_ids to token_ids_cpu. start_idx = self.input_batch.num_tokens_no_spec[i] - end_idx = start_idx + len(sampled_token_ids[i]) - self.input_batch.token_ids_cpu[ - i, start_idx:end_idx] = sampled_token_ids[i] + end_idx = start_idx + num_sampled_ids + self.input_batch.token_ids_cpu[i, start_idx:end_idx] = sampled_ids drafter_output = self.drafter.propose( self.input_batch.token_ids_cpu[i, :end_idx], self.speculative_config.ngram_prompt_lookup_min, @@ -1204,7 +1175,7 @@ class GPUModelRunner(LoRAModelRunnerMixin): # multiplying the list, to avoid Dynamo from treating them as # tensor aliasing. dummy_kv_caches = [ - torch.tensor([], dtype=torch.float32, device=self.device) + torch.tensor((), dtype=torch.float32, device=self.device) for _ in range(self.num_attn_layers) ] diff --git a/vllm/v1/worker/tpu_model_runner.py b/vllm/v1/worker/tpu_model_runner.py index 4ee6853ba..e60268f04 100644 --- a/vllm/v1/worker/tpu_model_runner.py +++ b/vllm/v1/worker/tpu_model_runner.py @@ -1048,8 +1048,6 @@ def swap_positions(b: InputBatch, id_1, id_2): b.min_tokens[id_1], b.min_tokens[id_2] = b.min_tokens[id_2], b.min_tokens[ id_1] - b.stop_token_ids[id_1], b.stop_token_ids[id_2] = b.stop_token_ids[ - id_2], b.stop_token_ids[id_1] gen_1 = b.generators.pop(id_1, None) gen_2 = b.generators.pop(id_2, None)
[ "vllm.v1.sample.metadata.SamplingMetadata", "vllm.v1.sample.sampler.Sampler", "vllm.v1.worker.gpu_input_batch.InputBatch", "vllm.v1.worker.gpu_model_runner.GPUModelRunner" ]
[ "vllm/v1/spec_decode/metadata.py", "vllm/v1/sample/metadata.py", "vllm/v1/pool/metadata.py", "vllm/v1/sample/tpu/metadata.py", "vllm/v1/sample/sampler.py", "vllm/model_executor/layers/sampler.py", "vllm/v1/sample/tpu/sampler.py", "vllm/v1/worker/gpu_input_batch.py" ]
vllm
H100
lm_eval --model vllm --model_args pretrained=meta-llama/Llama-3.1-8B-Instruct,dtype=auto --tasks gsm8k --batch_size auto --limit 100