8000 [dynamo][user-defined] User class.__new__ instead of special casing by anijain2305 · Pull Request #146677 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[dynamo][user-defined] User class.__new__ instead of special casing #146677

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 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update on "[dynamo][user-defined] User class.__new__ instead of speci…
…al casing"

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

[ghstack-poisoned]
  • Loading branch information
anijain2305 committed Feb 9, 2025
commit 13162c37c88f2c0fe7ff60bf6f80b94319e3ff73
11 changes: 11 additions & 0 deletions torch/_dynamo/side_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,20 @@ def track_object_new(
return variable

def get_variable_cls(self, user_cls):
from torch.overrides import TorchFunctionMode

from .variables.ctx_manager import GenericContextWrappingVariable
from .variables.torch_function import TorchFunctionModeVariable

variable_cls: type[
variables.UserDefinedObjectVariable
] = variables.UserDefinedObjectVariable
if issubclass(
user_cls, TorchFunctionMode
) and TorchFunctionModeVariable.is_supported_torch_function_mode(user_cls):
variable_cls = TorchFunctionModeVariable
elif hasattr(user_cls, "__enter__") and hasattr(user_cls, "__exit__"):
variable_cls = GenericContextWrappingVariable
if issubclass(user_cls, torch.nn.Module):
variable_cls = variables.UnspecializedNNModuleVariable
elif issubclass(user_cls, (dict, collections.OrderedDict)):
Expand Down
22 changes: 4 additions & 18 deletions torch/_dynamo/variables/user_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,23 +480,10 @@ def call_function(
and self.source
and not is_forbidden_context_manager(self.value)
):
from torch.overrides import TorchFunctionMode

from .ctx_manager import GenericContextWrappingVariable
from .functions import (
BaseUserFunctionVariable,
FunctionDecoratedByContextlibContextManagerVariable,
)
from .torch_function import TorchFunctionModeVariable

if issubclass(
self.value, TorchFunctionMode
) and TorchFunctionModeVariable.is_supported_torch_function_mode(
self.value
):
var_cls = TorchFunctionModeVariable
else:
var_cls = GenericContextWrappingVariable

# graph break on any contextlib.* that it is not contextlib.contextmanager
# Some of the APIs below are not supported because they rely on features
Expand Down Expand Up @@ -533,11 +520,10 @@ def call_function(
)
] + args[1:]

options = {}
options["base_cls_vt"] = variables.BuiltinVariable(object)
options["init_args"] = []
cm_obj = tx.output.side_effects.track_object_new(
self.source, self.value, var_cls, options
cm_obj = tx.output.side_effects.track_new_user_defined_object(
variables.BuiltinVariable(object),
self,
args,
)
cm_obj.call_method(tx, "__init__", args, kwargs)
return cm_obj
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0