Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions python/sglang/srt/layers/logits_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ def from_forward_batch(cls, forward_batch: ForwardBatch):
)

def compute_dp_attention_metadata(self):
# TODO(ch-wan): gathered_buffer here is larger than the actual required size in draft extend,
# we may use a smaller buffer in draft extend.

cumtokens = torch.cumsum(self.global_num_tokens_for_logprob_gpu, dim=0)
dp_rank = get_attention_dp_rank()
Expand All @@ -186,6 +184,19 @@ def compute_dp_attention_metadata(self):
self.dp_local_start_pos = dp_local_start_pos
self.dp_local_num_tokens = dp_local_num_tokens

if self.global_num_tokens_for_logprob_cpu is not None:
# create a smaller buffer to reduce peak memory usage
self.gathered_buffer = torch.empty(
(
sum(self.global_num_tokens_for_logprob_cpu),
self.gathered_buffer.shape[1],
),
dtype=self.gathered_buffer.dtype,
device=self.gathered_buffer.device,
)
else:
self.gathered_buffer = torch.empty_like(self.gathered_buffer)


class LogitsProcessor(nn.Module):
def __init__(
Expand Down Expand Up @@ -430,7 +441,7 @@ def _get_logits(
if self.do_tensor_parallel_all_gather_dp_attn:
logits_metadata.compute_dp_attention_metadata()
hidden_states, local_hidden_states = (
torch.empty_like(logits_metadata.gathered_buffer),
logits_metadata.gathered_buffer,
hidden_states,
)
dp_gather_replicate(hidden_states, local_hidden_states, logits_metadata)
Expand Down
Loading