Draft
Conversation
Contributor
Restructure tests into fixtures + classes with full resource cleanup: - Fixtures: sm_resource, wq_resource, green_ctx (with CUDAError skip), green_ctx_active (with try/finally restore), fill_kernel - _use_green_ctx context manager for safe push/pop in all tests - TestSMResourceQuery: properties, arch constraints per CC - TestSMResourceSplit: single/two-group splits, discovery, alignment, dry-run vs real parity - TestGreenContextKernelLaunch: compile + launch + verify in green ctx, two independent green contexts, SM + workqueue combined All set_current calls are paired with restore in finally blocks to prevent context stack leaks on test failure. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
Author
|
/ok to test ac5c0fc |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Close #1563. Close #112.
Summary
Add green context support to cuda.core v1.0 — the push-model API for querying device resources, splitting SMs, and creating/using green contexts.
Design
See the companion design doc for full rationale. Key decisions:
Contexttype — no user-visibleGreenContextsubclass. A singleContextwraps either a primaryCUcontextor aCUgreenCtx+ derivedCUcontext.ctx.is_greendistinguishes them. Inspired by the CUDA runtime's execution-context (EC) abstraction.dev.resourcesnamespace —DeviceResourcesgroups hardware resource queries (dev.resources.sm,dev.resources.workqueue). Follows the existing "plural = namespace" pattern (dev.properties,kernel.attributes).SMResourceOptionswith SoA broadcasting — single dataclass forSMResource.split(). Scalar fields broadcast;countdrives the group count.count=Nonemeans discovery mode (translated tosmCount=0internally).WorkqueueResourcemergesCU_DEV_RESOURCE_TYPE_WORKQUEUE_CONFIGandCU_DEV_RESOURCE_TYPE_WORKQUEUEunder one user-facing class. Strings for option values (e.g.sharing_scope="green_ctx_balanced").ContextOptions(resources=[...])→dev.create_context()— resource descriptor generation andcuGreenCtxCreateare internal. The user passes pre-split resource objects.ctx.close()does not manage the context stack — the user must swap out viadev.set_current(prev)before closing. Closing a current context raisesRuntimeError.New public API
Device.resources→DeviceResources(namespace:.sm,.workqueue)SMResource— properties:sm_count,min_partition_size,coscheduled_alignment,flags,handle; method:split(options, *, dry_run=False)SMResourceOptions—count,coscheduled_sm_count,preferred_coscheduled_sm_countWorkqueueResource— method:configure(options)WorkqueueResourceOptions—sharing_scopeContextOptions.resources— acceptsSequence[SMResource | WorkqueueResource]Context.is_green— bool propertyImplementation details
C++ handle layer (
resource_handles.hpp/cpp):GreenCtxHandle(shared_ptr<const CUgreenCtx>) — owning handle; destructor callscuGreenCtxDestroy.ContextBoxgains aGreenCtxHandlefield so the derivedCUcontextkeeps the green ctx alive.get_context_green_ctx()provides reverse lookup.create_green_ctx_handle()combinescuDevResourceGenerateDesc+cuGreenCtxCreatein one call — the descriptor is transient (noDevResourceDescHandleneeded since CUDA has no explicit destroy for it).context_registry/stream_registry(HandleRegistry) deduplicate handles by raw CUDA pointer, enabling identity-preservingset_currentswaps.Bug fix — stream context tracking:
StreamBoxnow carries aContextHandledependency, populated at creation time.get_stream_context()returns it without a driver call.Stream._from_handleandStream_ensure_ctxprefer the registry-backed handle before falling back tocuStreamGetCtx. This fixes a latent issue where streams created in a green context would lose their context association after aset_currentswap.Version guards:
IF CUDA_CORE_BUILD_MAJOR >= 13gatescuDevSmResourceSplit(the general/structured form).cy_driver_version() >= (12, 4, 0)for all green ctx APIs;>= (13, 1, 0)for structured splits.cuDevSmResourceSplitByCountfor basic (homogeneous) splits. Per-groupcoscheduled_sm_countand heterogeneous counts require 13.1+ and raiseNotImplementedErroron 12.x._get_optional_driver_fn— gracefulNULLwhen bindings lack the symbol.Test coverage
27 tests in
test_green_context.py, organized with proper pytest fixtures and classes:sm_resource,wq_resource,green_ctx(withCUDAError→ skip),green_ctx_active(push/pop with try/finally),fill_kernel_use_green_ctxcontext manager for safe push/pop in all tests — prevents context stack leaks on failureTestSMResourceQuery— properties, arch constraints (pre-Hopper vs Hopper+)TestWorkqueueResource— query, configure valid/invalidTestSMResourceSplitValidation— scalar/Sequence mismatch, negative count, dry-run blockedTestSMResourceSplit— single/two-group splits with arch-aligned counts, discovery mode, alignment, dry-run parityTestGreenContextLifecycle—is_green, identity-preserving swap, stream/event context tracking, close-while-current guardTestGreenContextKernelLaunch— compile + launch + host-verify in green ctx, two independent green contexts with different fill values, SM + workqueue combinedValidation
-- Leo's bot