Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
1e0fb127
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
399
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
1e0fb127
编写于
11月 09, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
perf(mge/functional): reduce matmul python overhead
GitOrigin-RevId: 738d0da10ee7efdf3bd3e3030f10ac9c8aa16a5c
上级
72619bb4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
43 addition
and
20 deletion
+43
-20
imperative/python/megengine/core/tensor/tensor_wrapper.py
imperative/python/megengine/core/tensor/tensor_wrapper.py
+3
-2
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+40
-18
未找到文件。
imperative/python/megengine/core/tensor/tensor_wrapper.py
浏览文件 @
1e0fb127
...
...
@@ -345,9 +345,10 @@ class ArrayMethodMixin(abc.ABC):
@
property
def
ndim
(
self
):
shape
=
self
.
shape
# XXX: assume ndim is not changed during trace
if
isinstance
(
shape
,
self
.
__class__
):
shape
=
shape
.
numpy
()
# XXX: assume ndim is not changed during trace
ndim
=
shape
.
__wrapped__
.
shape
[
0
]
return
ndim
return
len
(
shape
)
@
property
...
...
imperative/python/megengine/functional/nn.py
浏览文件 @
1e0fb127
...
...
@@ -10,6 +10,7 @@
from
typing
import
Optional
,
Sequence
,
Tuple
,
Union
from
..core._imperative_rt
import
CompNode
from
..core._trace_option
import
use_symbolic_shape
from
..core.ops
import
builtin
from
..core.ops._internal
import
param_defs
as
P
from
..core.ops.builtin
import
BatchNorm
...
...
@@ -1015,23 +1016,39 @@ def matmul(
remove_col
=
True
batch_shape
=
None
shape1
=
astensor1d
(
inp1
.
shape
,
inp1
,
dtype
=
"int32"
,
device
=
inp1
.
device
)
shape2
=
astensor1d
(
inp2
.
shape
,
inp2
,
dtype
=
"int32"
,
device
=
inp2
.
device
)
shape1
=
inp1
.
shape
shape2
=
inp2
.
shape
maxdim
=
dim1
if
dim1
>
dim2
else
dim2
if
dim1
>=
3
or
dim2
>=
3
:
if
dim1
==
dim2
:
assert
(
shape1
[:
-
2
]
==
shape2
[:
-
2
]
).
min
(),
"operands could not be broadcasted together."
if
dim1
>
dim2
:
shape2
=
concat
([
shape1
[:
-
2
],
shape2
[
-
2
:]])
inp2
=
broadcast_to
(
inp2
,
shape2
)
if
dim1
<
dim2
:
shape1
=
concat
([
shape2
[:
-
2
],
shape1
[
-
2
:]])
inp1
=
broadcast_to
(
inp1
,
shape1
)
batch_shape
=
shape1
[:
-
2
]
# compress inputs to 3d
inp1
=
inp1
.
reshape
(
concat
([
prod
(
shape1
[:
-
2
]),
shape1
[
-
2
:]]))
inp2
=
inp2
.
reshape
(
concat
([
prod
(
shape2
[:
-
2
]),
shape2
[
-
2
:]]))
if
use_symbolic_shape
():
if
dim1
>
dim2
:
shape2
=
concat
([
shape1
[:
-
2
],
shape2
[
-
2
:]])
inp2
=
broadcast_to
(
inp2
,
shape2
)
if
dim1
<
dim2
:
shape1
=
concat
([
shape2
[:
-
2
],
shape1
[
-
2
:]])
inp1
=
broadcast_to
(
inp1
,
shape1
)
if
maxdim
>
3
:
batch_shape
=
shape1
[:
-
2
]
# compress inputs to 3d
(
inp1
,)
=
apply
(
builtin
.
Reshape
(),
inp1
,
concat
([
prod
(
shape1
[:
-
2
]),
shape1
[
-
2
:]])
)
(
inp2
,)
=
apply
(
builtin
.
Reshape
(),
inp2
,
concat
([
prod
(
shape2
[:
-
2
]),
shape2
[
-
2
:]])
)
else
:
if
dim1
>
dim2
:
shape2
=
shape1
[:
-
2
]
+
shape2
[
-
2
:]
inp2
=
broadcast_to
(
inp2
,
shape2
)
if
dim1
<
dim2
:
shape1
=
shape2
[:
-
2
]
+
shape1
[
-
2
:]
inp1
=
broadcast_to
(
inp1
,
shape1
)
if
maxdim
>
3
:
batch_shape
=
shape1
[:
-
2
]
# compress inputs to 3d
inp1
=
inp1
.
reshape
((
-
1
,
shape1
[
-
2
],
shape1
[
-
1
]))
inp2
=
inp2
.
reshape
((
-
1
,
shape2
[
-
2
],
shape2
[
-
1
]))
op
=
builtin
.
BatchedMatrixMul
(
transposeA
=
transpose_a
,
...
...
@@ -1048,8 +1065,13 @@ def matmul(
)
(
result
,)
=
apply
(
op
,
inp1
,
inp2
)
if
batch_shape
is
not
None
:
result
=
result
.
reshape
(
concat
([
batch_shape
,
result
.
shape
[
-
2
:]]))
if
maxdim
>
3
:
if
use_symbolic_shape
():
(
result
,)
=
apply
(
builtin
.
Reshape
(),
result
,
concat
([
batch_shape
,
result
.
shape
[
-
2
:]])
)
else
:
result
=
result
.
reshape
(
batch_shape
+
result
.
shape
[
-
2
:])
if
remove_row
:
result
=
squeeze
(
result
,
axis
=-
2
)
if
remove_col
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录