-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[fx] When generating names, avoid shadowing builtins #43653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
When nodes are created without an explicit name, a name is generated for it based on the target. In these cases, we need to avoid shadowing builtin names. Otherwise, code like: ``` a.foo.bar ``` results in pretty-printed code like: ``` getattr = a.foo getattr_1 = getattr.bar ``` While this is technically allowed in Python, it's probably a bad idea, and more importantly is not supported by TorchScript (where `getattr` is hardcoded). This PR changes the name generation logic to avoid shadowing all builtins and langauge keywords. We already do this for PyTorch built-ins, so just extend that logic. So now the generated code will look like: ``` getattr_1 = a.foo getattr_2 = getattr_1.bar ``` Fixes #43522 [ghstack-poisoned]
kwargs = {} if kwargs is None else kwargs | ||
self._mark_uses(args) | ||
self._mark_uses(kwargs) | ||
n = Node(self, name if name is not None else self._name(target or op), op, target, args, kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just a small cleanup; target
is never None so we don't need this or
-statement.
When nodes are created without an explicit name, a name is generated for it based on the target. In these cases, we need to avoid shadowing builtin names. Otherwise, code like: ``` a.foo.bar ``` results in pretty-printed code like: ``` getattr = a.foo getattr_1 = getattr.bar ``` While this is technically allowed in Python, it's probably a bad idea, and more importantly is not supported by TorchScript (where `getattr` is hardcoded). This PR changes the name generation logic to avoid shadowing all builtins and langauge keywords. We already do this for PyTorch built-ins, so just extend that logic. So now the generated code will look like: ``` getattr_1 = a.foo getattr_2 = getattr_1.bar ``` Fixes #43522 [ghstack-poisoned]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When nodes are created without an explicit name, a name is generated for it based on the target. In these cases, we need to avoid shadowing builtin names. Otherwise, code like: ``` a.foo.bar ``` results in pretty-printed code like: ``` getattr = a.foo getattr_1 = getattr.bar ``` While this is technically allowed in Python, it's probably a bad idea, and more importantly is not supported by TorchScript (where `getattr` is hardcoded). This PR changes the name generation logic to avoid shadowing all builtins and langauge keywords. We already do this for PyTorch built-ins, so just extend that logic. So now the generated code will look like: ``` getattr_1 = a.foo getattr_2 = getattr_1.bar ``` Fixes #43522 [ghstack-poisoned]
When nodes are created without an explicit name, a name is generated for it based on the target. In these cases, we need to avoid shadowing builtin names. Otherwise, code like: ``` a.foo.bar ``` results in pretty-printed code like: ``` getattr = a.foo getattr_1 = getattr.bar ``` While this is technically allowed in Python, it's probably a bad idea, and more importantly is not supported by TorchScript (where `getattr` is hardcoded). This PR changes the name generation logic to avoid shadowing all builtins and langauge keywords. We already do this for PyTorch built-ins, so just extend that logic. So now the generated code will look like: ``` getattr_1 = a.foo getattr_2 = getattr_1.bar ``` Fixes #43522 ghstack-source-id: 3ed5bc4 Pull Request resolved: #43653
💊 CI failures summary and remediationsAs of commit 6c24783 (more details on the Dr. CI page):
🚧 1 fixed upstream failure:These were probably caused by upstream breakages that were already fixed.
Please rebase on the
|
Stack from ghstack:
When nodes are created without an explicit name, a name is generated for
it based on the target. In these cases, we need to avoid shadowing
builtin names. Otherwise, code like:
results in pretty-printed code like:
While this is technically allowed in Python, it's probably a bad idea,
and more importantly is not supported by TorchScript (where
getattr
ishardcoded).
This PR changes the name generation logic to avoid shadowing all
builtins and langauge keywords. We already do this for PyTorch
built-ins, so just extend that logic. So now the generated code will
look like:
Fixes #43522
Differential Revision: D23357420