Inconsistencies in ONNX exporting of operation `torch.full()`
Labels: module: onnx, low priority, triaged, OSS contribution wanted

### 🐛 Describe the bug

Hello all! At our team we have been testing the ONNX exporting functionality, and we found a slight inconsistency with respect to tensor datatypes and using tensors with different datatypes through `torch.full()`.

Our main aim is to be able to use a list of tensors that we know are one-dimensional as an input to the shape of `torch.full()`. However, these tensors are `int32`, which seems not to be supported by the export (see below). We've tried some things, and we're struggling to understand the differences between three ways of treating the issue. At each alternative you'll see the exported graph, obtained with `verbose=True` when calling `torch.onnx.export`:

When converting from `int32` to `int64` through `tensor_object.long()` (final graph [here](https://gist.github.com/Icemole/3617ba90038a4e8af6efe116d06b4651)), we don't get any warning. However, when we try NOT to convert (final graph [here](https://gist.github.com/Icemole/3eedac24a108f63cb441defb7897d039)), we get the following warning (which is practically an error for us since we want the model to run inference):

```python
UserWarning: The exported ONNX model failed ONNX shape inference.
The model will not be executable by the ONNX Runtime.If this is unintended
and you believe there is a bug,please report an issue at
https://github.com/pytorch/pytorch/issues.Error reported by strict ONNX shape inference:
[ShapeInferenceError] (op_type:ConstantOfShape, node name: /ConstantOfShape):
input typestr: T1, has unsupported type: tensor(int32)
(Triggered internally at ../torch/csrc/jit/serialization/export.cpp:1407.)
  _C._check_onnx_proto(proto)
```

When I try to run the model in inference mode, the model doesn't work (as expected), and fails with the following error:

```python
Traceback (most recent call last):
  File "/home/nbeneitez/Documentos/work/repos/returnn_pytorch/testonnxinference.py", line 9, in <module>
    session = ort.InferenceSession(onnx_model)
  File "/home/nbeneitez/.venv/returnn_pytorch/lib/python3.10/site-packages/onnxruntime
/capi/onnxruntime_inference_collection.py", line 383, in __init__
    self._create_inference_session(providers, provider_options, disabled_optimizers)
  File "/home/nbeneitez/.venv/returnn_pytorch/lib/python3.10/site-packages/onnxruntime
/capi/onnxruntime_inference_collection.py", line 424, in _create_inference_session
    sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : 
Load model from my_filename.onnx failed:This is an invalid model.
Type Error: Type 'tensor(int32)' of input parameter (/Concat_output_0) of operator
(ConstantOfShape) in node (/ConstantOfShape) is invalid.
```

A minimal example is shown below ([gist](https://gist.github.com/Icemole/55c68dc814cf20b639e2592eda8cb14e)). Note that for the code to run without any warning, at the beginning of `forward()` you should add `d1 = d1.long()`:

```python
import torch


class DummyModel(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, d1):
        # To run without warnings, add: d1 = d1.long()
        return torch.full([d1], 1)


dummy_model = DummyModel()
d1 = torch.tensor(2, dtype=torch.int32)
torch.onnx.export(
    dummy_model,
    (d1,),
    f="my_filename.onnx",
    verbose=True,
    input_names=["d1"],
    output_names=["casted_data"],
)
```

A minimal example for inference ([gist](https://gist.github.com/Icemole/ee48f313587c568c7ae460cee48008d8)):

```python
import onnxruntime as ort
import torch
import sys


onnx_model = sys.argv[1]
dummy_data = torch.randint(low=1, high=10, size=[], dtype=torch.int32)

session = ort.InferenceSession(onnx_model)
outputs_onnx = torch.FloatTensor(session.run(None, {"data": dummy_data.numpy()})[0])
```

My main questions are:

- In the latter warning, there's a mention to an `onnx::ConstantOfShape` operator. This operator does establish in the [corresponding ONNX docs](https://onnx.ai/onnx/operators/onnx__ConstantOfShape.html#type-constraints) that its shape must be given as an `int64`. Why isn't this operator logged in the final graph? **Edit**: I see now that the `ConstantOfShape` operator refers to the last operation, that stores the result in `%casted_data`. Still, the code seems to be working with PyTorch, but is failing with ONNX.
- Is this behavior of not being able to use `torch.full()` with the shape being `int32` values expected? If so, what's the best way of handling this? With the reported `tensor.long()` approach?

Thank you in advance!

### Versions

```
Collecting environment information...
PyTorch version: 2.0.1+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 23.04 (x86_64)
GCC version: (Ubuntu 12.2.0-17ubuntu1) 12.2.0
Clang version: Could not collect
CMake version: version 3.26.4
Libc version: glibc-2.37

Python version: 3.10.12 (main, Jun 16 2023, 17:09:59) [GCC 12.2.0] (64-bit runtime)
Python platform: Linux-6.2.0-23-generic-x86_64-with-glibc2.37
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:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   39 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          16
On-line CPU(s) list:             0-15
Vendor ID:                       GenuineIntel
Model name:                      13th Gen Intel(R) Core(TM) i5-1340P
CPU family:                      6
Model:                           186
Thread(s) per core:              2
Core(s) per socket:              12
Socket(s):                       1
Stepping:                        2
CPU(s) scaling MHz:              46%
CPU max MHz:                     4600,0000
CPU min MHz:                     400,0000
BogoMIPS:                        4377,60
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       448 KiB (12 instances)
L1i cache:                       640 KiB (12 instances)
L2 cache:                        9 MiB (6 instances)
L3 cache:                        12 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-15
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.3
[pip3] torch==2.0.1
[pip3] torchdata==0.6.1
[pip3] triton==2.0.0
[conda] Could not collect
```

Note: it is important that the version is 2.0.1! As far as I remember, running the code in version 1.13.1 doesn't yield the `UserWarning` explicitly, although the model still cannot be used for inference.