Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
03492d44
提交
03492d44
authored
9月 29, 2025
作者:
Virgile Andreani
提交者:
Ricardo Vieira
10月 07, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove using_numpy_2
上级
f15a953c
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
4 行增加
和
237 行删除
+4
-237
npy_2_compat.py
pytensor/npy_2_compat.py
+1
-10
subtensor.py
pytensor/tensor/subtensor.py
+2
-222
test_math.py
tests/tensor/test_math.py
+1
-5
没有找到文件。
pytensor/npy_2_compat.py
浏览文件 @
03492d44
...
@@ -3,21 +3,12 @@ from textwrap import dedent
...
@@ -3,21 +3,12 @@ from textwrap import dedent
import
numpy
as
np
import
numpy
as
np
numpy_version_tuple
=
tuple
(
int
(
n
)
for
n
in
np
.
__version__
.
split
(
"."
)[:
2
])
numpy_version
=
np
.
lib
.
NumpyVersion
(
np
.
__version__
)
# used to compare with version strings, e.g. numpy_version < "1.16.0"
using_numpy_2
=
numpy_version
>=
"2.0.0rc1"
# function that replicates np.unique from numpy < 2.0
# function that replicates np.unique from numpy < 2.0
def
old_np_unique
(
def
old_np_unique
(
arr
,
return_index
=
False
,
return_inverse
=
False
,
return_counts
=
False
,
axis
=
None
arr
,
return_index
=
False
,
return_inverse
=
False
,
return_counts
=
False
,
axis
=
None
):
):
"""Replicate np.unique from numpy versions < 2.0"""
"""Replicate np.unique from numpy versions < 2.0"""
if
not
return_inverse
or
not
using_numpy_2
:
if
not
return_inverse
:
return
np
.
unique
(
arr
,
return_index
,
return_inverse
,
return_counts
,
axis
)
return
np
.
unique
(
arr
,
return_index
,
return_inverse
,
return_counts
,
axis
)
outs
=
list
(
np
.
unique
(
arr
,
return_index
,
return_inverse
,
return_counts
,
axis
))
outs
=
list
(
np
.
unique
(
arr
,
return_index
,
return_inverse
,
return_counts
,
axis
))
...
...
pytensor/tensor/subtensor.py
浏览文件 @
03492d44
...
@@ -6,6 +6,7 @@ from itertools import chain, groupby, zip_longest
...
@@ -6,6 +6,7 @@ from itertools import chain, groupby, zip_longest
from
typing
import
cast
,
overload
from
typing
import
cast
,
overload
import
numpy
as
np
import
numpy
as
np
from
numpy.lib.array_utils
import
normalize_axis_tuple
import
pytensor
import
pytensor
from
pytensor
import
scalar
as
ps
from
pytensor
import
scalar
as
ps
...
@@ -18,7 +19,6 @@ from pytensor.graph.type import Type
...
@@ -18,7 +19,6 @@ from pytensor.graph.type import Type
from
pytensor.graph.utils
import
MethodNotDefined
from
pytensor.graph.utils
import
MethodNotDefined
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
normalize_axis_tuple
,
numpy_version
,
using_numpy_2
from
pytensor.printing
import
Printer
,
pprint
,
set_precedence
from
pytensor.printing
import
Printer
,
pprint
,
set_precedence
from
pytensor.scalar.basic
import
ScalarConstant
,
ScalarVariable
from
pytensor.scalar.basic
import
ScalarConstant
,
ScalarVariable
from
pytensor.tensor
import
(
from
pytensor.tensor
import
(
...
@@ -2330,199 +2330,6 @@ class AdvancedIncSubtensor1(COp):
...
@@ -2330,199 +2330,6 @@ class AdvancedIncSubtensor1(COp):
return
f
"""(PyArrayObject*)PyArray_FromAny(py_{x}, NULL, 0, 0,
return
f
"""(PyArrayObject*)PyArray_FromAny(py_{x}, NULL, 0, 0,
NPY_ARRAY_ENSURECOPY, NULL)"""
NPY_ARRAY_ENSURECOPY, NULL)"""
def
c_support_code
(
self
,
**
kwargs
):
if
numpy_version
<
"1.8.0"
or
using_numpy_2
:
return
None
types
=
[
"npy_"
+
t
for
t
in
[
"int8"
,
"int16"
,
"int32"
,
"int64"
,
"uint8"
,
"uint16"
,
"uint32"
,
"uint64"
,
"float16"
,
"float32"
,
"float64"
,
]
]
complex_types
=
[
"npy_"
+
t
for
t
in
(
"complex32"
,
"complex64"
,
"complex128"
)]
inplace_map_template
=
"""
#if defined(
%(typen)
s)
static void
%(type)
s_inplace_add(PyArrayMapIterObject *mit,
PyArrayIterObject *it, int inc_or_set)
{
int index = mit->size;
while (index--) {
%(op)
s
PyArray_MapIterNext(mit);
PyArray_ITER_NEXT(it);
}
}
#endif
"""
floatadd
=
(
"((
%(type)
s*)mit->dataptr)[0] = "
"(inc_or_set ? ((
%(type)
s*)mit->dataptr)[0] : 0)"
" + ((
%(type)
s*)it->dataptr)[0];"
)
complexadd
=
"""
((
%(type)
s*)mit->dataptr)[0].real =
(inc_or_set ? ((
%(type)
s*)mit->dataptr)[0].real : 0)
+ ((
%(type)
s*)it->dataptr)[0].real;
((
%(type)
s*)mit->dataptr)[0].imag =
(inc_or_set ? ((
%(type)
s*)mit->dataptr)[0].imag : 0)
+ ((
%(type)
s*)it->dataptr)[0].imag;
"""
fns
=
""
.
join
(
[
inplace_map_template
%
{
"type"
:
t
,
"typen"
:
t
.
upper
(),
"op"
:
floatadd
%
{
"type"
:
t
}}
for
t
in
types
]
+
[
inplace_map_template
%
{
"type"
:
t
,
"typen"
:
t
.
upper
(),
"op"
:
complexadd
%
{
"type"
:
t
}}
for
t
in
complex_types
]
)
def
gen_binop
(
type
,
typen
):
return
f
"""
#if defined({typen})
{type}_inplace_add,
#endif
"""
fn_array
=
(
"static inplace_map_binop addition_funcs[] = {"
+
""
.
join
(
gen_binop
(
type
=
t
,
typen
=
t
.
upper
())
for
t
in
types
+
complex_types
)
+
"NULL};
\n
"
)
def
gen_num
(
typen
):
return
f
"""
#if defined({typen})
{typen},
#endif
"""
type_number_array
=
(
"static int type_numbers[] = {"
+
""
.
join
(
gen_num
(
typen
=
t
.
upper
())
for
t
in
types
+
complex_types
)
+
"-1000};"
)
code
=
(
"""
typedef void (*inplace_map_binop)(PyArrayMapIterObject *,
PyArrayIterObject *, int inc_or_set);
"""
+
fns
+
fn_array
+
type_number_array
+
"""
static int
map_increment(PyArrayMapIterObject *mit, PyArrayObject *op,
inplace_map_binop add_inplace, int inc_or_set)
{
PyArrayObject *arr = NULL;
PyArrayIterObject *it;
PyArray_Descr *descr;
if (mit->ait == NULL) {
return -1;
}
descr = PyArray_DESCR(mit->ait->ao);
Py_INCREF(descr);
arr = (PyArrayObject *)PyArray_FromAny((PyObject *)op, descr,
0, 0, NPY_ARRAY_FORCECAST, NULL);
if (arr == NULL) {
return -1;
}
if ((mit->subspace != NULL) && (mit->consec)) {
PyArray_MapIterSwapAxes(mit, (PyArrayObject **)&arr, 0);
if (arr == NULL) {
return -1;
}
}
it = (PyArrayIterObject*)
PyArray_BroadcastToShape((PyObject*)arr, mit->dimensions, mit->nd);
if (it == NULL) {
Py_DECREF(arr);
return -1;
}
(*add_inplace)(mit, it, inc_or_set);
Py_DECREF(arr);
Py_DECREF(it);
return 0;
}
static int
inplace_increment(PyArrayObject *a, PyObject *index, PyArrayObject *inc,
int inc_or_set)
{
inplace_map_binop add_inplace = NULL;
int type_number = -1;
int i = 0;
PyArrayMapIterObject * mit;
if (PyArray_FailUnlessWriteable(a, "input/output array") < 0) {
return -1;
}
if (PyArray_NDIM(a) == 0) {
PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed.");
return -1;
}
type_number = PyArray_TYPE(a);
while (type_numbers[i] >= 0 && addition_funcs[i] != NULL){
if (type_number == type_numbers[i]) {
add_inplace = addition_funcs[i];
break;
}
i++ ;
}
if (add_inplace == NULL) {
PyErr_SetString(PyExc_TypeError, "unsupported type for a");
return -1;
}
mit = (PyArrayMapIterObject *) PyArray_MapIterArray(a, index);
if (mit == NULL) {
goto fail;
}
if (map_increment(mit, inc, add_inplace, inc_or_set) != 0) {
goto fail;
}
Py_DECREF(mit);
Py_INCREF(Py_None);
return 0;
fail:
Py_XDECREF(mit);
return -1;
}
"""
)
return
code
def
c_code
(
self
,
node
,
name
,
input_names
,
output_names
,
sub
):
def
c_code
(
self
,
node
,
name
,
input_names
,
output_names
,
sub
):
x
,
y
,
idx
=
input_names
x
,
y
,
idx
=
input_names
[
out
]
=
output_names
[
out
]
=
output_names
...
@@ -2636,34 +2443,7 @@ class AdvancedIncSubtensor1(COp):
...
@@ -2636,34 +2443,7 @@ class AdvancedIncSubtensor1(COp):
"""
"""
return
code
return
code
if
numpy_version
<
"1.8.0"
or
using_numpy_2
:
raise
NotImplementedError
raise
NotImplementedError
return
f
"""
PyObject* rval = NULL;
if ({params}->inplace)
{{
if ({x} != {out})
{{
Py_XDECREF({out});
Py_INCREF({x});
{out} = {x};
}}
}}
else
{{
Py_XDECREF({out});
{out} = {copy_of_x};
if (!{out}) {{
// Exception already set
{fail}
}}
}}
if (inplace_increment({out}, (PyObject *){idx}, {y}, (1 - {params}->set_instead_of_inc))) {{
{fail};
}}
Py_XDECREF(rval);
"""
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
10
,)
return
(
10
,)
...
...
tests/tensor/test_math.py
浏览文件 @
03492d44
...
@@ -24,7 +24,6 @@ from pytensor.graph.fg import FunctionGraph
...
@@ -24,7 +24,6 @@ from pytensor.graph.fg import FunctionGraph
from
pytensor.graph.replace
import
vectorize_node
from
pytensor.graph.replace
import
vectorize_node
from
pytensor.graph.traversal
import
ancestors
,
applys_between
from
pytensor.graph.traversal
import
ancestors
,
applys_between
from
pytensor.link.c.basic
import
DualLinker
from
pytensor.link.c.basic
import
DualLinker
from
pytensor.npy_2_compat
import
using_numpy_2
from
pytensor.printing
import
pprint
from
pytensor.printing
import
pprint
from
pytensor.raise_op
import
Assert
from
pytensor.raise_op
import
Assert
from
pytensor.tensor
import
blas
,
blas_c
from
pytensor.tensor
import
blas
,
blas_c
...
@@ -399,10 +398,7 @@ TestAbsBroadcast = makeBroadcastTester(
...
@@ -399,10 +398,7 @@ TestAbsBroadcast = makeBroadcastTester(
# in numpy >= 2.0, negating a uint raises an error
# in numpy >= 2.0, negating a uint raises an error
neg_good
=
_good_broadcast_unary_normal
.
copy
()
neg_good
=
_good_broadcast_unary_normal
.
copy
()
if
using_numpy_2
:
neg_bad
=
{
"uint8"
:
neg_good
.
pop
(
"uint8"
),
"uint16"
:
neg_good
.
pop
(
"uint16"
)}
neg_bad
=
{
"uint8"
:
neg_good
.
pop
(
"uint8"
),
"uint16"
:
neg_good
.
pop
(
"uint16"
)}
else
:
neg_bad
=
None
TestNegBroadcast
=
makeBroadcastTester
(
TestNegBroadcast
=
makeBroadcastTester
(
op
=
neg
,
op
=
neg
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论