Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
223ee154
提交
223ee154
authored
12月 15, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
12月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update Reshape C implementation
These changes remove the stride-based manual computation of the new shape, since those are potentially sensitive to broadcasted arrays with no strides.
上级
79961a62
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
59 行增加
和
60 行删除
+59
-60
shape.py
aesara/tensor/shape.py
+34
-44
test_shape.py
tests/tensor/test_shape.py
+25
-16
没有找到文件。
aesara/tensor/shape.py
浏览文件 @
223ee154
...
...
@@ -14,7 +14,7 @@ from aesara.tensor import _get_vector_length
from
aesara.tensor
import
basic
as
aet
from
aesara.tensor.exceptions
import
NotScalarConstantError
from
aesara.tensor.type
import
TensorType
,
int_dtypes
,
tensor
from
aesara.tensor.var
import
TensorConstant
,
TensorVariable
from
aesara.tensor.var
import
TensorConstant
def
register_shape_c_code
(
type
,
code
,
version
=
()):
...
...
@@ -570,15 +570,11 @@ class Reshape(COp):
if
len
(
shp
)
!=
self
.
ndim
:
raise
ValueError
(
(
"shape argument to Reshape.perform has incorrect"
f
" length {len(shp)}"
f
", should be {self.ndim}"
"Shape argument to Reshape has incorrect"
f
" length: {len(shp)}, should be {self.ndim}"
)
)
try
:
out
[
0
]
=
np
.
reshape
(
x
,
shp
)
except
Exception
:
raise
ValueError
(
f
"Cannot reshape input of shape {x.shape} to shape {shp}"
)
out
[
0
]
=
np
.
reshape
(
x
,
shp
)
def
connection_pattern
(
self
,
node
):
return
[[
True
],
[
False
]]
...
...
@@ -669,44 +665,38 @@ class Reshape(COp):
]
def
c_code_cache_version
(
self
):
return
(
8
,)
return
(
9
,)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
if
isinstance
(
node
.
inputs
[
0
],
TensorVariable
):
x
,
shp
=
inputs
(
z
,)
=
outputs
sdtype
=
node
.
inputs
[
1
]
.
type
.
dtype_specs
()[
1
]
fail
=
sub
[
"fail"
]
params
=
sub
[
"params"
]
return
(
"""
assert (PyArray_NDIM(
%(shp)
s) == 1);
npy_intp new_dims[
%(params)
s->ndim];
PyArray_Dims newshape;
newshape.ptr = new_dims;
newshape.len =
%(params)
s->ndim;
for (int ii = 0; ii <
%(params)
s->ndim; ++ii)
{
// -- We do not want an explicit cast here. the shp can be any
// -- int* dtype. The compiler will explicitly upcast it, but
// -- will err if this will downcast. This could happen if the
// -- user pass an int64 dtype, but npy_intp endup being int32.
new_dims[ii] = ((
%(sdtype)
s*)(
PyArray_BYTES(
%(shp)
s) +
ii * PyArray_STRIDES(
%(shp)
s)[0]))[0];
}
Py_XDECREF(
%(z)
s);
%(z)
s = (PyArrayObject *) PyArray_Newshape(
%(x)
s, &newshape, NPY_CORDER);
if (!
%(z)
s)
{
//The error message should have been set by PyArray_Newshape
%(fail)
s;
}
"""
%
locals
()
)
else
:
raise
NotImplementedError
()
x
,
shp
=
inputs
(
z
,)
=
outputs
fail
=
sub
[
"fail"
]
params
=
sub
[
"params"
]
return
f
"""
assert (PyArray_NDIM({shp}) == 1);
PyArray_Dims newshape;
if (!PyArray_IntpConverter((PyObject *){shp}, &newshape)) {{
{fail};
}}
if ({params}->ndim != newshape.len) {{
PyErr_SetString(PyExc_ValueError, "Shape argument to Reshape has incorrect length");
PyDimMem_FREE(newshape.ptr);
{fail};
}}
Py_XDECREF({z});
{z} = (PyArrayObject *) PyArray_Newshape({x}, &newshape, NPY_CORDER);
PyDimMem_FREE(newshape.ptr);
if (!{z}) {{
//The error message should have been set by PyArray_Newshape
{fail};
}}
"""
def
reshape
(
x
,
newshape
,
ndim
=
None
):
...
...
tests/tensor/test_shape.py
浏览文件 @
223ee154
...
...
@@ -222,6 +222,10 @@ class TestReshape(utt.InferShapeTester, utt.OptimizationTestMixin):
f
(
a_val
,
[
7
,
5
])
with
pytest
.
raises
(
ValueError
):
f
(
a_val
,
[
-
1
,
-
1
])
with
pytest
.
raises
(
ValueError
,
match
=
".*Shape argument to Reshape has incorrect length.*"
):
f
(
a_val
,
[
3
,
4
,
1
])
def
test_0
(
self
):
x
=
fvector
(
"x"
)
...
...
@@ -267,14 +271,14 @@ class TestReshape(utt.InferShapeTester, utt.OptimizationTestMixin):
[
admat
],
[
Reshape
(
ndim
)(
admat
,
[
-
1
,
4
])],
[
admat_val
],
Reshape
)
# enable when infer_shape is generalized:
# self._compile_and_check([admat, aivec],
# [Reshape(ndim)(admat, aivec)],
# [admat_val, [4, 3]], Reshape
)
#
# self._compile_and_check([admat, aivec],
# [Reshape(ndim)(admat, aivec)],
# [admat_val, [4, -1]], Reshape
)
aivec
=
ivector
()
self
.
_compile_and_check
(
[
admat
,
aivec
],
[
Reshape
(
ndim
)(
admat
,
aivec
)],
[
admat_val
,
[
4
,
3
]],
Reshape
)
self
.
_compile_and_check
(
[
admat
,
aivec
],
[
Reshape
(
ndim
)(
admat
,
aivec
)],
[
admat_val
,
[
4
,
-
1
]],
Reshape
)
adtens4
=
dtensor4
()
ndim
=
4
...
...
@@ -287,14 +291,19 @@ class TestReshape(utt.InferShapeTester, utt.OptimizationTestMixin):
[
adtens4
],
[
Reshape
(
ndim
)(
adtens4
,
[
1
,
3
,
10
,
4
])],
[
adtens4_val
],
Reshape
)
# enable when infer_shape is generalized:
# self._compile_and_check([adtens4, aivec],
# [Reshape(ndim)(adtens4, aivec)],
# [adtens4_val, [1, -1, 10, 4]], Reshape)
#
# self._compile_and_check([adtens4, aivec],
# [Reshape(ndim)(adtens4, aivec)],
# [adtens4_val, [1, 3, 10, 4]], Reshape)
self
.
_compile_and_check
(
[
adtens4
,
aivec
],
[
Reshape
(
ndim
)(
adtens4
,
aivec
)],
[
adtens4_val
,
[
1
,
-
1
,
10
,
4
]],
Reshape
,
)
self
.
_compile_and_check
(
[
adtens4
,
aivec
],
[
Reshape
(
ndim
)(
adtens4
,
aivec
)],
[
adtens4_val
,
[
1
,
3
,
10
,
4
]],
Reshape
,
)
def
test_shape_i_hash
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论