Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5f374dbd
提交
5f374dbd
authored
5月 08, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
5月 24, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix numba AdvancedIncSubtensor1 with broadcasted values
上级
1e96b894
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
89 行增加
和
23 行删除
+89
-23
basic.py
pytensor/link/numba/dispatch/basic.py
+54
-20
test_basic.py
tests/link/numba/test_basic.py
+35
-3
没有找到文件。
pytensor/link/numba/dispatch/basic.py
浏览文件 @
5f374dbd
...
@@ -604,36 +604,70 @@ def numba_funcify_IncSubtensor(op, node, **kwargs):
...
@@ -604,36 +604,70 @@ def numba_funcify_IncSubtensor(op, node, **kwargs):
return
numba_njit
(
incsubtensor_fn
,
boundscheck
=
True
)
return
numba_njit
(
incsubtensor_fn
,
boundscheck
=
True
)
@numba_njit
(
boundscheck
=
True
)
@numba_funcify.register
(
AdvancedIncSubtensor1
)
def
advancedincsubtensor1_inplace_set
(
x
,
vals
,
idxs
):
def
numba_funcify_AdvancedIncSubtensor1
(
op
,
node
,
**
kwargs
):
for
idx
,
val
in
zip
(
idxs
,
vals
):
inplace
=
op
.
inplace
x
[
idx
]
=
val
set_instead_of_inc
=
op
.
set_instead_of_inc
x
,
vals
,
idxs
=
node
.
inputs
# TODO: Add explicit expand_dims in make_node so we don't need to worry about this here
broadcast
=
vals
.
type
.
ndim
<
x
.
type
.
ndim
or
vals
.
type
.
broadcastable
[
0
]
if
set_instead_of_inc
:
if
broadcast
:
@numba_njit
(
boundscheck
=
True
)
def
advancedincsubtensor1_inplace
(
x
,
val
,
idxs
):
if
val
.
ndim
==
x
.
ndim
:
core_val
=
val
[
0
]
elif
val
.
ndim
==
0
:
# Workaround for https://github.com/numba/numba/issues/9573
core_val
=
val
.
item
()
else
:
core_val
=
val
for
idx
in
idxs
:
x
[
idx
]
=
core_val
return
x
return
x
else
:
@numba_njit
(
boundscheck
=
True
)
@numba_njit
(
boundscheck
=
True
)
def
advancedincsubtensor1_inplace_inc
(
x
,
vals
,
idxs
):
def
advancedincsubtensor1_inplace
(
x
,
vals
,
idxs
):
if
not
len
(
idxs
)
==
len
(
vals
):
raise
ValueError
(
"The number of indices and values must match."
)
for
idx
,
val
in
zip
(
idxs
,
vals
):
for
idx
,
val
in
zip
(
idxs
,
vals
):
x
[
idx
]
+
=
val
x
[
idx
]
=
val
return
x
return
x
else
:
if
broadcast
:
@numba_njit
(
boundscheck
=
True
)
def
advancedincsubtensor1_inplace
(
x
,
val
,
idxs
):
if
val
.
ndim
==
x
.
ndim
:
core_val
=
val
[
0
]
elif
val
.
ndim
==
0
:
# Workaround for https://github.com/numba/numba/issues/9573
core_val
=
val
.
item
()
else
:
core_val
=
val
for
idx
in
idxs
:
x
[
idx
]
+=
core_val
return
x
@numba_funcify.register
(
AdvancedIncSubtensor1
)
def
numba_funcify_AdvancedIncSubtensor1
(
op
,
node
,
**
kwargs
):
inplace
=
op
.
inplace
set_instead_of_inc
=
op
.
set_instead_of_inc
if
set_instead_of_inc
:
advancedincsubtensor1_inplace
=
global_numba_func
(
advancedincsubtensor1_inplace_set
)
else
:
else
:
advancedincsubtensor1_inplace
=
global_numba_func
(
advancedincsubtensor1_inplace_inc
@numba_njit
(
boundscheck
=
True
)
)
def
advancedincsubtensor1_inplace
(
x
,
vals
,
idxs
):
if
not
len
(
idxs
)
==
len
(
vals
):
raise
ValueError
(
"The number of indices and values must match."
)
for
idx
,
val
in
zip
(
idxs
,
vals
):
x
[
idx
]
+=
val
return
x
if
inplace
:
if
inplace
:
return
global_numba_func
(
advancedincsubtensor1_inplace
)
return
advancedincsubtensor1_inplace
else
:
else
:
@numba_njit
@numba_njit
...
...
tests/link/numba/test_basic.py
浏览文件 @
5f374dbd
...
@@ -406,6 +406,7 @@ def test_Subtensor(x, indices):
...
@@ -406,6 +406,7 @@ def test_Subtensor(x, indices):
"x, indices"
,
"x, indices"
,
[
[
(
pt
.
as_tensor
(
np
.
arange
(
3
*
4
*
5
)
.
reshape
((
3
,
4
,
5
))),
([
1
,
2
],)),
(
pt
.
as_tensor
(
np
.
arange
(
3
*
4
*
5
)
.
reshape
((
3
,
4
,
5
))),
([
1
,
2
],)),
(
pt
.
as_tensor
(
np
.
arange
(
3
*
4
*
5
)
.
reshape
((
3
,
4
,
5
))),
([
1
],)),
],
],
)
)
def
test_AdvancedSubtensor1
(
x
,
indices
):
def
test_AdvancedSubtensor1
(
x
,
indices
):
...
@@ -498,6 +499,27 @@ def test_IncSubtensor(x, y, indices):
...
@@ -498,6 +499,27 @@ def test_IncSubtensor(x, y, indices):
pt
.
as_tensor
(
rng
.
poisson
(
size
=
(
2
,
4
,
5
))),
pt
.
as_tensor
(
rng
.
poisson
(
size
=
(
2
,
4
,
5
))),
([
1
,
1
],),
([
1
,
1
],),
),
),
# Broadcasting values
(
pt
.
as_tensor
(
np
.
arange
(
3
*
4
*
5
)
.
reshape
((
3
,
4
,
5
))),
pt
.
as_tensor
(
rng
.
poisson
(
size
=
(
1
,
4
,
5
))),
([
0
,
2
,
0
],),
),
(
pt
.
as_tensor
(
np
.
arange
(
3
*
4
*
5
)
.
reshape
((
3
,
4
,
5
))),
pt
.
as_tensor
(
rng
.
poisson
(
size
=
(
5
,))),
([
0
,
2
],),
),
(
pt
.
as_tensor
(
np
.
arange
(
3
*
4
*
5
)
.
reshape
((
3
,
4
,
5
))),
pt
.
as_tensor
(
rng
.
poisson
(
size
=
())),
([
2
,
0
],),
),
(
pt
.
as_tensor
(
np
.
arange
(
5
)),
pt
.
as_tensor
(
rng
.
poisson
(
size
=
())),
([
2
,
0
],),
),
],
],
)
)
def
test_AdvancedIncSubtensor1
(
x
,
y
,
indices
):
def
test_AdvancedIncSubtensor1
(
x
,
y
,
indices
):
...
@@ -511,11 +533,21 @@ def test_AdvancedIncSubtensor1(x, y, indices):
...
@@ -511,11 +533,21 @@ def test_AdvancedIncSubtensor1(x, y, indices):
out_fg
=
FunctionGraph
([],
[
out_pt
])
out_fg
=
FunctionGraph
([],
[
out_pt
])
compare_numba_and_py
(
out_fg
,
[])
compare_numba_and_py
(
out_fg
,
[])
# With symbolic inputs
x_pt
=
x
.
type
()
x_pt
=
x
.
type
()
out_pt
=
pt_subtensor
.
AdvancedIncSubtensor1
(
inplace
=
True
)(
x_pt
,
y
,
*
indices
)
y_pt
=
y
.
type
()
out_pt
=
pt_subtensor
.
AdvancedIncSubtensor1
(
inplace
=
True
)(
x_pt
,
y_pt
,
*
indices
)
assert
isinstance
(
out_pt
.
owner
.
op
,
pt_subtensor
.
AdvancedIncSubtensor1
)
assert
isinstance
(
out_pt
.
owner
.
op
,
pt_subtensor
.
AdvancedIncSubtensor1
)
out_fg
=
FunctionGraph
([
x_pt
],
[
out_pt
])
out_fg
=
FunctionGraph
([
x_pt
,
y_pt
],
[
out_pt
])
compare_numba_and_py
(
out_fg
,
[
x
.
data
])
compare_numba_and_py
(
out_fg
,
[
x
.
data
,
y
.
data
])
out_pt
=
pt_subtensor
.
AdvancedIncSubtensor1
(
set_instead_of_inc
=
True
,
inplace
=
True
)(
x_pt
,
y_pt
,
*
indices
)
assert
isinstance
(
out_pt
.
owner
.
op
,
pt_subtensor
.
AdvancedIncSubtensor1
)
out_fg
=
FunctionGraph
([
x_pt
,
y_pt
],
[
out_pt
])
compare_numba_and_py
(
out_fg
,
[
x
.
data
,
y
.
data
])
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论