Skip to content

Commit b963404

Browse files
committed
feat(cuda.core): support CU_LAUNCH_ATTRIBUTE_PRIORITY in LaunchConfig
1 parent 0477f61 commit b963404

5 files changed

Lines changed: 67 additions & 4 deletions

File tree

‎cuda_core/cuda/core/_launch_config.pxd‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -16,6 +16,7 @@ cdef class LaunchConfig:
1616
public int shmem_size
1717
public bint is_cooperative
1818
public bint programmatic_stream_serialization
19+
public object priority
1920

2021
vector[cydriver.CUlaunchAttribute] _attrs
2122
object __weakref__

‎cuda_core/cuda/core/_launch_config.pyi‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Any
44

5-
_LAUNCH_CONFIG_ATTRS = ('grid', 'cluster', 'block', 'shmem_size', 'is_cooperative', 'programmatic_stream_serialization')
5+
_LAUNCH_CONFIG_ATTRS = ('grid', 'cluster', 'block', 'shmem_size', 'is_cooperative', 'programmatic_stream_serialization', 'priority')
66
__all__ = ['LaunchConfig']
77

88
class LaunchConfig:
@@ -39,15 +39,19 @@ class LaunchConfig:
3939
Whether to allow programmatic stream serialization (PDL). When True,
4040
the kernel may overlap with a previous kernel in the same stream that
4141
signals completion via programmatic means.
42+
priority : int, optional
43+
Execution priority of the kernel. Lower numbers represent higher
44+
priorities. When omitted, the launch uses the stream's priority.
4245
"""
4346
grid: tuple[Any, ...]
4447
cluster: tuple[Any, ...]
4548
block: tuple[Any, ...]
4649
shmem_size: int
4750
is_cooperative: bool
4851
programmatic_stream_serialization: bool
52+
priority: object
4953

50-
def __init__(self, grid: int | tuple[int, ...] | None=None, cluster: int | tuple[int, ...] | None=None, block: int | tuple[int, ...] | None=None, shmem_size: int | None=None, is_cooperative: bool=False, programmatic_stream_serialization: bool=False) -> None:
54+
def __init__(self, grid: int | tuple[int, ...] | None=None, cluster: int | tuple[int, ...] | None=None, block: int | tuple[int, ...] | None=None, shmem_size: int | None=None, is_cooperative: bool=False, programmatic_stream_serialization: bool=False, priority: int | None=None) -> None:
5155
"""Initialize LaunchConfig with validation.
5256
5357
Parameters
@@ -64,6 +68,9 @@ class LaunchConfig:
6468
Whether to launch as cooperative kernel (default: False)
6569
programmatic_stream_serialization : bool, optional
6670
Whether to allow programmatic stream serialization / PDL (default: False)
71+
priority : int, optional
72+
Execution priority of the kernel. Lower numbers represent higher
73+
priorities. When omitted, the launch uses the stream's priority.
6774
"""
6875
def _identity(self) -> tuple[Any, ...]: ...
6976
def __repr__(self) -> str:

‎cuda_core/cuda/core/_launch_config.pyx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ _LAUNCH_CONFIG_ATTRS = (
2020
'shmem_size',
2121
'is_cooperative',
2222
'programmatic_stream_serialization',
23+
'priority',
2324
)
2425

2526
__all__ = ['LaunchConfig']
@@ -59,6 +60,9 @@ cdef class LaunchConfig:
5960
Whether to allow programmatic stream serialization (PDL). When True,
6061
the kernel may overlap with a previous kernel in the same stream that
6162
signals completion via programmatic means.
63+
priority : int, optional
64+
Execution priority of the kernel. Lower numbers represent higher
65+
priorities. When omitted, the launch uses the stream's priority.
6266
"""
6367

6468
# TODO: expand LaunchConfig to include other attributes
@@ -72,6 +76,7 @@ cdef class LaunchConfig:
7276
shmem_size: int | None = None,
7377
is_cooperative: bool = False,
7478
programmatic_stream_serialization: bool = False,
79+
priority: int | None = None,
7580
) -> None:
7681
"""Initialize LaunchConfig with validation.
7782

@@ -89,6 +94,9 @@ cdef class LaunchConfig:
8994
Whether to launch as cooperative kernel (default: False)
9095
programmatic_stream_serialization : bool, optional
9196
Whether to allow programmatic stream serialization / PDL (default: False)
97+
priority : int, optional
98+
Execution priority of the kernel. Lower numbers represent higher
99+
priorities. When omitted, the launch uses the stream's priority.
92100
"""
93101
# Convert and validate grid and block dimensions
94102
self.grid = cast_to_3_tuple("LaunchConfig.grid", grid)
@@ -116,6 +124,7 @@ cdef class LaunchConfig:
116124

117125
self.is_cooperative = is_cooperative
118126
self.programmatic_stream_serialization = programmatic_stream_serialization
127+
self.priority = priority
119128

120129
if self.is_cooperative and not Device().properties.cooperative_launch:
121130
raise CUDAError("cooperative kernels are not supported on this device")
@@ -169,6 +178,11 @@ cdef class LaunchConfig:
169178
attr.value.programmaticStreamSerializationAllowed = 1
170179
self._attrs.push_back(attr)
171180

181+
if self.priority is not None:
182+
attr.id = cydriver.CUlaunchAttributeID.CU_LAUNCH_ATTRIBUTE_PRIORITY
183+
attr.value.priority = self.priority
184+
self._attrs.push_back(attr)
185+
172186
drv_cfg.numAttrs = self._attrs.size()
173187
drv_cfg.attrs = self._attrs.data()
174188

@@ -230,6 +244,12 @@ cpdef object _to_native_launch_config(LaunchConfig config):
230244
attr.value.programmaticStreamSerializationAllowed = 1
231245
attrs.append(attr)
232246

247+
if config.priority is not None:
248+
attr = driver.CUlaunchAttribute()
249+
attr.id = driver.CUlaunchAttributeID.CU_LAUNCH_ATTRIBUTE_PRIORITY
250+
attr.value.priority = config.priority
251+
attrs.append(attr)
252+
233253
drv_cfg.numAttrs = len(attrs)
234254
drv_cfg.attrs = attrs
235255

‎cuda_core/tests/test_launcher.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,41 @@ def test_to_native_launch_config_pdl():
202202
)
203203

204204

205+
@pytest.mark.parametrize(
206+
("initial_priority", "updated_priority"),
207+
((-1, 0), (0, 1), (1, -1)),
208+
)
209+
def test_launch_config_priority_getter_setter(initial_priority, updated_priority):
210+
config = LaunchConfig(grid=1, block=1, priority=initial_priority)
211+
212+
assert config.priority == initial_priority
213+
config.priority = updated_priority
214+
assert config.priority == updated_priority
215+
216+
217+
@pytest.mark.parametrize(
218+
("priority", "expected_num_attrs"),
219+
((None, 0), (0, 1), (-1, 1), (1, 1)),
220+
)
221+
def test_to_native_launch_config_priority(priority, expected_num_attrs):
222+
"""LaunchConfig priority maps to the native attribute, including zero."""
223+
from cuda.bindings import driver
224+
from cuda.core._launch_config import _to_native_launch_config
225+
226+
config = LaunchConfig(grid=2, block=4, priority=priority)
227+
native = _to_native_launch_config(config)
228+
229+
assert config.priority == priority
230+
assert native.numAttrs == expected_num_attrs
231+
if priority is None:
232+
assert list(native.attrs) == []
233+
return
234+
235+
attr = native.attrs[0]
236+
assert attr.id == driver.CUlaunchAttributeID.CU_LAUNCH_ATTRIBUTE_PRIORITY
237+
assert attr.value.priority == priority
238+
239+
205240
@skipif_need_cuda_headers
206241
def test_pdl_primary_secondary_overlap_same_stream():
207242
"""Primary + secondary PDL launch on one stream can overlap on Hopper+.

‎cuda_core/tests/test_object_protocols.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def sample_object_b(request):
704704
"sample_launch_config",
705705
r"LaunchConfig\(grid=\(\d+, \d+, \d+\), cluster=.+, block=\(\d+, \d+, \d+\), "
706706
r"shmem_size=\d+, is_cooperative=(?:True|False), "
707-
r"programmatic_stream_serialization=(?:True|False)\)",
707+
r"programmatic_stream_serialization=(?:True|False), priority=(?:None|-?\d+)\)",
708708
),
709709
("sample_kernel", r"<Kernel handle=0x[0-9a-f]+>"),
710710
# ObjectCode variations (by code_type)

0 commit comments

Comments
 (0)