Skip to content

Commit

Permalink
Merge pull request apache#3 from DongBaiYue/sycl
Browse files Browse the repository at this point in the history
[SYCL] Support auto_scheduler
  • Loading branch information
DongBaiYue authored Jan 4, 2023
2 parents c32e75b + 09f198b commit f55d769
Show file tree
Hide file tree
Showing 15 changed files with 404 additions and 849 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tvm_option(USE_OPENCL "Build with OpenCL" OFF)
tvm_option(USE_OPENCL_GTEST "Path to OpenCL specific gtest version for runtime cpp tests." /path/to/opencl/gtest)
tvm_option(USE_VULKAN "Build with Vulkan" OFF)
tvm_option(USE_SYCL "Build with SYCL" OFF)
tvm_option(SYCL_TEMP_FOLDER "Build with SYCL" "/tmp/tvm_sycl")


# Whether to use spirv-tools.and SPIRV-Headers from Khronos github or gitlab.
Expand Down Expand Up @@ -525,6 +526,9 @@ add_library(tvm_objs OBJECT ${COMPILER_SRCS})
add_library(tvm_runtime_objs OBJECT ${RUNTIME_SRCS})
add_library(tvm_libinfo_objs OBJECT ${LIBINFO_FILE})
if(USE_SYCL)
set(CMAKE_CXX_COMPILER "${USE_SYCL}/bin/clang++")
add_definitions(-DSYCL_CXX_COMPILER="${USE_SYCL}/bin/clang++")
add_definitions(-DSYCL_TEMP_FOLDER="${SYCL_TEMP_FOLDER}")
set(CMAKE_CXX_FLAGS "-fsycl -fsycl-targets=nvptx64-nvidia-cuda ${CMAKE_CXX_FLAGS}")
add_library(tvm_sycl_runtime_objs OBJECT ${SYCL_RUNTIME_SRCS})
add_library(tvm SHARED $<TARGET_OBJECTS:tvm_objs> $<TARGET_OBJECTS:tvm_runtime_objs> $<TARGET_OBJECTS:tvm_sycl_runtime_objs> $<TARGET_OBJECTS:tvm_libinfo_objs>)
Expand Down
4 changes: 3 additions & 1 deletion cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ set(USE_AOCL OFF)
# Whether enable SYCL runtime
#
# Possible values:
# - ON: enable SYCL with cmake's auto search
# - ON: enable SYCL with cmake's auto search, not support now.
# - OFF: disable SYCL
# - /path/to/sycl: use specific path to sycl
set(USE_SYCL OFF)
# if enable SYCL runtime, please set storage path for sycl temporary files. The default path is "/tmp/tvm_sycl/".
set(SYCL_TEMP_FOLDER "/tmp/tvm_sycl")

# Whether enable OpenCL runtime
#
Expand Down
4 changes: 2 additions & 2 deletions cmake/modules/SYCL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ if(USE_SYCL)
tvm_file_glob(GLOB RUNTIME_SYCL_SRCS src/runtime/sycl/*.cc)
list(APPEND SYCL_RUNTIME_SRCS ${RUNTIME_SYCL_SRCS})

include_directories(BEFORE SYSTEM $ENV{SYCL_ROOT_DIR}/include/sycl/)
list(APPEND TVM_RUNTIME_LINKER_LIBS $ENV{SYCL_ROOT_DIR}/lib/)
include_directories(BEFORE SYSTEM ${USE_SYCL}/include/sycl/)
list(APPEND TVM_RUNTIME_LINKER_LIBS ${USE_SYCL}/lib/)
else()
list(APPEND COMPILER_SRCS src/target/opt/build_sycl_off.cc)
endif(USE_SYCL)
9 changes: 9 additions & 0 deletions python/tvm/auto_scheduler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ def call_func_with_timeout(
worker.send(func, args, kwargs, timeout)
try:
res = worker.recv()
except ChildProcessError:
# when a sycl runtime error occurs in the child process used for measure, the child process is terminated.
# Restart measure the previous schedule.
print("Restart measure the previous schedule.")
worker.send(func, args, kwargs, timeout)
try:
res = worker.recv()
except Exception: # pylint: disable=broad-except
res = Exception(make_traceback_info())
except Exception: # pylint: disable=broad-except
res = Exception(make_traceback_info())

Expand Down
6 changes: 5 additions & 1 deletion python/tvm/autotvm/tophub.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ def context(target, extra_files=None):
device = tgt.attrs.get("device", "")
if device != "":
possible_names.append(_alias(device))
possible_names.append(tgt.kind.name)
# use opencl pre-tuned log when target=sycl, because lack of sycl pre-tuned log
if(tgt.kind.name == "sycl"):
possible_names.append("opencl")
else:
possible_names.append(tgt.kind.name)

all_packages = list(PACKAGE_VERSION.keys())
for name in possible_names:
Expand Down
7 changes: 6 additions & 1 deletion src/auto_scheduler/search_policy/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ inline bool IsGPUTask(const SearchTask& task) {
(task)->target->kind->device_type == kDLVulkan ||
(task)->target->kind->device_type == kDLMetal ||
(task)->target->kind->device_type == kDLROCM ||
(task)->target->kind->device_type == kOpenGL;
(task)->target->kind->device_type == kOpenGL ||
(task)->target->kind->device_type == kDLSYCL;
}

/*! \brief Return whether the search task is targeting a CUDA GPU. */
Expand All @@ -70,6 +71,10 @@ inline bool IsCUDATask(const SearchTask& task) {
inline bool IsOpenCLTask(const SearchTask& task) {
return (task)->target->kind->device_type == kDLOpenCL;
}
/*! \brief Return whether the search task is targeting a SYCL GPU. */
inline bool IsSYCLTask(const SearchTask& task) {
return (task)->target->kind->device_type == kDLSYCL;
}

/*! \brief Argsort. Order: largest to smallest */
template <typename T>
Expand Down
27 changes: 27 additions & 0 deletions src/auto_scheduler/search_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,33 @@ HardwareParams HardwareParamsNode::GetDefaultHardwareParams(const Target& target

return HardwareParams(-1, 16, 64, max_shared_memory_per_block, max_local_memory_per_block,
max_threads_per_block, max_vthread_extent, warp_size);
} else if (device_type == kDLSYCL) {
auto dev = Device{static_cast<DLDeviceType>(device_type), 0};
auto device_name = "device_api.sycl";
auto func = tvm::runtime::Registry::Get(device_name);
ICHECK(func != nullptr) << "Cannot find SYCL device_api in registry";
auto device_api = static_cast<tvm::runtime::DeviceAPI*>(((*func)()).operator void*());

tvm::runtime::TVMRetValue ret;

//device_api->GetAttr(dev, tvm::runtime::DeviceAttrKind::kCacheLineSize, &ret);
//int cache_line_size = ret;

device_api->GetAttr(dev, tvm::runtime::DeviceAttrKind::kMaxSharedMemoryPerBlock, &ret);
int max_shared_memory_per_block = ret;

int max_local_memory_per_block = INT32_MAX;

device_api->GetAttr(dev, tvm::runtime::DeviceAttrKind::kMaxThreadsPerBlock, &ret);
int max_threads_per_block = ret;

device_api->GetAttr(dev, tvm::runtime::DeviceAttrKind::kWarpSize, &ret);
int warp_size = ret;

int max_vthread_extent = std::max(1, warp_size / 4);
return HardwareParams(-1, 16, 64, max_shared_memory_per_block, max_local_memory_per_block,
max_threads_per_block, max_vthread_extent, warp_size);

} else {
LOG(FATAL) << "No default hardware parameters for target: " << target;
}
Expand Down
7 changes: 0 additions & 7 deletions src/runtime/opencl/opencl_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,6 @@ void OpenCLModuleNode::Init() {
// zero initialize cl_program pointers for each device kernel
for (auto& kv : parsed_kernels_) {
programs_.insert({kv.first, std::vector<cl_program>(workspace_->devices.size(), nullptr)});
std::string kernel_name = kv.first;
std::string kernel_code = kv.second;
std::string filepath = "/tmp/tvm_sycl/"+kernel_name+"_opencl.cpp";
std::ofstream myfile;
myfile.open(filepath);
myfile << kernel_code;
myfile.close();
}
}

Expand Down
Loading

0 comments on commit f55d769

Please sign in to comment.