Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate 2 gpu ops during sampling when logit_bias is zero #338

Merged
merged 3 commits into from
Apr 3, 2024
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
40 changes: 32 additions & 8 deletions python/sglang/srt/managers/router/infer_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,14 @@ def prepare_for_extend(self, vocab_size: int, int_token_logit_bias: torch.Tensor
] = out_cache_loc[pt : pt + extend_lens[i]]
pt += extend_lens[i]

# Handle logit bias
logit_bias = torch.zeros((bs, vocab_size), dtype=torch.float32, device=device)
# Handle logit bias but only allocate when needed
logit_bias = None
for i in range(bs):
if reqs[i].sampling_params.dtype == "int":
if logit_bias is None:
logit_bias = torch.zeros(
(bs, vocab_size), dtype=torch.float32, device=device
)
logit_bias[i] = int_token_logit_bias

# Set fields
Expand Down Expand Up @@ -433,9 +437,12 @@ def filter_batch(self, unfinished_indices: List[int]):
"presence_penalties",
"logit_bias",
]:
setattr(self, item, getattr(self, item)[new_indices])
self_val = getattr(self, item, None)
# logit_bias can be None
if self_val is not None:
setattr(self, item, self_val[new_indices])

def merge(self, other):
def merge(self, other: "Batch"):
self.reqs.extend(other.reqs)

self.req_pool_indices = torch.concat(
Expand All @@ -456,17 +463,34 @@ def merge(self, other):
"top_ks",
"frequency_penalties",
"presence_penalties",
"logit_bias",
]:
setattr(
self, item, torch.concat([getattr(self, item), getattr(other, item)])
self_val = getattr(self, item, None)
other_val = getattr(other, item, None)
setattr(self, item, torch.concat([self_val, other_val]))

# logit_bias can be None
if self.logit_bias is not None or other.logit_bias is not None:
vocab_size = (
self.logit_bias.shape[1]
if self.logit_bias is not None
else other.logit_bias.shape[1]
)
if self.logit_bias is None:
self.logit_bias = torch.zeros(
(len(self.reqs), vocab_size), dtype=torch.float32, device="cuda"
)
if other.logit_bias is None:
other.logit_bias = torch.zeros(
(len(other.reqs), vocab_size), dtype=torch.float32, device="cuda"
)
self.logit_bias = torch.concat([self.logit_bias, other.logit_bias])

def sample(self, logits: torch.Tensor):
# Post process logits
logits = logits.contiguous()
logits.div_(self.temperatures)
logits.add_(self.logit_bias)
if self.logit_bias is not None:
logits.add_(self.logit_bias)

has_regex = any(req.regex_fsm is not None for req in self.reqs)
if has_regex:
Expand Down