Repository: pytorch
Issue: torch._dynamo.exc.Unsupported: Tensor.backward with aten_graph=True
Labels: triaged,oncall: pt2,oncall: export

### 🐛 Describe the bug

When trying to export the ATen graph of any model containing a `backward()` call using dynamo I'm hitting an "Unsupported" exception. However, exporting the graph of a model without the backward call works completely fine:
```
graph():
    %arg0 : [#users=0] = placeholder[target=arg0]
    %arg1 : [#users=1] = placeholder[target=arg1]
    %arg2 : [#users=1] = placeholder[target=arg2]
    %view_default : [#users=1] = call_function[target=torch.ops.aten.view.default](args = (%arg1, [16, 784]), kwargs = {})
    %_param_constant0 : [#users=1] = get_attr[target=_param_constant0]
    %t_default : [#users=1] = call_function[target=torch.ops.aten.t.default](args = (%_param_constant0,), kwargs = {})
    %_param_constant1 : [#users=1] = get_attr[target=_param_constant1]
    %addmm_default : [#users=1] = call_function[target=torch.ops.aten.addmm.default](args = (%_param_constant1, %view_default, %t_default), kwargs = {})
    %relu_default : [#users=2] = call_function[target=torch.ops.aten.relu.default](args = (%addmm_default,), kwargs = {})
    %detach_default : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%relu_default,), kwargs = {})
    %_log_softmax_default : [#users=2] = call_function[target=torch.ops.aten._log_softmax.default](args = (%relu_default, 1, False), kwargs = {})
    %detach_default_1 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%_log_softmax_default,), kwargs = {})
    %nll_loss_forward_default : [#users=2] = call_function[target=torch.ops.aten.nll_loss_forward.default](args = (%_log_softmax_default, %arg2, None, 1, -100), kwargs = {})
    %getitem : [#users=1] = call_function[target=operator.getitem](args = (%nll_loss_forward_default, 0), kwargs = {})
    %getitem_1 : [#users=0] = call_function[target=operator.getitem](args = (%nll_loss_forward_default, 1), kwargs = {})
    return [getitem]
```

Since currently there [seems to be no issue tracking ATen export functionality](https://discuss.pytorch.org/t/torch-dynamo-exc-unsupported-tensor-backward/169246/4?u=gengrill), I'm creating it here.

### Error logs

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 601, in export
    result_traced = opt_f(*args, **kwargs)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 209, in _fn
    return fn(*args, **kwargs)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 337, in catch_errors
    return callback(frame, cache_size, hooks)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 104, in _fn
    return fn(*args, **kwargs)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 262, in _convert_frame_assert
    return _compile(
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 163, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 324, in _compile
    out_code = transform_code_object(code, transform)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/bytecode_transformation.py", line 445, in transform_code_object
    transformations(instructions, code_options)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 311, in transform
    tracer.run()
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 1726, in run
    super().run()
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 576, in run
    and self.step()
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 540, in step
    getattr(self, inst.opname)(inst)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 342, in wrapper
    return inner_fn(self, inst)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 965, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 474, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/variables/misc.py", line 744, in call_function
    return self.obj.call_method(tx, self.name, args, kwargs).add_options(self)
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/variables/tensor.py", line 341, in call_method
    unimplemented(f"Tensor.{name}")
  File "/home/user/projects/pytorch-2.0/lib/python3.10/site-packages/torch/_dynamo/exc.py", line 71, in unimplemented
    raise Unsupported(msg)
torch._dynamo.exc.Unsupported: Tensor.backward

### Minified repro

```
import torch
import torch._dynamo as dynamo
from torch import nn

class Simple(nn.Module):
    def __init__(self, H=28, W=28, C=10):
        super(Simple, self).__init__()
        self.linear = nn.Linear(H*W, C)
    def forward(self, x):
        x = torch.flatten(x, start_dim=1)
        x = self.linear(x)
        return nn.functional.relu(x)

def generate_data(b):
    return (torch.randn(b,28,28).to(torch.float32), torch.randint(10, (b,)))

def no_train(model, data):
    pred = model(data[0])
    loss = nn.CrossEntropyLoss()(pred, data[1])
    return loss

def train(model, data):
    pred = model(data[0])
    loss = nn.CrossEntropyLoss()(pred, data[1])
    loss.backward()
    return loss

model = Simple()
model_exp_no_train = dynamo.export(no_train, model, generate_data(16), aten_graph=True)
print(model_exp_no_train[0].graph)

model_exp_train = dynamo.export(train, model, generate_data(16), aten_graph=True)
print(model_exp_train[0].graph)
```

### Versions

master

cc @ezyang @msaroufim @wconstab @bdhirsh @anijain2305 @zou3519