Skip to content

Fix NaN handling in GPU cast operations for int32/int64 conversions (#12345)#106214

Open
Mohataseem89 wants to merge 1 commit intotensorflow:masterfrom
Mohataseem89:patch-2
Open

Fix NaN handling in GPU cast operations for int32/int64 conversions (#12345)#106214
Mohataseem89 wants to merge 1 commit intotensorflow:masterfrom
Mohataseem89:patch-2

Conversation

@Mohataseem89
Copy link
Copy Markdown

Added IsNan function templates for different types and updated CastFunctor implementations to handle NaN values during type casting.

This PR addresses Issue #12345, which reported incorrect behavior when casting floating-point tensors containing NaN values to integer types on GPU devices.

Previously, GPU cast functors directly used isnan(v) in device lambdas. This caused undefined behavior for types like Eigen::half and bfloat16 on CUDA/ROCm, and NaN inputs were not safely handled when converting to int32 or int64.

Changes

  • Added a device-friendly IsNan() utility function that supports float, double, Eigen::half, and bfloat16.
  • Updated the following CastFunctors to use IsNan(v):
    • CastFunctor<GPUDevice, int32, float>
    • CastFunctor<GPUDevice, int64, float>
    • CastFunctor<GPUDevice, int32, double>
    • CastFunctor<GPUDevice, int64, double>
  • Ensures that NaN values are safely converted to 0 during casting.

Files Affected

  • tensorflow/core/kernels/cast_op_gpu.cu.cc

This PR resolves Issue #12345 by adding safe NaN handling in GPU casting.

@google-ml-butler google-ml-butler bot added the size:M CL Change Size: Medium label Dec 14, 2025
@google-ml-butler google-ml-butler bot added the awaiting review Pull request awaiting review label Dec 15, 2025
@github-project-automation github-project-automation bot moved this to Assigned Reviewer in PR Queue Dec 15, 2025
@keerthanakadiri keerthanakadiri added the comp:core issues related to core part of tensorflow label Dec 15, 2025
@github-project-automation github-project-automation bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Dec 15, 2025
@mihaimaruseac
Copy link
Copy Markdown
Contributor

Please don't use "add file"/"update file"/"fix file"/etc. commit messages. These are hard to reason about when looking at the history of the file/repository. Instead, please write explanatory git commit messages.

The commit message is also the title of the PR if the PR has only one commit. It is thus twice important to have commit messages that are relevant, as PRs would be easier to understand and easier to analyze in search results.

For how to write good quality git commit messages, please consult https://cbea.ms/git-commit/


Please collapse both commits into just the first one (git rebase -i)

Add explicit IsNan helpers and update GPU CastFunctor implementations
to safely handle NaN values when casting floating-point tensors to
integer types on GPU devices, aligning behavior with CPU and NumPy.
@Mohataseem89
Copy link
Copy Markdown
Author

Please don't use "add file"/"update file"/"fix file"/etc. commit messages. These are hard to reason about when looking at the history of the file/repository. Instead, please write explanatory git commit messages.

The commit message is also the title of the PR if the PR has only one commit. It is thus twice important to have commit messages that are relevant, as PRs would be easier to understand and easier to analyze in search results.

For how to write good quality git commit messages, please consult https://cbea.ms/git-commit/

Please collapse both commits into just the first one (git rebase -i)

Thanks for the guidance
I have squashed the commits into one and updated the commit message to be more descriptive following the recommended guidelines.

@mihaimaruseac
Copy link
Copy Markdown
Contributor

Waiting for review from @cantonios or someone else with more GPU kernels expertise

@keerthanakadiri keerthanakadiri requested review from cantonios and removed request for cantonios January 9, 2026 08:13
@Mohataseem89 Mohataseem89 marked this pull request as draft February 6, 2026 11:38
@Mohataseem89 Mohataseem89 marked this pull request as ready for review February 9, 2026 13:42
@keerthanakadiri keerthanakadiri removed the request for review from mihaimaruseac February 24, 2026 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review Pull request awaiting review comp:core issues related to core part of tensorflow size:M CL Change Size: Medium

Projects

Status: Reviewer Requested Changes

Development

Successfully merging this pull request may close these issues.

PEP 484 Type Annotations (feature request)

4 participants