-
Notifications
You must be signed in to change notification settings - Fork 15
[WIP] GPU support through KernelAbstractions #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
lkdvos
wants to merge
17
commits into
main
Choose a base branch
from
ld-gpu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e9cd9c1
Add GPU mapreduce support via KernelAbstractions kernel
lkdvos 96b7d09
Add GPU mapreduce tests using JLArrays
lkdvos 5e69002
Fix GPU mapreduce: scalar indexing, output type, test expectations
lkdvos b21ea74
Use GPUArrays.neutral_element for _mapreduce init value
lkdvos 7b30822
Extend GPU mapreduce tests with nontrivial strides and offsets
lkdvos 4f66fac
Restrict GPU _mapreduce_fuse! dispatch to all-GPU input arrays
lkdvos 32b66fb
Introduce GPUStridedView type alias to reduce verbosity
lkdvos eec30de
some test reworking
lkdvos 936c38e
hijack some more linearalgebra methods
lkdvos c935288
bypass something
lkdvos 42bb19b
correctly allocate output type
lkdvos 5f991ee
formatter
lkdvos 87a801c
remove duplicate definitions
lkdvos 971056c
add Metal to test deps
lkdvos 3872de4
remove fill! specialization
lkdvos 90521ee
remove some GPU specializations
lkdvos 4f2f364
cleanup tests
lkdvos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,44 +3,154 @@ module StridedGPUArraysExt | |
| using Strided, GPUArrays | ||
| using GPUArrays: Adapt, KernelAbstractions | ||
| using GPUArrays.KernelAbstractions: @kernel, @index | ||
| using StridedViews: ParentIndex | ||
|
|
||
| ALL_FS = Union{typeof(adjoint), typeof(conj), typeof(identity), typeof(transpose)} | ||
|
|
||
| KernelAbstractions.get_backend(sv::StridedView{T, N, TA}) where {T, N, TA <: AnyGPUArray{T}} = KernelAbstractions.get_backend(parent(sv)) | ||
| # StridedView backed by any GPU array type, with element type linked to the parent. | ||
| const GPUStridedView{T, N} = StridedView{T, N, <:AnyGPUArray{T}} | ||
|
|
||
| function Base.Broadcast.BroadcastStyle(gpu_sv::StridedView{T, N, TA}) where {T, N, TA <: AnyGPUArray{T}} | ||
| raw_style = Base.Broadcast.BroadcastStyle(TA) | ||
| return typeof(raw_style)(Val(N)) # sets the dimensionality correctly | ||
| end | ||
| KernelAbstractions.get_backend(sv::GPUStridedView) = KernelAbstractions.get_backend(parent(sv)) | ||
|
|
||
| function Base.copy!(dst::AbstractArray{TD, ND}, src::StridedView{TS, NS, TAS, FS}) where {TD <: Number, ND, TS <: Number, NS, TAS <: AbstractGPUArray{TS}, FS <: ALL_FS} | ||
| bc_style = Base.Broadcast.BroadcastStyle(TAS) | ||
| bc = Base.Broadcast.Broadcasted(bc_style, identity, (src,), axes(dst)) | ||
| GPUArrays._copyto!(dst, bc) | ||
| return dst | ||
| end | ||
|
|
||
| # lifted from GPUArrays.jl | ||
| function Base.fill!(A::StridedView{T, N, TA, F}, x) where {T, N, TA <: AbstractGPUArray{T}, F <: ALL_FS} | ||
| isempty(A) && return A | ||
| @kernel function fill_kernel!(a, val) | ||
| idx = @index(Global, Cartesian) | ||
| @inbounds a[idx] = val | ||
| end | ||
| # ndims check for 0D support | ||
| kernel = fill_kernel!(KernelAbstractions.get_backend(A)) | ||
| f_x = F <: Union{typeof(conj), typeof(adjoint)} ? conj(x) : x | ||
| kernel(A, f_x; ndrange = size(A)) | ||
| return A | ||
| # Conversion to CPU Array: materialise into a contiguous GPU array first (so the | ||
| # GPU-to-GPU copy! path is used), then let the GPU array type handle the transfer. | ||
| function Base.Array(a::GPUStridedView) | ||
| b = similar(parent(a), eltype(a), size(a)) | ||
| copy!(StridedView(b), a) | ||
| return Array(b) | ||
| end | ||
|
|
||
| function Strided.__mul!( | ||
| C::StridedView{TC, 2, <:AnyGPUArray{TC}}, | ||
| A::StridedView{TA, 2, <:AnyGPUArray{TA}}, | ||
| B::StridedView{TB, 2, <:AnyGPUArray{TB}}, | ||
| C::GPUStridedView{TC, 2}, | ||
| A::GPUStridedView{TA, 2}, | ||
| B::GPUStridedView{TB, 2}, | ||
| α::Number, β::Number | ||
| ) where {TC, TA, TB} | ||
| return GPUArrays.generic_matmatmul!(C, A, B, α, β) | ||
| end | ||
|
|
||
| # ---------- GPU mapreduce support ---------- | ||
|
|
||
| @inline _gpu_init_acc(::Nothing, current_val) = current_val | ||
| @inline _gpu_init_acc(initop, current_val) = initop(current_val) | ||
|
|
||
| @inline _gpu_accum(::Nothing, acc, val) = val | ||
| @inline _gpu_accum(op, acc, val) = op(acc, val) | ||
|
|
||
| @inline function _strides_dot(strides::NTuple{N, Int}, cidx::CartesianIndex{N}) where {N} | ||
| s = 0 | ||
| for d in Base.OneTo(N) | ||
| @inbounds s += strides[d] * (cidx[d] - 1) | ||
| end | ||
| return s | ||
| end | ||
|
|
||
| @kernel function _mapreduce_gpu_kernel!( | ||
| f, op, initop, | ||
| dims::NTuple{N, Int}, | ||
| out::OT, | ||
| inputs::IT | ||
| ) where {N, OT <: StridedView, IT <: Tuple} | ||
|
|
||
| out_linear = @index(Global, Linear) | ||
|
|
||
| # Non-reduction subspace sizes (1 for reduction dims) | ||
| nred_sizes = ntuple(Val(N)) do d | ||
| @inbounds iszero(out.strides[d]) ? 1 : dims[d] | ||
| end | ||
| # Reduction subspace sizes (1 for non-reduction dims) | ||
| red_sizes = ntuple(Val(N)) do d | ||
| @inbounds iszero(out.strides[d]) ? dims[d] : 1 | ||
| end | ||
|
|
||
| # Map out_linear → cartesian in non-reduction subspace | ||
| nred_cidx = CartesianIndices(nred_sizes)[out_linear] | ||
| out_parent = out.offset + 1 + _strides_dot(out.strides, nred_cidx) | ||
|
|
||
| # Initialize accumulator from current output value (or apply initop) | ||
| @inbounds acc = _gpu_init_acc(initop, out[ParentIndex(out_parent)]) | ||
|
|
||
| # Sequential reduction loop over reduction subspace | ||
| @inbounds for red_linear in Base.OneTo(prod(red_sizes)) | ||
| red_cidx = CartesianIndices(red_sizes)[red_linear] | ||
| complete_cidx = CartesianIndex( | ||
| ntuple(Val(N)) do d | ||
| @inbounds nred_cidx[d] + red_cidx[d] - 1 | ||
| end | ||
| ) | ||
|
|
||
| val = f( | ||
| ntuple(Val(length(inputs))) do m | ||
| @inbounds begin | ||
| a = inputs[m] | ||
| ip = a.offset + 1 + _strides_dot(a.strides, complete_cidx) | ||
| a[ParentIndex(ip)] | ||
| end | ||
| end... | ||
| ) | ||
|
|
||
| acc = _gpu_accum(op, acc, val) | ||
| end | ||
|
|
||
| @inbounds out[ParentIndex(out_parent)] = acc | ||
| end | ||
|
|
||
| # GPU-compatible _mapreduce: avoids scalar indexing (first(A), out[ParentIndex(1)]) | ||
| # that JLArrays/real GPUs prohibit. Mirrors GPUArrays' neutral_element approach: | ||
| # infer output type via Broadcast machinery, look up the neutral element (errors on | ||
| # unknown ops), fill the output buffer, then read back a single scalar via Array(). | ||
| function Strided._mapreduce( | ||
| f, op, A::GPUStridedView{T, N}, nt = nothing | ||
| ) where {T, N} | ||
| if length(A) == 0 | ||
| b = Base.mapreduce_empty(f, op, T) | ||
| return nt === nothing ? b : op(b, nt.init) | ||
| end | ||
|
|
||
| dims = size(A) | ||
|
|
||
| if nt === nothing | ||
| ET = Base.Broadcast.combine_eltypes(f, (A,)) | ||
| ET = Base.promote_op(op, ET, ET) | ||
| (ET === Union{} || ET === Any) && | ||
| error("cannot infer output element type for mapreduce; pass an explicit `init`") | ||
| init = GPUArrays.neutral_element(op, ET) | ||
| else | ||
| ET = typeof(nt.init) | ||
| init = nt.init | ||
| end | ||
|
|
||
| out = similar(parent(A), ET, (1,)) | ||
| fill!(out, init) | ||
|
|
||
| Strided._mapreducedim!(f, op, nothing, dims, (sreshape(StridedView(out), one.(dims)), A)) | ||
|
|
||
| return Array(out)[1] | ||
| end | ||
|
|
||
| function Strided._mapreduce_fuse!( | ||
| f, op, initop, | ||
| dims::Dims{N}, | ||
| arrays::Tuple{GPUStridedView{TO, N}, Vararg{GPUStridedView{<:Any, N}}} | ||
| ) where {TO, N} | ||
|
|
||
| out = arrays[1] | ||
| inputs_raw = Base.tail(arrays) | ||
| M = length(inputs_raw) | ||
| inputs = ntuple(i -> inputs_raw[i], Val(M)) | ||
|
|
||
| # Number of output elements = product of non-reduction dims | ||
| out_total = prod( | ||
| ntuple(Val(N)) do d | ||
| @inbounds iszero(out.strides[d]) ? 1 : dims[d] | ||
| end | ||
| ) | ||
|
Comment on lines
+143
to
+147
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
|
||
| backend = KernelAbstractions.get_backend(parent(out)) | ||
| kernel! = _mapreduce_gpu_kernel!(backend) | ||
| kernel!(f, op, initop, dims, out, inputs; ndrange = out_total) | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the generic
_mapreduce_fuse!step is still valid forGPUStridedViewobjects, so maybe_mapreduce_order!is where the lowering could be intercepted forGPUStridedView?