Repository: pytorch
Issue: Profiling with stack enabled results in error when Python's cProfile is also running
Labels: oncall: profiler

### 🐛 Describe the bug

I am trying to use cProfile and torch's profiler at the same time, and this worked fine for me before I updated Pytorch from 1.11 to 1.13 (with conda). Here's an example:

```python
import torch
import cProfile


# set up profilers
c_prof = cProfile.Profile()

schedule = torch.profiler.schedule(
    wait=1, warmup=1, active=1, repeat=1
)
torch_prof = torch.profiler.profile(
    schedule=schedule, 
    with_stack=True # results in error
)


# enable python profiler first
c_prof.enable()

# enable torch profiler second
torch_prof.start()


# profile some code
for i in range(3):
    t = torch.rand(4, 4)

    torch_prof.step() # error on last step

    print(f'step {i+1} complete')


# disable profilers
torch_prof.stop()
c_prof.disable()
```

out:
```
step 1 complete
step 2 complete
STAGE:2023-01-09 13:06:00 24668:24668 ActivityProfilerController.cpp:294] Completed Stage: Warm Up
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[1], line 21
     19 for i in range(3):
     20     t = torch.rand(4, 4)
---> 21     torch_prof.step()
     22     print(f'step {i+1} complete')
     24 torch_prof.stop()

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/torch/profiler/profiler.py:500, in profile.step(self)
    497 self.step_num += 1
    498 self.current_action = self.schedule(self.step_num)
--> 500 self._transit_action(prev_action, self.current_action)
    502 prof.kineto_step()
    503 if self.record_steps:

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/torch/profiler/profiler.py:515, in profile._transit_action(self, prev_action, current_action)
    513 if action_list:
    514     for action in action_list:
--> 515         action()

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/torch/profiler/profiler.py:119, in _KinetoProfile.stop_trace(self)
    117 def stop_trace(self):
    118     assert self.profiler is not None
--> 119     self.profiler.__exit__(None, None, None)
...
    232     use_cuda=self.use_cuda,
    233     profile_memory=self.profile_memory,
    234     with_flops=self.with_flops)

RuntimeError: stack.size() INTERNAL ASSERT FAILED at "/opt/conda/conda-bld/pytorch_1670525539683/work/torch/csrc/autograd/profiler_python.cpp":884, please report a bug to PyTorch. Python replay stack is empty.
```

### Versions

PyTorch version: 1.13.1
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: CentOS Linux 7 (Core) (x86_64)
GCC version: (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.17

Python version: 3.9.12 (main, Jun  1 2022, 11:38:51)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-3.10.0-1160.80.1.el7.x86_64-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 11.7.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A100 80GB PCIe
GPU 1: NVIDIA A100 80GB PCIe

Nvidia driver version: 520.61.05
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.23.5
[pip3] torch==1.13.1
[pip3] torch-cluster==1.6.0
[pip3] torch-geometric==2.2.0
[pip3] torch-scatter==2.1.0
[pip3] torch-sparse==0.6.16
[pip3] torch-tb-profiler==0.4.0
[pip3] torchaudio==0.13.1
[pip3] torchvision==0.14.1
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.3.1               h2bc3f7f_2  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0            py39h7f8727e_0  
[conda] mkl_fft                   1.3.1            py39hd3c417c_0  
[conda] mkl_random                1.2.2            py39h51133e4_0  
[conda] numpy                     1.23.5           py39h14f4228_0  
[conda] numpy-base                1.23.5           py39h31eccc5_0  
[conda] pyg                       2.2.0           py39_torch_1.13.0_cu117    pyg
[conda] pytorch                   1.13.1          py3.9_cuda11.7_cudnn8.5.0_0    pytorch
[conda] pytorch-cluster           1.6.0           py39_torch_1.13.0_cu117    pyg
[conda] pytorch-cuda              11.7                 h67b0de4_1    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] pytorch-scatter           2.1.0           py39_torch_1.13.0_cu117    pyg
[conda] pytorch-sparse            0.6.16          py39_torch_1.13.0_cu117    pyg
[conda] torch-tb-profiler         0.4.0                    pypi_0    pypi
[conda] torchaudio                0.13.1               py39_cu117    pytorch
[conda] torchvision               0.14.1               py39_cu117    pytorch

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