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 @@ -34,7 +34,7 @@ SPIRVIntrinsics = {path = "lib/intrinsics"}
[compat]
Adapt = "4"
GPUArrays = "12.1"
GPUCompiler = "2.18"
GPUCompiler = "2.20"
GPUToolbox = "3.5"
KernelAbstractions = "0.10"
KernelInterface = "0.4"
Expand Down
12 changes: 11 additions & 1 deletion lib/cl/device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ end

# error handling inspired by rusticl
# https://gitlab.freedesktop.org/mesa/mesa/-/blob/c4385d6fb0938231114eb3023082cd33788b89b4/src/gallium/frontends/rusticl/api/device.rs#L314-320
# cl_khr_spirv_queries: the SPIR-V extensions that modules may declare
if s == :spirv_extensions
"cl_khr_spirv_queries" in d.extensions || 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

if s == :sub_group_sizes
res_size = Ref{Csize_t}()
err = unchecked_clGetDeviceInfo(d, CL_DEVICE_SUB_GROUP_SIZES_INTEL, C_NULL, C_NULL, res_size)
Expand Down Expand Up @@ -189,7 +199,7 @@ function Base.propertynames(::Device, private::Bool=false)
:has_image_support, :has_local_mem, :host_unified_memory, :available,
:compiler_available, :non_uniform_work_group_support
# other
:extensions, :platform, :device_type, :max_work_item_size, :sub_group_sizes,
:extensions, :spirv_extensions, :platform, :device_type, :max_work_item_size, :sub_group_sizes,
:max_image2d_shape, :max_image3d_shape,
)
return private ? (fieldnames(Device)..., props...) : props
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,19 @@ end
error("Device does not support cl_intel_required_subgroup_size")
end

# `BFloat16` loads, stores and conversions; GPUCompiler widens the arithmetic to `float`.
# (spirv2clc has no bfloat16 support.)
supports_bfloat16 = !source && "SPV_KHR_bfloat16" in dev.spirv_extensions
if supports_bfloat16
extensions = union(something(extensions, String[]), ["SPV_KHR_bfloat16"])
end

# GPUCompiler adds the extensions that the atomics need
spirv_ext = extensions === nothing ? "" : join(("+$ext" for ext in extensions), ",")

# create GPUCompiler objects
target = SPIRVCompilerTarget(; version=SPIRV_VERSION, supports_fp16, supports_fp64,
supports_bfloat16,
validate=true, extensions=spirv_ext, atomics,
backend=llvm_to_spirv_backend,
driver=spirv_driver(dev), kwargs...)
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b"
CompilerCaching = "9db33cc3-5358-4881-8759-fa4194144afd"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down Expand Up @@ -36,6 +37,7 @@ OpenCL = {path = ".."}
SPIRVIntrinsics = {path = "../lib/intrinsics"}

[compat]
BFloat16s = "0.6"
IOCapture = "1"
Statistics = "1"
intel_opencl_rt_jll = "2026.1"
Expand Down
6 changes: 4 additions & 2 deletions test/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ end
# nothing enables native floating-point min/max
@test !occursin("min_max", GPUCompiler.spirv_extensions(OpenCL.compiler_config(dev).target))

# the user's extensions are kept, and the atomics add the ones they need
# the user's extensions are kept, next to the one for bfloat16 (on devices that support
# it), and the atomics add the ones they need
config = OpenCL.compiler_config(dev; extensions=["SPV_KHR_expect_assume"],
atomics=GPUCompiler.SPIRVAtomics(; fadd_f32_local=true))
bfloat16 = config.target.supports_bfloat16 ? ",+SPV_KHR_bfloat16" : ""
@test GPUCompiler.spirv_extensions(config.target) ==
"+SPV_KHR_expect_assume,+SPV_EXT_shader_atomic_float_add"
"+SPV_KHR_expect_assume$bfloat16,+SPV_EXT_shader_atomic_float_add"
end

# `@atomic` returns the old value when it uses an atomic operation, and the new one when it
Expand Down
106 changes: 106 additions & 0 deletions test/bfloat16.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# On Julia 1.11+, `BFloat16` is `Core.BFloat16`, which Julia lowers to LLVM's `bfloat`:
# storing it needs a device 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.

using BFloat16s

function bf16_copy(y, x)
i = get_global_id()
@inbounds y[i] = x[i]
return
end
function bf16_ops(out, x, y)
i = get_global_id()
@inbounds begin
a, b = x[i], y[i]
out[1, i] = a * b + 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] = -a
end
return
end
function bf16_convert(y, x)
i = get_global_id()
@inbounds y[i] = convert(eltype(y), x[i])
return
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)))

# 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

if !isdefined(Core, :BFloat16) || OpenCL.compiler_config(cl.device()).target.supports_bfloat16
n = 1024
hx, hy = 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 = CLArray(hx), CLArray(hy)

@testset "copy" begin
out = similar(x)
@opencl global_size = n bf16_copy(out, x)
@test isequal(Array(out), hx)
end

@testset "arithmetic" begin
out = CLArray{BFloat16}(undef, 9, n)
@opencl global_size = n bf16_ops(out, x, y)
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)...))
# (multiplied and added separately, each rounded to bfloat)
@test isequal(o[1, :], map((a, b) -> f32(+)(f32(*)(a, b), b), 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(-), hx))
end

# including inputs that a conversion through `Float32` would round wrongly
conversions = (
(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)]),
)
fp64 = "cl_khr_fp64" in cl.device().extensions
@testset "$T to BFloat16" for (T, extra) in filter(c -> fp64 || c[1] != Float64, conversions)
vals = T <: AbstractFloat ? T.(randn(n) .* 1000) : rand(T, n)
vals[1:length(extra)] .= extra
dst = CLArray{BFloat16}(undef, n)
@opencl global_size = n bf16_convert(dst, CLArray(vals))
@test isequal(Array(dst), map(bf16_nearest, vals))
end

@testset "BFloat16 to Float32" begin
dst = CLArray{Float32}(undef, n)
@opencl global_size = n bf16_convert(dst, x)
@test isequal(Array(dst), map(Float32, hx))
end
else
# without `SPV_KHR_bfloat16` (or on the OpenCL C source path), compiling must fail
# cleanly, rather than the driver aborting on a rejected extension
x = CLArray(BFloat16.(Float32[1, 2, 3, 4]))
@test_throws OpenCL.GPUCompiler.InvalidIRError @opencl global_size = 4 bf16_copy(similar(x), x)
end
Loading