Repository: pytorch
Issue: [ONNX] Intermediate values are encoded when exporting operators with custom namespace
Labels: module: onnx,triaged,onnx-triaged

### 🐛 Describe the bug

# Bug description
From torch 1.11, when exporting a module with custom operator, the intermediate values are encoded. Intermediate `value_info`s have `dim_param`s named by https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/jit/serialization/export.cpp#L563

Before torch 1.10, `value_info`s for intermediate values are not encoded into the output onnx.

# Example
```python
import torch
import torch.onnx.symbolic_helper
import torch.onnx.symbolic_registry

@torch.onnx.symbolic_helper.parse_args("v", "i", "v", "v")
def scatter_add(g, self, dim, index, src):
    return g.op("some_domain::FastScatterAdd", self, index, src, axis_i=dim)

torch.onnx.symbolic_registry.register_op("scatter_add", scatter_add, "", 9)

class Model(torch.nn.Module):
    def forward(self, a, b, c):
        return a.scatter_add(0, b, c) + 1

model = Model()
args = (torch.rand(3, 5), torch.tensor([[0, 1, 2, 0, 0], [0, 1, 2, 2, 2]]), torch.ones(2, 5))

torch.onnx.export(model, args, "model.onnx", opset_version=9)
```

by running the above code, `model.onnx` contains a `value_info` like
```
  value_info {
    name: "onnx::Add_3"
    type {
      tensor_type {
        elem_type: 1
        shape {
          dim {
            dim_param: "FastScatterAddonnx::Add_3_dim_0"
          }
          dim {
            dim_param: "FastScatterAddonnx::Add_3_dim_1"
          }
        }...
```
even if this value is not a graph output (for torch <= 1.10, the output onnx does not contain this value_info).

# Cause of this issue
In https://github.com/pytorch/pytorch/pull/63589, https://github.com/pytorch/pytorch/blob/bc2c6edaf163b1a1330e37a6e34caf8c553e4755/torch/csrc/jit/serialization/export.cpp#L1141 was implemented.

Since this function was originally designed for exporting modules with `export_modules_as_functions=True`, `EncodeIntermediateValueInfo` in `torch/csrc/jit/serialization/export.cpp`, which is called via `_export_onnx` defined in `torch/csrc/jit/python/python_ir.cpp`, should only be enabled when this kwarg is set to `True`.

### Versions

As described above, this bug happens since torch 1.11.0 (or more precisely, the commit https://github.com/pytorch/pytorch/pull/63589)
```
PyTorch version: 1.11.0+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 8.4.0-3ubuntu2) 8.4.0
Clang version: 10.0.0-4ubuntu1
CMake version: version 3.22.2
Libc version: glibc-2.31

Python version: 3.8.10 (default, Mar 15 2022, 12:22:08)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-100-generic-x86_64-with-glibc2.29
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2070 SUPER
Nvidia driver version: 470.103.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.3.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.3.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.3.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.3.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.3.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.3.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.3.2
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy==0.790
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.18.4
[pip3] pytorch-pfn-extras==0.5.7
[pip3] torch==1.11.0
[pip3] torchvision==0.12.0
[conda] Could not collect
```