Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
84936418
Unverified
提交
84936418
authored
2月 18, 2021
作者:
Kaustubh
提交者:
GitHub
2月 18, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor and simplify CAReduce, Max, and Min Ops (#297)
上级
584c0c15
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
86 行增加
和
51 行删除
+86
-51
elemwise.py
aesara/gpuarray/elemwise.py
+3
-5
basic.py
aesara/scalar/basic.py
+10
-8
elemwise.py
aesara/tensor/elemwise.py
+0
-0
math.py
aesara/tensor/math.py
+33
-2
math_opt.py
aesara/tensor/math_opt.py
+13
-8
test_elemwise.py
tests/tensor/test_elemwise.py
+27
-28
没有找到文件。
aesara/gpuarray/elemwise.py
浏览文件 @
84936418
...
@@ -3024,11 +3024,9 @@ class GpuCAReduceCPY(GpuKernelBase, HideC, CAReduceDtype):
...
@@ -3024,11 +3024,9 @@ class GpuCAReduceCPY(GpuKernelBase, HideC, CAReduceDtype):
"""
"""
def
__init__
(
self
,
scalar_op
,
axis
=
None
,
dtype
=
None
,
acc_dtype
=
None
):
def
__init__
(
self
,
scalar_op
,
axis
=
None
,
dtype
=
None
,
acc_dtype
=
None
):
if
not
hasattr
(
scalar_op
,
"identity"
)
:
if
scalar_op
.
identity
is
None
:
raise
ValueError
(
"No identity on scalar op"
)
raise
ValueError
(
"No identity on scalar op"
)
CAReduceDtype
.
__init__
(
super
()
.
__init__
(
scalar_op
,
axis
=
axis
,
dtype
=
dtype
,
acc_dtype
=
acc_dtype
)
self
,
scalar_op
,
axis
=
axis
,
dtype
=
dtype
,
acc_dtype
=
acc_dtype
)
def
__str__
(
self
):
def
__str__
(
self
):
ax
=
""
ax
=
""
...
@@ -3038,7 +3036,7 @@ class GpuCAReduceCPY(GpuKernelBase, HideC, CAReduceDtype):
...
@@ -3038,7 +3036,7 @@ class GpuCAReduceCPY(GpuKernelBase, HideC, CAReduceDtype):
def
make_node
(
self
,
input
):
def
make_node
(
self
,
input
):
ctx_name
=
infer_context_name
(
input
)
ctx_name
=
infer_context_name
(
input
)
res
=
CAReduceDtype
.
make_node
(
self
,
input
)
res
=
super
()
.
make_node
(
input
)
input
=
as_gpuarray_variable
(
input
,
ctx_name
)
input
=
as_gpuarray_variable
(
input
,
ctx_name
)
otype
=
GpuArrayType
(
otype
=
GpuArrayType
(
dtype
=
res
.
outputs
[
0
]
.
dtype
,
dtype
=
res
.
outputs
[
0
]
.
dtype
,
...
...
aesara/scalar/basic.py
浏览文件 @
84936418
...
@@ -1259,19 +1259,19 @@ class UnaryScalarOp(ScalarOp):
...
@@ -1259,19 +1259,19 @@ class UnaryScalarOp(ScalarOp):
class
BinaryScalarOp
(
ScalarOp
):
class
BinaryScalarOp
(
ScalarOp
):
# One may define in subclasses the following fields:
# One may define in subclasses the following fields:
# - `identity`: for an associative operation, identity corresponds to
# the neutral element. For instance, it will be 0 for addition, 1 for
# multiplication, True for "and", False for "or".
# - `commutative`: whether op(a, b) == op(b, a)
# - `commutative`: whether op(a, b) == op(b, a)
# - `associative`: whether op(op(a, b), c) == op(a, op(b, c))
# - `associative`: whether op(op(a, b), c) == op(a, op(b, c))
commutative
=
None
associative
=
None
identity
=
None
"""
For an associative operation, the identity object corresponds to the neutral
element. For instance, it will be ``0`` for addition, ``1`` for multiplication,
``True`` for ``and``, ``False`` for ``or``.
"""
nin
=
2
nin
=
2
###############
# Comparisons
###############
class
LogicalComparison
(
BinaryScalarOp
):
class
LogicalComparison
(
BinaryScalarOp
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
BinaryScalarOp
.
__init__
(
self
,
*
args
,
**
kwargs
)
BinaryScalarOp
.
__init__
(
self
,
*
args
,
**
kwargs
)
...
@@ -1725,6 +1725,7 @@ class ScalarMaximum(BinaryScalarOp):
...
@@ -1725,6 +1725,7 @@ class ScalarMaximum(BinaryScalarOp):
associative
=
True
associative
=
True
nfunc_spec
=
(
"maximum"
,
2
,
1
)
nfunc_spec
=
(
"maximum"
,
2
,
1
)
nfunc_variadic
=
"maximum"
nfunc_variadic
=
"maximum"
identity
=
-
np
.
inf
def
impl
(
self
,
*
inputs
):
def
impl
(
self
,
*
inputs
):
# The built-in max function don't support complex type
# The built-in max function don't support complex type
...
@@ -1767,6 +1768,7 @@ class ScalarMinimum(BinaryScalarOp):
...
@@ -1767,6 +1768,7 @@ class ScalarMinimum(BinaryScalarOp):
associative
=
True
associative
=
True
nfunc_spec
=
(
"minimum"
,
2
,
1
)
nfunc_spec
=
(
"minimum"
,
2
,
1
)
nfunc_variadic
=
"minimum"
nfunc_variadic
=
"minimum"
identity
=
np
.
inf
def
impl
(
self
,
*
inputs
):
def
impl
(
self
,
*
inputs
):
# The built-in min function don't support complex type
# The built-in min function don't support complex type
...
...
aesara/tensor/elemwise.py
浏览文件 @
84936418
差异被折叠。
点击展开。
aesara/tensor/math.py
浏览文件 @
84936418
...
@@ -585,14 +585,45 @@ def max_and_argmax(a, axis=None, keepdims=False):
...
@@ -585,14 +585,45 @@ def max_and_argmax(a, axis=None, keepdims=False):
return
[
out
,
argout
]
return
[
out
,
argout
]
class
Max
(
CAReduce
):
class
NonZeroCAReduce
(
CAReduce
):
def
_c_all
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
decl
,
checks
,
alloc
,
loop
,
end
=
super
()
.
_c_all
(
node
,
name
,
inames
,
onames
,
sub
)
# We add an additional check for zero-sized dimensions (This seems like
# something that could enabled in `elemwise_cgen.make_checks`.)
iname
=
inames
[
0
]
axis
=
self
.
axis
if
axis
is
None
:
axis
=
list
(
range
(
len
(
node
.
inputs
[
0
]
.
type
.
broadcastable
)))
pattern
=
[
0
]
*
len
(
node
.
inputs
[
0
]
.
broadcastable
)
for
i
in
axis
:
pattern
[
i
]
=
1
pattern_
=
str
(
pattern
)[
1
:
-
1
]
decl
+=
f
"""int tosum[]={{{pattern_}}};"""
alloc
+=
f
"""
for(int i=0;i<PyArray_NDIM({iname});i++){{
if(PyArray_DIMS({iname})[i]==0 && tosum[i]){{
PyErr_Format(PyExc_ValueError,
"Input of CAReduce{{{node.op.scalar_op}}} has zero-size on axis
%%
d",i);
{sub["fail"]};
}}
}}
"""
return
decl
,
checks
,
alloc
,
loop
,
end
class
Max
(
NonZeroCAReduce
):
nfunc_spec
=
(
"max"
,
1
,
1
)
nfunc_spec
=
(
"max"
,
1
,
1
)
def
__init__
(
self
,
axis
):
def
__init__
(
self
,
axis
):
super
()
.
__init__
(
aes
.
scalar_maximum
,
axis
)
super
()
.
__init__
(
aes
.
scalar_maximum
,
axis
)
class
Min
(
CAReduce
):
class
Min
(
NonZero
CAReduce
):
nfunc_spec
=
(
"min"
,
1
,
1
)
nfunc_spec
=
(
"min"
,
1
,
1
)
def
__init__
(
self
,
axis
):
def
__init__
(
self
,
axis
):
...
...
aesara/tensor/math_opt.py
浏览文件 @
84936418
...
@@ -60,6 +60,7 @@ from aesara.tensor.math import (
...
@@ -60,6 +60,7 @@ from aesara.tensor.math import (
All
,
All
,
Any
,
Any
,
Dot
,
Dot
,
NonZeroCAReduce
,
Prod
,
Prod
,
ProdWithoutZeros
,
ProdWithoutZeros
,
Sum
,
Sum
,
...
@@ -1534,14 +1535,18 @@ def local_op_of_op(fgraph, node):
...
@@ -1534,14 +1535,18 @@ def local_op_of_op(fgraph, node):
return
[
combined
(
node_inps
.
owner
.
inputs
[
0
])]
return
[
combined
(
node_inps
.
owner
.
inputs
[
0
])]
ALL_REDUCE
=
[
ALL_REDUCE
=
(
CAReduce
,
[
All
,
CAReduce
,
Any
,
All
,
Sum
,
Any
,
Prod
,
Sum
,
ProdWithoutZeros
,
Prod
,
]
+
CAReduce
.
__subclasses__
()
ProdWithoutZeros
,
]
+
CAReduce
.
__subclasses__
()
+
NonZeroCAReduce
.
__subclasses__
()
)
@register_canonicalize
@register_canonicalize
...
...
tests/tensor/test_elemwise.py
浏览文件 @
84936418
...
@@ -372,7 +372,7 @@ class TestCAReduce(unittest_tools.InferShapeTester):
...
@@ -372,7 +372,7 @@ class TestCAReduce(unittest_tools.InferShapeTester):
zv
=
xv
zv
=
xv
if
pre_scalar_op
is
not
None
:
if
pre_scalar_op
is
not
None
:
zv
=
Elemwise
(
scalar_op
=
pre_scalar_op
)(
x
)
.
eval
({
x
:
xv
})
zv
=
Elemwise
(
scalar_op
=
pre_scalar_op
)(
x
)
.
eval
({
x
:
xv
})
numpy_raised
=
False
if
len
(
tosum
)
>
1
and
any
([
a
<
0
for
a
in
tosum
]):
if
len
(
tosum
)
>
1
and
any
([
a
<
0
for
a
in
tosum
]):
# In that case, we need to use the good order of axis
# In that case, we need to use the good order of axis
# in the reduction.
# in the reduction.
...
@@ -404,17 +404,19 @@ class TestCAReduce(unittest_tools.InferShapeTester):
...
@@ -404,17 +404,19 @@ class TestCAReduce(unittest_tools.InferShapeTester):
for
axis
in
reversed
(
sorted
(
tosum
)):
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
np
.
multiply
.
reduce
(
zv
,
axis
)
zv
=
np
.
multiply
.
reduce
(
zv
,
axis
)
elif
scalar_op
==
aes
.
scalar_maximum
:
elif
scalar_op
==
aes
.
scalar_maximum
:
try
:
# There is no identity value for the maximum function
for
axis
in
reversed
(
sorted
(
tosum
)):
# So we can't support shape of dimensions 0.
zv
=
np
.
maximum
.
reduce
(
zv
,
axis
)
if
np
.
prod
(
zv
.
shape
)
==
0
:
except
ValueError
:
continue
numpy_raised
=
True
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
np
.
maximum
.
reduce
(
zv
,
axis
)
elif
scalar_op
==
aes
.
scalar_minimum
:
elif
scalar_op
==
aes
.
scalar_minimum
:
try
:
# There is no identity value for the minimum function
for
axis
in
reversed
(
sorted
(
tosum
)):
# So we can't support shape of dimensions 0.
zv
=
np
.
minimum
.
reduce
(
zv
,
axis
)
if
np
.
prod
(
zv
.
shape
)
==
0
:
except
ValueError
:
continue
numpy_raised
=
True
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
np
.
minimum
.
reduce
(
zv
,
axis
)
elif
scalar_op
==
aes
.
or_
:
elif
scalar_op
==
aes
.
or_
:
for
axis
in
reversed
(
sorted
(
tosum
)):
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
np
.
bitwise_or
.
reduce
(
zv
,
axis
)
zv
=
np
.
bitwise_or
.
reduce
(
zv
,
axis
)
...
@@ -432,24 +434,21 @@ class TestCAReduce(unittest_tools.InferShapeTester):
...
@@ -432,24 +434,21 @@ class TestCAReduce(unittest_tools.InferShapeTester):
raise
Exception
(
raise
Exception
(
f
"Test for CAReduce with scalar_op {scalar_op} not implemented"
f
"Test for CAReduce with scalar_op {scalar_op} not implemented"
)
)
if
scalar_op
in
[
aes
.
scalar_maximum
,
aes
.
scalar_minimum
]
and
numpy_raised
:
with
pytest
.
raises
(
ValueError
):
if
test_nan
:
f
(
xv
)
try
:
assert
self
.
type
.
values_eq
(
f
(
xv
),
zv
),
(
f
(
xv
),
zv
)
except
NotImplementedError
:
# GpuCAReduce don't implement all cases when size is 0
assert
xv
.
size
==
0
else
:
else
:
if
test_nan
:
try
:
try
:
f_xv
=
f
(
xv
)
assert
self
.
type
.
values_eq
(
f
(
xv
),
zv
),
(
f
(
xv
),
zv
)
assert
f_xv
.
shape
==
zv
.
shape
,
(
f_xv
,
zv
)
except
NotImplementedError
:
utt
.
assert_allclose
(
zv
,
f_xv
)
# GpuCAReduce don't implement all cases when size is 0
except
NotImplementedError
:
assert
xv
.
size
==
0
# GpuCAReduce don't implement all cases when size is 0
else
:
assert
xv
.
size
==
0
try
:
f_xv
=
f
(
xv
)
assert
f_xv
.
shape
==
zv
.
shape
,
(
f_xv
,
zv
)
utt
.
assert_allclose
(
zv
,
f_xv
)
except
NotImplementedError
:
# GpuCAReduce don't implement all cases when size is 0
assert
xv
.
size
==
0
x
=
self
.
type
(
dtype
,
[(
entry
==
1
)
for
entry
in
xsh
])(
"x"
)
x
=
self
.
type
(
dtype
,
[(
entry
==
1
)
for
entry
in
xsh
])(
"x"
)
if
tensor_op
is
None
:
if
tensor_op
is
None
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论