Commit 669b878
authored
Add negative log-acceptance (LK) loss for speculative decoding (#657)
## Summary
Adds a negative log-acceptance loss to the speculators loss interface —
the
hyperparameter-free LK loss variant.
## Motivation
Acceptance rate equals the target/draft overlap, `alpha = sum_v min(p_v,
q_v) = 1 - TV`.
This loss is `-log(alpha)`, which optimizes acceptance directly. Its
gradient is
`(1/alpha)*grad(TV)`: the `1/alpha` factor amplifies TV's
otherwise-vanishing gradient when
overlap is low, so it trains well from a cold start where pure TV
stalls. At a point-mass
target it reduces to cross-entropy. Source: Samarin et al., "LK Losses:
Direct Acceptance
Rate Optimization for Speculative Decoding" (arXiv 2602.23881).
## Changes
- `neg_log_acceptance_loss(logits, targets) -> [1, seq_len]` in
`metrics.py`, reusing the
same overlap term as `tv_loss`, with an `_EPS` floor before the log.
- Registered as `"nla"` in `resolve_loss_fn` (short name open to
maintainer preference).
- Unit tests: identical-dist = 0, equals `-log(overlap)`, reduces to
cross-entropy at a
point-mass target, finite at zero overlap, and resolution.
## Scope / notes
- Negative-log-acceptance variant only. The hybrid LK loss (`lambda*KL +
(1-lambda)*TV` with an
adaptive, eta-controlled weight) is deferred to a follow-up, since it
needs a
loss-hyperparameter interface decision. The e2e variant and kernel
optimizations are out of
scope.
- Target distribution is treated as the (detached) reference, consistent
with the existing
`kl_div`/`ce`/`tv` losses which assume frozen-target logits.
Signed-off-by: GIREESH7963 <abburigireesh@gmail.com>1 parent 9e322bd commit 669b878
2 files changed
Lines changed: 79 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 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 | + | |
149 | 178 | | |
150 | 179 | | |
151 | 180 | | |
| |||
186 | 215 | | |
187 | 216 | | |
188 | 217 | | |
189 | | - | |
190 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
191 | 221 | | |
192 | 222 | | |
193 | 223 | | |
| |||
199 | 229 | | |
200 | 230 | | |
201 | 231 | | |
| 232 | + | |
202 | 233 | | |
203 | 234 | | |
204 | 235 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
118 | 120 | | |
119 | 121 | | |
120 | 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 | + | |
121 | 167 | | |
122 | 168 | | |
123 | 169 | | |
| |||
0 commit comments