Skip to content

Conversation

@CodersAcademy006
Copy link
Contributor

This PR resolves a platform-specific bug where int64_t comparisons could produce incorrect results on MSVC (Windows builds) due to potential 32-bit truncation.

The Problem: Operations like tf.minimum and tf.maximum were not behaving correctly for 64-bit integers in some cases on Windows.

The Fix: This change introduces a patch that specializes the minimum and maximum functors for int64_t on MSVC, ensuring a true 64-bit comparison is always performed. This improves numerical stability and correctness on Windows.

@google-ml-butler google-ml-butler bot added the size:S CL Change Size: Small label Oct 15, 2025
@google-ml-butler google-ml-butler bot added the awaiting review Pull request awaiting review label Oct 15, 2025
@@ -0,0 +1,35 @@
// Platform-specific fix for MSVC 64-bit integer comparison bug in minimum/maximum functors.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Start with the license header

Comment on lines +13 to +33
#if defined(_MSC_VER)
// Specialize maximum for int64_t on MSVC to avoid 32-bit truncation.
template <>
struct maximum<int64_t> {
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE int64_t operator()(const int64_t& x, const int64_t& y) const {
// Use explicit cast to ensure 64-bit comparison
return (static_cast<uint64_t>(x) > static_cast<uint64_t>(y)) ? x : y;
}
};

template <>
struct minimum<int64_t> {
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE int64_t operator()(const int64_t& x, const int64_t& y) const {
// Use explicit cast to ensure 64-bit comparison
return (static_cast<uint64_t>(x) < static_cast<uint64_t>(y)) ? x : y;
}
};
#endif

} // namespace functor
} // namespace tensorflow
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not add these directly to cwise_ops.h?

@keerthanakadiri keerthanakadiri added the comp:core issues related to core part of tensorflow label Oct 17, 2025
@github-project-automation github-project-automation bot moved this to Assigned Reviewer in PR Queue Oct 17, 2025
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:S CL Change Size: Small

Projects

Status: Assigned Reviewer

Development

Successfully merging this pull request may close these issues.

4 participants