NVIDIA / NVIDIA/cuda-python

[FEA]: private constructor prevents derived classes of cythonized Stream, Event

Open
#750 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug cuda.core triage
Dominant language
Cython
Stars
3.4k
Forks
329
Avg merge
1d 23h
Merged PRs (30d)
116

Description

Is this a duplicate?
Area

cuda.core

Is your feature request related to a problem? Please describe.

In nvmath-python we have test the correctness of our code by checking how many times we have called Event.sync(). We used to do this by monkeypatching the Event class, so that it would update a global variable whenever called. This is no longer possible because Event is an immutable cython class in cuda-core 0.3.1, so it is unpatchable.

The alternative approach for us would be to create a derived class LoggedSyncEvent which overwrites the sync() method with our modified method. However, this is not possible because Event subverts the conventional constructor method __init__() and instead implements _init(). The problem with this custom constructor is that it will always return an Event even in derived classes (as opposed to __init__() which cython knows how to handle properly for class inheritance).

Describe the solution you'd like

Use the standard __init__() method for class constructors instead of using engineering controls to preventing users from calling the constructor directly by hiding the constructor in _init(). Instead just use procedural controls by just mentioning in the documentation that calling __init__() is deprecated/unsupported/unstable.

For example:

diff --git a/cuda_core/cuda/core/experimental/_event.pyx b/cuda_core/cuda/core/experimental/_event.pyx
index 94496985..76bb7b96 100644
--- a/cuda_core/cuda/core/experimental/_event.pyx
+++ b/cuda_core/cuda/core/experimental/_event.pyx
@@ -84,12 +84,7 @@ cdef class Event:
         int _device_id
         object _ctx_handle

-    def __init__(self, *args, **kwargs):
-        raise RuntimeError("Event objects cannot be instantiated directly. Please use Stream APIs (record).")
-
-    @classmethod
-    def _init(cls, device_id: int, ctx_handle: Context, options=None):
-        cdef Event self = Event.__new__(Event)
+    def __init__(self, device_id: int, ctx_handle: Context, options=None):
         cdef EventOptions opts = check_or_create_options(EventOptions, options, "Event options")
         flags = 0x0
         self._timing_disabled = False
@@ -106,7 +101,6 @@ cdef class Event:
         raise_if_driver_error(err)
         self._device_id = device_id
         self._ctx_handle = ctx_handle
-        return self

     cpdef close(self):
         """Destroy the event."""
Describe alternatives you've considered

Playing with cython syntax to try and get _init() to return a derived class instead of always Event. I tried this, but some parameters in the constructor are cdef attributes, so they are not accessible by instances of cls.

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with cuda_core/cuda/core/experimental/_event.pyx and inspect the corresponding Stream implementation, focusing on how _init() and init() construct Cython classes. Verify that derived Event and Stream classes can initialize correctly while existing Stream API event creation remains supported; the issue does not name tests to run.

Written by the indexing model from the issue text.

Assessment

Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.