Repository: pytorch
Issue: torch.export fails on a model with optional parameter
Labels: triaged,export-triage-review,oncall: export

### 🐛 Describe the bug

``` python
import torch
from typing import Optional
from torch import Tensor

class Model(torch.nn.Module):
    def forward(
        self,
        x,
        y: Optional[Tensor] = torch.ones(2, 3),
    ):
        if y is not None:
            return x + y
        return x
model = Model()
x = torch.randn(2, 3)
torch.export.export(model, (x,))
```

Running this results in 

    AssertionError: graph-captured input #1 (<class 'torch.Tensor'>) is not among original args (<class 'torch.Tensor'>)

My attempt to rewrite the `if` using `functorch.experimental.control_flow.cond` also failed with

    torch._dynamo.exc.UserError: speculate_subgraph: while introspecting cond, we were unable to trace function `<lambda>` into a single graph.
Probably because one input is needed in one case, and two in another.

### Versions

PyTorch version: 2.1.0+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Fedora release 36 (Thirty Six) (x86_64)
GCC version: (conda-forge gcc 10.3.0-16) 10.3.0
Clang version: 11.1.0 (https://github.com/conda-forge/clangdev-feedstock 2816c2cf231a2d3a6d621af9bbb2c590c9e63fe7)
CMake version: version 3.26.1
Libc version: glibc-2.35

Python version: 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:56:21)  [GCC 10.3.0] (64-bit runtime)
Python platform: Linux-6.2.15-100.fc36.x86_64-x86_64-with-glibc2.35
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:                   43 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          64
On-line CPU(s) list:             0-63
Vendor ID:                       AuthenticAMD
Model name:                      AMD Ryzen Threadripper PRO 3975WX 32-Cores
CPU family:                      23
Model:                           49
Thread(s) per core:              2
Core(s) per socket:              32
Socket(s):                       1
Stepping:                        0
Frequency boost:                 enabled
CPU(s) scaling MHz:              57%
CPU max MHz:                     4368.1641
CPU min MHz:                     2200.0000
BogoMIPS:                        6986.81
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sev sev_es
Virtualization:                  AMD-V
L1d cache:                       1 MiB (32 instances)
L1i cache:                       1 MiB (32 instances)
L2 cache:                        16 MiB (32 instances)
L3 cache:                        128 MiB (8 instances)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-63
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:          Mitigation; untrained return thunk; SMT enabled with STIBP protection
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; Retpolines, IBPB conditional, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] flake8==6.0.0
[pip3] flake8-bugbear==23.6.5
[pip3] flake8-comprehensions==3.3.0
[pip3] flake8-executable==2.0.4
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.5.0
[pip3] flake8-simplify==0.19.3
[pip3] mypy==1.4.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.1
[pip3] numpydoc==1.5.0
[pip3] onnx==1.14.1
[pip3] pytorch-sphinx-theme==0.0.19
[pip3] torch==2.1.0
[pip3] torcheval-nightly==2022.12.27
[pip3] torchsnapshot-nightly==2022.11.28
[pip3] torchtnt==0.0.4
[pip3] triton==2.1.0
[conda] magma-cuda116             2.6.1                         0    pytorch
[conda] mkl                       2022.1.0           h84fe81f_915    conda-forge
[conda] mkl-include               2022.1.0           h84fe81f_915    conda-forge
[conda] numpy                     1.23.1                   pypi_0    pypi
[conda] numpydoc                  1.5.0                    pypi_0    pypi
[conda] pytorch-sphinx-theme      0.0.19                   pypi_0    pypi
[conda] torch                     2.1.0                    pypi_0    pypi
[conda] torcheval-nightly         2022.12.27               pypi_0    pypi
[conda] torchfix                  0.1.1                    pypi_0    pypi
[conda] torchsnapshot-nightly     2022.11.28               pypi_0    pypi
[conda] torchtnt                  0.0.4                    pypi_0    pypi
[conda] triton                    2.1.0                    pypi_0    pypi


cc @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi