NVIDIA / NVIDIA/cuda-python

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

Offen
#750 6 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug cuda.core triage
Vorherrschende Sprache
Cython
Sterne
3.4k
Forks
329
Ø Merge
1 T. 23 Std.
Gemergte PRs (30 T.)
116

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit cuda_core/cuda/core/experimental/_event.pyx und untersuche die entsprechende Stream-Implementierung, wobei du dich darauf konzentrierst, wie _init() und init() Cython-Klassen instanziieren. Überprüfe, dass abgeleitete Event- und Stream-Klassen korrekt initialisiert werden können und dass die Erstellung von Events über die bestehende Stream-API weiterhin unterstützt wird; das Issue nennt keine auszuführenden Tests.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Bereich
api
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.