Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f4536c30
提交
f4536c30
authored
6月 15, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
6月 15, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make Split a view_op
This allows the outputs to be views of the inputs. The Python and Numba implementation do that, but the C still performs a copy
上级
91966e85
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
108 行增加
和
48 行删除
+108
-48
basic.py
pytensor/tensor/basic.py
+3
-1
test_tensor_basic.py
tests/link/numba/test_tensor_basic.py
+26
-1
test_basic.py
tests/tensor/rewriting/test_basic.py
+19
-6
test_basic.py
tests/tensor/test_basic.py
+60
-40
没有找到文件。
pytensor/tensor/basic.py
浏览文件 @
f4536c30
...
@@ -1903,6 +1903,7 @@ class Split(COp):
...
@@ -1903,6 +1903,7 @@ class Split(COp):
b == [3, 4]
b == [3, 4]
c == [5]
c == [5]
TODO: Don't make a copy in C impl
"""
"""
len_splits
=
None
len_splits
=
None
...
@@ -1913,6 +1914,7 @@ class Split(COp):
...
@@ -1913,6 +1914,7 @@ class Split(COp):
def
__init__
(
self
,
len_splits
):
def
__init__
(
self
,
len_splits
):
self
.
len_splits
=
int
(
len_splits
)
self
.
len_splits
=
int
(
len_splits
)
self
.
view_map
=
{
i
:
[
0
]
for
i
in
range
(
self
.
len_splits
)}
def
__str__
(
self
):
def
__str__
(
self
):
return
f
"{self.__class__.__name__ }{{{self.len_splits}}}"
return
f
"{self.__class__.__name__ }{{{self.len_splits}}}"
...
@@ -1949,7 +1951,7 @@ class Split(COp):
...
@@ -1949,7 +1951,7 @@ class Split(COp):
split_outs
=
np
.
split
(
x
,
np
.
cumsum
(
splits
[:
-
1
]),
axis
=
axis
)
split_outs
=
np
.
split
(
x
,
np
.
cumsum
(
splits
[:
-
1
]),
axis
=
axis
)
for
i
,
out
in
enumerate
(
split_outs
):
for
i
,
out
in
enumerate
(
split_outs
):
outputs
[
i
][
0
]
=
out
.
copy
()
outputs
[
i
][
0
]
=
out
def
infer_shape
(
self
,
fgraph
,
node
,
in_shapes
):
def
infer_shape
(
self
,
fgraph
,
node
,
in_shapes
):
axis
=
node
.
inputs
[
1
]
axis
=
node
.
inputs
[
1
]
...
...
tests/link/numba/test_tensor_basic.py
浏览文件 @
f4536c30
...
@@ -4,10 +4,11 @@ import pytest
...
@@ -4,10 +4,11 @@ import pytest
import
pytensor.scalar
as
aes
import
pytensor.scalar
as
aes
import
pytensor.tensor
as
at
import
pytensor.tensor
as
at
import
pytensor.tensor.basic
as
atb
import
pytensor.tensor.basic
as
atb
from
pytensor
import
config
from
pytensor
import
config
,
function
from
pytensor.compile.sharedvalue
import
SharedVariable
from
pytensor.compile.sharedvalue
import
SharedVariable
from
pytensor.graph.basic
import
Constant
from
pytensor.graph.basic
import
Constant
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.scalar
import
Add
from
pytensor.tensor.shape
import
Unbroadcast
from
pytensor.tensor.shape
import
Unbroadcast
from
tests.link.numba.test_basic
import
(
from
tests.link.numba.test_basic
import
(
compare_numba_and_py
,
compare_numba_and_py
,
...
@@ -332,6 +333,30 @@ def test_Split(n_splits, axis, values, sizes):
...
@@ -332,6 +333,30 @@ def test_Split(n_splits, axis, values, sizes):
)
)
def
test_Split_view
():
# https://github.com/pymc-devs/pytensor/issues/343
x1
=
at
.
matrix
(
"x1"
)
x2
=
at
.
matrix
(
"x2"
,
shape
=
(
None
,
1
))
v
=
at
.
vector
(
"v"
,
shape
=
(
2
,),
dtype
=
int
)
out
=
at
.
split
(
x1
,
v
,
n_splits
=
2
,
axis
=
1
)[
0
]
+
x2
fn
=
function
([
x1
,
x2
,
v
],
out
,
mode
=
"NUMBA"
)
# Check that the addition of split[0] and x2 is not in place
add_op
=
fn
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
assert
isinstance
(
add_op
.
scalar_op
,
Add
)
assert
not
add_op
.
inplace_pattern
rng
=
np
.
random
.
default_rng
(
123
)
test_x1
=
rng
.
normal
(
size
=
(
2
,
2
))
test_x2
=
rng
.
normal
(
size
=
(
2
,
1
))
test_v
=
np
.
array
([
1
,
1
])
np
.
testing
.
assert_allclose
(
fn
(
test_x1
,
test_x2
,
test_v
)
.
copy
(),
fn
(
test_x1
,
test_x2
,
test_v
)
.
copy
(),
)
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"val, offset"
,
"val, offset"
,
[
[
...
...
tests/tensor/rewriting/test_basic.py
浏览文件 @
f4536c30
...
@@ -1372,15 +1372,28 @@ def test_local_useless_split():
...
@@ -1372,15 +1372,28 @@ def test_local_useless_split():
f_rewritten
(
np
.
random
.
random
((
4
,
4
))
.
astype
(
config
.
floatX
),
[
4
])
f_rewritten
(
np
.
random
.
random
((
4
,
4
))
.
astype
(
config
.
floatX
),
[
4
])
f_not_rewritten
(
np
.
random
.
random
((
4
,
4
))
.
astype
(
config
.
floatX
),
[
1
,
2
,
1
])
f_not_rewritten
(
np
.
random
.
random
((
4
,
4
))
.
astype
(
config
.
floatX
),
[
1
,
2
,
1
])
graph_rewritten
=
f_rewritten
.
maker
.
fgraph
.
toposort
()
graph_rewritten
=
f_rewritten
.
maker
.
fgraph
graph_not_rewritten
=
f_not_rewritten
.
maker
.
fgraph
.
toposort
()
graph_not_rewritten
=
f_not_rewritten
.
maker
.
fgraph
assert
isinstance
(
graph_rewritten
[
-
1
]
.
op
,
DeepCopyOp
)
assert
all
(
assert
len
(
graph_not_rewritten
)
==
1
isinstance
(
out
.
owner
.
op
,
DeepCopyOp
)
for
out
in
graph_not_rewritten
.
outputs
assert
isinstance
(
graph_not_rewritten
[
0
]
.
op
,
Split
)
)
assert
all
(
isinstance
(
out
.
owner
.
op
,
DeepCopyOp
)
for
out
in
graph_rewritten
.
outputs
)
assert
sum
(
isinstance
(
node
.
op
,
Split
)
for
node
in
graph_rewritten
.
apply_nodes
)
==
0
assert
(
sum
(
isinstance
(
node
.
op
,
Split
)
for
node
in
graph_not_rewritten
.
apply_nodes
)
==
1
)
assert
sum
(
isinstance
(
node
.
op
,
Assert
)
for
node
in
graph_rewritten
.
apply_nodes
)
==
2
assert
(
sum
(
isinstance
(
node
.
op
,
Assert
)
for
node
in
graph_not_rewritten
.
apply_nodes
)
==
0
)
# The DeepCopy Ops don't have traces, so we can't check "all"
assert
check_stack_trace
(
f_rewritten
,
ops_to_check
=
[
Assert
])
assert
check_stack_trace
(
f_rewritten
,
ops_to_check
=
[
Assert
])
assert
check_stack_trace
(
f_not_rewritten
,
ops_to_check
=
"all"
)
assert
check_stack_trace
(
f_not_rewritten
,
ops_to_check
=
[
Split
]
)
@pytest.mark.parametrize
(
"i"
,
list
(
range
(
1
,
4
)))
@pytest.mark.parametrize
(
"i"
,
list
(
range
(
1
,
4
)))
...
...
tests/tensor/test_basic.py
浏览文件 @
f4536c30
...
@@ -11,7 +11,7 @@ import pytensor.tensor.basic as at
...
@@ -11,7 +11,7 @@ import pytensor.tensor.basic as at
import
pytensor.tensor.math
as
tm
import
pytensor.tensor.math
as
tm
from
pytensor
import
compile
,
config
,
function
,
shared
from
pytensor
import
compile
,
config
,
function
,
shared
from
pytensor.compile.io
import
In
,
Out
from
pytensor.compile.io
import
In
,
Out
from
pytensor.compile.mode
import
get_default_mode
from
pytensor.compile.mode
import
Mode
,
get_default_mode
from
pytensor.compile.ops
import
DeepCopyOp
from
pytensor.compile.ops
import
DeepCopyOp
from
pytensor.gradient
import
grad
,
hessian
from
pytensor.gradient
import
grad
,
hessian
from
pytensor.graph.basic
import
Apply
from
pytensor.graph.basic
import
Apply
...
@@ -2002,45 +2002,65 @@ class TestJoinAndSplit:
...
@@ -2002,45 +2002,65 @@ class TestJoinAndSplit:
y
=
Split
(
2
)(
x
,
0
,
[
s
,
5
-
s
])[
0
]
y
=
Split
(
2
)(
x
,
0
,
[
s
,
5
-
s
])[
0
]
assert
y
.
type
.
shape
==
(
None
,)
assert
y
.
type
.
shape
==
(
None
,)
def
test_join_inplace
(
self
):
def
test_join_inplace
():
# Test join to work inplace.
# Test join to work inplace.
#
#
# This function tests the case when several elements are passed to the
# This function tests the case when several elements are passed to the
# join function but all except one of them are empty. In this case join
# join function but all except one of them are empty. In this case join
# should work inplace and the output should be the view of the non-empty
# should work inplace and the output should be the view of the non-empty
# element.
# element.
s
=
lscalar
()
s
=
lscalar
()
x
=
vector
(
"x"
)
x
=
vector
(
"x"
)
z
=
at
.
zeros
((
s
,))
z
=
at
.
zeros
((
s
,))
join
=
Join
(
view
=
0
)
join
=
Join
(
view
=
0
)
c
=
join
(
0
,
x
,
z
,
z
)
c
=
join
(
0
,
x
,
z
,
z
)
f
=
pytensor
.
function
([
In
(
x
,
borrow
=
True
),
s
],
Out
(
c
,
borrow
=
True
))
f
=
pytensor
.
function
([
In
(
x
,
borrow
=
True
),
s
],
Out
(
c
,
borrow
=
True
))
data
=
np
.
array
([
3
,
4
,
5
],
dtype
=
config
.
floatX
)
data
=
np
.
array
([
3
,
4
,
5
],
dtype
=
config
.
floatX
)
if
config
.
mode
not
in
[
"DebugMode"
,
"DEBUG_MODE"
]:
if
config
.
mode
not
in
[
"DebugMode"
,
"DEBUG_MODE"
]:
assert
f
(
data
,
0
)
is
data
assert
f
(
data
,
0
)
is
data
assert
np
.
allclose
(
f
(
data
,
0
),
[
3
,
4
,
5
])
assert
np
.
allclose
(
f
(
data
,
0
),
[
3
,
4
,
5
])
def
test_join_oneInput
(
self
):
# Test join when only 1 input is given.
def
test_join_oneInput
():
#
# Test join when only 1 input is given.
# This functions tests the case when concatenate is called
#
# on an array of tensors but the array has only one element.
# This functions tests the case when concatenate is called
# In this case, we would like to avoid the computational
# on an array of tensors but the array has only one element.
# overhead of concatenation of one element.
# In this case, we would like to avoid the computational
x_0
=
fmatrix
()
# overhead of concatenation of one element.
x_1
=
fmatrix
()
x_0
=
fmatrix
()
x_2
=
fvector
()
x_1
=
fmatrix
()
join_0
=
at
.
concatenate
([
x_0
],
axis
=
1
)
x_2
=
fvector
()
join_1
=
at
.
concatenate
([
x_0
,
x_1
,
shape_padright
(
x_2
)],
axis
=
1
)
join_0
=
at
.
concatenate
([
x_0
],
axis
=
1
)
join_1
=
at
.
concatenate
([
x_0
,
x_1
,
shape_padright
(
x_2
)],
axis
=
1
)
assert
join_0
is
x_0
assert
join_1
is
not
x_0
assert
join_0
is
x_0
assert
join_1
is
not
x_0
@pytest.mark.parametrize
(
"linker"
,
(
"py"
,
"c"
))
def
test_split_view
(
self
,
linker
):
x
=
vector
(
"x"
)
axis
=
0
op
=
Split
(
len_splits
=
3
)
assert
op
.
view_map
==
{
0
:
[
0
],
1
:
[
0
],
2
:
[
0
]}
splits
=
op
(
x
,
axis
,
[
0
,
3
,
2
])
mode
=
Mode
(
linker
)
f
=
pytensor
.
function
(
[
In
(
x
,
borrow
=
True
)],
[
Out
(
s
,
borrow
=
True
)
for
s
in
splits
],
mode
=
mode
)
x_test
=
np
.
arange
(
5
,
dtype
=
config
.
floatX
)
res
=
f
(
x_test
)
for
r
,
expected
in
zip
(
res
,
([],
[
0
,
1
,
2
],
[
3
,
4
])):
assert
np
.
allclose
(
r
,
expected
)
if
linker
==
"py"
:
assert
r
.
base
is
x_test
else
:
# C impl always makes a copy
assert
r
.
base
is
not
x_test
def
test_TensorFromScalar
():
def
test_TensorFromScalar
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论