Repository: pytorch
Issue: Load torchscript model with custom operators (GNN) on Windows
Labels: oncall: jit

## 🐛 Bug

I successfully froze a model with custom operators (from very nice pytorch geometric library [torch-sparse/torch-scatter]) on Python side but cannot load it on C++ side with LibTorch on Windows only. I also attach the model I can load on Linux but not on Windows. I did not test in MacOS environment.

## To Reproduce

Steps to reproduce the behavior:

1. Build the c++ API for both torch-sparse and torch-scatter with visual studio 2017 (See https://github.com/rusty1s/pytorch_sparse#c-api and https://github.com/rusty1s/pytorch_scatter#c-api) or a Ubuntu compiler (e.g., GCC).
2. Build the minimal example main.cpp + CMakeLists.txt
3. Run it with hello-world.exe path/to/the/scripted/model

main.cpp

    #include <iostream>
    #include <torch/torch.h>
    #include <torch/script.h>
    #include <torchsparse/sparse.h>
    #include <torchscatter/scatter.h>

     int main(int argc, const char* argv[]) {

	// Test torchsparse library
	torch::Tensor tensor = torch::tensor({ 0, 0, 0, 0, 1, 1, 1 });
	std::cout << tensor << std::endl;
	std::cout << ind2ptr(tensor, 2) << std::endl;

	// Test torchscatter library
	torch::Tensor src = torch::tensor({ 0.5, 0.4, 0.1, 0.6 });
	torch::Tensor index = torch::tensor({ 0, 0, 1, 1 });
	std::cout << src << std::endl;
	std::cout << index << std::endl;
	std::cout << scatter_sum(src, index, 0, torch::nullopt, torch::nullopt) << std::endl;

	// Test frozen GNN loading
	if (argc != 2) {
		std::cerr << "usage: example-app <path-to-exported-script-module>\n";
		return -1;
	}

	std::cout << "Trying to load model..." << std::endl;
	torch::jit::script::Module module;

	try {
		module = torch::jit::load(argv[1]);
	}
	catch (const c10::Error& e) {
		std::cout << e.what() << std::endl;
		std::cerr << "error loading the model\n";
		return -1;
	}

	// Test inference forward pass
	auto x = torch::tensor({ {-1}, {0}, {1} }, at::kFloat);
	auto edge_index = torch::tensor({ {0, 1, 1, 2}, {1, 0, 2, 1} }, at::kLong);
	auto edge_weight = torch::tensor({ 0, 1, 1, 2 }, at::kFloat);
	auto batch = torch::tensor({ 0, 0, 0 }, at::kLong);

	auto output = module({ x, edge_index, edge_weight, batch }).toTensor();
	std::cout << "output: " << output << std::endl;
    }

CMakeLists.txt

    cmake_minimum_required(VERSION 3.10)
    project(hello-world)

    find_package(TorchSparse REQUIRED)
    find_package(TorchScatter REQUIRED)

    add_executable(hello-world main.cpp)

    target_compile_features(hello-world PUBLIC cxx_range_for)
    target_link_libraries(hello-world TorchSparse::TorchSparse TorchScatter::TorchScatter)
    set_property(TARGET hello-world PROPERTY CXX_STANDARD 14)

## Expected behavior

The model should be successfully loaded in c++ for both Linux and Windows OS. Currently I can only load it on Linux (Ubuntu 18.04) thanks to the built libtorchsparse.so and libtorchscatter.so shared libraries containing the custom ops. 

## Environment

 - PyTorch Version (e.g., 1.0): 1.8.1
 - OS (e.g., Linux): Windows AND Linux
 - How you installed PyTorch (`conda`, `pip`, source): pip
 - Build command you used (if compiling from source):
 - Python version: 3.6
 - CUDA/cuDNN version: 10.2
 - GPU models and configuration:
 - Any other relevant information: Windows compiler -> VS2017

## Additional context
On Python side, the model can be successfully reloaded on both Ubtuntu and Windows with torch.jit.load() function.
Moreover, I also tried to load the model (frozen on Windows with Python) on Ubuntu with LibTorch and it worked! So I think it is a problem very linked to Windows. Maybe on how the compiler links to the dlls? Of course, I copied the dlls (torchsparse.dll/torchscatter.dll) of my custom operators next to my hello-world.exe. Never so easy to deploy a ML model in production :/
[gcn.zip](https://github.com/pytorch/pytorch/files/7105852/gcn.zip)