Repository: pytorch
Issue: Pytorch member variable not working after converting to onnx format
Labels: module: onnx,triaged

### 🐛 Describe the bug
Hi team, 
we're now investigating the export to onnx feature and we found that some update logic in the original pytorch model is not working in the converted onnx model. The pytorch result kept updating as expected but the onnx result stays the same.
```
# onnx (stays the same)
[array([[ 0.09353793, -0.06549314, -0.17803375,  0.07057121, -0.07197426,
        -0.00245702,  0.09384082, -0.07102646,  0.00091066, -0.012063  ]],
      dtype=float32)]
[array([[ 0.09353793, -0.06549314, -0.17803375,  0.07057121, -0.07197426,
        -0.00245702,  0.09384082, -0.07102646,  0.00091066, -0.012063  ]],
      dtype=float32)]
[array([[ 0.09353793, -0.06549314, -0.17803375,  0.07057121, -0.07197426,
        -0.00245702,  0.09384082, -0.07102646,  0.00091066, -0.012063  ]],
      dtype=float32)]

# pytorch result (keep updating)
tensor([[ 0.1028, -0.0641, -0.1713,  0.0673, -0.0882, -0.0108,  0.1027, -0.0583,
          0.0012, -0.0174]], grad_fn=<DifferentiableGraphBackward>)
tensor([[ 0.0977, -0.0628, -0.1801,  0.0675, -0.0858, -0.0092,  0.1020, -0.0584,
          0.0034, -0.0185]], grad_fn=<DifferentiableGraphBackward>)
tensor([[ 0.0987, -0.0620, -0.1770,  0.0681, -0.0860, -0.0084,  0.1019, -0.0604,
          0.0033, -0.0192]], grad_fn=<DifferentiableGraphBackward>)
```

How to deal with such scenario that we need to update some member variable like self.last_hidden ?

### Reproduce code
```
import torch.nn as nn
import torch

class RNN(nn.Module):
    # you can also accept arguments in your model constructor
    def __init__(self, data_size, hidden_size, output_size):
        super(RNN, self).__init__()
        self.last_hidden = torch.zeros(1, hidden_size)

        input_size = data_size + hidden_size

        self.i2h = nn.Linear(input_size, hidden_size)
        self.h2o = nn.Linear(hidden_size, output_size)

    def forward(self, data):
        input = torch.cat((data, self.last_hidden), 1)
        hidden = self.i2h(input)
        output = self.h2o(hidden)

        self.last_hidden = hidden
        return output


data_size = 50
hidden_size = 20
output_size = 10
rnn_model = RNN(data_size, hidden_size, output_size)
rnn_model.eval()

data = torch.zeros(1, data_size)
last_hidden = torch.zeros(1, hidden_size)

torch.jit.save(torch.jit.script(rnn_model), 'rnn_model.pt')

pytorch_rnn_model = torch.load('rnn_model.pt')

torch.onnx.export(
    pytorch_rnn_model,
    data,
    'rnn_model.onnx',
    opset_version=15,
    input_names=('input',),
    output_names=('output',),
    dynamic_axes={
        'input': {0: 'batch', 1: 'sequence'},
        'output': {0: 'batch', 1: 'sequence'},
    },
    training=torch.onnx.TrainingMode.EVAL,
    do_constant_folding=False,
    verbose=True,
    keep_initializers_as_inputs=True,
)

import onnxruntime

rnn_onnx_model = onnxruntime.InferenceSession('rnn_model.onnx', providers=['CPUExecutionProvider'])
input_name = rnn_onnx_model.get_inputs()[0].name
output_name = rnn_onnx_model.get_outputs()[0].name

print(rnn_onnx_model.run([output_name], {input_name: data.numpy()}))
print(rnn_onnx_model.run([output_name], {input_name: data.numpy()}))
print(rnn_onnx_model.run([output_name], {input_name: data.numpy()}))

print(pytorch_rnn_model.forward(data))
print(pytorch_rnn_model.forward(data))
print(pytorch_rnn_model.forward(data))
```

### Versions
```
Collecting environment information...
PyTorch version: 2.0.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.1 (x86_64)
GCC version: Could not collect
Clang version: 14.0.0 (clang-1400.0.29.202)
CMake version: Could not collect
Libc version: N/A

Python version: 3.8.16 (default, Mar  1 2023, 21:19:10)  [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M1 Max

Versions of relevant libraries:
[pip3] numpy==1.23.5
[pip3] torch==2.0.0
[pip3] torchvision==0.15.1
[conda] blas                      1.0                         mkl    https://repo.anaconda.com/pkgs/main
[conda] mkl                       2021.4.0           hecd8cb5_637    https://repo.anaconda.com/pkgs/main
[conda] mkl-service               2.4.0            py38h9ed2024_0    https://repo.anaconda.com/pkgs/main
[conda] mkl_fft                   1.3.1            py38h4ab4a9b_0    https://repo.anaconda.com/pkgs/main
[conda] mkl_random                1.2.2            py38hb2f4e1b_0    https://repo.anaconda.com/pkgs/main
[conda] numpy                     1.23.5           py38he696674_0    https://repo.anaconda.com/pkgs/main
[conda] numpy-base                1.23.5           py38h9cd3388_0    https://repo.anaconda.com/pkgs/main
[conda] torch                     2.0.0                    pypi_0    pypi
[conda] torchvision               0.15.1                   pypi_0    pypi
```