Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
18449731
提交
18449731
authored
2月 06, 2026
作者:
ricardoV94
提交者:
Ricardo Vieira
2月 10, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix discrete xtensor reduction
上级
4ce8ef3c
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
58 行增加
和
6 行删除
+58
-6
reduction.py
pytensor/xtensor/reduction.py
+11
-6
test_reduction.py
tests/xtensor/test_reduction.py
+47
-0
没有找到文件。
pytensor/xtensor/reduction.py
浏览文件 @
18449731
...
...
@@ -9,7 +9,7 @@ from pytensor.tensor.math import variadic_mul
from
pytensor.xtensor.basic
import
XOp
from
pytensor.xtensor.math
import
neq
,
sqrt
from
pytensor.xtensor.math
import
sqr
as
square
from
pytensor.xtensor.type
import
as_xtensor
,
xtensor
from
pytensor.xtensor.type
import
XTensorVariable
,
as_xtensor
,
xtensor
REDUCE_DIM
=
str
|
Sequence
[
str
]
|
EllipsisType
|
None
...
...
@@ -47,22 +47,24 @@ class XReduce(XOp):
return
Apply
(
self
,
[
x
],
[
output
])
def
_process_user_dims
(
x
,
dim
:
REDUCE_DIM
)
->
Sequence
[
str
]:
def
_process_user_dims
(
x
:
XTensorVariable
,
dim
:
REDUCE_DIM
)
->
Sequence
[
str
]:
if
isinstance
(
dim
,
str
):
return
(
dim
,)
elif
dim
is
None
or
dim
is
Ellipsis
:
x
=
as_xtensor
(
x
)
return
typing
.
cast
(
tuple
[
str
],
x
.
type
.
dims
)
return
dim
def
reduce
(
x
,
dim
:
REDUCE_DIM
=
None
,
*
,
binary_op
):
def
reduce
(
x
,
dim
:
REDUCE_DIM
=
None
,
*
,
binary_op
,
upcast_discrete_inp
:
bool
=
False
):
x
=
as_xtensor
(
x
)
dims
=
_process_user_dims
(
x
,
dim
)
if
upcast_discrete_inp
and
((
x_kind
:
=
x
.
type
.
numpy_dtype
.
kind
)
in
"ibu"
):
x
=
x
.
astype
(
"uint64"
if
x_kind
==
"u"
else
"int64"
)
return
XReduce
(
binary_op
=
binary_op
,
dims
=
dims
)(
x
)
sum
=
partial
(
reduce
,
binary_op
=
ps
.
add
)
prod
=
partial
(
reduce
,
binary_op
=
ps
.
mul
)
sum
=
partial
(
reduce
,
binary_op
=
ps
.
add
,
upcast_discrete_inp
=
True
)
prod
=
partial
(
reduce
,
binary_op
=
ps
.
mul
,
upcast_discrete_inp
=
True
)
max
=
partial
(
reduce
,
binary_op
=
ps
.
maximum
)
min
=
partial
(
reduce
,
binary_op
=
ps
.
minimum
)
...
...
@@ -117,7 +119,10 @@ class XCumReduce(XOp):
def
cumreduce
(
x
,
dim
:
REDUCE_DIM
,
*
,
binary_op
):
x
=
as_xtensor
(
x
)
dims
=
_process_user_dims
(
x
,
dim
)
if
(
x_kind
:
=
x
.
type
.
numpy_dtype
.
kind
)
in
"ibu"
:
x
=
x
.
astype
(
"uint64"
if
x_kind
==
"u"
else
"int64"
)
return
XCumReduce
(
dims
=
dims
,
binary_op
=
binary_op
)(
x
)
...
...
tests/xtensor/test_reduction.py
浏览文件 @
18449731
...
...
@@ -4,6 +4,9 @@ import pytest
pytest
.
importorskip
(
"xarray"
)
pytestmark
=
pytest
.
mark
.
filterwarnings
(
"error"
)
import
numpy
as
np
import
xarray
as
xr
from
pytensor.xtensor.type
import
xtensor
from
tests.xtensor.util
import
xr_arange_like
,
xr_assert_allclose
,
xr_function
...
...
@@ -52,3 +55,47 @@ def test_std_var(method, dim):
results
[
1
],
getattr
(
x_test
,
method
)(
dim
=
dim
,
ddof
=
2
),
)
@pytest.mark.parametrize
(
"signed"
,
[
True
,
False
])
def
test_discrete_reduction_upcasting
(
signed
):
# Test that sum, prod reductions on discrete inputs are upcast to prevent overflow
# This is also a regression test for lower_xtensor, which would raise by returning a different dtype
in_dtype
=
"int8"
if
signed
else
"uint8"
out_dtype
=
"int64"
if
signed
else
"uint64"
test_val
=
127
if
signed
else
255
# max value allowed by in_dtype
x
=
xtensor
(
"x"
,
dtype
=
in_dtype
,
dims
=
(
"a"
,),
shape
=
(
2
,))
x_val
=
xr
.
DataArray
(
np
.
array
([
test_val
,
test_val
],
dtype
=
in_dtype
),
dims
=
"a"
)
assert
x_val
.
dtype
==
in_dtype
# sum
out
=
x
.
sum
()
assert
out
.
dtype
==
out_dtype
fn
=
xr_function
([
x
],
out
)
res
=
fn
(
x_val
)
assert
res
==
test_val
*
2
xr_assert_allclose
(
res
,
x_val
.
sum
())
# prod
out
=
x
.
prod
()
assert
out
.
dtype
==
out_dtype
fn
=
xr_function
([
x
],
out
)
res
=
fn
(
x_val
)
assert
res
==
test_val
**
2
xr_assert_allclose
(
res
,
x_val
.
prod
())
# cumsum
out
=
x
.
cumsum
()
assert
out
.
dtype
==
out_dtype
fn
=
xr_function
([
x
],
out
)
res
=
fn
(
x_val
)
np
.
testing
.
assert_allclose
(
res
,
[
test_val
,
test_val
*
2
])
xr_assert_allclose
(
res
,
x_val
.
cumsum
())
# cumprod
out
=
x
.
cumprod
()
assert
out
.
dtype
==
out_dtype
fn
=
xr_function
([
x
],
out
)
res
=
fn
(
x_val
)
np
.
testing
.
assert_allclose
(
res
,
[
test_val
,
test_val
**
2
])
xr_assert_allclose
(
res
,
x_val
.
cumprod
())
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论