Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bdf98ca9
提交
bdf98ca9
authored
11月 21, 2022
作者:
Brandon T. Willard
提交者:
Ricardo Vieira
11月 27, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add get_target_language function and remove tests.compile.test_modes
上级
b12cd96a
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
121 行增加
和
60 行删除
+121
-60
mode.py
pytensor/compile/mode.py
+25
-0
test_mode.py
tests/compile/test_mode.py
+96
-2
test_modes.py
tests/compile/test_modes.py
+0
-58
没有找到文件。
pytensor/compile/mode.py
浏览文件 @
bdf98ca9
...
@@ -7,6 +7,8 @@ import logging
...
@@ -7,6 +7,8 @@ import logging
import
warnings
import
warnings
from
typing
import
Optional
,
Tuple
,
Union
from
typing
import
Optional
,
Tuple
,
Union
from
typing_extensions
import
Literal
from
pytensor.compile.function.types
import
Supervisor
from
pytensor.compile.function.types
import
Supervisor
from
pytensor.configdefaults
import
config
from
pytensor.configdefaults
import
config
from
pytensor.graph.destroyhandler
import
DestroyHandler
from
pytensor.graph.destroyhandler
import
DestroyHandler
...
@@ -530,3 +532,26 @@ def register_mode(name, mode):
...
@@ -530,3 +532,26 @@ def register_mode(name, mode):
if
name
in
predefined_modes
:
if
name
in
predefined_modes
:
raise
ValueError
(
f
"Mode name already taken: {name}"
)
raise
ValueError
(
f
"Mode name already taken: {name}"
)
predefined_modes
[
name
]
=
mode
predefined_modes
[
name
]
=
mode
def
get_target_language
(
mode
=
None
)
->
Tuple
[
Literal
[
"py"
,
"c"
,
"numba"
,
"jax"
],
...
]:
"""Get the compilation target language."""
if
mode
is
None
:
mode
=
get_default_mode
()
linker
=
mode
.
linker
if
isinstance
(
linker
,
NumbaLinker
):
return
(
"numba"
,)
if
isinstance
(
linker
,
JAXLinker
):
return
(
"jax"
,)
if
isinstance
(
linker
,
PerformLinker
):
return
(
"py"
,)
if
isinstance
(
linker
,
CLinker
):
return
(
"c"
,)
if
isinstance
(
linker
,
(
VMLinker
,
OpWiseCLinker
)):
return
(
"c"
,
"py"
)
if
config
.
cxx
else
(
"py"
,)
raise
Exception
(
f
"Unsupported Linker: {linker}"
)
tests/compile/test_mode.py
浏览文件 @
bdf98ca9
import
copy
import
pytest
from
pytensor.compile.function
import
function
from
pytensor.compile.function
import
function
from
pytensor.compile.mode
import
AddFeatureOptimizer
,
Mode
from
pytensor.compile.mode
import
(
AddFeatureOptimizer
,
Mode
,
get_default_mode
,
get_target_language
,
)
from
pytensor.configdefaults
import
config
from
pytensor.graph.features
import
NoOutputFromInplace
from
pytensor.graph.features
import
NoOutputFromInplace
from
pytensor.graph.rewriting.db
import
RewriteDatabaseQuery
,
SequenceDB
from
pytensor.graph.rewriting.db
import
RewriteDatabaseQuery
,
SequenceDB
from
pytensor.link.basic
import
LocalLinker
from
pytensor.tensor.math
import
dot
,
tanh
from
pytensor.tensor.math
import
dot
,
tanh
from
pytensor.tensor.type
import
matrix
from
pytensor.tensor.type
import
matrix
,
vector
def
test_Mode_basic
():
def
test_Mode_basic
():
...
@@ -48,3 +59,86 @@ def test_including():
...
@@ -48,3 +59,86 @@ def test_including():
new_mode
=
mode
.
including
(
"fast_compile"
)
new_mode
=
mode
.
including
(
"fast_compile"
)
assert
set
(
new_mode
.
_optimizer
.
include
)
==
{
"merge"
,
"fast_compile"
}
assert
set
(
new_mode
.
_optimizer
.
include
)
==
{
"merge"
,
"fast_compile"
}
class
TestBunchOfModes
:
def
test_modes
(
self
):
# this is a quick test after the LazyLinker branch merge
# to check that all the current modes can still be used.
linker_classes_involved
=
[]
predef_modes
=
[
"FAST_COMPILE"
,
"FAST_RUN"
,
"DEBUG_MODE"
]
# Linkers to use with regular Mode
if
config
.
cxx
:
linkers
=
[
"py"
,
"c|py"
,
"c|py_nogc"
,
"vm"
,
"vm_nogc"
,
"cvm"
,
"cvm_nogc"
]
else
:
linkers
=
[
"py"
,
"c|py"
,
"c|py_nogc"
,
"vm"
,
"vm_nogc"
]
modes
=
predef_modes
+
[
Mode
(
linker
,
"fast_run"
)
for
linker
in
linkers
]
for
mode
in
modes
:
x
=
matrix
()
y
=
vector
()
f
=
function
([
x
,
y
],
x
+
y
,
mode
=
mode
)
# test that it runs something
f
([[
1
,
2
],
[
3
,
4
]],
[
5
,
6
])
linker_classes_involved
.
append
(
f
.
maker
.
mode
.
linker
.
__class__
)
# print 'MODE:', mode, f.maker.mode.linker, 'stop'
# regression check:
# there should be
# - `VMLinker`
# - OpWiseCLinker (FAST_RUN)
# - PerformLinker (FAST_COMPILE)
# - DebugMode's Linker (DEBUG_MODE)
assert
4
==
len
(
set
(
linker_classes_involved
))
class
TestOldModesProblem
:
def
test_modes
(
self
):
# Then, build a mode with the same linker, and a modified optimizer
default_mode
=
get_default_mode
()
modified_mode
=
default_mode
.
including
(
"specialize"
)
# The following line used to fail, with Python 2.4, in July 2012,
# because an fgraph was associated to the default linker
copy
.
deepcopy
(
modified_mode
)
# More straightforward test
linker
=
get_default_mode
()
.
linker
assert
not
hasattr
(
linker
,
"fgraph"
)
or
linker
.
fgraph
is
None
def
test_get_target_language
():
with
config
.
change_flags
(
mode
=
Mode
(
linker
=
"py"
)):
res
=
get_target_language
()
assert
res
==
(
"py"
,)
res
=
get_target_language
(
Mode
(
linker
=
"py"
))
assert
res
==
(
"py"
,)
res
=
get_target_language
(
Mode
(
linker
=
"c"
))
assert
res
==
(
"c"
,)
res
=
get_target_language
(
Mode
(
linker
=
"c|py"
))
assert
res
==
(
"c"
,
"py"
)
res
=
get_target_language
(
Mode
(
linker
=
"vm"
))
assert
res
==
(
"c"
,
"py"
)
with
config
.
change_flags
(
cxx
=
""
):
res
=
get_target_language
(
Mode
(
linker
=
"vm"
))
assert
res
==
(
"py"
,)
res
=
get_target_language
(
Mode
(
linker
=
"jax"
))
assert
res
==
(
"jax"
,)
res
=
get_target_language
(
Mode
(
linker
=
"numba"
))
assert
res
==
(
"numba"
,)
class
MyLinker
(
LocalLinker
):
pass
test_mode
=
Mode
(
linker
=
MyLinker
())
with
pytest
.
raises
(
Exception
):
get_target_language
(
test_mode
)
tests/compile/test_modes.py
deleted
100644 → 0
浏览文件 @
b12cd96a
"""
Test compilation modes
"""
import
copy
from
pytensor.compile.function
import
function
from
pytensor.compile.mode
import
Mode
,
get_default_mode
from
pytensor.configdefaults
import
config
from
pytensor.tensor.type
import
matrix
,
vector
class
TestBunchOfModes
:
def
test_modes
(
self
):
# this is a quick test after the LazyLinker branch merge
# to check that all the current modes can still be used.
linker_classes_involved
=
[]
predef_modes
=
[
"FAST_COMPILE"
,
"FAST_RUN"
,
"DEBUG_MODE"
]
# Linkers to use with regular Mode
if
config
.
cxx
:
linkers
=
[
"py"
,
"c|py"
,
"c|py_nogc"
,
"vm"
,
"vm_nogc"
,
"cvm"
,
"cvm_nogc"
]
else
:
linkers
=
[
"py"
,
"c|py"
,
"c|py_nogc"
,
"vm"
,
"vm_nogc"
]
modes
=
predef_modes
+
[
Mode
(
linker
,
"fast_run"
)
for
linker
in
linkers
]
for
mode
in
modes
:
x
=
matrix
()
y
=
vector
()
f
=
function
([
x
,
y
],
x
+
y
,
mode
=
mode
)
# test that it runs something
f
([[
1
,
2
],
[
3
,
4
]],
[
5
,
6
])
linker_classes_involved
.
append
(
f
.
maker
.
mode
.
linker
.
__class__
)
# print 'MODE:', mode, f.maker.mode.linker, 'stop'
# regression check:
# there should be
# - `VMLinker`
# - OpWiseCLinker (FAST_RUN)
# - PerformLinker (FAST_COMPILE)
# - DebugMode's Linker (DEBUG_MODE)
assert
4
==
len
(
set
(
linker_classes_involved
))
class
TestOldModesProblem
:
def
test_modes
(
self
):
# Then, build a mode with the same linker, and a modified optimizer
default_mode
=
get_default_mode
()
modified_mode
=
default_mode
.
including
(
"specialize"
)
# The following line used to fail, with Python 2.4, in July 2012,
# because an fgraph was associated to the default linker
copy
.
deepcopy
(
modified_mode
)
# More straightforward test
linker
=
get_default_mode
()
.
linker
assert
not
hasattr
(
linker
,
"fgraph"
)
or
linker
.
fgraph
is
None
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论