Error: ‘struct DLTensor’ has no member named ‘ctx’

Animesh_Gupta: <p>I am trying to compile custom cpp extension but I am getting following error:</p>
<pre><code class="lang-auto">/workspace/cpp_build/cpp_functions.cpp:81:28: error: ‘struct DLTensor’ has no member named ‘ctx’
   81 |       dlMTensor-&gt;dl_tensor.ctx.device_id = device_id;
</code></pre>
<p>Code related to this is:</p>
<pre><code class="lang-auto">#include &lt;torch/extension.h&gt;

#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;

#include &lt;ATen/NativeFunctions.h&gt;
#include &lt;ATen/Config.h&gt;
#include &lt;ATen/DLConvertor.h&gt;
#include &lt;ATen/Functions.h&gt;

at::Tensor backward_weight(
    c10::ArrayRef&lt;long int&gt; weight_size,
    const at::Tensor&amp; grad_output,
    const at::Tensor&amp; input,
    c10::ArrayRef&lt;long int&gt; padding,
    c10::ArrayRef&lt;long int&gt; stride,
    c10::ArrayRef&lt;long int&gt; dilation,
    int64_t groups,
    bool benchmark,
    bool deterministic) {

  return at::cudnn_convolution_backward_weight(
      weight_size,
      grad_output,
      input,
      padding,
      stride,
      dilation,
      groups,
      benchmark,
      deterministic);
}
at::Tensor backward_input(
    c10::ArrayRef&lt;long int&gt; input_size,
    const at::Tensor&amp; grad_output,
    const at::Tensor&amp; weight,
    c10::ArrayRef&lt;long int&gt; padding,
    c10::ArrayRef&lt;long int&gt; stride,
    c10::ArrayRef&lt;long int&gt; dilation,
    int64_t groups,
    bool benchmark,
    bool deterministic) {

  return at::cudnn_convolution_backward_input(
      input_size,
      grad_output,
      weight,
      padding,
      stride,
      dilation,
      groups,
      benchmark,
      deterministic);
}

// From pytorch/torch/csrc/Module.cpp
void DLPack_Capsule_Destructor(PyObject* data) {
  HANDLE_TH_ERRORS
    DLManagedTensor * dlMTensor = (DLManagedTensor *)PyCapsule_GetPointer(data, "dltensor");
  if (dlMTensor) {
    // the dlMTensor has not been consumed, call deleter ourselves
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
    dlMTensor-&gt;deleter(const_cast&lt;DLManagedTensor*&gt;(dlMTensor));
  } else {
    // the dlMTensor has been consumed
    // PyCapsule_GetPointer has set an error indicator
    PyErr_Clear();
  }
  END_HANDLE_TH_ERRORS_RET()
}

namespace py = pybind11;
using namespace pybind11::literals;

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("backward", &amp;backward_weight, "Conv backward_weight cudnn");
  m.def("backward_input", &amp;backward_input, "Conv backward_input cudnn");
  m.def("to_dlpack_with_device_id", [](const at::Tensor&amp; data, int64_t device_id) {
      DLManagedTensor* dlMTensor = at::toDLPack(data);
      
      dlMTensor-&gt;dl_tensor.ctx.device_id = device_id;
      auto capsule = py::capsule(dlMTensor, "dltensor", DLPack_Capsule_Destructor);
      return capsule;
  }, "Specify device_id in dlpack, for cupy to copy to right GPU");
}
</code></pre>
<p>I want to build this cpp extension for latest cudatoolkit version latest pytorch version.<br>
Can anyone guide me on how I can modify <code>dlMTensor-&gt;dl_tensor.ctx.device_id = device_id;</code> for latest pytorch version?</p>

ptrblck: <p>I don’t know which <code>DLPack</code> version you are using, but the missing <code>ctx</code> attribute might have been caused by <a href="https://github.com/dmlc/dlpack/commit/8323706bb5b2298f3c432e5fc23d75c8f3058a51#diff-1564e5abd0da5cf46e82b28b0058996df944007494520a2b0a6b629880eaa097">this PR</a> which replaced <code>DLContext ctx</code> with <code>DLDevice device</code>.</p>

Animesh_Gupta: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> for replying. I was able to solve it too. But I wanted to ask one more thing. In the <code>at::Tensor backward_weight</code> function it returns <code>cudnn_convolution_backward_weight</code> which is deprecated in the latest version of pytorch. I checked and I found that pytorch has now renamed the same function and is present in <code>native</code> library with name of <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/cudnn/ConvShared.cpp#L329" rel="noopener nofollow ugc">cudnn_convolution_backward_weight</a>. Can you please guide me how I can use this function with the latest pytorch version.</p>
<p>I looked at some relevant tutorials from <a href="https://pytorch.org/tutorials/advanced/dispatcher.html#defining-schema-and-backend-implementations" rel="noopener nofollow ugc">REGISTERING A DISPATCHED OPERATOR IN C++</a> and <a href="https://pytorch.org/tutorials/advanced/torch_script_custom_ops#using-the-torchscript-custom-operator-in-python" rel="noopener nofollow ugc">EXTENDING TORCHSCRIPT WITH CUSTOM C++ OPERATORS</a>.</p>
<p>Similar issue but with no solution till now. <a href="https://discuss.pytorch.org/t/how-to-include-native-functions-from-aten-in-cpp-cuda-extension/28749">How to include native functions from ATen in cpp/CUDA extension?</a></p>

ptrblck: <p>In Python you could use:</p>
<pre><code class="lang-python">torch.nn.grad.conv2d_weight
torch.nn.grad.conv2d_input
</code></pre>
<p>and I think in <code>libtorch</code></p>
<pre><code class="lang-auto">std::tuple&lt;Tensor, Tensor, Tensor&gt; convolution_backward(
    const Tensor&amp; grad_output_, const Tensor&amp; input_, const Tensor&amp; weight_,
    const at::OptionalIntArrayRef bias_sizes_opt,
    IntArrayRef stride, IntArrayRef padding, IntArrayRef dilation, bool transposed, IntArrayRef output_padding,
    int64_t groups, std::array&lt;bool, 3&gt; output_mask);
</code></pre>
<p>should work which will return:</p>
<pre><code class="lang-auto">std::make_tuple(backend_grad_input, backend_grad_weight, backend_grad_bias);
</code></pre>