Skip to content

Support splatted keyword arguments in @cuda - #3661

Merged
maleadt merged 1 commit into
mainfrom
tb/kwargs-splat
Dec 3, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/kwargs-splat

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

@cuda only accepts keyword arguments that are written out, so a configuration held in a NamedTuple has to be spelled out one by one. Splatting it throws:

hints = (; occupancy=2, num_ctas=1)
@cuda backend=cuTile blocks=grid hints... kernel(a, b, c)
# ERROR: ArgumentError: Invalid keyword argument 'hints...'

cuTile.jl needs this for autotuning, where a configuration is a NamedTuple of compiler hints.

The macro routed keyword arguments to compilation or launch by name at expansion time, but a splat's names are only known at run time. It now uses GPUToolbox's parse_launch_macro (JuliaGPU/GPUToolbox.jl#48), and routes the keyword arguments of host launches at run time: they are evaluated into one NamedTuple, in the order written and with the rightmost occurrence of a name winning, and partitioned with partition_kwargs into launch keywords and compiler keywords (backend-specific ones included). For a NamedTuple the partition folds away at compile time. On an RTX 5080 a launch with written-out keywords took 1.44 µs before and after, and one with splatted keywords 1.45–1.47 µs, all without allocating.

launch, dynamic and backend still have to be written out, once; splatting them throws. Device-side launches (dynamic=true) don't accept splats.

This also changes a few things for calls without a splat:

  • compiler and launch keyword values are evaluated in source order, where they used to be grouped by destination;
  • launch=false together with a launch keyword is reported when the call executes instead of at macro expansion;
  • writing launch, dynamic or backend twice is an error, where the last one used to win;
  • the launch flag no longer resolves Val in the caller's scope.

Requires GPUToolbox 3.8.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Documentation preview: https://cuda.juliagpu.org/previews/PR3661/

@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.86%. Comparing base (3f940e3) to head (d28183b).

Files with missing lines Patch % Lines
CUDACore/src/compiler/execution.jl 94.59% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3661      +/-   ##
==========================================
- Coverage   88.51%   85.86%   -2.65%     
==========================================
  Files         203      203              
  Lines       22057    22042      -15     
==========================================
- Hits        19523    18926     -597     
- Misses       2534     3116     +582     

☔ 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.

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with b045a6451, the merge base.
Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance −0.1% 3 0 79
Latency −0.3% 0 0 3
Applications −0.0% 0 0 6
Benchmark Baseline This PR Change
Performance
kernel/indexing 11.7 µs 15.4 µs +31.2% 🔴 slower
kernel/indexing_checked 12.4 µs 16.0 µs +29.2% 🔴 slower
kernel/rand 16.3 µs 20.0 µs +23.2% 🔴 slower
All benchmarks
Benchmark Baseline This PR Change
Performance
array/accumulate/Float32/1d 44.5 µs 43.9 µs −1.2%
array/accumulate/Float32/dims=1 30.6 µs 30.5 µs −0.6%
array/accumulate/Float32/dims=1L 96.8 µs 96.9 µs +0.0%
array/accumulate/Float32/dims=2 49.0 µs 48.6 µs −0.9%
array/accumulate/Float32/dims=2L 364 µs 365 µs +0.3%
array/accumulate/Int64/1d 68.4 µs 67.9 µs −0.6%
array/accumulate/Int64/dims=1 41.0 µs 41.3 µs +0.7%
array/accumulate/Int64/dims=1L 140 µs 140 µs −0.0%
array/accumulate/Int64/dims=2 71.2 µs 71.1 µs −0.1%
array/accumulate/Int64/dims=2L 762 µs 763 µs +0.1%
array/broadcast 10.7 µs 10.5 µs −1.9%
array/broadcast launch 5.81 µs 5.74 µs −1.2%
array/construct 1.32 µs 1.32 µs +0.2%
array/copy 17.9 µs 17.7 µs −1.2%
array/copyto!/cpu_to_gpu 208 µs 208 µs +0.0%
array/copyto!/gpu_to_cpu 240 µs 241 µs +0.4%
array/copyto!/gpu_to_gpu 12.3 µs 12.1 µs −1.3%
array/iteration/findall/bool 48.7 µs 48.3 µs −0.9%
array/iteration/findall/int 57.8 µs 59.7 µs +3.3%
array/iteration/findfirst/bool 32.9 µs 32.6 µs −0.9%
array/iteration/findfirst/int 34.0 µs 33.6 µs −1.3%
array/iteration/findmin/1d 63.4 µs 63.7 µs +0.4%
array/iteration/findmin/2d 39.5 µs 39.0 µs −1.3%
array/iteration/logical 48.7 µs 49.8 µs +2.3%
array/iteration/scalar 57.1 µs 58.2 µs +1.9%
array/permutedims/2d 55.7 µs 55.2 µs −0.9%
array/permutedims/3d 52.2 µs 51.7 µs −1.0%
array/permutedims/4d 43.1 µs 42.7 µs −1.0%
array/random/rand!/Float32 9.78 µs 9.55 µs −2.3%
array/random/rand!/Int64 16.4 µs 16.3 µs −0.7%
array/random/rand/Float32 11.6 µs 11.6 µs +0.0%
array/random/rand/Int64 21.0 µs 20.6 µs −1.8%
array/random/randn!/Float32 29.2 µs 29.1 µs −0.4%
array/random/randn/Float32 39.2 µs 39.2 µs +0.0%
array/reductions/mapreduce/Float32/1d 30.8 µs 30.2 µs −2.0%
array/reductions/mapreduce/Float32/dims=1 17.6 µs 17.7 µs +0.2%
array/reductions/mapreduce/Float32/dims=1L 54.1 µs 53.9 µs −0.4%
array/reductions/mapreduce/Float32/dims=2 24.5 µs 24.5 µs +0.0%
array/reductions/mapreduce/Float32/dims=2L 61.2 µs 60.9 µs −0.4%
array/reductions/mapreduce/Int64/1d 31.4 µs 31.3 µs −0.2%
array/reductions/mapreduce/Int64/dims=1 19.9 µs 19.9 µs +0.1%
array/reductions/mapreduce/Int64/dims=1L 92.9 µs 92.5 µs −0.5%
array/reductions/mapreduce/Int64/dims=2 27.3 µs 26.9 µs −1.4%
array/reductions/mapreduce/Int64/dims=2L 96.8 µs 96.1 µs −0.7%
array/reductions/reduce/Float32/1d 30.5 µs 29.6 µs −3.0%
array/reductions/reduce/Float32/dims=1 17.3 µs 17.8 µs +3.4%
array/reductions/reduce/Float32/dims=1L 54.1 µs 54.2 µs +0.1%
array/reductions/reduce/Float32/dims=2 24.2 µs 24.3 µs +0.3%
array/reductions/reduce/Float32/dims=2L 61.0 µs 61.1 µs +0.1%
array/reductions/reduce/Int64/1d 31.3 µs 31.7 µs +1.3%
array/reductions/reduce/Int64/dims=1 19.7 µs 19.9 µs +1.3%
array/reductions/reduce/Int64/dims=1L 92.8 µs 93.0 µs +0.1%
array/reductions/reduce/Int64/dims=2 26.7 µs 26.8 µs +0.3%
array/reductions/reduce/Int64/dims=2L 96.6 µs 96.7 µs +0.2%
array/reverse/1d 19.5 µs 19.1 µs −2.0%
array/reverse/1d_inplace 12.1 µs 11.9 µs −1.8%
array/reverse/1dL 73.3 µs 72.9 µs −0.6%
array/reverse/1dL_inplace 68.8 µs 68.5 µs −0.4%
array/reverse/2d 23.2 µs 23.0 µs −0.9%
array/reverse/2d_inplace 15.2 µs 15.0 µs −1.6%
array/reverse/2dL 92.6 µs 92.6 µs −0.0%
array/reverse/2dL_inplace 69.7 µs 69.4 µs −0.4%
array/sorting/1d 827 µs 826 µs −0.2%
array/sorting/2d 248 µs 248 µs +0.0%
array/sorting/by 967 µs 966 µs −0.1%
cuda/graph/capture 26.4 µs 24.9 µs −5.8%
cuda/graph/eager 25.6 µs 24.3 µs −4.8%
cuda/graph/launch 16.5 µs 16.6 µs +0.8%
cuda/synchronization/context/auto 1.92 µs 1.89 µs −1.6%
cuda/synchronization/context/blocking 667 ns 662 ns −0.6%
cuda/synchronization/context/nonblocking 6.68 µs 4.35 µs −34.9%
cuda/synchronization/stream/auto 586 ns 578 ns −1.3%
cuda/synchronization/stream/blocking 538 ns 541 ns +0.6%
cuda/synchronization/stream/nonblocking 6.43 µs 6.65 µs +3.5%
kernel/indexing 11.7 µs 15.4 µs +31.2% 🔴 slower
kernel/indexing_checked 12.4 µs 16.0 µs +29.2% 🔴 slower
kernel/launch 1.51 µs 1.41 µs −6.6%
kernel/occupancy 30.0 ns 30.0 ns +0.0%
kernel/rand 16.3 µs 20.0 µs +23.2% 🔴 slower
Latency
import 5.25 s 5.22 s −0.5%
precompile 7.56 s 7.56 s +0.0%
ttfp 5.63 s 5.61 s −0.3%
Applications
byval/reference 145 µs 145 µs −0.1%
byval/slices=1 146 µs 146 µs +0.1%
byval/slices=2 286 µs 286 µs −0.0%
byval/slices=3 426 µs 426 µs +0.0%
cudadevrt 104 µs 104 µs −0.2%
volumerhs 10.3 ms 10.3 ms +0.0%

History: https://juliagpu.org/perf/?pkg=cuda

`@cuda` only accepted keyword arguments written out as `key=value`, so
a launch configuration or compiler options held in a `NamedTuple`, such
as the configurations of an autotuner, had to be spelled out one by
one. Splatting it, as in `@cuda blocks=n config... kernel(a)`, threw
an "Invalid keyword argument" error, because the macro routes keyword
arguments to the launch or the compiler by name when it is expanded,
and the names in a splat are only known at run time.

Port the macro to GPUToolbox's `parse_launch_macro`, and route the
keyword arguments of host-side launches at run time instead: they are
evaluated together into one `NamedTuple`, so that they are evaluated
in the order they are written and repeated names resolve as in a
function call, and partitioned with `partition_kwargs`. For a
`NamedTuple` this folds away at compile time, so launches are as fast
as before and don't allocate. As a consequence, launch keyword
arguments with `launch=false` are now reported when the launch
executes rather than when the macro is expanded. `launch`, `dynamic`
and `backend` must still be written out, once, and device-side
launches (`dynamic=true`) don't accept splats.

This also passes the launch flag as a `Val` instance rather than a call
to `Val`, which resolved in the caller's scope.
@maleadt
maleadt merged commit b5d79ff into main Dec 3, 2026
16 of 17 checks passed
@maleadt
maleadt deleted the tb/kwargs-splat branch December 3, 2026 11:29
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