Repository: pytorch
Issue: [dynamo] Dynamo traces through __torch_dispatch__ on custom tensor subclasses
Labels: triaged,oncall: pt2,module: dynamo,module: pt2-dispatcher,dynamo-tensor-subclasses

Dynamo is tracing through `__torch_dispatch__` on tensor subclasses. This shouldn't be happening. Using `traceable_tensor_subclasses` seems to make the problem go away.

Repro:
```python
import torch
import torch._dynamo.config as config

class TensorDerived(torch.Tensor):
    @classmethod
    def __torch_dispatch__(cls, *args, **kwargs):
        result = super().__torch_dispatch__(*args, **kwargs)
        print("running torch dispatch!")
        return result

    @classmethod
    def __torch_function__(cls, *args, **kwargs):
        result = super().__torch_function__(*args, **kwargs)
        print("running torch function!")
        return result


x = TensorDerived([1, 2])
# config.traceable_tensor_subclasses.add(TensorDerived)

def f(x):
    return x + 1

opt_f = torch.compile(f, backend="eager")
opt_f(x)
```

Logs:
```
$ TORCH_LOGS="trace_source" python playground3.py 
V0606 13:44:47.612000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [0/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:21 in f (f)
V0606 13:44:47.612000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [0/0] [__trace_source]     def f(x):
V0606 13:44:47.612000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [0/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:22 in f (f)
V0606 13:44:47.612000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [0/0] [__trace_source]         return x + 1
V0606 13:44:47.619000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:11 in __torch_function__ (TensorDerived)
V0606 13:44:47.619000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]         @classmethod
V0606 13:44:47.619000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:13 in __torch_function__ (TensorDerived.__torch_function__)
V0606 13:44:47.619000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]             result = super().__torch_function__(*args, **kwargs)
V0606 13:44:47.621000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_tensor.py:1414 in __torch_function__ (Tensor) (inline depth: 1)
V0606 13:44:47.621000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]         @classmethod
V0606 13:44:47.645000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_tensor.py:1429 in __torch_function__ (Tensor.__torch_function__) (inline depth: 1)
V0606 13:44:47.645000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]             if kwargs is None:
V0606 13:44:47.645000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_tensor.py:1430 in __torch_function__ (Tensor.__torch_function__) (inline depth: 1)
V0606 13:44:47.645000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]                 kwargs = {}
V0606 13:44:47.645000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_tensor.py:1432 in __torch_function__ (Tensor.__torch_function__) (inline depth: 1)
V0606 13:44:47.645000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]             if not all(issubclass(cls, t) for t in types):
V0606 13:44:47.646000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_tensor.py:1432 in <genexpr> (Tensor.__torch_function__) (inline depth: 2)
V0606 13:44:47.646000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]             if not all(issubclass(cls, t) for t in types):
V0606 13:44:47.647000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_dynamo/polyfill.py:12 in all (all) (inline depth: 2)
V0606 13:44:47.647000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]     def all(iterator):
V0606 13:44:47.648000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_dynamo/polyfill.py:13 in all (all) (inline depth: 2)
V0606 13:44:47.648000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]         for elem in iterator:
V0606 13:44:47.648000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_dynamo/polyfill.py:14 in all (all) (inline depth: 2)
V0606 13:44:47.648000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]             if not elem:
V0606 13:44:47.648000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_dynamo/polyfill.py:13 in all (all) (inline depth: 2)
V0606 13:44:47.648000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]         for elem in iterator:
V0606 13:44:47.649000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_dynamo/polyfill.py:16 in all (all) (inline depth: 2)
V0606 13:44:47.649000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]         return True
V0606 13:44:47.649000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_tensor.py:1435 in __torch_function__ (Tensor.__torch_function__) (inline depth: 1)
V0606 13:44:47.649000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]             with _C.DisableTorchFunctionSubclass():
V0606 13:44:47.649000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/torch/_tensor.py:1436 in __torch_function__ (Tensor.__torch_function__) (inline depth: 1)
V0606 13:44:47.649000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0] [__trace_source]                 ret = func(*args, **kwargs)
V0606 13:44:47.651000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:11 in __torch_function__ (TensorDerived)
V0606 13:44:47.651000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0_1] [__trace_source]         @classmethod
V0606 13:44:47.651000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:13 in __torch_function__ (TensorDerived.__torch_function__)
V0606 13:44:47.651000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [1/0_1] [__trace_source]             result = super().__torch_function__(*args, **kwargs)
V0606 13:44:47.659000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:5 in __torch_dispatch__ (TensorDerived)
V0606 13:44:47.659000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0] [__trace_source]         @classmethod
V0606 13:44:47.659000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:7 in __torch_dispatch__ (TensorDerived.__torch_dispatch__)
V0606 13:44:47.659000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0] [__trace_source]             result = super().__torch_dispatch__(*args, **kwargs)
V0606 13:44:47.660000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:5 in __torch_dispatch__ (TensorDerived)
V0606 13:44:47.660000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0_1] [__trace_source]         @classmethod
V0606 13:44:47.660000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:7 in __torch_dispatch__ (TensorDerived.__torch_dispatch__)
V0606 13:44:47.660000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [2/0_1] [__trace_source]             result = super().__torch_dispatch__(*args, **kwargs)
V0606 13:44:47.667000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:7 in torch_dynamo_resume_in___torch_dispatch___at_7 (TensorDerived.__torch_dispatch__)
V0606 13:44:47.667000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0] [__trace_source]             result = super().__torch_dispatch__(*args, **kwargs)
V0606 13:44:47.668000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:8 in torch_dynamo_resume_in___torch_dispatch___at_7 (TensorDerived.__torch_dispatch__)
V0606 13:44:47.668000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0] [__trace_source]             print("running torch dispatch!")
V0606 13:44:47.669000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:7 in torch_dynamo_resume_in___torch_dispatch___at_7 (TensorDerived.__torch_dispatch__)
V0606 13:44:47.669000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0_1] [__trace_source]             result = super().__torch_dispatch__(*args, **kwargs)
V0606 13:44:47.670000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:8 in torch_dynamo_resume_in___torch_dispatch___at_7 (TensorDerived.__torch_dispatch__)
V0606 13:44:47.670000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [3/0_1] [__trace_source]             print("running torch dispatch!")
running torch dispatch!
V0606 13:44:47.677000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [4/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:8 in torch_dynamo_resume_in___torch_dispatch___at_8 (TensorDerived.__torch_dispatch__)
V0606 13:44:47.677000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [4/0] [__trace_source]             print("running torch dispatch!")
V0606 13:44:47.677000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [4/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:9 in torch_dynamo_resume_in___torch_dispatch___at_8 (TensorDerived.__torch_dispatch__)
V0606 13:44:47.677000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [4/0] [__trace_source]             return result
V0606 13:44:47.679000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:13 in torch_dynamo_resume_in___torch_function___at_13 (TensorDerived.__torch_function__)
V0606 13:44:47.679000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0] [__trace_source]             result = super().__torch_function__(*args, **kwargs)
V0606 13:44:47.679000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:14 in torch_dynamo_resume_in___torch_function___at_13 (TensorDerived.__torch_function__)
V0606 13:44:47.679000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0] [__trace_source]             print("running torch function!")
V0606 13:44:47.681000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:13 in torch_dynamo_resume_in___torch_function___at_13 (TensorDerived.__torch_function__)
V0606 13:44:47.681000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0_1] [__trace_source]             result = super().__torch_function__(*args, **kwargs)
V0606 13:44:47.681000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0_1] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:14 in torch_dynamo_resume_in___torch_function___at_13 (TensorDerived.__torch_function__)
V0606 13:44:47.681000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [5/0_1] [__trace_source]             print("running torch function!")
running torch function!
V0606 13:44:47.686000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [6/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:14 in torch_dynamo_resume_in___torch_function___at_14 (TensorDerived.__torch_function__)
V0606 13:44:47.686000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [6/0] [__trace_source]             print("running torch function!")
V0606 13:44:47.687000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [6/0] [__trace_source] TRACE starts_line /data/users/williamwen/pytorch/playground3.py:15 in torch_dynamo_resume_in___torch_function___at_14 (TensorDerived.__torch_function__)
V0606 13:44:47.687000 140181266732864 torch/_dynamo/symbolic_convert.py:774] [6/0] [__trace_source]             return result
```

cc @ezyang @msaroufim @bdhirsh @anijain2305 @chauhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @zou3519 @mlazos @qihqi