[GPT2] Refactor output tracing to use capture_outputs/can_return_tuple decorators#44059
Open
lakprigan wants to merge 1 commit intohuggingface:mainfrom
Open
[GPT2] Refactor output tracing to use capture_outputs/can_return_tuple decorators#44059lakprigan wants to merge 1 commit intohuggingface:mainfrom
lakprigan wants to merge 1 commit intohuggingface:mainfrom
Conversation
…decorators Part of huggingface#43979. Migrates GPT2 to the standardized output collection interface, removing ~89 lines of manual output_attentions, output_hidden_states, and return_dict boilerplate. Changes: - Add _can_record_outputs to GPT2PreTrainedModel (including cross_attentions via OutputRecorder) - Add @capture_outputs on GPT2Model.forward() - Add @can_return_tuple on all wrapper model forwards - GPT2Block returns a single tensor instead of a tuple - GPT2Attention always returns (attn_output, attn_weights)
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: gpt2 |
Author
This was referenced Apr 29, 2026
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.
Summary
Migrates GPT2 to the standardized output collection interface as part of #43979.
_can_record_outputstoGPT2PreTrainedModel(includingcross_attentionsviaOutputRecordertargeting thecrossattentionsubmodule)@capture_outputsonGPT2Model.forward()@can_return_tupleon all wrapper model forwards (GPT2LMHeadModel,GPT2DoubleHeadsModel,GPT2ForSequenceClassification,GPT2ForTokenClassification,GPT2ForQuestionAnswering)output_attentions,output_hidden_states, andreturn_dicthandling from all forward methodsGPT2Block.forward()now returns a singletorch.Tensorinstead of a tupleGPT2Attention.forward()always returns(attn_output, attn_weights)— hooks capture weights when neededNet reduction: ~89 lines of boilerplate removed (44 insertions, 133 deletions).
Testing
All 136 non-compile GPT2 tests pass. The 2
torch.compiletest failures (test_generate_compilation_all_outputs,test_generate_compile_model_forward_fullgraph) are pre-existing environment-specific issues (arm64 torch inductor) and also fail on unmodifiedmain.References
Used
llama,nllb_moe, andt5gemmaas reference implementations for the cross-attentionOutputRecorderpattern.