Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
27c21cd6
提交
27c21cd6
authored
10月 14, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
10月 16, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Test numba slice boxing and fix representation of None stop with negative step
上级
688d6883
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
63 行增加
和
12 行删除
+63
-12
subtensor.py
pytensor/link/numba/dispatch/subtensor.py
+22
-12
test_subtensor.py
tests/link/numba/test_subtensor.py
+41
-0
没有找到文件。
pytensor/link/numba/dispatch/subtensor.py
浏览文件 @
27c21cd6
...
@@ -46,33 +46,43 @@ def enable_slice_boxing():
...
@@ -46,33 +46,43 @@ def enable_slice_boxing():
"""
"""
start
=
c
.
builder
.
extract_value
(
val
,
0
)
start
=
c
.
builder
.
extract_value
(
val
,
0
)
stop
=
c
.
builder
.
extract_value
(
val
,
1
)
stop
=
c
.
builder
.
extract_value
(
val
,
1
)
step
=
c
.
builder
.
extract_value
(
val
,
2
)
if
typ
.
has_step
else
None
# Numba uses sys.maxsize and -sys.maxsize-1 to represent None
# We want to use None in the Python representation
none_val
=
ir
.
Constant
(
ir
.
IntType
(
64
),
sys
.
maxsize
)
none_val
=
ir
.
Constant
(
ir
.
IntType
(
64
),
sys
.
maxsize
)
neg_none_val
=
ir
.
Constant
(
ir
.
IntType
(
64
),
-
sys
.
maxsize
-
1
)
none_obj
=
c
.
pyapi
.
get_null_object
()
start_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
start
,
none_val
)
start
=
c
.
builder
.
select
(
start
=
c
.
builder
.
select
(
start_is_none
,
c
.
builder
.
icmp_signed
(
"=="
,
start
,
none_val
)
,
c
.
pyapi
.
get_null_object
()
,
none_obj
,
c
.
box
(
types
.
int64
,
start
),
c
.
box
(
types
.
int64
,
start
),
)
)
stop_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
stop
,
none_val
)
# None stop is represented as neg_none_val when step is negative
if
step
is
not
None
:
stop_none_val
=
c
.
builder
.
select
(
c
.
builder
.
icmp_signed
(
">"
,
step
,
ir
.
Constant
(
ir
.
IntType
(
64
),
0
)),
none_val
,
neg_none_val
,
)
else
:
stop_none_val
=
none_val
stop
=
c
.
builder
.
select
(
stop
=
c
.
builder
.
select
(
stop_is_none
,
c
.
builder
.
icmp_signed
(
"=="
,
stop
,
stop_none_val
)
,
c
.
pyapi
.
get_null_object
()
,
none_obj
,
c
.
box
(
types
.
int64
,
stop
),
c
.
box
(
types
.
int64
,
stop
),
)
)
if
typ
.
has_step
:
if
step
is
not
None
:
step
=
c
.
builder
.
extract_value
(
val
,
2
)
step_is_none
=
c
.
builder
.
icmp_signed
(
"=="
,
step
,
none_val
)
step
=
c
.
builder
.
select
(
step
=
c
.
builder
.
select
(
step_is_none
,
c
.
builder
.
icmp_signed
(
"=="
,
step
,
none_val
)
,
c
.
pyapi
.
get_null_object
()
,
none_obj
,
c
.
box
(
types
.
int64
,
step
),
c
.
box
(
types
.
int64
,
step
),
)
)
else
:
else
:
step
=
c
.
pyapi
.
get_null_object
()
step
=
none_obj
slice_val
=
slice_new
(
c
.
pyapi
,
start
,
stop
,
step
)
slice_val
=
slice_new
(
c
.
pyapi
,
start
,
stop
,
step
)
...
...
tests/link/numba/test_subtensor.py
浏览文件 @
27c21cd6
...
@@ -3,7 +3,9 @@ import contextlib
...
@@ -3,7 +3,9 @@ import contextlib
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
import
pytensor.scalar
as
ps
import
pytensor.tensor
as
pt
import
pytensor.tensor
as
pt
from
pytensor
import
Mode
,
as_symbolic
from
pytensor.tensor
import
as_tensor
from
pytensor.tensor
import
as_tensor
from
pytensor.tensor.subtensor
import
(
from
pytensor.tensor.subtensor
import
(
AdvancedIncSubtensor
,
AdvancedIncSubtensor
,
...
@@ -24,6 +26,45 @@ from tests.link.numba.test_basic import compare_numba_and_py, numba_mode
...
@@ -24,6 +26,45 @@ from tests.link.numba.test_basic import compare_numba_and_py, numba_mode
rng
=
np
.
random
.
default_rng
(
sum
(
map
(
ord
,
"Numba subtensors"
)))
rng
=
np
.
random
.
default_rng
(
sum
(
map
(
ord
,
"Numba subtensors"
)))
@pytest.mark.parametrize
(
"step"
,
[
None
,
1
,
2
,
-
2
,
"x"
],
ids
=
lambda
x
:
f
"step={x}"
)
@pytest.mark.parametrize
(
"stop"
,
[
None
,
10
,
"x"
],
ids
=
lambda
x
:
f
"stop={x}"
)
@pytest.mark.parametrize
(
"start"
,
[
None
,
0
,
3
,
"x"
],
ids
=
lambda
x
:
f
"start={x}"
)
def
test_slice
(
start
,
stop
,
step
):
x
=
ps
.
int64
(
"x"
)
sym_slice
=
as_symbolic
(
slice
(
x
if
start
==
"x"
else
start
,
x
if
stop
==
"x"
else
stop
,
x
if
step
==
"x"
else
step
,
)
)
no_opt_mode
=
Mode
(
linker
=
"numba"
,
optimizer
=
None
)
evaled_slice
=
sym_slice
.
eval
({
x
:
-
5
},
on_unused_input
=
"ignore"
,
mode
=
no_opt_mode
)
assert
isinstance
(
evaled_slice
,
slice
)
if
start
==
"x"
:
assert
evaled_slice
.
start
==
-
5
elif
start
is
None
and
(
evaled_slice
.
step
is
None
or
evaled_slice
.
step
>
0
):
# Numba can convert to 0 (and sometimes does) in this case
assert
evaled_slice
.
start
in
(
None
,
0
)
else
:
assert
evaled_slice
.
start
==
start
if
stop
==
"x"
:
assert
evaled_slice
.
stop
==
-
5
else
:
assert
evaled_slice
.
stop
==
stop
if
step
==
"x"
:
assert
evaled_slice
.
step
==
-
5
elif
step
is
None
:
# Numba can convert to 1 (and sometimes does) in this case
assert
evaled_slice
.
step
in
(
None
,
1
)
else
:
assert
evaled_slice
.
step
==
step
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"x, indices"
,
"x, indices"
,
[
[
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论