transformers - ✅(Solved) Fix Document limitations of `PreTrainedModel._can_set_*` source inspection logic [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#45162Fetched 2026-04-08 02:22:16
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Timeline (top)
commented ×1mentioned ×1subscribed ×1

PR fix notes

PR #45338: docs: document known limitations of _can_set_attn/experts_implementation source inspection

Description (problem / solution / changelog)

Closes #45162

What this PR does

Expands the docstrings of _can_set_attn_implementation and _can_set_experts_implementation in modeling_utils.py to explicitly document the known limitations of their source-inspection heuristic.

Coordination: I posted on the issue before opening this PR. The maintainer comment (#45162 (comment)) confirmed the approach is "definitely hacky" and that the team is "open to ideas" — this PR addresses the immediate documentation gap as a first step.

Limitations now documented

Both methods now document four known edge cases:

  1. False positives from comments/strings — the searched patterns (eager_attention_forward, ALL_ATTENTION_FUNCTIONS.get_interface(, @use_experts_implementation) may appear inside docstrings, comments, or string constants rather than actual code, causing a spurious True.
  2. Compiled modules (.so / .pyd) — no readable source file. Already handled safely by the __file__ guard (returns False), but this was not documented.
  3. Non-standard import environments (PyInstaller, zipimport, custom loaders) — __file__ may be absent or non-filesystem. Also caught by the guard, but undocumented.
  4. Interactive environments (Jupyter, REPL) — no __file__. Caught by the guard.

No behaviour change

This is a documentation-only PR. No logic was modified.

Testing

No tests changed. make style was run to verify formatting.

Note: Claude Code was used to assist in drafting the expanded docstrings. All changes were reviewed by the submitter.

Changed files

  • src/transformers/modeling_utils.py (modified, +37/-4)
RAW_BUFFERClick to expand / collapse

Hi! While reading the implementation of PreTrainedModel.can_set*, I noticed that it relies on inspecting the module source file via file and performing string-based checks.

This seems to work in typical cases, but there are a few scenarios where it may not behave as expected:

  • The searched strings may appear inside comments or string literals, not actual control flow
  • The module may be compiled (e.g. .so, .pyd) and not have readable source
  • __file__ may not exist or may not point to a real filesystem path (e.g. PyInstaller, zipimport, custom loaders)

Documenting these limitations could already help set expectations. More broadly, this approach may be somewhat fragile, so it might be worth revisiting in the future if a more robust mechanism is feasible.

@Cyrilvallez

extent analysis

TL;DR

Documenting the limitations of the current implementation of PreTrainedModel.can_set* and considering a more robust mechanism in the future may help mitigate potential issues.

Guidance

  • Document the known limitations of the current implementation, including the potential for false positives due to string matches in comments or string literals, and the inability to inspect compiled modules or modules without a readable source file.
  • Consider alternative approaches that do not rely on inspecting the module source file, such as using abstract syntax trees (ASTs) or other program analysis techniques.
  • Investigate the use of inspect module or other built-in Python features to improve the robustness of the implementation.
  • Review the code for potential edge cases, such as PyInstaller, zipimport, or custom loaders, and consider adding additional checks or workarounds as needed.

Notes

The current implementation may not be suitable for all use cases, and a more robust mechanism may be required to ensure reliable behavior.

Recommendation

Apply workaround: Document the limitations of the current implementation and consider alternative approaches to improve robustness, as a more reliable solution may not be feasible at this time.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING