Skip to content

Fix zero-alpha NaN propagation in LinearCombination - #3930

Open
Miya555orz wants to merge 1 commit into
NVIDIA:mainfrom
Miya555orz:codex/fix-linear-combination-alpha-zero-nan
Open

Miya555orz wants to merge 1 commit into
NVIDIA:mainfrom
Miya555orz:codex/fix-linear-combination-alpha-zero-nan

Conversation

@Miya555orz

@Miya555orz Miya555orz commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

Fixes #3901.

This change makes the default LinearCombination epilogue avoid evaluating
the accumulator term when alpha == 0.

In that case:

  • beta * source is returned directly when the source is required;
  • zero is returned when beta == 0 or the source is not required;
  • neither alpha * accumulator nor beta * source with a zero coefficient
    is evaluated.

The Fragment and scalar overloads use the same semantics.

Root cause

The SIMT GEMM mainloop may produce a NaN accumulator when A or B contains
NaN. The default epilogue previously evaluated:

alpha * accumulator + beta * source

unconditionally. Under IEEE 754, 0 * NaN is NaN, so setting alpha to zero
did not prevent NaNs from the accumulator from reaching the output.

The same problem can occur for the source term when both beta == 0 and the
source contains NaN.

The new zero-alpha path returns before converting or multiplying the
accumulator. It also produces zero directly when the source coefficient is
zero.

Scope

This patch changes the default LinearCombination operator used by the
reported device::Gemm path. Specialized epilogues with additional
activation or elementwise semantics remain unchanged.

The normal alpha != 0 arithmetic path remains the same after the uniform
guard. An sm_89 ptxas comparison showed identical resource usage before
and after the change:

  • 122 registers
  • 0 spill stores
  • 0 spill loads
  • 0-byte stack frame

Tests

Added thread-level tests covering:

  • Fragment and scalar overloads;
  • alpha = 0, beta = 1 with a NaN accumulator;
  • alpha = 0, beta = 0 with both accumulator and source set to NaN;
  • source and no-source overloads.

Added a device-level SM80 SIMT SGEMM regression test using an 8x8x8 problem:

  • A and B are filled with quiet_NaN();
  • alpha = 0, beta = 1 verifies that D equals C;
  • alpha = 0, beta = 0 uses a NaN C tensor and verifies that D is zero.

Validated on:

  • NVIDIA RTX 500 Ada Generation Laptop GPU (sm_89)
  • CUDA Toolkit 13.2 (nvcc 13.2.86)
  • Ubuntu 22.04

All targeted tests pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Default SIMT device::Gemm propagates NaNs when alpha is zero

1 participant