Add transformers v5.0.0 compatibility#79
Add transformers v5.0.0 compatibility#79speediedan wants to merge 29 commits intodecoderesearch:mainfrom
transformers v5.0.0 compatibility#79Conversation
Squashed changes from arbitrary-logit-attribution branch.
…he functionality proposed in PR decoderesearch#44 and provides a unified foundation for future attribution target enhancements.
…ing cases (decoderesearch#51) * fix: handle single module in offload_modules Fix TypeError when passing a single module (e.g., CrossLayerTranscoder) to offload_modules instead of a list. Now properly handles single modules, lists, and PyTorch container types (ModuleList, ModuleDict, Sequential). * minor aesthetic change, slight simplification of logic
…e range of gpus the test suite runs with (decoderesearch#50)
…PR for new multi-backend arch
…ch sizes to allow running more tests with minimum CI hardware profile
…entation in AttributionTargets
- Fix Gemma3 conditional model prefix (language_model → model.language_model in v5) - Fix GptOss MoE MLP hook reference for v5 reshape changes - Add TRANSFORMERS_GTE_5_0_0 version detection constant - Update hf_utils.py to use HF_XET_HIGH_PERFORMANCE (v5) or HF_HUB_ENABLE_HF_TRANSFER (v4) for sequential downloads - Cast tokenizer.decode() to str to fix pyright type errors - Add assertions for optional config fields in tests - Fix huggingface_hub >=1.3.4 mock construction (require response parameter) - Change GptOss test dtype to float32 (v5 MoE doesn't support float64) - Update pyproject.toml to allow transformers <=5.1.0
There was a problem hiding this comment.
Pull request overview
This PR adds support for transformers>=5.0.0 and huggingface_hub>=1.0.0 while maintaining backward compatibility with transformers v4.x. The changes address breaking API changes in model structure paths, MoE internals, exception constructors, and environment variables.
Changes:
- Added version detection using the
packaginglibrary to conditionally apply v4 vs v5 logic - Updated model path structures for Gemma3ForConditionalGeneration and GptOss MoE models
- Introduced new
AttributionTargetsandLogitTargetclasses to support flexible target specifications including arbitrary string tokens via virtual indices - Updated test fixtures for huggingface_hub v1.0+ API changes and transformers v5 MoE float32 requirements
- Modified environment variable handling for high-performance transfer modes (HF_HUB_ENABLE_HF_TRANSFER → HF_XET_HIGH_PERFORMANCE)
Reviewed changes
Copilot reviewed 29 out of 29 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| circuit_tracer/utils/tl_nnsight_mapping.py | Added version detection and conditional model path mappings for v4/v5 compatibility |
| circuit_tracer/utils/hf_utils.py | Updated environment variable handling for high-performance transfer modes |
| circuit_tracer/attribution/targets.py | New module with AttributionTargets class for flexible target specification |
| circuit_tracer/graph.py | Updated Graph class to use LogitTarget with backward compatibility for tensor format |
| circuit_tracer/attribution/attribute*.py | Updated attribution functions to use new AttributionTargets API |
| circuit_tracer/utils/create_graph_files.py | Updated to handle LogitTarget unpacking for graph file generation |
| tests/utils/test_hf_utils.py | Updated mock error constructors to include required response parameter |
| tests/test_attributions_gpt_oss_nnsight.py | Changed dtype from float64 to float32 with relaxed tolerances for v5 MoE compatibility |
| tests/test_attributions_gemma*.py | Added batch_size parameter to reduce memory usage and added test markers |
| pyproject.toml | Updated version constraints for transformers and huggingface_hub, added pytest-rerunfailures |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@hannamw Validating transformers v5.0.0 support was important for the world model analysis framework I'm building so I figured I should do my part helping out with I've based this v5 transformers compatibility PR on the |
… review feedback, includes comment and mark changes that will be separated into a separate PR
…tion validation approach, remove attribution_targets ref from `Graph`, remove unnecessary vocab_indices properties, convert multi-token string detection handling from warning to error
…icit maintainer/community feedback
- refined initial description and overview of the attribution targets API - updated colab notebook setup to allow testing the attribution-targets branch before merging - improved formatting of the attribution target, token mapping outputs and top feature comparison tables - verified the demo notebook is working as expected in colab
…an_cuda` context manager to allow simple orchestration of sequential backend parity tests. Useful for VRAM constrained environments or to enable parity testing of larger models that may not fit on GPU simultaneously.
…cted the `CustomTarget` examples and helper functions discussion to a distinct section. added a torch.Tensor version of the `Sequence[str]` example for completeness.
This PR adds support for
transformers>=5.0.0andhuggingface_hub>=1.0.0while maintaining backward compatibility with transformers v4.x. The changes address breaking API changes in both libraries that affected model structure paths, MoE internals, and exception constructors.Key adaptations necessary:
Gemma3ForConditionalGenerationKey Changes
1. Version Detection (tl_nnsight_mapping.py)
New module-level version detection:
This constant enables conditional logic throughout the codebase without repeated version checks.
2. Gemma3ForConditionalGeneration Path Updates
Breaking Change in transformers v5:
The multimodal Gemma3 model structure changed from:
to:
Solution:
All paths in
gemma_3_conditional_mappingnow use this dynamic prefix, including:attention_location_patternlayernorm_scale_location_patterns(7 patterns)pre_logit_locationembed_locationembed_weightfeature_hook_mapping(4 hooks)3. GptOss MoE MLP Hook Updates
Breaking Change in transformers v5:
The MoE layer's internal processing changed, affecting which NNSight
.sourcereference captures the 3D hidden states output:(num_tokens, num_experts)sparse(num_tokens, top_k)compactself_experts_0hidden_states_reshape_1Both versions return
(hidden_states, router_scores)tuple, but v5 adds an explicit reshape operation:v5: https://github.com/huggingface/transformers/blob/08810b1/src/transformers/models/gpt_oss/modular_gpt_oss.py#L136-L142
v4: https://github.com/huggingface/transformers/blob/47b0e47/src/transformers/models/gpt_oss/modular_gpt_oss.py#L167-L170
Both
mlp.hook_outandhook_mlp_outingpt_oss_mappingnow use this dynamic hook path.4. Test Fixture Updates
4.1 HuggingFace Utils Tests (test_hf_utils.py)
GatedRepoErrorandRepositoryNotFoundErrorconstructors now require aresponseparameter in huggingface_hub >=1.3.4.Before:
After:
4.2 GptOss Attribution Tests (test_attributions_gpt_oss_nnsight.py)
transformersv5 MoE operations (torch._grouped_mm) don't support float64 dtype.https://github.com/huggingface/transformers/blob/08810b1e278938278c50153ee1edfd7a20a759da/src/transformers/integrations/moe.py#L188
Changes:
float64tofloat32in:load_large_gpt_oss_model_with_dummy_clt()test_large_gpt_oss_model()test_large_gpt_oss_model:5. HuggingFace Transfer Mode Environment Variable (hf_utils.py)
The
HF_HUB_ENABLE_HF_TRANSFERenvironment variable was removed in huggingface_hub v1.0+ and replaced withHF_XET_HIGH_PERFORMANCE.These environment variables control high-performance file transfer modes that require sequential (non-parallel) downloads. The
download_hf_uris()function previously checkedHF_HUB_ENABLE_HF_TRANSFERto decide between sequential and parallel downloads.Solution:
This ensures:
HF_HUB_ENABLE_HF_TRANSFERfor hf_transfer compatibilityHF_XET_HIGH_PERFORMANCEfor hf_xet compatibility6. Dependency Updates (pyproject.toml)
Updated constraints:
Migration Path
For users on transformers v4.x:
No changes required. The version detection ensures v4 paths are used automatically.
For users upgrading to v5:
pip install transformers>=5.0.0pip install huggingface_hub>=1.0.0Backward Compatibility
The changes are designed to be fully backward compatible:
Technical Notes
NNSight
.sourceAttribute BehaviorBoth v4 and v5 return
(hidden_states, router_scores)tuple fromGptOssMLP.forward(). The difference is in how the 3D hidden states are obtained:self_experts_0hidden_states_reshape_1Additionally, the
router_scoresshape changed from sparse(num_tokens, num_experts)in v4 to compact(num_tokens, top_k)in v5.This change affects which NNSight
.sourcereference captures the 3D hidden states output when tracing the computational graph.Gemma3 Conditional Model Structure
The multimodal Gemma3 model wraps the language model differently in v5:
language_model.*model.language_model.*This is consistent with how other multimodal models (like LLaVA) structure their components.