Repository: pytorch
Issue: PyTorch profiler produces a trace that is huge and unreadable by perfetto webui when `torch._dynamo` is imported within the code traced
Labels: triaged,oncall: profiler,oncall: pt2

### 🐛 Describe the bug

Hi,

As per title.

```python
from torch.profiler import ProfilerActivity, profile
import torch

with profile(
    activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
    record_shapes=True,
    profile_memory=True,
    with_stack=True,
) as prof:
    import torch._dynamo as dynamo
    # import torch
    res = torch.rand(10, 20) @ torch.rand(20, 30)

prof.export_chrome_trace("trace.json")
```

Opening the resulting trace (which is huge, 300 MB) in perfetto gives
```
UI: https://ui.perfetto.dev/v45.0-f256f01ae

Failure parsing JSON: unable to parse string

Trace: not available (FILE). Provide repro steps.
UA: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Referrer: 
```

or with the legacy UI
```
While importing:
SyntaxError: Bad control character in string literal in JSON at position 11948131 (line 322583 column 51)
    at JSON.parse (<anonymous>)
    at new TraceEventImporter (https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:6185:19)
    at Import.createImporter_ (https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:2022:8)
    at https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:2016:167
    at Task.run (https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:3252:95)
    at runAnother (https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:3255:371)
    at runTask (https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:3019:57)
    at processIdleWork (https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:3024:116)
    at https://ui.perfetto.dev/v45.0-f256f01ae/assets/catapult_trace_viewer.js:3017:81
```

and indeed there is an entry with an illegal name in the `trace.json`:
```
  {
    "ph": "X", "cat": "python_function", "name": "£ù·(1): <module>", "pid": 41202, "tid": 41202,
    "ts": 1720796204239178, "dur": 7618,
    "args": {
      "Python parent id": 46055, "Python id": 46056, "Ev Idx": 46080
    }
  },
```

This issue does not occur when `import torch` is being traced.

### Versions

```
PyTorch version: 2.3.1+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
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: 14.0.0-1ubuntu1.1
CMake version: version 3.27.4
Libc version: glibc-2.35

Python version: 3.9.12 (main, Apr  5 2022, 06:56:58)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.15.0-113-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 11.7.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3060 Laptop GPU
Nvidia driver version: 535.183.01
cuDNN version: Probably one of the following:
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.6.0
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):                             20
On-line CPU(s) list:                0-19
Vendor ID:                          GenuineIntel
Model name:                         12th Gen Intel(R) Core(TM) i7-1280P
CPU family:                         6
Model:                              154
Thread(s) per core:                 2
Core(s) per socket:                 14
Socket(s):                          1
Stepping:                           3
CPU max MHz:                        4800,0000
CPU min MHz:                        400,0000
BogoMIPS:                           3993.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 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          544 KiB (14 instances)
L1i cache:                          704 KiB (14 instances)
L2 cache:                           11,5 MiB (8 instances)
L3 cache:                           24 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-19
Vulnerability Gather data sampling: Not affected
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 rstack overflow: Not affected
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; BHI BHI_DIS_S
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.12.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] lovely-numpy==0.2.11
[pip3] mypy==1.9.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.0
[pip3] onnx==1.16.0
[pip3] onnx-graphsurgeon==0.3.12
[pip3] onnx-simplifier==0.4.35
[pip3] onnx-tool==0.9.0
[pip3] onnxoptimizer==0.3.13
[pip3] onnxruntime==1.17.1
[pip3] onnxruntime-extensions==0.9.0
[pip3] onnxruntime-gpu==1.17.1
[pip3] onnxscript==0.1.0.dev20231010
[pip3] open-clip-torch==2.23.0
[pip3] optree==0.10.0
[pip3] torch==2.3.1+cu118
[pip3] torch-model-archiver==0.6.1
[pip3] torch-tb-profiler==0.4.1
[pip3] torch-workflow-archiver==0.2.5
[pip3] torchaudio==2.3.1+cu118
[pip3] torchinfo==1.7.0
[pip3] torchserve==0.6.1
[pip3] torchvision==0.18.1+cu118
[pip3] torchviz==0.0.2
[pip3] triton==2.3.1
[conda] cudatoolkit               11.3.1               h2bc3f7f_2    anaconda
[conda] lovely-numpy              0.2.11                   pypi_0    pypi
[conda] numpy                     1.26.0                   pypi_0    pypi
[conda] open-clip-torch           2.23.0                   pypi_0    pypi
[conda] optree                    0.10.0                   pypi_0    pypi
[conda] torch                     2.3.1+cu118              pypi_0    pypi
[conda] torch-model-archiver      0.6.1                    pypi_0    pypi
[conda] torch-tb-profiler         0.4.1                    pypi_0    pypi
[conda] torch-workflow-archiver   0.2.5                    pypi_0    pypi
[conda] torchaudio                2.3.1+cu118              pypi_0    pypi
[conda] torchfix                  0.4.0                    pypi_0    pypi
[conda] torchinfo                 1.7.0                    pypi_0    pypi
[conda] torchserve                0.6.1                    pypi_0    pypi
[conda] torchvision               0.18.1+cu118             pypi_0    pypi
[conda] torchviz                  0.0.2                    pypi_0    pypi
[conda] triton                    2.3.1                    pypi_0    pypi
```

cc @robieta @chaekit @aaronenyeshi @guotuofeng @guyang3532 @dzhulgakov @davidberard98 @briancoutinho @sraikund16 @sanrise @ezyang @anijain2305 @chauhang @penguinwu