Recently I have implemented a novel function and self-specified backward process. However, I have met the problem of RuntimeError: No grad accumulator for a saved leaf!
Therefore, I want to know if there are methods to:


Insert code and trace the backward process like forward


Why this error occurs?


The core part of my code is as follows
class MixedOp(nn.Module):
    MODE = None
    def __init__(self, C, stride):
        super(MixedOp, self).__init__()
        # candidate_ops is a list of pytorch modules, such as pooling and convolution
        self.candidate_ops = nn.ModuleList()
        # insert operations to the candidate_ops
        ......
    
    def forward(self, x):
        def run_function(candidate_ops, active_id):
            def forward(_x):
                return candidate_ops[active_id](_x)
            return forward
        
        # backward here is for the update of weight parameters
        def backward_function(candidate_ops, active_id, binary_gates):
            def backward(_x, _output, grad_output):
                binary_grads = torch.zeros_like(binary_gates.data)
                with torch.no_grad():
                    for k in range(len(candidate_ops)):
                        if k != active_id:
                            out_k = candidate_ops[k](_x.data)
                        else:
                            out_k = output.data
                        grad_k = torch.sum(out_k * grad_output)
                        binary_grads[k] = grad_k
                return binary_grads
            return backward
        
        output = ArchGradientFunction.apply(
            x, self.alpha_gate, run_function(self.candidate_ops, self.active_index[0]),
            backward_function(self.candidate_ops, self.active_index[0], self.alpha_gate)
        )
        return output


class ArchGradientFunction(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x, binary_gates, run_func, backward_func):
        ctx.run_func = run_func
        ctx.backward_func = backward_func

        detached_x = detach_variable(x)
        with torch.enable_grad():
            output = run_func(detached_x)
        ctx.save_for_backward(detached_x, output)
        return output.data
    
    @staticmethod
    def backward(ctx, grad_output):
        detached_x, output = ctx.saved_tensors

        grad_x = torch.autograd.grad(output, detached_x, grad_output, only_inputs=True)
        # compute gradients w.r.t. binary_gates
        binary_grads = ctx.backward_func(detached_x.data, output.data, grad_output.data)

        return grad_x[0], binary_grads, None, None