Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gdb special command to print tensors #54339

Closed
wants to merge 3 commits into from

Conversation

Copy link
Contributor

@antocuni antocuni commented Mar 19, 2021

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.

@facebook-github-bot
Copy link
Contributor

@facebook-github-bot facebook-github-bot commented Mar 19, 2021

💊 CI failures summary and remediations

As of commit 4c217dd (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

@heitorschueroff heitorschueroff requested review from ezyang and gchanan Mar 22, 2021
@heitorschueroff heitorschueroff added the triaged label Mar 22, 2021
PyGILState_Release(gil);
return result;

error:
Copy link
Contributor

@ezyang ezyang Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not using RAII here is kind of weird. Is there a particular reason, e.g., gdb doesn't like it when you have stack unwinding?

Copy link
Contributor Author

@antocuni antocuni Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the most honest answer is that I am used to write Python API code in C and I didn't think that this was C++ 🤦‍♂️

But also, I am not aware of any standard smart-pointer-like class to manage PyObject* in C++ with RAII. Do we have any helper for that?

Or you were suggesting do write a custom class just to handle this specific function?

@facebook-github-bot
Copy link
Contributor

@facebook-github-bot facebook-github-bot commented Mar 23, 2021

@ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@facebook-github-bot facebook-github-bot commented Mar 23, 2021

@ezyang merged this pull request in 21a9a93.

@samestep samestep mentioned this pull request Mar 23, 2021
facebook-github-bot pushed a commit that referenced this issue Mar 23, 2021
Summary:
#54339 broke Flake8. This PR fixes it.

Pull Request resolved: #54540

Test Plan:
```
flake8
```

Reviewed By: walterddr

Differential Revision: D27274171

Pulled By: samestep

fbshipit-source-id: 4b440d72b4b5615f45e6fcb25f7a4c0423add272
@wwwwcu
Copy link

@wwwwcu wwwwcu commented Jul 2, 2021

I saw the tool (pytorhc-gdb), so I want to have a try.
Following the guide, I can't get the value of at::tensor since my code walk into libtorch_cpu.so instead of UnaryOps.cpp:520

gdb python
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
[...]
(gdb) break at::native::negative
Function "at::native::negative" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (at::native::negative) pending.
(gdb) run
Starting program: /root/anaconda3/envs/pytorch/bin/python 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> t = torch.tensor([1, 2, 3, 4], dtype=torch.float64)
>>> t.negative()

Breakpoint 1, 0x00007fffe32c5f20 in at::native::negative(at::Tensor const&)@plt () from /root/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/lib/libtorch_cpu.so

I built pytorch from source with "DEBUG=1", I'm not sure if I use the tool correctly.

@antocuni
Copy link
Contributor Author

@antocuni antocuni commented Jul 2, 2021

I saw the tool (pytorhc-gdb), so I want to have a try.
Following the guide, I can't get the value of at::tensor since my code walk into libtorch_cpu.so instead of UnaryOps.cpp:520

you are correct, the example was out of date. I fixed it in #61175

facebook-github-bot pushed a commit that referenced this issue Jul 12, 2021
…1175)

Summary:
As pointed out by #54339 (comment), the `pytorch-gdb` example is currently broken because the code has been refactored.

This PR updates the example so that it works again.

Pull Request resolved: #61175

Reviewed By: heitorschueroff

Differential Revision: D29660336

Pulled By: ezyang

fbshipit-source-id: 8bcd32fc583c0b28a705ef37203ce7ad4d636732
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed Merged open source triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants