candle icon indicating copy to clipboard operation
candle copied to clipboard

Is there a way to print tensor while debugging?

Open mokeyish opened this issue 2 years ago • 9 comments
trafficstars

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.

mokeyish avatar Nov 07 '23 01:11 mokeyish

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

EricLBuehler avatar Nov 07 '23 10:11 EricLBuehler

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.

LLukas22 avatar Nov 07 '23 12:11 LLukas22

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

EricLBuehler avatar Nov 07 '23 12:11 EricLBuehler

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.

LaurentMazare avatar Nov 07 '23 12:11 LaurentMazare

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.

https://github.com/huggingface/candle/assets/65088241/7433878d-9b94-422a-a0ce-9790ac554899

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

LLukas22 avatar Nov 07 '23 13:11 LLukas22

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<...>?

EricLBuehler avatar Nov 07 '23 13:11 EricLBuehler

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.

LaurentMazare avatar Nov 07 '23 14:11 LaurentMazare

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

LLukas22 avatar Nov 07 '23 14:11 LLukas22

Took a stab at this in https://github.com/huggingface/candle/pull/1836

nickhs avatar Mar 11 '24 19:03 nickhs