Is there a libtorch.natvis for debugging in Visual Studio?

When debugging libtorch source in Visual Studio, it would be really nice to see a more readable inspector of tensor, etc. I figured VS supported this because at some point std::vector got significantly more readable in the debugger. It seems .natvis files can be installed to do this. Here is an example for the Eigen library: Visual-Studio-Visualizers/Eigen.natvis at master · cdcseacave/Visual-Studio-Visualizers · GitHub

I don’t have any expertise with this, but I’m willing to give it a try, at least to easily see the shape and strides of a tensor. However, I wanted to see if this already exists?

2 Likes

I believe my issue (Python/C++ mixed mode debug problem) may be related to this one.

I didn’t find any for libtorch so I made one for myself. This has tensor, data, requires_grad, grad_Fn and grad. You can change it and add anything yourself.

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="at::Tensor" Priority="High">
    <DisplayString>{{ size={impl_.target_->numel_} }}</DisplayString>
    <Expand>
      <Item Name="Raw" ExcludeView="simple">impl_.target_</Item>
      <Item Name="Data" ExcludeView="simple">impl_.target_->storage_.storage_impl_.target_->data_ptr_.ptr_</Item>
      <Item Name="requires_grad" ExcludeView="simple">(*((torch::autograd::AutogradMeta*)impl_.target_->autograd_meta_._Mypair._Myval2)).requires_grad_</Item>
      <Item Name="grad_fn" ExcludeView="simple">(*((torch::autograd::AutogradMeta*)(*((c10::TensorImpl*)impl_.target_)).autograd_meta_._Mypair._Myval2)).grad_fn_</Item>
      <Item Name="grad" ExcludeView="simple">(*((torch::autograd::AutogradMeta*)impl_.target_->autograd_meta_._Mypair._Myval2)).grad_</Item>
      <ArrayItems>
        <Size>impl_.target_->numel_</Size>
        <ValuePointer>(float *)impl_.target_->storage_.storage_impl_.target_->data_ptr_.ptr_.data_</ValuePointer>
        </ArrayItems>
    </Expand>
  </Type>
</AutoVisualizer>

This does not work in vscode.