Libtorch CUDA backpropagation

yessyw: <p>Hello, I want to make custom cuda layer in my model.</p>
<p>I found some examples that make custom forward and backward.</p>
<p>but, i can’t find the example how to teach my model using my gradient on c++.</p>
<p>Do i need to make custom optimizer?</p>

glaringlee: <p><a class="mention" href="/u/yessyw">@yessyw</a><br>
We have a end to end example here:<br>
<a href="https://pytorch.org/cppdocs/frontend.html" rel="nofollow noopener">https://pytorch.org/cppdocs/frontend.html</a> (scroll down to end to end example)</p>
<p>Yes, you might need optimizer, we have several optimizer available. check here:<br>
‘<a href="https://github.com/pytorch/pytorch/issues/28440" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/28440</a>’</p>
<p>And yes you can add your own optimizer, they should be the same as how you add forward and backward, same python binding, only the input and output are different.</p>

yessyw: <p>Thanks to your reply.</p>
<p>Sorry. my question was not clear.</p>
<p>I want to make custom CUDA layer in my model<br>
It means to make (.cu) file.<br>
So, I have to use Aten tensor which means I can’t use autograd.<br>
I have to make custom forward and backward layer for getting right gradient.<br>
I found some example for make custom forward backwadr layer.<br>
But, I can’t find how to update variables in c++</p>
<p>Can I have to make custom optimizer to update variables or Is there simple way.</p>

glaringlee: <p><a class="mention" href="/u/yessyw">@yessyw</a><br>
Correct me if I am wrong, what you want to do is to apply your calculated gradient to the original input tensor, right? If your gradient apply logic is not trivial, you can make your own optimizer, but if you just want to do simple operation, you can do it directly.</p>
<p>eg. if you want to add your gradient to input tensor, you can just do input + grad where input is your input tensor and grad is the calculated gradient. Same as what you do in python.</p>

yessyw: <p>Thanks to your reply.</p>
<p>If the original input tensor that you mention means weight, yes that i want to do.<br>
Thanks to your help.  I will try to make optimizer.</p>

yessyw: <p>I found some solution for this problem in tutorial. But, I’m not sure. I want to check my code is working right whether or not. This example code is conceptual code.</p>
<ol>
<li>kernel.cu</li>
</ol>
<pre><code class="lang-auto">#include &lt;ATen/ATen.h&gt;
#include &lt;ATen/cuda/CUDAContext.h&gt;
#include &lt;ATen/core/TensorAccessor.h&gt;

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
using at::Tensor;

Tensor CustomLossFunction(Tensor &amp;grad, const Tensor &amp;A, const long long N){
    auto B = at::zeros(N).to(at::kCUDA);
    LossFunction&lt;&lt;&lt;nBlock, nThread&gt;&gt;&gt;(grad.data&lt;float&gt;(), A.data&lt;float&gt;(), B.data&lt;float&gt;(), N);
   return B;
}
template&lt;typename T&gt;
__global__ void LossFunction(T *grad, T *B, const T *A, const long long N){
    const size_t tid = blockDim.x * gridDim.x + blockIdx.x * blockDim.x + threadIdx.x;
    if(tid &lt; N){
         C[tid] = A[tid] * A[tid] * 2.f;
         grad[tid] = A[tid] * 4.f;
    }
}
</code></pre>
<ol start="2">
<li>main.cpp</li>
</ol>
<pre><code class="lang-auto">#include &lt;ATen/ATen.h&gt;
#include &lt;torch/torch.h&gt;
#include "main.h"
using at::Tensor;

Tensor CustomLossFunction(Tensor &amp;grad, const Tensor &amp;A, const long long N);
int main(){
   Net model;
   model.to(torch::kCUDA);
   torch::optim::SGD optimizer(model.parameters(), torch::optim::SGDOptions(0.001).momentum(0.5));
   auto model_in = DataLoader(fileName).to(torch::kCUDA);
   auto Exact = DataLoader(fileName).to(torch::kCUDA);
   
   optimizer.zero_grad();
   auto grad = torch::zeros(10).to(Torch::kCUDA);
   auto Y = model.forward(model_in);
   auto Loss = CustomLossFunction(grad, Y, 10).mean();
   Y.backward(grad / 10);
   optimizer.step();
}
</code></pre>
<p>Updating parameters using custom loss function.</p>