Repository: pytorch
Issue: Unrecognized data format when using release libtorch libraries in debug build
Labels: oncall: jit

## Issue description

Once the model has been created, trained and evaluated (in Python using PyTorch), it is exported to a PT file (using TorchScript). It is then simply loaded using libtorch C++ to see if an error occurs.

The error (see **Code example** below) states that there is an issue with the format of the data supplied. However once the build type for the application that uses libtorch is switched to release, the issue disappears.

It appears that at least for VS2017 (MSVC v141, CMake 3.12) combining different build versions (release with debug and vice versa) is not working. Using the release version of libtorch in a debug build of a 3rd party software should be allowed for two main reasons:

 * libtorch debug version (especially GPU) is huge
 * Developer may not want to actually debug libtorch and only debug code that is calling libtorch functionality.

## Code example

Standard project with CMake using the following code (taken from the examples in the documentation):

```
#include <torch/script.h>
#include <iostream>
#include <memory>

int main(int argc, const char* argv[])
{
	torch::jit::script::Module module;
	try {
		std::cout << "Trying to load model..." << std::endl;
		module = torch::jit::load("test_model.pt");
	}
	catch (const c10::Error& e) {
		std::cerr << "Loading failed" << std::endl;
		std::cerr << e.what() << std::endl;
		return -1;
	}

	std::cout << "Loading successful" << std::endl;
}
```
Creating a debug build, while linking at the release libraries of libtorch, creates and error:

```
Trying to load model...
Loading failed
Unrecognized data format
Exception raised from load at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\jit\serialization\import.cpp:449 (most recent call first):
00007FF84F79DA2200007FF84F79D9C0 c10.dll!c10::Error::Error [<unknown file> @ <unknown line number>]
00007FF84F79D43E00007FF84F79D3F0 c10.dll!c10::detail::torchCheckFail [<unknown file> @ <unknown line number>]
00007FFFD64CB54700007FFFD64CB4E0 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]
00007FFFD64CB42A00007FFFD64CB380 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]
00007FF762B5682B00007FF762B566E0 pytroch_load_model.exe!main [c:\users\USER\projects\cmake dx cuda pytorch\cmake_integration_examples\pytorch\src\pytroch_load_model.cpp @ 17]
00007FF762B81BF400007FF762B81BC0 pytroch_load_model.exe!invoke_main [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 79]
00007FF762B81A9E00007FF762B81970 pytroch_load_model.exe!__scrt_common_main_seh [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288]
00007FF762B8195E00007FF762B81950 pytroch_load_model.exe!__scrt_common_main [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 331]
00007FF762B81C8900007FF762B81C80 pytroch_load_model.exe!mainCRTStartup [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp @ 17]
00007FF884C6703400007FF884C67020 KERNEL32.DLL!BaseThreadInitThunk [<unknown file> @ <unknown line number>]
00007FF884F4265100007FF884F42630 ntdll.dll!RtlUserThreadStart [<unknown file> @ <unknown line number>]
```

The error, even if it had something to do with the loaded data, is not helpful at all since `unknown file` and `unknown line number` is as vague as it gets.

Switching to a release build (linking against the release libtorch libraries) resolves the problem and the model is loaded successfully. Note that the model itself is not important here. Multiple both self-made as well as download from various "Paper with Code" sources and repositories for PyTorch models suffer from the same issue.

## System Info
Environment (using Python script to collect data):

```
PyTorch version: 1.12.1
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 Enterprise
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 3.25.0-rc1
Libc version: N/A

Python version: 3.10.6 | packaged by conda-forge | (main, Oct  7 2022, 20:14:50) [MSC v.1916 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.19043-SP0
Is CUDA available: True
CUDA runtime version: 11.2.67
CUDA_MODULE_LOADING set to:
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3080 Laptop GPU
Nvidia driver version: 516.94
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.1
[pip3] torch==1.12.1
[pip3] torchaudio==0.12.1
[pip3] torchvision==0.13.1
[conda] blas                           1.0              mkl
[conda] cudatoolkit               11.3.1         h59b6b97_2
[conda] mkl                            2021.4.0    haa95532_640
[conda] mkl-service               2.4.0           py310h2bbff1b_0
[conda] mkl_fft                       1.3.1           py310ha0764ea_0
[conda] mkl_random             1.2.2           py310h4ed8f06_0
[conda] numpy                      1.23.1          py310h6d2d95c_0
[conda] numpy-base             1.23.1          py310h206c741_0
[conda] pytorch                    1.12.1          py3.10_cuda11.3_cudnn8_0    pytorch
[conda] pytorch-mutex         1.0              cuda    pytorch
[conda] torchaudio                0.12.1         py310_cu113    pytorch
[conda] torchvision               0.13.1          py310_cu113    pytorch
```

- PyTorch: latest, libtorch 1.12.1 (release build, cpu and gpu versions) 
- How you installed PyTorch (conda, pip, source): PyTorch is installed via conda (miniconda environment)
- CUDA/cuDNN version: CUDA 11.2, latest cuDNN
- CMake version: 3.12 (supplied by Visual Studio 2017 Pro installation)
- Compiler: MSVC v141 (supplied by Visual Studio 2017 Pro installation)