[SFT] Treat past_key_values and attentions as optional on the model output - #6686
Open
Hakureirm wants to merge 2 commits into
Open
[SFT] Treat past_key_values and attentions as optional on the model output#6686Hakureirm wants to merge 2 commits into
Hakureirm wants to merge 2 commits into
Conversation
…utput `_chunked_ce_forward` reads `outputs.past_key_values` and `outputs.attentions` unconditionally when rebuilding the output. Models whose forward returns neither raise `AttributeError` on the first step, and `loss_type` defaults to `"chunked_nll"`, so `SFTTrainer` fails out of the box for them. Ten model families in the causal-LM auto-mapping declare a return type without `past_key_values` (mamba, mamba2, falcon_mamba, xlstm and rwkv keep the recurrence under `cache_params` or `state`; xlnet uses `mems`); none of them carries `attentions` either. `last_hidden_state` and `hidden_states`, the two fields the chunked path reads elsewhere, are present on all of them, so reading the other two with `getattr` is enough. Covered by adding `trl-internal-testing/tiny-FalconMambaForCausalLM` to `TestSFTTrainer::test_train`, which uses the default `loss_type` and so fails before this change and passes after.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit eb54dda. Configure here.
Review feedback: `getattr` conflicts with the repo's own rule against `hasattr`/`getattr` (`.ai/AGENTS.md`), and the two fields are never populated here anyway — the backbone is called with `use_cache=False` and never asked for attentions. Measured on `trl-internal-testing/tiny-Qwen2ForCausalLM-2.5`: at the point the output is rebuilt, both are `None`. Passing them as `None` keeps this path independent of whether a given model type declares the fields at all, which is the property the FalconMamba test covers.
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.

What does this PR do?
_chunked_ce_forwardreadsoutputs.past_key_valuesandoutputs.attentionsunconditionally when it rebuilds the output, so a model whose forward returns
neither raises
AttributeErroron the first step.loss_typedefaults to"chunked_nll", soSFTTrainerfails out of the box for those models.Neither field is ever populated here — the backbone is called with
use_cache=Falseand attentions are never requested — so they are passed asNone. Measured ontrl-internal-testing/tiny-Qwen2ForCausalLM-2.5, at thepoint the output is rebuilt, both are already
Nonetoday.last_hidden_stateand
hidden_states, the fields the chunked path actually reads, are present onevery output type involved.
Ten entries of
MODEL_FOR_CAUSAL_LM_MAPPING_NAMES(transformers 5.14.1) declarea return type without
past_key_values—mamba,mamba2,falcon_mamba,xlstmandrwkvkeep the recurrence undercache_paramsorstate,xlnetunder
mems, andopenai-gpt,recurrent_gemma,reformerandxlmhave nostate field at all.
Test.
trl-internal-testing/tiny-FalconMambaForCausalLMis added toTestSFTTrainer::test_train, which uses a defaultSFTConfigand therefore thedefault
loss_type; it fails onmainand passes here, on CPU and on an A800.No behaviour change for models that carry the fields. Training
trl-internal-testing/tiny-Qwen2ForCausalLM-2.5for 3 epochs under a fixed seedon CPU gives byte-identical per-step losses and parameter sums with and without
this change.
tests/test_sft_trainer.pyon this branch: 112 passed / 153 skipped on CPU,187 passed / 78 skipped on an A800, no failures or errors either way.
Fixes #6685
Before submitting
AI writing disclosure