Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions cuda_core/tests/example_tests/test_sample_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from __future__ import annotations

import ast
import importlib.util
import types
from pathlib import Path

import numpy as np
Expand All @@ -17,6 +19,7 @@
"glInteropFluid",
"glInteropMipmapLod",
"glInteropPlasma",
"greenContext",
"ipcMemoryPool",
"launchConfigTuning",
"memoryResources",
Expand Down Expand Up @@ -141,3 +144,100 @@ def test_verification_exception_is_reported_as_sample_failure(tmp_path: Path) ->

assert result.status == "FAIL"
assert result.return_code != 0


def _load_green_context_sample():
sample_path = REPO_ROOT / "samples" / "cuda_core" / "greenContext" / "greenContext.py"
spec = importlib.util.spec_from_file_location("greenContext_sample", sample_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module


class _Partition:
def __init__(self, sm_count: int) -> None:
self.sm_count = sm_count


class _CountApiSM:
"""Stand-in for ``SMResource`` behind ``cuDevSmResourceSplitByCount``.

Mirrors the two rules that matter here: equal-sized groups from one
``min_count``, and sizes that are multiples of the device's minimum SM
partition size. The greenContext sample cannot ask for two different group
sizes, so a probe of it must never reach the unequal-count branch.
"""

def __init__(self, sm_count: int, min_partition_size: int) -> None:
self.sm_count = sm_count
self.min_partition_size = min_partition_size

def split(self, options, *, dry_run: bool = False):
counts = options.count if isinstance(options.count, tuple) else (options.count,)
if len(set(counts)) > 1:
raise RuntimeError("CUDA 12 SM splitting only supports homogeneous count values")
if any(count % self.min_partition_size for count in counts):
raise RuntimeError("CUDA_ERROR_INVALID_RESOURCE_CONFIGURATION")
if sum(counts) > self.sm_count:
raise RuntimeError("CUDA_ERROR_INVALID_RESOURCE_CONFIGURATION")
return [_Partition(count) for count in counts], _Partition(self.sm_count - sum(counts))


@pytest.mark.agent_authored(model="deepseek/deepseek-v4.1-flash-1017")
def test_green_context_auto_split_without_equal_two_way_split() -> None:
"""Regression: NVBug 7106432 - 14 SMs cannot form an equal two-way split."""
sm = _CountApiSM(sm_count=14, min_partition_size=2)

found, reason = _load_green_context_sample()._find_working_split(sm)

assert reason is None
assert found is not None
long_count, critical_count, _granularity = found
assert long_count + critical_count == sm.sm_count
assert long_count > 0
assert critical_count % sm.min_partition_size == 0


@pytest.mark.agent_authored(model="deepseek/deepseek-v4.1-flash-1017")
def test_green_context_device_without_usable_split_waives(monkeypatch) -> None:
"""A device without a usable two-partition split is a waiver, not a failure."""
# Non-default code, so a hard-coded exit value cannot pass this test.
monkeypatch.setenv("CUDA_PYTHON_SAMPLE_WAIVER_EXIT_CODE", "77")
sample = _load_green_context_sample()
sm = _CountApiSM(sm_count=2, min_partition_size=2)
device = types.SimpleNamespace(resources=types.SimpleNamespace(sm=sm))

found, _reason = sample._find_working_split(sm)
assert found is None

with pytest.raises(SystemExit) as exc_info:
sample.parse_split(None, device)
assert sample.EXIT_WAIVED == 77
assert exc_info.value.code == 77


@pytest.mark.agent_authored(model="deepseek/deepseek-v4.1-flash-1017")
def test_green_context_probe_propagates_allocation_failure() -> None:
"""A failed allocation is a broken environment, not a refused partition."""

class _OutOfMemorySM(_CountApiSM):
def split(self, options, *, dry_run: bool = False):
raise MemoryError

sample = _load_green_context_sample()

with pytest.raises(MemoryError):
sample._find_working_split(_OutOfMemorySM(14, 2))


@pytest.mark.agent_authored(model="deepseek/deepseek-v4.1-flash-1017")
def test_green_context_explicit_split_must_take_the_remainder() -> None:
"""``--split A,B`` is accepted only when A is what B leaves on the device."""
sample = _load_green_context_sample()
device = types.SimpleNamespace(resources=types.SimpleNamespace(sm=_CountApiSM(14, 2)))

assert sample.parse_split("12,2", device) == (12, 2)

with pytest.raises(SystemExit) as exc_info:
sample.parse_split("10,2", device)
assert exc_info.value.code == 1
29 changes: 18 additions & 11 deletions samples/cuda_core/greenContext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ partition. The reference row lets you separate those two effects:

- Querying a device's SM resources via `Device.resources.sm` and
reading `sm_count`, `min_partition_size`, `coscheduled_alignment`
- Splitting an `SMResource` into disjoint partitions with
`sm.split(SMResourceOptions(count=(A, B)))`
- Splitting an `SMResource` into disjoint partitions: reserve one with
`sm.split(SMResourceOptions(count=B))` and use the returned remainder
for the other
- Creating a green context from an SM partition via
`Device.create_context(ContextOptions(resources=[group]))`
- Creating a non-blocking stream on a green context with
Expand All @@ -61,8 +62,8 @@ partition. The reference row lets you separate those two effects:
### From `cuda.core`

- `Device.resources.sm` - the device's SM-type device resource
- `SMResource.split(SMResourceOptions(count=(A, B)))` - partition SMs
into disjoint groups (plus an optional remainder)
- `SMResource.split(SMResourceOptions(count=B))` - partition SMs by
reserving `B` SMs and keeping the returned remainder
- `Device.create_context(ContextOptions(resources=[sm_group]))` -
create a green context provisioned with a specific SM partition
- `Context.is_green` / `Context.resources` - introspect a green
Expand Down Expand Up @@ -111,13 +112,17 @@ pip install -r requirements.txt

The auto-default split reserves a small partition (~16 SMs) for the
critical kernel and gives the rest to the long-running kernel. The
exact sizes are chosen by probing the driver with a dry-run `sm.split`,
exact size is chosen by probing the driver with a dry-run `sm.split`,
escalating the alignment granularity in powers of two until the driver
accepts the pair. This handles architectures where the driver enforces
accepts it. This handles architectures where the driver enforces
stricter alignment (e.g. TPC/GPC-pair alignment on Blackwell) than the
reported `min_partition_size`. When that happens the sample prints a
`Note:` line with the granularity it landed on.

If the driver accepts no partition at all, the sample reports that the
device cannot host two SM partitions and exits with the sample-waiver
code instead of failing.

```bash
cd samples/cuda_core/greenContext
python greenContext.py
Expand Down Expand Up @@ -151,11 +156,13 @@ python greenContext.py --device 1
```
--device CUDA device ID (default: 0)
--split SM split as 'LONG,CRITICAL', e.g. '112,16'.
Each side must be a multiple of the device's
min_partition_size, and the driver may enforce additional
architecture-specific alignment (e.g. TPC/partition-grid
alignment on Blackwell). Omit --split to auto-select a
driver-accepted split.
CRITICAL SMs are reserved for the critical kernel and must
be a multiple of the device's min_partition_size; the driver
may enforce additional architecture-specific alignment
(e.g. TPC/partition-grid alignment on Blackwell). The
long-running kernel gets every SM the critical partition
leaves behind, so LONG must equal that remainder.
Omit --split to auto-select a driver-accepted split.
--delay-us Per-block busy-wait of the delay kernel, in us (default: 2000)
--delay-waves Number of waves of the delay kernel on the long
partition. Drives the default --delay-blocks (default: 16)
Expand Down
Loading
Loading