-
Notifications
You must be signed in to change notification settings - Fork 147
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
Display order of properties of a tensor object in debugging #1525
Comments
We don't officially support this, but I believe you could author a pydevd extension to customize the representation for your type - have a look at https://github.com/microsoft/debugpy/tree/main/src/debugpy/_vendored/pydevd/pydevd_plugins/extensions. Note however that we will be moving away from pydevd in the future. However, given that ML/AI is a particularly important scenario these days, I'd love to hear more about the nature of customizations in question, so that we can properly address this in our new debugger. |
The shape(dimension) of a tensor in vscode is far away from the top. Frequent need to expand and view results in low efficiency. And pycharm can see the type and shape without opening the variable, as shown in the figure. We are very concerned about the dimensions of tensors in ML/AI, it's so convenient. |
I believe the following issue are all about the same thing. The shape of numpy and tensor is so important for debuger in ML/AI. #1191 |
The shape of a variable cannot be easily viewed.Whether mouse hover will be supported in the future to see the shape of the variable. |
It is obvious that everyone is very satisfied with the debugging of pycharm. If there are no patents or other restrictions, you can copy the display stype and information of pycharm. If there are restrictions, ignore my comments pls. |
In addition to tensor and numpy, pycharm also has good type display support for list, tuple, dict, etc. |
I tempory use this but I strongly suggest vscode to support it natively |
I strongly suggest VS Code can show the len/shape of an countable object when debug, which PyCharm can do this, thanks to your work~~ |
any progress on this issue ? vscode is so unefficient for debugging deep learning program |
I made a quick attempt to follow the advice
Here is how I did: find the local directory where my pydevd extensions sit, in my case is '''
A simple example to show tensor shape on debugger
'''
from _pydevd_bundle.pydevd_extension_api import StrPresentationProvider
from .pydevd_helpers import find_mod_attr
class PyTorchTensorShapeStr:
def can_provide(self, type_object, type_name):
torch_tensor = find_mod_attr('torch', 'Tensor')
return torch_tensor is not None and issubclass(type_object, torch_tensor)
def get_str(self, val):
return f'shape: {val.shape}, value: {val}'
import sys
if not sys.platform.startswith("java"):
StrPresentationProvider.register(PyTorchTensorShapeStr) Then, reload my vscode, and I can see the shapes And if you would like to see the length/shape at any possible cases, you may leverage the from _pydevd_bundle.pydevd_extension_api import StrPresentationProvider
from .pydevd_helpers import find_mod_attr
class SizedShapeStr:
'''Displays the size of a Sized object before displaying its value.
'''
def can_provide(self, type_object, type_name):
sized_obj = find_mod_attr('collections.abc', 'Sized')
return sized_obj is not None and issubclass(type_object, sized_obj)
def get_str(self, val):
if hasattr(val, 'shape'):
return f'shape: {val.shape}, value: {val}'
return f'len: {len(val)}, value: {val}'
import sys
if not sys.platform.startswith("java"):
StrPresentationProvider.register(SizedShapeStr) Hi @ChaXxl @xuanhua @54nzg @Jesbong, I made a pull request #1661 ,which is waiting to be reviewed. In case it is not approved, perhaps you make it by yourself. Hope this may help you. |
@qige96 glad to hear this, I will try 👍 |
@qige96 Hi,sorry for this late feedback, I just tried to add a extension under debugpy like you did. Unable to load extension: pydevd_plugins.extensions.pydevd_plugin_pytorch_tensor_str I also add the root directory to the PYTHONPATH, which is required by link: https://github.com/microsoft/debugpy/tree/main/src/debugpy/_vendored/pydevd/pydevd_plugins/extensions But still the same error. And it cannot work both on my macbook pro and a container environment. My vscode version is Any suggestions ? |
Hi @xuanhua , I am not sure exactly about what was going wrong with your case, but I guess you put the plugin file in the wrong directory (due to my misleading words, sorry). You should put in If you put it in Hope this help you. |
@qige96 yes, following your suggestions, I move the my added file into |
@qige96 , Thanks for your solution, it maks my life really better, but one more question about these pydevd extentions, I am often used to use docker container as the dev environment. And I found in a new container, I have to copied them to that container from host manually. Is there any better way to avoid such manual copying everytime lauching a totally new container |
Hi @xuanhua , if they pydevd extension can merge my code into its codebase, then all problems will be gone. However, the author of Pydevd had a concern of the code in my PR to be an over-design (which indeed I admit). I am still thinking a better design to make us AI/ML people happy, but also comfort other users. But it takes time, not only for redesigning the code, but also for all other Open Source project processes, such as code review, and the author of Pydevd still has other things to do. For now and for you, the best way could be writing a script to do all the copying/downloading jobs, and execute that script in your Dockerfile, so that everytime you launch a new container. |
Hey, just to note, the whole plugins idea means you shouldn't need to change the debugger code for this, you can just add it to your own code or create your own library just with that (then you don't need to worry as much about all the other use cases out there -- sure, it'd be nice to have a fix in pydevd which would fix it for all, but in that case the bar is really higher so that it doesn't break unrelated use cases). The readme https://github.com/fabioz/PyDev.Debugger/blob/main/pydevd_plugins/extensions/README.md should explain the structure you can have without adding code to pydevd. |
Hi @fabioz , thanks for the reminder and that would be a good news for us. We don't need to bother the source repository. But I just had a quick try yet it didn't work. Here is my
# __init__.py
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__) # pydevd_plugin_shape_str.py
import sys
sys.path.append(r'C:\Users\ruiqi\.cursor\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd')
print(sys.path)
from _pydevd_bundle.pydevd_extension_api import StrPresentationProvider
# from pydevd_helpers import find_mod_attr, find_class_name
def find_mod_attr(mod_name, attr):
mod = find_cached_module(mod_name)
if mod is None:
return None
return getattr(mod, attr, None)
def find_class_name(val):
class_name = str(val.__class__)
if class_name.find(".") != -1:
class_name = class_name.split(".")[-1]
elif class_name.find("'") != -1: # does not have '.' (could be something like <type 'int'>)
class_name = class_name[class_name.index("'") + 1 :]
if class_name.endswith("'>"):
class_name = class_name[:-2]
return class_name
class SizedShapeStr:
'''Displays the size of a Sized object before displaying its value.
'''
def can_provide(self, type_object, type_name):
sized_obj = find_mod_attr('collections.abc', 'Sized')
return sized_obj is not None and issubclass(type_object, sized_obj)
def get_str(self, val):
if hasattr(val, 'shape'):
return f'shape: {val.shape}, value: {val}'
return f'len: {len(val)}, value: {val}'
if not sys.platform.startswith("java"):
StrPresentationProvider.register(SizedShapeStr) |
Your directory structure should like this: the extension directory is unnecessary in your zip file. |
Can I adjust the display order of properties of a tensor object when debugging Python in vscode? Because in deep learning, we focus more on the shape of a tensor rather than specific values. Now we want to see the value of a tensor by pulling it down a long distance. This has reduced my debugging efficiency.
The text was updated successfully, but these errors were encountered: