Repository: pytorch
Issue: Torch profiler corrupted names with Python 3.11
Labels: oncall: profiler

### 🐛 Describe the bug

When profiling using `with_stacks=True`, the chrome trace export can be corrupted due to corrupted function names. However, there seems to be a discrepancy between Python 3.10 and Python 3.11 and how imports are handled. I believe this is related to #64345 and #113564.

The following snippet produces good trace files with Python 3.10, but occasionally produces corrupted output on Python 3.11:
```python
import torch

with torch.profiler.profile(with_stack=True) as profile:
    pass
profile.export_chrome_trace("trace.pt.trace.json")
```

Python 3.10 trace: [trace_py310.pt.trace.json](https://github.com/pytorch/pytorch/files/14494070/trace_py310.pt.trace.json)
Python 3.11 trace: [trace_py311.pt.trace.json](https://github.com/pytorch/pytorch/files/14494086/trace_py311.pt.trace.json)

The 3.11 trace contains entries from imported modules of the form:
```json
"ph": "X", "cat": "python_function", "name": "torch/_inductor/__init__.py(0): �u}��"
```
It also contains entries related to `_distutils_hack`, but I'm not sure whether that is just another symptom.

The 3.10 trace doesn't contain those entries and instead contains:
```json
"ph": "X", "cat": "python_function", "name": "torch/_inductor/__init__.py(1): <module>"
```

### Versions

```
Collecting environment information...
PyTorch version: 2.2.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.11.8 (main, Feb 25 2024, 16:39:33) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.15.123.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2080 Ti
Nvidia driver version: 551.52
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.7
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:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          24
On-line CPU(s) list:             0-23
Vendor ID:                       GenuineIntel
Model name:                      13th Gen Intel(R) Core(TM) i7-13700K
CPU family:                      6
Model:                           183
Thread(s) per core:              2
Core(s) per socket:              12
Socket(s):                       1
Stepping:                        1
BogoMIPS:                        6835.20
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq vmx ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni umip waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm serialize flush_l1d arch_capabilities
Virtualization:                  VT-x
Hypervisor vendor:               Microsoft
Virtualization type:             full
L1d cache:                       576 KiB (12 instances)
L1i cache:                       384 KiB (12 instances)
L2 cache:                        24 MiB (12 instances)
L3 cache:                        30 MiB (1 instance)
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; Enhanced IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
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] numpy==1.26.4
[pip3] pytorch-lightning==2.2.0.post0
[pip3] torch==2.2.1+cu121
[pip3] torch-tb-profiler==0.4.3
[pip3] torchmetrics==1.3.1
[pip3] triton==2.2.0
[conda] Could not collect
```

cc @robieta @chaekit @aaronenyeshi @nbcsm @guotuofeng @guyang3532 @gaoteng-git @tiffzhaofb @dzhulgakov @davidberard98