Repository: pytorch
Issue: Migration from c10::variant to std::variant causes undefined symbols when linking against older pytorch
Labels: module: build,triaged

### 🐛 Describe the bug

# Context

We build our library against *nightly PyTorch wheel*.
We ship our library as a wheel package and expects customer to install PyTorch wheel along side with it. Our test infrastructure tests our pip wheel against both: 1. nightly pytorch wheel; and 2. pytorch 2.1 release wheel.

The above workflow has been working smoothly, until the recent change that breaks the compatibility, and our binary no-longer works across pytorch nightly and 2.1 release.

# Pitch

PR #109723 migrates to `std::variant` from `c10::variant`. This change is leaked in our API.
https://github.com/pytorch/pytorch/blob/4f79161452c876a76be0499f29c0b84bc64537e6/c10/util/Exception.h#L118-L130

Our library built against pytorch nightly has undefined symbols which is provided by torch.
```
root@cc2800f1e374:/opt/pytorch/nvfuser# nm -C nvfuser/lib/libnvfuser_codegen.so | grep c10::Warning
                 U c10::WarningUtils::get_warnAlways()
                 U c10::warn(c10::Warning const&)
                 U c10::Warning::Warning(std::variant<c10::Warning::UserWarning, c10::Warning::DeprecationWarning>, c10::SourceLocation, char const*, bool)
                 U c10::Warning::Warning(std::variant<c10::Warning::UserWarning, c10::Warning::DeprecationWarning>, c10::SourceLocation const&, std::string, bool)
```

However, this means that the library we have won't work against older PyTorch release. (i.e. either older pytorch nightly wheel from before that code change and more importantly 2.1 release). Since the library gives a different symbol using c10::variant instead of std::variant.

In a general case, `c10::Warning` could be only a pytorch internal bits. Targets that are *not* using `c10::Warning` explicitly should not care about an internal implementation change.
By marking c10::Warning as a weak symbol allows targets built against PyTorch to ignore the undefined symbol from the name change. (see PR #111860).

### Versions

Collecting environment information...
PyTorch version: 2.2.0a0+gitbabb6c6
Is debug build: False
CUDA used to build PyTorch: 12.3
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.27.7
Libc version: glibc-2.35

Python version: 3.10.11 (main, Oct 15 2023, 07:05:32) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-126-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.3.52
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100 80GB PCIe
Nvidia driver version: 525.105.17
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.6
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.6
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
BogoMIPS:                        6987.00
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 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 sme ssbd mba sev ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip rdpid overflow_recov succor smca
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 Spec store bypass: Vulnerable
Vulnerability Spectre v1:        Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers
Vulnerability Spectre v2:        Vulnerable, IBPB: disabled, STIBP: disabled
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.1
[pip3] optree==0.9.2
[pip3] torch==2.2.0a0+gitbabb6c6
[pip3] torchvision==0.17.0a0+68161e9
[pip3] triton==2.1.0
[conda] Could not collect


cc @malfet @seemethere