Repository: pytorch
Issue: `torch.jit.save` fails on Windows
Labels: oncall: jit

## 🐛 Bug

It looks like `torch.jit.save(model, IOBuffer)` fails on Windows.

This is happening in torchaudio's CircleCI unit test job.
https://app.circleci.com/pipelines/github/pytorch/audio/7480/workflows/8f04fd1e-6add-4be6-975d-98701dd81fbd/jobs/347505

But, the same workflow in Linux and macOS is fine.

```
torchaudio_unittest\models\wav2vec2\model_test.py::TestWav2Vec2Model::test_finetune_torchscript_4_hubert_asr_xlarge Fatal Python error: Aborted

Thread 0x00001244 (most recent call first):

Current thread 0x000027d4 (most recent call first):
  File "C:\Users\circleci\project\env\lib\site-packages\torch\jit\_script.py", line 706 in save_to_buffer
  File "C:\Users\circleci\project\env\lib\site-packages\torch\jit\_serialization.py", line 83 in save
  File "C:\Users\circleci\project\test\torchaudio_unittest\common_utils\func_utils.py", line 8 in torch_script
  File "C:\Users\circleci\project\test\torchaudio_unittest\models\wav2vec2\model_test.py", line 180 in _test_torchscript
  File "C:\Users\circleci\project\test\torchaudio_unittest\models\wav2vec2\model_test.py", line 195 in test_finetune_torchscript
  File "C:\Users\circleci\project\env\lib\site-packages\parameterized\parameterized.py", line 533 in standalone_func
  File "C:\Users\circleci\project\env\lib\unittest\case.py", line 605 in run
  File "C:\Users\circleci\project\env\lib\site-packages\torch\testing\_internal\common_utils.py", line 1403 in run
  File "C:\Users\circleci\project\env\lib\unittest\case.py", line 653 in __call__
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\unittest.py", line 321 in runtest
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\runner.py", line 162 in pytest_runtest_call
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\runner.py", line 255 in <lambda>
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\runner.py", line 311 in from_call
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\runner.py", line 255 in call_runtest_hook
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\runner.py", line 215 in call_and_report
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\runner.py", line 126 in runtestprotocol
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\runner.py", line 109 in pytest_runtest_protocol
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\main.py", line 348 in pytest_runtestloop
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\main.py", line 323 in _main
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\main.py", line 269 in wrap_session
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\main.py", line 316 in pytest_cmdline_main
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\circleci\project\env\lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\config\__init__.py", line 163 in main
  File "C:\Users\circleci\project\env\lib\site-packages\_pytest\config\__init__.py", line 185 in console_main
  File "C:\Users\circleci\project\env\Scripts\pytest-script.py", line 10 in <module>
```



My speculation is 
1. the model is huge (3.8GB)
2. Something specific about CircleCI.

## To Reproduce

Since the issue is happening on torchaudio's Windows CI, I cannot reproduce it on my local env, but the following script, if it's ran on Windows, should reproduce it.

**Notes**
* This requires the latest torchaudio, so either install the nightly or `git clone https://github.com/pytorch/audio` then `cd audio` (no need to build)

```python
import io

import torch
from torchaudio.models import hubert_asr_xlarge

print('Building model')
model = hubert_asr_xlarge(num_out=32)
buffer = io.BytesIO()
print('Scripting the model')
model = torch.jit.script(model)
print('Saving the model to the buffer')
torch.jit.save(model, buffer)
print(f'tell(): {buffer.tell()}')
print('Loading the model from the buffer')
buffer.seek(0)
torch.jit.load(buffer)
```

Running this in macOS / Linux prints the following. The model size is 3.8 GB. 
```
Building model
Scripting the model
Removing weight_norm from ConvolutionalPositionalEmbedding
Saving the model to the buffer
tell(): 3850398932
Loading the model from the buffer
```

## Expected behavior

`torch.jit.save` does not fail.

## Environment

From the CI log of https://app.circleci.com/pipelines/github/pytorch/audio/7480/workflows/8f04fd1e-6add-4be6-975d-98701dd81fbd/jobs/347505

```
Collecting environment information...
PyTorch version: 1.11.0.dev20210928
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Microsoft Windows Server 2019 Datacenter
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 3.21.3
Libc version: N/A

Python version: 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.17763-SP0
Is CUDA available: False
CUDA runtime version: No CUDA
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

Versions of relevant libraries:
[pip3] numpy==1.19.5

[pip3] torch==1.11.0.dev20210928

[pip3] torchaudio==0.11.0a0+0646cee
[conda] Could not collect
```