Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7620efdf
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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,发现更多精彩内容 >>
提交
7620efdf
编写于
9月 23, 2017
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
combine gpu&cpu code in multiplex_op
上级
85a5d384
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
81 addition
and
96 deletion
+81
-96
paddle/operators/multiplex_op.cc
paddle/operators/multiplex_op.cc
+11
-15
paddle/operators/multiplex_op.cu
paddle/operators/multiplex_op.cu
+6
-64
paddle/operators/multiplex_op.h
paddle/operators/multiplex_op.h
+64
-17
未找到文件。
paddle/operators/multiplex_op.cc
浏览文件 @
7620efdf
...
@@ -22,10 +22,7 @@ using LoDTensor = framework::LoDTensor;
...
@@ -22,10 +22,7 @@ using LoDTensor = framework::LoDTensor;
class
MultiplexOp
:
public
framework
::
OperatorWithKernel
{
class
MultiplexOp
:
public
framework
::
OperatorWithKernel
{
public:
public:
MultiplexOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorWithKernel
(
type
,
inputs
,
outputs
,
attrs
)
{}
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
...
@@ -64,12 +61,12 @@ class MultiplexOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -64,12 +61,12 @@ class MultiplexOpMaker : public framework::OpProtoAndCheckerMaker {
Multiplex multiple tensors according to the index provided by the first
Multiplex multiple tensors according to the index provided by the first
input tensor.
input tensor.
ins[0]: the index
of the tensor to output of size batchSize
.
ins[0]: the index
tensor
.
ins[1:N]: the candidate output tensor.
ins[1:N]: the candidate output tensor
s
.
For each index i from 0 to batchSize - 1, the output is the i-th row of the
For each index i from 0 to batchSize - 1, the output is the i-th row of the
the (index[i] + 1)-th tensor.
the (index[i] + 1)-th tensor.
For
each i-th row of output
:
For
i-th row of the output tensor
:
y[i][j] = x_{k}[i][j], j = 0,1, ... , (x_{1}.width - 1)
y[i][j] = x_{k}[i][j], j = 0,1, ... , (x_{1}.width - 1)
...
@@ -82,11 +79,7 @@ and `k = x{0}[i] + 1`.
...
@@ -82,11 +79,7 @@ and `k = x{0}[i] + 1`.
class
MultiplexGradOp
:
public
framework
::
OperatorWithKernel
{
class
MultiplexGradOp
:
public
framework
::
OperatorWithKernel
{
public:
public:
MultiplexGradOp
(
const
std
::
string
&
type
,
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorWithKernel
(
type
,
inputs
,
outputs
,
attrs
)
{}
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
...
@@ -98,7 +91,7 @@ class MultiplexGradOp : public framework::OperatorWithKernel {
...
@@ -98,7 +91,7 @@ class MultiplexGradOp : public framework::OperatorWithKernel {
"Input(Out@GRAD) shouldn't be null."
);
"Input(Out@GRAD) shouldn't be null."
);
auto
d_ins
=
ctx
.
MultiOutput
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
d_ins
=
ctx
.
MultiOutput
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
// don
;t compute gradient for index
// don
't compute gradient for index (ins[0])
for
(
size_t
i
=
1
;
i
<
ins
.
size
();
i
++
)
{
for
(
size_t
i
=
1
;
i
<
ins
.
size
();
i
++
)
{
if
(
d_ins
[
i
])
{
if
(
d_ins
[
i
])
{
d_ins
[
i
]
->
Resize
(
ins
[
i
]
->
dims
());
d_ins
[
i
]
->
Resize
(
ins
[
i
]
->
dims
());
...
@@ -113,5 +106,8 @@ namespace ops = paddle::operators;
...
@@ -113,5 +106,8 @@ namespace ops = paddle::operators;
REGISTER_OP
(
multiplex
,
ops
::
MultiplexOp
,
ops
::
MultiplexOpMaker
,
multiplex_grad
,
REGISTER_OP
(
multiplex
,
ops
::
MultiplexOp
,
ops
::
MultiplexOpMaker
,
multiplex_grad
,
ops
::
MultiplexGradOp
);
ops
::
MultiplexGradOp
);
REGISTER_OP_CPU_KERNEL
(
multiplex
,
ops
::
MultiplexCPUKernel
<
float
>
);
REGISTER_OP_CPU_KERNEL
(
multiplex
,
REGISTER_OP_CPU_KERNEL
(
multiplex_grad
,
ops
::
MultiplexGradCPUKernel
<
float
>
);
ops
::
MultiplexKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
multiplex_grad
,
ops
::
MultiplexGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/multiplex_op.cu
浏览文件 @
7620efdf
...
@@ -13,70 +13,12 @@
...
@@ -13,70 +13,12 @@
limitations under the License. */
limitations under the License. */
#include "paddle/framework/op_registry.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/multiplex_op.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
T
>
class
MultiplexGPUKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
// copy index to cpu
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
paddle
::
platform
::
CPUPlace
());
auto
index
=
index_t_cpu
.
data
<
T
>
();
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
int
)
index
[
i
]
+
1
;
cudaMemcpy
(
out
->
data
<
T
>
()
+
i
*
cols
,
ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
),
cudaMemcpyDeviceToDevice
);
}
}
};
template
<
typename
T
>
class
MultiplexGradGPUKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
d_out
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
d_ins
=
ctx
.
MultiOutput
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
for
(
size_t
i
=
1
;
i
<
d_ins
.
size
();
++
i
)
{
if
(
d_ins
[
i
])
{
d_ins
[
i
]
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
dims
=
d_ins
[
i
]
->
dims
();
cudaMemset
(
d_ins
[
i
]
->
data
<
T
>
(),
0
,
framework
::
product
(
dims
)
*
sizeof
(
T
));
}
}
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
// copy index to cpu
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
paddle
::
platform
::
CPUPlace
());
auto
index
=
index_t_cpu
.
data
<
T
>
();
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
int
)
index
[
i
]
+
1
;
if
(
d_ins
[
k
])
{
cudaMemcpy
(
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
d_out
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
),
cudaMemcpyDeviceToDevice
);
}
}
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
multiplex
,
ops
::
MultiplexGPUKernel
<
float
>
);
REGISTER_OP_GPU_KERNEL
(
multiplex
,
REGISTER_OP_GPU_KERNEL
(
multiplex_grad
,
ops
::
MultiplexGradGPUKernel
<
float
>
);
ops
::
MultiplexKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
multiplex_grad
,
ops
::
MultiplexGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/multiplex_op.h
浏览文件 @
7620efdf
...
@@ -17,31 +17,56 @@
...
@@ -17,31 +17,56 @@
#include "paddle/framework/eigen.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/op_registry.h"
#include "paddle/memory/memcpy.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
template
<
typename
T
>
template
<
typename
Place
,
typename
T
>
class
Multiplex
CPU
Kernel
:
public
framework
::
OpKernel
{
class
MultiplexKernel
:
public
framework
::
OpKernel
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
index
=
ins
[
0
]
->
data
<
T
>
();
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
if
(
platform
::
is_cpu_place
(
ctx
.
GetPlace
()))
{
int
k
=
(
int
)
index
[
i
]
+
1
;
auto
*
index
=
ins
[
0
]
->
data
<
T
>
();
memcpy
(
out
->
data
<
T
>
()
+
i
*
cols
,
ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
platform
::
CPUPlace
place
=
boost
::
get
<
platform
::
CPUPlace
>
(
ctx
.
GetPlace
());
cols
*
sizeof
(
T
));
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
int
)
index
[
i
]
+
1
;
PADDLE_ENFORCE_LT
(
k
,
ins
.
size
(),
"index exceeds the number of candidate tensors."
);
memory
::
Copy
(
place
,
out
->
data
<
T
>
()
+
i
*
cols
,
place
,
ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
));
}
}
else
{
#ifndef PADDLE_ONLY_CPU
// copy index to cpu
framework
::
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
platform
::
CPUPlace
());
auto
*
index
=
index_t_cpu
.
data
<
T
>
();
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
.
device_context
())
.
stream
();
platform
::
GPUPlace
place
=
boost
::
get
<
platform
::
GPUPlace
>
(
ctx
.
GetPlace
());
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
int
)
index
[
i
]
+
1
;
PADDLE_ENFORCE_LT
(
k
,
ins
.
size
(),
"index exceeds the number of candidate tensors."
);
memory
::
Copy
(
place
,
out
->
data
<
T
>
()
+
i
*
cols
,
place
,
ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
),
stream
);
}
#endif
}
}
}
}
};
};
template
<
typename
T
>
template
<
typename
Place
,
typename
T
>
class
MultiplexGrad
CPU
Kernel
:
public
framework
::
OpKernel
{
class
MultiplexGradKernel
:
public
framework
::
OpKernel
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
d_out
=
ctx
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
d_out
=
ctx
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
...
@@ -51,20 +76,42 @@ class MultiplexGradCPUKernel : public framework::OpKernel {
...
@@ -51,20 +76,42 @@ class MultiplexGradCPUKernel : public framework::OpKernel {
for
(
size_t
i
=
1
;
i
<
d_ins
.
size
();
i
++
)
{
for
(
size_t
i
=
1
;
i
<
d_ins
.
size
();
i
++
)
{
if
(
d_ins
[
i
])
{
if
(
d_ins
[
i
])
{
d_ins
[
i
]
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
d_ins
[
i
]
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
dims
=
d_ins
[
i
]
->
dims
(
);
auto
t
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
d_ins
[
i
]
);
memset
(
d_ins
[
i
]
->
data
<
T
>
(),
0
,
framework
::
product
(
dims
)
*
sizeof
(
T
));
t
.
device
(
ctx
.
GetEigenDevice
<
Place
>
())
=
t
.
constant
(
static_cast
<
T
>
(
0
));
}
}
}
}
auto
index
=
ins
[
0
]
->
data
<
T
>
();
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
if
(
platform
::
is_cpu_place
(
ctx
.
GetPlace
()))
{
int
k
=
(
int
)
index
[
i
]
+
1
;
auto
*
index
=
ins
[
0
]
->
data
<
T
>
();
if
(
d_ins
[
k
])
{
platform
::
CPUPlace
place
=
boost
::
get
<
platform
::
CPUPlace
>
(
ctx
.
GetPlace
());
memcpy
(
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
d_out
->
data
<
T
>
()
+
i
*
cols
,
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
cols
*
sizeof
(
T
));
int
k
=
(
int
)
index
[
i
]
+
1
;
if
(
d_ins
[
k
])
{
memory
::
Copy
(
place
,
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
place
,
d_out
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
));
}
}
}
else
{
#ifndef PADDLE_ONLY_CPU
// copy index to cpu
framework
::
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
platform
::
CPUPlace
());
auto
*
index
=
index_t_cpu
.
data
<
T
>
();
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
.
device_context
())
.
stream
();
platform
::
GPUPlace
place
=
boost
::
get
<
platform
::
GPUPlace
>
(
ctx
.
GetPlace
());
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
int
)
index
[
i
]
+
1
;
if
(
d_ins
[
k
])
{
memory
::
Copy
(
place
,
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
place
,
d_out
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
),
stream
);
}
}
}
#endif
}
}
}
}
};
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录