Screen deeper LiSA sharing with complete autoregressive answers

This commit is contained in:
2026-11-24 15:58:43 +08:00
parent 38545d4217
commit 453b2ff787
13 changed files with 2474 additions and 14 deletions
+4 -1
View File
@@ -114,10 +114,13 @@ class _FrozenVocabularyCE(torch.autograd.Function):
return grad_hidden * (gradient * weights[:, None] / weights.sum()), None, None, None
def language_model_loss(hidden, ids, lengths, vocabulary, answer_weight=1.0):
def language_model_loss(hidden, ids, lengths, vocabulary, answer_weight=1.0,
completion_starts=None):
"""Train on all valid next-token targets, avoiding padding and final positions."""
positions = torch.arange(ids.shape[1] - 1, device=ids.device)
valid = positions[None, :] + 1 < lengths[:, None]
if completion_starts is not None:
valid = valid & (positions[None, :] + 1 >= completion_starts[:, None])
weights = torch.where(positions[None, :] + 2 == lengths[:, None], answer_weight, 1.0)
return _FrozenVocabularyCE.apply(hidden[:, :-1][valid], ids[:, 1:][valid],
weights[valid].to(dtype=hidden.dtype), vocabulary)