Skip to content

Gamma distribution broken on Intel GPUs#194926

Description

@niteya-shah

馃悰 Describe the bug

For $X\sim\Gamma(\alpha,\beta)$ in the shape-rate parameterization, the theoretical mean and variance are $\alpha/\beta$ and $\alpha/\beta^2$, respectively. Therefore for torch.distributions.Gamma(concentration=alpha, rate=1), both the expected mean and variance are $\alpha$. This follows from the standard Gamma distribution moments given by Wikipedia and from PyTorch's documented parameterization.

However for the intel GPU, we are seeing the expected statistics. The code I am using to test this is

import math
import torch

device = "xpu"  # or "cuda" depending on device
dtype = torch.float64
n = 10000000

for alpha in [1.1644, 1.4164, 2.3335, 10.076]:
    concentration = torch.tensor(alpha, device=device, dtype=dtype)
    rate = torch.tensor(1.0, device=device, dtype=dtype)

    dist = torch.distributions.Gamma(concentration, rate)
    x = dist.sample((n,))

    mean = x.to('cpu').mean().item()
    var = x.to('cpu').var().item()
    expected_mean = alpha
    expected_var = alpha
    mean_se = math.sqrt(expected_var / n)

    print(
        f"alpha={alpha:<7} "
        f"mean={mean:.10f} expected_mean={expected_mean:.10f} "
        f"mean_z={(mean - expected_mean) / mean_se:.2f} "
        f"var={var:.10f} expected_var={expected_var:.10f}"
    )

On Intel GPUs I get

(/opt/aurora/26.26.0/frameworks/aurora_frameworks-2025.3.1) niteya@x4311c2s5b0n0:~/tests> python xpu_test.py 
alpha=1.1644  mean=1.1274936642 expected_mean=1.1644000000 mean_z=-108.16 var=0.9738913693 expected_var=1.1644000000
alpha=1.4164  mean=1.3808148438 expected_mean=1.4164000000 mean_z=-94.55 var=1.1929258174 expected_var=1.4164000000
alpha=2.3335  mean=2.3093565129 expected_mean=2.3335000000 mean_z=-49.98 var=2.0742910082 expected_var=2.3335000000
alpha=10.076  mean=10.0579647756 expected_mean=10.0760000000 mean_z=-17.97 var=9.4731084899 expected_var=10.0760000000

On nvidia

(quantom_loits_cuda) niteya@saturn:~/testing$ python test.py 
alpha=1.1644  mean=1.1643172241 expected_mean=1.1644000000 mean_z=-0.24 var=1.1645859138 expected_var=1.1644000000
alpha=1.4164  mean=1.4163616598 expected_mean=1.4164000000 mean_z=-0.10 var=1.4162850560 expected_var=1.4164000000
alpha=2.3335  mean=2.3338548478 expected_mean=2.3335000000 mean_z=0.73 var=2.3342634044 expected_var=2.3335000000
alpha=10.076  mean=10.0772972788 expected_mean=10.0760000000 mean_z=1.29 var=10.0779441303 expected_var=10.0760000000

On AMD

(quantom_loits) [niteya@odyssey testing]$ python test.py 
alpha=1.1644  mean=1.1646313435 expected_mean=1.1644000000 mean_z=0.68 var=1.1658620441 expected_var=1.1644000000
alpha=1.4164  mean=1.4163486995 expected_mean=1.4164000000 mean_z=-0.14 var=1.4157618006 expected_var=1.4164000000
alpha=2.3335  mean=2.3324789512 expected_mean=2.3335000000 mean_z=-2.11 var=2.3323716726 expected_var=2.3335000000
alpha=10.076  mean=10.0761668439 expected_mean=10.0760000000 mean_z=0.17 var=10.0803041080 expected_var=10.0760000000

The gamma distribution for intel GPUs has substantially larger errors, and we dont get the expected statistics we would expect from the implementation.

Also as a side note, removing the to('cpu') on the intel code causes it to segfault.

Versions

(/opt/aurora/26.26.0/frameworks/aurora_frameworks-2025.3.1) niteya@x4311c2s5b0n0:~/tests> curl -sL https://raw.githubusercontent.com/pytorch/pytorch/main/torch/utils/collect_env.py | python

Collecting environment information...
PyTorch version: 2.10.0a0+git449b176
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: SUSE Linux Enterprise Server 15 SP4 (x86_64)
GCC version: (Spack GCC) 13.4.0
Clang version: Could not collect
CMake version: version 3.31.10
Libc version: glibc-2.31

Python version: 3.12.12 | packaged by conda-forge | (main, Jan 26 2026, 23:51:32) [GCC 14.3.0] (64-bit runtime)
Python platform: Linux-5.14.21-150400.24.225-default-x86_64-with-glibc2.31
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: True
XPU used to build PyTorch: 20250302
Intel GPU driver version:
* intel-opencl:	25.18.33578.42-1146
* level-zero:	1.24.0.0-i1146
Intel GPU models onboard:
* Intel(R) Data Center GPU Max 1550
* Intel(R) Data Center GPU Max 1550
* Intel(R) Data Center GPU Max 1550
* Intel(R) Data Center GPU Max 1550
* Intel(R) Data Center GPU Max 1550
* Intel(R) Data Center GPU Max 1550
Intel GPU models detected:
* [0] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=a75e6ba0-c835-b2ca-0000-000000000001, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [1] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=a75e6ba0-c835-b2ca-0000-000000000002, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [2] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=564f403d-d7c8-d1e0-0000-000000000001, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [3] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=564f403d-d7c8-d1e0-0000-000000000002, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [4] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=d982ab7c-b3f5-4f06-0000-000000000001, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [5] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=d982ab7c-b3f5-4f06-0000-000000000002, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [6] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=74ea596e-ab27-c800-0000-000000000001, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [7] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=74ea596e-ab27-c800-0000-000000000002, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [8] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=90d78633-b91c-24ee-0000-000000000001, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [9] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=90d78633-b91c-24ee-0000-000000000002, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [10] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=b81af61a-8a64-a366-0000-000000000001, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
* [11] _XpuDeviceProperties(name='Intel(R) Data Center GPU Max 1550', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero', type='gpu', device_id=0xBD6, uuid=b81af61a-8a64-a366-0000-000000000002, driver_version='1.6.33578+42', total_memory=65520MB, max_compute_units=448, gpu_eu_count=448, gpu_subslice_count=56, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1)
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Address sizes:                           52 bits physical, 57 bits virtual
Byte Order:                              Little Endian
CPU(s):                                  208
On-line CPU(s) list:                     0-207
Vendor ID:                               GenuineIntel
Model name:                              Intel(R) Xeon(R) CPU Max 9470C
CPU family:                              6
Model:                                   143
Thread(s) per core:                      2
Core(s) per socket:                      52
Socket(s):                               2
Stepping:                                8
Frequency boost:                         enabled
CPU max MHz:                             2001.0000
CPU min MHz:                             800.0000
BogoMIPS:                                4000.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 tsc_known_freq 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 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr avx512_fp16 amx_tile flush_l1d arch_capabilities
Virtualization:                          VT-x
L1d cache:                               4.9 MiB (104 instances)
L1i cache:                               3.3 MiB (104 instances)
L2 cache:                                208 MiB (104 instances)
L3 cache:                                210 MiB (2 instances)
NUMA node(s):                            4
NUMA node0 CPU(s):                       0-51,104-155
NUMA node1 CPU(s):                       52-103,156-207
NUMA node2 CPU(s):                       
NUMA node3 CPU(s):                       
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      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; Enhanced / Automatic IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Versions of relevant libraries:
[pip3] flake8==7.3.0
[pip3] intel_extension_for_pytorch==2.10.10+gitd0f992f
[pip3] numpy==2.2.6
[pip3] optree==0.18.0
[pip3] torch==2.10.0a0+git449b176
[pip3] torchao==0.15.0+git9338966da
[pip3] torchcomms==0.1.0
[pip3] torchdata==0.11.0+377e64c
[pip3] torchvision==0.25.0+8ac84ee
[pip3] triton==3.6.0+git225cdbde
[conda] intel-extension-for-pytorch          2.10.10+gitd0f992f   pypi_0              pypi
[conda] numpy                                2.2.6                pypi_0              pypi
[conda] optree                               0.18.0               pypi_0              pypi
[conda] torch                                2.10.0a0+git449b176  pypi_0              pypi
[conda] torchao                              0.15.0+git9338966da  pypi_0              pypi
[conda] torchcomms                           0.1.0                pypi_0              pypi
[conda] torchdata                            0.11.0+377e64c       pypi_0              pypi
[conda] torchvision                          0.25.0+8ac84ee       pypi_0              pypi
[conda] triton                               3.6.0+git225cdbde    pypi_0              pypi

cc @fritzo @neerajprad @alicanb @nikitaved @gujinghui @EikanWang @fengyuan14 @guangyey

Metadata

Metadata

Assignees

Labels

bot-triagedThis is a label only to be used by the auto triage botmodule: correctness (silent)issue that returns an incorrect result silentlymodule: crashProblem manifests as a hard crash, as opposed to a RuntimeErrormodule: distributionsRelated to torch.distributionsmodule: xpuIntel XPU related issuestriage review

Type

No type

Projects

Status
No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions