Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6c6bf08a
提交
6c6bf08a
authored
8月 11, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
8月 12, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use MD5 hashing for ExternalCOp versioning
上级
c78a626a
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
94 行增加
和
1 行删除
+94
-1
op.py
aesara/link/c/op.py
+2
-1
test_op.py
tests/link/c/test_op.py
+92
-0
没有找到文件。
aesara/link/c/op.py
浏览文件 @
6c6bf08a
...
@@ -27,6 +27,7 @@ from aesara.graph.type import HasDataType
...
@@ -27,6 +27,7 @@ from aesara.graph.type import HasDataType
from
aesara.graph.utils
import
MethodNotDefined
from
aesara.graph.utils
import
MethodNotDefined
from
aesara.link.c.interface
import
CLinkerOp
from
aesara.link.c.interface
import
CLinkerOp
from
aesara.link.c.params_type
import
ParamsType
from
aesara.link.c.params_type
import
ParamsType
from
aesara.utils
import
hash_from_code
if
TYPE_CHECKING
:
if
TYPE_CHECKING
:
...
@@ -450,7 +451,7 @@ class ExternalCOp(COp):
...
@@ -450,7 +451,7 @@ class ExternalCOp(COp):
return
params
return
params
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
version
=
(
hash
(
tuple
(
self
.
func_codes
)),)
version
=
(
hash
_from_code
(
"
\n
"
.
join
(
self
.
func_codes
)),)
if
self
.
params_type
is
not
None
:
if
self
.
params_type
is
not
None
:
version
+=
(
self
.
params_type
.
c_code_cache_version
(),)
version
+=
(
self
.
params_type
.
c_code_cache_version
(),)
return
version
return
version
...
...
tests/link/c/test_op.py
浏览文件 @
6c6bf08a
import
os
import
subprocess
import
sys
import
tempfile
from
pathlib
import
Path
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
...
@@ -9,6 +15,52 @@ from aesara.graph.utils import MethodNotDefined
...
@@ -9,6 +15,52 @@ from aesara.graph.utils import MethodNotDefined
from
aesara.link.c.op
import
COp
from
aesara.link.c.op
import
COp
test_dir
=
Path
(
__file__
)
.
parent
.
absolute
()
externalcop_test_code
=
f
"""
from aesara import tensor as at
from aesara.graph.basic import Apply
from aesara.link.c.params_type import ParamsType
from aesara.link.c.op import ExternalCOp
from aesara.scalar import ScalarType
from aesara.link.c.type import Generic
from aesara.tensor.type import TensorType
tensor_type_0d = TensorType("float64", tuple())
scalar_type = ScalarType("float64")
generic_type = Generic()
class QuadraticCOpFunc(ExternalCOp):
__props__ = ("a", "b", "c")
params_type = ParamsType(a=tensor_type_0d, b=scalar_type, c=generic_type)
def __init__(self, a, b, c):
super().__init__(
"{test_dir}/c_code/test_quadratic_function.c", "APPLY_SPECIFIC(compute_quadratic)"
)
self.a = a
self.b = b
self.c = c
def make_node(self, x):
x = at.as_tensor_variable(x)
return Apply(self, [x], [x.type()])
def perform(self, node, inputs, output_storage, coefficients):
x = inputs[0]
y = output_storage[0]
y[0] = coefficients.a * (x**2) + coefficients.b * x + coefficients.c
if __name__ == "__main__":
qcop = QuadraticCOpFunc(1, 2, 3)
print(qcop.c_code_cache_version())
print("__success__")
"""
class
StructOp
(
COp
):
class
StructOp
(
COp
):
__props__
=
()
__props__
=
()
...
@@ -141,3 +193,43 @@ class TestMakeThunk:
...
@@ -141,3 +193,43 @@ class TestMakeThunk:
else
:
else
:
with
pytest
.
raises
((
NotImplementedError
,
MethodNotDefined
)):
with
pytest
.
raises
((
NotImplementedError
,
MethodNotDefined
)):
thunk
()
thunk
()
def
get_hash
(
modname
,
seed
=
None
):
"""From https://hg.python.org/cpython/file/5e8fa1b13516/Lib/test/test_hash.py#l145"""
env
=
os
.
environ
.
copy
()
if
seed
is
not
None
:
env
[
"PYTHONHASHSEED"
]
=
str
(
seed
)
else
:
env
.
pop
(
"PYTHONHASHSEED"
,
None
)
cmd_line
=
[
sys
.
executable
,
modname
]
p
=
subprocess
.
Popen
(
cmd_line
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
,
env
=
env
,
)
out
,
err
=
p
.
communicate
()
return
out
,
err
def
test_ExternalCOp_c_code_cache_version
():
"""Make sure the C cache versions produced by `ExternalCOp` don't depend on `hash` seeding."""
with
tempfile
.
NamedTemporaryFile
(
dir
=
"."
,
suffix
=
".py"
)
as
tmp
:
tmp
.
write
(
externalcop_test_code
.
encode
())
tmp
.
seek
(
0
)
# modname = os.path.splitext(tmp.name)[0]
modname
=
tmp
.
name
out_1
,
err
=
get_hash
(
modname
,
seed
=
428
)
assert
err
is
None
out_2
,
err
=
get_hash
(
modname
,
seed
=
3849
)
assert
err
is
None
hash_1
,
msg
,
_
=
out_1
.
decode
()
.
split
(
"
\n
"
)
assert
msg
==
"__success__"
hash_2
,
msg
,
_
=
out_2
.
decode
()
.
split
(
"
\n
"
)
assert
msg
==
"__success__"
assert
hash_1
==
hash_2
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论