Skip to content

KernelAbstractions 0.10 - #688

Open
christiangnrd wants to merge 8 commits into
mainfrom
kaintr
Open

KernelAbstractions 0.10#688
christiangnrd wants to merge 8 commits into
mainfrom
kaintr

Conversation

@christiangnrd

Copy link
Copy Markdown
Member

Not a draft to also run benchmarks

@github-actions

github-actions Bot commented Oct 22, 2025

Copy link
Copy Markdown
Contributor

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic main) to apply these changes.

Click here to view the suggested changes.
diff --git a/src/MetalKernels.jl b/src/MetalKernels.jl
index 4e856194..7573c5e1 100644
--- a/src/MetalKernels.jl
+++ b/src/MetalKernels.jl
@@ -136,26 +136,26 @@ end
 
 KI.argconvert(::MetalBackend, arg) = mtlconvert(arg)
 
-function KI.kernel_function(::MetalBackend, f::F, tt::TT=Tuple{}; name=nothing, kwargs...) where {F,TT}
+function KI.kernel_function(::MetalBackend, f::F, tt::TT = Tuple{}; name = nothing, kwargs...) where {F, TT}
     kern = mtlfunction(f, tt; name, kwargs...)
-    KI.Kernel{MetalBackend, typeof(kern)}(MetalBackend(), kern)
+    return KI.Kernel{MetalBackend, typeof(kern)}(MetalBackend(), kern)
 end
 
-function (obj::KI.Kernel{MetalBackend})(args...; numworkgroups=1, workgroupsize=1)
+function (obj::KI.Kernel{MetalBackend})(args...; numworkgroups = 1, workgroupsize = 1)
     KI.check_launch_args(numworkgroups, workgroupsize)
 
-    obj.kern(args...; threads=workgroupsize, groups=numworkgroups)
+    return obj.kern(args...; threads = workgroupsize, groups = numworkgroups)
 end
 
 
-function KI.kernel_max_work_group_size(kikern::KI.Kernel{<:MetalBackend}; max_work_items::Int=typemax(Int))::Int
-    Int(min(kikern.kern.pipeline.maxTotalThreadsPerThreadgroup, max_work_items))
+function KI.kernel_max_work_group_size(kikern::KI.Kernel{<:MetalBackend}; max_work_items::Int = typemax(Int))::Int
+    return Int(min(kikern.kern.pipeline.maxTotalThreadsPerThreadgroup, max_work_items))
 end
 function KI.max_work_group_size(::MetalBackend)::Int
-    Int(device().maxThreadsPerThreadgroup.width)
+    return Int(device().maxThreadsPerThreadgroup.width)
 end
 function KI.multiprocessor_count(::MetalBackend)::Int
-    Metal.num_gpu_cores()
+    return Metal.num_gpu_cores()
 end
 
 
diff --git a/src/broadcast.jl b/src/broadcast.jl
index 5d107ec2..3455fad2 100644
--- a/src/broadcast.jl
+++ b/src/broadcast.jl
@@ -66,9 +66,9 @@ end
     if _broadcast_shapes[Is] > BROADCAST_SPECIALIZATION_THRESHOLD
         ## COV_EXCL_START
         function broadcast_cartesian_static(dest, bc, Is)
-             i = KI.get_global_id().x
-             stride = KI.get_global_size().x
-             while 1 <= i <= length(dest)
+            i = KI.get_global_id().x
+            stride = KI.get_global_size().x
+            while 1 <= i <= length(dest)
                 I = @inbounds Is[i]
                 @inbounds dest[I] = bc[I]
                 i += stride
@@ -91,13 +91,13 @@ end
        (isa(IndexStyle(dest), IndexLinear) && isa(IndexStyle(bc), IndexLinear))
         ## COV_EXCL_START
         function broadcast_linear(dest, bc)
-             i = KI.get_global_id().x
-             stride = KI.get_global_size().x
-             while 1 <= i <= length(dest)
-                 @inbounds dest[i] = bc[i]
-                 i += stride
-             end
-             return
+            i = KI.get_global_id().x
+            stride = KI.get_global_size().x
+            while 1 <= i <= length(dest)
+                @inbounds dest[i] = bc[i]
+                i += stride
+            end
+            return
         end
         ## COV_EXCL_STOP
 
@@ -168,9 +168,9 @@ end
     else
         ## COV_EXCL_START
         function broadcast_cartesian(dest, bc)
-             i = KI.get_global_id().x
-             stride = KI.get_global_size().x
-             while 1 <= i <= length(dest)
+            i = KI.get_global_id().x
+            stride = KI.get_global_size().x
+            while 1 <= i <= length(dest)
                 I = @inbounds CartesianIndices(dest)[i]
                 @inbounds dest[I] = bc[I]
                 i += stride
diff --git a/src/device/random.jl b/src/device/random.jl
index 12b053a2..edc999cd 100644
--- a/src/device/random.jl
+++ b/src/device/random.jl
@@ -89,8 +89,8 @@ end
         @inbounds global_random_counters()[simdgroupId]
     elseif field === :ctr2
         globalId = KI.get_global_id().x +
-                   (KI.get_global_id().y - 1i32) * KI.get_global_size().x +
-                   (KI.get_global_id().z - 1i32) * KI.get_global_size().x * KI.get_global_size().y
+            (KI.get_global_id().y - 1i32) * KI.get_global_size().x +
+            (KI.get_global_id().z - 1i32) * KI.get_global_size().x * KI.get_global_size().y
         globalId % UInt32
     end::UInt32
 end
diff --git a/src/mapreduce.jl b/src/mapreduce.jl
index 7be5ef43..a737e8d0 100644
--- a/src/mapreduce.jl
+++ b/src/mapreduce.jl
@@ -224,7 +224,8 @@ function GPUArrays.mapreducedim!(f::F, op::OP, R::WrappedMtlArray{T},
     # we might not be able to launch all those threads to reduce each slice in one go.
     # that's why each threads also loops across their inputs, processing multiple values
     # so that we can span the entire reduction dimension using a single item group.
-    kernel = KI.@kernel backend launch = false partial_mapreduce_device(f, op, init, Val(maxthreads), Val(Rreduce), Val(Rother),
+    kernel = KI.@kernel backend launch = false partial_mapreduce_device(
+        f, op, init, Val(maxthreads), Val(Rreduce), Val(Rother),
                                                           Val(UInt64(length(Rother))), Val(grain), Val(shuffle), R, A)
 
     # how many threads do we want?
@@ -260,7 +261,8 @@ function GPUArrays.mapreducedim!(f::F, op::OP, R::WrappedMtlArray{T},
         # we can cover the dimensions to reduce using a single group
         kernel(f, op, init, Val(maxthreads), Val(Rreduce), Val(Rother),
                Val(UInt64(length(Rother))), Val(grain), Val(shuffle), R, A;
-               workgroupsize = threads, numworkgroups = groups)
+            workgroupsize = threads, numworkgroups = groups
+        )
     else
         # temporary empty array whose type will match the final partial array
 	    partial = similar(R, ntuple(_ -> 0, Val(ndims(R)+1)))
@@ -287,7 +289,8 @@ function GPUArrays.mapreducedim!(f::F, op::OP, R::WrappedMtlArray{T},
         partial_kernel(f, op, init, Val(threads), Val(Rreduce),
                         Val(Rother), Val(UInt64(length(Rother))),
                         Val(grain), Val(shuffle), partial, A;
-                        numworkgroups = partial_groups, workgroupsize = partial_threads)
+            numworkgroups = partial_groups, workgroupsize = partial_threads
+        )
 
         GPUArrays.mapreducedim!(identity, op, R, partial; init=init)
     end
diff --git a/test/kernelabstractions.jl b/test/kernelabstractions.jl
index cda5b249..339fcbc8 100644
--- a/test/kernelabstractions.jl
+++ b/test/kernelabstractions.jl
@@ -7,6 +7,6 @@ Testsuite.testsuite(MetalBackend, "Metal", Metal, MtlArray, Metal.MtlDeviceArray
     "Convert",           # depends on https://github.com/JuliaGPU/Metal.jl/issues/69
     "SpecialFunctions",  # gamma and erfc not currently supported on Metal.jl
     "sparse",            # not supported yet
-    "CPU synchronization",
-    "fallback test: callable types",
+            "CPU synchronization",
+            "fallback test: callable types",
 ]))
diff --git a/test/runtests.jl b/test/runtests.jl
index 32b45c8c..14fcfb93 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -1,6 +1,6 @@
 @static if VERSION < v"1.11" && get(ENV, "BUILDKITE_PIPELINE_NAME", "Metal.jl") == "Metal.jl"
     using Pkg
-    Pkg.add(url="https://github.com/JuliaGPU/KernelAbstractions.jl", rev="main")
+    Pkg.add(url = "https://github.com/JuliaGPU/KernelAbstractions.jl", rev = "main")
 end
 
 using Metal

@christiangnrd
christiangnrd force-pushed the kaintr branch 3 times, most recently from 9ac3d49 to 6314372 Compare October 22, 2025 04:31

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metal Benchmarks

Details
Benchmark suite Current: 2d2a094 Previous: 7c5b7d4 Ratio
array/accumulate/Float32/1d 404167 ns 405125 ns 1.00
array/accumulate/Float32/dims=1 378125 ns 380709 ns 0.99
array/accumulate/Float32/dims=1L 8801292 ns 8813708 ns 1.00
array/accumulate/Float32/dims=2 439792 ns 449833 ns 0.98
array/accumulate/Float32/dims=2L 2633125 ns 2672417 ns 0.99
array/accumulate/Int64/1d 914792 ns 857042 ns 1.07
array/accumulate/Int64/dims=1 849584 ns 934334 ns 0.91
array/accumulate/Int64/dims=1L 9677125 ns 9530500 ns 1.02
array/accumulate/Int64/dims=2 1390584 ns 1253708 ns 1.11
array/accumulate/Int64/dims=2L 6523083 ns 6539791 ns 1.00
array/broadcast 210792 ns 186583 ns 1.13
array/construct 2166 ns 2208 ns 0.98
array/permutedims/2d 672167 ns 545333 ns 1.23
array/permutedims/3d 1237916 ns 1066167 ns 1.16
array/permutedims/4d 1427709 ns 1252917 ns 1.14
array/private/copy 239500 ns 235208 ns 1.02
array/private/copyto!/cpu_to_gpu 195500 ns 199791 ns 0.98
array/private/copyto!/gpu_to_cpu 196250 ns 199709 ns 0.98
array/private/copyto!/gpu_to_gpu 197584 ns 196458 ns 1.01
array/private/iteration/findall/bool 1124333 ns 1075000 ns 1.05
array/private/iteration/findall/int 1281500 ns 1238042 ns 1.04
array/private/iteration/findfirst/bool 1182208 ns 1101750 ns 1.07
array/private/iteration/findfirst/int 1165625 ns 1119041 ns 1.04
array/private/iteration/findmin/1d 1295500 ns 1239875 ns 1.04
array/private/iteration/findmin/2d 1091417 ns 1086292 ns 1.00
array/private/iteration/logical 1808459 ns 1752250 ns 1.03
array/private/iteration/scalar 1175500 ns 1229458 ns 0.96
array/random/rand/Float32 429500 ns 430042 ns 1.00
array/random/rand/Int64 519875 ns 526708 ns 0.99
array/random/rand!/Float32 320458 ns 370666 ns 0.86
array/random/rand!/Int64 316584 ns 401208 ns 0.79
array/random/randn/Float32 388458 ns 388208 ns 1.00
array/random/randn!/Float32 334958 ns 322500 ns 1.04
array/reductions/mapreduce/Float32/1d 400875 ns 410500 ns 0.98
array/reductions/mapreduce/Float32/dims=1 331584 ns 297000 ns 1.12
array/reductions/mapreduce/Float32/dims=1L 625709 ns 635084 ns 0.99
array/reductions/mapreduce/Float32/dims=2 370750 ns 332959 ns 1.11
array/reductions/mapreduce/Float32/dims=2L 1033833 ns 1023375 ns 1.01
array/reductions/mapreduce/Int64/1d 577292 ns 586875 ns 0.98
array/reductions/mapreduce/Int64/dims=1 597625 ns 620583 ns 0.96
array/reductions/mapreduce/Int64/dims=1L 1040250 ns 1044000 ns 1.00
array/reductions/mapreduce/Int64/dims=2 725625 ns 541875 ns 1.34
array/reductions/mapreduce/Int64/dims=2L 2257500 ns 2194542 ns 1.03
array/reductions/reduce/Float32/1d 406250 ns 405708 ns 1.00
array/reductions/reduce/Float32/dims=1 333542 ns 283625 ns 1.18
array/reductions/reduce/Float32/dims=1L 630416 ns 638291 ns 0.99
array/reductions/reduce/Float32/dims=2 226709 ns 226208 ns 1.00
array/reductions/reduce/Float32/dims=2L 457708 ns 470917 ns 0.97
array/reductions/reduce/Int64/1d 575834 ns 543209 ns 1.06
array/reductions/reduce/Int64/dims=1 601542 ns 633083 ns 0.95
array/reductions/reduce/Int64/dims=1L 1038167 ns 1033417 ns 1.00
array/reductions/reduce/Int64/dims=2 244792 ns 233958 ns 1.05
array/reductions/reduce/Int64/dims=2L 652083 ns 662833 ns 0.98
array/shared/copy 135292 ns 135666 ns 1.00
array/shared/copyto!/cpu_to_gpu 37958 ns 37250 ns 1.02
array/shared/copyto!/gpu_to_cpu 38333 ns 37500 ns 1.02
array/shared/copyto!/gpu_to_gpu 38625 ns 38375 ns 1.01
array/shared/iteration/findall/bool 1150416 ns 1091042 ns 1.05
array/shared/iteration/findall/int 1293833 ns 1249125 ns 1.04
array/shared/iteration/findfirst/bool 855208 ns 902250 ns 0.95
array/shared/iteration/findfirst/int 872750 ns 977458 ns 0.89
array/shared/iteration/findmin/1d 1162584 ns 866167 ns 1.34
array/shared/iteration/findmin/2d 1091625 ns 791583 ns 1.38
array/shared/iteration/logical 1679250 ns 1582166 ns 1.06
array/shared/iteration/scalar 3593.75 ns 3890.625 ns 0.92
array/sorting/1d 1785375 ns 1624625 ns 1.10
array/sorting/2d 8309958 ns 8289250 ns 1.00
integration/byval/reference 1112209 ns 1110334 ns 1.00
integration/byval/slices=1 1113333 ns 1116542 ns 1.00
integration/byval/slices=2 2032250 ns 2020917 ns 1.01
integration/byval/slices=3 6877709 ns 9519250 ns 0.72
integration/metaldevrt 374875 ns 312708 ns 1.20
kernel/indexing 182917 ns 191000 ns 0.96
kernel/indexing_checked 351667 ns 366959 ns 0.96
kernel/launch 1895.9 ns 1850 ns 1.02
kernel/rand 375208 ns 377666 ns 0.99
latency/import 2125140000 ns 2163364250 ns 0.98
latency/precompile 54332746125 ns 42157958500 ns 1.29
latency/ttfp 2562425125 ns 2484808458 ns 1.03
metal/synchronization/context 644.2095808383234 ns 665.6139240506329 ns 0.97
metal/synchronization/stream 335.6090909090909 ns 338.27853881278537 ns 0.99

This comment was automatically generated by workflow using github-action-benchmark.

@christiangnrd
christiangnrd force-pushed the kaintr branch 2 times, most recently from 89a8970 to f33628b Compare July 15, 2026 13:47
@christiangnrd
christiangnrd force-pushed the kaintr branch 5 times, most recently from 7f653a1 to 19fa603 Compare July 22, 2026 17:24
@christiangnrd christiangnrd changed the title [Do not merge] Test KernelIntrinsics [Do not merge] Test KernelInterface Jul 28, 2026
@christiangnrd
christiangnrd force-pushed the kaintr branch 2 times, most recently from 88a98ca to ef90b50 Compare July 30, 2026 19:37
@christiangnrd christiangnrd changed the title [Do not merge] Test KernelInterface [Do not merge] KernelAbstractions 0.10 Aug 16, 2026
@christiangnrd christiangnrd changed the title [Do not merge] KernelAbstractions 0.10 KernelAbstractions 0.10 Aug 16, 2026
@christiangnrd
christiangnrd force-pushed the kaintr branch 6 times, most recently from 3691127 to 083f1a7 Compare August 18, 2026 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant