From d16f799db2b995f21074ab83ba60e2f77c487fc4 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 3 Nov 2026 18:30:11 +0100 Subject: [PATCH 1/2] POCL: support BFloat16 kernels when PoCL accepts SPV_KHR_bfloat16 On Julia 1.11+, `BFloat16` is `Core.BFloat16`, which lowers to LLVM's `bfloat`, so any kernel touching a `BFloat16` array failed GPUCompiler's validation. Query the device's SPIR-V extensions (cl_khr_spirv_queries) and, when PoCL lists SPV_KHR_bfloat16 (from pocl_standalone_jll 7.2.3+3), enable it together with GPUCompiler's `supports_bfloat16`. Arithmetic is widened to `float` by GPUCompiler 2.20; older PoCL builds keep failing cleanly with an `InvalidIRError`. Closes #921. --- Project.toml | 2 +- src/pocl/compiler/compilation.jl | 9 ++- src/pocl/nanoOpenCL.jl | 12 +++ test/Project.toml | 2 + test/bfloat16.jl | 127 +++++++++++++++++++++++++++++++ test/runtests.jl | 14 ++-- 6 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 test/bfloat16.jl diff --git a/Project.toml b/Project.toml index e9443f4a9..e0f9b3da9 100644 --- a/Project.toml +++ b/Project.toml @@ -49,7 +49,7 @@ oneAPIExt = "oneAPI" AMDGPU = "2" Adapt = "0.4, 1.0, 2.0, 3.0, 4" Atomix = "1.5" -GPUCompiler = "2.18" +GPUCompiler = "2.20" GPUToolbox = "3.4.1" KernelInterface = "0.4" LLVM = "10" diff --git a/src/pocl/compiler/compilation.jl b/src/pocl/compiler/compilation.jl index 5649b5ac1..a80121c10 100644 --- a/src/pocl/compiler/compilation.jl +++ b/src/pocl/compiler/compilation.jl @@ -217,11 +217,18 @@ end ) supports_fp16 = "cl_khr_fp16" in dev.extensions supports_fp64 = "cl_khr_fp64" in dev.extensions + # `BFloat16` loads, stores and conversions. GPUCompiler widens the arithmetic to `float`, + # because PoCL doesn't accept any SPIR-V extension for bfloat16 arithmetic. + supports_bfloat16 = "SPV_KHR_bfloat16" in dev.spirv_extensions if sub_group_size !== nothing && sub_group_size ∉ dev.sub_group_sizes error("$sub_group_size is not a valid sub-group size for this device.") end + if supports_bfloat16 + extensions = extensions === nothing ? "+SPV_KHR_bfloat16" : "+SPV_KHR_bfloat16,$extensions" + end + # the vector shuffles of `KI.shfl_word_tuple` (see `POCLKernels`); `SPV_INTEL_subgroups` # is only declared by modules that use it if "cl_intel_subgroups" in dev.extensions @@ -230,7 +237,7 @@ end # create GPUCompiler objects target = SPIRVCompilerTarget(; - supports_fp16, supports_fp64, atomics, + supports_fp16, supports_fp64, supports_bfloat16, atomics, extensions = join(filter(!isempty, [default_extensions, something(extensions, "")]), ","), validate = true, kwargs... ) diff --git a/src/pocl/nanoOpenCL.jl b/src/pocl/nanoOpenCL.jl index ef632d933..6a8d1fad0 100644 --- a/src/pocl/nanoOpenCL.jl +++ b/src/pocl/nanoOpenCL.jl @@ -420,6 +420,8 @@ const CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM = 0x11b7 const CL_DEVICE_SUB_GROUP_SIZES_INTEL = 0x4108 +const CL_DEVICE_SPIRV_EXTENSIONS_KHR = 0x12ba + struct CLError <: Exception code::Cint end @@ -1009,6 +1011,16 @@ end return String[string(s) for s in split(bs)] end + # cl_khr_spirv_queries: the SPIR-V extensions that modules may declare + if s == :spirv_extensions + "cl_khr_spirv_queries" in d.extensions::Vector{String} || return String[] + size = Ref{Csize_t}() + clGetDeviceInfo(d, CL_DEVICE_SPIRV_EXTENSIONS_KHR, 0, C_NULL, size) + result = Vector{Ptr{Cchar}}(undef, size[] ÷ sizeof(Ptr{Cchar})) + clGetDeviceInfo(d, CL_DEVICE_SPIRV_EXTENSIONS_KHR, sizeof(result), result, C_NULL) + return String[unsafe_string(ptr) for ptr in result] + end + # cl_ext_float_atomics: per-precision bitfields of natively supported floating-point # atomic operations (zero when the device does not expose the extension) if s == :single_fp_atomic_capabilities || s == :double_fp_atomic_capabilities || s == :half_fp_atomic_capabilities diff --git a/test/Project.toml b/test/Project.toml index c191d6ced..7c9732077 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,6 +1,7 @@ [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b" FileCheck = "4e644321-382b-4b05-b0b6-5d23c3d944fb" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c" @@ -20,4 +21,5 @@ KernelInterface = {path = "../lib/KernelInterface"} [compat] Aqua = "0.8" +BFloat16s = "0.6" FileCheck = "1.2" diff --git a/test/bfloat16.jl b/test/bfloat16.jl new file mode 100644 index 000000000..91b197448 --- /dev/null +++ b/test/bfloat16.jl @@ -0,0 +1,127 @@ +# `BFloat16` kernels on the POCL back end (#921). On Julia 1.11+, `BFloat16` is +# `Core.BFloat16`, which Julia lowers to LLVM's `bfloat`: storing it needs a PoCL that accepts +# `SPV_KHR_bfloat16`, and GPUCompiler widens its arithmetic to `float`. On Julia 1.10, +# BFloat16s.jl defines it as a 16-bit primitive type, which needs nothing from the device. + +using BFloat16s + +@kernel function bf16_copy!(y, @Const(x)) + i = @index(Global) + @inbounds y[i] = x[i] +end +@kernel function bf16_axpy!(y, a, @Const(x)) + i = @index(Global) + @inbounds y[i] = a * x[i] + y[i] +end +@kernel function bf16_ops!(out, @Const(x), @Const(y), @Const(z)) + i = @index(Global) + @inbounds begin + a, b, c = x[i], y[i], z[i] + out[1, i] = a / b + out[2, i] = a - b + out[3, i] = min(a, b) + out[4, i] = max(a, b) + out[5, i] = abs(a) + out[6, i] = copysign(a, b) + out[7, i] = ifelse(a < b, a, b) + out[8, i] = sqrt(abs(a)) + out[9, i] = floor(a) + out[10, i] = BFloat16(isfinite(a / b)) + out[11, i] = -a + end +end +@kernel function bf16_convert!(y, @Const(x)) + i = @index(Global) + @inbounds y[i] = convert(eltype(y), x[i]) +end + +# correctly rounded, unlike the host's conversions from `Float64` and wide integers on Julia +# 1.11 and 1.12 (which round twice, see JuliaMath/BFloat16s.jl#78) +bf16_nearest(x) = BFloat16(Float32(BigFloat(x; precision = 8))) + +const bf16_supported = !isdefined(Core, :BFloat16) || + "SPV_KHR_bfloat16" in POCL.device().spirv_extensions + +if bf16_supported + @testset "POCL BFloat16" begin + backend = POCLBackend() + n = 1024 + # finite values from bit patterns, rather than from rounding on the host (Julia 1.11's + # vectorized `Float32` to `BFloat16` conversion crashes LLVM on AVX512-BF16 hosts) + rand_bf16(n) = map(1:n) do _ + while true + v = reinterpret(BFloat16, rand(UInt16)) + isfinite(v) && return v + end + end + hx, hy, hz = rand_bf16(n), rand_bf16(n), rand_bf16(n) + hx[1:4] .= BFloat16.((-0.0, 0.0, 1.0, -2.0)) + hy[1:4] .= BFloat16.((0.0, -0.0, 0.0, 3.0)) + x, y, z = (KernelAbstractions.allocate(backend, BFloat16, n) for _ in 1:3) + copyto!(x, hx); copyto!(y, hy); copyto!(z, hz) + + @testset "copy" begin + out = similar(x) + bf16_copy!(backend)(out, x; ndrange = n) + @test isequal(Array(out), hx) + end + + @testset "axpy" begin + out = copy(y) + a = BFloat16(1.5) + bf16_axpy!(backend)(out, a, x; ndrange = n) + # widened to `float`, multiplied and added separately (each rounded to bfloat) + @test isequal(Array(out), map((x, y) -> BFloat16(Float32(BFloat16(Float32(a) * Float32(x))) + Float32(y)), hx, hy)) + end + + @testset "arithmetic" begin + out = KernelAbstractions.allocate(backend, BFloat16, 11, n) + bf16_ops!(backend)(out, x, y, z; ndrange = n) + o = Array(out) + # (references in `Float32`: on Julia 1.11, the host miscompiles vectorized `BFloat16` + # operations such as `copysign`) + f32(op) = (xs...) -> BFloat16(op(Float32.(xs)...)) + @test isequal(o[1, :], map(f32(/), hx, hy)) + @test isequal(o[2, :], map(f32(-), hx, hy)) + @test isequal(o[3, :], map(f32(min), hx, hy)) + @test isequal(o[4, :], map(f32(max), hx, hy)) + @test isequal(o[5, :], map(f32(abs), hx)) + @test isequal(o[6, :], map(f32(copysign), hx, hy)) + @test isequal(o[7, :], map(f32((a, b) -> ifelse(a < b, a, b)), hx, hy)) + @test isequal(o[8, :], map(f32(a -> sqrt(abs(a))), hx)) + @test isequal(o[9, :], map(f32(floor), hx)) + @test isequal(o[10, :], map(f32((a, b) -> isfinite(a / b)), hx, hy)) + @test isequal(o[11, :], map(f32(-), hx)) + end + + # including inputs that a conversion through `Float32` would round wrongly + @testset "$T to BFloat16" for (T, extra) in ( + (Float32, Float32[1 + 2.0f0^-8 + 2.0f0^-20, floatmax(Float32)]), + (Float64, [1 + 2.0^-8 + 2.0^-30, -(1 + 2.0^-8 + 2.0^-30), 1.0e39]), + (Int32, Int32[2^30 + 2^22 + 1, typemin(Int32), typemax(Int32)]), + (Int64, Int64[2^62 + 2^54 + 1, typemin(Int64), typemax(Int64)]), + (UInt64, [UInt64(2)^63 + UInt64(2)^55 + 1, typemax(UInt64)]), + ) + vals = T <: AbstractFloat ? T.(randn(n) .* 1000) : rand(T, n) + vals[1:length(extra)] .= extra + src = KernelAbstractions.allocate(backend, T, n) + copyto!(src, vals) + dst = KernelAbstractions.allocate(backend, BFloat16, n) + bf16_convert!(backend)(dst, src; ndrange = n) + @test isequal(Array(dst), map(bf16_nearest, vals)) + end + + @testset "BFloat16 to $T" for T in (Float32, Float64) + dst = KernelAbstractions.allocate(backend, T, n) + bf16_convert!(backend)(dst, x; ndrange = n) + @test isequal(Array(dst), map(T, hx)) + end + end +else + # PoCL without `SPV_KHR_bfloat16` (pocl_standalone_jll before 7.2.3+3): compiling must + # fail cleanly, rather than PoCL aborting on a rejected extension + @testset "POCL BFloat16 (unsupported)" begin + x = BFloat16.(Float32[1, 2, 3, 4]) + @test_throws POCL.GPUCompiler.InvalidIRError bf16_copy!(POCLBackend())(similar(x), x; ndrange = 4) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 4d09525ca..831541a59 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -75,13 +75,13 @@ end fadd_f64_global = true, fadd_f64_local = true ) @test POCL.compiler_config(dev).target.atomics == atomics - # explicitly requested extensions are kept, in addition to the default ones and the one - # for vector shuffles - @test POCL.compiler_config(dev).target.extensions == - "+SPV_KHR_no_integer_wrap_decoration,+SPV_INTEL_subgroups" + # explicitly requested extensions are kept, in addition to the default ones and the ones + # for vector shuffles and (with a PoCL that supports it) bfloat16 + device_extensions = "+SPV_KHR_no_integer_wrap_decoration,+SPV_INTEL_subgroups" * + ("SPV_KHR_bfloat16" in dev.spirv_extensions ? ",+SPV_KHR_bfloat16" : "") + @test POCL.compiler_config(dev).target.extensions == device_extensions config = POCL.compiler_config(dev; extensions = "+SPV_KHR_expect_assume") - @test config.target.extensions == - "+SPV_KHR_no_integer_wrap_decoration,+SPV_INTEL_subgroups,+SPV_KHR_expect_assume" + @test config.target.extensions == device_extensions * ",+SPV_KHR_expect_assume" @test config.target.atomics == atomics @test POCL.compiler_config(dev; extensions = nothing).target == POCL.compiler_config(dev).target @@ -320,6 +320,8 @@ if "cl_khr_fp16" in POCL.device().extensions end end +include("bfloat16.jl") + # the sub-group width kernels are compiled for is the one `KI.sub_group_size` reports # (PoCL folds `get_max_sub_group_size` to that constant itself, see pocl/pocl#2375) function max_sub_group_size_kernel(out) From 2399b1ba9e11063a2be2525ef8988a33f968d36f Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 3 Nov 2026 19:14:16 +0100 Subject: [PATCH 2/2] Correct why the BFloat16 tests avoid the host's conversions BFloat16s.jl 0.6.4 rounds them correctly; the reason left is that vectorized ones crash LLVM on Julia 1.11 on AVX512-BF16 hosts. --- test/bfloat16.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/bfloat16.jl b/test/bfloat16.jl index 91b197448..b8acef640 100644 --- a/test/bfloat16.jl +++ b/test/bfloat16.jl @@ -35,8 +35,8 @@ end @inbounds y[i] = convert(eltype(y), x[i]) end -# correctly rounded, unlike the host's conversions from `Float64` and wide integers on Julia -# 1.11 and 1.12 (which round twice, see JuliaMath/BFloat16s.jl#78) +# the correctly rounded value, computed without the host's `BFloat16` conversions (on Julia +# 1.11, vectorized ones crash LLVM on AVX512-BF16 hosts) bf16_nearest(x) = BFloat16(Float32(BigFloat(x; precision = 8))) const bf16_supported = !isdefined(Core, :BFloat16) ||