Skip to content
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
5 changes: 3 additions & 2 deletions examples/kernels/relu/build.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ rocm-archs = [
"gfx906",
"gfx908",
"gfx90a",
"gfx940",
"gfx941",
"gfx942",
"gfx950",
"gfx1030",
"gfx1100",
"gfx1101",
"gfx1200",
"gfx1201",
]
src = ["relu_cuda/relu.cu"]

Expand Down
58 changes: 41 additions & 17 deletions kernel-builder/src/pyproject/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ fn render_kernel_component(
Kernel::Cpu { .. } => {
render_kernel_component_cpu(env, kernel_name, kernel, sources, write)?
}
Kernel::Cuda { .. } | Kernel::Rocm { .. } => {
Kernel::Cuda { .. } => {
render_kernel_component_cuda(env, kernel_name, kernel, sources, write)?
}
Kernel::Rocm { .. } => {
render_kernel_component_hip(env, kernel_name, kernel, sources, write)?
}
Kernel::Metal { .. } => {
render_kernel_component_metal(env, kernel_name, kernel, sources, write)?
}
Expand Down Expand Up @@ -83,46 +86,67 @@ fn render_kernel_component_cuda(
sources: String,
write: &mut impl Write,
) -> Result<()> {
let (cuda_capabilities, rocm_archs, cuda_flags, hip_flags, cuda_minver) = match kernel {
let (cuda_capabilities, cuda_flags, cuda_minver) = match kernel {
Kernel::Cuda {
cuda_capabilities,
cuda_flags,
cuda_minver,
..
} => (
cuda_capabilities.as_deref(),
None,
cuda_flags.as_deref(),
None,
cuda_minver.as_ref(),
),
Kernel::Rocm {
rocm_archs,
hip_flags,
..
} => (
None,
rocm_archs.as_deref(),
None,
hip_flags.as_deref(),
None,
Comment on lines 96 to -108

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very nice. Way less confusing.

),
_ => unreachable!("Unsupported kernel type for CUDA rendering"),
};

env.get_template("kernel-component/cuda.cmake")
.wrap_err("Cannot get kernel template")?
.render_captured_to(
context! {
name => kernel_name,
cuda_capabilities => cuda_capabilities,
cuda_flags => cuda_flags.map(|flags| flags.join(";")),
cuda_minver => cuda_minver.map(ToString::to_string),
cxx_flags => kernel.cxx_flags().map(|flags| flags.join(";")),
includes => kernel.include().map(prefix_and_join_includes),
kernel_name => kernel_name,
sources => sources,
},
&mut *write,
)
.wrap_err("Cannot render kernel template")?;

write.write_all(b"\n")?;

Ok(())
}

fn render_kernel_component_hip(
env: &Environment,
kernel_name: &str,
kernel: &Kernel,
sources: String,
write: &mut impl Write,
) -> Result<()> {
let (rocm_archs, hip_flags) = match kernel {
Kernel::Rocm {
rocm_archs,
hip_flags,
..
} => (rocm_archs.as_deref(), hip_flags.as_deref()),
_ => unreachable!("Unsupported kernel type for ROCm rendering"),
};

env.get_template("kernel-component/hip.cmake")
.wrap_err("Cannot get kernel template")?
.render_captured_to(
context! {
cxx_flags => kernel.cxx_flags().map(|flags| flags.join(";")),
rocm_archs => rocm_archs,
hip_flags => hip_flags.map(|flags| flags.join(";")),
includes => kernel.include().map(prefix_and_join_includes),
kernel_name => kernel_name,
supports_hipify => matches!(kernel, Kernel::Rocm{ .. }),
name => kernel_name,
sources => sources,
},
&mut *write,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
if(GPU_LANG STREQUAL "CUDA")
cuda_kernel_component(SRC
NAME {{ name }}
SOURCES {{ sources }}
{% if cuda_minver %}CUDA_MINVER {{ cuda_minver }}{% endif %}
{% if includes %}INCLUDES "{{ includes }}"{% endif %}
{% if cuda_capabilities %}CUDA_CAPABILITIES {{ cuda_capabilities|join(" ") }}{% endif %}
{% if cuda_flags %}CUDA_FLAGS "{{ cuda_flags }}"{% endif %}
{% if cxx_flags %}CXX_FLAGS "{{ cxx_flags }}"{% endif %}
{% if supports_hipify %}SUPPORTS_HIPIFY{% endif %}
{% if hip_flags %}HIP_FLAGS "{{ hip_flags }}"{% endif %}
{% if rocm_archs %}ROCM_ARCHS {{ rocm_archs|join(" ") }}{% endif %}
)
endif()
10 changes: 10 additions & 0 deletions kernel-builder/src/pyproject/templates/kernel-component/hip.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if(GPU_LANG STREQUAL "HIP")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice separation.

hip_kernel_component(SRC
NAME {{ name }}
SOURCES {{ sources }}
{% if includes %}INCLUDES "{{ includes }}"{% endif %}
{% if cxx_flags %}CXX_FLAGS "{{ cxx_flags }}"{% endif %}
{% if hip_flags %}HIP_FLAGS "{{ hip_flags }}"{% endif %}
{% if rocm_archs %}ROCM_ARCHS {{ rocm_archs|join(" ") }}{% endif %}
)
endif()
172 changes: 95 additions & 77 deletions kernel-builder/src/pyproject/templates/kernel.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ function(accumulate_gpu_archs OUT_ACC ACC EXTRA_ARCHS)
endfunction()

function(cuda_kernel_component SRC_VAR)
set(options SUPPORTS_HIPIFY)
set(oneValueArgs CUDA_MINVER)
set(multiValueArgs SOURCES INCLUDES CUDA_CAPABILITIES CUDA_FLAGS CXX_FLAGS HIP_FLAGS ROCM_ARCHS)
set(oneValueArgs CUDA_MINVER NAME)
set(multiValueArgs SOURCES INCLUDES CUDA_CAPABILITIES CUDA_FLAGS CXX_FLAGS)
cmake_parse_arguments(KERNEL "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(NOT KERNEL_SOURCES)
Expand All @@ -32,97 +31,116 @@ function(cuda_kernel_component SRC_VAR)
PROPERTIES INCLUDE_DIRECTORIES "${KERNEL_INCLUDES}")
endif()

if(GPU_LANG STREQUAL "CUDA")
# Determine CUDA architectures
if(KERNEL_CUDA_CAPABILITIES)
cuda_archs_loose_intersection(_KERNEL_ARCHS "${KERNEL_CUDA_CAPABILITIES}" "${CUDA_ARCHS}")
else()
set(_KERNEL_ARCHS "${CUDA_KERNEL_ARCHS}")
# Determine CUDA architectures
if(KERNEL_CUDA_CAPABILITIES)
cuda_archs_loose_intersection(_KERNEL_ARCHS "${KERNEL_CUDA_CAPABILITIES}" "${CUDA_ARCHS}")
else()
set(_KERNEL_ARCHS "${CUDA_KERNEL_ARCHS}")
endif()
message(STATUS "CUDA kernel: ${KERNEL_NAME}, capabilities: ${_KERNEL_ARCHS}")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the only real change here, the rest is just moving the code out of the conditional block due to the CUDA/HIP split.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it make sense to split the CUDA and HIP functions into their own scripts and use them here or too much moving around?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I considered that in the big CMake refactor a few months back, but in the end we need all those functions anyway (since the kernel may be multi-backend), so I decided to put them together and do the variable substitution in cuda.cmake, cpu.cmake, xpu.cmake, etc.

set_gencode_flags_for_srcs(SRCS "${_KERNEL_SRC}" CUDA_ARCHS "${_KERNEL_ARCHS}")

accumulate_gpu_archs(_ALL_GPU_ARCHS "${ALL_GPU_ARCHS}" "${_KERNEL_ARCHS}")
set(ALL_GPU_ARCHS ${_ALL_GPU_ARCHS} PARENT_SCOPE)

# Apply CUDA-specific compile flags
if(KERNEL_CUDA_FLAGS)
set(_CUDA_FLAGS "${KERNEL_CUDA_FLAGS}")
# -static-global-template-stub is not supported on CUDA < 12.8. Remove this
# once we don't support CUDA 12.6 anymore.
if(CUDA_VERSION VERSION_LESS 12.8)
string(REGEX REPLACE "-static-global-template-stub=(true|false)" "" _CUDA_FLAGS "${_CUDA_FLAGS}")
endif()
message(STATUS "CUDA kernel capabilities: ${_KERNEL_ARCHS}")
set_gencode_flags_for_srcs(SRCS "${_KERNEL_SRC}" CUDA_ARCHS "${_KERNEL_ARCHS}")

accumulate_gpu_archs(_ALL_GPU_ARCHS "${ALL_GPU_ARCHS}" "${_KERNEL_ARCHS}")
set(ALL_GPU_ARCHS ${_ALL_GPU_ARCHS} PARENT_SCOPE)

# Apply CUDA-specific compile flags
if(KERNEL_CUDA_FLAGS)
set(_CUDA_FLAGS "${KERNEL_CUDA_FLAGS}")
# -static-global-template-stub is not supported on CUDA < 12.8. Remove this
# once we don't support CUDA 12.6 anymore.
if(CUDA_VERSION VERSION_LESS 12.8)
string(REGEX REPLACE "-static-global-template-stub=(true|false)" "" _CUDA_FLAGS "${_CUDA_FLAGS}")

foreach(_SRC ${_KERNEL_SRC})
if(_SRC MATCHES ".*\\.cu$")
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CUDA>:${_CUDA_FLAGS}>"
)
endif()
endforeach()
endif()

foreach(_SRC ${_KERNEL_SRC})
if(_SRC MATCHES ".*\\.cu$")
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CUDA>:${_CUDA_FLAGS}>"
)
endif()
endforeach()
endif()
# Apply CXX-specific compile flags
if(KERNEL_CXX_FLAGS)
foreach(_SRC ${_KERNEL_SRC})
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:${KERNEL_CXX_FLAGS}>"
)
endforeach()
endif()

# Apply CXX-specific compile flags
if(KERNEL_CXX_FLAGS)
foreach(_SRC ${_KERNEL_SRC})
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:${KERNEL_CXX_FLAGS}>"
)
endforeach()
endif()
set(_TMP_SRC ${${SRC_VAR}})
list(APPEND _TMP_SRC ${_KERNEL_SRC})
set(${SRC_VAR} ${_TMP_SRC} PARENT_SCOPE)
endfunction()

set(_TMP_SRC ${${SRC_VAR}})
list(APPEND _TMP_SRC ${_KERNEL_SRC})
set(${SRC_VAR} ${_TMP_SRC} PARENT_SCOPE)
function(hip_kernel_component SRC_VAR)
set(options SUPPORTS_HIPIFY)
set(oneValueArgs CUDA_MINVER NAME)
set(multiValueArgs SOURCES INCLUDES CXX_FLAGS HIP_FLAGS ROCM_ARCHS)
cmake_parse_arguments(KERNEL "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

elseif(GPU_LANG STREQUAL "HIP")
# Apply HIP-specific compile flags
if(KERNEL_HIP_FLAGS)
foreach(_SRC ${_KERNEL_SRC})
if(_SRC MATCHES ".*\\.(cu|hip)$")
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:HIP>:${KERNEL_HIP_FLAGS}>"
)
endif()
endforeach()
endif()
if(NOT KERNEL_SOURCES)
message(FATAL_ERROR "hip_kernel_component: SOURCES argument is required")
endif()

# Determine ROCm architectures
if(KERNEL_ROCM_ARCHS)
hip_archs_loose_intersection(_KERNEL_ARCHS "${KERNEL_ROCM_ARCHS}" "${ROCM_ARCHS}")
else()
set(_KERNEL_ARCHS "${ROCM_ARCHS}")
endif()
message(STATUS "HIP kernel archs: ${_KERNEL_ARCHS}")
set(_KERNEL_SRC ${KERNEL_SOURCES})

accumulate_gpu_archs(_ALL_GPU_ARCHS "${ALL_GPU_ARCHS}" "${_KERNEL_ARCHS}")
set(ALL_GPU_ARCHS ${_ALL_GPU_ARCHS} PARENT_SCOPE)
if(KERNEL_INCLUDES)
# TODO: check if CLion support this:
# https://youtrack.jetbrains.com/issue/CPP-16510/CLion-does-not-handle-per-file-include-directories
set_source_files_properties(
${_KERNEL_SRC}
PROPERTIES INCLUDE_DIRECTORIES "${KERNEL_INCLUDES}")
endif()

# Apply HIP-specific compile flags
if(KERNEL_HIP_FLAGS)
foreach(_SRC ${_KERNEL_SRC})
if(_SRC MATCHES ".*\\.(cu|hip)$")
foreach(_ARCH ${_KERNEL_ARCHS})
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:HIP>:--offload-arch=${_ARCH}>"
)
endforeach()
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:HIP>:${KERNEL_HIP_FLAGS}>"
)
endif()
endforeach()
endif()

set(_TMP_SRC ${${SRC_VAR}})
list(APPEND _TMP_SRC ${_KERNEL_SRC})
set(${SRC_VAR} ${_TMP_SRC} PARENT_SCOPE)
# Determine ROCm architectures
if(KERNEL_ROCM_ARCHS)
hip_archs_loose_intersection(_KERNEL_ARCHS "${KERNEL_ROCM_ARCHS}" "${ROCM_ARCHS}")
else()
set(_KERNEL_ARCHS "${ROCM_ARCHS}")
endif()
message(STATUS "ROCm kernel: ${KERNEL_NAME}, archs: ${_KERNEL_ARCHS}")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the only real change here, the rest is just moving the code out of the conditional block due to the CUDA/HIP split.


accumulate_gpu_archs(_ALL_GPU_ARCHS "${ALL_GPU_ARCHS}" "${_KERNEL_ARCHS}")
set(ALL_GPU_ARCHS ${_ALL_GPU_ARCHS} PARENT_SCOPE)

foreach(_SRC ${_KERNEL_SRC})
if(_SRC MATCHES ".*\\.(cu|hip)$")
foreach(_ARCH ${_KERNEL_ARCHS})
set_property(
SOURCE ${_SRC}
APPEND PROPERTY
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:HIP>:--offload-arch=${_ARCH}>"
)
endforeach()
endif()
endforeach()

set(_TMP_SRC ${${SRC_VAR}})
list(APPEND _TMP_SRC ${_KERNEL_SRC})
set(${SRC_VAR} ${_TMP_SRC} PARENT_SCOPE)
endfunction()


function(xpu_kernel_component SRC_VAR)
set(options)
set(oneValueArgs)
Expand Down
7 changes: 5 additions & 2 deletions kernel-builder/src/pyproject/templates/torch/preamble.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ message(STATUS "FetchContent base directory: ${FETCHCONTENT_BASE_DIR}")

set(HIP_SUPPORTED_ARCHS "gfx906;gfx908;gfx90a;gfx942;gfx950;gfx1030;gfx1100;gfx1101;gfx1200;gfx1201")

# Make Torch CMake machinery happy. Whatever we use does not matter, since
# we set the arches per-file later anyway.
set(ENV{PYTORCH_ROCM_ARCH} "${HIP_SUPPORTED_ARCHS}")

include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/kernel.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/get_gpu_lang.cmake)
Expand Down Expand Up @@ -136,8 +140,7 @@ elseif(GPU_LANG STREQUAL "HIP")
# .hip extension automatically, HIP must be enabled explicitly.
enable_language(HIP)

override_gpu_arches(GPU_ARCHES HIP ${HIP_SUPPORTED_ARCHS})
set(ROCM_ARCHS ${GPU_ARCHES})
set(ROCM_ARCHS ${HIP_SUPPORTED_ARCHS})
message(STATUS "ROCM supported target architectures: ${ROCM_ARCHS}")

# TODO: deprecate one of these settings.
Expand Down
Loading
Loading