Repository: pytorch
Issue:  Improve Error Message in MultiMarginLoss for Inconsistent Target Size
Labels: module: nn,good first issue,module: error checking,triaged,actionable

### 🐛 Describe the bug

While using the MultiMarginLoss function in PyTorch, I encountered a RuntimeError that could be more informative. The error message received was:
```
RuntimeError: inconsistent target size, got: [7]
```
This error occurred with the following code:

```python
import torch
import torch.nn as nn

ip_size = [8,8]
target_size = [7]
input_tensor = torch.rand(ip_size)
target_tensor = torch.rand(target_size).type(torch.LongTensor)

loss_function = nn.MultiMarginLoss()

# Compute loss
loss = loss_function(input_tensor, target_tensor)
```
For a more effective debugging experience, I suggest revising the error message to provide more specifics about the size and dimension mismatches, similar to other loss functions error messages. A proposed change could be:
```python
"MultiMarginLoss: The size of input tensor ({input size}) must match the size of target tensor ({target size}) at non-singleton dimension {dimension}"
```
This message would not only communicate the problem but also provide the specific mismatched sizes and dimension, which would assist users in correcting their code.

Please let me know if there's any additional information you need or any other ways I can assist. If this behavior is expected and known, feel free to close this issue.
While this message indicates that there is an inconsistency with the target size, it does not provide specific details about the expected size or the mismatched dimensions.





### Versions

PyTorch version: 2.1.0.dev20230622+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: 12.0.0-3ubuntu1~20.04.5
CMake version: version 3.26.1
Libc version: glibc-2.31

Python version: 3.9.16 (main, Mar  8 2023, 14:00:05)  [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.4.0-152-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 12.1.105
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 2070
GPU 1: NVIDIA GeForce RTX 2070
GPU 2: NVIDIA GeForce RTX 2070
GPU 3: NVIDIA GeForce RTX 2070

Nvidia driver version: 530.30.02
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.2
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          32
On-line CPU(s) list:             0-31
Thread(s) per core:              2
Core(s) per socket:              8
Socket(s):                       2
NUMA node(s):                    2
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           63
Model name:                      Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz
Stepping:                        2
CPU MHz:                         1224.656
CPU max MHz:                     3200.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        4794.39
Virtualization:                  VT-x
L1d cache:                       512 KiB
L1i cache:                       512 KiB
L2 cache:                        4 MiB
L3 cache:                        40 MiB
NUMA node0 CPU(s):               0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30
NUMA node1 CPU(s):               1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31
Vulnerability Itlb multihit:     KVM: Mitigation: Split huge pages
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm ida arat pln pts md_clear flush_l1d

Versions of relevant libraries:
[pip3] numpy==1.24.1
[pip3] pytorch-triton==2.1.0+440fd1bf20
[pip3] torch==2.1.0.dev20230622+cu118
[pip3] torchaudio==2.1.0.dev20230622+cu118
[pip3] torchvision==0.16.0.dev20230622+cu118
[conda] blas                      1.0                         mkl

[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2023.1.0         h6d00ec8_46342

[conda] mkl-service               2.4.0            py39h5eee18b_1

[conda] mkl_fft                   1.3.6            py39h417a72b_1

[conda] mkl_random                1.2.2            py39h417a72b_1

[conda] numpy                     1.24.1                   pypi_0    pypi
[conda] pytorch-mutex             1.0                         cpu    pytorch
[conda] pytorch-triton            2.1.0+440fd1bf20          pypi_0    pypi
[conda] torch                     2.1.0.dev20230622+cu118          pypi_0    pypi
[conda] torchaudio                2.1.0.dev20230622+cu118          pypi_0    pypi
[conda] torchvision               0.16.0.dev20230622+cu118          pypi_0    pypi

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki @malfet