Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6ad1c5cf
Unverified
提交
6ad1c5cf
authored
7月 18, 2024
作者:
Pham Nguyen Hung
提交者:
GitHub
7月 18, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement Dot and BatchedDot in PyTensor (#878)
上级
426931b0
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
84 行增加
和
1 行删除
+84
-1
__init__.py
pytensor/link/__init__.py
+1
-0
__init__.py
pytensor/link/pytorch/dispatch/__init__.py
+4
-1
blas.py
pytensor/link/pytorch/dispatch/blas.py
+14
-0
math.py
pytensor/link/pytorch/dispatch/math.py
+12
-0
test_blas.py
tests/link/pytorch/test_blas.py
+24
-0
test_math.py
tests/link/pytorch/test_math.py
+29
-0
没有找到文件。
pytensor/link/__init__.py
浏览文件 @
6ad1c5cf
from
pytensor.link.pytorch.linker
import
PytorchLinker
pytensor/link/pytorch/dispatch/__init__.py
浏览文件 @
6ad1c5cf
...
...
@@ -2,9 +2,12 @@
from
pytensor.link.pytorch.dispatch.basic
import
pytorch_funcify
,
pytorch_typify
# # Load dispatch specializations
import
pytensor.link.pytorch.dispatch.blas
import
pytensor.link.pytorch.dispatch.scalar
import
pytensor.link.pytorch.dispatch.elemwise
import
pytensor.link.pytorch.dispatch.math
import
pytensor.link.pytorch.dispatch.extra_ops
import
pytensor.link.pytorch.dispatch.sort
import
pytensor.link.pytorch.dispatch.shape
import
pytensor.link.pytorch.dispatch.sort
# isort: on
pytensor/link/pytorch/dispatch/blas.py
0 → 100644
浏览文件 @
6ad1c5cf
import
torch
from
pytensor.link.pytorch.dispatch
import
pytorch_funcify
from
pytensor.tensor.blas
import
BatchedDot
@pytorch_funcify.register
(
BatchedDot
)
def
pytorch_funcify_BatchedDot
(
op
,
**
kwargs
):
def
batched_dot
(
a
,
b
):
if
a
.
shape
[
0
]
!=
b
.
shape
[
0
]:
raise
TypeError
(
"Shapes must match in the 0-th dimension"
)
return
torch
.
bmm
(
a
,
b
)
return
batched_dot
pytensor/link/pytorch/dispatch/math.py
0 → 100644
浏览文件 @
6ad1c5cf
import
torch
from
pytensor.link.pytorch.dispatch
import
pytorch_funcify
from
pytensor.tensor.math
import
Dot
@pytorch_funcify.register
(
Dot
)
def
pytorch_funcify_Dot
(
op
,
**
kwargs
):
def
dot
(
x
,
y
):
return
torch
.
matmul
(
x
,
y
)
return
dot
tests/link/pytorch/test_blas.py
0 → 100644
浏览文件 @
6ad1c5cf
import
numpy
as
np
import
pytest
from
pytensor.configdefaults
import
config
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.tensor
import
blas
as
pt_blas
from
pytensor.tensor.type
import
tensor3
from
tests.link.pytorch.test_basic
import
compare_pytorch_and_py
def
test_pytorch_BatchedDot
():
# tensor3 . tensor3
a
=
tensor3
(
"a"
)
a_test
=
np
.
linspace
(
-
1
,
1
,
10
*
5
*
3
)
.
astype
(
config
.
floatX
)
.
reshape
((
10
,
5
,
3
))
b
=
tensor3
(
"b"
)
b_test
=
np
.
linspace
(
1
,
-
1
,
10
*
3
*
2
)
.
astype
(
config
.
floatX
)
.
reshape
((
10
,
3
,
2
))
out
=
pt_blas
.
BatchedDot
()(
a
,
b
)
fgraph
=
FunctionGraph
([
a
,
b
],
[
out
])
pytensor_pytorch_fn
,
_
=
compare_pytorch_and_py
(
fgraph
,
[
a_test
,
b_test
])
# A dimension mismatch should raise a TypeError for compatibility
inputs
=
[
a_test
[:
-
1
],
b_test
]
with
pytest
.
raises
(
TypeError
):
pytensor_pytorch_fn
(
*
inputs
)
tests/link/pytorch/test_math.py
0 → 100644
浏览文件 @
6ad1c5cf
import
numpy
as
np
from
pytensor.configdefaults
import
config
from
pytensor.graph.fg
import
FunctionGraph
from
pytensor.tensor.type
import
matrix
,
scalar
,
vector
from
tests.link.pytorch.test_basic
import
compare_pytorch_and_py
def
test_pytorch_dot
():
y
=
vector
(
"y"
)
y_test
=
np
.
r_
[
1.0
,
2.0
]
.
astype
(
config
.
floatX
)
x
=
vector
(
"x"
)
x_test
=
np
.
r_
[
3.0
,
4.0
]
.
astype
(
config
.
floatX
)
A
=
matrix
(
"A"
)
A_test
=
np
.
array
([[
6
,
3
],
[
3
,
0
]],
dtype
=
config
.
floatX
)
alpha
=
scalar
(
"alpha"
)
alpha_test
=
np
.
array
(
3.0
,
dtype
=
config
.
floatX
)
beta
=
scalar
(
"beta"
)
beta_test
=
np
.
array
(
5.0
,
dtype
=
config
.
floatX
)
# 2D * 2D
out
=
A
.
dot
(
A
*
alpha
)
+
beta
*
A
fgraph
=
FunctionGraph
([
A
,
alpha
,
beta
],
[
out
])
compare_pytorch_and_py
(
fgraph
,
[
A_test
,
alpha_test
,
beta_test
])
# 1D * 2D and 1D * 1D
out
=
y
.
dot
(
alpha
*
A
)
.
dot
(
x
)
+
beta
*
y
fgraph
=
FunctionGraph
([
y
,
x
,
A
,
alpha
,
beta
],
[
out
])
compare_pytorch_and_py
(
fgraph
,
[
y_test
,
x_test
,
A_test
,
alpha_test
,
beta_test
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论