python: add the ensure_current_context #612 imports but never defined - #638
Conversation
…efined NVIDIA#612 added 'from ._device import ensure_current_context' to _pygraph.py (used on the python-engine execute path) without adding the function to _device.py, so 'import cudnn' fails with ImportError on any package built from develop. Hoist the established pattern from linear_attention/cutile/kernels/common.py (ensure_cuda_context) into _device.py using this module's driver shim: bind a driver context to the calling thread when none is bound - a JIT engine talks to the driver directly, which reads the calling THREAD's context stack, and an autograd backward runs on a worker thread where cudaSetDevice has only moved the runtime's thread-local slot. Prefer the stream's context, else retain the runtime-current device's primary one; best-effort like the cutile original. Fixes NVIDIA#634. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan includes up to 12 reviews per rolling hour; 8 remain after this review. 📝 WalkthroughWalkthroughThe change adds ChangesCUDA context initialization
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to The PR adds the missing context-binding helper needed for package imports and execution paths; no actionable merge-blocking risk remains beyond normal checks and review. Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
@cudnn-ci-bot run frost |
|
🚀 Running mirror pipeline Branch: cudnn-gh/pr-638-e69273b |
…g cannot name it (#635) ### What `DeviceInfo.oversized_shared_memory_per_block` gated the query on BOTH the driver version and whether cuda-python's `CUdevice_attribute` carries `CU_DEVICE_ATTRIBUTE_MAX_OVERSIZED_SHARED_MEMORY_PER_BLOCK`. Those are independent axes: the DRIVER decides whether the mode exists, the BINDING only decides how to ask. Refusing on the second gives up the carveout on a live combination — driver 13.5 with a cuda-python 13.3.1 binding reports 0 although the device really offers 327 KiB. On SM 10.7 block-scale GEMM that costs 3 AB pipeline stages (8 → 5) for nothing. ### Change Keep the driver gate, drop the binding one: name the enum member when the binding has it, else pass its ordinal, which that binding forwards fine. Only bindings old enough to reject a bare int (they read `attrib.value`) genuinely cannot make the query, and those return 0 through a narrow `AttributeError` arm. A real driver failure still raises rather than being masked. ### Test SM 10.7, driver 13050, cuda-python 13.3.1: - the query goes 0 → 334848, back to the ceiling the part reports - `test_device_info` + `test_public_execute_flavors` + `test_build_device` + `test_stream_respect` + `test_tile_select_analytic`: 46 passed / 2 skipped - sm107 block-scale kernels (`-k "sm107 and (128x128 or mixed_cga)"`): 42 passed ### Notes - The `ensure_current_context` fix this branch originally carried landed upstream as #638; that commit was dropped from this PR. - Supersedes #615, which fixed the same class of bug in `frost/device.py` before #612 moved the query into `cudnn/_device.py` — #615 can be closed.
…efined (NVIDIA#638) NVIDIA#612 added 'from ._device import ensure_current_context' to _pygraph.py (used on the python-engine execute path) without adding the function to _device.py, so 'import cudnn' fails with ImportError on any package built from develop. Hoist the established pattern from linear_attention/cutile/kernels/common.py (ensure_cuda_context) into _device.py using this module's driver shim: bind a driver context to the calling thread when none is bound - a JIT engine talks to the driver directly, which reads the calling THREAD's context stack, and an autograd backward runs on a worker thread where cudaSetDevice has only moved the runtime's thread-local slot. Prefer the stream's context, else retain the runtime-current device's primary one; best-effort like the cutile original. Fixes NVIDIA#634. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
…g cannot name it (NVIDIA#635) ### What `DeviceInfo.oversized_shared_memory_per_block` gated the query on BOTH the driver version and whether cuda-python's `CUdevice_attribute` carries `CU_DEVICE_ATTRIBUTE_MAX_OVERSIZED_SHARED_MEMORY_PER_BLOCK`. Those are independent axes: the DRIVER decides whether the mode exists, the BINDING only decides how to ask. Refusing on the second gives up the carveout on a live combination — driver 13.5 with a cuda-python 13.3.1 binding reports 0 although the device really offers 327 KiB. On SM 10.7 block-scale GEMM that costs 3 AB pipeline stages (8 → 5) for nothing. ### Change Keep the driver gate, drop the binding one: name the enum member when the binding has it, else pass its ordinal, which that binding forwards fine. Only bindings old enough to reject a bare int (they read `attrib.value`) genuinely cannot make the query, and those return 0 through a narrow `AttributeError` arm. A real driver failure still raises rather than being masked. ### Test SM 10.7, driver 13050, cuda-python 13.3.1: - the query goes 0 → 334848, back to the ceiling the part reports - `test_device_info` + `test_public_execute_flavors` + `test_build_device` + `test_stream_respect` + `test_tile_select_analytic`: 46 passed / 2 skipped - sm107 block-scale kernels (`-k "sm107 and (128x128 or mixed_cga)"`): 42 passed ### Notes - The `ensure_current_context` fix this branch originally carried landed upstream as NVIDIA#638; that commit was dropped from this PR. - Supersedes NVIDIA#615, which fixed the same class of bug in `frost/device.py` before NVIDIA#612 moved the query into `cudnn/_device.py` — NVIDIA#615 can be closed.
Fixes #634.
What
#612 added
from ._device import ensure_current_contextto_pygraph.py(used on the python-engine execute path at_pygraph.py:1783) without adding the function to_device.py, soimport cudnnfails with ImportError on any package built from current develop, and every python test fails at collection.Hoist the established pattern from
linear_attention/cutile/kernels/common.py(ensure_cuda_context) into_device.pyusing this module's own driver shim: bind a driver context to the calling thread when none is bound. A JIT engine talks to the driver directly, which reads the calling THREAD's context stack - and an autograd backward runs on a worker thread wherecudaSetDevicehas only moved the runtime's thread-local slot. Prefer the stream's context (cuStreamGetCtx), else retain the runtime-current device's primary one; best-effort like the cutile original (a context this cannot establish fails at the launch, with the launch's own diagnostics).A follow-up could de-duplicate cutile's
ensure_cuda_contextonto this; kept out of this PR to stay minimal.Verification (H100, cuDNN dev build, package built from this branch)
import cudnnsucceeds;create_handle()returns aHandle.ensure_current_context(None)after the process established a primary context ends up with a bound context.test_mhas_v2 -k "random_bwd_ragged or fp8_fwd_ragged": 258 passed / 7 skipped / 23 failed - the 23 are the pre-existing no-implementation typing issue fixed separately in sdpa: throw cudnnGraphNotSupportedException when no implementation supports the attributes #633, unrelated to this import fix.Summary by CodeRabbit