Gdb debugging access index({}) or size()

Hi,

I was playing with libtorch. I’ve configured and built with cmake a really simple example.

cppFrontEnd.cpp

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

int main() {
  torch::Tensor tensor1 = torch::ones(5);
  std::cout << tensor1 << std::endl;
  tensor1.index_put_({2},5);
  std::cout << tensor1 << std::endl;
  std::cout<< tensor1.index({2});
  // std::cout<<tensor1.size(0)<<std::endl;


  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(cppFrontEnd)
set(CMAKE_PREFIX_PATH "/path/to/cppFrontEnd/libtorch")
set(CMAKE_BUILD_TYPE "Debug")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -O0")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${TORCH_CXX_FLAGS_DEBUG} -O0")
add_executable(cppFrontEnd cppFrontEnd.cpp)
target_link_libraries(cppFrontEnd "${TORCH_LIBRARIES}")
set_property(TARGET cppFrontEnd PROPERTY CXX_STANDARD 14)

Then with gdb when I tried to access one element of the tensor
with
p tensor1.index({1})
I got
Cannot evaluate function -- may be inlined

and with

p tensor1.size(0)
Cannot resolve method at::Tensor::size to any overloaded instance

Otherwise
p tensor.sizes()[0] works if I uncomment the line std::cout<<tensor1.size(0)<<std::endl;

I assume the inline problem is generated by the compiler as if you don’t use or explicitly instantiate some template method it is not generated in resulting binary. then gdb cannot see it and that is why sizes()[] start working when I call tensor1.size() in the source code, am I right?

Is there a way to get a complete debug of the methods and attributes of the objects define in libtorch? Sorry if this is a basic question or if this is not the place to do it.

Thank you

You could try to build your application with debug symbols via cmake --build . --config Debug.
I haven’t checked this approach and would guess that you would also need a debug libtorch build.

Thanks! I tried to build with the exact same command you provided but the lack of debug info still happens I assume it is due to the lack of debug symbols in libtorch but I have not try this yet.