diff --git a/docs/source/builder/writing-kernels.md b/docs/source/builder/writing-kernels.md index fbc7a85c..60b0b4d2 100644 --- a/docs/source/builder/writing-kernels.md +++ b/docs/source/builder/writing-kernels.md @@ -234,6 +234,18 @@ The following sections enumerate all supported options for `build.toml`. is provided for kernels that require functionality only provided by newer CUDA toolkits. +### `general.xpu` + +- `cri-grf-size` (optional): the number of GRF registers per thread (128, 256, + or 512) for the CRI (Xe3P) kernels that do not request a size with a kernel + property. This lets kernels use 512 GRF, which the `grf_size` kernel property + does not support. It is an option for the whole AOT image, so it also applies + to kernels that come from other `kernel.` sections. Use `grf_size<256>` + on kernels that should not get the larger GRF. This option requires a kernel + that depends on `sycl_tla` and is built for CRI only (with + `-D__SYCL_TARGET_INTEL_GPU_CRI__=1` in its `sycl-flags`), because the option + is not supported for other GPUs. + ### Framework sections The framework section specifies framework-specific settings. The name of the diff --git a/kernel-builder/src/pyproject/deps.rs b/kernel-builder/src/pyproject/deps.rs index b9acb122..3ef93ecb 100644 --- a/kernel-builder/src/pyproject/deps.rs +++ b/kernel-builder/src/pyproject/deps.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use std::io::Write; -use eyre::{Context, Result}; +use eyre::{bail, Context, Result}; use minijinja::{context, Environment}; use kernels_common::config::{Build, Dependency}; @@ -13,6 +13,13 @@ pub fn render_deps(env: &Environment, build: &Build, write: &mut impl Write) -> deps.extend(kernel.depends()) } + // Only the sycl-tla setup composes the AOT options, do not drop the + // GRF size silently. + let cri_grf_size = build.general.xpu.as_ref().and_then(|xpu| xpu.cri_grf_size); + if cri_grf_size.is_some() && !deps.contains(&Dependency::SyclTla) { + bail!("`general.xpu.cri-grf-size` needs a kernel that depends on `sycl-tla`"); + } + for dep in deps { match dep { Dependency::Cutlass2_10 => { @@ -97,6 +104,11 @@ pub fn render_deps(env: &Environment, build: &Build, write: &mut impl Write) -> // generic image, so that image can only lower to cri and pvc/bmg need // alias targets. Otherwise one image serves all three, halving the // device compilations per TU. + // + // The SYCL runtime takes the last AOT image that matches a + // device, and a spir64_gen image matches every Intel GPU, so + // spir64_gen must come before the aliases. spir64 is the JIT + // image for other GPUs. let cri_isolated_image = build.kernels.values().any(|kernel| { kernel.sycl_flags().is_some_and(|flags| { flags @@ -105,14 +117,32 @@ pub fn render_deps(env: &Environment, build: &Build, write: &mut impl Write) -> }) }); let (sycl_aot_devices, sycl_offload_targets) = if cri_isolated_image { - ("cri", "intel_gpu_pvc,intel_gpu_bmg_g21,spir64_gen,spir64") + ("cri", "spir64_gen,intel_gpu_pvc,intel_gpu_bmg_g21,spir64") } else { ("pvc,bmg-g21,cri", "spir64_gen,spir64") }; + // The GRF size is an ocloc option for the whole image, which + // crashes ocloc for pvc and bmg-g21, so it needs the cri-only + // image. + let sycl_aot_internal_options = match cri_grf_size { + None => None, + Some(size) if !matches!(size, 128 | 256 | 512) => { + bail!("`general.xpu.cri-grf-size` must be 128, 256, or 512, got {size}") + } + Some(_) if !cri_isolated_image => bail!( + "`general.xpu.cri-grf-size` needs a kernel built for CRI only \ + (`-D__SYCL_TARGET_INTEL_GPU_CRI__=1` in its `sycl-flags`)" + ), + Some(size) => Some(format!("-cl-intel-{size}-GRF-per-thread")), + }; env.get_template("xpu/dep-sycl-tla.cmake") .wrap_err("Cannot get SYCL TLA dependency template")? .render_captured_to( - context! { sycl_aot_devices, sycl_offload_targets }, + context! { + sycl_aot_devices, + sycl_offload_targets, + sycl_aot_internal_options, + }, &mut *write, ) .wrap_err("Cannot render SYCL TLA dependency template")?; @@ -128,3 +158,81 @@ pub fn render_deps(env: &Environment, build: &Build, write: &mut impl Write) -> Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + fn render(general_xpu: &str, cri_kernel: bool) -> Result { + let cri_flags = if cri_kernel { + r#"sycl-flags = ["-fsycl-targets=spir64_gen", "-D__SYCL_TARGET_INTEL_GPU_CRI__=1"]"# + } else { + "" + }; + let tmp = tempfile::tempdir().unwrap(); + std::fs::write( + tmp.path().join("build.toml"), + format!( + r#" + [general] + name = "xpu-demo" + version = 1 + edition = 6 + license = "Apache-2.0" + backends = ["xpu"] + + {general_xpu} + + [torch] + src = ["torch-ext/torch_binding.cpp"] + + [kernel.demo_xpu] + language = "cpp-xpu" + depends = ["torch", "sycl_tla"] + src = ["demo/demo.cpp"] + {cri_flags} + "# + ), + ) + .unwrap(); + let build = Build::open(tmp.path()).unwrap(); + + let mut env = Environment::new(); + minijinja_embed::load_templates!(&mut env); + let mut rendered = vec![]; + render_deps(&env, &build, &mut rendered)?; + Ok(String::from_utf8(rendered).unwrap()) + } + + #[test] + fn cri_grf_size_sets_ocloc_internal_options() { + let rendered = render("[general.xpu]\ncri-grf-size = 512", true).unwrap(); + assert!(rendered.contains(r#"set(SYCL_AOT_DEVICES "cri")"#)); + assert!(rendered.contains( + r#"set(SYCL_OFFLOAD_TARGETS "spir64_gen,intel_gpu_pvc,intel_gpu_bmg_g21,spir64")"# + )); + assert!( + rendered.contains(r#"set(SYCL_AOT_INTERNAL_OPTIONS "-cl-intel-512-GRF-per-thread")"#) + ); + } + + #[test] + fn no_cri_grf_size_keeps_ocloc_defaults() { + let rendered = render("", true).unwrap(); + assert!(!rendered.contains("SYCL_AOT_INTERNAL_OPTIONS")); + } + + #[test] + fn cri_grf_size_needs_cri_only_image() { + let err = render("[general.xpu]\ncri-grf-size = 512", false).unwrap_err(); + assert!(err + .to_string() + .contains("needs a kernel built for CRI only")); + } + + #[test] + fn cri_grf_size_rejects_unknown_size() { + let err = render("[general.xpu]\ncri-grf-size = 384", true).unwrap_err(); + assert!(err.to_string().contains("must be 128, 256, or 512")); + } +} diff --git a/kernel-builder/src/pyproject/templates/torch/preamble.cmake b/kernel-builder/src/pyproject/templates/torch/preamble.cmake index 1189b238..4b9758df 100644 --- a/kernel-builder/src/pyproject/templates/torch/preamble.cmake +++ b/kernel-builder/src/pyproject/templates/torch/preamble.cmake @@ -214,6 +214,8 @@ elseif(GPU_LANG STREQUAL "SYCL") set(SYCL_OFFLOAD_TARGETS "spir64_gen,spir64") set(SYCL_AOT_DEVICES "pvc,xe-lpg,ats-m150") set(SYCL_AOT_BACKEND_OPTIONS " -cl-intel-enable-auto-large-GRF-mode -cl-poison-unsupported-fp64-kernels -cl-intel-greater-than-4GB-buffer-required") + # ocloc -internal_options, spir64_gen only (e.g. a GRF size for one device). + set(SYCL_AOT_INTERNAL_OPTIONS "") set(SYCL_SPIRV_EXT "") # Rebuild sycl_flags (compile) and sycl_link_flags (link) from the variables @@ -231,14 +233,20 @@ elseif(GPU_LANG STREQUAL "SYCL") # Backend options reach one target each: spir64_gen takes -device, the # intel_gpu_* aliases imply theirs. SHELL: stops CMake from deduplicating # the identical -options and silently dropping all but the first target. + # -exclude_ir: AOT images do not carry a SPIR-V copy (spir64 is the JIT + # image when there is one). string(REPLACE "," ";" _sycl_aot_targets "${SYCL_OFFLOAD_TARGETS}") + set(_sycl_aot_internal_options "") + if(SYCL_AOT_INTERNAL_OPTIONS) + set(_sycl_aot_internal_options " -internal_options ${SYCL_AOT_INTERNAL_OPTIONS}") + endif() foreach(_tgt IN LISTS _sycl_aot_targets) if(_tgt STREQUAL "spir64_gen") string(APPEND sycl_link_flags - ";SHELL:-Xsycl-target-backend=spir64_gen \"-device ${SYCL_AOT_DEVICES} -options '${SYCL_AOT_BACKEND_OPTIONS}'\"") + ";SHELL:-Xsycl-target-backend=spir64_gen \"-device ${SYCL_AOT_DEVICES} -exclude_ir${_sycl_aot_internal_options} -options '${SYCL_AOT_BACKEND_OPTIONS}'\"") elseif(_tgt MATCHES "^intel_gpu_") string(APPEND sycl_link_flags - ";SHELL:-Xsycl-target-backend=${_tgt} \"-options '${SYCL_AOT_BACKEND_OPTIONS}'\"") + ";SHELL:-Xsycl-target-backend=${_tgt} \"-exclude_ir -options '${SYCL_AOT_BACKEND_OPTIONS}'\"") endif() endforeach() diff --git a/kernel-builder/src/pyproject/templates/torch/torch-extension.cmake b/kernel-builder/src/pyproject/templates/torch/torch-extension.cmake index 85c66285..5afaeee7 100644 --- a/kernel-builder/src/pyproject/templates/torch/torch-extension.cmake +++ b/kernel-builder/src/pyproject/templates/torch/torch-extension.cmake @@ -26,6 +26,12 @@ endif() if(GPU_LANG STREQUAL "SYCL") target_link_options(${OPS_NAME} PRIVATE ${sycl_link_flags}) target_link_libraries(${OPS_NAME} PRIVATE dnnl) + # The static symbol table and the device symbol table (.tgtsym) that + # the offload bundler adds are not loaded at runtime, but take tens of + # MB in large kernels. + add_custom_command(TARGET ${OPS_NAME} POST_BUILD + COMMAND ${CMAKE_STRIP} --strip-all --remove-section=.tgtsym $ + VERBATIM) endif() # Compile Metal shaders if any were found diff --git a/kernel-builder/src/pyproject/templates/tvm_ffi/preamble.cmake b/kernel-builder/src/pyproject/templates/tvm_ffi/preamble.cmake index 57650aa6..c656847c 100644 --- a/kernel-builder/src/pyproject/templates/tvm_ffi/preamble.cmake +++ b/kernel-builder/src/pyproject/templates/tvm_ffi/preamble.cmake @@ -174,6 +174,8 @@ elseif(GPU_LANG STREQUAL "SYCL") set(SYCL_OFFLOAD_TARGETS "spir64_gen,spir64") set(SYCL_AOT_DEVICES "pvc,xe-lpg,ats-m150") set(SYCL_AOT_BACKEND_OPTIONS " -cl-intel-enable-auto-large-GRF-mode -cl-poison-unsupported-fp64-kernels -cl-intel-greater-than-4GB-buffer-required") + # ocloc -internal_options, spir64_gen only (e.g. a GRF size for one device). + set(SYCL_AOT_INTERNAL_OPTIONS "") set(SYCL_SPIRV_EXT "") # Rebuild sycl_flags (compile) and sycl_link_flags (link) from the variables @@ -192,14 +194,20 @@ elseif(GPU_LANG STREQUAL "SYCL") # Backend options reach one target each: spir64_gen takes -device, the # intel_gpu_* aliases imply theirs. SHELL: stops CMake from deduplicating # the identical -options and silently dropping all but the first target. + # -exclude_ir: AOT images do not carry a SPIR-V copy (spir64 is the JIT + # image when there is one). string(REPLACE "," ";" _sycl_aot_targets "${SYCL_OFFLOAD_TARGETS}") + set(_sycl_aot_internal_options "") + if(SYCL_AOT_INTERNAL_OPTIONS) + set(_sycl_aot_internal_options " -internal_options ${SYCL_AOT_INTERNAL_OPTIONS}") + endif() foreach(_tgt IN LISTS _sycl_aot_targets) if(_tgt STREQUAL "spir64_gen") string(APPEND sycl_link_flags - ";SHELL:-Xsycl-target-backend=spir64_gen \"-device ${SYCL_AOT_DEVICES} -options '${SYCL_AOT_BACKEND_OPTIONS}'\"") + ";SHELL:-Xsycl-target-backend=spir64_gen \"-device ${SYCL_AOT_DEVICES} -exclude_ir${_sycl_aot_internal_options} -options '${SYCL_AOT_BACKEND_OPTIONS}'\"") elseif(_tgt MATCHES "^intel_gpu_") string(APPEND sycl_link_flags - ";SHELL:-Xsycl-target-backend=${_tgt} \"-options '${SYCL_AOT_BACKEND_OPTIONS}'\"") + ";SHELL:-Xsycl-target-backend=${_tgt} \"-exclude_ir -options '${SYCL_AOT_BACKEND_OPTIONS}'\"") endif() endforeach() diff --git a/kernel-builder/src/pyproject/templates/tvm_ffi/tvm-ffi-extension.cmake b/kernel-builder/src/pyproject/templates/tvm_ffi/tvm-ffi-extension.cmake index 6bb8d566..3d30b203 100644 --- a/kernel-builder/src/pyproject/templates/tvm_ffi/tvm-ffi-extension.cmake +++ b/kernel-builder/src/pyproject/templates/tvm_ffi/tvm-ffi-extension.cmake @@ -35,6 +35,12 @@ target_link_rust_kernels(${OPS_NAME}) if(GPU_LANG STREQUAL "SYCL") target_link_options(${OPS_NAME} PRIVATE ${sycl_link_flags}) target_link_libraries(${OPS_NAME} PRIVATE dnnl) + # The static symbol table and the device symbol table (.tgtsym) that + # the offload bundler adds are not loaded at runtime, but take tens of + # MB in large kernels. + add_custom_command(TARGET ${OPS_NAME} POST_BUILD + COMMAND ${CMAKE_STRIP} --strip-all --remove-section=.tgtsym $ + VERBATIM) endif() # Compile Metal shaders if any were found diff --git a/kernel-builder/src/pyproject/templates/xpu/dep-sycl-tla.cmake b/kernel-builder/src/pyproject/templates/xpu/dep-sycl-tla.cmake index 53864850..e0c13020 100644 --- a/kernel-builder/src/pyproject/templates/xpu/dep-sycl-tla.cmake +++ b/kernel-builder/src/pyproject/templates/xpu/dep-sycl-tla.cmake @@ -77,6 +77,10 @@ set(DPCPP_AOT_MIN_VERSION "2026.0") if(DPCPP_VERSION VERSION_GREATER_EQUAL DPCPP_AOT_MIN_VERSION) set(SYCL_AOT_DEVICES "{{ sycl_aot_devices }}") set(SYCL_OFFLOAD_TARGETS "{{ sycl_offload_targets }}") +{% if sycl_aot_internal_options %} + # general.xpu.cri-grf-size: GRF size of the cri kernels that do not set one. + set(SYCL_AOT_INTERNAL_OPTIONS "{{ sycl_aot_internal_options }}") +{% endif %} else() set(SYCL_OFFLOAD_TARGETS "spir64") endif() diff --git a/kernels-common/src/config/mod.rs b/kernels-common/src/config/mod.rs index 9652f71d..5a8a2866 100644 --- a/kernels-common/src/config/mod.rs +++ b/kernels-common/src/config/mod.rs @@ -268,6 +268,7 @@ pub struct CudaGeneral { } pub struct XpuGeneral { + pub cri_grf_size: Option, pub kernel_depends: Option>, pub python_depends: Option>, } diff --git a/kernels-common/src/config/v3.rs b/kernels-common/src/config/v3.rs index fc2344a3..baf6fed6 100644 --- a/kernels-common/src/config/v3.rs +++ b/kernels-common/src/config/v3.rs @@ -237,6 +237,7 @@ impl From for super::NeuronGeneral { impl From for super::XpuGeneral { fn from(xpu: XpuGeneral) -> Self { Self { + cri_grf_size: None, kernel_depends: None, python_depends: xpu.python_depends, } diff --git a/kernels-common/src/config/v4.rs b/kernels-common/src/config/v4.rs index f1e6cf09..06685dda 100644 --- a/kernels-common/src/config/v4.rs +++ b/kernels-common/src/config/v4.rs @@ -247,6 +247,7 @@ impl From for super::NeuronGeneral { impl From for super::XpuGeneral { fn from(xpu: XpuGeneral) -> Self { Self { + cri_grf_size: None, kernel_depends: None, python_depends: xpu.python_depends, } diff --git a/kernels-common/src/config/v5.rs b/kernels-common/src/config/v5.rs index 59f90ef7..1cbc04c2 100644 --- a/kernels-common/src/config/v5.rs +++ b/kernels-common/src/config/v5.rs @@ -282,6 +282,7 @@ impl From for super::TpuGeneral { impl From for super::XpuGeneral { fn from(xpu: XpuGeneral) -> Self { Self { + cri_grf_size: None, kernel_depends: xpu.kernel_depends, python_depends: xpu.python_depends, } diff --git a/kernels-common/src/config/v6.rs b/kernels-common/src/config/v6.rs index ec4232e7..52d1b91a 100644 --- a/kernels-common/src/config/v6.rs +++ b/kernels-common/src/config/v6.rs @@ -98,6 +98,7 @@ pub struct TpuGeneral { #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] pub struct XpuGeneral { + pub cri_grf_size: Option, pub kernel_depends: Option>, pub python_depends: Option>, } @@ -356,6 +357,7 @@ impl From for super::TpuGeneral { impl From for super::XpuGeneral { fn from(xpu: XpuGeneral) -> Self { Self { + cri_grf_size: xpu.cri_grf_size, kernel_depends: xpu.kernel_depends, python_depends: xpu.python_depends, } @@ -608,6 +610,7 @@ impl From for TpuGeneral { impl From for XpuGeneral { fn from(xpu: super::XpuGeneral) -> Self { Self { + cri_grf_size: xpu.cri_grf_size, kernel_depends: xpu.kernel_depends, python_depends: xpu.python_depends, } diff --git a/nix-builder/pkgs/xpu-packages/ocloc.nix b/nix-builder/pkgs/xpu-packages/ocloc.nix index d39cada0..aa97ad2b 100644 --- a/nix-builder/pkgs/xpu-packages/ocloc.nix +++ b/nix-builder/pkgs/xpu-packages/ocloc.nix @@ -11,32 +11,32 @@ stdenv.mkDerivation (finalAttrs: { pname = "ocloc"; - version = "26.22"; + version = "26.44"; srcs = [ (fetchurl { - url = "https://github.com/intel/compute-runtime/releases/download/26.22.38646.4/intel-ocloc_26.22.38646.4-0_amd64.deb"; - hash = "sha256-JXRYQeZiecf/H/bC1NqeyRFoXBLhycVgnZnk1Uo2TcA="; + url = "https://github.com/intel/compute-runtime/releases/download/26.44.40734.8/intel-ocloc_26.44.40734.8-0_amd64.deb"; + hash = "sha256-/HO2difIVoLC+cRyZvMP4gkigBpl5i+GOSOvB/1nYX4="; }) (fetchurl { - url = "https://github.com/intel/intel-graphics-compiler/releases/download/v2.36.3/intel-igc-core-2_2.36.3+21719_amd64.deb"; - hash = "sha256-ngl1rHUBW0Meuy2oGoArn9HiijwnAxOpdWnNHmpsYEg="; + url = "https://github.com/intel/intel-graphics-compiler/releases/download/v2.43.9/intel-igc-core-2_2.43.9+23453_amd64.deb"; + hash = "sha256-VSkW93HY7GmgToj3xadg8pycsjAtv3IeveIUTq6UgX8="; }) (fetchurl { - url = "https://github.com/intel/intel-graphics-compiler/releases/download/v2.36.3/intel-igc-opencl-2_2.36.3+21719_amd64.deb"; - hash = "sha256-NQpSMx54S7f7ntQumTtcRLfmVi/HTSzzECsptqV2+oU="; + url = "https://github.com/intel/intel-graphics-compiler/releases/download/v2.43.9/intel-igc-opencl-2_2.43.9+23453_amd64.deb"; + hash = "sha256-rsCb8uHzD2v7eVl6QrP32ZNcWDXFz7+GPyjO/0j8iNY="; }) (fetchurl { - url = "https://github.com/intel/compute-runtime/releases/download/26.22.38646.4/intel-opencl-icd_26.22.38646.4-0_amd64.deb"; - hash = "sha256-b9rC6KKqz4ROv9kFIb9xArPrtE9pwbztGpeFp86Wo8I="; + url = "https://github.com/intel/compute-runtime/releases/download/26.44.40734.8/intel-opencl-icd_26.44.40734.8-0_amd64.deb"; + hash = "sha256-nVSiOVfLaVqWAtOJ5w0SSzc0MxpzLn4gB0u2iU4iHSI="; }) (fetchurl { - url = "https://github.com/intel/compute-runtime/releases/download/26.22.38646.4/libigdgmm12_22.10.0_amd64.deb"; - hash = "sha256-YDGmPW6KEs5hwU78FfLI5ycGEobjgguFlObQBhXgTVQ="; + url = "https://github.com/intel/compute-runtime/releases/download/26.44.40734.8/libigdgmm12_22.10.2_amd64.deb"; + hash = "sha256-BT/OrbhKHS+aMKDRvJsWQks/GpDMSZAPzYCq3PJcik0="; }) (fetchurl { - url = "https://github.com/intel/compute-runtime/releases/download/26.22.38646.4/libze-intel-gpu1_26.22.38646.4-0_amd64.deb"; - hash = "sha256-i++fJOA/gm+TwHYIG9oTxqw6+9nkK5+48pj6tlIzDi8="; + url = "https://github.com/intel/compute-runtime/releases/download/26.44.40734.8/libze-intel-gpu1_26.44.40734.8-0_amd64.deb"; + hash = "sha256-ngozX/7ubTiXSyddB07yqVxI3d29UHr4EEGIVyeOgUI="; }) ]; dontStrip = true;