Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add numeric_limits for MLFloat16 and BFloat16 #22197

Merged
merged 5 commits into from
Sep 26, 2024
Merged

Conversation

tianleiwu
Copy link
Contributor

@tianleiwu tianleiwu commented Sep 24, 2024

Description

  • Add std::numeric_limits for MLFloat16 and BFloat16.
  • Update some comments in csharp ORTFloat16.shared.cs.
  • Add unit tests (including Clip)

Note that the canonical NaN is not consistent in C++ and C#. C# uses negative quiet NaN as canonical NaN, while C++ uses positive quiet NaN. The choice of CSharp Float16.NaN is to be consistent with System.Half.NaN.

FP16 data returns from CUDA might have 7FFF as NaN; FP16 data from CPU provider might have 0x7E00 as NaN. Anyway there is no consistent canonical NaN in ORT right now. Because all these NaNs are aligned with IEEE spec, there shall not an issue in downstream.

Motivation and Context

std::numeric_limits is used in codebase but not defined for MLFloat16 and BFloat16. It causes some bugs like #21957 introduced by #21493.

snnn
snnn previously approved these changes Sep 24, 2024
@yuslepukhin
Copy link
Member

Do we want to add a test for Clip or this is separate?

yuslepukhin
yuslepukhin previously approved these changes Sep 25, 2024
Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

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

:shipit:

yuslepukhin
yuslepukhin previously approved these changes Sep 25, 2024
Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

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

:shipit:

template <typename T>
struct Clip::ComputeImpl {
void operator()(cudaStream_t stream, const Tensor* X, const Tensor* min, const Tensor* max, Tensor* Y) const {
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
template <typename T>
struct Clip::ComputeImpl {
void operator()(cudaStream_t stream, const Tensor* X, const Tensor* min, const Tensor* max, Tensor* Y) const {
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits<__int64>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits<__int64>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
template <typename T>
struct Clip::ComputeImpl {
void operator()(cudaStream_t stream, const Tensor* X, const Tensor* min, const Tensor* max, Tensor* Y) const {
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
template <typename T>
struct Clip::ComputeImpl {
void operator()(cudaStream_t stream, const Tensor* X, const Tensor* min, const Tensor* max, Tensor* Y) const {
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
template <typename T>
struct Clip::ComputeImpl {
void operator()(cudaStream_t stream, const Tensor* X, const Tensor* min, const Tensor* max, Tensor* Y) const {
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();
auto max_default = std::numeric_limits<T>::max();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits<__int64>::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits<__int64>::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();
auto max_default = std::numeric_limits<T>::max();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();
auto max_default = std::numeric_limits<T>::max();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();
auto max_default = std::numeric_limits<T>::max();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).
auto min_default = clip_internal::LowMax<T>::low();
auto max_default = clip_internal::LowMax<T>::max();
auto min_default = std::numeric_limits<T>::lowest();
auto max_default = std::numeric_limits<T>::max();

Check warning

Code scanning / PREfast

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5). Warning

The function 'std::numeric_limits::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).
@tianleiwu tianleiwu merged commit 7880342 into main Sep 26, 2024
85 checks passed
@tianleiwu tianleiwu deleted the tlwu/fp16_bf16_limits branch September 26, 2024 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants