Repository navigation
Extract and document training losses - #22
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
This PR extracts several training loss computations into a dedicated langset.loss module and rewires training/strategy code to call these shared loss functions, improving reuse and documentation.
Changes:
- Introduces
src/langset/loss.pywith documented loss contexts + kernels (learn/recon, InfoNCE, soft-target CE, stop loss, SupCon, CoT, bridge loss). - Refactors trainer, strategies, bridge emission, and tests to use the new loss APIs instead of inline implementations.
- Replaces direct
wandbimports withimport_module("wandb")in trainer flows.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_replay.py | Updates replay-loss test to call learn_loss via LearnLossContext. |
| src/langset/trainer.py | Removes inline loss helpers and routes training to langset.loss functions; tweaks wandb import. |
| src/langset/strategies.py | Replaces inline multi-latent loss math with calls into shared loss kernels. |
| src/langset/loss.py | New centralized, documented loss implementations and contexts. |
| src/langset/bridge_emission.py | Switches bridge objective computation to bridge_loss. |
Suppressed comments (1)
src/langset/trainer.py:1
import_module("wandb")will raiseModuleNotFoundErrorwhenwandbisn’t installed, but the error won’t clearly explain that it’s an optional dependency required only forreport_to="wandb". Consider catchingImportErrorhere and raising a clearer, actionable message (e.g., “Install wandb or set report_to to 'none'”).
"""Trainer: fit the LLM emitter so `emit(input_text)` lands where `emit(target_text)` does — a native
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| """Synthetic prefix embeddings generated from the latent. Prepending them lets target | ||
| token predictions depend on the latent, so reconstruction gradients update it.""" | ||
| sequence = torch.cat([synthetic_tokens, target_embeddings], dim=1) | ||
| """Combined sequence of synthetic prefix and target text embeddings""" |
Comment on lines
+188
to
+196
| prediction_logits = F.linear(target_hidden.float(), ctx.model.embed.weight.float()) | ||
| """Vocabulary logits derived from the target-prediction hidden states.""" | ||
| return ScalarLoss( | ||
| F.cross_entropy( | ||
| prediction_logits.reshape(-1, ctx.model.vocab_size), | ||
| target_ids.masked_fill(target_mask == 0, -100).reshape(-1), | ||
| ignore_index=-100, | ||
| ) | ||
| ) |
Comment on lines
+348
to
+355
| anchors = F.normalize(ctx.anchors, dim=-1) if ctx.normalize_embeddings else ctx.anchors | ||
| """Anchor vectors, optionally normalized to unit length.""" | ||
| candidates = F.normalize(ctx.candidates, dim=-1) if ctx.normalize_embeddings else ctx.candidates | ||
| """Candidate vectors, optionally normalized to unit length.""" | ||
| logits = anchors @ candidates.t() / ctx.temperature | ||
| """Temperature-scaled embedding similarity logits, one row per anchor.""" | ||
| if ctx.logit_mask is not None: | ||
| logits = logits.masked_fill(ctx.logit_mask, float("-inf")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.