4
import gdb # type: ignore[import]
7
class DisableBreakpoints:
9
Context-manager to temporarily disable all gdb breakpoints, useful if
10
there is a risk to hit one during the evaluation of one of our custom
14
def __enter__(self) -> None:
15
self.disabled_breakpoints = []
16
for b in gdb.breakpoints():
19
self.disabled_breakpoints.append(b)
21
def __exit__(self, etype: Any, evalue: Any, tb: Any) -> None:
22
for b in self.disabled_breakpoints:
26
class TensorRepr(gdb.Command): # type: ignore[misc, no-any-unimported]
28
Print a human readable representation of the given at::Tensor.
29
Usage: torch-tensor-repr EXP
31
at::Tensor instances do not have a C++ implementation of a repr method: in
32
pytorch, this is done by pure-Python code. As such, torch-tensor-repr
33
internally creates a Python wrapper for the given tensor and call repr()
37
__doc__ = textwrap.dedent(__doc__).strip()
39
def __init__(self) -> None:
41
self, "torch-tensor-repr", gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION
44
def invoke(self, args: str, from_tty: bool) -> None:
45
args = gdb.string_to_argv(args)
47
print("Usage: torch-tensor-repr EXP")
50
with DisableBreakpoints():
51
res = gdb.parse_and_eval(f"torch::gdb::tensor_repr({name})")
52
print(f"Python-level repr of {name}:")
54
# torch::gdb::tensor_repr returns a malloc()ed buffer, let's free it
55
gdb.parse_and_eval(f"(void)free({int(res)})")