Skip to content

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

Closed
RudrenduPaul wants to merge 2 commits intohuggingface:mainfrom
RudrenduPaul:docs/document-can-set-source-inspection-limitations
Closed

docs: document known limitations of _can_set_attn/experts_implementation source inspection#45338
RudrenduPaul wants to merge 2 commits intohuggingface:mainfrom
RudrenduPaul:docs/document-can-set-source-inspection-limitations

Conversation

@RudrenduPaul
Copy link
Copy Markdown
Contributor

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.

RudrenduPaul and others added 2 commits April 9, 2026 02:35
The source-inspection heuristics in `_can_set_attn_implementation` and
`_can_set_experts_implementation` already contained a brief disclaimer
("ugly check based on opening the file"), but did not document the
specific failure modes. Expands both docstrings to cover:

- False positives when the searched strings appear in comments or
  string literals rather than actual control-flow code
- Compiled modules (.so/.pyd) — already handled by the __file__ guard
- Non-standard import environments (PyInstaller, zipimport, custom
  loaders) — also handled by the __file__ guard
- Interactive environments (Jupyter, REPL) — handled by __file__ guard

No behaviour change. Closes huggingface#45162.

Built by Rudrendu Paul, developed with Claude Code
@Rocketknight1
Copy link
Copy Markdown
Member

Hi, I'm sorry, we probably don't want this PR! The new docstring is very long, and most of these risks are obvious once you see that it's string-searching inside the file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document limitations of PreTrainedModel._can_set_* source inspection logic

2 participants