Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
cb7c4eb
Replace np.cast with np.asarray
Armavica Apr 3, 2024
e3a536f
Replace np.sctype2char
Armavica Apr 3, 2024
20634b2
Remove np.obj2sctype
Armavica Apr 3, 2024
99a48ee
Replace np.find_common_type with np.result_type
Armavica Apr 3, 2024
8063c61
Add ruff numpy2 transition rule
Armavica Apr 3, 2024
969e88b
Update numpy deprecated imports
ricardoV94 Apr 5, 2024
8831b7f
Handle change in behavior np.dtype
ricardoV94 Apr 5, 2024
d3744e8
Update access to `_get_ndarray_c_version`
ricardoV94 Apr 5, 2024
7445fc8
Replace `->elsize` by `PyArray_ITEMSIZE`
ricardoV94 Apr 5, 2024
463604a
Don't use deprecated PyArray_MoveInto
ricardoV94 Apr 5, 2024
d09f468
Added missing header file
brendan-m-murphy Jul 8, 2024
85605d0
Make C++ generic get/set real/imag function
brendan-m-murphy Jul 29, 2024
51ece96
Updated pytensor_complex struct to uses get/set
brendan-m-murphy Jul 8, 2024
9a53de7
Updated assignment and operations +, -, *
brendan-m-murphy Jul 8, 2024
c5cd08f
Ruff formatting
brendan-m-murphy Jul 9, 2024
8b3e256
Pinned numpy to < 2.1
brendan-m-murphy Aug 27, 2024
9731d80
Updated Abs Op to use get_real, get_imag
brendan-m-murphy Jul 9, 2024
d865792
Raise NotImplementedError for c code if numpy>=2.0
brendan-m-murphy Jul 29, 2024
60ff0e9
Update test to remove numpy TypeError
brendan-m-murphy Aug 5, 2024
cbd4b0a
Fixed new TypeError
brendan-m-murphy Aug 5, 2024
456d1eb
Fixed casting in test_jacobian_scalar
brendan-m-murphy Aug 5, 2024
0d40476
Fixed casting in typed_list/test_basic.py
brendan-m-murphy Aug 5, 2024
21076ca
Removed "mode" argument from test_inner_graph_...
brendan-m-murphy Aug 6, 2024
4693b63
Changed copy to deepcopy for rng
brendan-m-murphy Aug 6, 2024
6dcef3d
Added npy_2_compat code and updated np.MAXDIMS
brendan-m-murphy Aug 7, 2024
34a0f91
Updated MAXDIMS to RAVEL_AXIS in c code
brendan-m-murphy Aug 7, 2024
ae8486a
Updated axis rel'd code for CumSum
brendan-m-murphy Aug 13, 2024
5883ee9
Fixes to SoftMax re: NPY_RAVEL_AXIS
brendan-m-murphy Aug 27, 2024
8e9caa6
Fixed AxisError related bugs in elemwise.py
brendan-m-murphy Aug 27, 2024
acfbe0e
Fixed failed test due to uint8 overflow
brendan-m-murphy Aug 27, 2024
674e209
Moved `format` to cover "axis_code"
brendan-m-murphy Aug 27, 2024
9a67dc6
replaced np.AxisError with np.exceptions.AxisError
brendan-m-murphy Aug 27, 2024
9c5da2c
Changed test to match new numpy promotion rules
brendan-m-murphy Aug 29, 2024
07192d0
Changed test to use higher precision
brendan-m-murphy Aug 29, 2024
b50a200
Changed autocaster due to new promotion rules
brendan-m-murphy Aug 30, 2024
3e2f3d2
Removed use of _asarray from scalar/basic.py
brendan-m-murphy Nov 10, 2024
5329214
Fixed "amb" vs. "amd" typo
brendan-m-murphy Nov 10, 2024
5f9d69b
Fixes missed in rebase
brendan-m-murphy Nov 10, 2024
89e7534
Restored complex type code lost in rebase
brendan-m-murphy Nov 10, 2024
28b50f2
Fixed namespace for AxisError
brendan-m-murphy Nov 10, 2024
c100b8d
Replaced eval with mapping to functions
brendan-m-murphy Nov 10, 2024
74a57fc
Updated environment.yml to match pyproject.toml
brendan-m-murphy Nov 10, 2024
33aa2ef
Formatting/fix error from rebase
brendan-m-murphy Nov 10, 2024
4bb08d9
Fixed format/f-string conflict from rebase
brendan-m-murphy Nov 11, 2024
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
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ channels:
dependencies:
- python>=3.10
- compilers
- numpy>=1.17.0,<2
- numpy>=1.17.0,<2.1
- scipy>=1,<2
- filelock>=3.15
- etuples
Expand Down
7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ keywords = [
dependencies = [
"setuptools>=59.0.0",
"scipy>=1,<2",
"numpy>=1.17.0,<2",
"numpy>=1.17.0,<2.1",
"filelock>=3.15",
"etuples",
"logical-unification",
Expand Down Expand Up @@ -129,9 +129,8 @@ exclude = ["doc/", "pytensor/_version.py"]
docstring-code-format = true

[tool.ruff.lint]
select = ["C", "E", "F", "I", "UP", "W", "RUF", "PERF", "PTH", "ISC"]
ignore = ["C408", "C901", "E501", "E741", "RUF012", "PERF203", "ISC001"]

select = ["C", "E", "F", "I", "UP", "W", "RUF", "NPY201"]
ignore = ["C408", "C901", "E501", "E741", "RUF012"]

[tool.ruff.lint.isort]
lines-after-imports = 2
Expand Down
4 changes: 2 additions & 2 deletions pytensor/link/c/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,8 +1363,8 @@ def cmodule_key_(

# We must always add the numpy ABI version here as
# DynamicModule always add the include <numpy/arrayobject.h>
if np.lib.NumpyVersion(np.__version__) < "1.16.0a":
ndarray_c_version = np.core.multiarray._get_ndarray_c_version()
if np.lib.NumpyVersion(np.__version__) >= "2.0.0rc":
ndarray_c_version = np._core._multiarray_umath._get_ndarray_c_version()
else:
ndarray_c_version = np.core._multiarray_umath._get_ndarray_c_version()
sig.append(f"NPY_ABI_VERSION=0x{ndarray_c_version:X}")
Expand Down
223 changes: 223 additions & 0 deletions pytensor/npy_2_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
from textwrap import dedent


def npy_2_compat_header() -> str:
return dedent("""
#ifndef NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_
#define NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_


/*
* This header is meant to be included by downstream directly for 1.x compat.
* In that case we need to ensure that users first included the full headers
* and not just `ndarraytypes.h`.
*/

#ifndef NPY_FEATURE_VERSION
#error "The NumPy 2 compat header requires `import_array()` for which " \\
"the `ndarraytypes.h` header include is not sufficient. Please " \\
"include it after `numpy/ndarrayobject.h` or similar." \\
"" \\
"To simplify inclusion, you may use `PyArray_ImportNumPy()` " \\
"which is defined in the compat header and is lightweight (can be)."
#endif

#if NPY_ABI_VERSION < 0x02000000
/*
* Define 2.0 feature version as it is needed below to decide whether we
* compile for both 1.x and 2.x (defining it gaurantees 1.x only).
*/
#define NPY_2_0_API_VERSION 0x00000012
/*
* If we are compiling with NumPy 1.x, PyArray_RUNTIME_VERSION so we
* pretend the `PyArray_RUNTIME_VERSION` is `NPY_FEATURE_VERSION`.
* This allows downstream to use `PyArray_RUNTIME_VERSION` if they need to.
*/
#define PyArray_RUNTIME_VERSION NPY_FEATURE_VERSION
/* Compiling on NumPy 1.x where these are the same: */
#define PyArray_DescrProto PyArray_Descr
#endif


/*
* Define a better way to call `_import_array()` to simplify backporting as
* we now require imports more often (necessary to make ABI flexible).
*/
#ifdef import_array1

static inline int
PyArray_ImportNumPyAPI()
{
if (NPY_UNLIKELY(PyArray_API == NULL)) {
import_array1(-1);
}
return 0;
}

#endif /* import_array1 */


/*
* NPY_DEFAULT_INT
*
* The default integer has changed, `NPY_DEFAULT_INT` is available at runtime
* for use as type number, e.g. `PyArray_DescrFromType(NPY_DEFAULT_INT)`.
*
* NPY_RAVEL_AXIS
*
* This was introduced in NumPy 2.0 to allow indicating that an axis should be
* raveled in an operation. Before NumPy 2.0, NPY_MAXDIMS was used for this purpose.
*
* NPY_MAXDIMS
*
* A constant indicating the maximum number dimensions allowed when creating
* an ndarray.
*
* NPY_NTYPES_LEGACY
*
* The number of built-in NumPy dtypes.
*/
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
#define NPY_DEFAULT_INT NPY_INTP
#define NPY_RAVEL_AXIS NPY_MIN_INT
#define NPY_MAXARGS 64

#elif NPY_ABI_VERSION < 0x02000000
#define NPY_DEFAULT_INT NPY_LONG
#define NPY_RAVEL_AXIS 32
#define NPY_MAXARGS 32

/* Aliases of 2.x names to 1.x only equivalent names */
#define NPY_NTYPES NPY_NTYPES_LEGACY
#define PyArray_DescrProto PyArray_Descr
#define _PyArray_LegacyDescr PyArray_Descr
/* NumPy 2 definition always works, but add it for 1.x only */
#define PyDataType_ISLEGACY(dtype) (1)
#else
#define NPY_DEFAULT_INT \\
(PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? NPY_INTP : NPY_LONG)
#define NPY_RAVEL_AXIS \\
(PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? -1 : 32)
#define NPY_MAXARGS \\
(PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? 64 : 32)
#endif


/*
* Access inline functions for descriptor fields. Except for the first
* few fields, these needed to be moved (elsize, alignment) for
* additional space. Or they are descriptor specific and are not generally
* available anymore (metadata, c_metadata, subarray, names, fields).
*
* Most of these are defined via the `DESCR_ACCESSOR` macro helper.
*/
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION || NPY_ABI_VERSION < 0x02000000
/* Compiling for 1.x or 2.x only, direct field access is OK: */

static inline void
PyDataType_SET_ELSIZE(PyArray_Descr *dtype, npy_intp size)
{
dtype->elsize = size;
}

static inline npy_uint64
PyDataType_FLAGS(const PyArray_Descr *dtype)
{
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
return dtype->flags;
#else
return (unsigned char)dtype->flags; /* Need unsigned cast on 1.x */
#endif
}

#define DESCR_ACCESSOR(FIELD, field, type, legacy_only) \\
static inline type \\
PyDataType_##FIELD(const PyArray_Descr *dtype) { \\
if (legacy_only && !PyDataType_ISLEGACY(dtype)) { \\
return (type)0; \\
} \\
return ((_PyArray_LegacyDescr *)dtype)->field; \\
}
#else /* compiling for both 1.x and 2.x */

static inline void
PyDataType_SET_ELSIZE(PyArray_Descr *dtype, npy_intp size)
{
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
((_PyArray_DescrNumPy2 *)dtype)->elsize = size;
}
else {
((PyArray_DescrProto *)dtype)->elsize = (int)size;
}
}

static inline npy_uint64
PyDataType_FLAGS(const PyArray_Descr *dtype)
{
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
return ((_PyArray_DescrNumPy2 *)dtype)->flags;
}
else {
return (unsigned char)((PyArray_DescrProto *)dtype)->flags;
}
}

/* Cast to LegacyDescr always fine but needed when `legacy_only` */
#define DESCR_ACCESSOR(FIELD, field, type, legacy_only) \\
static inline type \\
PyDataType_##FIELD(const PyArray_Descr *dtype) { \\
if (legacy_only && !PyDataType_ISLEGACY(dtype)) { \\
return (type)0; \\
} \\
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) { \\
return ((_PyArray_LegacyDescr *)dtype)->field; \\
} \\
else { \\
return ((PyArray_DescrProto *)dtype)->field; \\
} \\
}
#endif

DESCR_ACCESSOR(ELSIZE, elsize, npy_intp, 0)
DESCR_ACCESSOR(ALIGNMENT, alignment, npy_intp, 0)
DESCR_ACCESSOR(METADATA, metadata, PyObject *, 1)
DESCR_ACCESSOR(SUBARRAY, subarray, PyArray_ArrayDescr *, 1)
DESCR_ACCESSOR(NAMES, names, PyObject *, 1)
DESCR_ACCESSOR(FIELDS, fields, PyObject *, 1)
DESCR_ACCESSOR(C_METADATA, c_metadata, NpyAuxData *, 1)

#undef DESCR_ACCESSOR


#if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
static inline PyArray_ArrFuncs *
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
{
return _PyDataType_GetArrFuncs(descr);
}
#elif NPY_ABI_VERSION < 0x02000000
static inline PyArray_ArrFuncs *
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
{
return descr->f;
}
#else
static inline PyArray_ArrFuncs *
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
{
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
return _PyDataType_GetArrFuncs(descr);
}
else {
return ((PyArray_DescrProto *)descr)->f;
}
}
#endif


#endif /* not internal build */

#endif /* NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_ */

""")
Loading
Loading