-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrastive_decoding.py
More file actions
190 lines (156 loc) · 7.37 KB
/
Copy pathcontrastive_decoding.py
File metadata and controls
190 lines (156 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
Contrastive Decoding for Autoregressive Language Models
Implements Contrastive Decoding (Li et al., ACL 2023: "Contrastive Decoding: Open-ended Text Generation as Optimization").
Uses a smaller 'amateur' (draft) model to identify and penalize undesirable linguistic artifacts,
repetition loops, and degenerate tokens produced by the 'expert' model.
Mathematical Formulation:
1. Adaptive Plausibility Constraint (APC):
V_head(x) = { v in V | P_exp(v | x) >= beta * max_w P_exp(w | x) }
Restricts candidate search space to tokens that are plausible according to the expert,
preventing the penalty from selecting hallucinations or nonsensical words.
2. Contrastive Objective:
Score(v) = log P_exp(v | x) - alpha * log P_ama(v | x) for v in V_head(x)
Score(v) = -infinity otherwise
3. Token Selection:
v* = argmax_{v in V_head} Score(v) (Greedy Mode)
or sample from Softmax(Score(v) / tau) (Sampling Mode)
"""
from typing import List, Tuple, Optional, Dict
from dataclasses import dataclass
import numpy as np
@dataclass
class ContrastiveDecodingStats:
"""Statistics tracking performance and behavior of contrastive decoding."""
total_tokens_generated: int = 0
avg_plausibility_set_size: float = 0.0
num_steps: int = 0
plausibility_set_sizes: List[int] = None
def __post_init__(self):
if self.plausibility_set_sizes is None:
self.plausibility_set_sizes = []
class ContrastiveDecoder:
"""
Pure NumPy Contrastive Decoding engine.
"""
def __init__(
self,
alpha: float = 0.5,
beta: float = 0.1,
temperature: float = 1.0,
do_sample: bool = False,
):
"""
Args:
alpha: Weight of amateur model penalty (higher = stronger amateur suppression)
beta: Adaptive Plausibility Constraint (APC) cutoff ratio in (0.0, 1.0]
temperature: Temperature applied to contrastive scores when do_sample=True
do_sample: If True, sample from contrastive score distribution; otherwise pick argmax
"""
self.alpha = float(alpha)
self.beta = float(beta)
self.temperature = max(float(temperature), 1e-5)
self.do_sample = do_sample
def _get_probs_and_log_probs(self, logits: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Compute numerically stable probability and log-probability distributions."""
if logits.ndim > 1:
logits = logits.flatten()
max_logit = np.max(logits)
exp_logits = np.exp(logits - max_logit)
sum_exp = np.sum(exp_logits) + 1e-12
probs = exp_logits / sum_exp
log_probs = (logits - max_logit) - np.log(sum_exp)
return probs, log_probs
def compute_contrastive_scores(
self,
expert_logits: np.ndarray,
amateur_logits: np.ndarray,
) -> Tuple[np.ndarray, np.ndarray]:
"""
Compute contrastive search scores with Adaptive Plausibility Constraint.
Args:
expert_logits: 1D array of logits from the expert model
amateur_logits: 1D array of logits from the amateur model
Returns:
contrastive_scores: 1D array with -inf for unviable candidates
plausible_indices: 1D boolean mask of valid candidate tokens in V_head
"""
expert_probs, expert_log_probs = self._get_probs_and_log_probs(expert_logits)
_, amateur_log_probs = self._get_probs_and_log_probs(amateur_logits)
# 1. Adaptive Plausibility Constraint: V_head = { v | P_exp(v) >= beta * max_w P_exp(w) }
max_expert_prob = np.max(expert_probs)
plausibility_threshold = self.beta * max_expert_prob
plausible_mask = expert_probs >= plausibility_threshold
# Ensure at least the top expert token is always included
if not np.any(plausible_mask):
plausible_mask[np.argmax(expert_probs)] = True
# 2. Contrastive objective: S(v) = log P_exp(v) - alpha * log P_ama(v)
contrastive_scores = np.full_like(expert_log_probs, -np.inf, dtype=np.float32)
contrastive_scores[plausible_mask] = (
expert_log_probs[plausible_mask] - self.alpha * amateur_log_probs[plausible_mask]
)
return contrastive_scores, plausible_mask
def select_next_token(
self,
expert_logits: np.ndarray,
amateur_logits: np.ndarray,
) -> Tuple[int, int]:
"""
Select next token ID using contrastive objective.
Returns:
selected_token_id: Integer index of selected token
num_plausible: Count of candidates in plausibility set
"""
scores, plausible_mask = self.compute_contrastive_scores(expert_logits, amateur_logits)
num_plausible = int(np.sum(plausible_mask))
if not self.do_sample:
# Greedy contrastive selection
selected_token = int(np.argmax(scores))
return selected_token, num_plausible
# Sampling mode: Softmax over plausible contrastive scores
plausible_scores = scores[plausible_mask] / self.temperature
max_score = np.max(plausible_scores)
exp_scores = np.exp(plausible_scores - max_score)
probs = exp_scores / (np.sum(exp_scores) + 1e-12)
plausible_indices = np.where(plausible_mask)[0]
selected_token = int(np.random.choice(plausible_indices, p=probs))
return selected_token, num_plausible
def generate(
self,
expert_model,
amateur_model,
prompt_ids: List[int],
max_new_tokens: int = 50,
eos_token_id: Optional[int] = None,
) -> Tuple[List[int], ContrastiveDecodingStats]:
"""
Execute full contrastive autoregressive generation.
Args:
expert_model: Competent primary model
amateur_model: Lightweight draft/amateur model
prompt_ids: List of input prompt token IDs
max_new_tokens: Maximum new tokens to generate
eos_token_id: Optional End-of-Sequence token ID to terminate generation
Returns:
generated_ids: List of all tokens (prompt + generated)
stats: ContrastiveDecodingStats with diagnostic information
"""
stats = ContrastiveDecodingStats()
generated = list(prompt_ids)
expert_context_len = getattr(expert_model, "seq_length", 128)
amateur_context_len = getattr(amateur_model, "seq_length", 128)
for _ in range(max_new_tokens):
# Crop inputs to respective context windows
expert_input = np.array([generated[-expert_context_len:]], dtype=np.int32)
amateur_input = np.array([generated[-amateur_context_len:]], dtype=np.int32)
expert_logits = expert_model.forward(expert_input)[0, -1, :]
amateur_logits = amateur_model.forward(amateur_input)[0, -1, :]
next_token, num_plausible = self.select_next_token(expert_logits, amateur_logits)
generated.append(next_token)
stats.total_tokens_generated += 1
stats.num_steps += 1
stats.plausibility_set_sizes.append(num_plausible)
if eos_token_id is not None and next_token == eos_token_id:
break
if stats.plausibility_set_sizes:
stats.avg_plausibility_set_size = float(np.mean(stats.plausibility_set_sizes))
return generated, stats