Skip to content
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

Crash: 'model' not found on <ClassDef.OptimizedModule > and then inf recursion if torch.compile is used #9365

Closed
olegsinavski opened this issue Jan 15, 2024 · 1 comment
Labels
Duplicate 🐫 Duplicate of an already existing issue

Comments

@olegsinavski
Copy link

olegsinavski commented Jan 15, 2024

Bug description

When parsing the following:

from typing import Any, Mapping, Optional, Tuple, Callable

import torch
import torch.optim
from pytorch_lightning import LightningModule

class TransformersModule(LightningModule):
    def __init__(
        self,
        model_factory: Callable[[], torch.nn.Module],
    ):
        super().__init__()
        self.model: Optional[torch.nn.Module] = None
        self.model_factory = model_factory

    def configure_model(self) -> None:
        if self.model is None:
            self.model = self.model_factory()
            if self.hparams.compile:
                self.model: torch.nn.Module = torch.compile(self.model)

    def forward(self, idx: torch.Tensor, labels: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
        assert self.model is not None
        output = self.model(input_ids=idx, labels=labels, return_dict=True)
        logits, loss = output.logits, output.loss
        return logits, loss

It works if I comment out torch.compile

Configuration

No response

Command used

pylint a.py

Pylint output

<details open>
    <summary>
        pylint crashed with a ``AstroidError`` and with the following stacktrace:
    </summary>


Traceback (most recent call last):
  File "/astroid/bases.py", line 245, in getattr
    values = self._proxied.instance_attr(name, context)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2353, in instance_attr
    raise AttributeInferenceError(target=self, attribute=name, context=context)
astroid.exceptions.AttributeInferenceError: 'model' not found on <ClassDef.OptimizedModule l.109 at 0x7f25ae20ac10>.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/astroid/bases.py", line 278, in igetattr
    get_attr = self.getattr(name, context, lookupclass=False)
  File "/astroid/bases.py", line 255, in getattr
    raise AttributeInferenceError(
astroid.exceptions.AttributeInferenceError: 'model' not found on <Instance of torch._dynamo.eval_frame.OptimizedModule at 0x139799781225952>.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2492, in igetattr
    attributes = self.getattr(name, context, class_context=class_context)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2425, in getattr
    raise AttributeInferenceError(target=self, attribute=name, context=context)
astroid.exceptions.AttributeInferenceError: 'model' not found on <ClassDef.OptimizedModule l.109 at 0x7f25ae20ac10>.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/astroid/decorators.py", line 90, in inner
    yield next(generator)
  File "/astroid/decorators.py", line 44, in wrapped
    if context.push(node):
RecursionError: maximum recursion depth exceeded

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/pylint/lint/pylinter.py", line 788, in _lint_file
    check_astroid_module(module)
  File "/pylint/lint/pylinter.py", line 1017, in check_astroid_module
    retval = self._check_astroid_module(
  File "/pylint/lint/pylinter.py", line 1069, in _check_astroid_module
    walker.walk(node)
  File "/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  [Previous line repeated 2 more times]
  File "/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/pylint/checkers/typecheck.py", line 1088, in visit_attribute
    inferred = list(node.expr.infer())
  File "/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/astroid/decorators.py", line 103, in inner
    yield from generator
  File "/astroid/decorators.py", line 49, in wrapped
    for res in _func(node, context, **kwargs):
  File "/astroid/bases.py", line 179, in _infer_stmts
    for inf in stmt.infer(context=context):
  File "/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/astroid/decorators.py", line 103, in inner
    yield from generator
  File "/astroid/decorators.py", line 49, in wrapped
    for res in _func(node, context, **kwargs):
  File "/astroid/bases.py", line 179, in _infer_stmts
    for inf in stmt.infer(context=context):
  File "/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/astroid/decorators.py", line 103, in inner
    yield from generator
  File "/astroid/decorators.py", line 49, in wrapped
    for res in _func(node, context, **kwargs):
  File "/astroid/nodes/node_classes.py", line 1765, in _infer
    yield from callee.infer_call_result(
  File "/astroid/bases.py", line 331, in infer_call_result
    for res in node.infer_call_result(caller, context):
  File "/astroid/bases.py", line 331, in infer_call_result
    for res in node.infer_call_result(caller, context):
  File "/astroid/bases.py", line 331, in infer_call_result
    for res in node.infer_call_result(caller, context):
  [Previous line repeated 7949 more times]
  File "/astroid/bases.py", line 323, in infer_call_result
    for res in self.igetattr(caller.func.attrname, context):
  File "/astroid/bases.py", line 290, in igetattr
    yield from self._wrap_attr(attrs, context)
  File "/astroid/bases.py", line 298, in _wrap_attr
    for attr in attrs:
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2539, in igetattr
    if not name.startswith("__") and self.has_dynamic_getattr(context):
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2562, in has_dynamic_getattr
    return _valid_getattr(self.getattr("__getattr__", context)[0])
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2415, in getattr
    values += self._metaclass_lookup_attribute(name, context)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2439, in _metaclass_lookup_attribute
    attrs.update(set(cls_attributes))
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2446, in _get_attribute_from_metaclass
    attrs = cls.getattr(name, context=context, class_context=True)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2415, in getattr
    values += self._metaclass_lookup_attribute(name, context)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2435, in _metaclass_lookup_attribute
    metaclass = self.metaclass(context=context)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2727, in metaclass
    return self._find_metaclass(context=context)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2707, in _find_metaclass
    klass = self.declared_metaclass(context=context)
  File "/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2679, in declared_metaclass
    for baseobj in base.infer(context=context):
  File "/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/astroid/decorators.py", line 99, in inner
    raise InferenceError(
  File "/astroid/exceptions.py", line 273, in __init__
    super().__init__(message, **kws)
RecursionError: maximum recursion depth exceeded while calling a Python object

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/pylint/lint/pylinter.py", line 752, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/pylint/lint/pylinter.py", line 790, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError




</details>

Expected behavior

Not crash

Pylint version

pylint 3.0.3
astroid 3.0.2.post1
Python 3.9.17 (main, Jul 26 2023, 13:24:07) 
[Clang 16.0.3 ]

OS / Environment

linux (Linux)

Additional dependencies

transformers==4.36.2
torch==2.1.2
@olegsinavski olegsinavski added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 15, 2024
@olegsinavski olegsinavski changed the title Crash: 'model' not found on <ClassDef.OptimizedModule > and then inf recursion Crash: 'model' not found on <ClassDef.OptimizedModule > and then inf recursion if torch.compile is used Jan 15, 2024
@jacobtylerwalls
Copy link
Member

Thanks for the report. Duplicate of #9139

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Feb 26, 2024
@jacobtylerwalls jacobtylerwalls added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants