Skip to content

Extract and document training losses - #22

Merged
trashhalo merged 3 commits into
mainfrom
extract-document-loss-functions
Aug 17, 2026
Merged

trashhalo merged 3 commits into
mainfrom
extract-document-loss-functions

Conversation

@trashhalo

Copy link
Copy Markdown
Contributor

No description provided.

@trashhalo
trashhalo requested a balanced review from Copilot August 17, 2026 17:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py with 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 wandb imports with import_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 raise ModuleNotFoundError when wandb isn’t installed, but the error won’t clearly explain that it’s an optional dependency required only for report_to="wandb". Consider catching ImportError here 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.

Comment thread src/langset/loss.py
"""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 thread src/langset/loss.py
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 thread src/langset/loss.py
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"))
@trashhalo
trashhalo merged commit c81f004 into main Aug 17, 2026
2 checks passed
@trashhalo
trashhalo deleted the extract-document-loss-functions branch August 17, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants