Skip to content

[Do not merge] Test KernelIntrinsics#688

Open
christiangnrd wants to merge 10 commits into
mainfrom
kaintr
Open

[Do not merge] Test KernelIntrinsics#688
christiangnrd wants to merge 10 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: 7f653a1 Previous: ac0202d Ratio
array/accumulate/Float32/1d 406916 ns 407375 ns 1.00
array/accumulate/Float32/dims=1 377959 ns 351791 ns 1.07
array/accumulate/Float32/dims=1L 8804750 ns 8796584 ns 1.00
array/accumulate/Float32/dims=2 451000 ns 439792 ns 1.03
array/accumulate/Float32/dims=2L 2686083 ns 2603208 ns 1.03
array/accumulate/Int64/1d 913375 ns 842333 ns 1.08
array/accumulate/Int64/dims=1 1060375 ns 944125 ns 1.12
array/accumulate/Int64/dims=1L 9665667 ns 9521000 ns 1.02
array/accumulate/Int64/dims=2 1035667 ns 1247208 ns 0.83
array/accumulate/Int64/dims=2L 6523042 ns 6499125 ns 1.00
array/broadcast 230167 ns 222000 ns 1.04
array/construct 2333 ns 2417 ns 0.97
array/permutedims/2d 525334 ns 449458 ns 1.17
array/permutedims/3d 1127917 ns 1011000 ns 1.12
array/permutedims/4d 1479042 ns 1275416 ns 1.16
array/private/copy 239167 ns 229791 ns 1.04
array/private/copyto!/cpu_to_gpu 199417 ns 195833 ns 1.02
array/private/copyto!/gpu_to_cpu 196875 ns 194167 ns 1.01
array/private/copyto!/gpu_to_gpu 212042 ns 173708 ns 1.22
array/private/iteration/findall/bool 1146458 ns 1057250 ns 1.08
array/private/iteration/findall/int 1290083 ns 1228125 ns 1.05
array/private/iteration/findfirst/bool 1162291 ns 1111542 ns 1.05
array/private/iteration/findfirst/int 1185208 ns 1117792 ns 1.06
array/private/iteration/findmin/1d 1283458 ns 1229625 ns 1.04
array/private/iteration/findmin/2d 785458 ns 769709 ns 1.02
array/private/iteration/logical 1815791 ns 1688959 ns 1.08
array/private/iteration/scalar 1250250 ns 1216958 ns 1.03
array/random/rand/Float32 398916 ns 426125 ns 0.94
array/random/rand/Int64 519625 ns 513417 ns 1.01
array/random/rand!/Float32 381833 ns 313042 ns 1.22
array/random/rand!/Int64 409917 ns 406791 ns 1.01
array/random/randn/Float32 340875 ns 391833 ns 0.87
array/random/randn!/Float32 356375 ns 340208 ns 1.05
array/reductions/mapreduce/Float32/1d 439500 ns 383792 ns 1.15
array/reductions/mapreduce/Float32/dims=1 269083 ns 263000 ns 1.02
array/reductions/mapreduce/Float32/dims=1L 625417 ns 621791 ns 1.01
array/reductions/mapreduce/Float32/dims=2 362417 ns 275708 ns 1.31
array/reductions/mapreduce/Float32/dims=2L 1050208 ns 841125 ns 1.25
array/reductions/mapreduce/Int64/1d 614750 ns 621125 ns 0.99
array/reductions/mapreduce/Int64/dims=1 440208 ns 450958 ns 0.98
array/reductions/mapreduce/Int64/dims=1L 1031625 ns 1026500 ns 1.00
array/reductions/mapreduce/Int64/dims=2 570334 ns 778292 ns 0.73
array/reductions/mapreduce/Int64/dims=2L 2246167 ns 2179166 ns 1.03
array/reductions/reduce/Float32/1d 437750 ns 412916 ns 1.06
array/reductions/reduce/Float32/dims=1 287917 ns 345084 ns 0.83
array/reductions/reduce/Float32/dims=1L 632167 ns 635167 ns 1.00
array/reductions/reduce/Float32/dims=2 231458 ns 228750 ns 1.01
array/reductions/reduce/Float32/dims=2L 450125 ns 448708 ns 1.00
array/reductions/reduce/Int64/1d 593875 ns 576542 ns 1.03
array/reductions/reduce/Int64/dims=1 613375 ns 614541 ns 1.00
array/reductions/reduce/Int64/dims=1L 1024666 ns 1030125 ns 0.99
array/reductions/reduce/Int64/dims=2 244459 ns 240250 ns 1.02
array/reductions/reduce/Int64/dims=2L 655375 ns 644833 ns 1.02
array/shared/copy 136625 ns 136583 ns 1.00
array/shared/copyto!/cpu_to_gpu 37708 ns 38292 ns 0.98
array/shared/copyto!/gpu_to_cpu 38000 ns 38167 ns 1.00
array/shared/copyto!/gpu_to_gpu 38292 ns 38917 ns 0.98
array/shared/iteration/findall/bool 1142708 ns 1076292 ns 1.06
array/shared/iteration/findall/int 1304458 ns 1220458 ns 1.07
array/shared/iteration/findfirst/bool 1002791 ns 956459 ns 1.05
array/shared/iteration/findfirst/int 848000 ns 650041 ns 1.30
array/shared/iteration/findmin/1d 1152333 ns 1089541 ns 1.06
array/shared/iteration/findmin/2d 1088958 ns 1075500 ns 1.01
array/shared/iteration/logical 1446500 ns 1553459 ns 0.93
array/shared/iteration/scalar 4160.714285714285 ns 4023.714285714286 ns 1.03
array/sorting/1d 1603541 ns 1786083 ns 0.90
array/sorting/2d 8302125 ns 8305833 ns 1.00
integration/byval/reference 1111167 ns 1119042 ns 0.99
integration/byval/slices=1 1104792 ns 1109583 ns 1.00
integration/byval/slices=2 2022583 ns 2021291 ns 1.00
integration/byval/slices=3 7723750 ns 6610875 ns 1.17
integration/metaldevrt 387458 ns 379833 ns 1.02
kernel/indexing 207125 ns 208708 ns 0.99
kernel/indexing_checked 374583 ns 370667 ns 1.01
kernel/launch 1891.6 ns 1883.3 ns 1.00
kernel/rand 393750 ns 393792 ns 1.00
latency/import 2208580083 ns 2038510291 ns 1.08
latency/precompile 44965841000 ns 38792921333 ns 1.16
latency/ttfp 2539808458 ns 2354706125 ns 1.08
metal/synchronization/context 583.5635359116022 ns 583.7912087912088 ns 1.00
metal/synchronization/stream 331.4663677130045 ns 332.58744394618833 ns 1.00

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

@christiangnrd
christiangnrd force-pushed the kaintr branch 4 times, most recently from 9f95616 to fb61557 Compare June 12, 2026 15:19
@christiangnrd
christiangnrd force-pushed the kaintr branch 3 times, most recently from 86196fa to 9ee5724 Compare June 17, 2026 18:54
@christiangnrd
christiangnrd force-pushed the kaintr branch 2 times, most recently from b36b4ba to cf3cbfb Compare July 1, 2026 13:45
@christiangnrd
christiangnrd force-pushed the kaintr branch 3 times, most recently from dae5922 to e1c67c7 Compare July 2, 2026 15:31
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.20%. Comparing base (b70608b) to head (7f653a1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/MetalKernels.jl 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #688      +/-   ##
==========================================
- Coverage   86.21%   86.20%   -0.01%     
==========================================
  Files          76       76              
  Lines        5246     5264      +18     
==========================================
+ Hits         4523     4538      +15     
- Misses        723      726       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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 2 times, most recently from dc9333b to 52bdf27 Compare July 17, 2026 15:31
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