From 1f9b28c6300ca773bedcf2cc05a0c42984931fb8 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Wed, 18 Nov 2026 12:01:17 +0100 Subject: [PATCH] Add an EnzymeCore extension for KernelAbstractions' Enzyme support KernelAbstractions' Enzyme extension calls `EnzymeCore.compiler_job_from_backend` to compile the derivatives of kernels and their tapes. Implement it for `oneAPIBackend`, compiling like `KI.kernel_function` does (with the back end's `always_inline` and the sub-group size), and mark `synchronize` inactive. Reverse mode needs SPIRVIntrinsics 1.6.2, whose work-item built-ins tell Enzyme that they return integers: oneAPI computes KernelInterface indices in 32 bits, and Enzyme can't infer the type of a built-in's result from its truncation. --- Project.toml | 9 ++++++++- ext/EnzymeCoreExt.jl | 27 +++++++++++++++++++++++++++ test/Project.toml | 1 + test/extensions/enzyme.jl | 15 +++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 ext/EnzymeCoreExt.jl create mode 100644 test/extensions/enzyme.jl diff --git a/Project.toml b/Project.toml index 6dddeeb5..a5e69e29 100644 --- a/Project.toml +++ b/Project.toml @@ -35,11 +35,18 @@ oneAPI_Level_Zero_Headers_jll = "f4bc562b-d309-54f8-9efb-476e56f0410d" oneAPI_Level_Zero_Loader_jll = "13eca655-d68d-5b81-8367-6d99d727ab01" oneAPI_Support_jll = "b049733a-a71d-5ed3-8eba-7d323ac00b36" +[weakdeps] +EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869" + +[extensions] +EnzymeCoreExt = "EnzymeCore" + [compat] AbstractFFTs = "1.5.0" Adapt = "4" CEnum = "0.5, 1" Crayons = "4" +EnzymeCore = "0.8" GPUArrays = "12.1" GPUCompiler = "2.24" GPUToolbox = "3.7" @@ -52,7 +59,7 @@ PTI_jll = "1.2" PrecompileTools = "1" PrettyTables = "3" Preferences = "1" -SPIRVIntrinsics = "1.6" +SPIRVIntrinsics = "1.6.2" SPIRV_LLVM_Backend_jll = "23" SPIRV_LLVM_Translator_jll = "23" SPIRV_Tools_jll = "2025.4.0" diff --git a/ext/EnzymeCoreExt.jl b/ext/EnzymeCoreExt.jl new file mode 100644 index 00000000..19e3649b --- /dev/null +++ b/ext/EnzymeCoreExt.jl @@ -0,0 +1,27 @@ +# compatibility with EnzymeCore +module EnzymeCoreExt + +using oneAPI +import oneAPI: GPUCompiler, oneAPIBackend, oneL0 +import KernelInterface as KI + +import EnzymeCore +using EnzymeCore.EnzymeRules + +# the compiler job that kernels for `backend` are compiled with (see `KI.kernel_function`), +# for Enzyme to compile the derivatives of kernels (e.g. by KernelAbstractions' Enzyme +# extension) and their tapes +function EnzymeCore.compiler_job_from_backend( + backend::oneAPIBackend, @nospecialize(F::Type), @nospecialize(TT::Type) + ) + mi = GPUCompiler.methodinstance(F, TT) + sub_group_size = KI.sub_group_size(backend) + config = oneAPI.compiler_config(oneAPI.device(); backend.always_inline, + sub_group_size = sub_group_size > 0 ? sub_group_size : nothing) + return GPUCompiler.CompilerJob(mi, config) +end + +# waiting for the GPU has no derivative +EnzymeRules.inactive(::typeof(oneL0.synchronize), args...; kwargs...) = nothing + +end diff --git a/test/Project.toml b/test/Project.toml index e1c214c7..34e11157 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,6 +4,7 @@ Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" +EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7" GPUToolbox = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc" diff --git a/test/extensions/enzyme.jl b/test/extensions/enzyme.jl new file mode 100644 index 00000000..5abd3b2f --- /dev/null +++ b/test/extensions/enzyme.jl @@ -0,0 +1,15 @@ +import EnzymeCore +import KernelInterface as KI +using oneAPI: GPUCompiler + +@testset "compiler_job_from_backend" begin + # compiled like `KI.kernel_function` compiles kernels for the back end + for always_inline in (false, true) + backend = oneAPIBackend(; always_inline) + job = EnzymeCore.compiler_job_from_backend(backend, typeof(identity), Tuple{Float32}) + @test job isa GPUCompiler.CompilerJob + @test job.config.always_inline == always_inline + sub_group_size = KI.sub_group_size(backend) + @test job.config.params.sub_group_size == (sub_group_size > 0 ? sub_group_size : nothing) + end +end