mirror of https://github.com/open-mmlab/mmcv.git
upload file
parent
3054b18de2
commit
a0c37412e3
|
@ -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
|
|
@ -0,0 +1,55 @@
|
||||||
|
import textwrap
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import gdb # type: ignore[import]
|
||||||
|
|
||||||
|
|
||||||
|
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) -> None:
|
||||||
|
self.disabled_breakpoints = []
|
||||||
|
for b in gdb.breakpoints():
|
||||||
|
if b.enabled:
|
||||||
|
b.enabled = False
|
||||||
|
self.disabled_breakpoints.append(b)
|
||||||
|
|
||||||
|
def __exit__(self, etype: Any, evalue: Any, tb: Any) -> None:
|
||||||
|
for b in self.disabled_breakpoints:
|
||||||
|
b.enabled = True
|
||||||
|
|
||||||
|
|
||||||
|
class TensorRepr(gdb.Command): # type: ignore[misc, no-any-unimported]
|
||||||
|
"""
|
||||||
|
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) -> None:
|
||||||
|
gdb.Command.__init__(self, 'torch-tensor-repr', gdb.COMMAND_USER,
|
||||||
|
gdb.COMPLETE_EXPRESSION)
|
||||||
|
|
||||||
|
def invoke(self, args: str, from_tty: bool) -> None:
|
||||||
|
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
|
||||||
|
gdb.parse_and_eval('(void)free(%s)' % int(res))
|
||||||
|
|
||||||
|
|
||||||
|
TensorRepr()
|
Loading…
Reference in New Issue