Skip to content

Commit dcdf48d

Browse files
fzyzcjyjimoosciuc
authored andcommitted
Let bench_one_batch support enable_dp_attention (sgl-project#4058)
1 parent c4a6c35 commit dcdf48d

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

python/sglang/bench_one_batch.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
from sglang.srt.entrypoints.engine import _set_envs_and_config
6161
from sglang.srt.hf_transformers_utils import get_tokenizer
6262
from sglang.srt.managers.schedule_batch import Req, ScheduleBatch
63+
from sglang.srt.managers.scheduler import Scheduler
6364
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
6465
from sglang.srt.model_executor.model_runner import ModelRunner
6566
from sglang.srt.sampling.sampling_params import SamplingParams
@@ -184,6 +185,7 @@ def prepare_inputs_for_correctness_test(bench_args, tokenizer):
184185
req.prefix_indices = []
185186
req.fill_ids = req.origin_input_ids
186187
req.extend_input_len = len(req.fill_ids) - len(req.prefix_indices)
188+
req.logprob_start_len = len(req.origin_input_ids) - 1
187189
reqs.append(req)
188190

189191
return input_ids, reqs
@@ -199,6 +201,7 @@ def prepare_extend_inputs_for_correctness_test(
199201
i, : bench_args.cut_len
200202
]
201203
req.extend_input_len = len(req.fill_ids) - len(req.prefix_indices)
204+
req.logprob_start_len = len(req.origin_input_ids) - 1
202205
return reqs
203206

204207

@@ -220,6 +223,7 @@ def prepare_synthetic_inputs_for_latency_test(batch_size, input_len):
220223
req.prefix_indices = []
221224
req.fill_ids = req.origin_input_ids
222225
req.extend_input_len = len(req.fill_ids) - len(req.prefix_indices)
226+
req.logprob_start_len = len(req.origin_input_ids) - 1
223227
reqs.append(req)
224228

225229
return reqs
@@ -238,6 +242,7 @@ def extend(reqs, model_runner):
238242
enable_custom_logit_processor=False,
239243
)
240244
batch.prepare_for_extend()
245+
_maybe_prepare_dp_attn_batch(batch, model_runner)
241246
model_worker_batch = batch.get_model_worker_batch()
242247
forward_batch = ForwardBatch.init_new(model_worker_batch, model_runner)
243248
logits_output = model_runner.forward(forward_batch)
@@ -249,13 +254,28 @@ def extend(reqs, model_runner):
249254
def decode(input_token_ids, batch, model_runner):
250255
batch.output_ids = input_token_ids
251256
batch.prepare_for_decode()
257+
_maybe_prepare_dp_attn_batch(batch, model_runner)
252258
model_worker_batch = batch.get_model_worker_batch()
253259
forward_batch = ForwardBatch.init_new(model_worker_batch, model_runner)
254260
logits_output = model_runner.forward(forward_batch)
255261
next_token_ids = model_runner.sample(logits_output, forward_batch)
256262
return next_token_ids, logits_output.next_token_logits
257263

258264

265+
def _maybe_prepare_dp_attn_batch(batch: ScheduleBatch, model_runner):
266+
if model_runner.server_args.enable_dp_attention:
267+
Scheduler.prepare_dp_attn_batch_raw(
268+
batch,
269+
dp_size=model_runner.server_args.dp_size,
270+
attn_tp_size=1,
271+
tp_cpu_group=model_runner.tp_group.cpu_group,
272+
get_idle_batch=None,
273+
disable_cuda_graph=model_runner.server_args.disable_cuda_graph,
274+
spec_algorithm=SpeculativeAlgorithm.NONE,
275+
speculative_num_draft_tokens=None,
276+
)
277+
278+
259279
def correctness_test(
260280
server_args,
261281
port_args,

python/sglang/srt/managers/scheduler.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,14 +1706,36 @@ def process_batch_result(
17061706
self.send_to_tokenizer.send_pyobj(HealthCheckOutput())
17071707

17081708
def prepare_dp_attn_batch(self, local_batch: ScheduleBatch):
1709+
return self.prepare_dp_attn_batch_raw(
1710+
local_batch,
1711+
dp_size=self.server_args.dp_size,
1712+
attn_tp_size=self.attn_tp_size,
1713+
tp_cpu_group=self.tp_cpu_group,
1714+
get_idle_batch=self.get_idle_batch,
1715+
disable_cuda_graph=self.server_args.disable_cuda_graph,
1716+
spec_algorithm=self.spec_algorithm,
1717+
speculative_num_draft_tokens=self.server_args.speculative_num_draft_tokens,
1718+
)
1719+
1720+
@staticmethod
1721+
def prepare_dp_attn_batch_raw(
1722+
local_batch: ScheduleBatch,
1723+
dp_size,
1724+
attn_tp_size: int,
1725+
tp_cpu_group,
1726+
get_idle_batch,
1727+
disable_cuda_graph: bool,
1728+
spec_algorithm,
1729+
speculative_num_draft_tokens,
1730+
):
17091731
# Check if other DP workers have running batches
17101732
if local_batch is None:
17111733
num_tokens = 0
17121734
global_num_tokens_for_logprob = 0
17131735
elif local_batch.forward_mode.is_decode():
17141736
num_tokens = local_batch.batch_size()
1715-
if not self.spec_algorithm.is_none() and self.spec_algorithm.is_eagle():
1716-
num_tokens = num_tokens * self.server_args.speculative_num_draft_tokens
1737+
if not spec_algorithm.is_none() and spec_algorithm.is_eagle():
1738+
num_tokens = num_tokens * speculative_num_draft_tokens
17171739
global_num_tokens_for_logprob = num_tokens
17181740
else:
17191741
num_tokens = local_batch.extend_num_tokens
@@ -1732,7 +1754,7 @@ def prepare_dp_attn_batch(self, local_batch: ScheduleBatch):
17321754
else:
17331755
can_cuda_graph = 0
17341756

1735-
if not self.spec_algorithm.is_none():
1757+
if not spec_algorithm.is_none():
17361758
# TODO(sang): Support cuda graph when idle batch is there.
17371759
if local_batch is None or local_batch.forward_mode.is_idle():
17381760
can_cuda_graph = 0
@@ -1750,28 +1772,28 @@ def prepare_dp_attn_batch(self, local_batch: ScheduleBatch):
17501772
dtype=torch.int64,
17511773
)
17521774
global_info = torch.empty(
1753-
(self.server_args.dp_size, self.attn_tp_size, 4),
1775+
(dp_size, attn_tp_size, 4),
17541776
dtype=torch.int64,
17551777
)
17561778
torch.distributed.all_gather_into_tensor(
17571779
global_info.flatten(),
17581780
local_info,
1759-
group=self.tp_cpu_group,
1781+
group=tp_cpu_group,
17601782
)
17611783
global_num_tokens = global_info[:, 0, 0].tolist()
17621784
can_cuda_graph = min(global_info[:, 0, 1].tolist())
17631785
global_num_tokens_for_logprob = global_info[:, 0, 2].tolist()
17641786
is_extend_in_batch = global_info[:, 0, 3].tolist()
17651787

17661788
if local_batch is None and max(global_num_tokens) > 0:
1767-
local_batch = self.get_idle_batch()
1789+
local_batch = get_idle_batch()
17681790

17691791
if local_batch is not None:
17701792
local_batch.global_num_tokens = global_num_tokens
17711793
local_batch.global_num_tokens_for_logprob = global_num_tokens_for_logprob
17721794

17731795
# Check forward mode for cuda graph
1774-
if not self.server_args.disable_cuda_graph:
1796+
if not disable_cuda_graph:
17751797
local_batch.can_run_dp_cuda_graph = can_cuda_graph
17761798

17771799
return local_batch, any(is_extend_in_batch)

0 commit comments

Comments
 (0)