Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
17fa8b13
Unverified
提交
17fa8b13
authored
6月 28, 2024
作者:
Habeeb Shopeju
提交者:
GitHub
6月 28, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
PyTorch Softmax Ops (#846)
Co-authored-by:
HarshvirSandhu
<
harshvir2173@gmail.com
>
上级
f3d2ede9
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
100 行增加
和
1 行删除
+100
-1
environment.yml
environment.yml
+1
-1
elemwise.py
pytensor/link/pytorch/dispatch/elemwise.py
+50
-0
test_elemwise.py
tests/link/pytorch/test_elemwise.py
+49
-0
没有找到文件。
environment.yml
浏览文件 @
17fa8b13
...
@@ -9,7 +9,7 @@ channels:
...
@@ -9,7 +9,7 @@ channels:
dependencies
:
dependencies
:
-
python>=3.10
-
python>=3.10
-
compilers
-
compilers
-
numpy>=1.17.0
-
numpy>=1.17.0
,<2
-
scipy>=0.14,<1.14.0
-
scipy>=0.14,<1.14.0
-
filelock
-
filelock
-
etuples
-
etuples
...
...
pytensor/link/pytorch/dispatch/elemwise.py
浏览文件 @
17fa8b13
...
@@ -2,6 +2,7 @@ import torch
...
@@ -2,6 +2,7 @@ import torch
from
pytensor.link.pytorch.dispatch.basic
import
pytorch_funcify
from
pytensor.link.pytorch.dispatch.basic
import
pytorch_funcify
from
pytensor.tensor.elemwise
import
DimShuffle
,
Elemwise
from
pytensor.tensor.elemwise
import
DimShuffle
,
Elemwise
from
pytensor.tensor.special
import
LogSoftmax
,
Softmax
,
SoftmaxGrad
@pytorch_funcify.register
(
Elemwise
)
@pytorch_funcify.register
(
Elemwise
)
...
@@ -34,3 +35,52 @@ def pytorch_funcify_DimShuffle(op, **kwargs):
...
@@ -34,3 +35,52 @@ def pytorch_funcify_DimShuffle(op, **kwargs):
return
res
return
res
return
dimshuffle
return
dimshuffle
@pytorch_funcify.register
(
Softmax
)
def
pytorch_funcify_Softmax
(
op
,
**
kwargs
):
axis
=
op
.
axis
dtype
=
kwargs
[
"node"
]
.
inputs
[
0
]
.
dtype
if
not
dtype
.
startswith
(
"float"
):
raise
NotImplementedError
(
"Pytorch Softmax is not currently implemented for non-float types."
)
def
softmax
(
x
):
if
axis
is
not
None
:
return
torch
.
softmax
(
x
,
dim
=
axis
)
else
:
return
torch
.
softmax
(
x
.
ravel
(),
dim
=
0
)
.
reshape
(
x
.
shape
)
return
softmax
@pytorch_funcify.register
(
LogSoftmax
)
def
pytorch_funcify_LogSoftmax
(
op
,
**
kwargs
):
axis
=
op
.
axis
dtype
=
kwargs
[
"node"
]
.
inputs
[
0
]
.
dtype
if
not
dtype
.
startswith
(
"float"
):
raise
NotImplementedError
(
"Pytorch LogSoftmax is not currently implemented for non-float types."
)
def
log_softmax
(
x
):
if
axis
is
not
None
:
return
torch
.
log_softmax
(
x
,
dim
=
axis
)
else
:
return
torch
.
log_softmax
(
x
.
ravel
(),
dim
=
0
)
.
reshape
(
x
.
shape
)
return
log_softmax
@pytorch_funcify.register
(
SoftmaxGrad
)
def
jax_funcify_SoftmaxGrad
(
op
,
**
kwargs
):
axis
=
op
.
axis
def
softmax_grad
(
dy
,
sm
):
dy_times_sm
=
dy
*
sm
return
dy_times_sm
-
torch
.
sum
(
dy_times_sm
,
dim
=
axis
,
keepdim
=
True
)
*
sm
return
softmax_grad
tests/link/pytorch/test_elemwise.py
浏览文件 @
17fa8b13
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytensor.tensor
as
pt
import
pytensor.tensor
as
pt
from
pytensor.configdefaults
import
config
from
pytensor.configdefaults
import
config
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.tensor
import
elemwise
as
pt_elemwise
from
pytensor.tensor
import
elemwise
as
pt_elemwise
from
pytensor.tensor.special
import
SoftmaxGrad
,
log_softmax
,
softmax
from
pytensor.tensor.type
import
matrix
,
tensor
,
vector
from
pytensor.tensor.type
import
matrix
,
tensor
,
vector
from
tests.link.pytorch.test_basic
import
compare_pytorch_and_py
from
tests.link.pytorch.test_basic
import
compare_pytorch_and_py
...
@@ -53,3 +55,50 @@ def test_pytorch_elemwise():
...
@@ -53,3 +55,50 @@ def test_pytorch_elemwise():
fg
=
FunctionGraph
([
x
],
[
out
])
fg
=
FunctionGraph
([
x
],
[
out
])
compare_pytorch_and_py
(
fg
,
[[
0.9
,
0.9
]])
compare_pytorch_and_py
(
fg
,
[[
0.9
,
0.9
]])
@pytest.mark.parametrize
(
"dtype"
,
[
"float64"
,
"int64"
])
@pytest.mark.parametrize
(
"axis"
,
[
None
,
0
,
1
])
def
test_softmax
(
axis
,
dtype
):
x
=
matrix
(
"x"
,
dtype
=
dtype
)
out
=
softmax
(
x
,
axis
=
axis
)
fgraph
=
FunctionGraph
([
x
],
[
out
])
test_input
=
np
.
arange
(
6
,
dtype
=
config
.
floatX
)
.
reshape
(
2
,
3
)
if
dtype
==
"int64"
:
with
pytest
.
raises
(
NotImplementedError
,
match
=
"Pytorch Softmax is not currently implemented for non-float types."
,
):
compare_pytorch_and_py
(
fgraph
,
[
test_input
])
else
:
compare_pytorch_and_py
(
fgraph
,
[
test_input
])
@pytest.mark.parametrize
(
"dtype"
,
[
"float64"
,
"int64"
])
@pytest.mark.parametrize
(
"axis"
,
[
None
,
0
,
1
])
def
test_logsoftmax
(
axis
,
dtype
):
x
=
matrix
(
"x"
,
dtype
=
dtype
)
out
=
log_softmax
(
x
,
axis
=
axis
)
fgraph
=
FunctionGraph
([
x
],
[
out
])
test_input
=
np
.
arange
(
6
,
dtype
=
config
.
floatX
)
.
reshape
(
2
,
3
)
if
dtype
==
"int64"
:
with
pytest
.
raises
(
NotImplementedError
,
match
=
"Pytorch LogSoftmax is not currently implemented for non-float types."
,
):
compare_pytorch_and_py
(
fgraph
,
[
test_input
])
else
:
compare_pytorch_and_py
(
fgraph
,
[
test_input
])
@pytest.mark.parametrize
(
"axis"
,
[
None
,
0
,
1
])
def
test_softmax_grad
(
axis
):
dy
=
matrix
(
"dy"
)
dy_value
=
np
.
array
([[
1
,
1
,
1
],
[
0
,
0
,
0
]],
dtype
=
config
.
floatX
)
sm
=
matrix
(
"sm"
)
sm_value
=
np
.
arange
(
6
,
dtype
=
config
.
floatX
)
.
reshape
(
2
,
3
)
out
=
SoftmaxGrad
(
axis
=
axis
)(
dy
,
sm
)
fgraph
=
FunctionGraph
([
dy
,
sm
],
[
out
])
compare_pytorch_and_py
(
fgraph
,
[
dy_value
,
sm_value
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论