Skip to content

test(cuda.core): run the GL interop samples headless and drop their test-helper dependency - #3654

Open
mdboom wants to merge 1 commit into
NVIDIA:mainfrom
mdboom:issue3576
Open

mdboom wants to merge 1 commit into
NVIDIA:mainfrom
mdboom:issue3576

Conversation

@mdboom

@mdboom mdboom commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Closes #3576. Part of #2077.

Summary

glInteropPlasma, glInteropFluid and glInteropMipmapLod waived whenever DISPLAY was unset, so test_sample[...] never ran them on a CI runner. pyglet 2.x can render offscreen through EGL, so the samples now do that instead of waiving:

 main()
-    if Linux and not DISPLAY:
-        waive
+    headless = use_headless_gl_if_no_display(cuda_device)   # pyglet headless EGL on the GPU behind the CUDA device
     ...setup CUDA, open window (offscreen when headless), register the PBO...
     on_draw():
         CUDA kernel -> PBO -> texture -> full-window quad
         if last --frames frame:
+            if headless:
+                verify_frame_shows_pixel_buffer(pbo_id, WIDTH, HEIGHT)   # nobody sees the frame, so check it
             pyglet.app.exit()
  • verify_frame_shows_pixel_buffer reads back the PBO the kernel wrote and the frame just drawn, and raises if they differ by more than 8 per channel (the headless EGL surface is RGB565; measured differences are at most 7) or if the PBO is one flat color.
  • The samples still waive when there is no display and no EGL ("Could not open a pyglet window"), or when the GL context is not on an NVIDIA GPU.
  • glInteropFluidNumbaCudaMlir is not changed: it is self-contained by design, copies through the host rather than a PBO, has no --frames limit, and no CI environment installs numba-cuda-mlir yet (CI: install the packages never-run samples need in the nightly-pytorch job #3578).
  • The three samples are removed from the "not currently run in any CI job" comment in test_samples.py.

Also: samples no longer depend on cuda_python_test_helpers

The GL samples imported cuda_python_test_helpers.graphics, a test-only package that is not published, so users running a sample outside this repository could not satisfy that import. That dependency should not have existed. This PR moves the helpers the samples need into the samples tree and adds a guard:

 samples/cuda_core/Utilities/
+    graphics.py   # use_headless_gl_if_no_display, verify_frame_shows_pixel_buffer,
+                  # gl_context_not_on_nvidia_gpu_reason (copied from the test helpers)
 samples/cuda_core/glInterop{Plasma,Fluid,MipmapLod}/*.py
-    sys.path.insert(0, ".../cuda_python_test_helpers"); from cuda_python_test_helpers.graphics import ...
+    sys.path.insert(0, ".../Utilities");                from graphics import ...
 cuda_python_test_helpers/sample_runner.py
+    run_sample(): FAIL any sample whose directory or Utilities imports cuda_python_test_helpers

No other sample imported it. The guard runs before config waivers and dependency checks, so an import on a path that CI never executes is still caught, and it applies to both the cuda.core and cuda.bindings sample suites.

Evidence

CI coverage: on main, the three samples waive in every configuration ("sample-reported"). After this change:

  • glInteropPlasma should pass on 36 Linux rows (31 standard CUDA 13, 5 nightly); its cuda-python>=13 pin still skips CUDA 12 rows.
  • glInteropFluid and glInteropMipmapLod should pass on 50 Linux rows each (12 standard CUDA 12, 31 standard CUDA 13, 7 nightly). If samples: fix unsatisfiable dependency pins and depend on cuda-core[cu13] #3601 lands first, their CUDA 12 rows skip on its cuda-core[cu13] pin.
  • Windows and WSL rows keep waiving (no opengl32, or a Mesa GL context).

Locally (headless, DISPLAY unset):

  • test_samples.py -k glInterop: the three samples pass; the MLIR variant skips for its missing dependency. Plasma also passes on a second GPU, and with cuda_python_test_helpers blocked from import.
  • Forcing a frame mismatch makes each sample fail (rc 1); hiding EGL makes each waive (rc 2).
  • The guard flags none of the 56 current samples, and fails a sample that imports cuda_python_test_helpers directly or through Utilities.
  • cuda_core/tests/example_tests (113 passed) and cuda_bindings_13/tests/example_tests (13 passed).

Confirming in CI: in this PR's CI run, the Linux cuda.core jobs should report the three samples PASSED, where main reports them SKIPPED ("sample-reported"). For example:

gh run view <CI run id> --repo NVIDIA/cuda-python --log | grep -E "test_sample\[cuda_core/glInterop(Plasma|Fluid|MipmapLod)\]"

Merge Danger

Door: two-way.

Blast Radius: the three GL samples (their headless behavior; with a display they behave as before), samples/cuda_core/Utilities, and the shared sample runner, which can now fail a sample before running it. #3653 adds a similar headless helper to cuda_python_test_helpers.graphics for the bindings tests; the two are independent.

Reviewers: please feel free to push commits directly to this PR to improve it.

🤖 Generated with Claude Code

…est-helper dependency

glInteropPlasma, glInteropFluid and glInteropMipmapLod waived whenever
DISPLAY was unset, so they never ran on a CI runner.

On Linux without DISPLAY, the samples now switch pyglet to an offscreen
EGL context on the GPU behind the CUDA device, and on the last frame check
that the rendered frame shows the pixel buffer the CUDA kernel wrote
(within the precision of an RGB565 surface). They still waive when there
is neither a display nor an NVIDIA GL context.

The samples also imported cuda_python_test_helpers, a test-only package
users cannot install. Move the helpers they need into a new
samples/cuda_core/Utilities/graphics.py, and make the sample runner fail
any sample that imports cuda_python_test_helpers, directly or through
Utilities.

Remove the three samples from the "not currently run in any CI job"
comment.

Closes NVIDIA#3576.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mdboom mdboom added this to the cuda.core 1.4.0 milestone Nov 30, 2026
@mdboom mdboom added test Improvements or additions to tests cuda.core Everything related to the cuda.core module labels Nov 30, 2026
@mdboom mdboom self-assigned this Nov 30, 2026
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

Summary

Summary by CodeRabbit

  • New Features
    • OpenGL interop samples can now render offscreen on supported headless Linux systems using GPU EGL, instead of automatically waiving when no display is available.
    • Headless runs check that the rendered frame reflects CUDA output. Samples waive when neither a display nor a suitable NVIDIA EGL device is available.
  • Documentation
    • Updated sample guides with headless rendering requirements, expected behavior, and completion details.

Three CUDA/OpenGL samples now use shared helpers for headless EGL rendering and frame verification when DISPLAY is unset. The sample runner also detects imports from the test-only helper package.

Changes: Headless GL interop

Layer / File(s) Summary
Shared graphics helpers
samples/cuda_core/Utilities/graphics.py, samples/cuda_core/Utilities/README.md
The shared utilities add EGL device selection, headless setup, NVIDIA context inspection, and PBO frame verification. The README documents the helpers and their dependencies.
GL sample headless rendering
samples/cuda_core/glInteropFluid/*, samples/cuda_core/glInteropMipmapLod/*, samples/cuda_core/glInteropPlasma/*
The three samples use the shared helpers instead of waiving when DISPLAY is unset. In headless mode, they verify the final frame when their frame limit is reached. Their READMEs describe the headless behavior and waiver condition.
Sample import boundary
cuda_python_test_helpers/cuda_python_test_helpers/sample_runner.py, cuda_core/tests/example_tests/test_samples.py
The sample runner reports a failure when it finds imports from cuda_python_test_helpers. The sample key comment no longer lists the three GL interop samples.

Assessment against linked issues

Objective Addressed Explanation
Headless EGL setup for the three GL interop samples, with EGL device selection and waiving when NVIDIA GL is unavailable [3576] ✅
Render offscreen and validate the rendered output rather than only exiting [3576] ✅
Remove the three sample keys from the test_samples.py marker [3576] ✅

Possibly related issues

  • Issue 3578: The PR adds headless EGL support to the three GL interop samples named in the issue context. It does not change glInteropFluidNumbaCudaMlir.

Suggested reviewers: danielfrg

Change: Other

Merge Risk: Low · up to 88925

The headless EGL change works as intended in the normal case. Two edge cases remain: a broken EGL library produces a crash instead of a waiver, and a malformed sample source file aborts the whole sample batch instead of reporting one failure. Both are small fixes and worth addressing before or soon after merge.

Architecture risk: Medium · up to 88925

The change affects 3 source areas.

Changed areas: samples, cuda_python_test_helpers, cuda_core

Review details

Systems and components

  • observed — 8 changed files mapped to samples.
  • observed — 1 changed file mapped to cuda_python_test_helpers.
  • observed — 1 changed file mapped to cuda_core.

Reliability and maintainability

  • inferred — Risk-relevant change factors for samples: blast_radius_1; direct_dependents_1
  • inferred — Risk-relevant change factors for cuda_python_test_helpers: blast_radius_1; direct_dependents_1
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 1c9ed66 and 88925f2.

Files selected for processing: 10

  • cuda_core/tests/example_tests/test_samples.py
  • cuda_python_test_helpers/cuda_python_test_helpers/sample_runner.py
  • samples/cuda_core/Utilities/README.md
  • samples/cuda_core/Utilities/graphics.py
  • samples/cuda_core/glInteropFluid/README.md
  • samples/cuda_core/glInteropFluid/glInteropFluid.py
  • samples/cuda_core/glInteropMipmapLod/README.md
  • samples/cuda_core/glInteropMipmapLod/glInteropMipmapLod.py
  • samples/cuda_core/glInteropPlasma/README.md
  • samples/cuda_core/glInteropPlasma/glInteropPlasma.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 6 remain after this review.

found = []
for directory in directories:
for path in sorted(directory.rglob("*.py")):
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Functional Correctness · Quick win

important: Convert source-inspection errors into a sample result.

If a sample contains a syntax error, ast.parse raises before run_sample reaches its subprocess handling. The exception escapes into main, interrupts the batch, and prevents its summary. Previously, the same sample produced a nonzero child exit and a FAIL result.

Handle inspection failures narrowly and return an ERROR result with the source diagnostic. Do not waive the sample. Valid samples must continue through the existing checks.

import pyglet

pyglet.options["headless"] = True
egl_device = _select_egl_device_for_cuda(cuda_device_ordinal)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Stability & Availability · Quick win

Waive the samples when the EGL loader fails during headless setup.

use_headless_gl_if_no_display finds libEGL with ctypes.util.find_library. It then calls _select_egl_device_for_cuda, which imports pyglet.libs.egl at samples/cuda_core/Utilities/graphics.py:37. If the library is present but a native dependency cannot load, pyglet raises during that import. The import is outside the try block in _select_egl_device_for_cuda. Each sample calls use_headless_gl_if_no_display before its create_window waiver handler, and no outer handler translates the error. The sample then exits with an uncaught traceback instead of EXIT_WAIVED.

The PR waives a sample when no NVIDIA EGL device is available. Route this setup failure to the same waiver path. Keep unrelated errors visible. The exception class and the handler location are a design choice for the author. The inspected code does not establish either one.

@github-actions

Copy link
Copy Markdown
Contributor

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module test Improvements or additions to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

samples: run the cuda.core GL interop samples headless (EGL) instead of waiving without DISPLAY

1 participant