Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c655b028
提交
c655b028
authored
5月 20, 2023
作者:
David Horsley
提交者:
Ricardo Vieira
5月 25, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Split blas Ops and rewrites
Having Ops and rewrites in the same files was causing circular imports.
上级
86cbde57
全部展开
显示空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
112 行增加
和
117 行删除
+112
-117
blas.py
pytensor/tensor/blas.py
+0
-0
blas_c.py
pytensor/tensor/blas_c.py
+0
-72
blas_scipy.py
pytensor/tensor/blas_scipy.py
+1
-43
__init__.py
pytensor/tensor/rewriting/__init__.py
+3
-0
blas.py
pytensor/tensor/rewriting/blas.py
+0
-0
blas_c.py
pytensor/tensor/rewriting/blas_c.py
+70
-0
blas_scipy.py
pytensor/tensor/rewriting/blas_scipy.py
+37
-0
test_blas.py
tests/tensor/test_blas.py
+1
-2
没有找到文件。
pytensor/tensor/blas.py
浏览文件 @
c655b028
差异被折叠。
点击展开。
pytensor/tensor/blas_c.py
浏览文件 @
c655b028
from
pytensor.configdefaults
import
config
from
pytensor.graph.rewriting.basic
import
in2out
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.scalar
import
bool
as
bool_t
from
pytensor.tensor
import
basic
as
at
from
pytensor.tensor.blas
import
(
Gemv
,
Ger
,
blas_header_text
,
blas_header_version
,
blas_optdb
,
gemv_inplace
,
gemv_no_inplace
,
ger
,
ger_destructive
,
ldflags
,
node_rewriter
,
optdb
,
)
...
...
@@ -344,23 +334,6 @@ cger_inplace = CGer(True)
cger_no_inplace
=
CGer
(
False
)
@node_rewriter
([
ger
,
ger_destructive
])
def
use_c_ger
(
fgraph
,
node
):
if
not
config
.
blas__ldflags
:
return
# Only float32 and float64 are supported for now.
if
node
.
op
==
ger
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
CGer
(
False
)(
*
node
.
inputs
)]
if
node
.
op
==
ger_destructive
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
CGer
(
True
)(
*
node
.
inputs
)]
@node_rewriter
([
CGer
(
False
)])
def
make_c_ger_destructive
(
fgraph
,
node
):
if
isinstance
(
node
.
op
,
CGer
)
and
not
node
.
op
.
destructive
:
return
[
cger_inplace
(
*
node
.
inputs
)]
# ##### ####### #######
# GEMV
# ##### ####### #######
...
...
@@ -697,48 +670,3 @@ int main() {
check_force_gemv_init
.
_force_init_beta
=
None
@node_rewriter
([
gemv_inplace
,
gemv_no_inplace
])
def
use_c_gemv
(
fgraph
,
node
):
if
not
config
.
blas__ldflags
:
return
# Only float32 and float64 are supported for now.
if
node
.
op
==
gemv_no_inplace
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
cgemv_no_inplace
(
*
node
.
inputs
)]
if
node
.
op
==
gemv_inplace
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
cgemv_inplace
(
*
node
.
inputs
)]
@node_rewriter
([
CGemv
(
inplace
=
False
)])
def
make_c_gemv_destructive
(
fgraph
,
node
):
if
isinstance
(
node
.
op
,
CGemv
)
and
not
node
.
op
.
inplace
:
inputs
=
list
(
node
.
inputs
)
dest
=
inputs
[
0
]
if
(
dest
.
owner
and
isinstance
(
dest
.
owner
.
op
,
at
.
AllocEmpty
)
and
len
(
fgraph
.
clients
[
dest
])
>
1
):
inputs
[
0
]
=
at
.
AllocEmpty
(
dest
.
dtype
)(
*
dest
.
owner
.
inputs
)
return
[
cgemv_inplace
(
*
inputs
)]
# ##### ####### #######
# Optimizers
# ##### ####### #######
blas_optdb
.
register
(
"use_c_blas"
,
in2out
(
use_c_ger
,
use_c_gemv
),
"fast_run"
,
"c_blas"
,
position
=
20
)
# this matches the InplaceBlasOpt defined in blas.py
optdb
.
register
(
"c_blas_destructive"
,
in2out
(
make_c_ger_destructive
,
make_c_gemv_destructive
,
name
=
"c_blas_destructive"
),
"fast_run"
,
"inplace"
,
"c_blas"
,
position
=
70.0
,
)
pytensor/tensor/blas_scipy.py
浏览文件 @
c655b028
...
...
@@ -4,16 +4,7 @@ Implementations of BLAS Ops based on scipy's BLAS bindings.
import
numpy
as
np
from
pytensor.graph.rewriting.basic
import
in2out
from
pytensor.tensor.blas
import
(
Ger
,
blas_optdb
,
ger
,
ger_destructive
,
have_fblas
,
node_rewriter
,
optdb
,
)
from
pytensor.tensor.blas
import
Ger
,
have_fblas
if
have_fblas
:
...
...
@@ -56,36 +47,3 @@ class ScipyGer(Ger):
scipy_ger_no_inplace
=
ScipyGer
(
False
)
scipy_ger_inplace
=
ScipyGer
(
True
)
@node_rewriter
([
ger
,
ger_destructive
])
def
use_scipy_ger
(
fgraph
,
node
):
if
node
.
op
==
ger
:
return
[
scipy_ger_no_inplace
(
*
node
.
inputs
)]
@node_rewriter
([
scipy_ger_no_inplace
])
def
make_ger_destructive
(
fgraph
,
node
):
if
node
.
op
==
scipy_ger_no_inplace
:
return
[
scipy_ger_inplace
(
*
node
.
inputs
)]
use_scipy_blas
=
in2out
(
use_scipy_ger
)
make_scipy_blas_destructive
=
in2out
(
make_ger_destructive
)
if
have_fblas
:
# scipy_blas is scheduled in the blas_optdb very late, because scipy sortof
# sucks, but it is almost always present.
# C implementations should be scheduled earlier than this, so that they take
# precedence. Once the original Ger is replaced, then these optimizations
# have no effect.
blas_optdb
.
register
(
"scipy_blas"
,
use_scipy_blas
,
"fast_run"
,
position
=
100
)
# this matches the InplaceBlasOpt defined in blas.py
optdb
.
register
(
"make_scipy_blas_destructive"
,
make_scipy_blas_destructive
,
"fast_run"
,
"inplace"
,
position
=
70.0
,
)
pytensor/tensor/rewriting/__init__.py
浏览文件 @
c655b028
import
pytensor.tensor.rewriting.basic
import
pytensor.tensor.rewriting.blas
import
pytensor.tensor.rewriting.blas_c
import
pytensor.tensor.rewriting.blas_scipy
import
pytensor.tensor.rewriting.elemwise
import
pytensor.tensor.rewriting.extra_ops
...
...
pytensor/tensor/rewriting/blas.py
0 → 100644
浏览文件 @
c655b028
差异被折叠。
点击展开。
pytensor/tensor/rewriting/blas_c.py
0 → 100644
浏览文件 @
c655b028
from
pytensor.configdefaults
import
config
from
pytensor.graph.rewriting.basic
import
in2out
from
pytensor.tensor
import
basic
as
at
from
pytensor.tensor.blas
import
gemv_inplace
,
gemv_no_inplace
,
ger
,
ger_destructive
from
pytensor.tensor.blas_c
import
(
CGemv
,
CGer
,
cgemv_inplace
,
cgemv_no_inplace
,
cger_inplace
,
)
from
pytensor.tensor.rewriting.blas
import
blas_optdb
,
node_rewriter
,
optdb
@node_rewriter
([
ger
,
ger_destructive
])
def
use_c_ger
(
fgraph
,
node
):
if
not
config
.
blas__ldflags
:
return
# Only float32 and float64 are supported for now.
if
node
.
op
==
ger
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
CGer
(
False
)(
*
node
.
inputs
)]
if
node
.
op
==
ger_destructive
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
CGer
(
True
)(
*
node
.
inputs
)]
@node_rewriter
([
CGer
(
False
)])
def
make_c_ger_destructive
(
fgraph
,
node
):
if
isinstance
(
node
.
op
,
CGer
)
and
not
node
.
op
.
destructive
:
return
[
cger_inplace
(
*
node
.
inputs
)]
@node_rewriter
([
gemv_inplace
,
gemv_no_inplace
])
def
use_c_gemv
(
fgraph
,
node
):
if
not
config
.
blas__ldflags
:
return
# Only float32 and float64 are supported for now.
if
node
.
op
==
gemv_no_inplace
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
cgemv_no_inplace
(
*
node
.
inputs
)]
if
node
.
op
==
gemv_inplace
and
node
.
outputs
[
0
]
.
dtype
in
(
"float32"
,
"float64"
):
return
[
cgemv_inplace
(
*
node
.
inputs
)]
@node_rewriter
([
CGemv
(
inplace
=
False
)])
def
make_c_gemv_destructive
(
fgraph
,
node
):
if
isinstance
(
node
.
op
,
CGemv
)
and
not
node
.
op
.
inplace
:
inputs
=
list
(
node
.
inputs
)
dest
=
inputs
[
0
]
if
(
dest
.
owner
and
isinstance
(
dest
.
owner
.
op
,
at
.
AllocEmpty
)
and
len
(
fgraph
.
clients
[
dest
])
>
1
):
inputs
[
0
]
=
at
.
AllocEmpty
(
dest
.
dtype
)(
*
dest
.
owner
.
inputs
)
return
[
cgemv_inplace
(
*
inputs
)]
blas_optdb
.
register
(
"use_c_blas"
,
in2out
(
use_c_ger
,
use_c_gemv
),
"fast_run"
,
"c_blas"
,
position
=
20
)
# this matches the InplaceBlasOpt defined in blas.py
optdb
.
register
(
"c_blas_destructive"
,
in2out
(
make_c_ger_destructive
,
make_c_gemv_destructive
,
name
=
"c_blas_destructive"
),
"fast_run"
,
"inplace"
,
"c_blas"
,
position
=
70.0
,
)
pytensor/tensor/rewriting/blas_scipy.py
0 → 100644
浏览文件 @
c655b028
from
pytensor.graph.rewriting.basic
import
in2out
from
pytensor.tensor.blas
import
ger
,
ger_destructive
,
have_fblas
from
pytensor.tensor.blas_scipy
import
scipy_ger_inplace
,
scipy_ger_no_inplace
from
pytensor.tensor.rewriting.blas
import
blas_optdb
,
node_rewriter
,
optdb
@node_rewriter
([
ger
,
ger_destructive
])
def
use_scipy_ger
(
fgraph
,
node
):
if
node
.
op
==
ger
:
return
[
scipy_ger_no_inplace
(
*
node
.
inputs
)]
@node_rewriter
([
scipy_ger_no_inplace
])
def
make_ger_destructive
(
fgraph
,
node
):
if
node
.
op
==
scipy_ger_no_inplace
:
return
[
scipy_ger_inplace
(
*
node
.
inputs
)]
use_scipy_blas
=
in2out
(
use_scipy_ger
)
make_scipy_blas_destructive
=
in2out
(
make_ger_destructive
)
if
have_fblas
:
# scipy_blas is scheduled in the blas_optdb very late, because scipy sortof
# sucks, but it is almost always present.
# C implementations should be scheduled earlier than this, so that they take
# precedence. Once the original Ger is replaced, then these optimizations
# have no effect.
blas_optdb
.
register
(
"scipy_blas"
,
use_scipy_blas
,
"fast_run"
,
position
=
100
)
# this matches the InplaceBlasOpt defined in blas.py
optdb
.
register
(
"make_scipy_blas_destructive"
,
make_scipy_blas_destructive
,
"fast_run"
,
"inplace"
,
position
=
70.0
,
)
tests/tensor/test_blas.py
浏览文件 @
c655b028
...
...
@@ -44,12 +44,11 @@ from pytensor.tensor.blas import (
gemv_no_inplace
,
ger
,
ger_destructive
,
local_dot22_to_dot22scalar
,
local_gemm_to_ger
,
res_is_a
,
)
from
pytensor.tensor.elemwise
import
DimShuffle
from
pytensor.tensor.math
import
Dot
,
dot
,
mean
,
mul
,
neg
,
outer
,
sigmoid
,
sqrt
from
pytensor.tensor.rewriting.blas
import
local_dot22_to_dot22scalar
,
local_gemm_to_ger
from
pytensor.tensor.type
import
(
cmatrix
,
col
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论