Skip to content

Add native loss support for JEV decision models - #50586

Open
kashif wants to merge 4 commits into
huggingface:mainfrom
kashif:add-jev-native-loss
Open

kashif wants to merge 4 commits into
huggingface:mainfrom
kashif:add-jev-native-loss

Conversation

@kashif

@kashif kashif commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

JEV currently returns only logits, so training needs a custom loss outside the model. This adds JevForDecisionLoss through LOSS_MAPPING and returns standard loss/logits outputs for Trainer and TRL, while preserving decision-pipeline inference.

The default follows the AutoTrust JEV-9B model card: KL(target || model) over active options, plus 0.5 × RPS for Score questions. Includes hard/soft targets, question weights, training docs, and loss/gradient tests. The loss uses max-excluded log1p normalization to retain confident-prediction gradients.

Extracted from #50411 against main; that PR stays a draft.

Validation: 20 tests passed, 4 skipped, 31 subtests passed; 128 independent float64 objective/gradient comparisons passed. make style passes. make check-repo only reports the existing torch.distributed.get_rank type-check warning, also reproduced on unchanged main.

Who can review?

Anyone interested in decision-model training.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Pages changed in this PR:

@mangeshraut712 mangeshraut712 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.

Nice work: the packed KL + optional RPS objective is carefully written, decide() now forces return_dict=True and reads .logits so it keeps working with JevDecisionOutput, and the independent Categorical/RPS reference test (ragged questions, ignore index, soft labels, weights, confident fp32 gradients, microbatch accumulation) is excellent.

A few residual issues:

  1. Soft labels are documented to "sum to one" but never checked. JevForDecisionLoss will happily take a soft row that sums to 0.5 or 1.5; kl_div will still run and RPS CDFs will be biased. A cheap targets.sum(-1) check on the active slots (or a test that this is rejected / renormalized) would match the docstring.

  2. num_options == 0 / empty packed logits. positions = torch.arange(num_options.max()) and scores.argmax(-1) are undefined or awkward on an empty question. Even a single-question test with zero options (or an empty logits) would show whether this should raise a clear error.

  3. if rps_weight: treats a programmed 0.0 correctly, but a negative weight would still be applied. If the weight is user-facing on JevConfig, a < 0 guard might be worth it.

  4. Shape of logits. The loss indexes logits as 1-D (logits[indices], logits.numel()). The new output type says shape (total_num_options,), which matches the current forward. If a caller ever passes a batched (1, total) tensor, numel() and advanced indexing will go wrong. Worth asserting logits.ndim == 1 (or flattening a leading batch-1 dim) so a future wrapper cannot fail silently.

accepts_loss_kwargs = False is a good call given packed soft labels, and normalizing by valid-question count even when loss_weights is set is consistent with the new docs.

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

NiceQ Smalls nits but will be nice

"""
decision_config = decision_config or self.decision_config
inputs = {"input_ids": input_ids, **kwargs}
inputs = {"input_ids": input_ids, **kwargs, "return_dict": True}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe return tuple decortor missing?

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: jev

This branch has not been deployed

No deployments
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.

4 participants