IntList loses initial values


(Martin Huber) #1

Hey, here is some super strange behaviour for at::IntList that I do not understand. I try to initialize a at::IntList of class A from class B.

Main

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

class A {

public:

	A(torch::IntList list) : list_(list) { };

	void Print() { std::cout << list_ << std::endl; };

private:

	torch::IntList list_;
};


class B {

public:

	B(int64_t c, int64_t d) : a_({c, d}) { a_.Print(); };

	void Print() { a_.Print(); };

private:

	A a_;
};


int main() {

	B b(10, 10);

	b.Print();
	
	return 0;
}

The output is

[10, 10]
[140726518609664, 4206890]

CMake

cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

find_package(Torch REQUIRED)

include_directories(${Torch_INCLUDE_DIRS})

add_executable(main main.cpp)
target_link_libraries(main ${TORCH_LIBRARIES})

Is it a mistake or a bug? This thing blows up my whole code as libtorch trys to allocate tons of GB of RAM due to that.


(Martin Huber) #2

ok, if you switch from at::IntList, which is a typedef for at::ArrayRef<int64_t> to at::ArrayRef<int>, everything works fine. at::IntList seems to be depracted or something :man_shrugging:

But this doesn’t solve the issue, as every tensor is initialized with at::IntLists

#16824


#3

IntList is a reference to an array. You’re not keeping the original array alive, as far as I can tell, so the IntList becomes a reference to some garbage memory.