pytorch - 💡(How to fix) Fix torch.xlogy(0, 0) returns NaN, while in many numerical computing libraries (e.g., SciPy) and common machine learning practice, it is defined as 0.

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

Minimal 3-line version:

print("\nMinimal: xlogy at x=0") v = torch.tensor([0.0, 0.5, 1.0, 2.0]) print(f"xlogy(v, v) = {torch.xlogy(v, v)}")

Expected: [nan, -0.3466, 0.0, 1.3863]

NaN because xlogy(0, 0) = 0 * log(0) = 0 * -inf = NaN

Code Example

import torch
import torch.nn as nn

torch.manual_seed(0)

fc1 = nn.Linear(8, 8)
fc2 = nn.Linear(8, 8)
bn  = nn.BatchNorm1d(8)

x = torch.randn(4, 8)

with torch.enable_grad():
    x = x.requires_grad_(True)
    x_ = fc1(x)
    x_ = torch.nn.functional.hardswish(x_)   # can output exactly 0
    x_ = bn(x_)
    x_ = fc2(x_)
    x_ = torch.log1p(x_)
    out = torch.xlogy(x_, x_)                 # xlogy(0, 0) = NaN if any element = 0

print(f"hardswish zeros: {(torch.nn.functional.hardswish(fc1(x).detach()) == 0).sum().item()}")
print(f"xlogy output: nan={torch.isnan(out).any().item()}")

# Minimal 3-line version:
print("\nMinimal: xlogy at x=0")
v = torch.tensor([0.0, 0.5, 1.0, 2.0])
print(f"xlogy(v, v) = {torch.xlogy(v, v)}")
# Expected: [nan, -0.3466, 0.0, 1.3863]
# NaN because xlogy(0, 0) = 0 * log(0) = 0 * -inf = NaN

# The mathematically correct value:
print(f"lim_{{x→0}} x*log(x) = 0, but torch gives: {torch.xlogy(torch.tensor(0.0), torch.tensor(0.0))}")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
import torch.nn as nn

torch.manual_seed(0)

fc1 = nn.Linear(8, 8)
fc2 = nn.Linear(8, 8)
bn  = nn.BatchNorm1d(8)

x = torch.randn(4, 8)

with torch.enable_grad():
    x = x.requires_grad_(True)
    x_ = fc1(x)
    x_ = torch.nn.functional.hardswish(x_)   # can output exactly 0
    x_ = bn(x_)
    x_ = fc2(x_)
    x_ = torch.log1p(x_)
    out = torch.xlogy(x_, x_)                 # xlogy(0, 0) = NaN if any element = 0

print(f"hardswish zeros: {(torch.nn.functional.hardswish(fc1(x).detach()) == 0).sum().item()}")
print(f"xlogy output: nan={torch.isnan(out).any().item()}")

# Minimal 3-line version:
print("\nMinimal: xlogy at x=0")
v = torch.tensor([0.0, 0.5, 1.0, 2.0])
print(f"xlogy(v, v) = {torch.xlogy(v, v)}")
# Expected: [nan, -0.3466, 0.0, 1.3863]
# NaN because xlogy(0, 0) = 0 * log(0) = 0 * -inf = NaN

# The mathematically correct value:
print(f"lim_{{x→0}} x*log(x) = 0, but torch gives: {torch.xlogy(torch.tensor(0.0), torch.tensor(0.0))}")

Versions

2.9.1+cu128 (PyTorch 2.9.1, CUDA 12.8)

torch.xlogy(0, 0) returns NaN, while in many numerical computing libraries (e.g., SciPy) and common machine learning practice, it is defined as 0.

cc @mruberry @kshitij12345

extent analysis

TL;DR

The issue can be fixed by handling the special case where the input to torch.xlogy is zero, as the function currently returns NaN in this case, whereas the expected result is 0.

Guidance

  • Identify where torch.xlogy is used in the code and add a check for zero input values to handle this special case.
  • Consider using a small epsilon value to avoid division by zero and log of zero issues.
  • Review the documentation of torch.xlogy to see if there are any plans to change its behavior for zero inputs.
  • If possible, use a different function or library that handles this case correctly, such as SciPy.

Example

import torch

def safe_xlogy(x, y):
    return torch.where(x == 0, torch.zeros_like(x), torch.xlogy(x, y))

v = torch.tensor([0.0, 0.5, 1.0, 2.0])
print(f"safe_xlogy(v, v) = {safe_xlogy(v, v)}")

Notes

This fix assumes that the desired behavior for torch.xlogy(0, 0) is to return 0, as is common in many numerical computing libraries.

Recommendation

Apply workaround: use a custom function like safe_xlogy to handle the special case where the input to torch.xlogy is zero.

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