Stack overflow - conv1d.forward in DLL on CUDA

username: cheggars
cooked: <p>I faced a problem with calling conv1d.forward in DLL on CUDA.  In the following C++ code snippet line 4 ‘conv1d.forward’ crashes with stack overflow. The full CPP file (27 lines) is at the bottom of this message.</p>
<pre><code class="lang-auto">auto Net = torch::nn::Conv1d(torch::nn::Conv1dOptions(21, 2, 3));
Net-&gt;to(device);
torch::Tensor X = torch::rand({ 5,21,25 }).to(device);
torch::Tensor Y = Net-&gt;forward(X);
</code></pre>
<p>Having experimented on two PCs with different GPU types I found the problem is consistently reproduced if all of the following criteria are met:</p>
<ol>
<li>DLL. The same code in console EXE application runs normally.</li>
<li>GPU/CUDA. There is no problem running the same code on CPU</li>
<li>Convolutional layer. No problem with other layer types, e.g. linear.</li>
</ol>
<p>I tried debugging DLL in Visual Studio (debugger window screenshot attached). Call stack suggests that stack overflow happens inside cudnn_cnn_infer64_8.dll module. This is part of Nvidia CUDNN library.</p>
<p>I am not sure if this error is part of Pytorch or Nvidia CUDNN. If anyone has any suggestion on how to resolve this please respond.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/1/112ee38a10e833e8de5365241f2a0a34131ace23.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/112ee38a10e833e8de5365241f2a0a34131ace23" title="screenshot"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/1/112ee38a10e833e8de5365241f2a0a34131ace23_2_690x388.jpeg" alt="screenshot" data-base62-sha1="2s0zHxu4O6m8f4lOUvFhirZBOp5" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/1/112ee38a10e833e8de5365241f2a0a34131ace23_2_690x388.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/1/1/112ee38a10e833e8de5365241f2a0a34131ace23_2_1035x582.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/1/112ee38a10e833e8de5365241f2a0a34131ace23_2_1380x776.jpeg 2x" data-dominant-color="E9EAED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">screenshot</span><span class="informations">2112×1188 339 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto">
#include &lt;torch/torch.h&gt;
#define XLExport extern "C" __declspec(dllexport)
XLExport int _stdcall MLP_DLL();

int _stdcall MLP_DLL()
{
	std::ofstream log("output.txt");

	torch::Device device(torch::kCPU);
	if (torch::cuda::is_available()) {		
		log &lt;&lt; "Cuda found" &lt;&lt; std::endl;
		device = torch::Device(torch::kCUDA);
	}

	auto Net = torch::nn::Conv1d(torch::nn::Conv1dOptions(21, 2, 3));
	Net-&gt;to(device);
	torch::Tensor X = torch::rand({ 5,21,25 }).to(device);

	log &lt;&lt; " starting forward" &lt;&lt; std::endl;		
	
	torch::Tensor Y = Net-&gt;forward(X);
	log &lt;&lt; "Y = " &lt;&lt; std::endl;
	log &lt;&lt; Y &lt;&lt; std::endl;

	return 0;
}
</code></pre>

username: cheggars
cooked: <p>UPDATE: this was my mistake. I used rundll32.exe to execute my DLL. Apparently, as rundll32 was originally designed to run Windows modules it is not suitable for custom-made DLL libraries.</p>
<p>My DLL runs normally if called not by rundll32.</p>