CUDA/HIP defines overloaded version of host device std::min/max with higher precedence than those in standard C++ headers:

https://github.com/llvm/llvm-project/blob/6ee845d2401b7f0e5f385fc0e3a8cb44afd667dc/clang/lib/Headers/cuda_wrappers/algorithm#L101

https://github.com/gcc-mirror/gcc/blob/d9c90c82d900fdae95df4499bf5f0a4ecb903b53/libstdc%2B%2B-v3/include/bits/stl_algobase.h#L233 

https://github.com/llvm/llvm-project/blob/6ee845d2401b7f0e5f385fc0e3a8cb44afd667dc/libcxx/include/__algorithm/min.h#L36

However, there is a subtle difference of the CUDA/HIP version compared to libstdc++/libc++ version.

CUDA/HIP version is like `__a < __b ? __a : __b`. If there is NaN, it chooese `__b`.

libstdc++/libc++ version is like `__b < __a ? __b : __a`. If there is NaN, it chooses `__a`.

This difference causes annoyance for PyTorch

https://github.com/ROCm/HIP/issues/2209#issuecomment-2141205965

according to https://en.cppreference.com/w/cpp/algorithm/min

 std::min(a,b) returns the smaller of a and b. If the values are equivalent, returns a. It seems libstdc++/libc++ behavior is correct. CUDA/HIP std::min/max needs to be fixed.

@Artem-B 
[CUDA][HIP] std::min/max differs from libstdc++/libc++ about NaN
This is a GitHub Issue
repo:llvm-project
owner:llvm
Title : [CUDA][HIP] std::min/max differs from libstdc++/libc++ about NaN
Issue date:

--- start body ---
CUDA/HIP defines overloaded version of host device std::min/max with higher precedence than those in standard C++ headers:

https://github.com/llvm/llvm-project/blob/6ee845d2401b7f0e5f385fc0e3a8cb44afd667dc/clang/lib/Headers/cuda_wrappers/algorithm#L101

https://github.com/gcc-mirror/gcc/blob/d9c90c82d900fdae95df4499bf5f0a4ecb903b53/libstdc%2B%2B-v3/include/bits/stl_algobase.h#L233 

https://github.com/llvm/llvm-project/blob/6ee845d2401b7f0e5f385fc0e3a8cb44afd667dc/libcxx/include/__algorithm/min.h#L36

However, there is a subtle difference of the CUDA/HIP version compared to libstdc++/libc++ version.

CUDA/HIP version is like `__a < __b ? __a : __b`. If there is NaN, it chooese `__b`.

libstdc++/libc++ version is like `__b < __a ? __b : __a`. If there is NaN, it chooses `__a`.

This difference causes annoyance for PyTorch

https://github.com/ROCm/HIP/issues/2209#issuecomment-2141205965

according to https://en.cppreference.com/w/cpp/algorithm/min

 std::min(a,b) returns the smaller of a and b. If the values are equivalent, returns a. It seems libstdc++/libc++ behavior is correct. CUDA/HIP std::min/max needs to be fixed.

@Artem-B 
--- end body ---
