Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ad135cf7
提交
ad135cf7
authored
9月 30, 2025
作者:
Virgile Andreani
提交者:
Ricardo Vieira
10月 07, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove the numpy < 2 compatibility header
上级
31cc3975
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
6 行增加
和
254 行删除
+6
-254
npy_2_compat.py
pytensor/npy_2_compat.py
+0
-226
extra_ops.py
pytensor/tensor/extra_ops.py
+2
-6
math.py
pytensor/tensor/math.py
+1
-6
special.py
pytensor/tensor/special.py
+3
-16
没有找到文件。
pytensor/npy_2_compat.py
浏览文件 @
ad135cf7
from
textwrap
import
dedent
import
numpy
as
np
import
numpy
as
np
...
@@ -22,227 +20,3 @@ def old_np_unique(
...
@@ -22,227 +20,3 @@ def old_np_unique(
outs
[
inv_idx
]
=
outs
[
inv_idx
]
.
reshape
(
inv_shape
)
outs
[
inv_idx
]
=
outs
[
inv_idx
]
.
reshape
(
inv_shape
)
return
tuple
(
outs
)
return
tuple
(
outs
)
# compatibility header for C code
def
npy_2_compat_header
()
->
str
:
"""Compatibility header that Numpy suggests is vendored with code that uses Numpy < 2.0 and Numpy 2.x"""
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_ */
"""
)
pytensor/tensor/extra_ops.py
浏览文件 @
ad135cf7
...
@@ -18,7 +18,7 @@ from pytensor.graph.replace import _vectorize_node
...
@@ -18,7 +18,7 @@ from pytensor.graph.replace import _vectorize_node
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.link.c.type
import
EnumList
,
Generic
from
pytensor.link.c.type
import
EnumList
,
Generic
from
pytensor.npy_2_compat
import
npy_2_compat_header
,
old_np_unique
from
pytensor.npy_2_compat
import
old_np_unique
from
pytensor.raise_op
import
Assert
from
pytensor.raise_op
import
Assert
from
pytensor.scalar
import
int64
as
int_t
from
pytensor.scalar
import
int64
as
int_t
from
pytensor.scalar
import
upcast
from
pytensor.scalar
import
upcast
...
@@ -362,10 +362,6 @@ class CumOp(COp):
...
@@ -362,10 +362,6 @@ class CumOp(COp):
return
shapes
return
shapes
def
c_support_code_apply
(
self
,
node
:
Apply
,
name
:
str
)
->
str
:
"""Needed to define NPY_RAVEL_AXIS"""
return
npy_2_compat_header
()
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
(
x
,)
=
inames
(
x
,)
=
inames
(
z
,)
=
onames
(
z
,)
=
onames
...
@@ -424,7 +420,7 @@ class CumOp(COp):
...
@@ -424,7 +420,7 @@ class CumOp(COp):
return
code
return
code
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
9
,)
return
(
10
,)
def
__str__
(
self
):
def
__str__
(
self
):
return
f
"{self.__class__.__name__}{{{self.axis}, {self.mode}}}"
return
f
"{self.__class__.__name__}{{{self.axis}, {self.mode}}}"
...
...
pytensor/tensor/math.py
浏览文件 @
ad135cf7
...
@@ -14,7 +14,6 @@ from pytensor.graph.op import Op
...
@@ -14,7 +14,6 @@ from pytensor.graph.op import Op
from
pytensor.graph.replace
import
_vectorize_node
from
pytensor.graph.replace
import
_vectorize_node
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.npy_2_compat
import
npy_2_compat_header
from
pytensor.printing
import
pprint
from
pytensor.printing
import
pprint
from
pytensor.raise_op
import
Assert
from
pytensor.raise_op
import
Assert
from
pytensor.scalar.basic
import
BinaryScalarOp
from
pytensor.scalar.basic
import
BinaryScalarOp
...
@@ -205,10 +204,6 @@ class Argmax(COp):
...
@@ -205,10 +204,6 @@ class Argmax(COp):
max_idx
[
0
]
=
np
.
asarray
(
np
.
argmax
(
reshaped_x
,
axis
=-
1
),
dtype
=
"int64"
)
max_idx
[
0
]
=
np
.
asarray
(
np
.
argmax
(
reshaped_x
,
axis
=-
1
),
dtype
=
"int64"
)
def
c_support_code_apply
(
self
,
node
:
Apply
,
name
:
str
)
->
str
:
"""Needed to define NPY_RAVEL_AXIS"""
return
npy_2_compat_header
()
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
(
x
,)
=
inp
(
x
,)
=
inp
(
argmax
,)
=
out
(
argmax
,)
=
out
...
@@ -255,7 +250,7 @@ class Argmax(COp):
...
@@ -255,7 +250,7 @@ class Argmax(COp):
"""
"""
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
2
,)
return
(
3
,)
def
infer_shape
(
self
,
fgraph
,
node
,
shapes
):
def
infer_shape
(
self
,
fgraph
,
node
,
shapes
):
(
ishape
,)
=
shapes
(
ishape
,)
=
shapes
...
...
pytensor/tensor/special.py
浏览文件 @
ad135cf7
...
@@ -6,7 +6,6 @@ import scipy
...
@@ -6,7 +6,6 @@ import scipy
from
pytensor.graph.basic
import
Apply
from
pytensor.graph.basic
import
Apply
from
pytensor.graph.replace
import
_vectorize_node
from
pytensor.graph.replace
import
_vectorize_node
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.op
import
COp
from
pytensor.npy_2_compat
import
npy_2_compat_header
from
pytensor.tensor.basic
import
as_tensor_variable
from
pytensor.tensor.basic
import
as_tensor_variable
from
pytensor.tensor.elemwise
import
get_normalized_batch_axes
from
pytensor.tensor.elemwise
import
get_normalized_batch_axes
from
pytensor.tensor.math
import
gamma
,
gammaln
,
log
,
neg
,
sum
from
pytensor.tensor.math
import
gamma
,
gammaln
,
log
,
neg
,
sum
...
@@ -61,11 +60,7 @@ class SoftmaxGrad(COp):
...
@@ -61,11 +60,7 @@ class SoftmaxGrad(COp):
return
[
shape
[
1
]]
return
[
shape
[
1
]]
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
5
,)
return
(
6
,)
def
c_support_code_apply
(
self
,
node
:
Apply
,
name
:
str
)
->
str
:
# return super().c_support_code_apply(node, name)
return
npy_2_compat_header
()
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
dy
,
sm
=
inp
dy
,
sm
=
inp
...
@@ -296,10 +291,6 @@ class Softmax(COp):
...
@@ -296,10 +291,6 @@ class Softmax(COp):
def
c_headers
(
self
,
**
kwargs
):
def
c_headers
(
self
,
**
kwargs
):
return
[
"<cmath>"
]
return
[
"<cmath>"
]
def
c_support_code_apply
(
self
,
node
:
Apply
,
name
:
str
)
->
str
:
"""Needed to define NPY_RAVEL_AXIS"""
return
npy_2_compat_header
()
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
(
x
,)
=
inp
(
x
,)
=
inp
(
sm
,)
=
out
(
sm
,)
=
out
...
@@ -495,7 +486,7 @@ class Softmax(COp):
...
@@ -495,7 +486,7 @@ class Softmax(COp):
@staticmethod
@staticmethod
def
c_code_cache_version
():
def
c_code_cache_version
():
return
(
5
,)
return
(
6
,)
def
softmax
(
c
,
axis
=
None
):
def
softmax
(
c
,
axis
=
None
):
...
@@ -555,10 +546,6 @@ class LogSoftmax(COp):
...
@@ -555,10 +546,6 @@ class LogSoftmax(COp):
def
c_headers
(
self
,
**
kwargs
):
def
c_headers
(
self
,
**
kwargs
):
return
[
"<cmath>"
]
return
[
"<cmath>"
]
def
c_support_code_apply
(
self
,
node
:
Apply
,
name
:
str
)
->
str
:
"""Needed to define NPY_RAVEL_AXIS"""
return
npy_2_compat_header
()
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
(
x
,)
=
inp
(
x
,)
=
inp
(
sm
,)
=
out
(
sm
,)
=
out
...
@@ -750,7 +737,7 @@ class LogSoftmax(COp):
...
@@ -750,7 +737,7 @@ class LogSoftmax(COp):
@staticmethod
@staticmethod
def
c_code_cache_version
():
def
c_code_cache_version
():
return
(
2
,)
return
(
3
,)
def
log_softmax
(
c
,
axis
=
None
):
def
log_softmax
(
c
,
axis
=
None
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论