Change:
// torch.SparseTensor() if (num_args == 0) { self->cdata = THSTensor_(new)(LIBRARY_STATE_NOARGS); <del> return (PyObject*)self.release(); } PyObject *first_arg = PyTuple_GET_ITEM(args, 0); </del> <ins> } else { PyObject *first_arg = PyTuple_GET_ITEM(args, 0); </ins> <del> // torch.SparseTensor(size) if (num_args == 1 && THPUtils_checkLong(first_arg)) { long size = THPUtils_unpackLong(first_arg); self->cdata = THSTensor_(newWithSize1d)(LIBRARY_STATE size); } // torch.SparseTensor(torch.Size sizes) else if (num_args == 1 && THPSize_Check(first_arg)) { THLongStoragePtr sizes(THPUtils_unpackSize(first_arg)); self->cdata = THSTensor_(newWithSize)(LIBRARY_STATE sizes.get()); } // torch.SparseTensor(torch.LongTensor indices, torch.LongTensor values) else if (num_args == 2 && THPIndexTensor_Check(first_arg)) { PyObject *second_arg = PyTuple_GET_ITEM(args, 1); if (!THPTensor_(Check)(second_arg)) goto invalid_arguments; </del> <ins> // torch.SparseTensor(size) if (num_args == 1 && THPUtils_checkLong(first_arg)) { long size = THPUtils_unpackLong(first_arg); self->cdata = THSTensor_(newWithSize1d)(LIBRARY_STATE size); } // torch.SparseTensor(torch.Size sizes) else if (num_args == 1 && THPSize_Check(first_arg)) { THLongStoragePtr sizes(THPUtils_unpackSize(first_arg)); self->cdata = THSTensor_(newWithSize)(LIBRARY_STATE sizes.get()); } // torch.SparseTensor(torch.LongTensor indices, torch.LongTensor values) else if (num_args == 2 && THPIndexTensor_Check(first_arg)) { PyObject *second_arg = PyTuple_GET_ITEM(args, 1); if (!THPTensor_(Check)(second_arg)) goto invalid_arguments; </ins> <del> THIndexTensor *indices = ((THPIndexTensor*)first_arg)->cdata; THTensor *values = ((THPTensor*)second_arg)->cdata; </del> <ins> THIndexTensor *indices = ((THPIndexTensor*)first_arg)->cdata; THTensor *values = ((THPTensor*)second_arg)->cdata; </ins> #ifdef THC_GENERIC_FILE <del> THCAssertSameGPU(THSTensor_(checkGPU)(LIBRARY_STATE 0, 2, indices, values)); </del> <ins> THCAssertSameGPU(THSTensor_(checkGPU)(LIBRARY_STATE 0, 2, indices, values)); </ins> #endif <del> self->cdata = THSTensor_(newWithTensor)(LIBRARY_STATE indices, values); } // torch.SparseTensor(torch.LongTensor indices, //                    torch.Tensor values, //                    torch.Size sizes) else if (num_args > 2 && THPIndexTensor_Check(first_arg)) { PyObject *second_arg = PyTuple_GET_ITEM(args, 1); PyObject *third_arg = PyTuple_GET_ITEM(args, 2); if (!THPTensor_(Check)(second_arg)) goto invalid_arguments; if (!THPSize_Check(third_arg)) goto invalid_arguments; </del> <ins> self->cdata = THSTensor_(newWithTensor)(LIBRARY_STATE indices, values); } // torch.SparseTensor(torch.LongTensor indices, //                    torch.Tensor values, //                    torch.Size sizes) else if (num_args > 2 && THPIndexTensor_Check(first_arg)) { PyObject *second_arg = PyTuple_GET_ITEM(args, 1); PyObject *third_arg = PyTuple_GET_ITEM(args, 2); if (!THPTensor_(Check)(second_arg)) goto invalid_arguments; if (!THPSize_Check(third_arg)) goto invalid_arguments; </ins> <del> THIndexTensor *indices = ((THPIndexTensor*)first_arg)->cdata; THTensor *values = ((THPTensor*)second_arg)->cdata; THLongStoragePtr sizes(THPUtils_unpackSize(third_arg)); </del> <ins> THIndexTensor *indices = ((THPIndexTensor*)first_arg)->cdata; THTensor *values = ((THPTensor*)second_arg)->cdata; THLongStoragePtr sizes(THPUtils_unpackSize(third_arg)); </ins> #ifdef THC_GENERIC_FILE <del> THCAssertSameGPU(THSTensor_(checkGPU)(LIBRARY_STATE 0, 2, indices, values)); </del> <ins> THCAssertSameGPU(THSTensor_(checkGPU)(LIBRARY_STATE 0, 2, indices, values)); </ins> #endif <del> self->cdata = THSTensor_(newWithTensorAndSize)( LIBRARY_STATE indices, values, sizes); } // torch.SparseTensor(int ...) else if (THPUtils_tryUnpackLongVarArgs(args, 0, sizes)) { self->cdata = THSTensor_(newWithSize)(LIBRARY_STATE sizes.get()); </del> <ins> self->cdata = THSTensor_(newWithTensorAndSize)( LIBRARY_STATE indices, values, sizes); } // torch.SparseTensor(int ...) else if (THPUtils_tryUnpackLongVarArgs(args, 0, sizes)) { self->cdata = THSTensor_(newWithSize)(LIBRARY_STATE sizes.get()); } else goto invalid_arguments; // All other cases </ins> } <del> else goto invalid_arguments; // All other cases THSPTensor_(initStorage)(self->cdata); </del> <ins> THSTensor_(initStorage)(self->cdata); </ins> return (PyObject*)self.release(); invalid_arguments:

Discussion:
This causes to not work properly on newly created empty sparse tensors. In the example below, both should print 1 instead.