Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
f94109d4
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f94109d4
编写于
9月 25, 2017
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
replace LoDTensor in multiplex_op
上级
47fbc96f
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
22 addition
and
23 deletion
+22
-23
paddle/operators/multiplex_op.cc
paddle/operators/multiplex_op.cc
+8
-9
paddle/operators/multiplex_op.cu
paddle/operators/multiplex_op.cu
+11
-11
paddle/operators/multiplex_op.h
paddle/operators/multiplex_op.h
+3
-3
未找到文件。
paddle/operators/multiplex_op.cc
浏览文件 @
f94109d4
...
@@ -18,7 +18,6 @@ namespace paddle {
...
@@ -18,7 +18,6 @@ namespace paddle {
namespace
operators
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
class
MultiplexOp
:
public
framework
::
OperatorWithKernel
{
class
MultiplexOp
:
public
framework
::
OperatorWithKernel
{
public:
public:
...
@@ -27,11 +26,11 @@ class MultiplexOp : public framework::OperatorWithKernel {
...
@@ -27,11 +26,11 @@ class MultiplexOp : public framework::OperatorWithKernel {
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
!
ctx
.
MultiInputVar
(
"X"
).
empty
(),
PADDLE_ENFORCE
(
!
ctx
.
MultiInputVar
(
"X"
).
empty
(),
"Input(X) should not be null"
);
"Input(X) should not be null
.
"
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Out"
),
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Out"
),
"Output(Out) shouldn't be null."
);
"Output(Out) shouldn't be null."
);
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
num_ins
=
ins
.
size
();
auto
num_ins
=
ins
.
size
();
PADDLE_ENFORCE
(
num_ins
>
2
,
PADDLE_ENFORCE
(
num_ins
>
2
,
"multiplex operator should have more than 2 inputs."
);
"multiplex operator should have more than 2 inputs."
);
...
@@ -41,9 +40,9 @@ class MultiplexOp : public framework::OperatorWithKernel {
...
@@ -41,9 +40,9 @@ class MultiplexOp : public framework::OperatorWithKernel {
for
(
size_t
i
=
2
;
i
<
num_ins
;
i
++
)
{
for
(
size_t
i
=
2
;
i
<
num_ins
;
i
++
)
{
auto
dim
=
ins
[
i
]
->
dims
();
auto
dim
=
ins
[
i
]
->
dims
();
PADDLE_ENFORCE
(
PADDLE_ENFORCE
(
in_dim
==
dim
,
in_dim
==
dim
,
"All the input tensors except the first one must have the "
"All the input tensors except the first one must have the same size
"
);
"same size.
"
);
}
}
out
->
Resize
(
in_dim
);
out
->
Resize
(
in_dim
);
}
}
...
@@ -84,12 +83,12 @@ class MultiplexGradOp : public framework::OperatorWithKernel {
...
@@ -84,12 +83,12 @@ class MultiplexGradOp : public framework::OperatorWithKernel {
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
!
ctx
.
MultiInputVar
(
"X"
).
empty
(),
PADDLE_ENFORCE
(
!
ctx
.
MultiInputVar
(
"X"
).
empty
(),
"Input(X) should not be null"
);
"Input(X) should not be null
.
"
);
PADDLE_ENFORCE
(
!
ctx
.
MultiOutputVar
(
framework
::
GradVarName
(
"X"
)).
empty
(),
PADDLE_ENFORCE
(
!
ctx
.
MultiOutputVar
(
framework
::
GradVarName
(
"X"
)).
empty
(),
"Output(X@Grad) should not be null"
);
"Output(X@Grad) should not be null
.
"
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) shouldn't be null."
);
"Input(Out@GRAD) shouldn't be null."
);
auto
d_ins
=
ctx
.
MultiOutput
<
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
d_ins
=
ctx
.
MultiOutput
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
// don't compute gradient for index (ins[0])
// 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
++
)
{
...
...
paddle/operators/multiplex_op.cu
浏览文件 @
f94109d4
...
@@ -18,19 +18,20 @@
...
@@ -18,19 +18,20 @@
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
Place
,
typename
T
>
template
<
typename
Place
,
typename
T
>
class
MultiplexGPUKernel
:
public
framework
::
OpKernel
{
class
MultiplexGPUKernel
:
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
<
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
// copy index to cpu
// copy index to cpu
framework
::
Tensor
index_t_cpu
;
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
platform
::
CPUPlace
());
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
platform
::
CPUPlace
());
auto
*
index
=
index_t_cpu
.
data
<
T
>
();
auto
*
index
=
index_t_cpu
.
data
<
T
>
();
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
...
@@ -38,7 +39,7 @@ class MultiplexGPUKernel : public framework::OpKernel {
...
@@ -38,7 +39,7 @@ class MultiplexGPUKernel : public framework::OpKernel {
.
stream
();
.
stream
();
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
in
t
)
index
[
i
]
+
1
;
size_t
k
=
(
size_
t
)
index
[
i
]
+
1
;
PADDLE_ENFORCE_LT
(
k
,
ins
.
size
(),
PADDLE_ENFORCE_LT
(
k
,
ins
.
size
(),
"index exceeds the number of candidate tensors."
);
"index exceeds the number of candidate tensors."
);
memory
::
Copy
(
place
,
out
->
data
<
T
>
()
+
i
*
cols
,
place
,
memory
::
Copy
(
place
,
out
->
data
<
T
>
()
+
i
*
cols
,
place
,
...
@@ -51,10 +52,9 @@ template <typename Place, typename T>
...
@@ -51,10 +52,9 @@ template <typename Place, typename T>
class
MultiplexGradGPUKernel
:
public
framework
::
OpKernel
{
class
MultiplexGradGPUKernel
:
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
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
ins
=
ctx
.
MultiInput
<
Tensor
>
(
"X"
);
auto
d_ins
=
auto
d_ins
=
ctx
.
MultiOutput
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
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
());
...
@@ -66,7 +66,7 @@ class MultiplexGradGPUKernel : public framework::OpKernel {
...
@@ -66,7 +66,7 @@ class MultiplexGradGPUKernel : public framework::OpKernel {
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
rows
=
ins
[
1
]
->
dims
()[
0
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
auto
cols
=
ins
[
1
]
->
dims
()[
1
];
// copy index to cpu
// copy index to cpu
framework
::
Tensor
index_t_cpu
;
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
platform
::
CPUPlace
());
index_t_cpu
.
CopyFrom
<
T
>
(
*
(
ins
[
0
]),
platform
::
CPUPlace
());
auto
*
index
=
index_t_cpu
.
data
<
T
>
();
auto
*
index
=
index_t_cpu
.
data
<
T
>
();
...
@@ -75,7 +75,7 @@ class MultiplexGradGPUKernel : public framework::OpKernel {
...
@@ -75,7 +75,7 @@ class MultiplexGradGPUKernel : public framework::OpKernel {
.
stream
();
.
stream
();
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
in
t
)
index
[
i
]
+
1
;
size_t
k
=
(
size_
t
)
index
[
i
]
+
1
;
if
(
d_ins
[
k
])
{
if
(
d_ins
[
k
])
{
memory
::
Copy
(
place
,
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
place
,
memory
::
Copy
(
place
,
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
place
,
d_out
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
),
stream
);
d_out
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
),
stream
);
...
...
paddle/operators/multiplex_op.h
浏览文件 @
f94109d4
...
@@ -27,7 +27,7 @@ class MultiplexCPUKernel : public framework::OpKernel {
...
@@ -27,7 +27,7 @@ class MultiplexCPUKernel : 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
::
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
...
@@ -36,7 +36,7 @@ class MultiplexCPUKernel : public framework::OpKernel {
...
@@ -36,7 +36,7 @@ class MultiplexCPUKernel : public framework::OpKernel {
auto
*
index
=
ins
[
0
]
->
data
<
T
>
();
auto
*
index
=
ins
[
0
]
->
data
<
T
>
();
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
in
t
)
index
[
i
]
+
1
;
size_t
k
=
(
size_
t
)
index
[
i
]
+
1
;
PADDLE_ENFORCE_LT
(
k
,
ins
.
size
(),
PADDLE_ENFORCE_LT
(
k
,
ins
.
size
(),
"index exceeds the number of candidate tensors."
);
"index exceeds the number of candidate tensors."
);
memory
::
Copy
(
place
,
out
->
data
<
T
>
()
+
i
*
cols
,
place
,
memory
::
Copy
(
place
,
out
->
data
<
T
>
()
+
i
*
cols
,
place
,
...
@@ -66,7 +66,7 @@ class MultiplexGradCPUKernel : public framework::OpKernel {
...
@@ -66,7 +66,7 @@ class MultiplexGradCPUKernel : public framework::OpKernel {
auto
*
index
=
ins
[
0
]
->
data
<
T
>
();
auto
*
index
=
ins
[
0
]
->
data
<
T
>
();
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
Place
place
=
boost
::
get
<
Place
>
(
ctx
.
GetPlace
());
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
for
(
auto
i
=
0
;
i
<
rows
;
i
++
)
{
int
k
=
(
in
t
)
index
[
i
]
+
1
;
size_t
k
=
(
size_
t
)
index
[
i
]
+
1
;
if
(
d_ins
[
k
])
{
if
(
d_ins
[
k
])
{
memory
::
Copy
(
place
,
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
place
,
memory
::
Copy
(
place
,
d_ins
[
k
]
->
data
<
T
>
()
+
i
*
cols
,
place
,
d_out
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
));
d_out
->
data
<
T
>
()
+
i
*
cols
,
cols
*
sizeof
(
T
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录