-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Dynamo] Fix - str
handler for UserDefinedObjectVariable
#130506
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
[Dynamo] Fix - str
handler for UserDefinedObjectVariable
#130506
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/130506
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 318af0f with merge base d039b14 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
@oulgen @anijain2305 second attempt on this PR (previous #130320), it does seem that the changes affect the tests for some setups, which I cannot replicate locally.
In other setups (e.g., screenshot passes for dynamo, 3, 3, linux.2xlarge) I was not able to find this specific test, not sure why. Due to the proposed changes, there be trouble retireving 'value' and/or '_size' attributes from ParameterList UserDefinedObjectVariable. For '_size' not entirely sure why it would have difficulty since the attribute is defined in the class init function. Current commit is hoping to get some additional logs to understand better. I'm trying to run some tests on the CI but cannot trigger the tests - is there a way I can achieve this? ![]() |
Hi @oulgen @anijain2305 - I think I need an approval to proceed with the CI tests? |
I clicked on the run tests button, each time you publish a new version, you need someone to run tests for you |
@oulgen finally found the error, from test/test_nn.py], what I understand a |
hi @oulgen are you able to help triggering the tests to confirm changes? 🙏 |
f3d15d2
to
c07d066
Compare
@oulgen In Test pass locally now: |
test/test_autograd.py
Outdated
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.
you shouldnt need to do this? @bdhirsh
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.
Thanks for the review. Indeed I do not fully understand the effects.
@bdhirsh This is what I was able to infer:
In test_saving_variable_to_disk defining tensors inside the saved_tensors_hooks context caused unintended side effects during assertions, specifically when calling _compare_regular_values_close. This was fixed by moving the tensor definitions (a and y) outside the saved_tensors_hooks context.
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.
Hi @bdhirsh would appreciate any guide/comment on this one!
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.
pulled your PR locally and this test fails for me with the change. taking a look
Is there more work needed on this or close to done @datagero? |
@ashwani-rathee waiting on some feedback on a |
torch/_dynamo/variables/builtin.py
Outdated
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.
Maybe check if the arg.value has an overridden __str__
method. If yes, then either graph break right now, or inline the __str__
function.
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.
So for the test/test_autograd.py
discussed above, the call to str below is causing the issue for me. @bdhirsh were you able to have a look?
def pack(x):
name = os.path.join(tmp_dir, str(uuid.uuid4()))
torch.save(x, name)
return name
type(obj).__str__
is overriden to <function UUID.__str__ at 0x1026a8040>
if on the above we avoid str() and call uuid.uuid4().hex
or uuid.uuid4().__str__()
, then the test pass for me. Otherwise (even if changing torch/_dynamo/variables/builtin.py
1025 to be in-line value=arg.value.__str__()
), it will throw assertion error:
AssertionError: Tensor-likes are not close!
Mismatched elements: 5 / 5 (100.0%)
Greatest absolute difference: 2.0 at index (0,) (up to 1e-05 allowed)
Greatest relative difference: inf at index (0,) (up to 1.3e-06 allowed)
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.
You can do something like
elif isinstance(arg, (variables.UserDefinedObjectVariable)) and type(arg.value).__str__ is object.__str__:
This will incrementally add support for user defined objects relying on the object str method. And for rest, it will graph break.
If you want to support custom __str__
method then you might need to do more work.
- Inline the
__str__
method. Search fortx.inline_user_function_return
to get an idea on how we inline. - You will have to account for
__repr__
functions when__str__
is absent. See the next comment.
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.
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.
Thanks @anijain2305 will have a look today, seems good pointers! The requirement of the ticket is to deal with custom __str__
methods so I'lll look at tx.inline_user_function_return
and your notes.
From the ticket:
import torch
class C:
x = 1
def __str__(self):
return "ok"
def foo(x):
a = C()
return x, str(a)
print(torch.compile(foo, fullgraph=True)(torch.ones(4)))
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.
Sorcery! That helped a lot
c07d066
to
ed976a8
Compare
For reviewer (@anijain2305 , @oulgen , @bdhirsh ):
|
ed976a8
to
e4273df
Compare
bee8e48
to
275f81c
Compare
torch/_dynamo/variables/builtin.py
Outdated
return | ||
|
||
# Inline the user function | ||
user_func_variable = variables.UserFunctionVariable(bound_method) |
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.
No need of this line. L1064 already does it.
try: | ||
# Only supports certain function types | ||
user_func_variable = variables.UserFunctionVariable(bound_method) | ||
except AssertionError as e: |
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.
I think we should do an if
condition instead of relying on Exception. We should probably just call VariableBuilder (cc @mlazos, I think we need to add a function in builder.py for identifying C functions and graph break on them). But I think this is not required in this PR.
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.
Thanks - I did try/except in case UserFunctionVariable gets expanded to other function types. As per your note, I did not adjusted this on last push.
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.
Thanks for the changes.
str
handler for UserDefinedObjectVariable
6ba58bb
to
5160181
Compare
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.
can you revert these changes?
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.
@oulgen done
5160181
to
318af0f
Compare
@pytorchbot merge |
Merge startedYour 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 |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 3, 3, macos-m1-stable) Details for Dev Infra teamRaised by workflow job |
@pytorchbot merge |
Merge startedYour 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 |
Fixes #130301
Adjusted the call_str method to handle str conversion for UserDefinedObjectVariable.
Attempt in a clean branch for unrelated test errors.
cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @voznesenskym @penguinwu @EikanWang @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames