Skip to content
Open
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
34 changes: 33 additions & 1 deletion python/sglang/srt/speculative/eagle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from sglang.srt.runtime_context import get_parallel, get_spec
from sglang.srt.utils import (
get_bool_env_var,
is_cpu,
is_cuda,
is_hip,
Expand All @@ -42,6 +43,9 @@

_is_cuda = is_cuda()
_is_hip = is_hip()
_SPEC_TEMPERATURE_SAMPLING_TARGET_VERIFY = (
get_bool_env_var("SGLANG_SPEC_TEMPERATURE_SAMPLING_TARGET_VERIFY") and _is_hip
)
_is_npu = is_npu()
_is_musa = is_musa()
_is_xpu = is_xpu()
Expand Down Expand Up @@ -374,6 +378,28 @@ def verify_tree_greedy_triton(
)


def select_target_predict(
next_token_logits: torch.Tensor,
sampling_info: SamplingBatchInfo,
draft_token_num: int,
) -> torch.Tensor:
if not _SPEC_TEMPERATURE_SAMPLING_TARGET_VERIFY:
return torch.argmax(next_token_logits, dim=-1)

if sampling_info.need_top_p_sampling or sampling_info.need_top_k_sampling:
logger.warning_once(
"SGLANG_SPEC_TEMPERATURE_SAMPLING_TARGET_VERIFY does not implement "
"top_p/top_k truncation yet; falling back to greedy verify."
)
return torch.argmax(next_token_logits, dim=-1)

temperatures = torch.repeat_interleave(
sampling_info.temperatures, draft_token_num, dim=0
)
probs = torch.softmax(next_token_logits / temperatures, dim=-1)
return torch.multinomial(probs, num_samples=1).flatten()


def verify_tree_greedy_func(
predicts: torch.Tensor,
accept_index: torch.Tensor,
Expand Down Expand Up @@ -781,8 +807,14 @@ def eagle_sample(

# Sample tokens
target_predict = None
if sampling_info.is_all_greedy or _is_cpu or _is_hip or _is_xpu:
if sampling_info.is_all_greedy or _is_cpu or _is_xpu:
target_predict = torch.argmax(next_token_logits, dim=-1)
elif _is_hip:
target_predict = select_target_predict(
next_token_logits, sampling_info, verify_input.draft_token_num
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do:

elif _is_hip:
    target_predict = select_target_predict(
        next_token_logits, sampling_info, verify_input.draft_token_num
    )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


if target_predict is not None:
target_predict = target_predict.reshape(bs, verify_input.draft_token_num)
predict, accept_index, num_correct_drafts = verify_tree_greedy_func(
predicts=predict, # mutable
Expand Down
Loading