Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
acd23ab6
提交
acd23ab6
authored
4月 13, 2021
作者:
kc611
提交者:
Brandon T. Willard
4月 17, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Generalize Numba conversion of Scalar Ops
上级
aff7183b
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
33 行增加
和
27 行删除
+33
-27
dispatch.py
aesara/link/numba/dispatch.py
+10
-16
test_numba.py
tests/link/test_numba.py
+23
-11
没有找到文件。
aesara/link/numba/dispatch.py
浏览文件 @
acd23ab6
...
@@ -4,7 +4,7 @@ import numba
...
@@ -4,7 +4,7 @@ import numba
from
aesara.graph.fg
import
FunctionGraph
from
aesara.graph.fg
import
FunctionGraph
from
aesara.link.utils
import
fgraph_to_python
from
aesara.link.utils
import
fgraph_to_python
from
aesara.scalar.basic
import
Add
,
Composite
,
Mul
from
aesara.scalar.basic
import
Composite
,
ScalarOp
from
aesara.tensor.elemwise
import
Elemwise
from
aesara.tensor.elemwise
import
Elemwise
...
@@ -41,26 +41,20 @@ def numba_funcify_FunctionGraph(
...
@@ -41,26 +41,20 @@ def numba_funcify_FunctionGraph(
)
)
# TODO: Generalize Add and Mul
@numba_funcify.register
(
ScalarOp
)
@numba_funcify.register
(
Add
)
def
numba_funcify_ScalarOp
(
op
,
**
kwargs
):
def
numba_funcify_ScalarAdd
(
op
,
**
kwargs
):
import
numpy
as
np
@numba.njit
def
add
(
x
,
y
):
result
=
0
result
=
x
+
y
return
result
return
add
numpy_func
=
getattr
(
np
,
op
.
nfunc_spec
[
0
])
@numba_funcify.register
(
Mul
)
def
numba_funcify_ScalarMul
(
op
,
**
kwargs
):
@numba.njit
@numba.njit
def
mul
(
x
,
y
,
z
):
def
scalar_func
(
*
args
):
result
=
x
*
y
*
z
result
=
args
[
0
]
for
arg
in
args
[
1
:]:
result
=
numpy_func
(
arg
,
result
)
return
result
return
result
return
mul
return
scalar_func
@numba_funcify.register
(
Elemwise
)
@numba_funcify.register
(
Elemwise
)
...
...
tests/link/test_numba.py
浏览文件 @
acd23ab6
import
numpy
as
np
import
numpy
as
np
import
aesara
import
aesara
import
aesara.scalar.basic
as
aes
import
aesara.tensor
as
aet
import
aesara.tensor
as
aet
from
aesara.compile.mode
import
Mode
from
aesara.compile.mode
import
Mode
from
aesara.graph.optdb
import
Query
from
aesara.graph.optdb
import
Query
...
@@ -15,7 +16,7 @@ numba_mode = Mode(NumbaLinker(), opts)
...
@@ -15,7 +16,7 @@ numba_mode = Mode(NumbaLinker(), opts)
py_mode
=
Mode
(
"py"
,
opts
)
py_mode
=
Mode
(
"py"
,
opts
)
def
test_
c
omposite
():
def
test_
C
omposite
():
y
=
aet
.
vector
(
"y"
)
y
=
aet
.
vector
(
"y"
)
x
=
aet
.
vector
(
"x"
)
x
=
aet
.
vector
(
"x"
)
...
@@ -24,6 +25,10 @@ def test_composite():
...
@@ -24,6 +25,10 @@ def test_composite():
func
=
aesara
.
function
([
x
,
y
],
[
z
],
mode
=
py_mode
)
func
=
aesara
.
function
([
x
,
y
],
[
z
],
mode
=
py_mode
)
numba_fn
=
aesara
.
function
([
x
,
y
],
[
z
],
mode
=
numba_mode
)
numba_fn
=
aesara
.
function
([
x
,
y
],
[
z
],
mode
=
numba_mode
)
# Make sure the graph had a `Composite` `Op` in it
composite_op
=
numba_fn
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
.
scalar_op
assert
isinstance
(
composite_op
,
aes
.
Composite
)
x_val
=
np
.
random
.
randn
(
1000
)
x_val
=
np
.
random
.
randn
(
1000
)
y_val
=
np
.
random
.
randn
(
1000
)
y_val
=
np
.
random
.
randn
(
1000
)
...
@@ -32,18 +37,25 @@ def test_composite():
...
@@ -32,18 +37,25 @@ def test_composite():
assert
np
.
array_equal
(
res
,
numba_res
)
assert
np
.
array_equal
(
res
,
numba_res
)
# y1 = aet.vector("y1")
y1
=
aet
.
vector
(
"y1"
)
# x1 = aet.vector("x1")
x1
=
aet
.
vector
(
"x1"
)
z
=
(
x
+
y
)
*
(
x1
+
y1
)
*
y
# z = (x + y) * (x1 + y1) * y
x1_val
=
np
.
random
.
randn
(
1000
)
y1_val
=
np
.
random
.
randn
(
1000
)
# x1_val = np.random.randn(1000
)
func
=
aesara
.
function
([
x
,
y
,
x1
,
y1
],
[
z
],
mode
=
py_mode
)
# y1_val = np.random.randn(1000
)
numba_fn
=
aesara
.
function
([
x
,
y
,
x1
,
y1
],
[
z
],
mode
=
numba_mode
)
# func = aesara.function([x, y, x1, y1], [z], mode=mode)
composite_op
=
numba_fn
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
.
scalar_op
# numba_fn = compile_graph(func.maker.fgraph, debug=Tru
e)
assert
isinstance
(
composite_op
,
aes
.
Composit
e
)
# res = func(x_val, y_val, x1_val, y1_val) # Answer from python mode compilation of FunctionGraph
res
=
func
(
# numba_res = numba_fn(x_val, y_val,x1_val,y1_val) # Answer from Numba converted FunctionGraph
x_val
,
y_val
,
x1_val
,
y1_val
)
# Answer from python mode compilation of FunctionGraph
numba_res
=
numba_fn
(
x_val
,
y_val
,
x1_val
,
y1_val
)
# Answer from Numba converted FunctionGraph
#
assert np.array_equal(res, numba_res)
assert
np
.
array_equal
(
res
,
numba_res
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论