[FEA]: private constructor prevents derived classes of cythonized Stream, Event
Nessuno ha ancora preso questa issue.
- Lingua principale
- Cython
- Stelle
- 3.4k
- Fork
- 329
- Merge medio
- 1g 23h
- PR unite (30g)
- 116
Descrizione
Is this a duplicate?
- I confirmed there appear to be no duplicate issues for this request and that I agree to the Code of Conduct
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia da cuda_core/cuda/core/experimental/_event.pyx e ispeziona l’implementazione corrispondente di Stream, concentrandoti su come _init() e init() costruiscono le classi Cython. Verifica che le classi derivate Event e Stream possano inizializzarsi correttamente e che la creazione di eventi tramite l’API Stream esistente continui a essere supportata; l’issue non indica quali test eseguire.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Ambito
- api
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 52/100