Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0fa34db7
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
0fa34db7
编写于
10月 12, 2017
作者:
D
dzhwinter
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
nccl init
上级
408e21af
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
161 addition
and
26 deletion
+161
-26
paddle/operators/nccl/nccl_gpu_common.cc
paddle/operators/nccl/nccl_gpu_common.cc
+9
-0
paddle/operators/nccl/nccl_gpu_common.h
paddle/operators/nccl/nccl_gpu_common.h
+39
-14
paddle/operators/nccl/nccl_ops.cc
paddle/operators/nccl/nccl_ops.cc
+59
-11
paddle/operators/nccl/nccl_ops.h
paddle/operators/nccl/nccl_ops.h
+54
-1
未找到文件。
paddle/operators/nccl/nccl_gpu_common.cc
0 → 100644
浏览文件 @
0fa34db7
#include "paddle/operators/nccl/nccl_gpu_common.h"
namespace
paddle
{
namespace
platform
{
}
// namespace operators
}
// namespace paddle
paddle/operators/nccl/nccl_gpu_common.h
浏览文件 @
0fa34db7
#pragma once
#pragma once
#include <nccl.h>
#include <nccl.h>
#include <memory>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <unordered_map>
#include "paddle/platform/device_context.h"
#include "paddle/platform/device_context.h"
namespace
paddle
{
namespace
paddle
{
namespace
platform
{
namespace
platform
{
// class NCCLContext : public DeviceContext {
// public:
// explicit NCCLContext(GPUPlace place);
// virtual ~NCCLContext();
// private:
// std::vector<int> gpu_ids_;
// std::vector<cudaStream_t> streams_;
// };
class
Communicator
;
class
NCCLManager
{
class
NCCLManager
{
public:
public:
static
NCCLManager
*
Get
()
{
static
NCCLManager
*
Get
()
{
...
@@ -13,23 +33,28 @@ class NCCLManager {
...
@@ -13,23 +33,28 @@ class NCCLManager {
return
&
m
;
return
&
m
;
}
}
NCCLManager
()
{
_comms
.
resize
(
_gpu_worlds
.
size
());
}
NCCLManager
()
{
}
~
NCCLManager
()
{}
~
NCCLManager
()
{}
// for each card only have one communicator
Communicator
*
GetCommunicator
()
const
;
private:
private:
std
::
vector
<
ncclComm_t
>
_comms
;
struct
Communicator
{
std
::
vector
<
int
>
_gpu_worlds
;
std
::
vector
<
ncclComm_t
>
comms_
;
};
std
::
vector
<
cudaStream_t
*>
streams_
;
// do not own
std
::
vector
<
cudaEvent_t
>
events_
;
int
root_gpu
;
};
class
NCCLContext
:
public
DeviceContext
{
// the gpu id list available. Note that only support
public:
// whole world communication.
explicit
NCCLContext
(
GPUPlace
place
);
std
::
vector
<
int
>
_gpu_worlds
;
virtual
~
NCCLContext
();
private:
// communicator list
std
::
vector
<
int
>
_gpu_ids
;
std
::
unordered_map
<
std
::
string
/* key*/
,
Communicator
*>
comms_
;
std
::
vector
<
cudaStream_t
>
_streams
;
int
root_gpu
;
};
};
}
}
}
// namespace operators
}
// namespace paddle
paddle/operators/nccl/nccl_ops.cc
浏览文件 @
0fa34db7
#include "paddle/framework/op_registry.h"
#include "paddle/operators/nccl/nccl_ops.h"
#include "paddle/operators/nccl/nccl_gpu_common.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
// AllreduceOp
// AllreduceOp
class
NCCLAll
r
educeOp
:
public
framework
::
OperatorWithKernel
{
class
NCCLAll
R
educeOp
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
// allreduce do nothing in infershape
// allreduce do nothing in infershape
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{}
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
" Input(X) of AllReduce op input should not be NULL"
);
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
outs
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
"Out"
);
PADDLE_ENFORCE
(
ins
.
size
()
==
outs
.
size
(),
"Input(X) and Output(Out) must have same size"
);
for
(
size_t
i
=
0
;
i
<
ins
.
size
();
++
i
)
{
outs
[
i
]
->
Resize
(
ins
[
i
]
->
dims
());
}
std
::
string
reduction
=
ctx
.
Attr
<
std
::
string
>
(
"reduction"
);
PADDLE_ENFORCE
(
(
reduction
==
"ncclSum"
||
reduction
==
"ncclProd"
||
reduction
==
"ncclMin"
||
reduction
==
"ncclMax"
),
"invalid reduction!"
);
}
};
};
template
<
typename
T
>
template
<
typename
T
>
...
@@ -19,30 +30,67 @@ class NCCLAllreduceOp : public framework::OpKernel {
...
@@ -19,30 +30,67 @@ class NCCLAllreduceOp : public framework::OpKernel {
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
ctx
=
static_cast
<
NCCLContext
*>
(
context
.
device_context
());
auto
*
ctx
=
static_cast
<
NCCLContext
*>
(
context
.
device_context
());
// auto *comm = ;
// auto *src = ;
// ncclAllReduce(src, dest, )
}
}
};
};
// BcastSendOp
// BcastSendOp
template
<
typename
T
>
template
<
typename
T
>
class
NCCLB
road
castSendOp
final
:
public
framework
::
OperatorWithKernel
{
class
NCCLBcastSendOp
final
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{}
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
" Input(X) of BcastSend op input should not be NULL"
);
}
};
};
// BcastRecvOp
// BcastRecvOp
template
<
typename
T
>
template
<
typename
T
>
class
NCCLB
road
castRecvOp
final
:
public
framework
::
OperatorWithKernel
{
class
NCCLBcastRecvOp
final
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{}
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Out"
),
" Input(X) of BcastRecv op input should not be NULL"
);
}
};
class
NCCLAllReduceOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
NCCLAllReduceOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input of AllReduce op"
);
AddOutput
(
"Out"
,
"The output of AllReduce op"
);
AddAttr
<
std
::
string
>
(
"reduction: {'min', 'max', 'prod', 'sum'}."
);
AddComment
(
R"DOC(
AllReduce the input tensors.
)DOC"
);
}
};
};
class
NCCLBcastSendOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
NCCLAllReduceOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input of BcastSend op"
);
AddComment
(
R"DOC(
BcastSend the tensors.
)DOC"
);
}
};
class
NCCLBcastRecvOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
NCCLAllReduceOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddOutput
(
"Out"
,
"The output of BcastRecv op"
);
AddComment
(
R"DOC(
BcastRecv the tensors.
)DOC"
);
}
};
}
}
}
}
paddle/operators/nccl/nccl_ops.h
浏览文件 @
0fa34db7
...
@@ -2,6 +2,59 @@
...
@@ -2,6 +2,59 @@
#include "paddle/framework/op_registry.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/nccl/nccl_gpu_common.h"
#include "paddle/operators/nccl/nccl_gpu_common.h"
#include <string.h>
namespace
paddle
{
namespace
paddle
{
namespace
operators
{}
namespace
operators
{
template
<
typename
Type
>
class
NCCLTypeWrapper
;
template
<
>
class
NCCLTypeWrapper
<
float
>
{
static
const
ncclDataType_t
type
=
ncclFloat
;
};
template
<
>
class
NCCLTypeWrapper
<
double
>
{
static
const
ncclDataType_t
type
=
ncclDouble
;
};
template
<
typename
T
>
class
NCCLAllReduceKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
outs
=
ctx
.
MultiOutput
<
Tensor
>
(
"Out"
);
std
::
string
reduction
=
ctx
.
Attr
<
std
::
string
>
(
"reduction"
);
ncclRedOp_t
op_type
;
if
(
reduction
==
"ncclSum"
)
{
op_type
=
ncclSum
;
}
else
if
(
reduction
==
"ncclProd"
)
{
op_type
=
ncclProd
;
}
else
if
(
reduction
==
"ncclMin"
)
{
op_type
=
ncclMin
;
}
else
(
reduction
==
"ncclMax"
)
{
op_type
=
ncclMax
;
}
auto
dev_ctx
=
ctx
.
device_context
();
for
(
size_t
i
=
0
;
i
<
ins
.
size
();
++
i
)
{
ncclAllReduce
(
ins
[
i
]
->
data
<
T
>
(),
outs
[
i
]
->
mutable_data
<
T
>
(),
outs
[
i
]
->
numel
()
*
sizeof
(
T
),
NCCLTypeWrapper
<
T
>::
type
,
op_type
,
comm
,
stream
);
}
}
};
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录