Implement adaptive parallel decoding for language models using diffusion-based next-sequence prediction. Enable dynamic block-based token generation with confidence thresholds to achieve 2x+ speedups while maintaining competitive performance. Retrofit existing autoregressive models with minimal additional training data.
Installer avec Codex ou Claude Copiez ce prompt, collez-le dans Codex, Claude ou un autre assistant, puis laissez-le vérifier la page du skill et l'installer pour vous.
Une commande directe contourne le prompt de vérification. Examinez la source avant de l'exécuter.
Implement adaptive parallel decoding for language models using diffusion-based next-sequence prediction. Enable dynamic block-based token generation with confidence thresholds to achieve 2x+ speedups while maintaining competitive performance. Retrofit existing autoregressive models with minimal additional training data.
Achieve Adaptive Fast Decoding for Language Models
This skill teaches how to implement Sequential Diffusion Language Models (SDLMs), which generate multiple tokens per inference step by combining diffusion principles with autoregressive language modeling. Instead of generating one token at a time, SDLMs predict variable-length sequences within fixed-size masked blocks, then dynamically select which predictions to keep based on model confidence.
The Problem
Standard autoregressive language models generate one token per forward pass, making inference inherently sequential and slow. Existing multi-token prediction methods either require architectural modifications, don't support KV caching for efficiency, or use fixed output block sizes that don't adapt to prediction difficulty.
Core Concept: Next Sequence Prediction (NSP)
NSP generalizes next-token and next-block prediction into a unified framework. The model predicts a full sequence within a masked block during training and inference, then uses confidence scores to determine how many predicted tokens to accept at each generation step. This creates an adaptive length generation strategy: easy tokens get accepted quickly, hard tokens trigger shorter steps that allow the model to refine predictions.
The key insight is that "causality matters for historical context but tokens being generated in the current step can attend to each other bidirectionally." This enables parallel training on next-block predictions while maintaining autoregressive semantics during inference.
Architecture Overview
Training Block Setup: Partition input sequence into historical context (masked to attend only to previous tokens) and prediction block (full mutual attention allowed). Train the model to predict masked token positions within the prediction block using a custom attention mask.
Attention Mechanism: Apply causal masking for historical tokens u<i. Apply full mutual attention for prediction tokens u,v≥i. This enables parallel computation during training while respecting causality at generation time.
Confidence Selection: After each forward pass within a block, use logit probabilities or entropy-normalized scores to determine how many generated tokens to accept. Tokens below a confidence threshold remain masked for refinement in the next iteration.
Inference Methods: Two strategies—greedy decoding accepts tokens above a threshold per step, and self-speculative decoding validates predictions through consistency checks before committing them.
KV Cache Compatibility: Unlike fixed block diffusion, NSP maintains standard transformer KV caching. Only tokens added to the sequence extend the cache, enabling efficient streaming generation without special logic.
Implementation Details
Step 1: Create Masked Block Attention Pattern
Implement custom attention masking that enforces causality for history and allows full attention within the prediction block.
import torch
import torch.nn as nn
import torch.nn.functional as F
defcreate_block_attention_mask(seq_len, history_len, block_size, device):
"""
Create attention mask for NSP training.
Args:
seq_len: Total sequence length (history + block)
history_len: Number of historical tokens (read-only)
block_size: Size of prediction block
device: Torch device
Returns:
Mask tensor shape (seq_len, seq_len) where 1 = attend, 0 = mask
"""
mask = torch.zeros(seq_len, seq_len, dtype=torch.bool, device=device)
# Historical tokens can only attend to previous historyfor i inrange(history_len):
mask[i, :i+1] = True# Prediction block tokens (history_len:) can attend to history and each otherfor i inrange(history_len, seq_len):
mask[i, :i+1] = True# Causal for history
mask[i, history_len:seq_len] = True# Full attention within blockreturn mask.unsqueeze(0) # Add batch dimensiondefapply_block_mask_to_attention(attn_scores, mask):
"""
Apply block mask to attention scores before softmax.
Args:
attn_scores: Shape (batch, heads, query_len, key_len)
mask: Shape (1, seq_len, seq_len)
Returns:
Masked attention scores
"""
attn_scores = attn_scores.masked_fill(~mask, float('-inf'))
return attn_scores
Use cases where quality cannot degrade at all (SDLM trades some quality for speed)
Real-time interactive applications where 2-3 rounds of refinement per block are unacceptable
Models under 1B parameters (training efficiency gains diminish; use standard autoregressive)
Systems already optimized with speculative decoding or other multi-token methods (marginal gains)
Common Pitfalls
Setting confidence threshold too high: Results in single-token generation per step, negating speedup. Start conservative (0.8) and gradually increase if quality permits.
Ignoring KV cache invalidation: When tokens are not accepted and need refinement, ensure KV cache state aligns with current sequence position. Bugs here cause silent correctness errors.
Block size larger than necessary: Large blocks increase prediction difficulty. Confidence threshold must drop to maintain throughput, harming quality. Tune together.
Insufficient training data: Retrofitting works with 3.5M tokens, but model quality depends on data diversity. Use data similar to downstream tasks.
Not ablating confidence method: Logit-based and entropy-based selection have different behaviors. Test both on your quality metrics before production deployment.
Applying to instruction-tuned models without fine-tuning: SDLM requires training on the specific instruction format. Base model training alone may not generalize to instructions.
Mismatch between training and inference confidence thresholds: If you train with one threshold but deploy with another, performance degrades. Must match or re-train.