Skip to content

Fix integer overflow in GPU kernel loops for large tensors - #112249

Closed
kokol16 wants to merge 1 commit into
tensorflow:masterfrom
kokol16:fix/roll-populationCount
Closed

kokol16 wants to merge 1 commit into
tensorflow:masterfrom
kokol16:fix/roll-populationCount

Conversation

@kokol16

@kokol16 kokol16 commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

GPU_1D_KERNEL_LOOP and CUDA_1D_KERNEL_LOOP currently hardcode the loop
index type to int via GpuGridRangeX<int>. That can be safe for some kernels, but
it becomes fragile for very large launches: when the iteration space is close to
the 32-bit limit, the range helper’s internal stride arithmetic can overflow,
which may yield incorrect indices.

This patch extends both macros with an optional explicit index type:
GPU_1D_KERNEL_LOOP(i, n, T) and CUDA_1D_KERNEL_LOOP(i, n, T). Callers that
operate on large tensor sizes can now request int64_t, so the range/stride
computation is performed in 64-bit arithmetic.

The change is backward compatible. The existing 2-argument form is preserved and
continues to instantiate GpuGridRangeX<int>, while the new 3-argument form is
selected only for call sites that explicitly provide a type. This patch updates
the population_count and roll GPU kernels to use int64_t indices.
fixed issues #109520 #104077
There are ~20 open issues that can be fixed using this new macro, would you like me
to create one pull request that fixes them all or split in chunks?

@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Mar 12, 2026
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Mar 31, 2026
@keerthanakadiri keerthanakadiri added comp:core issues related to core part of tensorflow and removed awaiting review Pull request awaiting review labels Mar 31, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Mar 31, 2026
@keerthanakadiri keerthanakadiri added the awaiting review Pull request awaiting review label Mar 31, 2026
@dmiltr3
dmiltr3 self-requested a review June 28, 2026 22:36

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kokol16,

Thank you for contributing! Extending the 1D GPU kernel loops to support generic types is highly valuable for numerical robustness. However, we found a few issues that need to be resolved before this PR is ready to merge:

1. 32-Bit Parameter Truncation in GPU Kernels

Simply changing the loop iteration type to int64_t in the macro is not sufficient because the loop boundaries passed to the kernel signatures are still 32-bit signed integers:

  • In tensorflow/core/kernels/population_count_op_gpu.cu.cc, the kernel signature takes const int size.
  • In tensorflow/core/kernels/roll_op_gpu.cu.cc, the kernel signature takes const int32_t nthreads.

If a tensor exceeds $2^{31}-1$ elements, the size will truncate when passed to the kernel, meaning the loop boundary is already corrupted by the time the kernel starts. The signatures must be updated to accept int64_t (or int64 type alias).

2. 32-Bit Launch Configuration

The host-side launch logic for both operations currently uses GetGpuLaunchConfig(), which only accepts a 32-bit signed integer count and returning a 32-bit GpuLaunchConfig struct (truncating your actual 64-bit totals).

To fully support these large sizes, you'll need to update the launch configurations to use GetGpuLaunchConfig64 and GpuLaunchConfig64 respectively.

3. Missing Unit Tests

Please add test coverage to verify compilation and execution of the three-argument variant (such as with int64_t) in tensorflow/core/util/gpu_kernel_helper_test.cu.cc.

Could you please update the PR to include these updates? Let us know if you have any questions!

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Jun 28, 2026
@keerthanakadiri keerthanakadiri added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Jun 30, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions Bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Jul 15, 2026
@github-actions

Copy link
Copy Markdown

This PR was closed because it has been inactive for 14 days since being marked as stale. Please reopen if you'd like to work on this further.

@github-actions github-actions Bot closed this Jul 29, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Closed/Rejected in PR Queue Jul 29, 2026
@kokol16

kokol16 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

@dmiltr3 Reopening via a new PR with your requested changes addressed:

  • kernel signatures now take int64_t size / nthreads
  • host launch uses GetGpuLaunchConfig64 / GpuLaunchConfig64
  • added GetGpuLaunchConfig64WithTyped1DKernelLoop in gpu_kernel_helper_test.cu.cc

New PR: #126698

Thanks for the review feedback.

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

Labels

comp:core issues related to core part of tensorflow size:S CL Change Size: Small stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author

Projects

Status: Closed/Rejected

Development

Successfully merging this pull request may close these issues.

4 participants