Files
pytorch/tools/gdb/pytorch-gdb.py
Justin Chu 4cc1745b13 [BE] f-stringify torch/ and scripts (#105538)
This PR is a follow up on the pyupgrade series to convert more strings to use f-strings using `flynt`.

- https://docs.python.org/3/reference/lexical_analysis.html#f-strings
- https://pypi.org/project/flynt/

Command used:

```
flynt torch/ -ll 120
flynt scripts/ -ll 120
flynt tools/ -ll 120
```

and excluded `collect_env.py`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/105538
Approved by: https://github.com/ezyang, https://github.com/malfet
2023-07-21 19:35:24 +00:00

59 lines
1.8 KiB
Python

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
pytorch, 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(f"torch::gdb::tensor_repr({name})")
print(f"Python-level repr of {name}:")
print(res.string())
# torch::gdb::tensor_repr returns a malloc()ed buffer, let's free it
gdb.parse_and_eval(f"(void)free({int(res)})")
TensorRepr()