Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0a4a35c7
提交
0a4a35c7
authored
7月 22, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
7月 23, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add output_filter method to JITLinker and use it to convert Numba scalars
上级
2c9ee770
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
38 行增加
和
11 行删除
+38
-11
basic.py
aesara/link/basic.py
+10
-11
linker.py
aesara/link/numba/linker.py
+17
-0
test_numba.py
tests/link/test_numba.py
+11
-0
没有找到文件。
aesara/link/basic.py
浏览文件 @
0a4a35c7
...
@@ -12,8 +12,6 @@ from typing import (
...
@@ -12,8 +12,6 @@ from typing import (
Union
,
Union
,
)
)
from
numpy
import
ndarray
from
aesara.configdefaults
import
config
from
aesara.configdefaults
import
config
from
aesara.graph.basic
import
Apply
,
Variable
from
aesara.graph.basic
import
Apply
,
Variable
from
aesara.graph.fg
import
FunctionGraph
from
aesara.graph.fg
import
FunctionGraph
...
@@ -23,6 +21,8 @@ from aesara.utils import difference
...
@@ -23,6 +21,8 @@ from aesara.utils import difference
if
TYPE_CHECKING
:
if
TYPE_CHECKING
:
from
numpy.typing
import
NDArray
from
aesara.compile.profiling
import
ProfileStats
from
aesara.compile.profiling
import
ProfileStats
from
aesara.graph.op
import
(
from
aesara.graph.op
import
(
BasicThunkType
,
BasicThunkType
,
...
@@ -505,7 +505,7 @@ class WrapLinker(Linker):
...
@@ -505,7 +505,7 @@ class WrapLinker(Linker):
def
pre
(
def
pre
(
self
,
self
,
f
:
"WrapLinker"
,
f
:
"WrapLinker"
,
inputs
:
Union
[
List
[
ndarray
],
List
[
Optional
[
float
]]],
inputs
:
Union
[
List
[
"NDArray"
],
List
[
Optional
[
float
]]],
order
:
List
[
Apply
],
order
:
List
[
Apply
],
thunk_groups
:
List
[
Tuple
[
Callable
]],
thunk_groups
:
List
[
Tuple
[
Callable
]],
)
->
None
:
)
->
None
:
...
@@ -609,6 +609,10 @@ class JITLinker(PerformLinker):
...
@@ -609,6 +609,10 @@ class JITLinker(PerformLinker):
def
jit_compile
(
self
,
fn
:
Callable
)
->
Callable
:
def
jit_compile
(
self
,
fn
:
Callable
)
->
Callable
:
"""JIT compile a converted ``FunctionGraph``."""
"""JIT compile a converted ``FunctionGraph``."""
def
output_filter
(
self
,
var
:
Variable
,
out
:
Any
)
->
Any
:
"""Apply a filter to the data output by a JITed function call."""
return
out
def
create_jitable_thunk
(
def
create_jitable_thunk
(
self
,
compute_map
,
order
,
input_storage
,
output_storage
,
storage_map
self
,
compute_map
,
order
,
input_storage
,
output_storage
,
storage_map
):
):
...
@@ -663,14 +667,9 @@ class JITLinker(PerformLinker):
...
@@ -663,14 +667,9 @@ class JITLinker(PerformLinker):
):
):
outputs
=
fgraph_jit
(
*
[
x
[
0
]
for
x
in
thunk_inputs
])
outputs
=
fgraph_jit
(
*
[
x
[
0
]
for
x
in
thunk_inputs
])
for
o_node
,
o_storage
,
o_val
in
zip
(
fgraph
.
outputs
,
thunk_outputs
,
outputs
):
for
o_var
,
o_storage
,
o_val
in
zip
(
fgraph
.
outputs
,
thunk_outputs
,
outputs
):
compute_map
[
o_node
][
0
]
=
True
compute_map
[
o_var
][
0
]
=
True
if
len
(
o_storage
)
>
1
:
o_storage
[
0
]
=
self
.
output_filter
(
o_var
,
o_val
)
assert
len
(
o_storage
)
==
len
(
o_val
)
for
i
,
o_sub_val
in
enumerate
(
o_val
):
o_storage
[
i
]
=
o_sub_val
else
:
o_storage
[
0
]
=
o_val
return
outputs
return
outputs
thunk
.
inputs
=
thunk_inputs
thunk
.
inputs
=
thunk_inputs
...
...
aesara/link/numba/linker.py
浏览文件 @
0a4a35c7
from
typing
import
TYPE_CHECKING
,
Any
import
numpy
as
np
import
aesara
from
aesara.link.basic
import
JITLinker
from
aesara.link.basic
import
JITLinker
if
TYPE_CHECKING
:
from
aesara.graph.basic
import
Variable
class
NumbaLinker
(
JITLinker
):
class
NumbaLinker
(
JITLinker
):
"""A `Linker` that JIT-compiles NumPy-based operations using Numba."""
"""A `Linker` that JIT-compiles NumPy-based operations using Numba."""
def
output_filter
(
self
,
var
:
"Variable"
,
out
:
Any
)
->
Any
:
if
not
isinstance
(
var
,
np
.
ndarray
)
and
isinstance
(
var
.
type
,
aesara
.
tensor
.
TensorType
):
return
np
.
asarray
(
out
,
dtype
=
var
.
type
.
dtype
)
return
out
def
fgraph_convert
(
self
,
fgraph
,
**
kwargs
):
def
fgraph_convert
(
self
,
fgraph
,
**
kwargs
):
from
aesara.link.numba.dispatch
import
numba_funcify
from
aesara.link.numba.dispatch
import
numba_funcify
...
...
tests/link/test_numba.py
浏览文件 @
0a4a35c7
...
@@ -3575,3 +3575,14 @@ def test_config_options_cached():
...
@@ -3575,3 +3575,14 @@ def test_config_options_cached():
aesara_numba_fn
=
function
([
x
],
x
*
2
,
mode
=
numba_mode
)
aesara_numba_fn
=
function
([
x
],
x
*
2
,
mode
=
numba_mode
)
numba_mul_fn
=
aesara_numba_fn
.
vm
.
jit_fn
.
py_func
.
__globals__
[
"mul"
]
numba_mul_fn
=
aesara_numba_fn
.
vm
.
jit_fn
.
py_func
.
__globals__
[
"mul"
]
assert
isinstance
(
numba_mul_fn
.
_dispatcher
.
cache
,
numba
.
core
.
caching
.
NullCache
)
assert
isinstance
(
numba_mul_fn
.
_dispatcher
.
cache
,
numba
.
core
.
caching
.
NullCache
)
def
test_scalar_return_value_conversion
():
r"""Make sure that we convert \"native\" scalars to `ndarray`\s in the graph outputs."""
x
=
at
.
scalar
(
name
=
"x"
)
x_fn
=
function
(
[
x
],
2
*
x
,
mode
=
numba_mode
,
)
assert
isinstance
(
x_fn
(
1.0
),
np
.
ndarray
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论