I have the following MCVE:
import numpy as np
cimport numpy as np
cimport cython
from cython cimport floating
def func1(floating[:] X_data, floating alpha):
if floating is double:
dtype = np.float64
else:
dtype = np.float32
cdef floating[:] prios = np.empty(12, dtype=dtype)
cdef int ws_size = 10
C = np.argpartition(np.asarray(prios), ws_size)[:ws_size].astype(np.int32)
cdef int res = func2(X_data, alpha, C)
cpdef int func2(floating[:] X_data, floating alpha, int[:] C):
cdef int epoch = 1
return epoch
Trying to run cython test_fused.pyx
gives me:
Error compiling Cython file:
------------------------------------------------------------
...
cdef floating[:] prios = np.empty(12, dtype=dtype)
cdef int ws_size = 10
C = np.argpartition(np.asarray(prios), ws_size)[:ws_size].astype(np.int32)
cdef int res = func2(X_data, alpha, C)
^
------------------------------------------------------------
test_fused.pyx:21:24: no suitable method found
Error compiling Cython file:
------------------------------------------------------------
...
cdef floating[:] prios = np.empty(12, dtype=dtype)
cdef int ws_size = 10
C = np.argpartition(np.asarray(prios), ws_size)[:ws_size].astype(np.int32)
cdef int res = func2(X_data, alpha, C)
^
------------------------------------------------------------
test_fused.pyx:21:24: no suitable method found
Error compiling Cython file:
------------------------------------------------------------
...
cdef floating[:] prios = np.empty(12, dtype=dtype)
cdef int ws_size = 10
C = np.argpartition(np.asarray(prios), ws_size)[:ws_size].astype(np.int32)
cdef int res = func2(X_data, alpha, C)
^
------------------------------------------------------------
test_fused.pyx:21:19: Invalid use of fused types, type cannot be specialized
Error compiling Cython file:
------------------------------------------------------------
...
cdef floating[:] prios = np.empty(12, dtype=dtype)
cdef int ws_size = 10
C = np.argpartition(np.asarray(prios), ws_size)[:ws_size].astype(np.int32)
cdef int res = func2(X_data, alpha, C)
^
------------------------------------------------------------
test_fused.pyx:21:19: Invalid use of fused types, type cannot be specialized
I had a more complicated code which also passed the array C
as a runtime defined value, which did not cause any issue. What is the cause of this compilation error?
I am puzzled, because slight modifications (adding a dummy keyword arg to func1
and two keyword args to func2
) make the code compile:
def func1(floating[:] X_data, floating alpha,
int dummy_variable=1): # added dummy_variable here
# same as before here
cdef int res = func2(X_data, alpha, C,
dummy_variable=dummy_variable)
cpdef int func2(floating[:] X_data, floating alpha, int[:] C,
int K=6, int dummy_variable=1): # added K and dummy variable here
cdef int epoch = 1
return epoch