Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] cython.final extension type with fused type method #5989

Closed
lorentzenchr opened this issue Feb 10, 2024 · 4 comments · Fixed by #6005
Closed

[BUG] cython.final extension type with fused type method #5989

lorentzenchr opened this issue Feb 10, 2024 · 4 comments · Fixed by #6005

Comments

@lorentzenchr
Copy link

lorentzenchr commented Feb 10, 2024

Describe the bug

When using cython.final decorator for a cdef class that has a method with an argument of fused type, the generated C code lacks some prefix __pyx_fuse_0 and __pyx_fuse_1 in a few places.
The code compiles without error. But when loading/importing one gets an error:

ImportError: dlopen(XXX.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace '___pyx_f_54_cython_magic_570ee92356009cb8197cfd5d5ebfbcc01ad73c1d_6AClass_c_sum'

Code to reproduce the behaviour:

import cython
%load_ext cython

# %%
%%cython

import cython
import numpy as np

ctypedef unsigned char uint8_t
ctypedef unsigned short uint16_t
ctypedef fused uint_fused:
    uint8_t
    uint16_t

@cython.final
cdef class AClass:
    def compute_sum(self, a):
        if a.dtype == np.float32:
            return self.c_sum[uint8_t](a)
        else:
            return self.c_sum[uint16_t](a)

    cdef c_sum(self, uint_fused[:] a):
        cdef:
            int i
            uint16_t s = 0
        for i in range(a.shape[0]):
            s += a[i]
        return s

# %%
aclass = AClass()
aclass.compute_sum(np.arange(10).astype(np.uint16))

Note that without the @cython.final, everything works as expected without error.

Expected behaviour

No error, result 45.

OS

macOS

Python version

3.11.7

Cython version

3.0.8

Additional context

No response

@lorentzenchr
Copy link
Author

@da-woods You seem active right now and this is a road blocker for one of my projects.

@da-woods
Copy link
Contributor

I'll try to have a look in the next week or so. I should warn: this fused stuff is usually quite heavy going to debug and fix, so I'm really not going to promise anything quick.

@lorentzenchr
Copy link
Author

lorentzenchr commented Feb 10, 2024

Thanks for having a look.
A striking argument for Cython is allocating numpy arrays in Python and then use memoryviews in Cython, in particular in a prange. Then one quickly wants c++ templates, the next best thing being fused types, last resort tempita.

So the combination of C++ with memoryviews would be super helpful, but the C(++) structure of memoryviews is quite hidden.

TLDR: If Cython works, it‘s very cool. If it doesn’t, try n error can drive you crazy 😜

@lorentzenchr
Copy link
Author

@da-woods @matusvalo Thanks for the fix, really appreciated 🥳

@scoder scoder added this to the 3.0.9 milestone Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants