Skip to content

Commit b22ee16

Browse files
committed
update
Signed-off-by: Woosuk Kwon <[email protected]>
1 parent 1dcafb3 commit b22ee16

File tree

4 files changed

+70
-63
lines changed

4 files changed

+70
-63
lines changed

vllm/v1/worker/gpu/input_batch.py

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from dataclasses import dataclass
44
from typing import Any
55

6-
import numba
76
import numpy as np
87
import torch
98

@@ -36,9 +35,6 @@ def __init__(
3635
self.seq_lens = torch.zeros(max_num_reqs, dtype=torch.int32, device=device)
3736
self.cu_num_logits = self._make_buffer(max_num_reqs + 1, dtype=torch.int32)
3837

39-
# Spec decoding.
40-
self.next_prefill_tokens = self._make_buffer(max_num_reqs, dtype=torch.int32)
41-
4238
# Structured outputs.
4339
self.bitmask_indices = self._make_buffer(max_num_reqs, dtype=torch.int32)
4440
self.grammar_bitmask = self._make_buffer(
@@ -146,41 +142,61 @@ def make_dummy(
146142
)
147143

148144

149-
@numba.njit(cache=True)
150-
def _prepare_prefill_inputs(
151-
idx_mapping: np.ndarray, # [B]
152-
query_lens: np.ndarray, # [B]
153-
query_start_loc: np.ndarray, # [B + 1]
154-
prefill_token_ids: np.ndarray, # [N, max_model_len]
155-
num_computed_prefill_tokens: np.ndarray, # [N]
156-
input_ids: np.ndarray, # [num_input_tokens]
157-
) -> None:
158-
num_reqs = idx_mapping.shape[0]
159-
query_starts = query_start_loc[:num_reqs]
160-
query_ends = query_start_loc[1 : num_reqs + 1]
161-
starts = num_computed_prefill_tokens[idx_mapping]
162-
ends = starts + query_lens
163-
for i in range(num_reqs):
164-
input_ids[query_starts[i] : query_ends[i]] = prefill_token_ids[
165-
idx_mapping[i], starts[i] : ends[i]
166-
]
145+
@triton.jit
146+
def _prepare_prefill_inputs_kernel(
147+
input_ids_ptr,
148+
next_prefill_tokens_ptr,
149+
idx_mapping_ptr,
150+
query_start_loc_ptr,
151+
prefill_token_ids_ptr,
152+
prefill_token_ids_stride,
153+
prefill_lens_ptr,
154+
num_computed_tokens_ptr,
155+
BLOCK_SIZE: tl.constexpr,
156+
):
157+
batch_idx = tl.program_id(0)
158+
req_state_idx = tl.load(idx_mapping_ptr + batch_idx)
159+
prefill_len = tl.load(prefill_lens_ptr + req_state_idx)
160+
num_computed = tl.load(num_computed_tokens_ptr + req_state_idx)
161+
if num_computed >= prefill_len:
162+
# Not prefill.
163+
return
164+
165+
query_start = tl.load(query_start_loc_ptr + batch_idx)
166+
query_end = tl.load(query_start_loc_ptr + batch_idx + 1)
167+
query_len = query_end - query_start
168+
169+
prefill_ptr = prefill_token_ids_ptr + req_state_idx * prefill_token_ids_stride
170+
for i in range(0, query_len, BLOCK_SIZE):
171+
block = i + tl.arange(0, BLOCK_SIZE)
172+
mask = block < query_len
173+
tokens = tl.load(prefill_ptr + num_computed + block, mask=mask)
174+
tl.store(input_ids_ptr + query_start + block, tokens, mask=mask)
175+
176+
next_token = tl.load(prefill_ptr + num_computed + query_len)
177+
tl.store(next_prefill_tokens_ptr + req_state_idx, next_token)
167178

168179

169180
def prepare_prefill_inputs(
170-
idx_mapping: np.ndarray,
171-
num_scheduled_tokens: np.ndarray,
172-
query_start_loc: np.ndarray,
173-
prefill_token_ids: np.ndarray,
174-
num_computed_prefill_tokens: np.ndarray,
175-
input_ids: np.ndarray,
181+
input_ids: torch.Tensor,
182+
next_prefill_tokens: torch.Tensor,
183+
idx_mapping: torch.Tensor,
184+
query_start_loc: torch.Tensor,
185+
prefill_token_ids: torch.Tensor,
186+
prefill_len: torch.Tensor,
187+
num_computed_tokens: torch.Tensor,
176188
) -> None:
177-
_prepare_prefill_inputs(
189+
num_reqs = idx_mapping.shape[0]
190+
_prepare_prefill_inputs_kernel[(num_reqs,)](
191+
input_ids,
192+
next_prefill_tokens,
178193
idx_mapping,
179-
num_scheduled_tokens,
180194
query_start_loc,
181195
prefill_token_ids,
182-
num_computed_prefill_tokens,
183-
input_ids,
196+
prefill_token_ids.stride(0),
197+
prefill_len,
198+
num_computed_tokens,
199+
BLOCK_SIZE=1024,
184200
)
185201

186202

vllm/v1/worker/gpu/model_runner.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ def __init__(
104104
if self.use_async_scheduling:
105105
self.input_prep_event = torch.cuda.Event()
106106
self.structured_outputs_event = torch.cuda.Event()
107-
self.spec_decode_event = torch.cuda.Event()
108107
else:
109108
self.input_prep_event = None
110109
self.structured_outputs_event = None
111-
self.spec_decode_event = None
112110

113111
if self.speculative_config is not None:
114112
self.do_spec_decode = True
@@ -507,16 +505,16 @@ def prepare_inputs(
507505
query_start_loc_cpu = self.input_buffers.query_start_loc.cpu[: num_reqs + 1]
508506
query_start_loc_np = self.input_buffers.query_start_loc.np[: num_reqs + 1]
509507

510-
# Copy prefill tokens from CPU to GPU.
508+
# Get prefill tokens.
511509
prepare_prefill_inputs(
512-
idx_mapping_np,
513-
num_scheduled_tokens,
514-
query_start_loc_np,
515-
self.req_states.prefill_token_ids.np,
516-
self.req_states.num_computed_prefill_tokens,
517-
self.input_buffers.input_ids.np,
510+
self.input_buffers.input_ids.gpu,
511+
self.req_states.next_prefill_tokens,
512+
idx_mapping,
513+
query_start_loc_gpu,
514+
self.req_states.prefill_token_ids.gpu,
515+
self.req_states.prefill_len.gpu,
516+
self.req_states.num_computed_tokens,
518517
)
519-
self.input_buffers.input_ids.copy_to_gpu(num_tokens)
520518

521519
# Prepare positions and seq_lens.
522520
prepare_pos_seq_lens(
@@ -782,28 +780,21 @@ def propose_draft(
782780
num_sampled: torch.Tensor,
783781
num_rejected: torch.Tensor,
784782
) -> torch.Tensor:
785-
num_reqs = input_batch.num_reqs
786-
idx_mapping_np = input_batch.idx_mapping_np
787-
with async_barrier(self.spec_decode_event):
788-
self.input_buffers.next_prefill_tokens.np[:num_reqs] = (
789-
self.req_states.prefill_token_ids.np[
790-
idx_mapping_np,
791-
self.req_states.num_computed_prefill_tokens[idx_mapping_np],
792-
]
793-
)
794-
next_prefill_tokens = self.input_buffers.next_prefill_tokens.copy_to_gpu(
795-
num_reqs
796-
)
797-
798783
assert self.speculator is not None
784+
last_sampled_tokens = self.req_states.last_sampled_tokens[
785+
input_batch.idx_mapping
786+
]
787+
next_prefill_tokens = self.req_states.next_prefill_tokens[
788+
input_batch.idx_mapping
789+
]
799790
draft_tokens = self.speculator.propose(
800791
input_batch,
801792
sampling_metadata,
802793
last_hidden_states,
803794
aux_hidden_states,
804795
num_sampled,
805796
num_rejected,
806-
self.req_states.last_sampled_tokens,
797+
last_sampled_tokens,
807798
next_prefill_tokens,
808799
)
809800
return draft_tokens

vllm/v1/worker/gpu/spec_decode/eagle.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def propose(
194194
num_sampled: torch.Tensor,
195195
# [num_reqs]
196196
num_rejected: torch.Tensor,
197-
# [max_num_reqs, 1]
197+
# [num_reqs]
198198
last_sampled: torch.Tensor,
199199
# [num_reqs]
200200
next_prefill_tokens: torch.Tensor,
@@ -316,7 +316,6 @@ def _prepare_eagle_inputs_kernel(
316316
eagle_positions_ptr,
317317
target_input_ids_ptr,
318318
target_positions_ptr,
319-
idx_mapping_ptr,
320319
last_sampled_ptr,
321320
next_prefill_tokens_ptr,
322321
num_sampled_ptr,
@@ -335,8 +334,7 @@ def _prepare_eagle_inputs_kernel(
335334

336335
num_sampled = tl.load(num_sampled_ptr + batch_idx)
337336
if num_sampled > 0:
338-
req_state_idx = tl.load(idx_mapping_ptr + batch_idx)
339-
next_token = tl.load(last_sampled_ptr + req_state_idx).to(tl.int32)
337+
next_token = tl.load(last_sampled_ptr + batch_idx).to(tl.int32)
340338
else:
341339
# Chunked prefilling.
342340
# Get the next prefill token.
@@ -368,9 +366,9 @@ def prepare_eagle_inputs(
368366
num_sampled: torch.Tensor,
369367
# [num_reqs]
370368
num_rejected: torch.Tensor,
371-
# [max_num_reqs, 1]
369+
# [num_reqs]
372370
last_sampled: torch.Tensor,
373-
# [max_num_reqs]
371+
# [num_reqs]
374372
next_prefill_tokens: torch.Tensor,
375373
) -> torch.Tensor:
376374
num_reqs = input_batch.num_reqs
@@ -385,7 +383,6 @@ def prepare_eagle_inputs(
385383
input_buffers.positions,
386384
input_batch.input_ids,
387385
input_batch.positions,
388-
input_batch.idx_mapping,
389386
last_sampled,
390387
next_prefill_tokens,
391388
num_sampled,

vllm/v1/worker/gpu/states.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ def __init__(
140140
dtype=torch.int64,
141141
device=device,
142142
)
143+
self.next_prefill_tokens = torch.zeros(
144+
self.max_num_reqs, dtype=torch.int32, device=device
145+
)
143146

144147
# LoRA.
145148
self.lora_ids = np.zeros(self.max_num_reqs, dtype=np.int32)

0 commit comments

Comments
 (0)