[LibTorch] How to print the content of a tensor in gdb?

Hey guys,

I’m relatively new to C++ and LibTorch, but I would like to know how to inspect a tensor content in the debugger, since my workflow involves a lot of usage of the debugger.

In gdb I’m trying to do:

print x

$8 = {impl_ = {target_ = 0x14dfa20}}

print x.data()

Couldn't find method at::Tensor::data

I know torch::Tensor is a pointer, but I just can’t figure out how to dereference it



You could try something like the following:

With some modifications it could be used for visualizing tensors.

how do you import that into gdb?

You can use some p *x[0].data<float>() if it is a single dimension tensor, or p *x.data<float>() if it is a no-dim tensor, and so on.


1 Like

it is a 4 dimensional tensor, but I would like to know how to do it for a tensor of arbitrary size.

Basically what I’m looking for is an output similar to

std::cout << x << '\n';

if I have to implement a python extension, so be it… but I still don’t understand how to do it.

I think you can see the content using cout in either run or debug consoles. Since you asked about gdb, I mentioned that approach. If you want to use gdb, you need to add more dimensions, e.g. p *x[i][j][k][l].data<float>().

1 Like

that is not working for me

it either says Could not find operator[]. or No symbol "operator*" in current context.

I also couldn’t use cout directly in gdb, so far.

I’m new to C++, so I might be doing something wrong.

you cannot use cout in gdb, you need to hard code it. The error of Could not find operator[] is your gdb error. Probably it does not mach torch.

Can you try print x.toString()?

That prints a memory address and the type of tensor

print at::print(std::cout, x, 99), 99 being the line width.

Best regards


1 Like

I’m getting No symbol "print" in namespace "at".

I must be missing something…

It’s defined in ATen/core/Formatting.h. As far as I can see it should be there for 1.0, 1.0.1, and master. You could try using it in your code to see what’s wrong.

Best regards


1 Like

i can use it while running the script, just not while attached to gdb

Strange. It worked for me the other day. While the version without the ostream might not be available due to the inlining, I would have expected the one with to be exported at the least.

Have you found a good solution?
In pytorch_1.3, try:

p self.impl_->sizes() 
# such as 4-dims
p self.impl_->sizes().Data[0]
p self.impl_->sizes().Data[3]

to inspect data size, and

p self.impl_->storage().storage_impl_->numel()   # num of elements
p  ((float *)self.impl_->storage().storage_impl_->data())[0]  # the first element

to inspect value of tensor.

As of 1.5.0, Tensor.print() seems to work to print type and shape info.

gdb) call t.print()
[CPUByteType [1, 10, 1, 10]]

To print full contents, it seems the form std::cout << Tensor << std::endl must be present in debugged C++ file first. But you also have to call fflush(0) since gdb has trouble with std::endl in my test case.

A .gdbinit quick solution:

define ptensor
  call std::cout << $arg0
  call fflush(0)
(gdb) ptensor tensor
(1,1,.,.) =
  0  0  1  3  4  4  3  1  0  0

$4 = (std::ostream &) @0x55555597e740: <incomplete type>
[ CPUByteType{1,10,1,10} ]$5 = 0

But this prints the WHOLE tensor so only a partial solution.

1 Like

got it working in LLDB … but it has to be similar. Are you still interested?
Best regards

I’d love to know how to view a tensor in CLION’s debugger. I have not been able to get any of these solutions to work. I would love to be able to see the contents of the tensor while inspecting or evaluating an expression like I am able to see a vector.
How were you able to get it working in LLDB?

Maybe try: