Debug error with torch::where() in libtorch

Hello everyone,
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.

// aten::where.ScalarSelf(Tensor condition, Scalar self, Tensor other) -> Tensor
TORCH_API inline at::Tensor where(const at::Tensor & condition, const at::Scalar & self, const at::Tensor & other) {
    return at::_ops::where_ScalarSelf::call(condition, self, other);
}

Here is the simple demo :

#include <iostream>
#include <torch/torch.h>
#include <torch/script.h>

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

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

	return 0;
}

Environment as follows:
libtorch 1.12.1+cuda11.3, vs2022.

Any help is greatly appreciated.

Your code works fine for me:

/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} ]

but I’m also using Linux and don’t know if this error might be Windows-specific.

I had the same problem. Try to release it

Thanks for your reply. It indeed works in release mode. Quite weird.

Thanks for your reply. The debug error still exist. But as @Myeong Hyun Lee mentioned, the code indeed works fine in release mode.

It’s also working in DEBUG mode for me.

@ptrblck
In my case, it doesn’t work. (torch::stack, torch::where) in debug mode
could you share your environment information? like linker version?

my environment
compiler : vs2022
linker : v143

Sure:

$ 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.