Skip to content

Add numeric_limits for MLFloat16 and BFloat16#22197

Merged
tianleiwu merged 5 commits intomainfrom
tlwu/fp16_bf16_limits
Sep 26, 2024
Merged

Add numeric_limits for MLFloat16 and BFloat16#22197
tianleiwu merged 5 commits intomainfrom
tlwu/fp16_bf16_limits

Conversation

@tianleiwu
Copy link
Copy Markdown
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.

@tianleiwu tianleiwu force-pushed the tlwu/fp16_bf16_limits branch from 13e2aa7 to 718e05b Compare September 24, 2024 08:26
snnn
snnn previously approved these changes Sep 24, 2024
Comment thread csharp/src/Microsoft.ML.OnnxRuntime/OrtFloat16.shared.cs Outdated
@yuslepukhin
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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:

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<unsigned __int64>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<unsigned __int64>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
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).

The function 'std::numeric_limits<__int64>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
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<unsigned char>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<unsigned char>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
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<signed char>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<signed char>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).
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<double>::lowest' is constexpr, mark variable 'min_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<double>::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).

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<unsigned char>::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<unsigned char>::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<signed char>::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<signed char>::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<double>::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<double>::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<float>::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).

The function 'std::numeric_limits<float>::max' is constexpr, mark variable 'max_default' constexpr if compile-time evaluation is desired (con.5).
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.

4 participants