you forgot the chain rule.
gradinput = gradoutput * local_gradient
The better implementation look like this:
class SomeLoss(Function):

    def forward(self, input):
        self.input_size = input.size()
        return input.new([input.sum()])

    def backward(self,grad_output):
        return grad_output.expand(self.input_size)

there are many  examples of Function in _functions  35, and  a good tutorial in docs 37