pytorch - 💡(How to fix) Fix torch.distributions.Cauchy.icdf(0) returns large finite value with wrong sign in float32; Gumbel.icdf(0/1) also finite instead of ±inf [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

Root causes:

Fix Action

Fixed

Code Example

import torch

print(torch.distributions.Cauchy(0., 1.).icdf(torch.tensor(0.0)))
# tensor(22877332.) — the 0th percentile of a Cauchy, reported as large POSITIVE

print(torch.distributions.Normal(0., 1.).icdf(torch.tensor(0.0)))
# tensor(-inf)Normal handles the boundary correctly
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

The quantile function at q=0 / q=1 should be the support endpoints (±inf for Cauchy and Gumbel). These inputs pass argument validation (q ∈ [0,1] is valid), but torch returns finite values — and for Cauchy in float32 the value has the wrong sign:

import torch

print(torch.distributions.Cauchy(0., 1.).icdf(torch.tensor(0.0)))
# tensor(22877332.) — the 0th percentile of a Cauchy, reported as large POSITIVE

print(torch.distributions.Normal(0., 1.).icdf(torch.tensor(0.0)))
# tensor(-inf) — Normal handles the boundary correctly
Inputfloat32float64expected (scipy)
Cauchy(0,1).icdf(0.0)+22877332.0 (wrong sign)−1.633e16−inf
Cauchy(0,1).icdf(1.0)−22877332.0 (wrong sign)+1.633e16+inf
Gumbel(0,1).icdf(0.0)−4.4698−6.5630−inf
Gumbel(0,1).icdf(1.0)15.942436.7368+inf

Root causes:

  • Cauchy.icdf evaluates torch.tan(math.pi * (value - 0.5)) * self.scale + self.loc with no boundary handling. In float32, the rounding of pi * (q - 0.5) lands on the far side of the tan pole at q=0/1, flipping the sign.
  • Gumbel.icdf inherits the deliberately truncated base Uniform(finfo.tiny, 1 - finfo.eps) (good for rsample stability), so the boundary quantiles come out as dtype-dependent finite values.

Normal.icdf (erfinv-based) already returns the correct ±inf, so the right convention exists in the module — suggested fix is to special-case q==0 / q==1 in Cauchy.icdf and Gumbel.icdf (the truncated base can stay for sampling; only icdf needs the clamp).

For reference, the same float32 sign-flip mechanism in a raw tan-based quantile was recently confirmed in JAX (jax-ml/jax#38318), so this is an easy trap for any direct-formula implementation.

Versions

torch 2.12.0, CPU (arm64, macOS); Cauchy.icdf unchanged on current main.

cc @fritzo @neerajprad @alicanb @nikitaved

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING