Fused types changed behavour with cython3 ?

27 views
Skip to first unread message

Jérôme Kieffer

unread,
Mar 3, 2023, 7:08:10 AM3/3/23
to Cython-users, thomas....@esrf.fr
Dear Cython community,

While checking all my code with cython3, I found some changes of
behavior which I did not expect (and causes some of my code to
fail later on). It simplifies to:

```
from cython cimport floating

def add(floating a, floating b):
return a+b
```
which accepts `a` and `b` as float, numpy.float64 but no more
numpy.float32 in cython3 while it is working with cython 0.29.x

Is this a known regression ? thanks for your comments

--
Jerome Kieffer
Cython3.html
Cython2.html

D Woods

unread,
Mar 4, 2023, 3:49:43 AM3/4/23
to cython-users
There isn't an intentional regression there.

In fact, I get exactly the same on both Cython 0.29.x and 3.0.0b1:

  File "fused_test.pyx", line 3, in fused_test.__pyx_fused_cpdef
   def add(floating a, floating b):
TypeError: No matching signature found

As far as I know, Cython doesn't handle these "fixed type" wrapper too well when calling `def` functions. The only check it really does is "isinstance(float)" since it isn't hugely easy to pick apart an arbitrary wrapper scalar. I do have a plan for how to improve this, but haven't had time yet.

If you do have an example of a regression here then please put it on the Cython bug-tracker. We've modified fused-type dispatch to make it faster, but I don't think any changes in behaviour were intended.

D Woods

unread,
Mar 4, 2023, 3:52:24 AM3/4/23
to cython-users
Looks at your attached notebooks, they also show the same behaviour between the branches.

I think you're right that ideally it should work, but I don't think it's a new limitation.

Jérôme Kieffer

unread,
Mar 5, 2023, 2:56:36 AM3/5/23
to cython...@googlegroups.com
Hi David,

Indeed, the notebook failed at reproducing the bug. My bad.

The bug is when calling this function:
https://github.com/silx-kit/pyFAI/blob/main/pyFAI/ext/preproc.pyx#L463
from
https://github.com/silx-kit/pyFAI/blob/main/pyFAI/ext/preproc.pyx#L640

Here is a work-around for the issue, using the actual signature method of the cdef function:
https://github.com/kif/pyFAI/blob/1833_cython3/pyFAI/ext/preproc.pyx#L640

Maybe I could have made it a cdef function instead of a def function,
since it is in the critical path of the program.

The usage of Cython3 introduces many slow-down (up to 4x slower) but
when adding the "noexcept" on all `cdef` function, especially the one
`nogil`, the slow-down is reduced to ~5%.

If you have an idea on the possible source for this regression ...

Cheers,

--
Jerome

D Woods

unread,
Mar 5, 2023, 8:58:53 AM3/5/23
to cython-users
Hi Jerome,

I worked out what the issue is:


Essentially, in Cython 0.29.x your ` any_int_t[::1] mask=None,` matches anything when set to `None`, so picks the first type. In Cython 3.0 it matches nothing. I think there's issues with the 0.29.x behaviour too though. Will have to have a look at what is best to do...
Reply all
Reply to author
Forward
0 new messages