Skip to content

[HIP] Handle required wg size attribute in HIP #954

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 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions source/adapters/cuda/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===----------------------------------------------------------------------===//

#include "program.hpp"
#include "ur_util.hpp"

bool getMaxRegistersJitOptionValue(const std::string &BuildOptions,
unsigned int &Value) {
Expand Down Expand Up @@ -52,15 +53,6 @@ ur_program_handle_t_::ur_program_handle_t_(ur_context_handle_t Context)

ur_program_handle_t_::~ur_program_handle_t_() { urContextRelease(Context); }

std::pair<std::string, std::string>
splitMetadataName(const std::string &metadataName) {
size_t splitPos = metadataName.rfind('@');
if (splitPos == std::string::npos)
return std::make_pair(metadataName, std::string{});
return std::make_pair(metadataName.substr(0, splitPos),
metadataName.substr(splitPos, metadataName.length()));
}

ur_result_t
ur_program_handle_t_::setMetadata(const ur_program_metadata_t *Metadata,
size_t Length) {
Expand Down
4 changes: 4 additions & 0 deletions source/adapters/hip/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1850,10 +1850,14 @@ setKernelParams(const ur_device_handle_t Device, const uint32_t WorkDim,
static_cast<size_t>(Device->getMaxBlockDimY()),
static_cast<size_t>(Device->getMaxBlockDimZ())};

auto &ReqdThreadsPerBlock = Kernel->ReqdThreadsPerBlock;
MaxWorkGroupSize = Device->getMaxWorkGroupSize();

if (LocalWorkSize != nullptr) {
auto isValid = [&](int dim) {
UR_ASSERT(ReqdThreadsPerBlock[dim] == 0 ||
LocalWorkSize[dim] == ReqdThreadsPerBlock[dim],
UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE);
UR_ASSERT(LocalWorkSize[dim] <= MaxThreadsPerBlock[dim],
UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE);
// Checks that local work sizes are a divisor of the global work sizes
Expand Down
19 changes: 11 additions & 8 deletions source/adapters/hip/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,17 @@ urKernelGetGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
return ReturnValue(size_t(MaxThreads));
}
case UR_KERNEL_GROUP_INFO_COMPILE_WORK_GROUP_SIZE: {
size_t group_size[3] = {0, 0, 0};
// Returns the work-group size specified in the kernel source or IL.
// If the work-group size is not specified in the kernel source or IL,
// (0, 0, 0) is returned.
// https://www.khronos.org/registry/OpenCL/sdk/2.1/docs/man/xhtml/clGetKernelWorkGroupInfo.html

// TODO: can we extract the work group size from the PTX?
return ReturnValue(group_size, 3);
size_t GroupSize[3] = {0, 0, 0};
const auto &ReqdWGSizeMDMap =
hKernel->getProgram()->KernelReqdWorkGroupSizeMD;
const auto ReqdWGSizeMD = ReqdWGSizeMDMap.find(hKernel->getName());
if (ReqdWGSizeMD != ReqdWGSizeMDMap.end()) {
const auto ReqdWGSize = ReqdWGSizeMD->second;
GroupSize[0] = std::get<0>(ReqdWGSize);
GroupSize[1] = std::get<1>(ReqdWGSize);
GroupSize[2] = std::get<2>(ReqdWGSize);
}
return ReturnValue(GroupSize, 3);
}
case UR_KERNEL_GROUP_INFO_LOCAL_MEM_SIZE: {
// OpenCL LOCAL == HIP SHARED
Expand Down
8 changes: 8 additions & 0 deletions source/adapters/hip/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ struct ur_kernel_handle_t_ {
ur_program_handle_t Program;
std::atomic_uint32_t RefCount;

static constexpr uint32_t ReqdThreadsPerBlockDimensions = 3u;
size_t ReqdThreadsPerBlock[ReqdThreadsPerBlockDimensions];

/// Structure that holds the arguments to the kernel.
/// Note earch argument size is known, since it comes
/// from the kernel signature.
Expand Down Expand Up @@ -154,6 +157,11 @@ struct ur_kernel_handle_t_ {
ur_context_handle_t Ctxt)
: Function{Func}, FunctionWithOffsetParam{FuncWithOffsetParam},
Name{Name}, Context{Ctxt}, Program{Program}, RefCount{1} {
assert(Program->getDevice());
UR_CHECK_ERROR(urKernelGetGroupInfo(
this, Program->getDevice(),
UR_KERNEL_GROUP_INFO_COMPILE_WORK_GROUP_SIZE,
sizeof(ReqdThreadsPerBlock), ReqdThreadsPerBlock, nullptr));
urProgramRetain(Program);
urContextRetain(Context);
}
Expand Down
35 changes: 23 additions & 12 deletions source/adapters/hip/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===----------------------------------------------------------------------===//

#include "program.hpp"
#include "ur_util.hpp"

#ifdef SYCL_ENABLE_KERNEL_FUSION
#ifdef UR_COMGR_VERSION4_INCLUDE
Expand Down Expand Up @@ -78,15 +79,6 @@ void getCoMgrBuildLog(const amd_comgr_data_set_t BuildDataSet, char *BuildLog,
} // namespace
#endif

std::pair<std::string, std::string>
splitMetadataName(const std::string &metadataName) {
size_t splitPos = metadataName.rfind('@');
if (splitPos == std::string::npos)
return std::make_pair(metadataName, std::string{});
return std::make_pair(metadataName.substr(0, splitPos),
metadataName.substr(splitPos, metadataName.length()));
}

ur_result_t
ur_program_handle_t_::setMetadata(const ur_program_metadata_t *Metadata,
size_t Length) {
Expand All @@ -107,8 +99,29 @@ ur_program_handle_t_::setMetadata(const ur_program_metadata_t *Metadata,
const char *MetadataValPtrEnd =
MetadataValPtr + MetadataElement.size - sizeof(std::uint64_t);
GlobalIDMD[Prefix] = std::string{MetadataValPtr, MetadataValPtrEnd};
} else if (Tag == __SYCL_UR_PROGRAM_METADATA_TAG_REQD_WORK_GROUP_SIZE) {
// If metadata is reqd_work_group_size, record it for the corresponding
// kernel name.
size_t MDElemsSize = MetadataElement.size - sizeof(std::uint64_t);

// Expect between 1 and 3 32-bit integer values.
UR_ASSERT(MDElemsSize >= sizeof(std::uint32_t) &&
MDElemsSize <= sizeof(std::uint32_t) * 3,
UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE);

// Get pointer to data, skipping 64-bit size at the start of the data.
const char *ValuePtr =
reinterpret_cast<const char *>(MetadataElement.value.pData) +
sizeof(std::uint64_t);
// Read values and pad with 1's for values not present.
std::uint32_t ReqdWorkGroupElements[] = {1, 1, 1};
std::memcpy(ReqdWorkGroupElements, ValuePtr, MDElemsSize);
KernelReqdWorkGroupSizeMD[Prefix] =
std::make_tuple(ReqdWorkGroupElements[0], ReqdWorkGroupElements[1],
ReqdWorkGroupElements[2]);
}
}

return UR_RESULT_SUCCESS;
}

Expand Down Expand Up @@ -459,8 +472,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
std::unique_ptr<ur_program_handle_t_> RetProgram{
new ur_program_handle_t_{hContext, hDevice}};

// TODO: Set metadata here and use reqd_work_group_size information.
// See urProgramCreateWithBinary in CUDA adapter.
if (pProperties) {
if (pProperties->count > 0 && pProperties->pMetadatas == nullptr) {
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
Expand All @@ -469,8 +480,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
}
Result =
RetProgram->setMetadata(pProperties->pMetadatas, pProperties->count);
UR_ASSERT(Result == UR_RESULT_SUCCESS, Result);
}
UR_ASSERT(Result == UR_RESULT_SUCCESS, Result);

auto pBinary_string = reinterpret_cast<const char *>(pBinary);
if (size == 0) {
Expand Down
7 changes: 5 additions & 2 deletions source/adapters/hip/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <ur_api.h>

#include <atomic>
#include <unordered_map>

#include "context.hpp"

Expand All @@ -30,6 +31,8 @@ struct ur_program_handle_t_ {
bool IsRelocatable = false;

std::unordered_map<std::string, std::string> GlobalIDMD;
std::unordered_map<std::string, std::tuple<uint32_t, uint32_t, uint32_t>>
KernelReqdWorkGroupSizeMD;

constexpr static size_t MAX_LOG_SIZE = 8192u;

Expand All @@ -38,8 +41,8 @@ struct ur_program_handle_t_ {
ur_program_build_status_t BuildStatus = UR_PROGRAM_BUILD_STATUS_NONE;

ur_program_handle_t_(ur_context_handle_t Ctxt, ur_device_handle_t Device)
: Module{nullptr}, Binary{},
BinarySizeInBytes{0}, RefCount{1}, Context{Ctxt}, Device{Device} {
: Module{nullptr}, Binary{}, BinarySizeInBytes{0}, RefCount{1},
Context{Ctxt}, Device{Device}, KernelReqdWorkGroupSizeMD{} {
urContextRetain(Context);
urDeviceRetain(Device);
}
Expand Down
11 changes: 1 addition & 10 deletions source/adapters/native_cpu/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ur_api.h"

#include "common.hpp"
#include "common/ur_util.hpp"
#include "program.hpp"
#include <cstdint>

Expand All @@ -27,16 +28,6 @@ urProgramCreateWithIL(ur_context_handle_t hContext, const void *pIL,
DIE_NO_IMPLEMENTATION
}

// TODO: taken from CUDA adapter, move this to a common header?
static std::pair<std::string, std::string>
splitMetadataName(const std::string &metadataName) {
size_t splitPos = metadataName.rfind('@');
if (splitPos == std::string::npos)
return std::make_pair(metadataName, std::string{});
return std::make_pair(metadataName.substr(0, splitPos),
metadataName.substr(splitPos, metadataName.length()));
}

static ur_result_t getReqdWGSize(const ur_program_metadata_t &MetadataElement,
native_cpu::ReqdWGSize_t &res) {
size_t MDElemsSize = MetadataElement.size - sizeof(std::uint64_t);
Expand Down
9 changes: 9 additions & 0 deletions source/common/ur_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,13 @@ namespace ur {
}
} // namespace ur

inline std::pair<std::string, std::string>
splitMetadataName(const std::string &metadataName) {
size_t splitPos = metadataName.rfind('@');
if (splitPos == std::string::npos) {
return std::make_pair(metadataName, std::string{});
}
return std::make_pair(metadataName.substr(0, splitPos),
metadataName.substr(splitPos, metadataName.length()));
}
#endif /* UR_UTIL_H */