Repository: pytorch
Issue: [dynamo] Crash when unspecialized nn.Module with custom getattr throws exception 
Labels: triaged,oncall: pt2,module: dynamo

There is a dynamo error that caused https://github.com/pytorch/pytorch/pull/128620 to be reverted.

I have a minified repro here:
```python
import torch

class Mod(torch.nn.Module):
    def __getattr__(self, name):
        try:
            return super().__getattr__(name)  # defer to nn.Module's logic
        except AttributeError:
            return getattr(self.mod, name)

    def __init__(self, mod):
        super().__init__()
        self.mod = mod

    def forward(self, *args):
        return self.mod(*args)

mod = torch.nn.Linear(3, 3)

def fn(mod, x):
    mod = Mod(mod)
    if hasattr(mod, "asdf"):
        return x + 1
    return x + 2

torch.compile(fn, backend="eager")(mod, torch.randn(3, 3))
```

which gives error
```
Traceback (most recent call last):
  File "/data/users/williamwen/pytorch/playground5.py", line 25, in <module>
    torch.compile(fn, backend="eager")(mod, torch.randn(3, 3))
  File "/data/users/williamwen/pytorch/torch/_dynamo/eval_frame.py", line 434, in _fn
    return fn(*args, **kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 1121, in __call__
    return self._torchdynamo_orig_callable(
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 948, in __call__
    result = self._inner_convert(
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 472, in __call__
    return _compile(
  File "/data/users/williamwen/pytorch/torch/_utils_internal.py", line 85, in wrapper_function
    return StrobelightCompileTimeProfiler.profile_compile_time(
  File "/data/users/williamwen/pytorch/torch/_strobelight/compile_time_profiler.py", line 129, in profile_compile_time
    return func(*args, **kwargs)
  File "/data/users/williamwen/py310-env/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 846, in _compile
    raise InternalTorchDynamoError(str(e)).with_traceback(
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 817, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "/data/users/williamwen/pytorch/torch/_dynamo/utils.py", line 233, in time_wrapper
    r = func(*args, **kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 636, in compile_inner
    out_code = transform_code_object(code, transform)
  File "/data/users/williamwen/pytorch/torch/_dynamo/bytecode_transformation.py", line 1270, in transform_code_object
    transformations(instructions, code_options)
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 178, in _fn
    return fn(*args, **kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/convert_frame.py", line 582, in transform
    tracer.run()
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 2476, in run
    super().run()
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 904, in run
    while self.step():
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 816, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 510, in wrapper
    return inner_fn(self, inst)
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 1480, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 754, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 964, in call_function
    return handler(tx, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 839, in builtin_dipatch
    rv = handler(tx, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 766, in call_self_handler
    result = self_handler(tx, *args, **kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 1472, in call_hasattr
    return obj.call_hasattr(tx, name)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/user_defined.py", line 1048, in call_hasattr
    ).call_function(tx, [variables.ConstantVariable.create(name)], {})
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/functions.py", line 353, in call_function
    return super().call_function(tx, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/functions.py", line 295, in call_function
    return super().call_function(tx, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/functions.py", line 95, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 760, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 2691, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 2807, in inline_call_
    tracer.run()
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 904, in run
    while self.step():
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 816, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 510, in wrapper
    return inner_fn(self, inst)
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 1480, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/data/users/williamwen/pytorch/torch/_dynamo/symbolic_convert.py", line 754, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 964, in call_function
    return handler(tx, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 848, in builtin_dipatch
    rv = fn(tx, args, kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 766, in call_self_handler
    result = self_handler(tx, *args, **kwargs)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/builtin.py", line 1595, in call_getattr
    return obj.var_getattr(tx, name)
  File "/data/users/williamwen/pytorch/torch/_dynamo/variables/nn_module.py", line 271, in var_getattr
    subobj = inspect.getattr_static(base, name)
  File "/data/users/williamwen/py310-env/lib/python3.10/inspect.py", line 1769, in getattr_static
    raise AttributeError(attr)
torch._dynamo.exc.InternalTorchDynamoError: asdf

from user code:
   File "/data/users/williamwen/pytorch/playground5.py", line 21, in fn
    if hasattr(mod, "asdf"):
  File "/data/users/williamwen/pytorch/playground5.py", line 8, in __getattr__
    return getattr(self.mod, name)
```

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