If you are using the unfold approach with an input tensor, which requires gradients, Autograd should be able to create the backward function automatically as long as you don’t detach the tensor from the computation graph.