Skip to content

[dynamo] Replace __str__ with __repr__ in some places #136316

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

Closed
wants to merge 4 commits into from

Conversation

rec
Copy link
Collaborator

@rec rec commented Sep 19, 2024

The problem

In a typical debugger, repr() is used to display variables and not str().

Several classes in Dynamo have a __str__() method that returns useful information and a __repr__() that does not. Having to call str(x) or [str(i) for i in x] in the debugger all the time is a chore.

str() should be "informal, nicely printable" and repr() should "attempt to return a string that would yield an object with the same value when passed to eval()".

The solution

In the Python object model, if there is no __str__ method, __repr__ is used instead (but not the other way around).

So renaming __str__ to __repr__ in a few cases where no __repr__ method exists now should not change observable behavior, and should make debugging easier.

The specific classes changed were all in torch._dynamo.variables:

  • builtin.BuiltinVariable
  • constant.ConstantVariable
  • constant.EnumVariable
  • functions.UserMethodVariable
  • lazy.LazyVariableTracker
  • lazy.LazySymNodeFormatString
  • misc.GetAttrVariable
  • misc.NullVariable
  • user_defined.UserDefinedObjectVariable

Stack from ghstack (oldest at bottom):

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Sep 19, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/136316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit c7182dd with merge base f568d48 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

rec added a commit that referenced this pull request Sep 19, 2024
ghstack-source-id: fd625ed
Pull Request resolved: #136316
@rec rec added the topic: not user facing topic category label Sep 19, 2024
@rec
Copy link
Collaborator Author

rec commented Sep 23, 2024

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Successfully rebased gh/rec/62/orig onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/136316)

pytorchmergebot pushed a commit that referenced this pull request Sep 23, 2024
ghstack-source-id: ac34079
Pull Request resolved: #136316
Copy link
Collaborator

@amjames amjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for better debug info, can you show a before/after example for context?

@rec
Copy link
Collaborator Author

rec commented Sep 30, 2024

@amjames: I decided to simply set breakpoints at all the constructors of classes changed and then ran every test in dynamo/test_misc.py until I got some good examples:

Before After
BuiltinVariable() BuiltinVariable(getattr)
ConstantVariable() ConstantVariable(str: 'nn')
UserMethodVariable() UserMethodVariable(<function Sequential.__init__ at 0x7f765c5188b0>, UnspecializedNNModuleVariable(Sequential))
GetAttrVariable() GetAttrVariable(SuperVariable(), __init__)
UnspecializedBuiltinNNModuleVariable(Linear) UnspecializedBuiltinNNModuleVariable(Linear)
LazyVariableTracker() LazyVariableTracker()
EnumVariable() EnumVariable(<enum 'MyEnum'>)

[ghstack-poisoned]
rec added a commit that referenced this pull request Oct 13, 2024
ghstack-source-id: 6499cd0
Pull Request resolved: #136316
@rec rec marked this pull request as ready for review October 15, 2024 13:44
@rec
Copy link
Collaborator Author

rec commented Oct 17, 2024

@amjames: ideas for reviewers?

The VariableTracker.build() PR has already been merged!

[ghstack-poisoned]
rec added a commit that referenced this pull request Oct 20, 2024
ghstack-source-id: 3dc1e2b
Pull Request resolved: #136316
@XuehaiPan XuehaiPan requested a review from jansel October 20, 2024 17:16
@rec
Copy link
Collaborator Author

rec commented Oct 20, 2024

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 20, 2024
@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: Approvers from one of the following sets are needed:

  • superuser (pytorch/metamates)
  • Core Reviewers (mruberry, lezcano, Skylion007, ngimel, peterbell10, ...)
  • Core Maintainers (soumith, gchanan, ezyang, dzhulgakov, malfet, ...)
Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@jansel
Copy link
Contributor

jansel commented Oct 21, 2024

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

facebook-github-bot pushed a commit to pytorch/benchmark that referenced this pull request Oct 22, 2024
Summary:
## The problem

In a typical debugger, `repr()` is used to display variables and not `str()`.

Several classes in Dynamo have a `__str__()` method that returns useful information and a  `__repr__()` that does not. Having to call `str(x)` or `[str(i) for i in x]` in the debugger all the time is a chore.

`str()` should be ["informal, nicely printable"](https://docs.python.org/3/library/stdtypes.html#str) and `repr()` should ["attempt to return a string that would yield an object with the same value when passed to eval()](https://docs.python.org/3/library/functions.html#repr)".

## The solution

In the Python object model, if there is no `__str__` method, `__repr__`  is used instead (but not the other way around).

So renaming `__str__` to `__repr__` in a few cases where no `__repr__` method exists now should not change observable behavior, and should make debugging easier.

The specific classes changed were all in `torch._dynamo.variables`:

* `builtin.BuiltinVariable`
* `constant.ConstantVariable`
* `constant.EnumVariable`
* `functions.UserMethodVariable`
* `lazy.LazyVariableTracker`
* `lazy.LazySymNodeFormatString`
* `misc.GetAttrVariable`
* `misc.NullVariable`
* `user_defined.UserDefinedObjectVariable`

X-link: pytorch/pytorch#136316
Approved by: https://github.com/XuehaiPan, https://github.com/jansel

Reviewed By: wdvr

Differential Revision: D64714511

fbshipit-source-id: 322f2f0110e5b45afe6a27c52a0bcc91d91d1d6a
@github-actions github-actions bot deleted the gh/rec/62/head branch November 21, 2024 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants