Repository: pytorch
Issue: ONNX export: torch.onnx.errors.SymbolicValueError: Unsupported prim::Constant kind: 'ival'
Labels: module: onnx,triaged

### 🐛 Describe the bug

Hi! 
Trying to export a model of mine to ONNX with a sub-dependency ([torchsde](https://github.com/google-research/torchsde)) that makes use of [torch Generators](https://pytorch.org/docs/stable/generated/torch.Generator.html#torch.Generator). I can't upload the model code but here is an MRE that triggers the same error:

```py
import torch

def _randn(size: list[int], device: torch.device):
    generator = torch.Generator(device=device)
    return torch.randn(size, device=device, generator=generator)
	
class MyModule(torch.nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()

    def forward(self, x):
        val = _randn(x.shape, torch.device("cpu"))
        return val

m = MyModule()
torch.onnx.export(MyModule(), torch.randn(1, 2), 'test.onnx')
```

# Observed behaviour

Running this outputs:
```
$ python issue.py
Traceback (most recent call last):
  File "/home/myrepo/issue.py", line 16, in <module>
    torch.onnx.export(MyModule(), torch.randn(1, 2), 'test.onnx')
  File "/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py", line 551, in export
    _export(
  File "/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py", line 1648, in _export
    graph, params_dict, torch_out = _model_to_graph(
  File "/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py", line 1174, in _model_to_graph
    graph = _optimize_graph(
  File "/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py", line 714, in _optimize_graph
    graph = _C._jit_pass_onnx(graph, operator_export_type)
  File "/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py", line 1997, in _run_symbolic_function
    return symbolic_fn(graph_context, *inputs, **attrs)
  File "/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/symbolic_opset9.py", line 6952, in prim_constant
    raise errors.SymbolicValueError(
torch.onnx.errors.SymbolicValueError: Unsupported prim::Constant kind: 'ival'. Please send a bug report at https://github.com/pytorch/pytorch/issues.  [Caused by the value '26 defined in (%26 : Generator = prim::Constant[value=torch.Generator(device="cpu", seed=67280421310721)](), scope: __main__.MyModule:: # /home/myrepo/issue.py:5:0
)' (type 'Generator') in the TorchScript graph. The containing node has kind 'prim::Constant'.]
    (node defined in /home/myrepo/issue.py(5): _randn
/home/myrepo/issue.py(12): forward
/home/myrepo/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py(1543): _slow_forward
/home/myrepo/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py(1562): _call_impl
/home/myrepo/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py(1553): _wrapped_call_impl
/home/myrepo/.venv/lib/python3.10/site-packages/torch/jit/_trace.py(132): wrapper
/home/myrepo/.venv/lib/python3.10/site-packages/torch/jit/_trace.py(141): forward
/home/myrepo/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py(1562): _call_impl
/home/myrepo/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py(1553): _wrapped_call_impl
/home/myrepo/.venv/lib/python3.10/site-packages/torch/jit/_trace.py(1497): _get_trace_graph
/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py(950): _trace_and_get_graph_from_model
/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py(1046): _create_jit_graph
/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py(1170): _model_to_graph
/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py(1648): _export
/home/myrepo/.venv/lib/python3.10/site-packages/torch/onnx/utils.py(551): export
/home/myrepo/issue.py(16): <module>
)

    Inputs:
        Empty
    Outputs:
        #0: 26 defined in (%26 : Generator = prim::Constant[value=torch.Generator(device="cpu", seed=67280421310721)](), scope: __main__.MyModule:: # /home/myrepo/issue.py:5:0
    )  (type 'Generator')
```

And here I am 🦊  I would have assumed that `torch.randn` uses a Generator under the hood as well but not explicitly passing a Generator to `randn` allows the model to successfully be exported.

Any help with how to work around this issue is greatly appreciated! 

# Expected behaviour
Model successfully exported to disk as `test.onnx`.

### Versions

```
Collecting environment information...
PyTorch version: 2.4.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Debian GNU/Linux 11 (bullseye) (x86_64)
GCC version: (Debian 10.2.1-6) 10.2.1 20210110
Clang version: Could not collect
CMake version: version 3.18.4
Libc version: glibc-2.31

Python version: 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-5.10.0-32-cloud-amd64-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100-SXM4-40GB
Nvidia driver version: 550.90.07
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] alias-free-torch==0.0.6
[pip3] clip-anytorch==2.6.0
[pip3] dctorch==0.1.2
[pip3] ema-pytorch==0.2.3
[pip3] numpy==1.23.5
[pip3] onnx==1.16.2
[pip3] onnxscript==0.1.0.dev20240926
[pip3] pytorch-lightning==2.4.0
[pip3] torch==2.4.1
[pip3] torch-stoi==0.2.1
[pip3] torchaudio==2.4.1
[pip3] torchdiffeq==0.2.4
[pip3] torchlibrosa==0.1.0
[pip3] torchmetrics==1.4.1
[pip3] torchsde==0.2.6
[pip3] torchvision==0.19.1
[pip3] triton==3.0.0
[pip3] v-diffusion-pytorch==0.0.2
[pip3] vector-quantize-pytorch==1.17.3
[conda] numpy                     1.23.5                   pypi_0    pypi
```