Shift all logits by -offset without touching the existing weights. Appends a bias column so that ``h_new @ W_new^T = h @ W^T - offset``. Since softmax is shift-invariant the expected sampling distribution is unchanged, but the all-negative logits exercise masked-fill handling in par
(
weights: torch.Tensor,
hidden_states: torch.Tensor,
offset: float,
)
| 209 | |
| 210 | |
| 211 | def shift_logits_negative( |
| 212 | weights: torch.Tensor, |
| 213 | hidden_states: torch.Tensor, |
| 214 | offset: float, |
| 215 | ) -> tuple[torch.Tensor, torch.Tensor]: |
| 216 | """Shift all logits by -offset without touching the existing weights. |
| 217 | |
| 218 | Appends a bias column so that ``h_new @ W_new^T = h @ W^T - offset``. |
| 219 | Since softmax is shift-invariant the expected sampling distribution is |
| 220 | unchanged, but the all-negative logits exercise masked-fill handling in |
| 221 | partial V-tiles (kernels must fill masked rows with -inf, not 0, or the |
| 222 | 0 beats all real negative values in the tile-max reduction). |
| 223 | |
| 224 | We use a bias column instead of baking the offset into the logits before |
| 225 | computing the pseudoinverse because bf16 cannot represent fine-grained |
| 226 | all-negative logits for vocab sizes above ~128. The bias column keeps the |
| 227 | original weights (centered near 0) intact and encodes the offset exactly. |
| 228 | """ |
| 229 | vocab_size = weights.shape[0] |
| 230 | n_hidden_states = hidden_states.shape[0] |
| 231 | device = weights.device |
| 232 | dtype = weights.dtype |
| 233 | bias_w = torch.ones(vocab_size, 1, dtype=dtype, device=device) |
| 234 | bias_h = torch.full((n_hidden_states, 1), -offset, dtype=dtype, device=device) |
| 235 | return ( |
| 236 | torch.cat([weights, bias_w], dim=1), |
| 237 | torch.cat([hidden_states, bias_h], dim=1), |
| 238 | ) |
no outgoing calls
no test coverage detected