bridge.diffusion.common.dllm#
Diffusion language model utilities: masking, block attention masks, and sampling.
The sampling primitives (add_gumbel_noise, get_num_transfer_tokens,
get_transfer_index) implement the iterative-denoising step shared by every
block-diffusion / masked-dLLM generation loop in this repo (NemotronLabsDiffusion,
LLaDA1.5, β¦). They are model-agnostic: each model keeps its own generation loop
with its own attention semantics (causal-with-KV-cache vs fully bidirectional) but
calls these helpers to score confidence and choose which masked positions to
unmask at each step.
Module Contents#
Functions#
Uniform random masking for diffusion LM training. |
|
Apply Gumbel noise to logits for stochastic sampling. |
|
Compute how many masked tokens to unmask at each diffusion step. |
|
Select which masked positions to unmask at one diffusion step. |
|
Compute the sbd_block_diff attention mask. |
API#
- bridge.diffusion.common.dllm.forward_process_simple_masking(
- input_ids,
- mask_token_id,
- eps=0.001,
- loss_mask=None,
- generator=None,
Uniform random masking for diffusion LM training.
For each sequence in the batch, sample a masking ratio t ~ U(eps, 1) and independently mask each token with probability t.
- Returns:
input_ids with masked positions replaced by mask_token_id masked_indices: boolean mask of shape (b, l) p_mask: per-token masking probability of shape (b, l)
- Return type:
noisy_batch
- bridge.diffusion.common.dllm.add_gumbel_noise(
- logits: torch.Tensor,
- temperature: float,
Apply Gumbel noise to logits for stochastic sampling.
At
temperature == 0this is a no-op (returnslogitsunchanged), so anargmaxover the result is plain greedy decoding.- Parameters:
logits β Unnormalized scores of shape
[..., vocab_size].temperature β Sampling temperature.
0disables noise (greedy).
- Returns:
Noised scores (float64 when noise is applied) whose
argmaxsamples from the temperature-scaled distribution.
- bridge.diffusion.common.dllm.get_num_transfer_tokens(
- mask_index: torch.Tensor,
- steps: int,
Compute how many masked tokens to unmask at each diffusion step.
Distributes the number of masked positions as evenly as possible across
steps, giving the earlier steps the remainder.- Parameters:
mask_index β Boolean tensor
[batch, seq_len](True where masked).steps β Number of denoising steps to spread the unmasking over.
- Returns:
Int64 tensor
[batch, steps]whose rows sum to each sequenceβs mask count.
- bridge.diffusion.common.dllm.get_transfer_index(
- logits: torch.Tensor,
- temperature: float,
- remasking: str,
- mask_index: torch.Tensor,
- x: torch.Tensor,
- num_transfer_tokens: torch.Tensor,
- threshold: Optional[float] = None,
- neg_entropy: bool = False,
Select which masked positions to unmask at one diffusion step.
Samples candidate tokens (
x0) fromlogitsand, among currently masked positions, transfers the highest-confidence ones from mask to real token. Used identically by every block-diffusion generation loop in the repo regardless of attention semantics.- Parameters:
logits β Per-position scores
[batch, seq_len, vocab_size].temperature β Sampling temperature for Gumbel noise (
0= greedy).remasking β Confidence source for ranking:
"low_confidence"uses the softmax probability of the chosen token;"random"uses uniform noise.mask_index β Boolean
[batch, seq_len]marking still-masked positions.x β Current token ids
[batch, seq_len]; non-masked positions are kept.num_transfer_tokens β Per-sequence count of tokens to unmask this step (
[batch]slice of :func:get_num_transfer_tokens). Ignored whenthresholdis set.threshold β If set, transfer every masked position whose confidence exceeds this value instead of a fixed count.
neg_entropy β If True, rank by negative entropy of the distribution instead of the chosen tokenβs probability.
- Returns:
Tuple
(x0, transfer_index)wherex0is the candidate token ids (non-masked positions unchanged) andtransfer_indexis a boolean mask of positions to commit this step.
- bridge.diffusion.common.dllm.compute_block_mask(block_size, max_seq_length)#
Compute the sbd_block_diff attention mask.
The semi-block-diffusion mask is composed of three sub-masks over a doubled sequence [xt | x0] of length 2*max_seq_length:
Block Diagonal (M_BD): self-attention within noised blocks (xt only)
Offset Block-Causal (M_OBC): cross-attention from xt to past x0 blocks
Fully Causal (M_FC): fully causal attention within x0
- Parameters:
block_size β Block size for block-based attention.
max_seq_length β Length of one half (xt or x0) of the sequence.
- Returns:
BlockMask for use with
flex_attention.