Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6de31513
提交
6de31513
authored
10月 12, 2024
作者:
Ch0ronomato
提交者:
Ricardo Vieira
11月 19, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve torch elemwise operator
上级
0ba554b3
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
45 行增加
和
0 行删除
+45
-0
elemwise.py
pytensor/link/pytorch/dispatch/elemwise.py
+12
-0
test_elemwise.py
tests/link/pytorch/test_elemwise.py
+33
-0
没有找到文件。
pytensor/link/pytorch/dispatch/elemwise.py
浏览文件 @
6de31513
...
@@ -11,9 +11,21 @@ def pytorch_funcify_Elemwise(op, node, **kwargs):
...
@@ -11,9 +11,21 @@ def pytorch_funcify_Elemwise(op, node, **kwargs):
scalar_op
=
op
.
scalar_op
scalar_op
=
op
.
scalar_op
base_fn
=
pytorch_funcify
(
scalar_op
,
node
=
node
,
**
kwargs
)
base_fn
=
pytorch_funcify
(
scalar_op
,
node
=
node
,
**
kwargs
)
if
hasattr
(
scalar_op
,
"nfunc_spec"
)
and
hasattr
(
torch
,
scalar_op
.
nfunc_spec
[
0
]):
# torch can handle this scalar
# broadcast, we'll let it.
def
elemwise_fn
(
*
inputs
):
def
elemwise_fn
(
*
inputs
):
Elemwise
.
_check_runtime_broadcast
(
node
,
inputs
)
Elemwise
.
_check_runtime_broadcast
(
node
,
inputs
)
return
base_fn
(
*
inputs
)
return
base_fn
(
*
inputs
)
else
:
def
elemwise_fn
(
*
inputs
):
Elemwise
.
_check_runtime_broadcast
(
node
,
inputs
)
broadcast_inputs
=
torch
.
broadcast_tensors
(
*
inputs
)
ufunc
=
base_fn
for
_
in
range
(
broadcast_inputs
[
0
]
.
dim
()):
ufunc
=
torch
.
vmap
(
ufunc
)
return
ufunc
(
*
broadcast_inputs
)
return
elemwise_fn
return
elemwise_fn
...
...
tests/link/pytorch/test_elemwise.py
浏览文件 @
6de31513
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
import
pytensor
import
pytensor.tensor
as
pt
import
pytensor.tensor
as
pt
import
pytensor.tensor.math
as
ptm
import
pytensor.tensor.math
as
ptm
from
pytensor.configdefaults
import
config
from
pytensor.configdefaults
import
config
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.scalar.basic
import
ScalarOp
,
get_scalar_type
from
pytensor.tensor.elemwise
import
Elemwise
from
pytensor.tensor.special
import
SoftmaxGrad
,
log_softmax
,
softmax
from
pytensor.tensor.special
import
SoftmaxGrad
,
log_softmax
,
softmax
from
pytensor.tensor.type
import
matrix
,
tensor
,
tensor3
,
vector
from
pytensor.tensor.type
import
matrix
,
tensor
,
tensor3
,
vector
from
tests.link.pytorch.test_basic
import
compare_pytorch_and_py
from
tests.link.pytorch.test_basic
import
compare_pytorch_and_py
...
@@ -150,3 +153,33 @@ def test_cast():
...
@@ -150,3 +153,33 @@ def test_cast():
fgraph
,
[
np
.
arange
(
6
,
dtype
=
"float32"
)
.
reshape
(
2
,
3
)]
fgraph
,
[
np
.
arange
(
6
,
dtype
=
"float32"
)
.
reshape
(
2
,
3
)]
)
)
assert
res
.
dtype
==
torch
.
int32
assert
res
.
dtype
==
torch
.
int32
def
test_vmap_elemwise
():
from
pytensor.link.pytorch.dispatch.basic
import
pytorch_funcify
class
TestOp
(
ScalarOp
):
def
__init__
(
self
):
super
()
.
__init__
(
output_types_preference
=
lambda
*
_
:
[
get_scalar_type
(
"float32"
)]
)
self
.
call_shapes
=
[]
self
.
nin
=
1
def
perform
(
self
,
*
_
):
raise
RuntimeError
(
"In perform"
)
@pytorch_funcify.register
(
TestOp
)
def
relu
(
op
,
node
,
**
kwargs
):
def
relu
(
row
):
op
.
call_shapes
.
append
(
row
.
size
())
return
torch
.
max
(
torch
.
zeros_like
(
row
),
row
)
return
relu
x
=
matrix
(
"x"
,
shape
=
(
2
,
3
))
op
=
TestOp
()
f
=
pytensor
.
function
([
x
],
Elemwise
(
op
)(
x
),
mode
=
"PYTORCH"
)
vals
=
torch
.
zeros
(
2
,
3
)
.
normal_
()
np
.
testing
.
assert_allclose
(
f
(
vals
),
torch
.
relu
(
vals
))
assert
op
.
call_shapes
==
[
torch
.
Size
([])],
op
.
call_shapes
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论