Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0cd21fac
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2292
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
0cd21fac
编写于
7月 19, 2021
作者:
R
Roc
提交者:
GitHub
7月 19, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU hybrid] Partial send /recv/ allgather for npu (#34189)
上级
2d5d5f37
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
278 addition
and
9 deletion
+278
-9
paddle/fluid/operators/collective/partial_allgather_op_npu.cc
...le/fluid/operators/collective/partial_allgather_op_npu.cc
+94
-0
paddle/fluid/operators/collective/partial_recv_op_npu.cc
paddle/fluid/operators/collective/partial_recv_op_npu.cc
+87
-0
paddle/fluid/operators/collective/partial_send_op_npu.cc
paddle/fluid/operators/collective/partial_send_op_npu.cc
+82
-0
python/paddle/fluid/optimizer.py
python/paddle/fluid/optimizer.py
+15
-9
未找到文件。
paddle/fluid/operators/collective/partial_allgather_op_npu.cc
0 → 100644
浏览文件 @
0cd21fac
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/operators/collective/partial_allgather_op.h"
#include <memory>
#include "paddle/fluid/platform/collective_helper.h"
#include "paddle/fluid/platform/hccl_helper.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
class
CallPartialGatherOpASCENDKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
#if defined(PADDLE_WITH_ASCEND_CL)
auto
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
int64_t
numel
=
in
->
numel
();
HcclDataType
dtype
=
platform
::
ToHCCLDataType
(
in
->
type
());
int
rank
=
ctx
.
Attr
<
int
>
(
"rank"
);
int
ring_id
=
ctx
.
Attr
<
int
>
(
"ring_id"
);
std
::
string
group
=
std
::
string
(
HCOM_GROUP_PREFIX
)
+
std
::
to_string
(
ring_id
);
auto
place
=
ctx
.
GetPlace
();
auto
comm
=
platform
::
HCCLCommContext
::
Instance
().
Get
(
ring_id
,
place
);
int
nranks
=
comm
->
nranks
();
PADDLE_ENFORCE_EQ
(
rank
,
comm
->
rank
(),
platform
::
errors
::
InvalidArgument
(
"rank: %s should equal to %s"
,
rank
,
comm
->
rank
()));
PADDLE_ENFORCE_EQ
(
(
numel
%
nranks
),
0
,
platform
::
errors
::
InvalidArgument
(
"The input numel (%d) must be divisible by nranks(%d)"
,
numel
,
nranks
));
framework
::
DDim
dims
=
in
->
dims
();
out
->
mutable_data
<
T
>
(
dims
,
place
);
int64_t
send_numel
=
numel
/
nranks
;
int
offset
=
send_numel
*
rank
;
void
*
send_buff
=
reinterpret_cast
<
void
*>
(
const_cast
<
T
*>
(
in
->
data
<
T
>
())
+
offset
);
void
*
recv_buff
=
reinterpret_cast
<
void
*>
(
out
->
data
<
T
>
());
aclrtStream
stream
=
nullptr
;
if
(
ctx
.
Attr
<
bool
>
(
"use_calc_stream"
))
{
auto
dev_ctx
=
platform
::
DeviceContextPool
::
Instance
().
Get
(
place
);
stream
=
static_cast
<
platform
::
NPUDeviceContext
*>
(
dev_ctx
)
->
stream
();
}
else
{
stream
=
comm
->
stream
();
}
VLOG
(
3
)
<<
"begin hccl allgather, parameter is: "
<<
", group is "
<<
group
<<
", ring_id is "
<<
ring_id
<<
", nranks is "
<<
nranks
<<
", rankid is "
<<
rank
;
PADDLE_ENFORCE_NPU_SUCCESS
(
platform
::
dynload
::
HcclAllGather
(
send_buff
,
recv_buff
,
send_numel
,
dtype
,
comm
->
comm
(),
reinterpret_cast
<
void
*>
(
stream
)));
#else
PADDLE_THROW
(
platform
::
errors
::
PreconditionNotMet
(
"PaddlePaddle should compile with NPU."
));
#endif
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_NPU_KERNEL
(
partial_allgather
,
ops
::
CallPartialGatherOpASCENDKernel
<
int8_t
>
,
ops
::
CallPartialGatherOpASCENDKernel
<
int
>
,
ops
::
CallPartialGatherOpASCENDKernel
<
float
>
,
ops
::
CallPartialGatherOpASCENDKernel
<
plat
::
float16
>
);
paddle/fluid/operators/collective/partial_recv_op_npu.cc
0 → 100644
浏览文件 @
0cd21fac
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/operators/collective/partial_recv_op.h"
#include "paddle/fluid/platform/collective_helper.h"
#include "paddle/fluid/platform/hccl_helper.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
class
PartialRecvOpASCENDKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
#if defined(PADDLE_WITH_ASCEND_CL)
auto
out
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
out
->
dims
(),
ctx
.
GetPlace
());
int
num
=
ctx
.
Attr
<
int
>
(
"num"
);
int
id
=
ctx
.
Attr
<
int
>
(
"id"
);
int
recv_numel
=
out
->
numel
()
/
num
;
int
offset
=
recv_numel
*
id
;
void
*
ptr
=
reinterpret_cast
<
void
*>
(
const_cast
<
T
*>
(
out
->
data
<
T
>
())
+
offset
);
int
numel
=
recv_numel
;
HcclDataType
dtype
=
platform
::
ToHCCLDataType
(
out
->
type
());
int
ring_id
=
ctx
.
Attr
<
int
>
(
"ring_id"
);
auto
place
=
ctx
.
GetPlace
();
auto
comm
=
paddle
::
platform
::
HCCLCommContext
::
Instance
().
Get
(
ring_id
,
place
);
aclrtStream
stream
=
nullptr
;
auto
dev_ctx
=
platform
::
DeviceContextPool
::
Instance
().
Get
(
place
);
if
(
ctx
.
Attr
<
bool
>
(
"use_calc_stream"
))
{
stream
=
static_cast
<
platform
::
NPUDeviceContext
*>
(
dev_ctx
)
->
stream
();
}
else
{
stream
=
comm
->
stream
();
}
int
nranks
=
comm
->
nranks
();
int
peer
=
ctx
.
Attr
<
int
>
(
"peer"
);
PADDLE_ENFORCE_EQ
(
nranks
,
2
,
platform
::
errors
::
InvalidArgument
(
"The nranks must be 2, but (%d)"
,
nranks
));
int
root
=
peer
;
VLOG
(
3
)
<<
"begin hccl recv, parameter is: "
<<
"ring_id:"
<<
ring_id
<<
", nranks:"
<<
nranks
<<
", peer:"
<<
peer
<<
", numel:"
<<
numel
<<
", ptr:"
<<
ptr
<<
", dtype:"
<<
dtype
<<
", root:"
<<
root
<<
", comm: "
<<
comm
->
comm
()
<<
", stream: "
<<
stream
;
PADDLE_ENFORCE_NPU_SUCCESS
(
platform
::
dynload
::
HcclBroadcast
(
ptr
,
numel
,
dtype
,
(
uint32_t
)
root
,
comm
->
comm
(),
stream
));
#else
PADDLE_THROW
(
platform
::
errors
::
PreconditionNotMet
(
"PaddlePaddle should compile with NPU."
));
#endif
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_NPU_KERNEL
(
partial_recv
,
ops
::
PartialRecvOpASCENDKernel
<
int
>
,
ops
::
PartialRecvOpASCENDKernel
<
int8_t
>
,
ops
::
PartialRecvOpASCENDKernel
<
float
>
,
ops
::
PartialRecvOpASCENDKernel
<
plat
::
float16
>
);
paddle/fluid/operators/collective/partial_send_op_npu.cc
0 → 100644
浏览文件 @
0cd21fac
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/operators/collective/send_v2_op.h"
#include "paddle/fluid/platform/collective_helper.h"
#include "paddle/fluid/platform/hccl_helper.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
class
PartialSendOpASCENDKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
#if defined(PADDLE_WITH_ASCEND_CL)
auto
x
=
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
);
int
num
=
ctx
.
Attr
<
int
>
(
"num"
);
int
id
=
ctx
.
Attr
<
int
>
(
"id"
);
int
send_numel
=
x
->
numel
()
/
num
;
int
offset
=
send_numel
*
id
;
void
*
ptr
=
reinterpret_cast
<
void
*>
(
const_cast
<
T
*>
(
x
->
data
<
T
>
())
+
offset
);
int
numel
=
send_numel
;
HcclDataType
dtype
=
platform
::
ToHCCLDataType
(
x
->
type
());
int
ring_id
=
ctx
.
Attr
<
int
>
(
"ring_id"
);
auto
place
=
ctx
.
GetPlace
();
auto
comm
=
paddle
::
platform
::
HCCLCommContext
::
Instance
().
Get
(
ring_id
,
place
);
aclrtStream
stream
=
nullptr
;
auto
dev_ctx
=
platform
::
DeviceContextPool
::
Instance
().
Get
(
place
);
if
(
ctx
.
Attr
<
bool
>
(
"use_calc_stream"
))
{
stream
=
static_cast
<
platform
::
NPUDeviceContext
*>
(
dev_ctx
)
->
stream
();
}
else
{
stream
=
comm
->
stream
();
}
int
nranks
=
comm
->
nranks
();
int
rank
=
comm
->
rank
();
PADDLE_ENFORCE_EQ
(
nranks
,
2
,
platform
::
errors
::
InvalidArgument
(
"The nranks must be 2, but (%d)"
,
nranks
));
int
root
=
rank
;
VLOG
(
3
)
<<
"begin hccl send, parameter is: "
<<
"root "
<<
root
<<
", comm: "
<<
comm
->
comm
()
<<
", stream: "
<<
stream
;
PADDLE_ENFORCE_NPU_SUCCESS
(
platform
::
dynload
::
HcclBroadcast
(
ptr
,
numel
,
dtype
,
(
uint32_t
)
root
,
comm
->
comm
(),
stream
));
#else
PADDLE_THROW
(
platform
::
errors
::
PreconditionNotMet
(
"PaddlePaddle should compile with NPU."
));
#endif
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_NPU_KERNEL
(
partial_send
,
ops
::
PartialSendOpASCENDKernel
<
int
>
,
ops
::
PartialSendOpASCENDKernel
<
int8_t
>
,
ops
::
PartialSendOpASCENDKernel
<
float
>
,
ops
::
PartialSendOpASCENDKernel
<
plat
::
float16
>
);
python/paddle/fluid/optimizer.py
浏览文件 @
0cd21fac
...
@@ -4190,6 +4190,11 @@ class PipelineOptimizer(object):
...
@@ -4190,6 +4190,11 @@ class PipelineOptimizer(object):
"""
"""
def
__init__
(
self
,
optimizer
,
num_microbatches
=
1
,
start_cpu_core_id
=
0
):
def
__init__
(
self
,
optimizer
,
num_microbatches
=
1
,
start_cpu_core_id
=
0
):
self
.
_device
=
'cpu'
if
core
.
is_compiled_with_npu
():
self
.
_device
=
"npu"
elif
core
.
is_compiled_with_cuda
():
self
.
_device
=
"gpu"
if
framework
.
in_dygraph_mode
():
if
framework
.
in_dygraph_mode
():
raise
Exception
(
"In dygraph, don't support PipelineOptimizer."
)
raise
Exception
(
"In dygraph, don't support PipelineOptimizer."
)
if
not
isinstance
(
optimizer
,
Optimizer
)
and
not
isinstance
(
if
not
isinstance
(
optimizer
,
Optimizer
)
and
not
isinstance
(
...
@@ -4387,7 +4392,7 @@ class PipelineOptimizer(object):
...
@@ -4387,7 +4392,7 @@ class PipelineOptimizer(object):
for
op
in
block
.
ops
:
for
op
in
block
.
ops
:
device
=
op
.
attr
(
self
.
_op_device_key
)
device
=
op
.
attr
(
self
.
_op_device_key
)
# Copy ops whose op_device set to "gpu:all" to all sections.
# Copy ops whose op_device set to "gpu:all" to all sections.
if
device
==
"gpu
:all"
:
if
device
==
f
"
{
self
.
_device
}
:all"
:
for
device
in
devices
:
for
device
in
devices
:
program
=
device_program_map
[
device
]
program
=
device_program_map
[
device
]
op_desc
=
op
.
desc
op_desc
=
op
.
desc
...
@@ -4539,7 +4544,7 @@ class PipelineOptimizer(object):
...
@@ -4539,7 +4544,7 @@ class PipelineOptimizer(object):
if
op
.
attr
(
self
.
_op_role_key
)
==
lrsched_role
:
if
op
.
attr
(
self
.
_op_role_key
)
==
lrsched_role
:
# For LRSched ops, we should put them on all sub-programs to
# For LRSched ops, we should put them on all sub-programs to
# make sure each sub-program update the lr correctly
# make sure each sub-program update the lr correctly
op
.
_set_attr
(
self
.
_op_device_key
,
"gpu
:all"
)
op
.
_set_attr
(
self
.
_op_device_key
,
f
"
{
self
.
_device
}
:all"
)
# bugfix in hybrid parallelism
# bugfix in hybrid parallelism
elif
op
.
type
==
"sum"
and
self
.
_is_backward_op
(
op
):
elif
op
.
type
==
"sum"
and
self
.
_is_backward_op
(
op
):
# For sum ops that compute the sum of @RENAMED@ vars
# For sum ops that compute the sum of @RENAMED@ vars
...
@@ -4606,10 +4611,10 @@ class PipelineOptimizer(object):
...
@@ -4606,10 +4611,10 @@ class PipelineOptimizer(object):
op
.
type
==
'fill_constant'
or
op
.
type
==
'fill_constant'
or
op
.
type
==
'elementwise_max'
or
op
.
type
==
'elementwise_max'
or
op
.
type
==
'elementwise_div'
):
op
.
type
==
'elementwise_div'
):
device
=
"gpu
:all"
device
=
f
"
{
self
.
_device
}
:all"
op
.
_set_attr
(
self
.
_op_device_key
,
device
)
op
.
_set_attr
(
self
.
_op_device_key
,
device
)
elif
op
.
type
==
"alloc_float_status"
:
elif
op
.
type
==
"alloc_float_status"
:
op
.
_set_attr
(
self
.
_op_device_key
,
"gpu
:all"
)
op
.
_set_attr
(
self
.
_op_device_key
,
f
"
{
self
.
_device
}
:all"
)
else
:
else
:
other_known_ops
=
[
other_known_ops
=
[
'update_loss_scaling'
,
'update_loss_scaling'
,
...
@@ -4623,7 +4628,7 @@ class PipelineOptimizer(object):
...
@@ -4623,7 +4628,7 @@ class PipelineOptimizer(object):
"op_device set, they must be one of {}, but it "
\
"op_device set, they must be one of {}, but it "
\
"is {}"
.
format
(
other_known_ops
,
op
.
type
)
"is {}"
.
format
(
other_known_ops
,
op
.
type
)
assert
self
.
_is_optimize_op
(
op
)
assert
self
.
_is_optimize_op
(
op
)
op
.
_set_attr
(
self
.
_op_device_key
,
"gpu
:all"
)
op
.
_set_attr
(
self
.
_op_device_key
,
f
"
{
self
.
_device
}
:all"
)
def
_add_op_device_attr
(
self
,
block
):
def
_add_op_device_attr
(
self
,
block
):
"""
"""
...
@@ -4638,7 +4643,7 @@ class PipelineOptimizer(object):
...
@@ -4638,7 +4643,7 @@ class PipelineOptimizer(object):
# We use "gpu:all" to represent the op should be put on all
# We use "gpu:all" to represent the op should be put on all
# sub-programs, such as lr-related ops. Note that: "gpu:all"
# sub-programs, such as lr-related ops. Note that: "gpu:all"
# is only used by pipeline as an indicator.
# is only used by pipeline as an indicator.
op
.
_set_attr
(
self
.
_op_device_key
,
"gpu
:all"
)
op
.
_set_attr
(
self
.
_op_device_key
,
f
"
{
self
.
_device
}
:all"
)
continue
continue
# op_device attribute has been set
# op_device attribute has been set
if
self
.
_get_op_device_attr
(
op
):
continue
if
self
.
_get_op_device_attr
(
op
):
continue
...
@@ -4691,7 +4696,7 @@ class PipelineOptimizer(object):
...
@@ -4691,7 +4696,7 @@ class PipelineOptimizer(object):
device
=
op
.
attr
(
self
.
_op_device_key
)
device
=
op
.
attr
(
self
.
_op_device_key
)
assert
device
,
(
"op_device attribute for op "
assert
device
,
(
"op_device attribute for op "
"{} has not been set."
.
format
(
op
.
type
))
"{} has not been set."
.
format
(
op
.
type
))
if
device
==
"gpu:all"
or
device
==
"npu
:all"
:
continue
if
device
==
f
"
{
self
.
_device
}
:all"
:
continue
dev_type
=
device
.
split
(
':'
)[
0
]
dev_type
=
device
.
split
(
':'
)[
0
]
stage_id
=
int
(
device
.
split
(
':'
)[
1
])
stage_id
=
int
(
device
.
split
(
':'
)[
1
])
...
@@ -4745,7 +4750,7 @@ class PipelineOptimizer(object):
...
@@ -4745,7 +4750,7 @@ class PipelineOptimizer(object):
for
index
,
op
in
enumerate
(
list
(
block
.
ops
)):
for
index
,
op
in
enumerate
(
list
(
block
.
ops
)):
cur_device
=
op
.
attr
(
self
.
_op_device_key
)
cur_device
=
op
.
attr
(
self
.
_op_device_key
)
if
cur_device
==
"gpu
:all"
:
continue
if
cur_device
==
f
"
{
self
.
_device
}
:all"
:
continue
for
var_name
in
op
.
input_arg_names
:
for
var_name
in
op
.
input_arg_names
:
var
=
block
.
var
(
var_name
)
var
=
block
.
var
(
var_name
)
# skip data var
# skip data var
...
@@ -4763,7 +4768,8 @@ class PipelineOptimizer(object):
...
@@ -4763,7 +4768,8 @@ class PipelineOptimizer(object):
prev_device
=
prev_op
.
attr
(
self
.
_op_device_key
)
\
prev_device
=
prev_op
.
attr
(
self
.
_op_device_key
)
\
if
prev_op
else
None
if
prev_op
else
None
if
prev_device
is
None
or
prev_device
==
"gpu:all"
:
continue
if
prev_device
is
None
or
prev_device
==
f
"
{
self
.
_device
}
:all"
:
continue
if
prev_device
==
cur_device
:
continue
if
prev_device
==
cur_device
:
continue
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录