<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">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) -&gt; Any'</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>Provided code sample</li>
</ol>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="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)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-k">import</span> <span class="pl-s1">torch</span>.<span class="pl-s1">nn</span> <span class="pl-k">as</span> <span class="pl-s1">nn</span>


<span class="pl-k">class</span> <span class="pl-v">CustomModule</span>(<span class="pl-s1">nn</span>.<span class="pl-v">Module</span>):
    <span class="pl-k">def</span> <span class="pl-en">__init__</span>(<span class="pl-s1">self</span>, <span class="pl-s1">dims</span>):
        <span class="pl-en">super</span>(<span class="pl-v">CustomModule</span>, <span class="pl-s1">self</span>).<span class="pl-en">__init__</span>()
        <span class="pl-s1">self</span>.<span class="pl-s1">dims</span> <span class="pl-c1">=</span> <span class="pl-s1">dims</span>

    <span class="pl-k">def</span> <span class="pl-en">forward</span>(<span class="pl-s1">self</span>, <span class="pl-s1">x</span>):
        <span class="pl-k">return</span> <span class="pl-s1">x</span> <span class="pl-c1">+</span> <span class="pl-s1">torch</span>.<span class="pl-en">randn_like</span>(<span class="pl-s1">x</span>)

    <span class="pl-k">def</span> <span class="pl-en">random_method</span>(<span class="pl-s1">self</span>):
        <span class="pl-k">return</span> <span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-s1">self</span>.<span class="pl-s1">dims</span>)


<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    <span class="pl-s1">dnn</span> <span class="pl-c1">=</span> <span class="pl-v">CustomModule</span>(<span class="pl-c1">100</span>)
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-s1">dnn</span>.<span class="pl-en">random_method</span>()
    <span class="pl-en">print</span>(<span class="pl-s1">out</span>)</pre></div>
<h2 dir="auto">Environment</h2>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0): 1.2.0</li>
<li>OS (e.g., Linux): Ubuntu 18.04.02</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source): pip</li>
<li>Build command you used (if compiling from source):</li>
<li>Python version: 3.6</li>
<li>CUDA/cuDNN version: 10</li>
<li>GPU models and configuration:</li>
<li>Any other relevant information:</li>
</ul>
<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>

<p dir="auto">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.<br>
I built the load &amp; run forward function into '.dll' library, and I implemented a mex api function can call them.<br>
When I call the mex api from Matlab, the 2 models are loaded normally, and the first model runs forward properly.<br>
However, when run forward the 2nd model, I got an exception.<br>
I have no clue while the c++ implementation works fine but the exception occurs when call it through mex api from Matlab.<br>
Because the load &amp; run forward functions are unchanged and the mex api just calls the function and do nothing,<br>
so, I expected the exactly same results from both all c++ implementation and running with mex api.<br>
It is more difficult to debug because there is no call-stack print.<br>
Is there any way to get call-stack  or catch the exception message? I placed on try-catch around  'model. forward()' but<br>
the exception was not caught in.<br>
Please give me any advice. Thanks in advance.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>load &amp; forward models (StructureFlow_in_Cpp.cpp)</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="int load_model(std::string model_file1, std::string model_file2, bool _gpu)
{
	g_module1 = torch::jit::load(model_file1, torch::kCUDA);  ==&gt; ok
	g_module2 = torch::jit::load(model_file2, torch::kCUDA);  ==&gt; ok
}
"><pre class="notranslate"><code class="notranslate">int load_model(std::string model_file1, std::string model_file2, bool _gpu)
{
	g_module1 = torch::jit::load(model_file1, torch::kCUDA);  ==&gt; ok
	g_module2 = torch::jit::load(model_file2, torch::kCUDA);  ==&gt; ok
}

</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="cv::Mat run_forward(cv::Mat&amp; image_rgb, cv::Mat&amp; struct_rgb, cv::Mat&amp; 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 =&gt; 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 ==&gt; **Exception occurs!!**
}"><pre class="notranslate"><code class="notranslate">cv::Mat run_forward(cv::Mat&amp; image_rgb, cv::Mat&amp; struct_rgb, cv::Mat&amp; 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 =&gt; 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 ==&gt; **Exception occurs!!**
}
</code></pre></div>
<ol start="2" dir="auto">
<li>Transfer call from mex api (StructureFlow_in_Cpp.cpp)</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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 = &quot;../model/model1_trace_torch15_usercat2.pt&quot;;
	std::string model2Path = &quot;../model/model2_trace_torch15_usercat2.pt&quot;;
	load_model(model1Path, model2Path, true);

        /*3 Forward Models*/
	cv::Mat result_img = run_forward(img, structure, mask, true);
}"><pre class="notranslate"><code class="notranslate">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);
}
</code></pre></div>
<ol start="3" dir="auto">
<li>mex api (StructureFlow_matlab_api.cpp)</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	test();
	return;
}"><pre class="notranslate"><code class="notranslate">void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	test();
	return;
}
</code></pre></div>
<ol start="4" dir="auto">
<li>
<p dir="auto">Call mex api from Matlab (matlab_call_structure_flow.m)<br>
<code class="notranslate">StructureFlow_matlab_api();</code></p>
</li>
<li>
<p dir="auto">Errors</p>
</li>
</ol>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<p dir="auto">You can reproduce the error by run the matlab <a href="https://drive.google.com/file/d/1VSRUVjD1Xa-CBqKk4NMbWEdnMccaKBK0/view?usp=sharing" rel="nofollow">codes</a></p>
<h2 dir="auto">Expected behavior</h2>

<p dir="auto">forward running of the 2nd model also works well.</p>
<h2 dir="auto">Environment</h2>
<ol dir="auto">
<li>python envs</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<ol start="2" dir="auto">
<li>c++ envs</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="libtorch 1.6.0 
Visutal Studio 2017 (C++14)
GPU models : GTX-1070
"><pre class="notranslate"><code class="notranslate">libtorch 1.6.0 
Visutal Studio 2017 (C++14)
GPU models : GTX-1070

</code></pre></div>
<ol start="3" dir="auto">
<li>matlab envs<br>
<code class="notranslate">Matlab R2020a</code></li>
</ol>
<h2 dir="auto">Additional context</h2>
<ol dir="auto">
<li>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.<br>
So I feel that this is another problem not related to torch.cat.</li>
</ol>