Avoid reading self source code via file open()#46152
Conversation
zucchini-nlp
left a comment
There was a problem hiding this comment.
Hmm, tbh the "open file to infer if impl can be set dynamically" is a dirty hack made in the past. I wonder if we can instead rely on self._supports_attention_backend to check if dynamic impl are supported, since checking the file content is not bullet proof anyway cc @Cyrilvallez
|
So in the end, we are still kind of forced to check this imo. But maybe I'm overlooking something |
0729274 to
8ac17c2
Compare
|
Thanks @zucchini-nlp @vasqu. I updated I checked the behavior of the new class-based checks against the old source-reading approach for all 2150
For those 12 mismatches, the new approach yielding LMK if this is okay, or if a different approach is preferred! |
|
Hey @rasmi. No matter what we decide here, we really need to break this PR in two different PRs, one for |
| return any( | ||
| "ALL_ATTENTION_FUNCTIONS" in getattr(getattr(attn_cls, "forward", None), "__globals__", {}) | ||
| for attn_cls in attention_classes | ||
| ) |
There was a problem hiding this comment.
Would be much better/easier with inspect.getsource or something
There was a problem hiding this comment.
@Cyrilvallez -- split this out into #46207 as requested, but we can keep the discussion here if easier. WDYT about the false positives in the source-reading approach vs. class inspection? Any preference in one approach over the other?
8ac17c2 to
485c71d
Compare
|
@Cyrilvallez -- moved the |
|
(@Cyrilvallez I requested a review here instead of #46207 by accident but it would be appreciated here too!) |
What does this PR do?
Avoid
open()-reading transformers' own.pysource increate_import_structure_from_path(alongside #46207). This is helpful for hermetic Python builds (e.g. with Bazel) and other non-standard environments (as opposed toopen()which can vary based on the precise environment and filesystem).Changes
create_import_structure_from_pathinto a path-based wrapper plus_create_import_structure_from_traversable, which walks viaTraversable.iterdir()/read_text()._resolve_traversableobtains the Traversable once from the already-imported roottransformerspackage and recurses through it (avoidingfiles(subpackage), which would force-import the subpackage and defeat the_LazyModulebootstrap). Paths outside the transformers package (e.g. test fixtures) fall back topathlib.Path, which exposes the same interface. (I left the API surface as-is so as to not have to refactor callers to here from__init__.py-- but can clean up the method and update callers if preferred).Code Agent Policy
The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.
PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.
This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read
CONTRIBUTING.md.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@zucchini-nlp @Rocketknight1