Skip to content

[SYCL][bfloat16] Simplify bfloat16 class #17258

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

Merged
merged 7 commits into from
Mar 3, 2025
Merged

Conversation

bader
Copy link
Contributor

@bader bader commented Mar 1, 2025

This change makes a few changes aiming to make the code more readable.

  • Move declaration of the bfloat16 class right on top of the header file
    and move all declarations with the implementation helpers after the
    class declaration.

  • Converted ConvertToBfloat16 class to namespace. The class doesn't
    represent any object, it's a collection of static methods.

  • Declare public API of the class first.

  • Move Bfloat16StorageT declaration from the detail namespace directly
    into bfloat16 class definition.

  • Co-locate declaration of functions with similar functionality (e.g.
    conversion functions).

  • Simplified uniry operator- implementation by removing additional
    branch for the SPIR target. Generic code for SPIR target produces
    exactly the same code.

  • Added compile time checks for the array size inside conversion helper
    functions. Device side implementation of these functions work
    correctly only for certain array sizes.

  • Include C++ header instead of C (i.e. cstdint instead of stdint.h).

  • Applied "no-else-after-return" and "no-braces-for-single-statement-if"
    coding style changes.

This change makes a few changes aiming to make the code more readable.

* Move declaration of the bfloat16 class right on top of the header file
  and move all declarations with the implementation helpers after the
  class declaration.

* Converted ConvertToBfloat16 class to namespace. The class doesn't
  represent any object, it's a collection of static methods.

* Declare public API of the class first.

* Define bfloat16 class as final.

* Move Bfloat16StorageT declaration from the detail namespace directly
  into bfloat16 class definition.

* Co-locate declaration of functions with similar functionality (e.g.
  conversion functions).

* Simplified uniry operator- implementation by removing additional
  branch for the SPIR target. Generic code for SPIR target produces
  exactly the same code.

* Added compile time checks for the array size inside conversion helper
  functions. Device side implementation of these functions work
  correctly only for certain array sizes.

* Include C++ header instead of C (i.e. cstdint instead of stdint.h).

* Applied "no-else-after-return" and "no-braces-for-single-statement-if"
  coding style changes.
@bader bader requested a review from a team as a code owner March 1, 2025 04:06
@bader bader requested a review from againull March 1, 2025 04:06
@bader bader temporarily deployed to WindowsCILock March 1, 2025 04:06 — with GitHub Actions Inactive
@bader bader temporarily deployed to WindowsCILock March 1, 2025 18:19 — with GitHub Actions Inactive
@bader bader temporarily deployed to WindowsCILock March 1, 2025 18:51 — with GitHub Actions Inactive
@bader bader temporarily deployed to WindowsCILock March 3, 2025 18:30 — with GitHub Actions Inactive
Copy link
Contributor

@cperkinsintel cperkinsintel left a comment

Choose a reason for hiding this comment

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

Nice.

When I saw the title I was hoping that it would also give blfloat16 a constexpr contstructor and operator==, but still a bridge too far.

@bader
Copy link
Contributor Author

bader commented Mar 3, 2025

When I saw the title I was hoping that it would also give blfloat16 a constexpr contstructor and operator==, but still a bridge too far.

That's good feedback. I suggest we apply a "rule of zero" to this class to make constructor constexpr.

Regarding operator==. I'll try to make all binary operators constexpr.

@bader bader temporarily deployed to WindowsCILock March 3, 2025 20:19 — with GitHub Actions Inactive
@bader
Copy link
Contributor Author

bader commented Mar 3, 2025

When I saw the title I was hoping that it would also give blfloat16 a constexpr contstructor and operator==, but still a bridge too far.

That's good feedback. I suggest we apply a "rule of zero" to this class to make constructor constexpr.

Regarding operator==. I'll try to make all binary operators constexpr.

Okay, I think I understand why both these ideas haven't been implemented yet.

  1. We can't apply "rule of zero" because we have defined conversion constructors from float and sycl::half data types.
  2. We can't make operator== constexpr because SPIR-V implementation uses __devicelib_* functions, which aren't constexpr.

Let's leave constexpr improvements for future patches.

I suppose we significantly simplify the implementation by switching to from uint16_t to __bf16 data type for value field. Unfortunately, __bf16 is currently supported only by just a few targets. x86 is the only target which we regularly test, and which supports __bf16 data type.

@bader
Copy link
Contributor Author

bader commented Mar 3, 2025

Pre-commit failures are known issues tracked by #17251, #17026.

One failure on NVIDIA GPU hasn't been reported before, so I opened #17285. It doesn't seem to be related to my change as this test passed for 3adc510 commit, which was the PR state before I moved one line with the code comments.

@bader bader merged commit a8fdb33 into intel:sycl Mar 3, 2025
19 of 21 checks passed
@bader bader deleted the bfloat16-refactor branch March 3, 2025 22: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.

2 participants