-
Notifications
You must be signed in to change notification settings - Fork 15
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
Don't use hipMemcpyDefault
in 2D memcpys due to bugs in HIP
#1106
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b8f856
Don't use hipMemcpyDefault due to bugs in HIP
msimberg 6a908e1
Infer memcpy direction in lacpy for buggy HIP versions
msimberg 0f2ce12
Merge remote-tracking branch 'origin/master' into hip-memcpy-no-default
msimberg 2a54766
Refactor workaround for checking memcpy direction in lacpy
msimberg 1f47ef8
Merge remote-tracking branch 'origin/master' into hip-memcpy-no-default
msimberg 47d6f36
Silence unused variable warnings in lacpy.cu
msimberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,80 @@ __global__ void lacpy(cublasFillMode_t uplo, const unsigned m, const unsigned n, | |
} | ||
} | ||
|
||
static whip::memcpy_kind get_lacpy_memcpy_kind(const void* src, const void* dst) { | ||
// If HIP is version 5.6 or newer, avoid the use of hipMemcpyDefault as it is | ||
// buggy with 2D memcpy. Instead try to infer the memory type using | ||
// hipPointerGetAttributes. See: | ||
// - https://github.com/ROCm/clr/commit/56daa6c4891b43ec233e9c63f755e3f7b45842b4 | ||
// - https://github.com/ROCm/clr/commit/d3bfb55d7a934355257a72fab538a0a634b43cad | ||
// | ||
// Note that hipMemoryTypeManaged is not available in older versions, but it is | ||
// already available in 5.3 so we don't do a separate check for availability. | ||
#if defined(DLAF_WITH_HIP) && HIP_VERSION >= 50600000 | ||
constexpr auto get_memory_type = [](const void* p) { | ||
hipPointerAttribute_t attributes{}; | ||
hipError_t status = hipPointerGetAttributes(&attributes, p); | ||
if (status == hipErrorInvalidValue) { | ||
// If HIP returns hipErrorInvalidValue we assume it's due | ||
// to HIP not recognizing a non-HIP allocated pointer as | ||
// host memory, and we assume the type is host. | ||
return hipMemoryTypeHost; | ||
} | ||
else if (status != hipSuccess) { | ||
throw whip::exception(status); | ||
} | ||
|
||
switch (attributes.type) { | ||
#if HIP_VERSION >= 60000000 | ||
case hipMemoryTypeUnregistered: | ||
[[fallthrough]]; | ||
#endif | ||
case hipMemoryTypeHost: | ||
return hipMemoryTypeHost; | ||
case hipMemoryTypeArray: | ||
[[fallthrough]]; | ||
case hipMemoryTypeDevice: | ||
return hipMemoryTypeDevice; | ||
case hipMemoryTypeUnified: | ||
return hipMemoryTypeUnified; | ||
case hipMemoryTypeManaged: | ||
return hipMemoryTypeManaged; | ||
default: | ||
DLAF_UNREACHABLE_PLAIN; | ||
} | ||
}; | ||
|
||
hipMemoryType src_type = get_memory_type(src); | ||
hipMemoryType dst_type = get_memory_type(dst); | ||
|
||
if (src_type == hipMemoryTypeDevice && dst_type == hipMemoryTypeHost) { | ||
return whip::memcpy_device_to_host; | ||
} | ||
else if (src_type == hipMemoryTypeHost && dst_type == hipMemoryTypeDevice) { | ||
return whip::memcpy_host_to_device; | ||
} | ||
else if (src_type == hipMemoryTypeDevice && dst_type == hipMemoryTypeDevice) { | ||
return whip::memcpy_device_to_device; | ||
} | ||
else if (src_type == hipMemoryTypeManaged || src_type == hipMemoryTypeUnified || | ||
dst_type == hipMemoryTypeManaged || dst_type == hipMemoryTypeUnified) { | ||
return whip::memcpy_default; | ||
} | ||
else if (src_type == hipMemoryTypeHost && dst_type == hipMemoryTypeHost) { | ||
DLAF_ASSERT( | ||
false, | ||
"Attempting to do a HIP lacpy with host source and destination, use the CPU lacpy instead"); | ||
} | ||
else { | ||
DLAF_ASSERT(false, | ||
"Attempting to do a HIP lacpy with unsupported source and destination memory type", | ||
src_type, dst_type); | ||
} | ||
Comment on lines
+122
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've slightly refactored this. It's still a bit of a mess but perhaps passable. I now:
All of the above is only done if HIP is 5.6 or newer. I'm hoping that we can put an upper bound on the workaround maybe with 6.1 or whichever version properly fixes the behaviour. |
||
#endif | ||
|
||
return whip::memcpy_default; | ||
} | ||
|
||
template <class T> | ||
void lacpy(const blas::Uplo uplo, const SizeType m, const SizeType n, const T* a, const SizeType lda, | ||
T* b, const SizeType ldb, const whip::stream_t stream) { | ||
|
@@ -118,7 +192,7 @@ void lacpy(const blas::Uplo uplo, const SizeType m, const SizeType n, const T* a | |
constexpr unsigned kernel_tile_size_cols = kernels::LacpyParams::kernel_tile_size_cols; | ||
|
||
if (uplo == blas::Uplo::General) { | ||
const whip::memcpy_kind kind = whip::memcpy_default; | ||
whip::memcpy_kind kind = get_lacpy_memcpy_kind(a, b); | ||
whip::memcpy_2d_async(b, to_sizet(ldb) * sizeof(T), a, to_sizet(lda) * sizeof(T), | ||
to_sizet(m) * sizeof(T), to_sizet(n), kind, stream); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this assertion is only triggered with HIP 5.6 and newer, but theoretically it's useful also with other versions, including with CUDA. I don't know if it's worth it to always do this assertion though. I don't think it's a big deal to not check it (as we do right now on
master
).