Warning spam then using MultiMarginLoss

/usr/local/lib/python3.8/dist-packages/torch/nn/functional.py:3349: UserWarning: An output with one or more elements was resized since it had shape [], which does not match the required output shape [56].This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (Triggered internally at  ../aten/src/ATen/native/Resize.cpp:23.)
  return torch._C._nn.multi_margin_loss(input, target, p, margin, weight, reduction_enum)

Not sure what it want from me, I checked inputs and shapes is [56, 22] and [56]

Could you post a minimal, executable code snippet reproducing this warning, please?

import torch
from torch import nn
from torch.nn import functional as F

bs = 56
model = nn.Linear(128, 22).cuda()
loss = nn.MultiMarginLoss()
x = torch.rand((bs, 128)).cuda()
targets = torch.randint(22, (bs,)).cuda()
out = model(x)
loss(out, targets)

Another observation: it is fine without cuda.

Thanks for finding and reporting this issue! I’ve opened a PR to address it here:
Abate spurious resize warnings in MultiMarginLoss on CUDA by eqy · Pull Request #75000 · pytorch/pytorch (github.com)

1 Like