8000 [Dynamo] Replace `unimplemented` with `unimplemented_v2` in `torch/_dynamo/variables/nn_module.py` by shink · Pull Request #151895 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[Dynamo] Replace unimplemented with unimplemented_v2 in torch/_dynamo/variables/nn_module.py #151895

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 8 commits into from
Closed
Changes from all commits
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
78 changes: 70 additions & 8 deletions torch/_dynamo/variables/nn_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from .. import graph_break_hints, trace_rules, variables
from ..exc import (
raise_observed_exception,
unimplemented,
unimplemented_v2,
UnspecializeRestartAnalysis,
Unsupported,
Expand Down Expand Up @@ -247,7 +246,17 @@ def has_key_in_generic_dict(self, tx: "InstructionTranslator", key):
base = tx.output.get_submodule(self.module_key)

if object_has_getattribute(base):
unimplemented("NNModuleVariable with custom __getattribute__")
unimplemented_v2(
gb_type="torch.nn.Module with a custom __getattribute__ defined",
context=f"has_key_in_generic_dict {self} {key}",
explanation="Dynamo does not support checking key existence "
"on `nn.Module` instances that have a custom "
"`__getattribute__` method defined.",
hints=[
"Avoid defining `__getattribute__` in your module.",
*graph_break_hints.SUPPORTABLE,
],
)

if tx.output.side_effects.has_pending_mutation_of_attr(self, key):
mutated_attr = tx.output.side_effects.load_attr(self, key, deleted_ok=True)
Expand All @@ -259,14 +268,40 @@ def has_key_in_generic_dict(self, tx: "InstructionTranslator", key):
def _custom_getattr_fallback(self, base, tx, name, obj_source):
"""Check for a __getattr__ and handle it specially if it is implemented"""
if object_has_getattribute(base):
unimplemented("torch.nn.Module with a custom __getattribute__ defined")
unimplemented_v2(
gb_type="torch.nn.Module with a custom __getattribute__ defined",
context=f"var_getattr {self} {name}",
explanation="Dynamo does not support checking key existence "
"on `nn.Module` instances that have a custom "
"`__getattribute__` method defined.",
hints=[
"Avoid defining `__getattribute__` in your module.",
*graph_break_hints.SUPPORTABLE,
],
)

getattr_fn = get_custom_getattr(base, ignore_nn_module_getattr=True)
if getattr_fn is None:
return None

if not isinstance(getattr_fn, types.FunctionType):
unimplemented("torch.nn.Module with a non-function custom __getattr__")
unimplemented_v2(
gb_type="torch.nn.Module with a non-function custom __getattr__",
context=f"var_getattr {self} {name}",
explanation=(
"Dynamo detected a nn.Module object with a custom "
"`__getattr__` method, but this method is not a standard "
"Python function (e.g., it might be implemented in C/C++). "
"Dynamo cannot currently trace into such non-standard "
"`__getattr__` methods."
),
hints=[
"Avoid using objects with non-standard __getattr__ methods "
"within the compiled region. If possible, implement "
"__getattr__ as a standard Python function.",
*graph_break_hints.SUPPORTABLE,
],
)

options = {"source": AttrSource(obj_source, "__getattr__")}
return variables.UserMethodVariable(getattr_fn, self, **options).call_function(
Expand All @@ -284,7 +319,14 @@ def var_getattr(self, tx: "InstructionTranslator", name):
all_class_attribute_names.update(x.__dict__.keys())

if not self.source:
unimplemented("GETATTR with no source")
unimplemented_v2(
gb_type="getattr with no source",
context=f"var_getattr {self} {name}",
explanation="Dynamo does not know how to access an attribute "
"on an `nn.Module` instance that lacks a source. This is "
"usually an internal error in Dynamo.",
hints=[*graph_break_hints.DYNAMO_BUG],
)

if name == "__dict__":
return variables.GetAttrVariable(self, name, source=source)
Expand Down Expand Up @@ -565,7 +607,13 @@ def assert_all_args_kwargs_const():
if not all(
x.is_python_constant() for x in itertools.chain(args, kwargs.values())
):
unimplemented(f"non-const NNModule method {name}")
unimplemented_v2(
gb_type="non-const argument in nn.Module method",
context=f"call_method: {self} {name} {args} {kwargs}",
explanation="Dynamo does not support calling "
f"method `{name}` of ``nn.Module`` {module} with non-constant arguments.",
hints=[],
)

def get_kwargs(*names):
assert_all_args_kwargs_const()
Expand Down Expand Up @@ -756,7 +804,13 @@ def gen_source(source, name):
elif args[0].is_python_constant():
key = args[0].as_python_constant()
else:
unimplemented(f"getitem on NNModuleVariable with key {args[0]}")
unimplemented_v2(
gb_type="Unsupported key type for nn.Module.__getitem__",
context=f"call_method: {self} {name} {args} {kwargs}",
explanation="Dynamo does not support getitem on "
"`nn.Module` with non-constant key.",
hints=[],
)

submod = module[key]
return tx.output.register_attr_or_module(
Expand Down Expand Up @@ -991,7 +1045,15 @@ def call_method(
hasattr(method, "__code__")
and id(method.__code__) in self._nn_module_method_ids()
):
unimplemented(f"UnspecializedNNModuleVariable missing {name}")
unimplemented_v2(
gb_type="UnspecializedNNModuleVariable missing method",
context=f"call_method: {self} {name} {args} {kwargs}",
explanation=f"Dynamo does not support tracing method {name} of nn.Module {self.value}",
hints=[
"Dynamo does not really define unspecialized nn.Module very well.",
*graph_break_hints.DIFFICULT,
],
)

# "_parameters" in self.value.__dict__ checks that module is initialized
if name == "__setattr__" and "_parameters" in self.value.__dict__:
Expand Down
Loading
0