Closed
Description
One of the reasons that people complain about CUDA Array Interface (CAI) being slow is because it has side effects. Consider this example:
>>> class KKK:
... @property
... def __proppp__(self):
... print("I am called")
...
>>>
>>> type(KKK.__proppp__)
<class 'property'>
>>> k = KKK()
>>> hasattr(k, "__proppp__")
I am called
True
One can see that the hasattr
call on any class attribute/property would actually execute the attribute implementation, but not if it is a method:
>>> class KKK:
... def __proppp__(self):
... print("I am called")
...
>>>
>>> type(KKK.__proppp__)
<class 'function'>
>>> k = KKK()
>>> hasattr(k, "__proppp__")
True
I realized I had forgotten about this during the design stage until now. This fact has also been pointed out by @gmarkall (back in the days of designing CAI, IIRC).
I propose to change __cuda_stream__
and any future protocols to a callable class method from a class property/attribute so as to avoid side effects (thereby reducing overhead) and establish a future-proof convention, following DLPack and other protocols.