pytorch / pytorch/pytorch

torch.compile silently accepts zero-batch input to Categorical that eager correctly rejects

Open
#194,548 1 comment 0 reactions 0 assignees View on GitHub
bot-triaged module: distributions module: dynamic shapes oncall: pt2 topic: fuzzer triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

### 🐛 Describe the bug

## 🐛 Bug Description

`torch.distributions.Categorical(logits=...).sample()` with a zero-batch-size
input correctly raises `RuntimeError` in eager mode (this is a known,
previously-reported eager-mode limitation — see #71689). Under
`torch.compile`, the same call silently succeeds instead of raising the same
error — the compiler is masking/tolerating an input eager correctly rejects.

```python
import torch

def fn(logits):
d = torch.distributions.Categorical(logits=logits)
return d.sample()

x = torch.randn(0, 2)

# eager
fn(x)
# RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1]
# because the unspecified dimension size -1 can be any value and is ambiguous

# compiled
torch.compile(fn, backend="inductor")(x)
# succeeds, returns tensor of shape [0]
```

## Actual behavior

Compiled mode returns a `shape=[0]` tensor with no error or warning.

## Expected behavior

Either compiled mode should raise the same `RuntimeError` as eager (matching
eager/compiled parity), or — if zero-batch support is intentionally added at
the compiled layer — eager's own rejection (#71689) should be revisited
separately, since right now the two modes disagree about whether this input
is valid at all.

## Related (not a duplicate)

#71689 documents eager's own rejection of zero-batch `Categorical` inputs,
but does not discuss `torch.compile` behavior at all — that issue predates
broad `torch.compile` support for `torch.distributions`. This report is
specifically about the eager/compiled *asymmetry*, not the underlying
eager-mode limitation itself.

## Additional context

Found via a coverage/gate-guided compiler fuzzer built for a research project
on cross-compiler bug-finding, targeting eager/compiled divergences
specifically. Confirmed reproducible across repeated runs.

## Versions

torch 2.14.0a0 (nightly)

### Versions

PyTorch version: 2.14.0a0+git1b8a19b
Is debug build: False
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
Clang version: Could not collect
CMake version: version 4.4.2
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jun 22 2026, 18:55:27) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-187-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration:
GPU 0: NVIDIA A100 80GB PCIe
GPU 1: NVIDIA A100 80GB PCIe
GPU 2: NVIDIA H200 NVL

Nvidia driver version: 590.48.01
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False
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): 128
On-line CPU(s) list: 0-127
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Gold 6448H
CPU family: 6
Model: 143
Thread(s) per core: 2
Core(s) per socket: 32
Socket(s): 2
Stepping: 8
CPU max MHz: 4100.0000
CPU min MHz: 800.0000
BogoMIPS: 4800.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 sgx bmi1 avx2 smep bmi2 erms invpcid 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 sgx_lc fsrm md_clear serialize tsxldtrk pconfig arch_lbr amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities ibpb_exit_to_user
Virtualization: VT-x
L1d cache: 3 MiB (64 instances)
L1i cache: 2 MiB (64 instances)
L2 cache: 128 MiB (64 instances)
L3 cache: 120 MiB (2 instances)
NUMA node(s): 2
NUMA node0 CPU(s): 0-31,64-95
NUMA node1 CPU(s): 32-63,96-127
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; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds: Not affected
Vulnerability Tsa: Not affected
Vulnerability Tsx async abort: Not affected
Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Versions of relevant libraries:
[pip3] numpy==2.2.6
[pip3] torch==2.14.0a0+git1b8a19b
[conda] Could not collect

cc @fritzo @neerajprad @alicanb @nikitaved @chauhang @penguinwu @ezyang @bobrenjc93 @aditvenk @laithsakka

Contributor guide

Open the contributing guide

Research direction

Start by running the provided eager and torch.compile(..., backend="inductor") reproducer with the reported zero-batch logits, then compare the Categorical.sample paths. Trace where the eager RuntimeError is avoided in compiled mode; done means the two modes have an intentional, tested agreement about whether this input is accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.