This is an automated email from the ASF dual-hosted git repository.
tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-tirx.git
The following commit(s) were added to refs/heads/main by this push:
new 885623696 [FIX][CUDA] Set the non-portable cluster function attribute
for cluster launches (#258)
885623696 is described below
commit 8856236965d5b35cf7f6f3d3e72e71a3f1cedf60
Author: Hongyi Jin <[email protected]>
AuthorDate: Sun Nov 22 18:59:05 2026 -0500
[FIX][CUDA] Set the non-portable cluster function attribute for cluster
launches (#258)
## Motivation
Clusters larger than the portable size of 8 need an explicit opt-in.
Kernels use them, for example, to broadcast one TMA load to 16 CTAs with
`multicast::cluster`. TIRx opts in automatically for every cluster
launch, but with CUDA 13.2+ it only sets the `PortableClusterSizeMode`
launch attribute. That attribute alone does not lift the limit on every
driver. A plain
```python
T.cuda.LaunchConfig(grid=32, block=32, cluster=16)
```
fails with `CUDA_ERROR_INVALID_CLUSTER_SIZE`, also with
`portable_cluster_size_mode="allow_non_portable"`. In the same
configuration `cudaOccupancyMaxPotentialClusterSize` still reports 8,
while setting `cudaFuncAttributeNonPortableClusterSizeAllowed` makes the
launch succeed. Before CUDA 13.2, TIRx set that function attribute, so
this is a regression from switching to the launch attribute.
## Changes
- Set `CU_FUNC_ATTRIBUTE_NON_PORTABLE_CLUSTER_SIZE_ALLOWED` once per
kernel for cluster launches that give no `portable_cluster_size_mode` or
give `allow_non_portable`. An explicit `require_portable` or `default`
leaves the function attribute alone, so those modes keep their CUDA
meaning (`default` uses whatever the function attribute currently is).
- Keep setting the launch attribute when no mode is given.
- Regenerate `launch_source.h`.
## Testing
- New `test_non_portable_cluster_size` launches a cluster of 16 and
checks `cluster_cta_id`.
- On an sm_103a GPU it fails before this change and passes after it,
together with the rest of `test_cuda_launch.py`.
---
src/backend/cuda/launch/launch_config.h | 14 +++++++++-----
src/backend/cuda/launch/launch_source.h | 20 ++++++++++++--------
tests/python/tirx/test_cuda_launch.py | 22 ++++++++++++++++++++++
3 files changed, 43 insertions(+), 13 deletions(-)
diff --git a/src/backend/cuda/launch/launch_config.h
b/src/backend/cuda/launch/launch_config.h
index 46ccbbf8f..f1143056b 100644
--- a/src/backend/cuda/launch/launch_config.h
+++ b/src/backend/cuda/launch/launch_config.h
@@ -200,14 +200,18 @@ inline void PrepareResources(typename API::Kernel kernel,
int device, LaunchValu
API::Initialize(kernel, device, state);
state->initialized = true;
}
- if (config->Has(Option::kCluster) &&
!config->Has(Option::kPortableClusterSizeMode)) {
-#if CUDA_VERSION >= 13020 && CUDART_VERSION >= 13020
- config->SetInt(Field::kPortableClusterSizeMode, 2);
-#else
- if (!state->nonportable_cluster) {
+ if (config->Has(Option::kCluster)) {
+ bool explicit_mode = config->Has(Option::kPortableClusterSizeMode);
+ // The portable-cluster-size launch attribute alone does not lift the
+ // portable limit on every driver; also set the function attribute unless
+ // the launch explicitly asks for portable cluster sizes.
+ if ((!explicit_mode || config->Int(Field::kPortableClusterSizeMode) == 2)
&&
+ !state->nonportable_cluster) {
API::AllowCluster(kernel);
state->nonportable_cluster = true;
}
+#if CUDA_VERSION >= 13020 && CUDART_VERSION >= 13020
+ if (!explicit_mode) config->SetInt(Field::kPortableClusterSizeMode, 2);
#endif
}
if (!config->Has(Option::kSharedMemoryMode)) {
diff --git a/src/backend/cuda/launch/launch_source.h
b/src/backend/cuda/launch/launch_source.h
index 83fdb4063..6148115ba 100644
--- a/src/backend/cuda/launch/launch_source.h
+++ b/src/backend/cuda/launch/launch_source.h
@@ -871,14 +871,18 @@ inline void PrepareResources(typename API::Kernel kernel,
int device, LaunchValu
API::Initialize(kernel, device, state);
state->initialized = true;
}
- if (config->Has(Option::kCluster) &&
!config->Has(Option::kPortableClusterSizeMode)) {
-#if CUDA_VERSION >= 13020 && CUDART_VERSION >= 13020
- config->SetInt(Field::kPortableClusterSizeMode, 2);
-#else
- if (!state->nonportable_cluster) {
+ if (config->Has(Option::kCluster)) {
+ bool explicit_mode = config->Has(Option::kPortableClusterSizeMode);
+ // The portable-cluster-size launch attribute alone does not lift the
+ // portable limit on every driver; also set the function attribute unless
+ // the launch explicitly asks for portable cluster sizes.
+ if ((!explicit_mode || config->Int(Field::kPortableClusterSizeMode) == 2)
&&
+ !state->nonportable_cluster) {
API::AllowCluster(kernel);
state->nonportable_cluster = true;
}
+#if CUDA_VERSION >= 13020 && CUDART_VERSION >= 13020
+ if (!explicit_mode) config->SetInt(Field::kPortableClusterSizeMode, 2);
#endif
}
if (!config->Has(Option::kSharedMemoryMode)) {
@@ -932,14 +936,14 @@ inline void EncodeRuntime(const LaunchValues& values,
const KernelRequirements&
cudaStream_t default_stream, cudaLaunchConfig_t*
config,
std::array<cudaLaunchAttribute, kMaxAttributes>*
attrs) {
unsigned int* grid[] = {&config->gridDim.x, &config->gridDim.y,
&config->gridDim.z};
- unsigned int* block[] = {&config->blockDim.x, &config->blockDim.y,
&config->blockDim.z};
+)TVM_CUDA_LAUNCH"
+ R"TVM_CUDA_LAUNCH( unsigned int* block[] = {&config->blockDim.x,
&config->blockDim.y, &config->blockDim.z};
for (int axis = 0; axis != 3; ++axis) {
*grid[axis] = values.Int(Axis(Field::kGridX, axis));
*block[axis] = values.Int(Axis(Field::kBlockX, axis));
if (kernel.required_block_size) {
#if CUDART_VERSION >= 13000
-)TVM_CUDA_LAUNCH"
- R"TVM_CUDA_LAUNCH( *grid[axis] /= values.Int(Axis(Field::kClusterX,
axis));
+ *grid[axis] /= values.Int(Axis(Field::kClusterX, axis));
*block[axis] = 1;
#else
throw std::invalid_argument("CUDA required block size requires CUDA 13
or newer");
diff --git a/tests/python/tirx/test_cuda_launch.py
b/tests/python/tirx/test_cuda_launch.py
index 4cdff6602..45b088853 100644
--- a/tests/python/tirx/test_cuda_launch.py
+++ b/tests/python/tirx/test_cuda_launch.py
@@ -350,6 +350,28 @@ def
test_oversized_dynamic_shared_memory_without_cluster(host, tmp_path):
tvm_tirx.testing.run_with_gpu_lock(check)
[email protected]
[email protected](not env.has_cuda_compute(10), reason="requires CUDA
compute >= 10.0")
[email protected]("host", ["llvm", "cuda_host"])
+def test_non_portable_cluster_size(host, tmp_path):
+ """Clusters above the portable size of 8 launch without an explicit
opt-in."""
+
+ @T.function
+ def clusters(A: T.Tensor((32,), "int32")):
+ T.device_entry(launch=T.cuda.LaunchConfig(grid=32, block=32,
cluster=16))
+ if T.cuda.thread_idx("x") == 0:
+ A[T.cuda.block_idx("x")] = T.cuda.cluster_cta_id("x")
+
+ module, _ = _compile(clusters, host, tmp_path)
+
+ def check():
+ out = tvm_tirx.runtime.empty((32,), "int32", tvm_tirx.cuda(0))
+ module["clusters"](out)
+ np.testing.assert_array_equal(out.numpy(), np.arange(32) % 16)
+
+ tvm_tirx.testing.run_with_gpu_lock(check)
+
+
def test_static_shared_memory_override_cannot_shrink_allocation():
@T.function
def kernel():