Skip to content

Call to contributions: refactor output tracing in transformers #43979

Description

@molbap

Following #43590 that updates 112 models, we want to finish migrating all models to the standardized output collection interface. So, this is a call to contributors to open PRs to link to this meta-issue and learn about the codebase!

Two decorators replace the old manual boilerplate:

  • @capture_outputs: goes on the base model forward. Automatically collects hidden_states/attentions via hooks on submodules declared in _can_record_outputs. Removes the need for explicit output_attentions/output_hidden_states/return_dict handling.
  • @can_return_tuple: goes on wrapper forwards (ForCausalLM, etc.). Handles return_dict only without hook installation.

How to refactor a model

  1. Add _can_record_outputs = {"hidden_states": DecoderLayer, "attentions": Attention} on the PreTrainedModel subclass. Use class references, not strings
  2. Add @capture_outputs on the base model forward (the one with the layer loop)
  3. Add @can_return_tuple on higher-level forwards
  4. Drop output_attentions, output_hidden_states, return_dict from signatures
  5. Remove parameter resolution lines and manual collection loops
  6. Decoder layers return hidden_states directly; attention always returns (attn_output, attn_weights)

You can use llama, mistral, or qwen2 as reference implementations. Modern agents are of some help in this issue, but correctly placing the decorators can be tricky.

Models to update

Please claim one or several models by commenting before starting. One PR per model (or small group of related models) could be ideal, for instance!

As of today here's the remaining list of models:

  • bark
  • beit
  • bit
  • blip_2
  • bloom
  • canine
  • codegen
  • colqwen2
  • cpmant
  • ctrl
  • cvt
  • dab_detr
  • dac
  • data2vec
  • deberta
  • deberta_v2
  • decision_transformer
  • depth_anything
  • depth_pro
  • dinat
  • donut
  • dpr
  • efficientnet
  • encodec
  • falcon
  • falcon_mamba
  • fastspeech2_conformer
  • flaubert
  • flava
  • fnet
  • focalnet
  • fsmt
  • funnel
  • glpn
  • gpt2
  • gpt_neo
  • gpt_neox_japanese
  • gptj
  • granite_speech
  • grounding_dino
  • hgnet_v2
  • hiera
  • hubert
  • ibert
  • imagegpt
  • kyutai_speech_to_text
  • led
  • levit
  • lilt
  • llama4
  • longformer
  • longt5
  • luke
  • lxmert
  • mamba
  • mamba2
  • mask2former
  • maskformer
  • megatron_bert
  • mgp_str
  • mimi
  • mm_grounding_dino
  • mobilenet_v1
  • mobilenet_v2
  • mobilevit
  • mobilevitv2
  • moshi
  • mpnet
  • mpt
  • mra
  • mt5
  • mvp
  • nystromformer
  • omdet_turbo
  • oneformer
  • openai
  • patchtsmixer
  • patchtst
  • perceiver
  • pix2struct
  • poolformer
  • pop2piano
  • prophetnet
  • pvt
  • pvt_v2
  • rag
  • recurrent_gemma
  • reformer
  • regnet
  • rembert
  • resnet
  • roformer
  • rt_detr
  • rwkv
  • seamless_m4t
  • seamless_m4t_v2
  • segformer
  • seggpt
  • sew
  • sew_d
  • speech_encoder_decoder
  • speecht5
  • squeezebert
  • superglue
  • superpoint
  • swiftformer
  • swin
  • swin2sr
  • swinv2
  • t5
  • table_transformer
  • tapas
  • textnet
  • timesformer
  • timm_backbone
  • trocr
  • tvp
  • udop
  • umt5
  • unispeech
  • unispeech_sat
  • univnet
  • upernet
  • vilt
  • vision_encoder_decoder
  • vision_text_dual_encoder
  • visual_bert
  • vitdet
  • vitmatte
  • vits
  • wav2vec2
  • wav2vec2_bert
  • wav2vec2_conformer
  • wavlm
  • xcodec
  • xlm
  • xlnet
  • yoso
  • zamba
  • zoedepth

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions