<p dir="auto">Hello,<br>
I was trying to code example of neural network on CUDA. I have run some examples which they were working on CPU but not on GPU.</p>
<p dir="auto">Not sure if it's a bug or my mistake.</p>
<p dir="auto">Please note that CUDA version of <a href="https://github.com/Andrej-sens/libtorch_examples_cpp/blob/master/MNIST_Example_GPU.cpp">MNIST example</a> is working properly.</p>
<p dir="auto">How to replicate error, just change bias to false. With <code class="notranslate">bias=true</code> I am able to move network model to CUDA.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#include &lt;torch/torch.h&gt;

// Define a new Module.
struct Net : torch::nn::Module {
	Net() {
		torch::nn::Conv2d conv = torch::nn::Conv2d(torch::nn::Conv2dOptions(3, 16, 3)
			.bias(true)
			.stride(1)
			.padding(1)
		);

		module-&gt;push_back(conv);

		register_module(&quot;Layer&quot;,module);
	}

	torch::Tensor forward(torch::Tensor x) {
		/**SOME FORWARD**/
		return x;
	}

	torch::nn::Sequential module;
};

int main() {
	auto net = std::make_shared&lt;Net&gt;();
	net-&gt;to(at::kCUDA); // It crashes here
}"><pre class="notranslate"><code class="notranslate">#include &lt;torch/torch.h&gt;

// Define a new Module.
struct Net : torch::nn::Module {
	Net() {
		torch::nn::Conv2d conv = torch::nn::Conv2d(torch::nn::Conv2dOptions(3, 16, 3)
			.bias(true)
			.stride(1)
			.padding(1)
		);

		module-&gt;push_back(conv);

		register_module("Layer",module);
	}

	torch::Tensor forward(torch::Tensor x) {
		/**SOME FORWARD**/
		return x;
	}

	torch::nn::Sequential module;
};

int main() {
	auto net = std::make_shared&lt;Net&gt;();
	net-&gt;to(at::kCUDA); // It crashes here
}
</code></pre></div>
<p dir="auto">It will throw</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Exception thrown at 0x00007FFB4A2EA839 in Object_detection.exe: Microsoft C++ exception: c10::Error at memory location 0x000000635F0FDE90.
Unhandled exception at 0x00007FFB4A2EA839 in Object_detection.exe: Microsoft C++ exception: c10::Error at memory location 0x000000635F0FDE90."><pre class="notranslate"><code class="notranslate">Exception thrown at 0x00007FFB4A2EA839 in Object_detection.exe: Microsoft C++ exception: c10::Error at memory location 0x000000635F0FDE90.
Unhandled exception at 0x00007FFB4A2EA839 in Object_detection.exe: Microsoft C++ exception: c10::Error at memory location 0x000000635F0FDE90.
</code></pre></div>
<p dir="auto">Thank you very much for any advise.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ezyang/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ezyang">@ezyang</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gchanan/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gchanan">@gchanan</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/zou3519/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/zou3519">@zou3519</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jerryzh168/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jerryzh168">@jerryzh168</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yf225/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yf225">@yf225</a></p>
<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">If a Linear or Conv module is constructed without bias, a subsequent move to another device fails</p>
<h2 dir="auto">To Reproduce</h2>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#include &lt;torch/torch.h&gt;

int main(int argc, char** argv) {
  torch::nn::Linear test(torch::nn::LinearOptions(10,20).bias(false));
  test-&gt;to(torch::kCUDA);
  return 0;
}"><pre class="notranslate">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>torch/torch.h<span class="pl-pds">&gt;</span></span>

<span class="pl-k">int</span> <span class="pl-en">main</span>(<span class="pl-k">int</span> argc, <span class="pl-k">char</span>** argv) {
  torch::nn::Linear <span class="pl-smi">test</span>(<span class="pl-c1">torch::nn::LinearOptions</span>(<span class="pl-c1">10</span>,<span class="pl-c1">20</span>).<span class="pl-c1">bias</span>(<span class="pl-c1">false</span>));
  test-&gt;<span class="pl-c1">to</span>(torch::<span class="pl-c1">kCUDA</span>);
  <span class="pl-k">return</span> <span class="pl-c1">0</span>;
}</pre></div>
<details><summary>Stack trace</summary>
<p dir="auto">
</p><div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="terminate called after throwing an instance of 'c10::Error'
  what():  tensor does not have a device (device at /opt/pytorch/c10/core/TensorImpl.h:463)
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) + 0x6a (0x7fffe51c9b1a in /usr/local/torch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0x1fd83a6 (0x7fffe73ba3a6 in /usr/local/torch/lib/libtorch.so)
frame #2: at::native::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) + 0xb36 (0x7fffe8c6b1f6 in /usr/local/torch/lib/libtorch.so)
frame #3: &lt;unknown function&gt; + 0x3c22636 (0x7fffe9004636 in /usr/local/torch/lib/libtorch.so)
frame #4: &lt;unknown function&gt; + 0x5882857 (0x7fffeac64857 in /usr/local/torch/lib/libtorch.so)
frame #5: &lt;unknown function&gt; + 0x3c89f2f (0x7fffe906bf2f in /usr/local/torch/lib/libtorch.so)
frame #6: &lt;unknown function&gt; + 0x25e1277 (0x7fffe79c3277 in /usr/local/torch/lib/libtorch.so)
frame #7: void torch::nn::Module::to_impl&lt;c10::Device&amp;, bool&amp;&gt;(c10::Device&amp;, bool&amp;) + 0x196 (0x7fffeb3a8266 in /usr/local/torch/lib/libtorch.so)
frame #8: torch::nn::Module::to(c10::Device, bool) + 0x18 (0x7fffeb3a2fe8 in /usr/local/torch/lib/libtorch.so)
frame #9: main + 0xba (0x4052e8 in /home/tobi/coar_ws/devel/lib/dgcnn/dev_stuff)
frame #10: __libc_start_main + 0xf0 (0x7fffe4855830 in /lib/x86_64-linux-gnu/libc.so.6)
frame #11: _start + 0x29 (0x405049 in /home/tobi/coar_ws/devel/lib/dgcnn/dev_stuff)


Thread 1 &quot;dev_stuff&quot; received signal SIGABRT, Aborted.
0x00007fffe486a428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) backtrace
#0  0x00007fffe486a428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007fffe486c02a in __GI_abort () at abort.c:89
#2  0x00007fffe4ea484d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007fffe4ea26b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007fffe4ea2701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007fffe4ea2919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007fffe73ba3fc in at::Tensor::options() const () from /usr/local/torch/lib/libtorch.so
#7  0x00007fffe8c6b1f6 in at::native::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#8  0x00007fffe9004636 in at::TypeDefault::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#9  0x00007fffeac64857 in torch::autograd::VariableType::(anonymous namespace)::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#10 0x00007fffe906bf2f in c10::detail::wrap_kernel_functor_unboxed_&lt;c10::detail::WrapRuntimeKernelFunctor_&lt;at::Tensor (*)(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;), at::Tensor, c10::guts::typelist::typelist&lt;at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt; &gt; &gt;, at::Tensor (at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;)&gt;::call(c10::OperatorKernel*, at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#11 0x00007fffe79c3277 in at::Tensor::to(c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) const () from /usr/local/torch/lib/libtorch.so
#12 0x00007fffeb3a8266 in void torch::nn::Module::to_impl&lt;c10::Device&amp;, bool&amp;&gt;(c10::Device&amp;, bool&amp;) () from /usr/local/torch/lib/libtorch.so
#13 0x00007fffeb3a2fe8 in torch::nn::Module::to(c10::Device, bool) [clone .localalias.489] () from /usr/local/torch/lib/libtorch.so
#14 0x00000000004052e8 in main (argc=1, argv=0x7fffffffccc8) at /home/tobi/coar_ws/src/dgcnn/src/dev_stuff.cpp:5"><pre class="notranslate"><code class="notranslate">terminate called after throwing an instance of 'c10::Error'
  what():  tensor does not have a device (device at /opt/pytorch/c10/core/TensorImpl.h:463)
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) + 0x6a (0x7fffe51c9b1a in /usr/local/torch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0x1fd83a6 (0x7fffe73ba3a6 in /usr/local/torch/lib/libtorch.so)
frame #2: at::native::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) + 0xb36 (0x7fffe8c6b1f6 in /usr/local/torch/lib/libtorch.so)
frame #3: &lt;unknown function&gt; + 0x3c22636 (0x7fffe9004636 in /usr/local/torch/lib/libtorch.so)
frame #4: &lt;unknown function&gt; + 0x5882857 (0x7fffeac64857 in /usr/local/torch/lib/libtorch.so)
frame #5: &lt;unknown function&gt; + 0x3c89f2f (0x7fffe906bf2f in /usr/local/torch/lib/libtorch.so)
frame #6: &lt;unknown function&gt; + 0x25e1277 (0x7fffe79c3277 in /usr/local/torch/lib/libtorch.so)
frame #7: void torch::nn::Module::to_impl&lt;c10::Device&amp;, bool&amp;&gt;(c10::Device&amp;, bool&amp;) + 0x196 (0x7fffeb3a8266 in /usr/local/torch/lib/libtorch.so)
frame #8: torch::nn::Module::to(c10::Device, bool) + 0x18 (0x7fffeb3a2fe8 in /usr/local/torch/lib/libtorch.so)
frame #9: main + 0xba (0x4052e8 in /home/tobi/coar_ws/devel/lib/dgcnn/dev_stuff)
frame #10: __libc_start_main + 0xf0 (0x7fffe4855830 in /lib/x86_64-linux-gnu/libc.so.6)
frame #11: _start + 0x29 (0x405049 in /home/tobi/coar_ws/devel/lib/dgcnn/dev_stuff)


Thread 1 "dev_stuff" received signal SIGABRT, Aborted.
0x00007fffe486a428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) backtrace
#0  0x00007fffe486a428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007fffe486c02a in __GI_abort () at abort.c:89
#2  0x00007fffe4ea484d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007fffe4ea26b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007fffe4ea2701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007fffe4ea2919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007fffe73ba3fc in at::Tensor::options() const () from /usr/local/torch/lib/libtorch.so
#7  0x00007fffe8c6b1f6 in at::native::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#8  0x00007fffe9004636 in at::TypeDefault::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#9  0x00007fffeac64857 in torch::autograd::VariableType::(anonymous namespace)::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#10 0x00007fffe906bf2f in c10::detail::wrap_kernel_functor_unboxed_&lt;c10::detail::WrapRuntimeKernelFunctor_&lt;at::Tensor (*)(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;), at::Tensor, c10::guts::typelist::typelist&lt;at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt; &gt; &gt;, at::Tensor (at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;)&gt;::call(c10::OperatorKernel*, at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) () from /usr/local/torch/lib/libtorch.so
#11 0x00007fffe79c3277 in at::Tensor::to(c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) const () from /usr/local/torch/lib/libtorch.so
#12 0x00007fffeb3a8266 in void torch::nn::Module::to_impl&lt;c10::Device&amp;, bool&amp;&gt;(c10::Device&amp;, bool&amp;) () from /usr/local/torch/lib/libtorch.so
#13 0x00007fffeb3a2fe8 in torch::nn::Module::to(c10::Device, bool) [clone .localalias.489] () from /usr/local/torch/lib/libtorch.so
#14 0x00000000004052e8 in main (argc=1, argv=0x7fffffffccc8) at /home/tobi/coar_ws/src/dgcnn/src/dev_stuff.cpp:5
</code></pre></div>
<p dir="auto"></p>
</details>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">Getting no error.</p>
<h2 dir="auto">Environment</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Collecting environment information...
Collecting environment information...
PyTorch version: 1.4.0a0+829499e
Is debug build: No
CUDA used to build PyTorch: 10.1

OS: Ubuntu 18.04.3 LTS
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
CMake version: version 3.15.3

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration:
GPU 0: Quadro RTX 6000
GPU 1: Quadro RTX 6000

Nvidia driver version: 418.87.00
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5

Versions of relevant libraries:
[pip3] numpy==1.17.4
[pip3] torch==1.4.0a0+94016b1
[conda] Could not collect
"><pre class="notranslate"><code class="notranslate">Collecting environment information...
Collecting environment information...
PyTorch version: 1.4.0a0+829499e
Is debug build: No
CUDA used to build PyTorch: 10.1

OS: Ubuntu 18.04.3 LTS
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
CMake version: version 3.15.3

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration:
GPU 0: Quadro RTX 6000
GPU 1: Quadro RTX 6000

Nvidia driver version: 418.87.00
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5

Versions of relevant libraries:
[pip3] numpy==1.17.4
[pip3] torch==1.4.0a0+94016b1
[conda] Could not collect

</code></pre></div>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0): 1.4.0a0+829499e</li>
<li>OS (e.g., Linux): Ubuntu 18.04.3 LTS</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source): compiled from source</li>
<li>Build command you used (if compiling from source): <code class="notranslate">CFLAGS=' -D_GLICXX_USE_CXX11_ABI ' USE_OPENCV=1 USE_CUDA=1 MAX_JOBS=7 BUILD_TEST=0 python3 setup.py install </code></li>
<li>Python version: 3.6</li>
<li>CUDA/cuDNN version: CUDA 10.1 / cuDNN 7.6.5</li>
<li>GPU models and configuration: 2x Quadro RTX 6000</li>
</ul>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ezyang/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ezyang">@ezyang</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gchanan/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gchanan">@gchanan</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/zou3519/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/zou3519">@zou3519</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jerryzh168/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jerryzh168">@jerryzh168</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yf225/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yf225">@yf225</a></p>