Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
fe70c69f
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看板
提交
fe70c69f
编写于
5月 09, 2018
作者:
Y
yangyaming
提交者:
fengjiayi
8月 31, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add forward and backward.
上级
cfe611f1
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
251 addition
and
0 deletion
+251
-0
paddle/fluid/operators/sequence_pad_op.cc
paddle/fluid/operators/sequence_pad_op.cc
+131
-0
paddle/fluid/operators/sequence_pad_op.cu
paddle/fluid/operators/sequence_pad_op.cu
+23
-0
paddle/fluid/operators/sequence_pad_op.h
paddle/fluid/operators/sequence_pad_op.h
+97
-0
未找到文件。
paddle/fluid/operators/sequence_pad_op.cc
0 → 100644
浏览文件 @
fe70c69f
/* Copyright (c) 2018 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/sequence_pad_op.h"
namespace
paddle
{
namespace
operators
{
class
SequencePadOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of SequencePadOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of SequencePadOp should not be null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
2
,
"Only support 2-D tensor, rank of Input(X) should be 2."
);
auto
out_dims
=
x_dims
;
if
(
ctx
->
IsRuntime
())
{
framework
::
Variable
*
x_var
=
boost
::
get
<
framework
::
Variable
*>
(
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
auto
&
x_lod
=
x_var
->
Get
<
LoDTensor
>
().
lod
();
PADDLE_ENFORCE_GE
(
x_lod
.
size
(),
1
,
"Input(X) should be sequences containing lod."
);
auto
last_level_lod
=
x_lod
[
x_lod
.
size
()
-
1
];
size_t
max_len
=
0
;
for
(
size_t
i
=
1
;
i
<
last_level_lod
.
size
();
++
i
)
{
auto
seq_len
=
last_level_lod
[
i
]
-
last_level_lod
[
i
-
1
];
max_len
=
max_len
<
seq_len
?
seq_len
:
max_len
;
}
out_dims
[
0
]
=
max_len
*
(
last_level_lod
.
size
()
-
1
);
}
else
{
framework
::
VarDesc
*
x_desc
=
boost
::
get
<
framework
::
VarDesc
*>
(
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
PADDLE_ENFORCE_GE
(
x_desc
->
GetLoDLevel
(),
1
,
"Input(X) should be sequences containing lod."
);
out_dims
[
0
]
=
-
1
;
}
ctx
->
SetOutputDim
(
"Out"
,
out_dims
);
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
return
framework
::
OpKernelType
(
framework
::
ToDataType
(
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
)
->
type
()),
ctx
.
device_context
());
}
};
class
SequencePadOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SequencePadOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"(LoDTensor, default LoDTensor<float>) Input variable which "
"should contain lod information. Length of each sequence would "
"be computed from the most bottom level lod."
);
AddOutput
(
"Out"
,
"(Tensor) Output variable which would be a common tensor "
"without lod. Each sequence would be padded to the maximum "
"length."
);
AddAttr
<
float
>
(
"pad_value"
,
"(float, default 0.0) Value to be padded "
"to the end of each sequence."
);
AddComment
(
R"DOC(
)DOC"
);
}
};
class
SequencePadGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of SequencePadGradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) of SequencePadGradOp should not be null."
);
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputDim
(
"X"
));
ctx
->
ShareLoD
(
"X"
,
/*->*/
framework
::
GradVarName
(
"X"
));
}
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
sequence_pad
,
ops
::
SequencePadOp
,
ops
::
SequencePadOpMaker
,
paddle
::
framework
::
DefaultGradOpDescMaker
<
true
>
);
REGISTER_OPERATOR
(
sequence_pad_grad
,
ops
::
SequencePadGradOp
);
REGISTER_OP_CPU_KERNEL
(
sequence_pad
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
REGISTER_OP_CPU_KERNEL
(
sequence_pad_grad
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
paddle/fluid/operators/sequence_pad_op.cu
0 → 100644
浏览文件 @
fe70c69f
/* Copyright (c) 2018 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/sequence_pad_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
sequence_pad
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
REGISTER_OP_CUDA_KERNEL
(
sequence_pad_grad
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
paddle/fluid/operators/sequence_pad_op.h
0 → 100644
浏览文件 @
fe70c69f
/* Copyright (c) 2018 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. */
#pragma once
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/operators/math/math_function.h"
namespace
paddle
{
namespace
operators
{
using
LoDTensor
=
framework
::
LoDTensor
;
using
LoD
=
framework
::
LoD
;
// @TODO clean code
template
<
typename
DeviceContext
,
typename
T
>
class
SequencePadOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x_ptr
=
ctx
.
Input
<
LoDTensor
>
(
"X"
);
auto
*
out_ptr
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
out_ptr
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
T
pad_value
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"pad_value"
));
math
::
SetConstant
<
DeviceContext
,
T
>
set_func
;
set_func
(
ctx
.
template
device_context
<
DeviceContext
>(),
out_ptr
,
pad_value
);
auto
&
x_lod
=
x_ptr
->
lod
();
auto
&
x_last_level_lod
=
x_lod
[
x_lod
.
size
()
-
1
];
auto
seq_num
=
x_last_level_lod
.
size
()
-
1
;
auto
max_len
=
out_ptr
->
dims
()[
0
]
/
seq_num
;
PADDLE_ENFORCE_EQ
(
max_len
*
seq_num
,
out_ptr
->
dims
()[
0
],
"First dimension of `Out` should be equal to "
"maximum length mulplied by sequence number."
);
for
(
size_t
i
=
1
;
i
<
x_last_level_lod
.
size
();
++
i
)
{
auto
x_start
=
x_last_level_lod
[
i
-
1
];
auto
x_end
=
x_last_level_lod
[
i
];
auto
out_start
=
(
i
-
1
)
*
max_len
;
auto
out_end
=
out_start
+
(
x_end
-
x_start
);
auto
x_sub_tensor
=
x_ptr
->
Slice
(
x_start
,
x_end
);
auto
out_sub_tensor
=
out_ptr
->
Slice
(
out_start
,
out_end
);
framework
::
TensorCopy
(
x_sub_tensor
,
ctx
.
GetPlace
(),
&
out_sub_tensor
);
}
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
SequencePadGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x_ptr
=
ctx
.
Input
<
LoDTensor
>
(
"X"
);
auto
*
g_out_ptr
=
ctx
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
g_x_ptr
=
ctx
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
math
::
SetConstant
<
DeviceContext
,
T
>
set_func
;
set_func
(
ctx
.
template
device_context
<
DeviceContext
>(),
g_x_ptr
,
static_cast
<
T
>
(
0
));
auto
&
x_lod
=
x_ptr
->
lod
();
auto
&
x_last_level_lod
=
x_lod
[
x_lod
.
size
()
-
1
];
auto
seq_num
=
x_last_level_lod
.
size
()
-
1
;
int64_t
max_len
=
g_out_ptr
->
dims
()[
0
]
/
seq_num
;
PADDLE_ENFORCE_EQ
(
max_len
*
seq_num
,
g_out_ptr
->
dims
()[
0
],
"First dimension of `Out` should be equal to "
"maximum length mulplied by sequence number."
);
for
(
size_t
i
=
1
;
i
<
x_last_level_lod
.
size
();
++
i
)
{
auto
x_start
=
x_last_level_lod
[
i
-
1
];
auto
x_end
=
x_last_level_lod
[
i
];
auto
out_start
=
(
i
-
1
)
*
max_len
;
auto
out_end
=
out_start
+
(
x_end
-
x_start
);
auto
g_out_sub
=
g_out_ptr
->
Slice
(
out_start
,
out_end
);
auto
g_x_sub
=
g_x_ptr
->
Slice
(
x_start
,
x_end
);
framework
::
TensorCopy
(
g_x_sub
,
ctx
.
GetPlace
(),
&
g_out_sub
);
}
}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录