Skip to content

nll_loss doesn't support empty tensors on gpu #31472

@alcinos

Description

@alcinos

🐛 Bug

Passing empty tensors to nll_loss should return an empty tensor. It is the case on cpu, but crashes on gpu

To Reproduce

>>> import torch
>>> a = torch.rand(0, 4).cuda()
>>> b = torch.tensor([]).cuda()
>>> torch.nn.functional.nll_loss(a, b.long(), reduction='none')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/private/home/alcinos/.conda/envs/detection/lib/python3.6/site-packages/torch/nn/functional.py", line 1845, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: invalid argument 2: non-empty vector or matrix expected at /opt/conda/conda-bld/pytorch_1576310878804/work/aten/src/THCUNN/generic/ClassNLLCriterion.cu:31

Expected behavior

On cpu it works well:

>>> a = torch.rand(0, 4)
>>> b = torch.tensor([])
>>> torch.nn.functional.nll_loss(a, b.long(), reduction='none')
tensor([])

Environment

PyTorch version: 1.4.0.dev20191214
Is debug build: No
CUDA used to build PyTorch: 9.2

OS: Ubuntu 18.04.1 LTS
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
CMake version: version 3.10.2

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 9.2.88
GPU models and configuration:
GPU 0: Quadro GP100
GPU 1: Quadro GP100

Nvidia driver version: 410.79
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] numpy==1.17.3
[pip] pytorch-memlab==0.0.4
[pip] torch==1.4.0.dev20191214
[pip] torchfile==0.1.0
[pip] torchnet==0.0.4
[pip] torchvision==0.5.0a0+4f2e755
[conda] blas 1.0 mkl
[conda] mkl 2019.4 243
[conda] mkl-service 2.3.0 py36he904b0f_0
[conda] mkl_fft 1.0.15 py36ha843d7b_0
[conda] mkl_random 1.1.0 py36hd6b4f25_0
[conda] pytorch 1.4.0.dev20191214 py3.6_cuda9.2.148_cudnn7.6.3_0 pytorch-nightly
[conda] pytorch-memlab 0.0.4 pypi_0 pypi
[conda] torchfile 0.1.0 pypi_0 pypi
[conda] torchnet 0.0.4 pypi_0 pypi
[conda] torchvision 0.5.0.dev20191214 py36_cu92 pytorch-nightly

cc @ngimel

Metadata

Metadata

Assignees

Labels

module: cudaRelated to torch.cuda, and CUDA support in generaltriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions