Route reasoning between discrete token space (when uncertain) and latent soft embeddings (when confident). Use maximum next-token probability as a routing threshold to dynamically select the reasoning space, improving accuracy under latent reasoning while reducing computational cost through selective discrete sampling.
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Route reasoning between discrete token space (when uncertain) and latent soft embeddings (when confident). Use maximum next-token probability as a routing threshold to dynamically select the reasoning space, improving accuracy under latent reasoning while reducing computational cost through selective discrete sampling.
ThinkRouter: Efficient Reasoning via Routing between Latent and Discrete
Problem Context
Extended reasoning models can operate in two spaces: discrete token space (one token sampled at a time) or latent space (soft embeddings aggregated from distributions). Latent reasoning is more efficient but accumulates noise from low-confidence steps, leading to spurious high confidence in wrong answers. Discrete reasoning commits to single tokens, avoiding aggregation noise but is slower. ThinkRouter solves this by routing between spaces based on model confidence.
Core Concept
At each reasoning step, ThinkRouter measures the maximum next-token probability. If probability is below a threshold τ, reasoning operates in discrete token space (commitment under uncertainty). If probability meets or exceeds τ, reasoning uses latent soft embeddings (efficient exploration when confident).
This hybrid approach leverages discrete reasoning's robustness when uncertain and latent reasoning's efficiency when confident.
Architecture Overview
Confidence detector: Compute maximum next-token probability at each step
Routing threshold: Learned or grid-searched threshold τ per model-dataset pair
Discrete branch: Sample single token when max_prob < τ
Latent branch: Aggregate soft embeddings when max_prob ≥ τ
Combined reasoning: Seamlessly switch spaces within single reasoning trajectory
Implementation
Step 1: Compute confidence scores and routing decision
Measure model confidence and determine reasoning space.
"""
Compute routing decision based on maximum token probability.
Args:
logits: Raw model logits
temperature: Sampling temperature
Returns:
max_prob: Maximum next-token probability [batch]
routing_mask: Boolean mask for routing (True=latent, False=discrete)
routing_decision: String labels for each sample
"""
# Compute probabilities
1
max
1
# Routing decision
self
return
def
visualize_routing_statistics
self,
max_probs: list,
route_decisions: list
dict
"""Track routing statistics for analysis."""
sum
len
return
'mean_max_prob'
'std_max_prob'
'latent_ratio'
len
'discrete_ratio'
len
Step 2: Discrete branch - token sampling with commitment
Implement discrete reasoning with single token selection.
classDiscreteReasoningBranch:
"""Discrete reasoning: sample and commit to single token."""def__init__(self, temperature: float = 0.7):
self.temperature = temperature
defsample_discrete_token(
self,
logits: torch.Tensor, # [batch, vocab_size]
top_k: int = 50) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Sample single token with commitment.
Args:
logits: Raw model logits
top_k: Only sample from top-k tokens
Returns:
sampled_token_ids: Selected token indices
log_probs: Log probability of selected tokens
"""# Top-k filtering
probs = F.softmax(logits / self.temperature, dim=-1)
# Get top-k probabilities
topk_probs, topk_indices = torch.topk(probs, top_k, dim=-1)
# Renormalize
topk_probs = topk_probs / topk_probs.sum(dim=-1, keepdim=True)
# Sample
batch_size = logits.shape[0]
sampled_positions = torch.multinomial(topk_probs, num_samples=1)
sampled_tokens = topk_indices.gather(-1, sampled_positions).squeeze(-1)
# Compute log probability of sampled tokens
log_probs_full = F.log_softmax(logits / self.temperature, dim=-1)
log_probs = log_probs_full.gather(-1, sampled_tokens.unsqueeze(-1)).squeeze(-1)
return sampled_tokens, log_probs