Issue 1:
🐛 Bug

When I implement a class that inherits nn.Module, the autocomplete of PyCharm does not find the reference of a custom method. For example in the code below PyCharm shows a warning: Cannot find reference 'random_method' in '(input: (Any, ...), kwargs: dict) -> Any'

To Reproduce

Steps to reproduce the behavior:

Provided code sample

import torch
import torch.nn as nn

class CustomModule(nn.Module):
def __init__(self, dims):
super(CustomModule, self).__init__()
self.dims = dims

def forward(self, x):
return x + torch.randn_like(x)

def random_method(self):
return torch.randn(self.dims)

if __name__ == '__main__':
dnn = CustomModule(100)
out = dnn.random_method()
print(out)

Environment

PyTorch Version (e.g., 1.0): 1.2.0

OS (e.g., Linux): Ubuntu 18.04.02

How you installed PyTorch (conda, pip, source): pip

Build command you used (if compiling from source):

Python version: 3.6

CUDA/cuDNN version: 10

GPU models and configuration:

Any other relevant information:

Issue 2:
🐛 Bug

I exported 2 pytorch models to ' .pt' files and implemented c++ functions that load models from the '.pt 'files and run models (forward). The c++ implementation works fine, I can get proper results from the models.

I built the load & run forward function into '.dll' library, and I implemented a mex api function can call them.

When I call the mex api from Matlab, the 2 models are loaded normally, and the first model runs forward properly.

However, when run forward the 2nd model, I got an exception.

I have no clue while the c++ implementation works fine but the exception occurs when call it through mex api from Matlab.

Because the load & run forward functions are unchanged and the mex api just calls the function and do nothing,

so, I expected the exactly same results from both all c++ implementation and running with mex api.

It is more difficult to debug because there is no call-stack print.

Is there any way to get call-stack or catch the exception message? I placed on try-catch around 'model. forward()' but

the exception was not caught in.

Please give me any advice. Thanks in advance.

To Reproduce

Steps to reproduce the behavior:

load & forward models (StructureFlow_in_Cpp.cpp)

int load_model(std::string model_file1, std::string model_file2, bool _gpu)
{
g_module1 = torch::jit::load(model_file1, torch::kCUDA); ==> ok
g_module2 = torch::jit::load(model_file2, torch::kCUDA); ==> ok
}

cv::Mat run_forward(cv::Mat& image_rgb, cv::Mat& struct_rgb, cv::Mat& mask_gray, bool _gpu)
{
//1st stage : first model forward
torch::Tensor input1 = torch::cat({ tensor_image_norm, tensor_struct_norm, tensor_mask_norm }, 1);
input1 = input1.to(at::kCUDA);
auto stage1 = g_module1.forward({ input1 }).toTensor(); // 1st model forward => ok

//2nd stage : second model forward
torch::Tensor input2 = torch::cat({ tensor_image_norm, stage1_cpu, tensor_mask_norm }, 1);
auto stage2_all = g_module2.forward({ input2 }).toTuple(); // 2nd model forward ==> **Exception occurs!!**
}

Transfer call from mex api (StructureFlow_in_Cpp.cpp)

int test()
{
/* 1. Load Images*/
Mat img = cv::imread(imgPath);
Mat mask = cv::imread(maskPath, IMREAD_GRAYSCALE);
Mat structure = cv::imread(structPath);

/*2. Load Model*/
std::string model1Path = "../model/model1_trace_torch15_usercat2.pt";
std::string model2Path = "../model/model2_trace_torch15_usercat2.pt";
load_model(model1Path, model2Path, true);

/*3 Forward Models*/
cv::Mat result_img = run_forward(img, structure, mask, true);
}

mex api (StructureFlow_matlab_api.cpp)

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
test();
return;
}

Call mex api from Matlab (matlab_call_structure_flow.m)

StructureFlow_matlab_api();

Errors

Unexpected Standard exception from MEX file
What():The following operation failed in the TorchScript interpreter.
Traceback of TorchScript (most recent call last):
RuntimeError: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript (most recent call last):
RuntimeError: error in LoadLibraryA

You can reproduce the error by run the matlab codes

Expected behavior

forward running of the 2nd model also works well.

Environment

python envs

PyTorch version: 1.5.1
Is debug build: No
CUDA used to build PyTorch: 10.2

OS: Ubuntu 18.04.4 LTS
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
CMake version: Could not collect

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 10.1.243
GPU models and configuration: GPU 0: TITAN Xp
Nvidia driver version: 440.82
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5

Versions of relevant libraries:
[pip] numpy==1.18.1
[pip] torch==1.5.1
[pip] torchvision==0.6.0a0+35d732a
[conda] blas 1.0 mkl
[conda] cudatoolkit 10.2.89 hfd86e86_1
[conda] mkl 2020.1 217
[conda] mkl-service 2.3.0 py36he904b0f_0
[conda] mkl_fft 1.0.15 py36ha843d7b_0
[conda] mkl_random 1.1.0 py36hd6b4f25_0
[conda] numpy 1.18.1 py36h4f9e942_0
[conda] numpy-base 1.18.1 py36hde5b4d6_1
[conda] pytorch 1.5.1 py3.6_cuda10.2.89_cudnn7.6.5_0 pytorch
[conda] torchvision 0.6.1 py36_cu102 pytorch

c++ envs

libtorch 1.6.0
Visutal Studio 2017 (C++14)
GPU models : GTX-1070

matlab envs

Matlab R2020a

Additional context

Before I faced this problem, I got 'RuntimeError: error in LoadLibraryA' at the lines using 'torch.cat'. I can check where the error happens because call stacks are printed. And It only occurs when I run it through mex api and all c++ implementation works fine. I fixed this by replacing 'torch.cat' with a self-implemented cat function.

So I feel that this is another problem not related to torch.cat.