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

NumPy 2.0 support #178

Merged
merged 7 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions performance/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,9 @@ def __init__(self):
self.values = [
np.longlong(-1), np.int_(-1), np.intc(-1), np.short(-1), np.byte(-1),
np.ubyte(1), np.ushort(1), np.uintc(1), np.uint(1), np.ulonglong(1),
np.half(1.0), np.single(1.0), np.float_(1.0), np.longfloat(1.0),
np.csingle(1.0j), np.complex_(1.0j), np.clongfloat(1.0j),
np.bool_(0), np.str_('1'), np.unicode_('1'), np.void(1),
np.half(1.0), np.single(1.0), np.float64(1.0), np.longdouble(1.0),
np.csingle(1.0j), np.complex_(1.0j), np.clongdouble(1.0j),
np.bool_(0), np.str_('1'), np.str_('1'), np.void(1),
np.object(), np.datetime64('NaT'), np.timedelta64('NaT'), np.nan,
12, 12.0, True, None, float('NaN'), object(), (1, 2, 3),
NT(1, 2, 3), datetime.date(2020, 12, 31), datetime.timedelta(14),
Expand Down
2 changes: 1 addition & 1 deletion requirements-build-3_11.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
numpy==1.23.5
numpy==2.0.0

2 changes: 1 addition & 1 deletion requirements-build-3_12.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
numpy==1.26.2
numpy==2.0.0
setuptools==69.*
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def get_ext_dir(*components: tp.Iterable[str]) -> tp.Sequence[str]:
'src/methods.c',
'src/tri_map.c',
],
include_dirs=get_ext_dir('numpy', 'core', 'include'),
library_dirs=get_ext_dir('numpy', 'core', 'lib'),
include_dirs=get_ext_dir('numpy', '_core', 'include'),
library_dirs=get_ext_dir('numpy', '_core', 'lib'),
define_macros=[("AK_VERSION", AK_VERSION)],
libraries=['npymath'], # not including mlib at this time
)
Expand Down
23 changes: 12 additions & 11 deletions src/delimited_to_arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ AK_CPL_Free(AK_CodePointLine* cpl)
PyMem_Free(cpl->buffer);
PyMem_Free(cpl->offsets);
if (cpl->type_parser) {
PyMem_Free(cpl->type_parser);
AK_TP_Free(cpl->type_parser);
}
PyMem_Free(cpl);
}
Expand Down Expand Up @@ -1364,7 +1364,7 @@ AK_CPL_to_array_float(AK_CodePointLine* cpl, PyArray_Descr* dtype, char tsep, ch
// initialize error code to 0; only update on error.
int error = 0;
bool matched_elsize = true;
int elsize = dtype->elsize;
int elsize = PyDataType_ELSIZE(dtype);

NPY_BEGIN_THREADS_DEF;
NPY_BEGIN_THREADS;
Expand Down Expand Up @@ -1442,7 +1442,7 @@ AK_CPL_to_array_int(AK_CodePointLine* cpl, PyArray_Descr* dtype, char tsep)
// initialize error code to 0; only update on error.
int error = 0;
bool matched_elsize = true;
int elsize = dtype->elsize;
int elsize = PyDataType_ELSIZE(dtype);

NPY_BEGIN_THREADS_DEF;
NPY_BEGIN_THREADS;
Expand Down Expand Up @@ -1515,7 +1515,7 @@ AK_CPL_to_array_uint(AK_CodePointLine* cpl, PyArray_Descr* dtype, char tsep)
// initialize error code to 0; only update on error.
int error = 0;
bool matched_elsize = true;
int elsize = dtype->elsize;
int elsize = PyDataType_ELSIZE(dtype);

NPY_BEGIN_THREADS_DEF;
NPY_BEGIN_THREADS;
Expand Down Expand Up @@ -1583,15 +1583,15 @@ AK_CPL_to_array_unicode(AK_CodePointLine* cpl, PyArray_Descr* dtype)
bool capped_points;

// mutate the passed dtype as it is new and will be stolen in array construction
if (dtype->elsize == 0) {
if (PyDataType_ELSIZE(dtype) == 0) {
field_points = cpl->offset_max;
dtype->elsize = (int)(field_points * UCS4_SIZE);
// dtype->elsize = (int)(field_points * UCS4_SIZE);
PyDataType_SET_ELSIZE(dtype, (npy_intp)(field_points * UCS4_SIZE));
capped_points = false;
}
else {
// assume that elsize is already given in units of 4
// assert(dtype->elsize % UCS4_SIZE == 0);
field_points = dtype->elsize / UCS4_SIZE;
field_points = PyDataType_ELSIZE(dtype) / UCS4_SIZE;
capped_points = true;
}

Expand Down Expand Up @@ -1649,13 +1649,14 @@ AK_CPL_to_array_bytes(AK_CodePointLine* cpl, PyArray_Descr* dtype)
Py_ssize_t field_points;
bool capped_points;

if (dtype->elsize == 0) {
if (PyDataType_ELSIZE(dtype) == 0) {
field_points = cpl->offset_max;
dtype->elsize = (int)field_points;
// dtype->elsize = (int)field_points;
PyDataType_SET_ELSIZE(dtype, (npy_intp)field_points);
capped_points = false;
}
else {
field_points = dtype->elsize;
field_points = PyDataType_ELSIZE(dtype);
capped_points = true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,16 @@ isna_element(PyObject *m, PyObject *args, PyObject *kwargs)
}
if (PyArray_IsScalar(element, Complex64)) {
npy_cfloat val = PyArrayScalar_VAL(element, Complex64);
return PyBool_FromLong(isnan(val.real) || isnan(val.imag));
return PyBool_FromLong(isnan(npy_crealf(val)) || isnan(npy_cimagf(val)));
}
if (PyArray_IsScalar(element, Complex128)) {
npy_cdouble val = PyArrayScalar_VAL(element, Complex128);
return PyBool_FromLong(isnan(val.real) || isnan(val.imag));
return PyBool_FromLong(isnan(npy_creal(val)) || isnan(npy_cimag(val)));
}
# ifdef PyComplex256ArrType_Type
if (PyArray_IsScalar(element, Complex256)) {
npy_clongdouble val = PyArrayScalar_VAL(element, Complex256);
return PyBool_FromLong(isnan(val.real) || isnan(val.imag));
return PyBool_FromLong(isnan(npy_creall(val)) || isnan(npy_cimagl(val)));
}
# endif

Expand Down
12 changes: 7 additions & 5 deletions src/tri_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
static inline NPY_DATETIMEUNIT
AK_dt_unit_from_array(PyArrayObject* a) {
// This is based on get_datetime_metadata_from_dtype in the NumPy source, but that function is private. This does not check that the dtype is of the appropriate type.
PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyArray_DESCR(a)->c_metadata)->meta);
PyArray_Descr* dt = PyArray_DESCR(a); // borrowed ref
PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyDataType_C_METADATA(dt))->meta);
// PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyArray_DESCR(a)->c_metadata)->meta);
return dma->base;
}

Expand Down Expand Up @@ -856,9 +858,9 @@ AK_TM_fill_object(TriMapObject* tm,
#define AK_TM_TRANSFER_FLEXIBLE(c_type) do { \
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;\
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one; \
npy_intp t_element_size = PyArray_DESCR(array_to)->elsize; \
npy_intp t_element_size = PyArray_ITEMSIZE(array_to); \
npy_intp t_element_cp = t_element_size / sizeof(c_type); \
npy_intp f_element_size = PyArray_DESCR(array_from)->elsize; \
npy_intp f_element_size = PyArray_ITEMSIZE(array_from); \
c_type* array_to_data = (c_type*)PyArray_DATA(array_to); \
c_type* f; \
c_type* t; \
Expand Down Expand Up @@ -906,7 +908,7 @@ AK_TM_fill_unicode(TriMapObject* tm,

Py_UCS4* array_to_data = (Py_UCS4*)PyArray_DATA(array_to);
// code points per element
npy_intp cp = PyArray_DESCR(array_to)->elsize / UCS4_SIZE;
npy_intp cp = PyArray_ITEMSIZE(array_to) / UCS4_SIZE;

bool decref_fill_value = false;
if (PyBytes_Check(fill_value)) {
Expand Down Expand Up @@ -948,7 +950,7 @@ AK_TM_fill_string(TriMapObject* tm,
? tm->final_src_fill : tm->final_dst_fill);

char* array_to_data = (char*)PyArray_DATA(array_to);
npy_intp cp = PyArray_DESCR(array_to)->elsize;
npy_intp cp = PyArray_ITEMSIZE(array_to);
if (!PyBytes_Check(fill_value)) {
return -1;
}
Expand Down
12 changes: 6 additions & 6 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ def test_dtype_from_element_core_dtypes(self) -> None:
np.ulonglong,
np.half,
np.single,
np.float_,
np.longfloat,
np.float64,
np.longdouble,
np.csingle,
np.complex_,
np.clongfloat,
np.complex128,
np.clongdouble,
np.bool_,
]
for dtype in dtypes:
Expand All @@ -540,12 +540,12 @@ def test_dtype_from_element_core_dtypes(self) -> None:
def test_dtype_from_element_str_and_misc_dtypes(self) -> None:
dtype_obj_pairs = [
(np.dtype('<U1'), np.str_('1')),
(np.dtype('<U1'), np.unicode_('1')),
(np.dtype('<U1'), np.str_('1')),
(np.dtype('V1'), np.void(1)),
(np.dtype('O'), object),
(np.dtype('<M8'), np.datetime64('NaT')),
(np.dtype('<m8'), np.timedelta64('NaT')),
(np.float_, np.nan),
(np.float64, np.nan),
]
for dtype, obj in dtype_obj_pairs:
self.assertEqual(dtype, dtype_from_element(obj))
Expand Down
Loading