Gradcheck: "object has no attribute 'is_sparse'" in

I’m trying to run the MulConstant code from Extending Pytorch

class MulConstant(Function):
    def forward(ctx, tensor, constant):
        # ctx is a context object that can be used to stash information
        # for backward computation
        ctx.constant = constant
        return tensor * constant

    def backward(ctx, grad_output):
        # We return as many input gradients as there were arguments.
        # Gradients of non-Tensor arguments to forward must be None.
        return grad_output * ctx.constant, None

In particular, I’m testing the gradcheck function

input = torch.randn(20, requires_grad=True)
print(gradcheck(MulConstant, (input, 2), eps=1e-6, atol=1e-4))

However, I get the error

Traceback (most recent call last):
.../site-packages/torch/autograd/", line 136, in get_analytical_jacobian
    if output.is_sparse:
AttributeError: 'MulConstant' object has no attribute 'is_sparse'

I can’t figure out what’s going on. I can’t find any documentation on this is_sparse attribute. The extending pytorch documentation doesn’t mention it either.

(I’m on torch 1.8.0)

Never mind. Apparently, gradcheck needs to be called with MulConstant.apply.

1 Like