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

Half base type #1706

Merged
merged 13 commits into from
Dec 3, 2024
Merged

Half base type #1706

merged 13 commits into from
Dec 3, 2024

Conversation

yhmtsai
Copy link
Member

@yhmtsai yhmtsai commented Oct 22, 2024

This pr moves the half out of extended_float.hpp with some modification and half is available in the public interface.

Currently, we can still mark some conversion/operation with GKO_ATTRIBUTE and GKO_INLINE.
However, I think it is good to ensure we only use the vendor's impl on device side.
To achieve it, Jacobi needs to use __half not half now.
This also removes the undefined behavior from reinterpret_cast to std::memcpy

one question: should we mark the half precision in experimental namespace? in usual interface from discussion

@yhmtsai yhmtsai added the 1:ST:ready-for-review This PR is ready for review label Oct 22, 2024
@yhmtsai yhmtsai self-assigned this Oct 22, 2024
@ginkgo-bot ginkgo-bot added reg:build This is related to the build system. reg:testing This is related to testing. mod:core This is related to the core module. mod:cuda This is related to the CUDA module. type:preconditioner This is related to the preconditioners mod:hip This is related to the HIP module. labels Oct 22, 2024
@yhmtsai yhmtsai force-pushed the half_type branch 2 times, most recently from 265cb47 to b0c488b Compare October 22, 2024 16:06
@yhmtsai yhmtsai added 1:ST:WIP This PR is a work in progress. Not ready for review. and removed 1:ST:ready-for-review This PR is ready for review labels Oct 22, 2024
@yhmtsai yhmtsai force-pushed the half_type branch 4 times, most recently from 8bd8d1c to 2ab1acd Compare October 23, 2024 12:45
@yhmtsai yhmtsai added 1:ST:ready-for-review This PR is ready for review and removed 1:ST:WIP This PR is a work in progress. Not ready for review. labels Oct 23, 2024
@yhmtsai yhmtsai added this to the Ginkgo 1.9.0 milestone Oct 24, 2024
@MarcelKoch MarcelKoch self-requested a review October 24, 2024 08:14
Copy link
Member

@MarcelKoch MarcelKoch left a comment

Choose a reason for hiding this comment

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

Just to reiterate, I'm in favor of putting this directly into the gko namespace.

@yhmtsai
Copy link
Member Author

yhmtsai commented Oct 24, 2024

@MarcelKoch I have made the half without types.hpp dependence such that we can include it directly without circular dependence and the instantiate function have the definition of half now

@yhmtsai yhmtsai force-pushed the half_type branch 2 times, most recently from 7eaf547 to d4f3893 Compare November 5, 2024 18:03
@yhmtsai yhmtsai force-pushed the half_type branch 2 times, most recently from f924ff5 to 5a7ae0c Compare November 18, 2024 11:16
Copy link
Member

@upsj upsj left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -17,7 +17,15 @@
#include "utils.hpp"


struct __half;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's a way around the issue, but we should be aware that __half is a reserved identifier according to the C++ standard, so we should technically not be defining anything with that name. Also there might be a tiny potential of name mangling issues if this is ever handled as a class instead of a struct with MSVC.

Comment on lines +30 to +32
half x = create_from_bits("0" "01111111" "00000000000000000000000");

ASSERT_EQ(get_bits(x), get_bits("0" "01111" "0000000000"));
Copy link
Member

Choose a reason for hiding this comment

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

FYI, since C++14 we can use binary literals 0b01111111


template <>
struct culibs_type_impl<std::complex<half>> {
using type = __half2;
Copy link
Member

Choose a reason for hiding this comment

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

Just for my understanding, __half2 is a vector type derived from __half?

Copy link
Member Author

Choose a reason for hiding this comment

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

you are right. only one function __hcmadd does complex operation, but the other are all vector type operation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have check cusparse they use __half2 to hold complex<__half>, too. https://docs.nvidia.com/cuda/cusparse/#cudadatatype-t

# GDB doesn't seem to consider the user-defined conversion in its Value.cast,
# so we need to call the conversion operator explicitly
address = hex(val.address)
self.float_val = gdb.parse_and_eval(f"reinterpret_cast<gko::half*>({address})->operator float()")
Copy link
Member

Choose a reason for hiding this comment

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

If that function is ever not exported, we may need to implement an xfunction for it

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not familar with that. This is done by @MarcelKoch
could you implement that?

Copy link
Member

Choose a reason for hiding this comment

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

We likely don't need it now, just for future reference

@yhmtsai yhmtsai force-pushed the half_type branch 3 times, most recently from 8cc9759 to 0fa6dc9 Compare November 28, 2024 18:11
@yhmtsai yhmtsai added 1:ST:ready-to-merge This PR is ready to merge. 1:ST:skip-full-test and removed 1:ST:ready-for-review This PR is ready for review labels Dec 3, 2024
@yhmtsai yhmtsai merged commit a144043 into develop Dec 3, 2024
10 of 11 checks passed
@yhmtsai yhmtsai deleted the half_type branch December 3, 2024 01:08
yhmtsai added a commit that referenced this pull request Dec 3, 2024
This PR implements the half precision for matrices and components

Related PR: #1706
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1:ST:ready-to-merge This PR is ready to merge. 1:ST:skip-full-test mod:core This is related to the core module. mod:cuda This is related to the CUDA module. mod:hip This is related to the HIP module. reg:build This is related to the build system. reg:testing This is related to testing. type:preconditioner This is related to the preconditioners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants