Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
57b344e7
提交
57b344e7
authored
11月 24, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
12月 05, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Numba Scan: make codegen more readable
上级
dca7f5d9
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
30 行增加
和
35 行删除
+30
-35
basic.py
pytensor/link/numba/dispatch/basic.py
+4
-7
scan.py
pytensor/link/numba/dispatch/scan.py
+24
-23
shape.py
pytensor/link/numba/dispatch/shape.py
+2
-5
没有找到文件。
pytensor/link/numba/dispatch/basic.py
浏览文件 @
57b344e7
...
...
@@ -199,13 +199,10 @@ def create_tuple_creator(f, n):
def
create_tuple_string
(
x
):
args
=
", "
.
join
(
x
+
([
""
]
if
len
(
x
)
==
1
else
[]))
return
f
"({args})"
def
create_arg_string
(
x
):
args
=
", "
.
join
(
x
)
return
args
if
len
(
x
)
==
1
:
return
f
"({x[0]},)"
else
:
return
f
"({', '.join(x)})"
@numba.extending.intrinsic
...
...
pytensor/link/numba/dispatch/scan.py
浏览文件 @
57b344e7
...
...
@@ -151,7 +151,7 @@ def numba_funcify_Scan(op: Scan, node, **kwargs):
# Inner-inputs are ordered as follows:
# sequences + mit-mot-inputs + mit-sot-inputs + sit-sot-inputs +
# untraced-sit-sot-inputs + non-sequences.
temp_
scalar
_storage_alloc_stmts
:
list
[
str
]
=
[]
temp_
0d
_storage_alloc_stmts
:
list
[
str
]
=
[]
inner_in_exprs_scalar
:
list
[
str
]
=
[]
inner_in_exprs
:
list
[
str
]
=
[]
...
...
@@ -169,7 +169,7 @@ def numba_funcify_Scan(op: Scan, node, **kwargs):
)
temp_storage
=
f
"{storage_name}_temp_scalar_{tap_offset}"
storage_dtype
=
outer_in_var
.
type
.
numpy_dtype
.
name
temp_
scalar
_storage_alloc_stmts
.
append
(
temp_
0d
_storage_alloc_stmts
.
append
(
f
"{temp_storage} = np.empty((), dtype=np.{storage_dtype})"
)
inner_in_exprs_scalar
.
append
(
...
...
@@ -181,7 +181,7 @@ def numba_funcify_Scan(op: Scan, node, **kwargs):
storage_name
if
tap_offset
is
None
else
idx_to_str
(
storage_name
,
tap_offset
,
size
=
storage_size_var
,
allow_scalar
=
Fals
e
storage_name
,
tap_offset
,
size
=
storage_size_var
,
allow_scalar
=
Tru
e
)
)
inner_in_exprs
.
append
(
indexed_inner_in_str
)
...
...
@@ -366,23 +366,27 @@ def numba_funcify_Scan(op: Scan, node, **kwargs):
curr_nit_sot_position
=
outer_in_nit_sot_names
.
index
(
outer_in_name
)
curr_nit_sot
=
op
.
inner_nitsot_outs
(
op
.
inner_outputs
)[
curr_nit_sot_position
]
storage_shape
=
create_tuple_string
(
[
storage_size_name
]
+
[
"0"
]
*
curr_nit_sot
.
ndim
)
known_static_shape
=
all
(
dim
is
not
None
for
dim
in
curr_nit_sot
.
type
.
shape
)
if
known_static_shape
:
storage_shape
=
create_tuple_string
(
(
storage_size_name
,
*
(
map
(
str
,
curr_nit_sot
.
type
.
shape
)))
)
else
:
storage_shape
=
create_tuple_string
(
(
storage_size_name
,
*
([
"0"
]
*
curr_nit_sot
.
ndim
))
)
storage_dtype
=
curr_nit_sot
.
type
.
numpy_dtype
.
name
storage_alloc_stmts
.
append
(
dedent
(
f
"""
{storage_size_name} =
({outer_in_name})
.item()
{storage_size_name} =
{outer_in_name}
.item()
{storage_name} = np.empty({storage_shape}, dtype=np.{storage_dtype})
"""
)
.
strip
()
)
if
curr_nit_sot
.
type
.
ndim
>
0
:
storage_alloc_stmts
.
append
(
f
"{outer_in_name}_ready = False"
)
if
not
known_static_shape
:
# In this case, we don't know the shape of the output storage
# array until we get some output from the inner-function.
# With the following we add delayed output storage initialization:
...
...
@@ -392,9 +396,8 @@ def numba_funcify_Scan(op: Scan, node, **kwargs):
inner_out_post_processing_stmts
.
append
(
dedent
(
f
"""
if
not {outer_in_name}_ready
:
if
i == 0
:
{storage_name} = np.empty(({storage_size_name},) + np.shape({inner_out_name}), dtype=np.{storage_dtype})
{outer_in_name}_ready = True
"""
)
.
strip
()
)
...
...
@@ -409,10 +412,11 @@ def numba_funcify_Scan(op: Scan, node, **kwargs):
assert
len
(
inner_in_exprs
)
==
len
(
op
.
fgraph
.
inputs
)
inner_scalar_in_args_to_temp_storage
=
"
\n
"
.
join
(
inner_in_exprs_scalar
)
inner_in_args
=
create_arg_string
(
inner_in_exprs
)
# Break inputs in new lines, just for readability of the source code
inner_in_args
=
f
",
\n
{' ' * 12}"
.
join
(
inner_in_exprs
)
inner_outputs
=
create_tuple_string
(
inner_output_names
)
input_storage_block
=
"
\n
"
.
join
(
storage_alloc_stmts
)
input_temp_
scalar_storage_block
=
"
\n
"
.
join
(
temp_scalar
_storage_alloc_stmts
)
input_temp_
0d_storage_block
=
"
\n
"
.
join
(
temp_0d
_storage_alloc_stmts
)
output_storage_post_processing_block
=
"
\n
"
.
join
(
output_storage_post_proc_stmts
)
inner_out_post_processing_block
=
"
\n
"
.
join
(
inner_out_post_processing_stmts
)
...
...
@@ -426,32 +430,29 @@ def scan({", ".join(outer_in_names)}):
{indent(input_storage_block, " " * 4)}
{indent(input_temp_
scalar
_storage_block, " " * 4)}
{indent(input_temp_
0d
_storage_block, " " * 4)}
i = 0
cond = np.array(False)
while i < n_steps and not cond.item():
{indent(inner_scalar_in_args_to_temp_storage, " " * 8)}
{inner_outputs} = scan_inner_func({inner_in_args})
{inner_outputs} = scan_inner_func(
{inner_in_args}
)
{indent(inner_out_post_processing_block, " " * 8)}
{indent(inner_out_to_outer_out_stmts, " " * 8)}
i += 1
{indent(output_storage_post_processing_block, " " * 4)}
return {
create_arg_string
(outer_output_names)}
return {
", ".join
(outer_output_names)}
"""
global_env
=
{
"np"
:
np
,
"scan_inner_func"
:
scan_inner_func
,
}
scan_op_fn
=
compile_numba_function_src
(
scan_op_src
,
"scan"
,
{
**
globals
(),
**
global_env
},
globals
()
|
{
"np"
:
np
,
"scan_inner_func"
:
scan_inner_func
},
)
if
inner_func_cache_key
is
None
:
...
...
pytensor/link/numba/dispatch/shape.py
浏览文件 @
57b344e7
...
...
@@ -4,10 +4,7 @@ import numpy as np
from
numba.np.unsafe
import
ndarray
as
numba_ndarray
from
pytensor.link.numba.dispatch
import
basic
as
numba_basic
from
pytensor.link.numba.dispatch.basic
import
(
create_arg_string
,
register_funcify_default_op_cache_key
,
)
from
pytensor.link.numba.dispatch.basic
import
register_funcify_default_op_cache_key
from
pytensor.link.utils
import
compile_function_src
from
pytensor.tensor
import
NoneConst
from
pytensor.tensor.shape
import
Reshape
,
Shape
,
Shape_i
,
SpecifyShape
...
...
@@ -48,7 +45,7 @@ def numba_funcify_SpecifyShape(op, node, **kwargs):
func
=
dedent
(
f
"""
def specify_shape(x, {
create_arg_string
(shape_input_names)}):
def specify_shape(x, {
", ".join
(shape_input_names)}):
{"; ".join(func_conditions)}
return x
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论