Skip to content

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

Open
@carterbox

Description

@carterbox

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNeeds the team's attention

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions