Skip to content

Commit

Permalink
Fix Cython backend with Numpy 2
Browse files Browse the repository at this point in the history
  • Loading branch information
paugier committed Jul 24, 2024
1 parent 3b1d10a commit b1ba32d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 deletions data_tests/saved__backend__/cython/type_hint_notemplate.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cimport numpy as np
ctypedef fused __compute__Array_TypeIint_complex128I_NDimI1_3I:
np.ndarray[np.complex128_t, ndim=1]
np.ndarray[np.complex128_t, ndim=3]
np.ndarray[np.int_t, ndim=1]
np.ndarray[np.int_t, ndim=3]
np.ndarray[np.int32_t, ndim=1]
np.ndarray[np.int32_t, ndim=3]

ctypedef fused __compute__TypeIint_complex128I:
cython.int
Expand All @@ -18,7 +18,7 @@ ctypedef fused __compute__UnionArray_TypeIint_complex128I_NDimI1_3I_Array_float3
np.ndarray[np.complex128_t, ndim=3]
np.ndarray[np.float32_t, ndim=2]
np.ndarray[np.float32_t, ndim=4]
np.ndarray[np.int_t, ndim=1]
np.ndarray[np.int_t, ndim=3]
np.ndarray[np.int32_t, ndim=1]
np.ndarray[np.int32_t, ndim=3]

cpdef compute(__compute__Array_TypeIint_complex128I_NDimI1_3I a, __compute__Array_TypeIint_complex128I_NDimI1_3I b, __compute__TypeIint_complex128I c, __compute__UnionArray_TypeIint_complex128I_NDimI1_3I_Array_float32_NDimI1_3Ip1 d, cython.str e)
7 changes: 5 additions & 2 deletions src/transonic/backends/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@


def normalize_type_name_for_array(name):
if name == "bool_":

if name in ("bool_", "bool"):
return "np.uint8"
if name == "int":
return "np.int32"
if any(name.endswith(str(number)) for number in (8, 16, 32, 64, 128)):
return "np." + name
if name in ("int", "float", "complex"):
if name in ("float", "complex"):
return "np." + name
return name

Expand Down
2 changes: 1 addition & 1 deletion src/transonic_cl/cythonize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from distutils.core import setup
from setuptools import setup

import numpy as np
from Cython.Build import cythonize
Expand Down
16 changes: 8 additions & 8 deletions src/transonic_cl/run_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ def main():
args = [sys.executable, "-m", "transonic_cl.cythonize", name]

name_lock.touch()
try:
completed_process = subprocess.run(
args, stdout=stdout, stderr=stderr, universal_newlines=True
)
except Exception:
pass
finally:
name_lock.unlink()
completed_process = subprocess.run(
args, stdout=stdout, stderr=stderr, text=True, check=False
)
name_lock.unlink()

if completed_process.returncode:
print(completed_process.stderr)
raise subprocess.CalledProcessError(completed_process.returncode, args)
if backend == "pythran" and "-o" in args and path_tmp.exists():
path_tmp.rename(path_out)
elif backend == "cython":
Expand Down
10 changes: 5 additions & 5 deletions tests/backends/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ def compare(result, dtype, ndim, memview, mem_layout=None, positive_indices=None

def test_memview():
memview = "memview"
compare("np.int_t[:, ::1]", int, "2d", memview, "C")
compare("np.int_t[:, :, :]", int, "3d", memview, "strided")
compare("np.int32_t[:, ::1]", int, "2d", memview, "C")
compare("np.int32_t[:, :, :]", int, "3d", memview, "strided")
compare("np.int32_t[::1, :]", np.int32, "2d", memview, "F")


def test_array():
memview = None
compare('np.ndarray[np.int_t, ndim=2, mode="c"]', int, "2d", memview, "C")
compare("np.ndarray[np.int_t, ndim=3]", int, "3d", memview, "strided")
compare('np.ndarray[np.int32_t, ndim=2, mode="c"]', int, "2d", memview, "C")
compare("np.ndarray[np.int32_t, ndim=3]", int, "3d", memview, "strided")
compare(
'np.ndarray[np.int32_t, ndim=2, mode="f"]', np.int32, "2d", memview, "F"
)
compare(
"np.ndarray[np.int_t, ndim=2, negative_indices=False]",
"np.ndarray[np.int32_t, ndim=2, negative_indices=False]",
int,
"2d",
memview,
Expand Down

0 comments on commit b1ba32d

Please sign in to comment.