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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion src/pocl/compiler/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...
)
Expand Down
12 changes: 12 additions & 0 deletions src/pocl/nanoOpenCL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -20,4 +21,5 @@ KernelInterface = {path = "../lib/KernelInterface"}

[compat]
Aqua = "0.8"
BFloat16s = "0.6"
FileCheck = "1.2"
127 changes: 127 additions & 0 deletions test/bfloat16.jl
Original file line number Diff line number Diff line change
@@ -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

# 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) ||
"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
14 changes: 8 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading