pytorch - 💡(How to fix) Fix Distribution.icdf performs no argument validation even with validate_args=True, unlike cdf/log_prob

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 cause: the base Distribution.icdf is a bare raise NotImplementedError with no validation contract, and no subclass icdf implementation (Uniform, Cauchy, Exponential, Laplace, ...) checks the unit interval — while the cdf/log_prob siblings all call self._validate_sample when self._validate_args is set. There is no _validate_sample equivalent for quantile arguments.

Code Example

import torch
td = torch.distributions

td.Uniform(0., 1.).cdf(torch.tensor(2.0))    # ValueError — validation works
td.Uniform(0., 1.).icdf(torch.tensor(2.0))   # tensor(2.) — silent linear extrapolation
td.Cauchy(0., 1.).icdf(torch.tensor(-1.0))   # tensor(83858288.)
td.Exponential(1.).icdf(torch.tensor(-2.0))  # tensor(-1.0986) — negative "quantile"
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

With argument validation enabled (the default), cdf and log_prob reject out-of-support input — but icdf accepts any quantile and silently evaluates its formula:

import torch
td = torch.distributions

td.Uniform(0., 1.).cdf(torch.tensor(2.0))    # ValueError — validation works
td.Uniform(0., 1.).icdf(torch.tensor(2.0))   # tensor(2.) — silent linear extrapolation
td.Cauchy(0., 1.).icdf(torch.tensor(-1.0))   # tensor(83858288.)
td.Exponential(1.).icdf(torch.tensor(-2.0))  # tensor(-1.0986) — negative "quantile"

Quantiles are undefined outside [0, 1] (scipy returns nan for all of the above).

Root cause: the base Distribution.icdf is a bare raise NotImplementedError with no validation contract, and no subclass icdf implementation (Uniform, Cauchy, Exponential, Laplace, ...) checks the unit interval — while the cdf/log_prob siblings all call self._validate_sample when self._validate_args is set. There is no _validate_sample equivalent for quantile arguments.

Suggested fix: validate q ∈ [0,1] in icdf when _validate_args is set — a unit-interval check in the base class (or a shared helper used by subclasses) would cover all distributions. Alternatively, document that icdf performs no validation.

Related: #186824 (even valid boundary quantiles q=0/1 produce wrong finite values for Cauchy/Gumbel).

Versions

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

cc @fritzo @neerajprad @alicanb @nikitaved @malfet

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