-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[SGLang-Diffusion LLM] Add inference support for d3LLM models (arXiv:2601.07568) #20615
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
Open
flowermouse
wants to merge
2
commits into
sgl-project:main
Choose a base branch
from
flowermouse:feat/dllm-llada-dream-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| from typing import List, Tuple, Union | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.srt.dllm.algorithm.base import DllmAlgorithm | ||
| from sglang.srt.dllm.config import DllmConfig | ||
| from sglang.srt.layers.logits_processor import LogitsProcessorOutput | ||
| from sglang.srt.model_executor.forward_batch_info import ForwardBatch | ||
| from sglang.srt.model_executor.model_runner import ModelRunner | ||
|
|
||
|
|
||
| def sample_tokens_with_entropy(logits, temperature=1.0): | ||
| """Compute entropy and sample tokens. Copied from d3LLM.""" | ||
| original_probs = torch.softmax(logits, dim=-1) | ||
| log_probs = torch.log(original_probs + 1e-8) | ||
| entropy = -torch.sum(original_probs * log_probs, dim=-1) | ||
|
|
||
| if temperature == 0: | ||
| samples = torch.argmax(logits, dim=-1) | ||
| else: | ||
| scaled_logits = logits / temperature | ||
| # Convert to probabilities and sample | ||
| probs = torch.softmax(scaled_logits, dim=-1) | ||
| samples = torch.multinomial(probs, num_samples=1).squeeze(-1) | ||
|
|
||
| return entropy, samples | ||
|
|
||
|
|
||
| class EntropyThreshold(DllmAlgorithm): | ||
|
|
||
| def __init__( | ||
| self, | ||
| config: DllmConfig, | ||
| ): | ||
| super().__init__(config) | ||
| self.threshold = config.algorithm_config.get("threshold", 0.95) | ||
|
|
||
| def run( | ||
| self, | ||
| model_runner: ModelRunner, | ||
| forward_batch: ForwardBatch, | ||
| ) -> Tuple[Union[LogitsProcessorOutput, torch.Tensor], List[torch.Tensor], bool]: | ||
| batch_size = forward_batch.batch_size | ||
| # Per-item token counts: supports variable-length sequences (needs_full_prefill) | ||
| item_lens = forward_batch.extend_seq_lens_cpu | ||
| offsets = [0] | ||
| for l in item_lens: | ||
| offsets.append(offsets[-1] + l) | ||
| start_list = [] | ||
| mask_index = forward_batch.input_ids == self.mask_id | ||
|
|
||
| # Fast path: if there is no mask token, forward and save kv cache | ||
| if torch.sum(mask_index).item() == 0: | ||
| out = model_runner.forward(forward_batch, pp_proxy_tensors=None) | ||
| logits_output, can_run_cuda_graph = out.logits_output, out.can_run_graph | ||
|
|
||
| next_token_ids = [] | ||
| return logits_output, next_token_ids, can_run_cuda_graph | ||
|
|
||
| # Calculate start positions for each batch item | ||
| for i in range(batch_size): | ||
| block_input_ids = forward_batch.input_ids[offsets[i] : offsets[i + 1]] | ||
| block_mask_index = block_input_ids == self.mask_id | ||
| start = item_lens[i] - torch.sum(block_mask_index).item() | ||
| start_list.append(start) | ||
|
|
||
| nfe = 0 | ||
| for _ in range(self.block_size): | ||
| mask_index = forward_batch.input_ids == self.mask_id | ||
| if torch.sum(mask_index).item() == 0: | ||
| break | ||
|
|
||
| out = model_runner.forward(forward_batch, pp_proxy_tensors=None) | ||
| nfe += 1 | ||
| logits_output, can_run_cuda_graph = out.logits_output, out.can_run_graph | ||
| for batch_id in range(batch_size): | ||
| s, e = offsets[batch_id], offsets[batch_id + 1] | ||
| block_input_ids = forward_batch.input_ids[s:e] | ||
| block_mask_index = block_input_ids == self.mask_id | ||
| if torch.sum(block_mask_index).item() == 0: | ||
| continue | ||
| curr_logits = logits_output.full_logits[s:e] | ||
|
|
||
| # Entropy-based selection (matching d3LLM's entropy_threshold algorithm) | ||
| mask_logits = curr_logits[block_mask_index] | ||
| entropy, x0 = sample_tokens_with_entropy(mask_logits, temperature=0) | ||
|
|
||
| x = block_input_ids.clone() | ||
| full_entropy = torch.full_like( | ||
| block_input_ids, float("inf"), dtype=curr_logits.dtype | ||
| ) | ||
|
|
||
| x[block_mask_index] = x0 | ||
| full_entropy[block_mask_index] = entropy | ||
|
|
||
| num_mask = block_mask_index.sum().item() | ||
| selected_entropy, select_index = torch.topk( | ||
| full_entropy, num_mask, largest=False | ||
| ) | ||
| transfer_index = torch.zeros_like(block_input_ids, dtype=torch.bool) | ||
|
|
||
| # Always accept the lowest-entropy token; accept others if entropy < threshold | ||
| transfer_index[select_index[0]] = True | ||
| for k in range(1, num_mask): | ||
| if selected_entropy[k] < self.threshold: | ||
| transfer_index[select_index[k]] = True | ||
| else: | ||
| transfer_index[select_index[k]] = False | ||
|
|
||
| block_input_ids[transfer_index] = x[transfer_index] | ||
|
|
||
| out = model_runner.forward(forward_batch, pp_proxy_tensors=None) | ||
| nfe += 1 | ||
| logits_output, can_run_cuda_graph = out.logits_output, out.can_run_graph | ||
| # Build per-sequence next_token_ids using variable-length offsets | ||
| next_token_ids_list = [ | ||
| forward_batch.input_ids[offsets[i] + start_list[i] : offsets[i + 1]] | ||
| for i in range(batch_size) | ||
| ] | ||
|
|
||
| # Token per forward: attach nfe so it flows via customized_info -> meta_info | ||
| logits_output.customized_info = {"nfe": [nfe] * batch_size} | ||
| return logits_output, next_token_ids_list, can_run_cuda_graph | ||
|
|
||
|
|
||
| Algorithm = EntropyThreshold |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The configuration keys and descriptions for
FullAttnMultiBlockin this documentation seem to be inconsistent with the implementation inpython/sglang/srt/dllm/algorithm/full_attn_multi_block.py.Specifically:
block_addin the YAML should likely beblock_add_threshold. Its description "Additional threshold increment per decoding step" is also misleading. Based on the code, it's the "previous block progress threshold to add the next block".decoded_threshshould likely bedecoded_token_threshold. Its description "Threshold for considering a token as 'decoded'" is also not quite accurate. It's the "previous block progress threshold for full activation".sub_block_sizeis documented here but does not appear to be used in theFullAttnMultiBlockalgorithm implementation.Could you please update the documentation to match the implementation for clarity and correctness? This will help users configure the algorithm correctly.