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

Is there a way to print tensor while debugging? #1286

Open
mokeyish opened this issue Nov 7, 2023 · 9 comments
Open

Is there a way to print tensor while debugging? #1286

mokeyish opened this issue Nov 7, 2023 · 9 comments

Comments

@mokeyish
Copy link
Contributor

mokeyish commented Nov 7, 2023

I need to see tensor values and shape.

Hardcoding println is inconvenient.

This seems to be less convenient than python.

I wonder if there is a better way.

@EricLBuehler
Copy link
Member

Perhaps you can comment to #1279? I have noticed that there appears no way to modify or access specific elements.

@LLukas22
Copy link
Contributor

LLukas22 commented Nov 7, 2023

I think what you are refering to are debugger visualizers, which allow to customize how the WinDbg or GDB / LLDB debuggers display certain structs.

This would allow us to customize the developer experience and e.g. display the values, type and shape of a tensor easily in the debugger.

@EricLBuehler
Copy link
Member

EricLBuehler commented Nov 7, 2023

Currently, there is no API to get the data of an n-dimensional tensor.

@LaurentMazare
Copy link
Collaborator

Currently, there is no API to get the data of a tensor.

What do you mean by this? The to_vecX() methods are here to retrieve the data from a tensor.

For the original question, we currently implement the Display trait to print a tensor in the same way pytorch would. We also have the Debug trait that provides a concise description with only the shape/element type.
Not sure what the debugger is using but it might be as simple as tweaking one of these traits or implementing a new one, I guess it depends on how the debugger is extracting the data so might be debugger dependent.

@LLukas22
Copy link
Contributor

LLukas22 commented Nov 7, 2023

For the original question, we currently implement the Display trait to print a tensor in the same way pytorch would. We also have the Debug trait that provides a concise description with only the shape/element type.
Not sure what the debugger is using but it might be as simple as tweaking one of these traits or implementing a new one, I guess it depends on how the debugger is extracting the data so might be debugger dependent.

I think the problem here is, that CodeLLDB (at least on windows) wont use the Debug or Display trait correctly and displays the "raw" tensor, which can be a bit hard to reason about if you don't know the internal structure.

BadDebuggingDisplay.mp4

But it should be relatively easy to implement some debugger visualizers as we already have working Debug and Display traits.

@EricLBuehler
Copy link
Member

What do you mean by this? The to_vecX() methods are here to retrieve the data from a tensor.

@LaurentMazare, sorry for the lack of clarity - I meant there is no way (to my knowledge) to get n-dimensional data out of a tensor without checking the rank. As another question: is there an API to modify tensor elements already - perhaps to return an &mut Vec<...>?

@LaurentMazare
Copy link
Collaborator

I think the problem here is, that CodeLLDB (at least on windows) wont use the Debug or Display trait correctly and displays the "raw" tensor, which can be a bit hard to reason about if you don't know the internal structure.

Sorry I'm a bit lazy here and haven't google but would you know if codelldb/... provide some hooks that let you customize the printing of arbitrary data structures. I imagine that it's likely to be the case.

@LLukas22
Copy link
Contributor

LLukas22 commented Nov 7, 2023

Sorry I'm a bit lazy here and haven't google but would you know if codelldb/... provide some hooks that let you customize the printing of arbitrary data structures. I imagine that it's likely to be the case.

I believe this should do the trick: https://rust-lang.github.io/rfcs/3191-debugger-visualizer.html#supporting-pretty-printers

@nickhs
Copy link

nickhs commented Mar 11, 2024

Took a stab at this in #1836

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants