← Back to Pull requests

[BUG] Avoid intermediate overflow in vectorized QuantileLoss #2465

Open
unknown opened this pull request with 421 lines diff across 5 files
View on GitHub
unknown unknown commented on Oct 14, 2026 00:31 UTC
Author

LLM generated content, by OpenAI Codex

Reference Issues/PRs

Follow-up to the vectorization in #2447. This addresses numerical overflow rather than the input-shape change proposed in #2113.

What does this implement/fix? Explain your changes.

The equivalent pinball expression (abs(error) - error) / 2 first doubles negative residuals. That intermediate can overflow even when the final quantile loss is representable. In float16, a residual of -40000 at q=.75 produces infinity instead of 20000; the public metric then substitutes its nonfinite-loss fallback of 1e9.

Use q error - min(error, 0) before the existing factor of two. This avoids doubling the residual, retains broadcasting and the vectorized implementation, and preserves the existing zero-residual subgradient through torch.minimum.

Reproducible example and motivation

This standalone CPU example addresses the request for a concrete failure:

import torch
from pytorch_forecasting.metrics import QuantileLoss

prediction = torch.tensor([[[40000.0]]], dtype=torch.float16)
target = torch.zeros((1, 1), dtype=torch.float16)
metric = QuantileLoss(quantiles=[0.75])

print(metric.loss(prediction, target))
print(metric(prediction, target).item())
# Expected: raw loss 20000, public metric 20000.
# Clean base: raw loss inf, public metric 1000000000.0
# PR head:   raw loss 20000, public metric 20000.0

The residual -40000 and final value 2 (1 - .75) * 40000 = 20000 both fit in float16. The former intermediate abs(error) - error = 80000 exceeds float16's maximum 65504 before division by two can bring it back into range. MultiHorizonMetric then substitutes 1e9, so this affects the public loss value rather than only a private intermediate. Keeping finite, representable quantile losses correct is the reason for the change.

I reran this exact input against the clean base's actual QuantileLoss class and the PR head on CPU; the values above were reproduced. This targets loss computations with float16 residuals; it does not claim every autocast training configuration uses that dtype or is affected.

What should a reviewer concentrate their feedback on?

The equivalent pinball expression and preservation of the zero-residual subgradient. Cases where the final mathematical loss itself exceeds the dtype range are outside this fix.

Did you add any tests for the change?

28 numerical regression cases cover float16/32/64, positive/negative and ordinary/large residuals, two batch/horizon layouts, analytical prediction gradients, zero-residual subgradients and the public one-quantile metric. Clean main: 7 failures, 21 passing controls. Final numerical plus existing metric suites: 98 passed, 6 existing skips because optional cpflows is absent.

Any other comments?

Python 3.12.14, PyTorch 2.13.0+cpu. Configured pre-commit hooks and git diff --check pass. Full-package, GPU, documentation and training-throughput tests were not run. A small one-thread CPU arithmetic benchmark at shape (256,32,7) showed no slowdown for float16/32; this is not a general performance claim.

AI disclosure: prepared and tested autonomously with OpenAI Codex. No independent human review has been performed.

PR checklist

  • The PR title starts with [BUG].

  • Added regression tests.

  • Ran the configured pre-commit hooks.
  • Diff: 421 lines (+315 / -106) across 5 files Reviewer: phoeenniixx Review duration: 25563m
    All Vouch review confidence checks passed
    Residual risk 0.29 = Change Risk (0.50) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 421 lines diff across 5 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.362
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #2465"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50% (MEDIUM) · Higher = larger blast radius
    50%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 28.5% · Safe to merge
    28.5%