Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ba4fcbe3
Unverified
提交
ba4fcbe3
authored
9月 17, 2024
作者:
Ian Schweer
提交者:
GitHub
9月 17, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement OpFromGraph in PyTorch backend (#956)
Co-authored-by:
Ian Schweer
<
ischweer@riotgames.com
>
Co-authored-by:
Ricardo Vieira
<
ricardo.vieira1994@gmail.com
>
上级
3e55a209
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
35 行增加
和
1 行删除
+35
-1
__init__.py
pytensor/compile/__init__.py
+1
-0
basic.py
pytensor/link/pytorch/dispatch/basic.py
+16
-0
test_basic.py
tests/link/pytorch/test_basic.py
+18
-1
没有找到文件。
pytensor/compile/__init__.py
浏览文件 @
ba4fcbe3
...
...
@@ -30,6 +30,7 @@ from pytensor.compile.mode import (
OPT_O3
,
OPT_STABILIZE
,
OPT_UNSAFE
,
PYTORCH
,
AddDestroyHandler
,
AddFeatureOptimizer
,
Mode
,
...
...
pytensor/link/pytorch/dispatch/basic.py
浏览文件 @
ba4fcbe3
...
...
@@ -3,7 +3,10 @@ from types import NoneType
import
numpy
as
np
import
torch
import
torch.compiler
from
pytensor.compile
import
PYTORCH
from
pytensor.compile.builders
import
OpFromGraph
from
pytensor.compile.ops
import
DeepCopyOp
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.link.utils
import
fgraph_to_python
...
...
@@ -150,6 +153,19 @@ def pytorch_funcify_MakeVector(op, **kwargs):
return
makevector
@pytorch_funcify.register
(
OpFromGraph
)
def
pytorch_funcify_OpFromGraph
(
op
,
node
,
**
kwargs
):
kwargs
.
pop
(
"storage_map"
,
None
)
# Apply inner rewrites
PYTORCH
.
optimizer
(
op
.
fgraph
)
fgraph_fn
=
pytorch_funcify
(
op
.
fgraph
,
**
kwargs
,
squeeze_output
=
True
)
# Disable one step inlining to prevent torch from trying to import local functions
# defined in `pytorch_funcify`
return
torch
.
compiler
.
disable
(
fgraph_fn
,
recursive
=
False
)
@pytorch_funcify.register
(
TensorFromScalar
)
def
pytorch_funcify_TensorFromScalar
(
op
,
**
kwargs
):
def
tensorfromscalar
(
x
):
...
...
tests/link/pytorch/test_basic.py
浏览文件 @
ba4fcbe3
...
...
@@ -5,6 +5,7 @@ import numpy as np
import
pytest
import
pytensor.tensor.basic
as
ptb
from
pytensor.compile.builders
import
OpFromGraph
from
pytensor.compile.function
import
function
from
pytensor.compile.mode
import
get_mode
from
pytensor.compile.sharedvalue
import
SharedVariable
,
shared
...
...
@@ -14,7 +15,7 @@ from pytensor.graph.fg import FunctionGraph
from
pytensor.graph.op
import
Op
from
pytensor.raise_op
import
CheckAndRaise
from
pytensor.tensor
import
alloc
,
arange
,
as_tensor
,
empty
,
eye
from
pytensor.tensor.type
import
matrix
,
scalar
,
vector
from
pytensor.tensor.type
import
matri
ces
,
matri
x
,
scalar
,
vector
torch
=
pytest
.
importorskip
(
"torch"
)
...
...
@@ -301,3 +302,19 @@ def test_pytorch_MakeVector():
x_fg
=
FunctionGraph
([],
[
x
])
compare_pytorch_and_py
(
x_fg
,
[])
def
test_pytorch_OpFromGraph
():
x
,
y
,
z
=
matrices
(
"xyz"
)
ofg_1
=
OpFromGraph
([
x
,
y
],
[
x
+
y
])
ofg_2
=
OpFromGraph
([
x
,
y
],
[
x
*
y
,
x
-
y
])
o1
,
o2
=
ofg_2
(
y
,
z
)
out
=
ofg_1
(
x
,
o1
)
+
o2
xv
=
np
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
np
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
np
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
f
=
FunctionGraph
([
x
,
y
,
z
],
[
out
])
compare_pytorch_and_py
(
f
,
[
xv
,
yv
,
zv
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论