Repository: pytorch
Issue: Wrong results when sampling from the beta distribution for small alpha=beta
Labels: module: numerical-stability,module: distributions,triaged

### 🐛 Describe the bug

There seems to be a numerical problem when sampling from `torch.distributions.Beta` with $\alpha=\beta\to 0$:
```py
import torch
import matplotlib.pyplot as plt
x = 1/1000
pdf = torch.distributions.Beta(torch.tensor([x]), torch.tensor([x]))
samples = pdf.sample((10_000,)).numpy()
plt.hist(samples, bins=10, density=True)
```
results in 
![image](https://github.com/user-attachments/assets/5c12ea9a-5e7a-44ba-8123-993cd0d812cd)

This appears wrong, as the density shouldn't have any mass around 1/2 when the two parameters approach zero. 


### Versions

PyTorch version: 2.3.0+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Fedora Linux 39 (Thirty Nine) (x86_64)
GCC version: (GCC) 13.3.1 20240522 (Red Hat 13.3.1-1)
Clang version: 17.0.6 (Fedora 17.0.6-2.fc39)
CMake version: version 3.27.7
Libc version: glibc-2.38

Python version: 3.12.4 (main, Jun  7 2024, 00:00:00) [GCC 13.3.1 20240522 (Red Hat 13.3.1-1)] (64-bit runtime)
Python platform: Linux-6.9.9-100.fc39.x86_64-x86_64-with-glibc2.38
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: Quadro RTX 8000
Nvidia driver version: 555.58.02
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
Address sizes:                        46 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               72
On-line CPU(s) list:                  0-71
Vendor ID:                            GenuineIntel
Model name:                           Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz
CPU family:                           6
Model:                                85
Thread(s) per core:                   2
Core(s) per socket:                   18
Socket(s):                            2
Stepping:                             7
CPU(s) scaling MHz:                   26%
CPU max MHz:                          3900.0000
CPU min MHz:                          1000.0000
BogoMIPS:                             5200.00
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 art 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 intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization:                       VT-x
L1d cache:                            1.1 MiB (36 instances)
L1i cache:                            1.1 MiB (36 instances)
L2 cache:                             36 MiB (36 instances)
L3 cache:                             49.5 MiB (2 instances)
NUMA node(s):                         2
NUMA node0 CPU(s):                    0-17,36-53
NUMA node1 CPU(s):                    18-35,54-71

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] torch==2.3.0
[pip3] torchvision==0.18.0
[pip3] torchviz==0.0.2
[conda] Could not collect

cc @fritzo @neerajprad @alicanb @nikitaved