Debug error with torch::where() in libtorch

username: Jacky
cooked: <p>Hello everyone,<br>
I am trying to port a python module into c++ and there is a problem with usage of torch::where(). I couldn’t find any related documentation about it except the head file ATen/ops/where.h.</p>
<pre><code class="lang-auto">// aten::where.ScalarSelf(Tensor condition, Scalar self, Tensor other) -&gt; Tensor
TORCH_API inline at::Tensor where(const at::Tensor &amp; condition, const at::Scalar &amp; self, const at::Tensor &amp; other) {
    return at::_ops::where_ScalarSelf::call(condition, self, other);
}
</code></pre>
<p>Here is the simple demo :</p>
<pre><code class="lang-auto">#include &lt;iostream&gt;
#include &lt;torch/torch.h&gt;
#include &lt;torch/script.h&gt;

int main()
{
	torch::Tensor a = torch::randn({ 3, 3 });
	std::cout &lt;&lt; a &lt;&lt; std::endl;	

	torch::Tensor b = torch::where(a &lt; 0.0f, 0.0f, a); // debug error. abort() has been called.
	std::cout &lt;&lt; b &lt;&lt; std::endl;

	return 0;
}
</code></pre>
<p>Environment as follows:<br>
libtorch 1.12.1+cuda11.3, vs2022.</p>
<p>Any help is greatly appreciated.</p>

username: ptrblck
cooked: <p>Your code works fine for me:</p>
<pre><code class="lang-auto">/example-app 
 0.4171  0.7544  0.1796
-0.9740 -1.1933  2.6715
-1.3155 -1.3456 -0.2083
[ CPUFloatType{3,3} ]
 0.4171  0.7544  0.1796
 0.0000  0.0000  2.6715
 0.0000  0.0000  0.0000
[ CPUFloatType{3,3} ]
</code></pre>
<p>but I’m also using Linux and don’t know if this error might be Windows-specific.</p>

username: abloom5448
cooked: <p>I had the same problem. Try to release it</p>

username: Jacky
cooked: <p>Thanks for your reply. It indeed works in release mode. Quite weird.</p>

username: Jacky
cooked: <p>Thanks for your reply. The debug error still exist. But as <span class="mention">@Myeong</span> Hyun Lee mentioned, the code indeed works fine in release mode.</p>

username: ptrblck
cooked: <p>It’s also working in DEBUG mode for me.</p>

username: BongHoe_Koo
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
In my case, it doesn’t work. (torch::stack, torch::where) in debug mode<br>
could you share your environment information? like linker version?</p>
<p>my environment<br>
compiler : vs2022<br>
linker : v143</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/f/aff9003152bced15bcc57190bc0a70f10ef92cbd.png" data-download-href="https://discuss.pytorch.org/uploads/default/aff9003152bced15bcc57190bc0a70f10ef92cbd" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/f/aff9003152bced15bcc57190bc0a70f10ef92cbd.png" alt="image" data-base62-sha1="p6J2pQbp0jENWsNKRzqLozRG5LD" width="690" height="220" data-dominant-color="6B6C6C"><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">image</span><span class="informations">959×307 19 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>

username: ptrblck
cooked: <p>Sure:</p>
<pre><code class="lang-auto">$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ ld --version
GNU ld (GNU Binutils for Ubuntu) 2.34
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
</code></pre>