Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
d7f0fc8f
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
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看板
提交
d7f0fc8f
编写于
4月 15, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test(mge/distributed): add gather scatter reduce broadcast grad test
GitOrigin-RevId: 8245e11f1dd1c9c8f43d553924972f20a028d39d
上级
b5ec9dfe
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
119 addition
and
6 deletion
+119
-6
imperative/python/test/unit/autodiff/test_grad_manger.py
imperative/python/test/unit/autodiff/test_grad_manger.py
+112
-3
imperative/python/test/unit/core/test_autodiff.py
imperative/python/test/unit/core/test_autodiff.py
+2
-2
imperative/python/test/unit/functional/test_functional_distributed.py
...ython/test/unit/functional/test_functional_distributed.py
+5
-1
未找到文件。
imperative/python/test/unit/autodiff/test_grad_manger.py
浏览文件 @
d7f0fc8f
...
...
@@ -5,6 +5,7 @@
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import
os
import
platform
import
weakref
...
...
@@ -151,9 +152,7 @@ def test_remote_grad(trace_mode):
def
train_func
(
x
):
with
gm
:
if
rank
!=
0
:
x
=
dist
.
functional
.
remote_recv
(
rank
-
1
,
shape
=
(
1
,
rank
*
2
+
2
),
dtype
=
np
.
float32
)
x
=
dist
.
functional
.
remote_recv
(
rank
-
1
)
y
=
m
(
x
)
if
rank
!=
size
-
1
:
dist
.
functional
.
remote_send
(
y
,
dest_rank
=
rank
+
1
)
...
...
@@ -170,3 +169,113 @@ def test_remote_grad(trace_mode):
train_func
(
x
)
worker
()
@
pytest
.
mark
.
require_ngpu
(
3
)
@
pytest
.
mark
.
isolated_distributed
@
pytest
.
mark
.
parametrize
(
"trace_mode"
,
[
True
,
False
,
None
],
ids
=
[
"symbolic"
,
"trace"
,
"no_trace"
]
)
def
test_gather_grad
(
trace_mode
):
@
dist
.
launcher
(
n_gpus
=
3
)
def
worker
():
m
=
M
.
Linear
(
10
,
10
)
x
=
F
.
ones
([
3
,
10
],
dtype
=
"float32"
)
def
func
():
with
GradManager
().
attach
(
m
.
parameters
())
as
gm
:
y
=
m
(
x
)
y
=
F
.
distributed
.
gather
(
y
)
if
dist
.
get_rank
()
==
0
:
loss
=
(
2
*
y
+
1
).
mean
()
gm
.
backward
(
loss
)
else
:
gm
.
backward
()
if
trace_mode
is
not
None
:
func
=
trace
(
symbolic
=
trace_mode
)(
func
)
func
()
worker
()
@
pytest
.
mark
.
require_ngpu
(
3
)
@
pytest
.
mark
.
isolated_distributed
@
pytest
.
mark
.
parametrize
(
"trace_mode"
,
[
True
,
False
,
None
],
ids
=
[
"symbolic"
,
"trace"
,
"no_trace"
]
)
def
test_scatter_grad
(
trace_mode
):
@
dist
.
launcher
(
n_gpus
=
3
)
def
worker
():
x
=
F
.
ones
([
3
,
10
],
dtype
=
"float32"
)
m
=
M
.
Linear
(
10
,
10
)
def
func
():
with
GradManager
().
attach
(
m
.
parameters
())
as
gm
:
if
dist
.
get_rank
()
==
0
:
y
=
m
(
x
)
else
:
y
=
x
y
=
F
.
distributed
.
scatter
(
y
)
gm
.
backward
(
y
)
if
trace_mode
is
not
None
:
func
=
trace
(
symbolic
=
trace_mode
)(
func
)
func
()
worker
()
@
pytest
.
mark
.
require_ngpu
(
3
)
@
pytest
.
mark
.
isolated_distributed
@
pytest
.
mark
.
parametrize
(
"trace_mode"
,
[
True
,
False
,
None
],
ids
=
[
"symbolic"
,
"trace"
,
"no_trace"
]
)
def
test_reduce_grad
(
trace_mode
):
@
dist
.
launcher
(
n_gpus
=
3
)
def
worker
():
m
=
M
.
Linear
(
10
,
10
)
x
=
F
.
ones
([
3
,
10
],
dtype
=
"float32"
)
def
func
():
with
GradManager
().
attach
(
m
.
parameters
())
as
gm
:
y
=
m
(
x
)
y
=
F
.
distributed
.
reduce_sum
(
y
)
if
dist
.
get_rank
()
==
0
:
loss
=
(
2
*
y
+
1
).
mean
()
gm
.
backward
(
loss
)
else
:
gm
.
backward
()
if
trace_mode
is
not
None
:
func
=
trace
(
symbolic
=
trace_mode
)(
func
)
func
()
worker
()
@
pytest
.
mark
.
require_ngpu
(
3
)
@
pytest
.
mark
.
isolated_distributed
@
pytest
.
mark
.
parametrize
(
"trace_mode"
,
[
True
,
False
,
None
],
ids
=
[
"symbolic"
,
"trace"
,
"no_trace"
]
)
def
test_broadcast_grad
(
trace_mode
):
@
dist
.
launcher
(
n_gpus
=
3
)
def
worker
():
x
=
F
.
ones
([
3
,
10
],
dtype
=
"float32"
)
m
=
M
.
Linear
(
10
,
10
)
def
func
():
with
GradManager
().
attach
(
m
.
parameters
())
as
gm
:
if
dist
.
get_rank
()
==
0
:
y
=
m
(
x
)
else
:
y
=
x
y
=
F
.
distributed
.
broadcast
(
y
)
gm
.
backward
(
y
)
if
trace_mode
is
not
None
:
func
=
trace
(
symbolic
=
trace_mode
)(
func
)
func
()
worker
()
imperative/python/test/unit/core/test_autodiff.py
浏览文件 @
d7f0fc8f
...
...
@@ -67,7 +67,7 @@ def test_dist_grad():
grad
.
wrt
(
x
,
callback
=
save_to
(
x
))
# need a placeholder to trace operator
remote_send
(
x
,
1
)
recv_x
=
remote_recv
(
1
,
x_np
.
shape
,
x_np
.
dtype
)
recv_x
=
remote_recv
(
1
)
y
=
recv_x
*
recv_x
grad
([
y
],
[
as_tensor
(
np
.
ones_like
(
x_np
))])
...
...
@@ -75,7 +75,7 @@ def test_dist_grad():
elif
rank
==
1
:
grad
=
Grad
()
recv_x
=
remote_recv
(
0
,
x_np
.
shape
,
x_np
.
dtype
)
recv_x
=
remote_recv
(
0
)
remote_send
(
recv_x
,
0
)
grad
([],
[])
...
...
imperative/python/test/unit/functional/test_functional_distributed.py
浏览文件 @
d7f0fc8f
...
...
@@ -44,6 +44,8 @@ def test_reduce_sum(shape):
output
=
reduce_sum
(
inp
)
if
rank
==
0
:
assert
np
.
allclose
(
output
.
numpy
(),
expect
[
rank
])
else
:
assert
output
is
None
x
=
np
.
random
.
random_sample
(
shape
).
astype
(
"float32"
)
y
=
np
.
random
.
random_sample
(
shape
).
astype
(
"float32"
)
...
...
@@ -177,6 +179,8 @@ def test_gather(shape):
output
=
gather
(
inp
)
if
rank
==
0
:
assert
np
.
allclose
(
output
.
numpy
(),
expect
[
rank
])
else
:
assert
output
is
None
x
=
np
.
random
.
random_sample
(
shape
).
astype
(
"float32"
)
y
=
np
.
random
.
random_sample
(
shape
).
astype
(
"float32"
)
...
...
@@ -236,7 +240,7 @@ def test_io_remote(shape):
remote_send
(
x
,
1
)
sync
()
else
:
# remote recv
y
=
remote_recv
(
0
,
shape
,
np
.
float32
)
y
=
remote_recv
(
0
)
assert
y
.
device
==
get_default_device
()
np
.
testing
.
assert_almost_equal
(
val
,
y
.
numpy
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录