forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gdb special command to print tensors (pytorch#54339)
Summary: This is something which I wrote because it was useful during my debugging sessions, but I think it might be generally useful to other people as well so I took the liberty of proposing an official `pytorch-gdb` extension. `pytorch-gdb` is a gdb script written in python. Currently, it contains only one command: `torch-tensor-repr`, which prints a human-readable repr of an `at::Tensor` object. Example: ``` Breakpoint 1, at::native::neg (self=...) at [...]/pytorch/aten/src/ATen/native/UnaryOps.cpp:520 520 Tensor neg(const Tensor& self) { return unary_op_impl(self, at::neg_out); } (gdb) # the default repr of 'self' is not very useful (gdb) p self $1 = (const at::Tensor &) 0x7ffff72ed780: {impl_ = {target_ = 0x5555559df6e0}} (gdb) torch-tensor-repr self Python-level repr of self: tensor([1., 2., 3., 4.], dtype=torch.float64) ``` The idea is that by having an official place where to put these things, `pytorch-gdb` will slowly grow other useful features and make the pytorch debugging experience nicer and faster. Pull Request resolved: pytorch#54339 Reviewed By: bdhirsh Differential Revision: D27253674 Pulled By: ezyang fbshipit-source-id: dba219e126cc2fe66b2d26740f3a8e3b886e56f5
- Loading branch information
1 parent
583c4bf
commit 21a9a93
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# automatically load the pytoch-gdb extension. | ||
# | ||
# gdb automatically tries to load this file whenever it is executed from the | ||
# root of the pytorch repo, but by default it is not allowed to do so due to | ||
# security reasons. If you want to use pytorch-gdb, please add the following | ||
# line to your ~/.gdbinit (i.e., the .gdbinit file which is in your home | ||
# directory, NOT this file): | ||
# add-auto-load-safe-path /path/to/pytorch/.gdbinit | ||
# | ||
# Alternatively, you can manually load the pytorch-gdb commands into your | ||
# existing gdb session by doing the following: | ||
# (gdb) source /path/to/pytorch/tools/gdb/pytorch-gdb.py | ||
|
||
source tools/gdb/pytorch-gdb.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import gdb | ||
import textwrap | ||
|
||
class DisableBreakpoints: | ||
""" | ||
Context-manager to temporarily disable all gdb breakpoints, useful if | ||
there is a risk to hit one during the evaluation of one of our custom | ||
commands | ||
""" | ||
|
||
def __enter__(self): | ||
self.disabled_breakpoints = [] | ||
for b in gdb.breakpoints(): | ||
if b.enabled: | ||
b.enabled = False | ||
self.disabled_breakpoints.append(b) | ||
|
||
def __exit__(self, etype, evalue, tb): | ||
for b in self.disabled_breakpoints: | ||
b.enabled = True | ||
|
||
class TensorRepr(gdb.Command): | ||
""" | ||
Print a human readable representation of the given at::Tensor. | ||
Usage: torch-tensor-repr EXP | ||
at::Tensor instances do not have a C++ implementation of a repr method: in | ||
pytoch, this is done by pure-Python code. As such, torch-tensor-repr | ||
internally creates a Python wrapper for the given tensor and call repr() | ||
on it. | ||
""" | ||
__doc__ = textwrap.dedent(__doc__).strip() | ||
|
||
def __init__(self): | ||
gdb.Command.__init__(self, 'torch-tensor-repr', | ||
gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION) | ||
|
||
def invoke(self, args, from_tty): | ||
args = gdb.string_to_argv(args) | ||
if len(args) != 1: | ||
print('Usage: torch-tensor-repr EXP') | ||
return | ||
name = args[0] | ||
with DisableBreakpoints(): | ||
res = gdb.parse_and_eval('torch::gdb::tensor_repr(%s)' % name) | ||
print('Python-level repr of %s:' % name) | ||
print(res.string()) | ||
# torch::gdb::tensor_repr returns a malloc()ed buffer, let's free it | ||
gdb.parse_and_eval('(void)free(%s)' % int(res)) | ||
|
||
TensorRepr() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters