[FEA]: private constructor prevents derived classes of cythonized Stream, Event
まだ誰も着手していません。
- 主要言語
- Cython
- スター
- 3.4k
- フォーク
- 329
- 平均マージ
- 1日 21時間
- マージ済み PR(30日)
- 113
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
cuda_core/cuda/core/experimental/_event.pyx から始め、対応する Stream の実装を調査し、_init() と init() が Cython クラスをどのように構築するかに注目してください。派生 Event クラスと Stream クラスが正しく初期化できること、および既存の Stream API によるイベント作成が引き続きサポートされることを確認してください。この issue には実行するテストの指定はありません。
索引モデルが issue の本文から書いたものです。
評価
- 領域
- api
- issue の種類
- 機能追加
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 52/100