diff --git a/build2cmake/src/templates/cpu/torch-binding.cmake b/build2cmake/src/templates/cpu/torch-binding.cmake deleted file mode 100644 index 799fa0d3..00000000 --- a/build2cmake/src/templates/cpu/torch-binding.cmake +++ /dev/null @@ -1,13 +0,0 @@ -set(TORCH_{{name}}_SRC - {{ src|join(' ') }} -) - -{% if 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( - {{'${TORCH_' + name + '_SRC}'}} - PROPERTIES INCLUDE_DIRECTORIES "{{ includes }}") -{% endif %} - -list(APPEND SRC {{'"${TORCH_' + name + '_SRC}"'}}) diff --git a/build2cmake/src/templates/cpu/torch-extension.cmake b/build2cmake/src/templates/cpu/torch-extension.cmake deleted file mode 100644 index 9d667e4a..00000000 --- a/build2cmake/src/templates/cpu/torch-extension.cmake +++ /dev/null @@ -1,9 +0,0 @@ -define_gpu_extension_target( - {{ ops_name }} - DESTINATION {{ ops_name }} - LANGUAGE ${GPU_LANG} - SOURCES ${SRC} - COMPILE_FLAGS ${GPU_FLAGS} - ARCHITECTURES ${GPU_ARCHES} - USE_SABI 3 - WITH_SOABI) diff --git a/build2cmake/src/templates/cuda/torch-binding.cmake b/build2cmake/src/templates/cuda/torch-binding.cmake deleted file mode 100644 index 5c1c0002..00000000 --- a/build2cmake/src/templates/cuda/torch-binding.cmake +++ /dev/null @@ -1,16 +0,0 @@ -get_torch_gpu_compiler_flags(TORCH_GPU_FLAGS ${GPU_LANG}) -list(APPEND GPU_FLAGS ${TORCH_GPU_FLAGS}) - -set(TORCH_{{name}}_SRC - {{ src|join(' ') }} -) - -{% if 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( - {{'${TORCH_' + name + '_SRC}'}} - PROPERTIES INCLUDE_DIRECTORIES "{{ includes }}") -{% endif %} - -list(APPEND SRC {{'"${TORCH_' + name + '_SRC}"'}}) diff --git a/build2cmake/src/templates/cuda/torch-extension.cmake b/build2cmake/src/templates/cuda/torch-extension.cmake deleted file mode 100644 index 5b934e10..00000000 --- a/build2cmake/src/templates/cuda/torch-extension.cmake +++ /dev/null @@ -1,25 +0,0 @@ -define_gpu_extension_target( - {{ ops_name }} - DESTINATION {{ ops_name }} - LANGUAGE ${GPU_LANG} - SOURCES ${SRC} - COMPILE_FLAGS ${GPU_FLAGS} - ARCHITECTURES ${GPU_ARCHES} - #INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR} - USE_SABI 3 - WITH_SOABI) - -if( NOT MSVC) - target_link_options({{ ops_name }} PRIVATE -static-libstdc++) -endif() - -{% if platform == 'windows' %} -# These methods below should be included from preamble.cmake on windows platform. - -# Add kernels_install target for huggingface/kernels library layout -add_kernels_install_target({{ ops_name }} "{{ name }}" "${BUILD_VARIANT_NAME}") - -# Add local_install target for local development with get_local_kernel() -add_local_install_target({{ ops_name }} "{{ name }}" "${BUILD_VARIANT_NAME}") - -{% endif %} diff --git a/build2cmake/src/templates/metal/torch-extension.cmake b/build2cmake/src/templates/metal/torch-extension.cmake deleted file mode 100644 index 9e9fa7aa..00000000 --- a/build2cmake/src/templates/metal/torch-extension.cmake +++ /dev/null @@ -1,17 +0,0 @@ -# Include Metal shader compilation utilities -include(${CMAKE_CURRENT_LIST_DIR}/cmake/compile-metal.cmake) - -define_gpu_extension_target( - {{ ops_name }} - DESTINATION {{ ops_name }} - LANGUAGE ${GPU_LANG} - SOURCES ${SRC} - COMPILE_FLAGS ${GPU_FLAGS} - ARCHITECTURES ${GPU_ARCHES} - USE_SABI 3 - WITH_SOABI) - -# Compile Metal shaders if any were found -if(ALL_METAL_SOURCES) - compile_metal_shaders({{ ops_name }} "${ALL_METAL_SOURCES}" "${METAL_INCLUDE_DIRS}") -endif() \ No newline at end of file diff --git a/build2cmake/src/templates/metal/torch-binding.cmake b/build2cmake/src/templates/torch-binding.cmake similarity index 73% rename from build2cmake/src/templates/metal/torch-binding.cmake rename to build2cmake/src/templates/torch-binding.cmake index 79872f04..93e47325 100644 --- a/build2cmake/src/templates/metal/torch-binding.cmake +++ b/build2cmake/src/templates/torch-binding.cmake @@ -1,5 +1,7 @@ -#get_torch_gpu_compiler_flags(TORCH_GPU_FLAGS ${GPU_LANG}) -#list(APPEND GPU_FLAGS ${TORCH_GPU_FLAGS}) +if(GPU_LANG STREQUAL "CUDA") + get_torch_gpu_compiler_flags(TORCH_GPU_FLAGS ${GPU_LANG}) + list(APPEND GPU_FLAGS ${TORCH_GPU_FLAGS}) +endif() set(TORCH_{{name}}_SRC {{ src|join(' ') }} diff --git a/build2cmake/src/templates/torch-extension.cmake b/build2cmake/src/templates/torch-extension.cmake new file mode 100644 index 00000000..2fdd220c --- /dev/null +++ b/build2cmake/src/templates/torch-extension.cmake @@ -0,0 +1,39 @@ +# Include Metal shader compilation utilities if needed +if(GPU_LANG STREQUAL "METAL") + include(${CMAKE_CURRENT_LIST_DIR}/cmake/compile-metal.cmake) +endif() + +# Define the extension target with unified parameters +define_gpu_extension_target( + {{ ops_name }} + DESTINATION {{ ops_name }} + LANGUAGE ${GPU_LANG} + SOURCES ${SRC} + COMPILE_FLAGS ${GPU_FLAGS} + ARCHITECTURES ${GPU_ARCHES} + USE_SABI 3 + WITH_SOABI) + +if(NOT (MSVC OR GPU_LANG STREQUAL "SYCL")) + target_link_options({{ ops_name }} PRIVATE -static-libstdc++) +endif() + +if(GPU_LANG STREQUAL "SYCL") + target_link_options({{ ops_name }} PRIVATE ${sycl_link_flags}) + target_link_libraries({{ ops_name }} PRIVATE dnnl) +endif() + +# Compile Metal shaders if any were found +if(GPU_LANG STREQUAL "METAL") + if(ALL_METAL_SOURCES) + compile_metal_shaders({{ ops_name }} "${ALL_METAL_SOURCES}" "${METAL_INCLUDE_DIRS}") + endif() +endif() + +{% if platform == 'windows' %} +# Add kernels_install target for huggingface/kernels library layout +add_kernels_install_target({{ ops_name }} "{{ name }}" "${BUILD_VARIANT_NAME}") + +# Add local_install target for local development with get_local_kernel() +add_local_install_target({{ ops_name }} "{{ name }}" "${BUILD_VARIANT_NAME}") +{% endif %} diff --git a/build2cmake/src/templates/xpu/preamble.cmake b/build2cmake/src/templates/xpu/preamble.cmake index aae15071..1687264a 100644 --- a/build2cmake/src/templates/xpu/preamble.cmake +++ b/build2cmake/src/templates/xpu/preamble.cmake @@ -79,7 +79,8 @@ add_compile_definitions(USE_XPU) # Set SYCL-specific flags # Set comprehensive SYCL compilation and linking flags set(sycl_link_flags "-fsycl;--offload-compress;-fsycl-targets=spir64_gen,spir64;-Xs;-device pvc,xe-lpg,ats-m150 -options ' -cl-intel-enable-auto-large-GRF-mode -cl-poison-unsupported-fp64-kernels -cl-intel-greater-than-4GB-buffer-required';") -set(sycl_flags "-fsycl;-fhonor-nans;-fhonor-infinities;-fno-associative-math;-fno-approx-func;-fno-sycl-instrument-device-code;--offload-compress;-fsycl-targets=spir64_gen,spir64;") +set(GPU_FLAGS "-fsycl;-fhonor-nans;-fhonor-infinities;-fno-associative-math;-fno-approx-func;-fno-sycl-instrument-device-code;--offload-compress;-fsycl-targets=spir64_gen,spir64;") +set(GPU_ARCHES "") message(STATUS "Configuring for Intel XPU backend using SYCL") {% if platform == 'windows' %} diff --git a/build2cmake/src/templates/xpu/torch-binding.cmake b/build2cmake/src/templates/xpu/torch-binding.cmake deleted file mode 100644 index 8812c529..00000000 --- a/build2cmake/src/templates/xpu/torch-binding.cmake +++ /dev/null @@ -1,13 +0,0 @@ -set(TORCH_{{name}}_SRC - {{ src|join(' ') }} -) - -{% if 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( - {{'${TORCH_' + name + '_SRC}'}} - PROPERTIES INCLUDE_DIRECTORIES "{{ includes }}") -{% endif %} - -list(APPEND SRC {{'"${TORCH_' + name + '_SRC}"'}}) \ No newline at end of file diff --git a/build2cmake/src/templates/xpu/torch-extension.cmake b/build2cmake/src/templates/xpu/torch-extension.cmake deleted file mode 100644 index ae7a69aa..00000000 --- a/build2cmake/src/templates/xpu/torch-extension.cmake +++ /dev/null @@ -1,23 +0,0 @@ -define_gpu_extension_target( - {{ ops_name }} - DESTINATION {{ ops_name }} - LANGUAGE ${GPU_LANG} - SOURCES ${SRC} - COMPILE_FLAGS ${sycl_flags} - USE_SABI 3 - WITH_SOABI) - -# Add XPU/SYCL specific linker flags -target_link_options({{ ops_name }} PRIVATE ${sycl_link_flags}) -target_link_libraries({{ ops_name }} PRIVATE dnnl) - -{% if platform == 'windows' %} -# These methods below should be included from preamble.cmake on windows platform. - -# Add kernels_install target for huggingface/kernels library layout -add_kernels_install_target({{ ops_name }} "{{ name }}" "${BUILD_VARIANT_NAME}") - -# Add local_install target for local development with get_local_kernel() -add_local_install_target({{ ops_name }} "{{ name }}" "${BUILD_VARIANT_NAME}") - -{% endif %} \ No newline at end of file diff --git a/build2cmake/src/torch/common.rs b/build2cmake/src/torch/common.rs index c5eb162d..08200340 100644 --- a/build2cmake/src/torch/common.rs +++ b/build2cmake/src/torch/common.rs @@ -1,8 +1,10 @@ +use std::io::Write; + use eyre::{Context, Result}; use itertools::Itertools; use minijinja::{context, Environment}; -use crate::config::{Backend, General}; +use crate::config::{Backend, General, Torch}; use crate::metadata::Metadata; use crate::FileSet; @@ -64,3 +66,49 @@ where .collect_vec() .join(";") } + +pub fn render_binding( + env: &Environment, + torch: &Torch, + name: &str, + write: &mut impl Write, +) -> Result<()> { + env.get_template("torch-binding.cmake") + .wrap_err("Cannot get Torch binding template")? + .render_to_write( + context! { + includes => torch.include.as_ref().map(prefix_and_join_includes), + name => name, + src => torch.src + }, + &mut *write, + ) + .wrap_err("Cannot render Torch binding template")?; + + write.write_all(b"\n")?; + + Ok(()) +} + +pub fn render_extension( + env: &Environment, + name: &str, + ops_name: &str, + write: &mut impl Write, +) -> Result<()> { + env.get_template("torch-extension.cmake") + .wrap_err("Cannot get Torch extension template")? + .render_to_write( + context! { + name => name, + ops_name => ops_name, + platform => std::env::consts::OS, + }, + &mut *write, + ) + .wrap_err("Cannot render Torch extension template")?; + + write.write_all(b"\n")?; + + Ok(()) +} diff --git a/build2cmake/src/torch/cpu.rs b/build2cmake/src/torch/cpu.rs index b93fa592..08c4e650 100644 --- a/build2cmake/src/torch/cpu.rs +++ b/build2cmake/src/torch/cpu.rs @@ -1,13 +1,13 @@ use std::{io::Write, path::PathBuf}; use eyre::{bail, Context, Result}; -use itertools::Itertools; use minijinja::{context, Environment}; use crate::config::{Backend, Build, Torch}; use crate::fileset::FileSet; -use crate::torch::common::write_metadata; -use crate::torch::common::write_pyproject_toml; +use crate::torch::common::{ + render_binding, render_extension, write_metadata, write_pyproject_toml, +}; use crate::torch::kernel::render_kernel_components; use crate::torch::kernel_ops_identifier; use crate::version::Version; @@ -103,51 +103,6 @@ fn write_cmake( Ok(()) } -fn render_binding( - env: &Environment, - torch: &Torch, - name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("cpu/torch-binding.cmake") - .wrap_err("Cannot get Torch binding template")? - .render_to_write( - context! { - includes => torch.include.as_ref().map(prefix_and_join_includes), - name => name, - src => torch.src - }, - &mut *write, - ) - .wrap_err("Cannot render Torch binding template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - -pub fn render_extension( - env: &Environment, - name: &str, - ops_name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("cpu/torch-extension.cmake") - .wrap_err("Cannot get Torch extension template")? - .render_to_write( - context! { - name => name, - ops_name => ops_name, - }, - &mut *write, - ) - .wrap_err("Cannot render Torch extension template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - fn render_preamble( env: &Environment, name: &str, @@ -234,15 +189,3 @@ fn write_torch_registration_macros(file_set: &mut FileSet) -> Result<()> { Ok(()) } - -fn prefix_and_join_includes(includes: impl AsRef<[S]>) -> String -where - S: AsRef, -{ - includes - .as_ref() - .iter() - .map(|include| format!("${{CMAKE_SOURCE_DIR}}/{}", include.as_ref())) - .collect_vec() - .join(";") -} diff --git a/build2cmake/src/torch/cuda.rs b/build2cmake/src/torch/cuda.rs index 970f5bc3..e7c5acdc 100644 --- a/build2cmake/src/torch/cuda.rs +++ b/build2cmake/src/torch/cuda.rs @@ -7,9 +7,9 @@ use eyre::{bail, Context, Result}; use minijinja::{context, Environment}; use crate::config::{Backend, Build, Dependency, Torch}; -use crate::torch::common::prefix_and_join_includes; -use crate::torch::common::write_metadata; -use crate::torch::common::write_pyproject_toml; +use crate::torch::common::{ + render_binding, render_extension, write_metadata, write_pyproject_toml, +}; use crate::torch::kernel::render_kernel_components; use crate::torch::kernel_ops_identifier; use crate::version::Version; @@ -189,29 +189,6 @@ fn write_cmake( Ok(()) } -pub fn render_binding( - env: &Environment, - torch: &Torch, - name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("cuda/torch-binding.cmake") - .wrap_err("Cannot get Torch binding template")? - .render_to_write( - context! { - includes => torch.include.as_ref().map(prefix_and_join_includes), - name => name, - src => torch.src - }, - &mut *write, - ) - .wrap_err("Cannot render Torch binding template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - fn render_deps( env: &Environment, backend: Backend, @@ -307,29 +284,6 @@ fn render_deps( Ok(()) } -pub fn render_extension( - env: &Environment, - name: &str, - ops_name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("cuda/torch-extension.cmake") - .wrap_err("Cannot get Torch extension template")? - .render_to_write( - context! { - name => name, - ops_name => ops_name, - platform => std::env::consts::OS, - }, - &mut *write, - ) - .wrap_err("Cannot render Torch extension template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - pub fn render_preamble( env: &Environment, name: &str, diff --git a/build2cmake/src/torch/metal.rs b/build2cmake/src/torch/metal.rs index 93089fe9..60e47a5f 100644 --- a/build2cmake/src/torch/metal.rs +++ b/build2cmake/src/torch/metal.rs @@ -1,13 +1,13 @@ use std::{io::Write, path::PathBuf}; use eyre::{bail, Context, Result}; -use itertools::Itertools; use minijinja::{context, Environment}; use crate::config::{Backend, Build, Torch}; use crate::fileset::FileSet; -use crate::torch::common::write_metadata; -use crate::torch::common::write_pyproject_toml; +use crate::torch::common::{ + render_binding, render_extension, write_metadata, write_pyproject_toml, +}; use crate::torch::kernel::render_kernel_components; use crate::torch::kernel_ops_identifier; use crate::version::Version; @@ -119,51 +119,6 @@ fn write_cmake( Ok(()) } -fn render_binding( - env: &Environment, - torch: &Torch, - name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("metal/torch-binding.cmake") - .wrap_err("Cannot get Torch binding template")? - .render_to_write( - context! { - includes => torch.include.as_ref().map(prefix_and_join_includes), - name => name, - src => torch.src - }, - &mut *write, - ) - .wrap_err("Cannot render Torch binding template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - -pub fn render_extension( - env: &Environment, - name: &str, - ops_name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("metal/torch-extension.cmake") - .wrap_err("Cannot get Torch extension template")? - .render_to_write( - context! { - name => name, - ops_name => ops_name, - }, - &mut *write, - ) - .wrap_err("Cannot render Torch extension template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - fn render_preamble( env: &Environment, name: &str, @@ -250,15 +205,3 @@ fn write_torch_registration_macros(file_set: &mut FileSet) -> Result<()> { Ok(()) } - -fn prefix_and_join_includes(includes: impl AsRef<[S]>) -> String -where - S: AsRef, -{ - includes - .as_ref() - .iter() - .map(|include| format!("${{CMAKE_SOURCE_DIR}}/{}", include.as_ref())) - .collect_vec() - .join(";") -} diff --git a/build2cmake/src/torch/xpu.rs b/build2cmake/src/torch/xpu.rs index 03738a68..3a14b65d 100644 --- a/build2cmake/src/torch/xpu.rs +++ b/build2cmake/src/torch/xpu.rs @@ -3,12 +3,12 @@ use std::io::Write; use std::path::PathBuf; use eyre::{bail, Context, Result}; -use itertools::Itertools; use minijinja::{context, Environment}; use crate::config::{Backend, Build, Dependency, Torch}; -use crate::torch::common::write_metadata; -use crate::torch::common::write_pyproject_toml; +use crate::torch::common::{ + render_binding, render_extension, write_metadata, write_pyproject_toml, +}; use crate::torch::kernel::render_kernel_components; use crate::torch::kernel_ops_identifier; use crate::version::Version; @@ -176,29 +176,6 @@ fn write_cmake( Ok(()) } -fn render_binding( - env: &Environment, - torch: &Torch, - name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("xpu/torch-binding.cmake") - .wrap_err("Cannot get Torch binding template")? - .render_to_write( - context! { - includes => torch.include.as_ref().map(prefix_and_join_includes), - name => name, - src => torch.src - }, - &mut *write, - ) - .wrap_err("Cannot render Torch binding template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - fn render_deps( env: &Environment, backend: Backend, @@ -233,29 +210,6 @@ fn render_deps( Ok(()) } -pub fn render_extension( - env: &Environment, - name: &str, - ops_name: &str, - write: &mut impl Write, -) -> Result<()> { - env.get_template("xpu/torch-extension.cmake") - .wrap_err("Cannot get Torch extension template")? - .render_to_write( - context! { - name => name, - ops_name => ops_name, - platform => std::env::consts::OS - }, - &mut *write, - ) - .wrap_err("Cannot render Torch extension template")?; - - write.write_all(b"\n")?; - - Ok(()) -} - pub fn render_preamble( env: &Environment, name: &str, @@ -280,15 +234,3 @@ pub fn render_preamble( Ok(()) } - -fn prefix_and_join_includes(includes: impl AsRef<[S]>) -> String -where - S: AsRef, -{ - includes - .as_ref() - .iter() - .map(|include| format!("${{CMAKE_SOURCE_DIR}}/{}", include.as_ref())) - .collect_vec() - .join(";") -}