Skip to content

bucketize -function wrong results on GPU #70343

Closed

Description

@pekaalto

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

2.18.0-dev20240624

Custom code

No

OS platform and distribution

No response

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

Bucketize function returns wrong results when executed on GPU.

Tested with 2.9.3, 2.15.0 and 2.18.0-dev20240624 and observed same incorrect behavior.

See the example below. The CPU and GPU results are different so both can't be correct. GPU result seems to be wrong.

Can reproduce in colab

Standalone code to reproduce the issue

from tensorflow.python.ops.gen_math_ops import bucketize
import tensorflow as tf

print(tf.__version__)
gpus = tf.config.list_physical_devices('GPU')
assert gpus

x = [0,1,2,3]
boundaries = [0.1, 1.1]
with tf.device("/CPU:0"):
    print(bucketize(x, boundaries=boundaries))
with tf.device("/GPU:0"):
    print(bucketize(x, boundaries=boundaries))

Relevant log output

2.18.0-dev20240624
tf.Tensor([0 1 2 2], shape=(4,), dtype=int32)
tf.Tensor([1 2 2 2], shape=(4,), dtype=int32)

Activity

  1. added
    comp:apis
    comp:gpu
    and removed
    comp:apis
    on Jun 26, 2024
  2. sushreebarsa commented on Jun 27, 2024

    @sushreebarsa
    Contributor

    @pekaalto The GPU implementation of bucketize might not handle the rounding or boundary conditions in the same way as the CPU implementation. This could lead to differences in how values are categorized into buckets.

    Thank you!

  3. added
    stat:awaiting response
    on Jun 27, 2024
  4. pekaalto commented on Jun 29, 2024

    @pekaalto
    Author

    @pekaalto The GPU implementation of bucketize might not handle the rounding or boundary conditions in the same way as the CPU implementation. This could lead to differences in how values are categorized into buckets.

    Thank you!

    Hi, you can see in the example that bucket boundaries are well away from the values to bucketize so it's not immediate boundary condition.

    However, I suspect that there is some implicit data type conversion happening to boundaries on GPU which makes it boundary condition and then it fails.

    Feeding int tensor doesn't work:

    bucketize(tf.convert_to_tensor([0,1,2,3], dtype=tf.int32), boundaries=[0.5, 1.5])
    >>> tf.Tensor([1 2 2 2], shape=(4,), dtype=int32)
    

    but with float it works

    bucketize(tf.convert_to_tensor([0,1,2,3], dtype=tf.float32), boundaries=[0.5, 1.5])
    >>> tf.Tensor([0 1 2 2], shape=(4,), dtype=int32) ```
    
    
  5. removed
    stat:awaiting response
    on Jun 29, 2024
  6. sushreebarsa commented on Jul 1, 2024

    @sushreebarsa
    Contributor

    @tilakrayal I was able to replicate the issue reported here.
    Thank you!

  7. ashok2216-A commented on Jul 1, 2024

    @ashok2216-A

    The bucketize function in TensorFlow appears to produce inconsistent results between CPU and GPU execution. When using TensorFlow versions 2.9.3, 2.15.0, and 2.18.0-dev20240624, the function yields different outcomes on these devices.

    Here's a reproducible example demonstrating the issue:

    from tensorflow.python.ops.gen_math_ops import bucketize
    import tensorflow as tf
    
    print(tf.__version__)
    gpus = tf.config.list_physical_devices('GPU')
    assert gpus
    
    x = [0,1,2,3]
    boundaries = [0.1, 1.1]
    with tf.device("/CPU:0"):
        print(bucketize(x, boundaries=boundaries))
    with tf.device("/GPU:0"):
        print(bucketize(x, boundaries=boundaries))

    Output:

    2.18.0-dev20240624
    tf.Tensor([0 1 2 2], shape=(4,), dtype=int32)
    tf.Tensor([1 2 2 2], shape=(4,), dtype=int32)
    

    The CPU output is [0 1 2 2], whereas the GPU output is [1 2 2 2], indicating a discrepancy. The GPU result seems incorrect based on the defined boundaries.

  8. ghostway0 commented on Jul 26, 2024

    @ghostway0

    note changing x = [0,1,2,3] to x = [0.,1.,2.,3.] fixes the problem. I think this is because the cpu version is casting to the boundaries' type and the gpu version casts into the type of the input. which is the more correct version? I personally lean towards the cpu's version, because it gives the correct output for the boundaries' type (if you already give it a float, you would assume it uses it without rounding down)

    it's also easier to fix the gpu's version (I already have the fix locally)

  9. removed
    comp:gpu
    on Jul 26, 2024
  10. 6 remaining items

  11. Venkat6871 commented on Jun 3, 2026

    @Venkat6871
    Contributor

    Hi @pekaalto ,
    Thank you so much for filing this issue. We truly appreciate your support. At the moment, this request is not prioritized and mark it for community contributions. If you would like to contribute on a fix, please feel free to fork the repository and submit a pull request in accordance with the TensorFlow CONTRIBUTION GUIDELINES and link this issue in your PR to ensure the maintainers can easily review and collaborate.
    To avoid duplicate work, please leave a comment here if you decide to take it on!

    Thank you again for your interest and we look forward to seeing your contributions!

  12. added
    stat:contribution welcome
    on Jun 3, 2026
  13. Venkat6871 commented on Sep 28, 2026

    @Venkat6871
    Contributor

    Hi @pekaalto ,
    Thank you for raising your concern here.
    A fix for this issue has been proposed in this #128013. Once it is merged, this issue will be resolved.

    Hi @bodapatisaikrishna ,
    Thank you for raising the PR to address this issue. We truly appreciate your initiative and the effort in contributing the fix.

  14. added
    awaiting PR merge
    and removed
    stat:contribution welcome
    on Sep 28, 2026
  15. removed
    awaiting PR merge
    on Sep 30, 2026
  16. Kayyuri commented on Oct 1, 2026

    @Kayyuri

    Hi @pekaalto, Thank you for your patience!

    The fix for this issue was merged into master via commit e2c7dee, from PR #128013. I verified the provided code using the latest tf-nightly build, and I am no longer able to reproduce the reported behavior. Now the code produces the consistent results on both CPU and GPU.

    tf.Tensor([0 1 2 2], shape=(4,), dtype=int32)
    tf.Tensor([0 1 2 2], shape=(4,), dtype=int32)

    Could you please test this with the latest tf-nightly build on your side and confirm whether the issue has been resolved?
    I have also attached gist file for your reference.

    Thank you!

  17. added
    stat:awaiting response
    on Oct 1, 2026
  18. github-actions commented on Oct 15, 2026

    @github-actions

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

  19. added
    stale
    on Oct 15, 2026
  20. github-actions commented on Oct 22, 2026

    @github-actions

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

  21. google-ml-butler commented on Oct 22, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

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

Metadata

Metadata

Assignees

Labels

2.17comp:opsstalestat:awaiting responsetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions