Repository: pytorch
Issue: Categorical Simplex constraint throws error for valid values
Labels: module: distributions,triaged

### 🐛 Describe the bug

When using `torch.distributions.Categorical` with a tensor of `torch.float16`, the Simplex validation fails.

Reproduces with the following code (on GPU)
```python
import torch
import torch.distributions as dist

device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
values = torch.tensor([0.10089111328125, 0.1925048828125, 0.15234375, 0.12152099609375, 0.1510009765625, 0.1805419921875, 0.150634765625, 0.1253662109375, 0.1356201171875, 0.14404296875, 0.13671875, 0.142822265625, 0.171630859375, 0.1619873046875, 0.1708984375, 0.083251953125, 0.178955078125, 0.121337890625, 0.1776123046875, 0.1689453125, 0.2469482421875, 0.193603515625, 0.162841796875, 0.2244873046875, 0.2734375, 0.26806640625, 0.1658935546875, 0.191162109375, 0.2017822265625, 0.191650390625, 0.1575927734375, 0.131103515625, 0.1680908203125, 0.1553955078125, 0.2208251953125, 0.2529296875, 0.2352294921875, 0.29443359375, 0.3447265625, 0.355224609375, 0.346923828125, 0.343994140625, 0.3125, 0.19873046875, 0.20751953125, 0.210205078125, 0.214599609375, 0.1605224609375, 0.1568603515625, 0.1431884765625, 0.2003173828125, 0.32080078125, 0.359619140625, 0.425048828125, 0.327392578125, 0.412841796875, 0.4619140625, 0.39892578125, 0.2408447265625, 0.206787109375, 0.254150390625, 0.314453125, 0.1744384765625, 0.1451416015625, 0.15625, 0.156982421875, 0.169189453125, 0.25146484375, 0.388427734375, 0.321044921875, 0.307861328125, 0.5224609375, 0.6650390625, 0.404296875, 0.263427734375, 0.389404296875, 0.44140625, 0.4267578125, 0.358154296875, 0.1767578125, 0.1795654296875, 0.2413330078125, 0.27001953125, 0.263671875, 0.2724609375, 0.283447265625, 0.28076171875, 0.41455078125, 0.3759765625, 0.30029296875, 0.40478515625, 0.4375, 0.486083984375, 0.60009765625, 0.4970703125, 0.178955078125, 0.26025390625, 0.27197265625, 0.359619140625, 0.381103515625, 0.3330078125, 0.31689453125, 0.368896484375, 0.40087890625, 0.321044921875, 0.299072265625, 0.489013671875, 0.54638671875, 0.654296875, 0.70068359375, 0.36767578125, 0.15576171875, 0.163818359375, 0.30322265625, 0.376953125, 0.385986328125, 0.306396484375, 0.3271484375, 0.39599609375, 0.331787109375, 0.301025390625, 0.3671875, 0.423095703125, 0.465087890625, 0.68896484375, 0.46337890625, 0.3037109375, 0.195068359375, 0.2332763671875, 0.298828125, 0.408935546875, 0.453125, 0.3779296875, 0.30859375, 0.27490234375, 0.260986328125, 0.29248046875, 0.360595703125, 0.5068359375, 0.36669921875, 0.443603515625, 0.349365234375, 0.29345703125, 0.2353515625, 0.233642578125, 0.335693359375, 0.391845703125, 0.453857421875, 0.371826171875, 0.40673828125, 0.47265625, 0.5556640625, 0.464599609375, 0.51171875, 0.469482421875, 0.4462890625, 0.47216796875, 0.37744140625, 0.36669921875, 0.248291015625, 0.20654296875, 0.2359619140625, 0.295654296875, 0.34130859375, 0.33837890625, 0.4853515625, 0.4111328125, 0.371337890625, 0.31787109375, 0.4345703125, 0.5263671875, 0.376220703125, 0.421875, 0.36572265625, 0.3193359375, 0.198974609375, 0.1947021484375, 0.22119140625, 0.27685546875, 0.291748046875, 0.257080078125, 0.32666015625, 0.31201171875, 0.29931640625, 0.2421875, 0.344482421875, 0.42041015625, 0.339111328125, 0.31689453125, 0.2176513671875, 0.222412109375, 0.174560546875, 0.1805419921875, 0.2244873046875, 0.288818359375, 0.300537109375, 0.283203125, 0.34375, 0.387939453125, 0.348388671875, 0.2919921875, 0.245849609375, 0.313232421875, 0.3017578125, 0.33984375, 0.2264404296875, 0.2109375, 0.1680908203125, 0.188232421875, 0.25244140625, 0.256591796875, 0.3115234375, 0.34130859375, 0.36474609375, 0.298583984375, 0.273193359375, 0.2275390625, 0.236572265625, 0.237060546875, 0.239990234375, 0.2010498046875, 0.196044921875, 0.1680908203125, 0.1383056640625, 0.209228515625, 0.1905517578125, 0.1845703125, 0.206787109375, 0.1903076171875, 0.1800537109375, 0.1734619140625, 0.15087890625, 0.1568603515625, 0.1729736328125, 0.1798095703125, 0.172119140625, 0.1512451171875, 0.1392822265625, 0.1326904296875, 0.129638671875, 0.1116943359375, 0.1904296875, 0.1793212890625, 0.2366943359375, 0.2391357421875, 0.197021484375, 0.2291259765625, 0.207763671875, 0.2169189453125, 0.2010498046875, 0.1904296875, 0.155029296875, 0.171875, 0.15380859375, 0.189697265625, 0.1802978515625])
values = values.to(device, torch.float16)  # If this line is commented out, there is no error
q = dist.Categorical(values)
```

The error is
```
Expected parameter probs (Tensor of shape (256,)) of distribution Categorical(probs: torch.Size([256])) to satisfy the constraint Simplex(), but found invalid values:
tensor([0.0014, 0.0026, 0.0021, 0.0017, 0.0021, 0.0025, 0.0021, 0.0017, 0.0019, ... 
File "<python_path>/lib/python3.10/site-packages/torch/distributions/distribution.py", line 62, in __init__
    raise ValueError(
  File "<python_path>/lib/python3.10/site-packages/torch/distributions/categorical.py", line 66, in __init__
    super().__init__(batch_shape, validate_args=validate_args)
  File "<project_path>/run.py", line 6, in <module>
    q = dist.Categorical(values)
  File "<python_path>/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "<python_path>/lib/python3.10/runpy.py", line 196, in _run_module_as_main (Current frame)
    return _run_code(code, main_globals, None,
```

### Versions

Collecting environment information...
PyTorch version: 2.0.0+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64)
GCC version: (GCC) 7.5.0
Clang version: Could not collect
CMake version: version 3.26.1
Libc version: glibc-2.31

Python version: 3.10.0 (default, Mar  3 2022, 09:58:08) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-122-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: Tesla V100-SXM2-32GB
GPU 1: Tesla V100-SXM2-32GB
GPU 2: Tesla V100-SXM2-32GB
GPU 3: Tesla V100-SXM2-32GB
GPU 4: Tesla V100-SXM2-32GB
GPU 5: Tesla V100-SXM2-32GB
GPU 6: Tesla V100-SXM2-32GB
GPU 7: Tesla V100-SXM2-32GB

Nvidia driver version: 470.129.06
cuDNN version: Could not collect
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):                          80
On-line CPU(s) list:             0-79
Thread(s) per core:              2
Core(s) per socket:              20
Socket(s):                       2
NUMA node(s):                    2
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           79
Model name:                      Intel(R) Xeon(R) CPU E5-2698 v4 @ 2.20GHz
Stepping:                        1
CPU MHz:                         1710.557
CPU max MHz:                     3600.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        4389.92
Virtualization:                  VT-x
L1d cache:                       1.3 MiB
L1i cache:                       1.3 MiB
L2 cache:                        10 MiB
L3 cache:                        100 MiB
NUMA node0 CPU(s):               0-19,40-59
NUMA node1 CPU(s):               20-39,60-79
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 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
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; Clear CPU buffers; SMT vulnerable
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 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap intel_pt xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts md_clear flush_l1d

Versions of relevant libraries:
[pip3] clip-anytorch==2.5.2
[pip3] dctorch==0.1.2
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.25.2
[pip3] open-clip-torch==2.16.2
[pip3] rotary-embedding-torch==0.3.0
[pip3] torch==2.0.0
[pip3] torchdiffeq==0.2.3
[pip3] torchsde==0.2.6
[pip3] torchvision==0.15.1
[pip3] triton==2.0.0
[conda] clip-anytorch             2.5.2                    pypi_0    pypi
[conda] dctorch                   0.1.2                    pypi_0    pypi
[conda] numpy                     1.25.2                   pypi_0    pypi
[conda] open-clip-torch           2.16.2                   pypi_0    pypi
[conda] rotary-embedding-torch    0.3.0                    pypi_0    pypi
[conda] torch                     2.0.0                    pypi_0    pypi
[conda] torchdiffeq               0.2.3                    pypi_0    pypi
[conda] torchsde                  0.2.6                    pypi_0    pypi
[conda] torchvision               0.15.1                   pypi_0    pypi
[conda] triton                    2.0.0                    pypi_0    pypi

cc @fritzo @neerajprad @alicanb @nikitaved