miguelvr
(Miguel Varela Ramos)
March 12, 2019, 10:00am
#1
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
Cheers,
Miguel
3 Likes
dtmoodie
(Dtmoodie)
March 12, 2019, 3:14pm
#2
You could try something like the following:
# Copyright (c) 2012, Renato Florentino Garcia <fgarcia.renato@gmail.com>
# Stefano Pellegrini <stefpell@ee.ethz.ch>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the authors nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
This file has been truncated. show original
With some modifications it could be used for visualizing tensors.
miguelvr
(Miguel Varela Ramos)
March 12, 2019, 3:27pm
#3
how do you import that into gdb?
afshin67
(Afshin Oroojlooy)
March 12, 2019, 4:02pm
#4
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.
Afshin
1 Like
miguelvr
(Miguel Varela Ramos)
March 12, 2019, 4:11pm
#5
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.
afshin67
(Afshin Oroojlooy)
March 12, 2019, 4:23pm
#6
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
miguelvr
(Miguel Varela Ramos)
March 12, 2019, 4:34pm
#7
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.
afshin67
(Afshin Oroojlooy)
March 12, 2019, 4:53pm
#8
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
.
yf225
(PyTorch Dev, Facebook AI Research)
March 20, 2019, 4:39pm
#9
Can you try print x.toString()
?
miguelvr
(Miguel Varela Ramos)
March 20, 2019, 8:22pm
#10
That prints a memory address and the type of tensor
tom
(Thomas V)
March 20, 2019, 9:45pm
#11
print at::print(std::cout, x, 99)
, 99 being the line width.
Best regards
Thomas
1 Like
miguelvr
(Miguel Varela Ramos)
March 25, 2019, 10:55am
#12
I’m getting No symbol "print" in namespace "at".
I must be missing something…
tom
(Thomas V)
March 25, 2019, 11:25am
#13
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
Thomas
1 Like
miguelvr
(Miguel Varela Ramos)
March 25, 2019, 2:28pm
#14
i can use it while running the script, just not while attached to gdb
tom
(Thomas V)
March 25, 2019, 3:06pm
#15
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.
Y.E_Zhou
(Y.E. Zhou)
April 23, 2020, 2:21pm
#16
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.
tshadley
(Tedd Hadley)
June 15, 2020, 8:48pm
#17
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)
end
(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
isai_roman
(Isai Roman Ballesteros)
March 21, 2021, 8:41pm
#18
Hi,
got it working in LLDB … but it has to be similar. Are you still interested?
Best regards