Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
6cdaa371
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看板
未验证
提交
6cdaa371
编写于
12月 05, 2022
作者:
柠
柠檬味~
提交者:
GitHub
12月 05, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
DenseTensor (#48419)
上级
2a3ddce0
变更
19
隐藏空白更改
内联
并排
Showing
19 changed file
with
195 addition
and
203 deletion
+195
-203
paddle/fluid/operators/sequence_ops/sequence_conv_op.cc
paddle/fluid/operators/sequence_ops/sequence_conv_op.cc
+15
-14
paddle/fluid/operators/sequence_ops/sequence_conv_op.h
paddle/fluid/operators/sequence_ops/sequence_conv_op.h
+16
-18
paddle/fluid/operators/sequence_ops/sequence_conv_op_xpu.cc
paddle/fluid/operators/sequence_ops/sequence_conv_op_xpu.cc
+16
-16
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cc
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cc
+4
-4
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cu
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cu
+2
-3
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.h
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.h
+3
-4
paddle/fluid/operators/sequence_ops/sequence_erase_op.cc
paddle/fluid/operators/sequence_ops/sequence_erase_op.cc
+19
-18
paddle/fluid/operators/sequence_ops/sequence_erase_op.cu
paddle/fluid/operators/sequence_ops/sequence_erase_op.cu
+2
-3
paddle/fluid/operators/sequence_ops/sequence_expand_as_op.cc
paddle/fluid/operators/sequence_ops/sequence_expand_as_op.cc
+12
-11
paddle/fluid/operators/sequence_ops/sequence_expand_as_op.cu
paddle/fluid/operators/sequence_ops/sequence_expand_as_op.cu
+4
-6
paddle/fluid/operators/sequence_ops/sequence_expand_op.h
paddle/fluid/operators/sequence_ops/sequence_expand_op.h
+17
-17
paddle/fluid/operators/sequence_ops/sequence_mask_op.cc
paddle/fluid/operators/sequence_ops/sequence_mask_op.cc
+1
-1
paddle/fluid/operators/sequence_ops/sequence_mask_op.h
paddle/fluid/operators/sequence_ops/sequence_mask_op.h
+6
-8
paddle/fluid/operators/sequence_ops/sequence_mask_op_npu.cc
paddle/fluid/operators/sequence_ops/sequence_mask_op_npu.cc
+5
-7
paddle/fluid/operators/sequence_ops/sequence_pad_op.cc
paddle/fluid/operators/sequence_ops/sequence_pad_op.cc
+18
-16
paddle/fluid/operators/sequence_ops/sequence_pad_op.h
paddle/fluid/operators/sequence_ops/sequence_pad_op.h
+13
-13
paddle/fluid/operators/sequence_ops/sequence_pool_op.cc
paddle/fluid/operators/sequence_ops/sequence_pool_op.cc
+10
-7
paddle/fluid/operators/sequence_ops/sequence_pool_op.h
paddle/fluid/operators/sequence_ops/sequence_pool_op.h
+10
-12
paddle/fluid/operators/sequence_ops/sequence_softmax_op.h
paddle/fluid/operators/sequence_ops/sequence_softmax_op.h
+22
-25
未找到文件。
paddle/fluid/operators/sequence_ops/sequence_conv_op.cc
浏览文件 @
6cdaa371
...
@@ -145,30 +145,31 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -145,30 +145,31 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
AddInput
(
"X"
,
"X"
,
"(
LoD
Tensor) the input(X) is a LodTensor, which supports "
"(
phi::Dense
Tensor) the input(X) is a LodTensor, which supports "
"variable-time length input sequence. The underlying tensor in "
"variable-time length input sequence. The underlying tensor in "
"this
LoD
Tensor is a matrix with shape (T, N), where T is the "
"this
phi::Dense
Tensor is a matrix with shape (T, N), where T is the "
"total time steps in this mini-batch and N is the input_hidden_size."
);
"total time steps in this mini-batch and N is the input_hidden_size."
);
AddInput
(
"PaddingData"
,
AddInput
(
"(Tensor, optional) the input(PaddingData) is an optional "
"PaddingData"
,
"parameter, and it is learnable. "
"(phi::DenseTensor, optional) the input(PaddingData) is an optional "
"This is a tensor with shape (P, N), where P is the "
"parameter, and it is learnable. "
"top_pad + bottom_pad, N is the input_hidden_size. In order to "
"This is a tensor with shape (P, N), where P is the "
"ensure the equal length of sequence before and after "
"top_pad + bottom_pad, N is the input_hidden_size. In order to "
"convolution, it is necessary to fill the top and bottom of each "
"ensure the equal length of sequence before and after "
"sequence according to context_length, context_stride and "
"convolution, it is necessary to fill the top and bottom of each "
"context_start"
)
"sequence according to context_length, context_stride and "
"context_start"
)
.
AsDispensable
();
.
AsDispensable
();
AddInput
(
AddInput
(
"Filter"
,
"Filter"
,
"(Tensor) the input(Filter) is an learnable parameter."
"(
phi::Dense
Tensor) the input(Filter) is an learnable parameter."
"This is a tensor with shape (K, M), where K is the "
"This is a tensor with shape (K, M), where K is the "
"context_length * input_hidden_size, M is the output feature size."
);
"context_length * input_hidden_size, M is the output feature size."
);
AddOutput
(
AddOutput
(
"Out"
,
"Out"
,
"(
LoD
Tensor) the output(Out) is a LodTensor, which support "
"(
phi::Dense
Tensor) the output(Out) is a LodTensor, which support "
"variable-time length output sequence. The underlying tensor in "
"variable-time length output sequence. The underlying tensor in "
"this
LoD
Tensor is a matrix with shape (T, M), where, T is the "
"this
phi::Dense
Tensor is a matrix with shape (T, M), where, T is the "
"total time steps in this mini-batch, M is the output feature size."
);
"total time steps in this mini-batch, M is the output feature size."
);
AddAttr
<
bool
>
(
"paddingTrainable"
,
AddAttr
<
bool
>
(
"paddingTrainable"
,
...
...
paddle/fluid/operators/sequence_ops/sequence_conv_op.h
浏览文件 @
6cdaa371
...
@@ -22,15 +22,12 @@ limitations under the License. */
...
@@ -22,15 +22,12 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
Tensor
=
phi
::
DenseTensor
;
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
SequenceConvKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceConvKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
context
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
context
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
auto
filter
=
*
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
auto
filter
=
*
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
...
@@ -40,11 +37,11 @@ class SequenceConvKernel : public framework::OpKernel<T> {
...
@@ -40,11 +37,11 @@ class SequenceConvKernel : public framework::OpKernel<T> {
int
context_stride
=
context
.
Attr
<
int
>
(
"contextStride"
);
int
context_stride
=
context
.
Attr
<
int
>
(
"contextStride"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"paddingTrainable"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"paddingTrainable"
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
in
->
lod
().
empty
(),
in
->
lod
().
empty
()
,
false
,
false
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"Input(X)
Tensor of SequenceConvOp "
"Input(X) phi::Dense
Tensor of SequenceConvOp "
"does not contain LoD information."
));
"does not contain LoD information."
));
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
in
->
lod
().
size
(),
in
->
lod
().
size
(),
1UL
,
1UL
,
...
@@ -64,7 +61,7 @@ class SequenceConvKernel : public framework::OpKernel<T> {
...
@@ -64,7 +61,7 @@ class SequenceConvKernel : public framework::OpKernel<T> {
framework
::
DDim
col_shape
=
{
in
->
dims
()[
0
],
framework
::
DDim
col_shape
=
{
in
->
dims
()[
0
],
context_length
*
sequence_width
};
context_length
*
sequence_width
};
Tensor
col
;
phi
::
Dense
Tensor
col
;
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
// Because if padding_trainable is false, padding data should be zeros.
// Because if padding_trainable is false, padding data should be zeros.
phi
::
funcs
::
SetConstant
<
DeviceContext
,
T
>
set_zero
;
phi
::
funcs
::
SetConstant
<
DeviceContext
,
T
>
set_zero
;
...
@@ -92,13 +89,14 @@ template <typename DeviceContext, typename T>
...
@@ -92,13 +89,14 @@ template <typename DeviceContext, typename T>
class
SequenceConvGradKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceConvGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in_g
=
context
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
in_g
=
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
out_g
=
context
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
out_g
=
context
.
Input
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
filter_g
=
auto
*
filter_g
=
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Filter"
));
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Filter"
));
auto
*
padding_data_g
=
auto
*
padding_data_g
=
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"PaddingData"
));
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"PaddingData"
));
auto
*
in
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
filter
=
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
auto
*
filter
=
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
int
context_start
=
context
.
Attr
<
int
>
(
"contextStart"
);
int
context_start
=
context
.
Attr
<
int
>
(
"contextStart"
);
...
@@ -125,7 +123,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
...
@@ -125,7 +123,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
// use col_shape in the im2col calculation
// use col_shape in the im2col calculation
framework
::
DDim
col_shape
=
{
in
->
dims
()[
0
],
framework
::
DDim
col_shape
=
{
in
->
dims
()[
0
],
sequence_width
*
context_length
};
sequence_width
*
context_length
};
Tensor
col
;
phi
::
Dense
Tensor
col
;
if
(
in_g
||
filter_g
||
(
padding_trainable
&&
padding_data_g
))
{
if
(
in_g
||
filter_g
||
(
padding_trainable
&&
padding_data_g
))
{
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
...
@@ -159,7 +157,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
...
@@ -159,7 +157,7 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
padding_data_g
->
mutable_data
<
T
>
(
context
.
GetPlace
());
padding_data_g
->
mutable_data
<
T
>
(
context
.
GetPlace
());
set_zero
(
dev_ctx
,
padding_data_g
,
static_cast
<
T
>
(
0
));
set_zero
(
dev_ctx
,
padding_data_g
,
static_cast
<
T
>
(
0
));
LoDTensor
*
input
=
const_cast
<
LoD
Tensor
*>
(
in
);
phi
::
DenseTensor
*
input
=
const_cast
<
phi
::
Dense
Tensor
*>
(
in
);
seq_project_grad_functor
(
dev_ctx
,
seq_project_grad_functor
(
dev_ctx
,
*
input
,
*
input
,
padding_trainable
,
padding_trainable
,
...
@@ -178,8 +176,8 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
...
@@ -178,8 +176,8 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
filter_g
->
mutable_data
<
T
>
(
context
.
GetPlace
());
filter_g
->
mutable_data
<
T
>
(
context
.
GetPlace
());
set_zero
(
dev_ctx
,
filter_g
,
static_cast
<
T
>
(
0
));
set_zero
(
dev_ctx
,
filter_g
,
static_cast
<
T
>
(
0
));
Tensor
filter_grad
=
*
filter_g
;
phi
::
Dense
Tensor
filter_grad
=
*
filter_g
;
LoD
Tensor
out_grad
=
*
out_g
;
phi
::
Dense
Tensor
out_grad
=
*
out_g
;
const
phi
::
DenseTensor
*
padding_data
=
nullptr
;
const
phi
::
DenseTensor
*
padding_data
=
nullptr
;
if
(
padding_trainable
)
{
if
(
padding_trainable
)
{
...
...
paddle/fluid/operators/sequence_ops/sequence_conv_op_xpu.cc
浏览文件 @
6cdaa371
...
@@ -19,14 +19,13 @@ limitations under the License. */
...
@@ -19,14 +19,13 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
Tensor
=
phi
::
DenseTensor
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
SequenceConvXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceConvXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
context
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
context
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
auto
filter
=
*
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
auto
filter
=
*
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
...
@@ -36,11 +35,11 @@ class SequenceConvXPUKernel : public framework::OpKernel<T> {
...
@@ -36,11 +35,11 @@ class SequenceConvXPUKernel : public framework::OpKernel<T> {
int
context_stride
=
context
.
Attr
<
int
>
(
"contextStride"
);
int
context_stride
=
context
.
Attr
<
int
>
(
"contextStride"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"paddingTrainable"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"paddingTrainable"
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
in
->
lod
().
empty
(),
in
->
lod
().
empty
()
,
false
,
false
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"Input(X)
Tensor of SequenceConvOp "
"Input(X) phi::Dense
Tensor of SequenceConvOp "
"does not contain LoD information."
));
"does not contain LoD information."
));
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
in
->
lod
().
size
(),
in
->
lod
().
size
(),
1UL
,
1UL
,
...
@@ -159,11 +158,12 @@ template <typename DeviceContext, typename T>
...
@@ -159,11 +158,12 @@ template <typename DeviceContext, typename T>
class
SequenceConvGradXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceConvGradXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in_g
=
context
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
in_g
=
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
out_g
=
context
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
out_g
=
context
.
Input
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
filter_g
=
auto
*
filter_g
=
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Filter"
));
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Filter"
));
auto
*
in
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
filter
=
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
auto
*
filter
=
context
.
Input
<
phi
::
DenseTensor
>
(
"Filter"
);
int
context_start
=
context
.
Attr
<
int
>
(
"contextStart"
);
int
context_start
=
context
.
Attr
<
int
>
(
"contextStart"
);
...
@@ -171,11 +171,11 @@ class SequenceConvGradXPUKernel : public framework::OpKernel<T> {
...
@@ -171,11 +171,11 @@ class SequenceConvGradXPUKernel : public framework::OpKernel<T> {
int
context_stride
=
context
.
Attr
<
int
>
(
"contextStride"
);
int
context_stride
=
context
.
Attr
<
int
>
(
"contextStride"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"paddingTrainable"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"paddingTrainable"
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
in
->
lod
().
empty
(),
in
->
lod
().
empty
()
,
false
,
false
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"Input(X)
Tensor of SequenceConvOp "
"Input(X) phi::Dense
Tensor of SequenceConvOp "
"does not contain LoD information."
));
"does not contain LoD information."
));
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
in
->
lod
().
size
(),
in
->
lod
().
size
(),
1UL
,
1UL
,
...
...
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cc
浏览文件 @
6cdaa371
...
@@ -36,11 +36,11 @@ class SequenceEnumerateOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -36,11 +36,11 @@ class SequenceEnumerateOpMaker : public framework::OpProtoAndCheckerMaker {
public:
public:
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
"X"
,
AddInput
(
"X"
,
"(2-D
LoD
Tensor with the 2nd dimension equal to 1) "
"(2-D
phi::Dense
Tensor with the 2nd dimension equal to 1) "
"Input
LoD
Tensor of SequenceEnumerate operator."
);
"Input
phi::Dense
Tensor of SequenceEnumerate operator."
);
AddOutput
(
"Out"
,
AddOutput
(
"Out"
,
"(2-D
LoD
Tensor with the 2nd dimension equal to win_size) "
"(2-D
phi::Dense
Tensor with the 2nd dimension equal to win_size) "
"Output
LoD
Tensor of SequenceEnumerate operator."
);
"Output
phi::Dense
Tensor of SequenceEnumerate operator."
);
AddAttr
<
int
>
(
"win_size"
,
"(int) The enumerate sequence window size."
)
AddAttr
<
int
>
(
"win_size"
,
"(int) The enumerate sequence window size."
)
.
AddCustomChecker
([](
const
int
&
win_size
)
{
.
AddCustomChecker
([](
const
int
&
win_size
)
{
PADDLE_ENFORCE_GE
(
win_size
,
PADDLE_ENFORCE_GE
(
win_size
,
...
...
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.cu
浏览文件 @
6cdaa371
...
@@ -21,7 +21,6 @@
...
@@ -21,7 +21,6 @@
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
phi
::
PADDLE_CUDA_NUM_THREADS
;
using
phi
::
PADDLE_CUDA_NUM_THREADS
;
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
T
>
template
<
typename
T
>
__global__
void
CalcOutPut
(
const
T
*
in_data
,
__global__
void
CalcOutPut
(
const
T
*
in_data
,
...
@@ -52,8 +51,8 @@ template <typename T>
...
@@ -52,8 +51,8 @@ template <typename T>
class
SequenceEnumerateOpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceEnumerateOpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
context
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
context
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
int
win_size
=
context
.
Attr
<
int
>
(
"win_size"
);
int
win_size
=
context
.
Attr
<
int
>
(
"win_size"
);
int
pad_value
=
context
.
Attr
<
int
>
(
"pad_value"
);
int
pad_value
=
context
.
Attr
<
int
>
(
"pad_value"
);
...
...
paddle/fluid/operators/sequence_ops/sequence_enumerate_op.h
浏览文件 @
6cdaa371
...
@@ -18,14 +18,13 @@
...
@@ -18,14 +18,13 @@
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
SequenceEnumerateKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceEnumerateKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
context
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
context
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
int
win_size
=
context
.
Attr
<
int
>
(
"win_size"
);
int
win_size
=
context
.
Attr
<
int
>
(
"win_size"
);
auto
pad_value
=
static_cast
<
T
>
(
context
.
Attr
<
int
>
(
"pad_value"
));
auto
pad_value
=
static_cast
<
T
>
(
context
.
Attr
<
int
>
(
"pad_value"
));
...
@@ -33,7 +32,7 @@ class SequenceEnumerateKernel : public framework::OpKernel<T> {
...
@@ -33,7 +32,7 @@ class SequenceEnumerateKernel : public framework::OpKernel<T> {
in
->
lod
().
empty
(),
in
->
lod
().
empty
(),
false
,
false
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"Input(X) Tensor of SequenceEnumerateOp does not contain "
"Input(X)
phi::Dense
Tensor of SequenceEnumerateOp does not contain "
"LoD information."
));
"LoD information."
));
auto
in_dims
=
in
->
dims
();
auto
in_dims
=
in
->
dims
();
...
...
paddle/fluid/operators/sequence_ops/sequence_erase_op.cc
浏览文件 @
6cdaa371
...
@@ -27,20 +27,21 @@ class SequenceEraseOp : public framework::OperatorWithKernel {
...
@@ -27,20 +27,21 @@ class SequenceEraseOp : public framework::OperatorWithKernel {
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"SequenceErase"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"SequenceErase"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"SequenceErase"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"SequenceErase"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE
(
x_dims
.
size
()
==
2
&&
x_dims
[
1
]
==
1
,
PADDLE_ENFORCE
(
platform
::
errors
::
InvalidArgument
(
x_dims
.
size
()
==
2
&&
x_dims
[
1
]
==
1
,
"Input(X) of SequenceEraseOp should be a 2-D LoDTensor "
platform
::
errors
::
InvalidArgument
(
"with the 2nd dimension equal to 1,"
"Input(X) of SequenceEraseOp should be a 2-D phi::DenseTensor "
"but received size %d with the 2nd dimension %d."
,
"with the 2nd dimension equal to 1,"
x_dims
.
size
(),
"but received size %d with the 2nd dimension %d."
,
x_dims
[
1
]));
x_dims
.
size
(),
x_dims
[
1
]));
ctx
->
SetOutputDim
(
"Out"
,
x_dims
);
ctx
->
SetOutputDim
(
"Out"
,
x_dims
);
// The output
LoD
Tensor's lod_level should be input X's lod_level.
// The output
phi::Dense
Tensor's lod_level should be input X's lod_level.
// For compile-time, we call SetLoDLevel to set output's lod_level.
// For compile-time, we call SetLoDLevel to set output's lod_level.
// For runtime, output
LoDTensor's lod is determined by input X's lod an
d
// For runtime, output
phi::DenseTensor's lod is determined by input X's lo
d
//
the level specified by input RandTable.
//
and the level specified by input RandTable. We cannot get X's detail lod
//
We cannot get X's detail lod and RankTable's level in this function, so
//
and RankTable's level in this function, so leave this work to the detail
//
leave this work to the detail
kernel implementation.
// kernel implementation.
if
(
!
ctx
->
IsRuntime
())
{
if
(
!
ctx
->
IsRuntime
())
{
ctx
->
SetLoDLevel
(
"Out"
,
ctx
->
GetLoDLevel
(
"X"
));
ctx
->
SetLoDLevel
(
"Out"
,
ctx
->
GetLoDLevel
(
"X"
));
}
}
...
@@ -51,11 +52,11 @@ class SequenceEraseOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -51,11 +52,11 @@ class SequenceEraseOpMaker : public framework::OpProtoAndCheckerMaker {
public:
public:
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
"X"
,
AddInput
(
"X"
,
"(2-D
LoD
Tensor with the 2nd dim. equal to 1) "
"(2-D
phi::Dense
Tensor with the 2nd dim. equal to 1) "
"Input
LoD
Tensor of SequenceEraseOp."
);
"Input
phi::Dense
Tensor of SequenceEraseOp."
);
AddOutput
(
"Out"
,
AddOutput
(
"Out"
,
"(2-D
LoD
Tensor with the 2nd dim. equal to 1) "
"(2-D
phi::Dense
Tensor with the 2nd dim. equal to 1) "
"Output
LoD
Tensor of SequenceEraseOp."
);
"Output
phi::Dense
Tensor of SequenceEraseOp."
);
AddAttr
<
std
::
vector
<
int
>>
(
"tokens"
,
AddAttr
<
std
::
vector
<
int
>>
(
"tokens"
,
"(vector<int>) Tokens need to be erased from "
"(vector<int>) Tokens need to be erased from "
"input sequences."
);
"input sequences."
);
...
@@ -64,7 +65,7 @@ Sequence Erase Operator.
...
@@ -64,7 +65,7 @@ Sequence Erase Operator.
Sequence erase operator erases tokens specified by Attr(tokens) from the input
Sequence erase operator erases tokens specified by Attr(tokens) from the input
sequences Input(X), and outputs the remaining data and modifies the LoD
sequences Input(X), and outputs the remaining data and modifies the LoD
information at the same time. For example, given a 2-D
LoD
Tensor
information at the same time. For example, given a 2-D
phi::Dense
Tensor
X = [[2, 2, 6, 1, 3, 9, 6, 1, 0, 1]]^T
X = [[2, 2, 6, 1, 3, 9, 6, 1, 0, 1]]^T
...
@@ -77,7 +78,7 @@ operation, the three sequences become
...
@@ -77,7 +78,7 @@ operation, the three sequences become
X1' = [[6]]^T, X2' = [[1, 9]]^T and X3' = [[6, 1, 0, 1]]^T.
X1' = [[6]]^T, X2' = [[1, 9]]^T and X3' = [[6, 1, 0, 1]]^T.
Hence the
LoD
Tensor Output(Out) should be
Hence the
phi::Dense
Tensor Output(Out) should be
Out = [[6, 1, 9, 6, 1, 0, 1]]^T,
Out = [[6, 1, 9, 6, 1, 0, 1]]^T,
...
...
paddle/fluid/operators/sequence_ops/sequence_erase_op.cu
浏览文件 @
6cdaa371
...
@@ -21,7 +21,6 @@ limitations under the License. */
...
@@ -21,7 +21,6 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
phi
::
PADDLE_CUDA_NUM_THREADS
;
using
phi
::
PADDLE_CUDA_NUM_THREADS
;
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
T
>
template
<
typename
T
>
__global__
void
LabelErasedIdx
(
const
T
*
in_dat
,
__global__
void
LabelErasedIdx
(
const
T
*
in_dat
,
...
@@ -67,8 +66,8 @@ template <typename T>
...
@@ -67,8 +66,8 @@ template <typename T>
class
SequenceEraseOpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceEraseOpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
in
=
ctx
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
ctx
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
auto
lod
=
in
->
lod
();
auto
lod
=
in
->
lod
();
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
...
...
paddle/fluid/operators/sequence_ops/sequence_expand_as_op.cc
浏览文件 @
6cdaa371
...
@@ -20,8 +20,6 @@ limitations under the License. */
...
@@ -20,8 +20,6 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
LoDTensor
=
phi
::
DenseTensor
;
class
SequenceExpandAsOp
:
public
framework
::
OperatorWithKernel
{
class
SequenceExpandAsOp
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
...
@@ -49,8 +47,8 @@ class SequenceExpandAsOp : public framework::OperatorWithKernel {
...
@@ -49,8 +47,8 @@ class SequenceExpandAsOp : public framework::OperatorWithKernel {
framework
::
Variable
*
y_var
=
framework
::
Variable
*
y_var
=
PADDLE_GET
(
framework
::
Variable
*
,
ctx
->
GetInputVarPtrs
(
"Y"
)[
0
]);
PADDLE_GET
(
framework
::
Variable
*
,
ctx
->
GetInputVarPtrs
(
"Y"
)[
0
]);
auto
&
x_dim
=
x_var
->
Get
<
LoD
Tensor
>
().
dims
();
auto
&
x_dim
=
x_var
->
Get
<
phi
::
Dense
Tensor
>
().
dims
();
auto
&
y_lod
=
y_var
->
Get
<
LoD
Tensor
>
().
lod
();
auto
&
y_lod
=
y_var
->
Get
<
phi
::
Dense
Tensor
>
().
lod
();
PADDLE_ENFORCE_EQ
(
y_lod
.
size
(),
PADDLE_ENFORCE_EQ
(
y_lod
.
size
(),
1
,
1
,
...
@@ -96,13 +94,16 @@ class SequenceExpandAsOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -96,13 +94,16 @@ class SequenceExpandAsOpMaker : public framework::OpProtoAndCheckerMaker {
public:
public:
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
"X"
,
AddInput
(
"X"
,
"(LoDTensor, default LoDTensor<float>) A 2-D LoDTensor whose lod "
"(phi::DenseTensor, default phi::DenseTensor<float>) A 2-D "
"phi::DenseTensor whose lod "
"level is at most 1."
);
"level is at most 1."
);
AddInput
(
"Y"
,
AddInput
(
"Y"
,
"(LoDTensor, default LoDTensor<float>) Referred LoDTensor whose "
"(phi::DenseTensor, default phi::DenseTensor<float>) Referred "
"phi::DenseTensor whose "
"lod (specified level) is referred by Input(X)."
);
"lod (specified level) is referred by Input(X)."
);
AddOutput
(
"Out"
,
AddOutput
(
"Out"
,
"(LodTensor, default LoDTensor<float>) Output LoDTensor which is "
"(phi::DenseTensor, default phi::DenseTensor<float>) Output "
"phi::DenseTensor which is "
"generated from Input(X) by referring lod of Input(Y)."
);
"generated from Input(X) by referring lod of Input(Y)."
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
Sequence Expand As Operator.
Sequence Expand As Operator.
...
@@ -116,26 +117,26 @@ Following are cases to better explain how this works:
...
@@ -116,26 +117,26 @@ Following are cases to better explain how this works:
Case 1:
Case 1:
Given a 1-level
LoD
Tensor input(X)
Given a 1-level
phi::Dense
Tensor input(X)
X.data = [[a], [b], [c], [d]]
X.data = [[a], [b], [c], [d]]
X.dims = [4, 1]
X.dims = [4, 1]
and input(Y)
and input(Y)
Y.lod = [[0, 3, 6, 7, 8]]
Y.lod = [[0, 3, 6, 7, 8]]
ref_level: 0
ref_level: 0
then we get 1-level
LoD
Tensor
then we get 1-level
phi::Dense
Tensor
Out.lod = [[0, 3, 6, 7, 8]]
Out.lod = [[0, 3, 6, 7, 8]]
Out.data = [[a], [a], [a], [b], [b], [b], [c], [d]]
Out.data = [[a], [a], [a], [b], [b], [b], [c], [d]]
Out.dims = [8, 1]
Out.dims = [8, 1]
Case 2:
Case 2:
Given a common Tensor input(X)
Given a common
phi::Dense
Tensor input(X)
X.data = [[a, b], [c, d], [e, f]]
X.data = [[a, b], [c, d], [e, f]]
X.dims = [3, 2]
X.dims = [3, 2]
and input(Y)
and input(Y)
Y.lod = [[0, 2, 3, 6]]
Y.lod = [[0, 2, 3, 6]]
ref_level: 0
ref_level: 0
then we get a common
LoD
Tensor
then we get a common
phi::Dense
Tensor
Out.lod = [[0, 2, 3, 6]]
Out.lod = [[0, 2, 3, 6]]
Out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]]
Out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]]
Out.dims = [6, 2]
Out.dims = [6, 2]
...
...
paddle/fluid/operators/sequence_ops/sequence_expand_as_op.cu
浏览文件 @
6cdaa371
...
@@ -20,8 +20,6 @@ limitations under the License. */
...
@@ -20,8 +20,6 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
T
>
template
<
typename
T
>
static
__global__
void
sequence_expand_as_kernel
(
const
T
*
in_data
,
static
__global__
void
sequence_expand_as_kernel
(
const
T
*
in_data
,
const
size_t
*
expand_offset
,
const
size_t
*
expand_offset
,
...
@@ -69,9 +67,9 @@ template <typename T>
...
@@ -69,9 +67,9 @@ template <typename T>
struct
SequenceExpandAsFunctor
<
phi
::
GPUContext
,
T
>
{
struct
SequenceExpandAsFunctor
<
phi
::
GPUContext
,
T
>
{
void
operator
()(
void
operator
()(
const
phi
::
GPUContext
&
context
,
const
phi
::
GPUContext
&
context
,
const
LoD
Tensor
&
x
,
const
phi
::
Dense
Tensor
&
x
,
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*expand referenced lod*/
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*expand referenced lod*/
LoD
Tensor
*
out
)
{
phi
::
Dense
Tensor
*
out
)
{
int
height
=
x
.
dims
()[
0
];
int
height
=
x
.
dims
()[
0
];
int
width
=
phi
::
product
(
x
.
dims
())
/
height
;
int
width
=
phi
::
product
(
x
.
dims
())
/
height
;
...
@@ -99,9 +97,9 @@ struct SequenceExpandAsFunctor<phi::GPUContext, T> {
...
@@ -99,9 +97,9 @@ struct SequenceExpandAsFunctor<phi::GPUContext, T> {
template
<
typename
T
>
template
<
typename
T
>
struct
SequenceExpandAsGradFunctor
<
phi
::
GPUContext
,
T
>
{
struct
SequenceExpandAsGradFunctor
<
phi
::
GPUContext
,
T
>
{
void
operator
()(
const
phi
::
GPUContext
&
context
,
void
operator
()(
const
phi
::
GPUContext
&
context
,
const
LoD
Tensor
&
dout
,
const
phi
::
Dense
Tensor
&
dout
,
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*expand based lod*/
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*expand based lod*/
LoD
Tensor
*
dx
)
{
phi
::
Dense
Tensor
*
dx
)
{
int
height
=
dx
->
dims
()[
0
];
int
height
=
dx
->
dims
()[
0
];
int
width
=
phi
::
product
(
dx
->
dims
())
/
height
;
int
width
=
phi
::
product
(
dx
->
dims
())
/
height
;
...
...
paddle/fluid/operators/sequence_ops/sequence_expand_op.h
浏览文件 @
6cdaa371
...
@@ -22,7 +22,6 @@ limitations under the License. */
...
@@ -22,7 +22,6 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
T
,
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
typename
IndexType
=
Eigen
::
DenseIndex
>
...
@@ -32,30 +31,30 @@ template <typename DeviceContext, typename T>
...
@@ -32,30 +31,30 @@ template <typename DeviceContext, typename T>
struct
SequenceExpandFunctor
{
struct
SequenceExpandFunctor
{
void
operator
()(
void
operator
()(
const
DeviceContext
&
ctx
,
const
DeviceContext
&
ctx
,
const
LoD
Tensor
&
x
,
const
phi
::
Dense
Tensor
&
x
,
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
LoD
Tensor
*
out
);
phi
::
Dense
Tensor
*
out
);
};
};
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
struct
SequenceExpandGradFunctor
{
struct
SequenceExpandGradFunctor
{
void
operator
()(
void
operator
()(
const
DeviceContext
&
ctx
,
const
DeviceContext
&
ctx
,
const
LoD
Tensor
&
dout
,
const
phi
::
Dense
Tensor
&
dout
,
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
LoD
Tensor
*
dx
);
phi
::
Dense
Tensor
*
dx
);
};
};
template
<
typename
T
>
template
<
typename
T
>
struct
SequenceExpandFunctor
<
phi
::
CPUContext
,
T
>
{
struct
SequenceExpandFunctor
<
phi
::
CPUContext
,
T
>
{
void
operator
()(
void
operator
()(
const
phi
::
CPUContext
&
context
,
const
phi
::
CPUContext
&
context
,
const
LoD
Tensor
&
x
,
const
phi
::
Dense
Tensor
&
x
,
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
LoD
Tensor
*
out
)
{
phi
::
Dense
Tensor
*
out
)
{
int
out_offset
=
0
;
int
out_offset
=
0
;
int
x_item_length
=
x
.
numel
()
/
x
.
dims
()[
0
];
int
x_item_length
=
x
.
numel
()
/
x
.
dims
()[
0
];
auto
out_data
=
out
->
data
<
T
>
();
auto
out_data
=
out
->
data
<
T
>
();
...
@@ -88,9 +87,9 @@ template <typename DeviceContext, typename T>
...
@@ -88,9 +87,9 @@ template <typename DeviceContext, typename T>
class
SequenceExpandKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceExpandKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
x
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
x
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
y
=
context
.
Input
<
LoD
Tensor
>
(
"Y"
);
auto
*
y
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"Y"
);
auto
*
out
=
context
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
context
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
int
ref_level
=
context
.
Attr
<
int
>
(
"ref_level"
);
int
ref_level
=
context
.
Attr
<
int
>
(
"ref_level"
);
auto
&
x_lod
=
x
->
lod
();
auto
&
x_lod
=
x
->
lod
();
...
@@ -100,7 +99,7 @@ class SequenceExpandKernel : public framework::OpKernel<T> {
...
@@ -100,7 +99,7 @@ class SequenceExpandKernel : public framework::OpKernel<T> {
y_lod
.
empty
(),
y_lod
.
empty
(),
false
,
false
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"Input(Y) Tensor of SequenceExpandOp does not contain "
"Input(Y)
phi::Dense
Tensor of SequenceExpandOp does not contain "
"LoD information."
));
"LoD information."
));
if
(
ref_level
==
-
1
)
ref_level
=
y_lod
.
size
()
-
1
;
if
(
ref_level
==
-
1
)
ref_level
=
y_lod
.
size
()
-
1
;
...
@@ -164,10 +163,10 @@ template <typename T>
...
@@ -164,10 +163,10 @@ template <typename T>
struct
SequenceExpandGradFunctor
<
phi
::
CPUContext
,
T
>
{
struct
SequenceExpandGradFunctor
<
phi
::
CPUContext
,
T
>
{
void
operator
()(
void
operator
()(
const
phi
::
CPUContext
&
context
,
const
phi
::
CPUContext
&
context
,
const
LoD
Tensor
&
dout
,
const
phi
::
Dense
Tensor
&
dout
,
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
x_lod
,
/*expand source lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
const
framework
::
Vector
<
size_t
>&
ref_lod
,
/*expand referenced lod*/
LoD
Tensor
*
dx
)
{
phi
::
Dense
Tensor
*
dx
)
{
int
dout_offset
=
0
;
int
dout_offset
=
0
;
for
(
size_t
i
=
1
;
i
<
ref_lod
.
size
();
++
i
)
{
for
(
size_t
i
=
1
;
i
<
ref_lod
.
size
();
++
i
)
{
int
repeat_num
=
ref_lod
[
i
]
-
ref_lod
[
i
-
1
];
int
repeat_num
=
ref_lod
[
i
]
-
ref_lod
[
i
-
1
];
...
@@ -193,10 +192,11 @@ template <typename DeviceContext, typename T>
...
@@ -193,10 +192,11 @@ template <typename DeviceContext, typename T>
class
SequenceExpandGradKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceExpandGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
g_out
=
context
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
g_out
=
auto
*
x
=
context
.
Input
<
LoDTensor
>
(
"X"
);
context
.
Input
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
y
=
context
.
Input
<
LoDTensor
>
(
"Y"
);
auto
*
x
=
context
.
Input
<
phi
::
DenseTensor
>
(
"X"
);
auto
*
g_x
=
context
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y
=
context
.
Input
<
phi
::
DenseTensor
>
(
"Y"
);
auto
*
g_x
=
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"X"
));
int
ref_level
=
context
.
Attr
<
int
>
(
"ref_level"
);
int
ref_level
=
context
.
Attr
<
int
>
(
"ref_level"
);
g_x
->
mutable_data
<
T
>
(
context
.
GetPlace
());
g_x
->
mutable_data
<
T
>
(
context
.
GetPlace
());
...
...
paddle/fluid/operators/sequence_ops/sequence_mask_op.cc
浏览文件 @
6cdaa371
...
@@ -82,7 +82,7 @@ class SequenceMaskOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -82,7 +82,7 @@ class SequenceMaskOpMaker : public framework::OpProtoAndCheckerMaker {
SequenceMask Operator
SequenceMask Operator
This operator outputs a Mask according to Input(X) and Attr(maxlen).
This operator outputs a Mask according to Input(X) and Attr(maxlen).
Supposing Input(X) is a Tensor with shape [d_1, d_2, ..., d_n], the
Supposing Input(X) is a
phi::Dense
Tensor with shape [d_1, d_2, ..., d_n], the
Output(Y) is a mask with shape [d_1, d_2, ..., d_n, maxlen], where:
Output(Y) is a mask with shape [d_1, d_2, ..., d_n, maxlen], where:
Y(i_1, i_2, ..., i_n, j) = (j < X(i_1, i_2, ..., i_n))
Y(i_1, i_2, ..., i_n, j) = (j < X(i_1, i_2, ..., i_n))
...
...
paddle/fluid/operators/sequence_ops/sequence_mask_op.h
浏览文件 @
6cdaa371
...
@@ -28,9 +28,6 @@
...
@@ -28,9 +28,6 @@
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
LoDTensor
=
phi
::
DenseTensor
;
using
Tensor
=
phi
::
DenseTensor
;
template
<
typename
Tx
,
typename
Ty
>
template
<
typename
Tx
,
typename
Ty
>
struct
SequenceMaskForRangeFunctor
{
struct
SequenceMaskForRangeFunctor
{
HOSTDEVICE
SequenceMaskForRangeFunctor
(
const
Tx
*
x
,
Ty
*
y
,
int
maxlen
)
HOSTDEVICE
SequenceMaskForRangeFunctor
(
const
Tx
*
x
,
Ty
*
y
,
int
maxlen
)
...
@@ -50,8 +47,11 @@ struct SequenceMaskForRangeFunctor {
...
@@ -50,8 +47,11 @@ struct SequenceMaskForRangeFunctor {
template
<
typename
DeviceContext
,
typename
Tx
>
template
<
typename
DeviceContext
,
typename
Tx
>
struct
SequenceMaskFunctor
{
struct
SequenceMaskFunctor
{
SequenceMaskFunctor
(
SequenceMaskFunctor
(
const
DeviceContext
&
ctx
,
const
DeviceContext
&
ctx
,
const
Tx
*
x
,
Tensor
*
y
,
int
limits
,
int
maxlen
)
const
Tx
*
x
,
phi
::
DenseTensor
*
y
,
int
limits
,
int
maxlen
)
:
ctx_
(
ctx
),
x_
(
x
),
y_
(
y
),
limits_
(
limits
),
maxlen_
(
maxlen
)
{}
:
ctx_
(
ctx
),
x_
(
x
),
y_
(
y
),
limits_
(
limits
),
maxlen_
(
maxlen
)
{}
template
<
typename
Ty
>
template
<
typename
Ty
>
...
@@ -64,15 +64,13 @@ struct SequenceMaskFunctor {
...
@@ -64,15 +64,13 @@ struct SequenceMaskFunctor {
private:
private:
const
DeviceContext
&
ctx_
;
const
DeviceContext
&
ctx_
;
const
Tx
*
x_
;
const
Tx
*
x_
;
Tensor
*
y_
;
phi
::
Dense
Tensor
*
y_
;
int
limits_
;
int
limits_
;
int
maxlen_
;
int
maxlen_
;
};
};
template
<
typename
DeviceContext
,
typename
Tx
>
template
<
typename
DeviceContext
,
typename
Tx
>
class
SequenceMaskKernel
:
public
framework
::
OpKernel
<
Tx
>
{
class
SequenceMaskKernel
:
public
framework
::
OpKernel
<
Tx
>
{
using
Tensor
=
phi
::
DenseTensor
;
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
phi
::
DenseTensor
>
(
"X"
);
auto
*
x
=
ctx
.
Input
<
phi
::
DenseTensor
>
(
"X"
);
...
...
paddle/fluid/operators/sequence_ops/sequence_mask_op_npu.cc
浏览文件 @
6cdaa371
...
@@ -18,8 +18,6 @@ limitations under the License. */
...
@@ -18,8 +18,6 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
Tensor
=
phi
::
DenseTensor
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
SequenceMaskNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceMaskNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
...
@@ -58,7 +56,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
...
@@ -58,7 +56,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
auto
y_dim
=
phi
::
vectorize
<
int
>
(
x
->
dims
());
auto
y_dim
=
phi
::
vectorize
<
int
>
(
x
->
dims
());
y_dim
.
push_back
(
maxlen
);
y_dim
.
push_back
(
maxlen
);
Tensor
cast_x
;
phi
::
Dense
Tensor
cast_x
;
cast_x
.
mutable_data
<
int32_t
>
(
x
->
dims
(),
ctx
.
GetPlace
());
cast_x
.
mutable_data
<
int32_t
>
(
x
->
dims
(),
ctx
.
GetPlace
());
const
auto
&
cast1_runner
=
NpuOpRunner
(
const
auto
&
cast1_runner
=
NpuOpRunner
(
"Cast"
,
"Cast"
,
...
@@ -68,7 +66,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
...
@@ -68,7 +66,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
ConvertToNpuDtype
(
framework
::
TransToProtoVarType
(
cast_x
.
dtype
()))}});
ConvertToNpuDtype
(
framework
::
TransToProtoVarType
(
cast_x
.
dtype
()))}});
cast1_runner
.
Run
(
dev_ctx
.
stream
());
cast1_runner
.
Run
(
dev_ctx
.
stream
());
Tensor
tmp
;
phi
::
Dense
Tensor
tmp
;
tmp
.
mutable_data
<
int32_t
>
(
phi
::
make_ddim
({
maxlen
}),
ctx
.
GetPlace
());
tmp
.
mutable_data
<
int32_t
>
(
phi
::
make_ddim
({
maxlen
}),
ctx
.
GetPlace
());
NpuOpRunner
range_runner
;
NpuOpRunner
range_runner
;
range_runner
.
SetType
(
"Range"
);
range_runner
.
SetType
(
"Range"
);
...
@@ -78,7 +76,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
...
@@ -78,7 +76,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
range_runner
.
AddOutput
(
tmp
);
range_runner
.
AddOutput
(
tmp
);
range_runner
.
Run
(
dev_ctx
.
stream
());
range_runner
.
Run
(
dev_ctx
.
stream
());
Tensor
expand_tmp
;
phi
::
Dense
Tensor
expand_tmp
;
expand_tmp
.
mutable_data
<
int32_t
>
(
phi
::
make_ddim
(
y_dim
),
ctx
.
GetPlace
());
expand_tmp
.
mutable_data
<
int32_t
>
(
phi
::
make_ddim
(
y_dim
),
ctx
.
GetPlace
());
const
auto
&
expand_runner
=
const
auto
&
expand_runner
=
NpuOpRunner
(
"ExpandD"
,
{
tmp
},
{
expand_tmp
},
{{
"shape"
,
y_dim
}});
NpuOpRunner
(
"ExpandD"
,
{
tmp
},
{
expand_tmp
},
{{
"shape"
,
y_dim
}});
...
@@ -87,7 +85,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
...
@@ -87,7 +85,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
auto
x_dims
=
phi
::
vectorize
<
int
>
(
x
->
dims
());
auto
x_dims
=
phi
::
vectorize
<
int
>
(
x
->
dims
());
x_dims
.
push_back
(
1
);
x_dims
.
push_back
(
1
);
cast_x
.
Resize
(
phi
::
make_ddim
({
x_dims
}));
cast_x
.
Resize
(
phi
::
make_ddim
({
x_dims
}));
Tensor
x_tmp
;
phi
::
Dense
Tensor
x_tmp
;
x_tmp
.
mutable_data
<
int32_t
>
(
phi
::
make_ddim
(
y_dim
),
ctx
.
GetPlace
());
x_tmp
.
mutable_data
<
int32_t
>
(
phi
::
make_ddim
(
y_dim
),
ctx
.
GetPlace
());
const
auto
&
tile_runner
=
const
auto
&
tile_runner
=
NpuOpRunner
(
"TileWithAxis"
,
NpuOpRunner
(
"TileWithAxis"
,
...
@@ -96,7 +94,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
...
@@ -96,7 +94,7 @@ class SequenceMaskNPUKernel : public framework::OpKernel<T> {
{{
"axis"
,
x
->
dims
().
size
()},
{
"tiles"
,
maxlen
}});
{{
"axis"
,
x
->
dims
().
size
()},
{
"tiles"
,
maxlen
}});
tile_runner
.
Run
(
dev_ctx
.
stream
());
tile_runner
.
Run
(
dev_ctx
.
stream
());
Tensor
y_tmp
;
phi
::
Dense
Tensor
y_tmp
;
y_tmp
.
mutable_data
<
uint8_t
>
(
phi
::
make_ddim
(
y_dim
),
ctx
.
GetPlace
());
y_tmp
.
mutable_data
<
uint8_t
>
(
phi
::
make_ddim
(
y_dim
),
ctx
.
GetPlace
());
const
auto
&
less_runner
=
const
auto
&
less_runner
=
NpuOpRunner
(
"Less"
,
{
expand_tmp
,
x_tmp
},
{
y_tmp
},
{});
NpuOpRunner
(
"Less"
,
{
expand_tmp
,
x_tmp
},
{
y_tmp
},
{});
...
...
paddle/fluid/operators/sequence_ops/sequence_pad_op.cc
浏览文件 @
6cdaa371
...
@@ -69,7 +69,7 @@ class SequencePadOp : public framework::OperatorWithKernel {
...
@@ -69,7 +69,7 @@ class SequencePadOp : public framework::OperatorWithKernel {
// run time
// run time
framework
::
Variable
*
x_var
=
framework
::
Variable
*
x_var
=
PADDLE_GET
(
framework
::
Variable
*
,
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
PADDLE_GET
(
framework
::
Variable
*
,
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
const
auto
&
x_lod
=
x_var
->
Get
<
LoD
Tensor
>
().
lod
();
const
auto
&
x_lod
=
x_var
->
Get
<
phi
::
Dense
Tensor
>
().
lod
();
PADDLE_ENFORCE_EQ
(
x_lod
.
empty
(),
PADDLE_ENFORCE_EQ
(
x_lod
.
empty
(),
false
,
false
,
platform
::
errors
::
NotFound
(
platform
::
errors
::
NotFound
(
...
@@ -145,20 +145,22 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -145,20 +145,22 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
public:
public:
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
"X"
,
AddInput
(
"X"
,
"(LoDTensor, default LoDTensor<float>) Input variable which "
"(phi::DenseTensor, default phi::DenseTensor<float>) Input "
"variable which "
"should contain lod information."
);
"should contain lod information."
);
AddInput
(
"PadValue"
,
AddInput
(
"PadValue"
,
"(LoDTensor), this Tensor holds values that will be fill into "
"(phi::DenseTensor), this phi::DenseTensor holds values that will "
"be fill into "
"padded steps. It can be a scalar or a tensor whose shape equals "
"padded steps. It can be a scalar or a tensor whose shape equals "
"to time steps in sequences. If it's a scalar, it will be "
"to time steps in sequences. If it's a scalar, it will be "
"automatically broadcasted to the shape of time step."
);
"automatically broadcasted to the shape of time step."
);
AddOutput
(
AddOutput
(
"Out"
,
"Out"
,
"(phi::DenseTensor) The output vairable, which contains padded "
"(LoDTensor) The output vairable, which contains padded
sequences."
);
"
sequences."
);
AddOutput
(
AddOutput
(
"Length"
,
"Length"
,
"(phi::DenseTensor) The output vairable, which contains the "
"(LoDTensor) The output vairable, which contains the
actual length of "
"
actual length of "
"sequences before padding."
);
"sequences before padding."
);
AddAttr
<
int
>
(
AddAttr
<
int
>
(
"padded_length"
,
"padded_length"
,
"The length of padded sequences. It can be set to -1 or "
"The length of padded sequences. It can be set to -1 or "
...
@@ -179,41 +181,41 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -179,41 +181,41 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
Case 1:
Case 1:
Given a 1-level
LoD
Tensor input(X):
Given a 1-level
phi::Dense
Tensor input(X):
X.lod = [[0, 2, 5]]
X.lod = [[0, 2, 5]]
X.data = [a, b, c, d, e]
X.data = [a, b, c, d, e]
and Input(PadValue):
and Input(PadValue):
PadValue.data = [0]
PadValue.data = [0]
and attribite 'padded_length' = 4,
and attribite 'padded_length' = 4,
then we get
LoD
Tensor:
then we get
phi::Dense
Tensor:
Out.data = [[a, b, 0, 0],
Out.data = [[a, b, 0, 0],
[c, d, e, 0]]
[c, d, e, 0]]
Length.data = [2, 3]
Length.data = [2, 3]
Case 2:
Case 2:
Given a 1-level
LoD
Tensor input(X):
Given a 1-level
phi::Dense
Tensor input(X):
X.lod = [[0, 2, 5]]
X.lod = [[0, 2, 5]]
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
and Input(PadValue):
and Input(PadValue):
PadValue.data = [0]
PadValue.data = [0]
and attribite 'padded_length' = -1, which mean using the length
and attribite 'padded_length' = -1, which mean using the length
of longest input sequence(3 in this case),
of longest input sequence(3 in this case),
then we get
LoD
Tensor:
then we get
phi::Dense
Tensor:
Out.data = [[[a1, a2], [b1, b2], [0, 0]],
Out.data = [[[a1, a2], [b1, b2], [0, 0]],
[[c1, c2], [d1, d2], [e1, e2]]]
[[c1, c2], [d1, d2], [e1, e2]]]
Length.data = [2, 3]
Length.data = [2, 3]
Case 3:
Case 3:
Given a 1-level
LoD
Tensor input(X):
Given a 1-level
phi::Dense
Tensor input(X):
X.lod = [[0, 2, 5]]
X.lod = [[0, 2, 5]]
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
and Input(PadValue):
and Input(PadValue):
PadValue.data = [p1, p2]
PadValue.data = [p1, p2]
and attribite 'padded_length' = -1, which mean using the length
and attribite 'padded_length' = -1, which mean using the length
of longest input sequence(3 in this case),
of longest input sequence(3 in this case),
then we get
LoD
Tensor:
then we get
phi::Dense
Tensor:
Out.data = [[[a1, a2], [b1, b2], [p1, p2]],
Out.data = [[[a1, a2], [b1, b2], [p1, p2]],
[[c1, c2], [d1, d2], [e1, e2]]]
[[c1, c2], [d1, d2], [e1, e2]]]
Length.data = [2, 3]
Length.data = [2, 3]
...
...
paddle/fluid/operators/sequence_ops/sequence_pad_op.h
浏览文件 @
6cdaa371
...
@@ -24,25 +24,24 @@ limitations under the License. */
...
@@ -24,25 +24,24 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
LoDTensor
=
phi
::
DenseTensor
;
using
LoD
=
framework
::
LoD
;
using
LoD
=
framework
::
LoD
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
SequencePadOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequencePadOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
auto
*
x
=
ctx
.
Input
<
LoD
Tensor
>
(
"X"
);
const
auto
*
x
=
ctx
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
auto
*
len_t
=
ctx
.
Output
<
LoD
Tensor
>
(
"Length"
);
auto
*
len_t
=
ctx
.
Output
<
phi
::
Dense
Tensor
>
(
"Length"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
x
->
lod
().
empty
(),
x
->
lod
().
empty
()
,
false
,
false
,
platform
::
errors
::
NotFound
(
platform
::
errors
::
NotFound
(
"Input(X)
Tensor of SequencePadOp does not "
"Input(X) phi::Dense
Tensor of SequencePadOp does not "
"contain LoD information."
));
"contain LoD information."
));
const
auto
*
pad_value
=
ctx
.
Input
<
LoD
Tensor
>
(
"PadValue"
);
const
auto
*
pad_value
=
ctx
.
Input
<
phi
::
Dense
Tensor
>
(
"PadValue"
);
int
padded_length
=
ctx
.
Attr
<
int
>
(
"padded_length"
);
int
padded_length
=
ctx
.
Attr
<
int
>
(
"padded_length"
);
...
@@ -56,7 +55,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> {
...
@@ -56,7 +55,7 @@ class SequencePadOpKernel : public framework::OpKernel<T> {
false
,
false
,
math
::
kBatchLengthWidth
);
math
::
kBatchLengthWidth
);
LoD
Tensor
seq_len
;
phi
::
Dense
Tensor
seq_len
;
seq_len
.
Resize
(
len_t
->
dims
());
seq_len
.
Resize
(
len_t
->
dims
());
int64_t
*
len_data
=
seq_len
.
mutable_data
<
int64_t
>
(
platform
::
CPUPlace
());
int64_t
*
len_data
=
seq_len
.
mutable_data
<
int64_t
>
(
platform
::
CPUPlace
());
for
(
size_t
i
=
1
;
i
<
x
->
lod
()[
0
].
size
();
++
i
)
{
for
(
size_t
i
=
1
;
i
<
x
->
lod
()[
0
].
size
();
++
i
)
{
...
@@ -73,9 +72,10 @@ template <typename DeviceContext, typename T>
...
@@ -73,9 +72,10 @@ template <typename DeviceContext, typename T>
class
SequencePadGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequencePadGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
d_x
=
ctx
.
Output
<
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
d_x
=
ctx
.
Output
<
phi
::
Dense
Tensor
>
(
framework
::
GradVarName
(
"X"
));
if
(
d_x
)
{
if
(
d_x
)
{
const
auto
*
d_out
=
ctx
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
const
auto
*
d_out
=
ctx
.
Input
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Out"
));
d_x
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
d_x
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
int
padded_length
=
ctx
.
Attr
<
int
>
(
"padded_length"
);
int
padded_length
=
ctx
.
Attr
<
int
>
(
"padded_length"
);
...
...
paddle/fluid/operators/sequence_ops/sequence_pool_op.cc
浏览文件 @
6cdaa371
...
@@ -53,12 +53,15 @@ class SequencePoolOp : public framework::OperatorWithKernel {
...
@@ -53,12 +53,15 @@ class SequencePoolOp : public framework::OperatorWithKernel {
class
SequencePoolOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
SequencePoolOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
"X"
,
"(LoDTensor) The variable-length input of SequencePoolOp"
);
AddInput
(
"X"
,
AddOutput
(
"Out"
,
"(phi::DenseTensor) The variable-length input of SequencePoolOp"
);
"(Tensor) The output of SequencePoolOp does not contain LoD "
AddOutput
(
"information."
);
"Out"
,
"(phi::DenseTensor) The output of SequencePoolOp does not contain LoD "
"information."
);
AddOutput
(
"MaxIndex"
,
AddOutput
(
"MaxIndex"
,
"(Tensor<int>) This tensor is used for the sequence max-pooling "
"(phi::DenseTensor<int>) This tensor is used for the sequence "
"max-pooling "
"to record the max indexes."
)
"to record the max indexes."
)
.
AsIntermediate
();
.
AsIntermediate
();
AddAttr
<
bool
>
(
"is_test"
,
AddAttr
<
bool
>
(
"is_test"
,
...
@@ -92,11 +95,11 @@ The following example explains how this works:
...
@@ -92,11 +95,11 @@ The following example explains how this works:
For a mini-batch of 3 variable-length sentences,
For a mini-batch of 3 variable-length sentences,
containing 2, 3, and 2 time-steps:
containing 2, 3, and 2 time-steps:
Assume X is a [7,M,N]
LoD
Tensor, and X->lod()[0] = [0, 2, 5, 7], 7=2+3+2.
Assume X is a [7,M,N]
phi::Dense
Tensor, and X->lod()[0] = [0, 2, 5, 7], 7=2+3+2.
Besides, for the sake of simplicity, we assume M=1 and N=1,
Besides, for the sake of simplicity, we assume M=1 and N=1,
and the value of X = [[1, 3], [2, 4, 6], [5, 1]].
and the value of X = [[1, 3], [2, 4, 6], [5, 1]].
Thus, Out is a [3,1,1] Tensor without LoD information.
Thus, Out is a [3,1,1]
phi::Dense
Tensor without LoD information.
And for different pooltype, the value of Out is as follows:
And for different pooltype, the value of Out is as follows:
- AVERAGE: [2, 4, 3], where 2=(1+3)/2, 4=(2+4+6)/3, 3=(5+1)/2
- AVERAGE: [2, 4, 3], where 2=(1+3)/2, 4=(2+4+6)/3, 3=(5+1)/2
...
...
paddle/fluid/operators/sequence_ops/sequence_pool_op.h
浏览文件 @
6cdaa371
...
@@ -23,15 +23,12 @@ limitations under the License. */
...
@@ -23,15 +23,12 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
Tensor
=
phi
::
DenseTensor
;
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
SequencePoolKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequencePoolKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in
=
context
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
in
=
context
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
context
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
context
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
std
::
string
pooltype
=
context
.
Attr
<
std
::
string
>
(
"pooltype"
);
std
::
string
pooltype
=
context
.
Attr
<
std
::
string
>
(
"pooltype"
);
T
pad_value
=
static_cast
<
T
>
(
context
.
Attr
<
float
>
(
"pad_value"
));
T
pad_value
=
static_cast
<
T
>
(
context
.
Attr
<
float
>
(
"pad_value"
));
...
@@ -39,11 +36,11 @@ class SequencePoolKernel : public framework::OpKernel<T> {
...
@@ -39,11 +36,11 @@ class SequencePoolKernel : public framework::OpKernel<T> {
auto
lod
=
in
->
lod
();
auto
lod
=
in
->
lod
();
auto
lod_level
=
lod
.
size
();
auto
lod_level
=
lod
.
size
();
// InferShape by lod
// InferShape by lod
PADDLE_ENFORCE_GT
(
PADDLE_ENFORCE_GT
(
lod_level
,
lod_level
,
0
,
0
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"Input(X)
Tensor of SequencePoolOp "
"Input(X) phi::Dense
Tensor of SequencePoolOp "
"does not contain LoD information."
));
"does not contain LoD information."
));
PADDLE_ENFORCE_LE
(
lod_level
,
PADDLE_ENFORCE_LE
(
lod_level
,
2UL
,
2UL
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
...
@@ -100,8 +97,9 @@ template <typename DeviceContext, typename T>
...
@@ -100,8 +97,9 @@ template <typename DeviceContext, typename T>
class
SequencePoolGradKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequencePoolGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
out_g
=
context
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
out_g
=
auto
*
in_g
=
context
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
context
.
Input
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
in_g
=
context
.
Output
<
phi
::
DenseTensor
>
(
framework
::
GradVarName
(
"X"
));
std
::
string
pooltype
=
context
.
Attr
<
std
::
string
>
(
"pooltype"
);
std
::
string
pooltype
=
context
.
Attr
<
std
::
string
>
(
"pooltype"
);
const
phi
::
DenseTensor
*
index
=
nullptr
;
const
phi
::
DenseTensor
*
index
=
nullptr
;
if
(
pooltype
==
"MAX"
)
{
if
(
pooltype
==
"MAX"
)
{
...
...
paddle/fluid/operators/sequence_ops/sequence_softmax_op.h
浏览文件 @
6cdaa371
...
@@ -19,33 +19,30 @@ limitations under the License. */
...
@@ -19,33 +19,30 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
Tensor
=
phi
::
DenseTensor
;
using
LoDTensor
=
phi
::
DenseTensor
;
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
struct
SequenceSoftmaxFunctor
{
struct
SequenceSoftmaxFunctor
{
void
operator
()(
void
operator
()(
const
DeviceContext
&
ctx
,
const
DeviceContext
&
ctx
,
const
LoD
Tensor
&
x
,
const
phi
::
Dense
Tensor
&
x
,
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*expand referenced lod*/
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*expand referenced lod*/
LoD
Tensor
*
out
);
phi
::
Dense
Tensor
*
out
);
};
};
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
struct
SequenceSoftmaxGradFunctor
{
struct
SequenceSoftmaxGradFunctor
{
void
operator
()(
const
DeviceContext
&
ctx
,
void
operator
()(
const
DeviceContext
&
ctx
,
const
LoD
Tensor
&
dout
,
const
phi
::
Dense
Tensor
&
dout
,
const
LoD
Tensor
&
out
,
const
phi
::
Dense
Tensor
&
out
,
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*referenced lod*/
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*referenced lod*/
LoD
Tensor
*
dx
);
phi
::
Dense
Tensor
*
dx
);
};
};
template
<
typename
T
>
template
<
typename
T
>
struct
SequenceSoftmaxFunctor
<
phi
::
CPUContext
,
T
>
{
struct
SequenceSoftmaxFunctor
<
phi
::
CPUContext
,
T
>
{
void
operator
()(
const
phi
::
CPUContext
&
ctx
,
void
operator
()(
const
phi
::
CPUContext
&
ctx
,
const
LoD
Tensor
&
x
,
const
phi
::
Dense
Tensor
&
x
,
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*referenced lod*/
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*referenced lod*/
LoD
Tensor
*
out
)
{
phi
::
Dense
Tensor
*
out
)
{
size_t
height
=
ref_lod
.
size
()
-
1
;
size_t
height
=
ref_lod
.
size
()
-
1
;
const
T
*
in_data
=
x
.
data
<
T
>
();
const
T
*
in_data
=
x
.
data
<
T
>
();
T
*
out_data
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
T
*
out_data
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
...
@@ -65,10 +62,10 @@ struct SequenceSoftmaxFunctor<phi::CPUContext, T> {
...
@@ -65,10 +62,10 @@ struct SequenceSoftmaxFunctor<phi::CPUContext, T> {
template
<
typename
T
>
template
<
typename
T
>
struct
SequenceSoftmaxGradFunctor
<
phi
::
CPUContext
,
T
>
{
struct
SequenceSoftmaxGradFunctor
<
phi
::
CPUContext
,
T
>
{
void
operator
()(
const
phi
::
CPUContext
&
ctx
,
void
operator
()(
const
phi
::
CPUContext
&
ctx
,
const
LoD
Tensor
&
dout
,
const
phi
::
Dense
Tensor
&
dout
,
const
LoD
Tensor
&
out
,
const
phi
::
Dense
Tensor
&
out
,
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*referenced lod*/
const
framework
::
Vector
<
size_t
>
&
ref_lod
,
/*referenced lod*/
LoD
Tensor
*
dx
)
{
phi
::
Dense
Tensor
*
dx
)
{
size_t
height
=
ref_lod
.
size
()
-
1
;
size_t
height
=
ref_lod
.
size
()
-
1
;
const
T
*
softmax_grad_data
=
dout
.
data
<
T
>
();
const
T
*
softmax_grad_data
=
dout
.
data
<
T
>
();
...
@@ -94,17 +91,17 @@ template <typename DeviceContext, typename T>
...
@@ -94,17 +91,17 @@ template <typename DeviceContext, typename T>
class
SequenceSoftmaxKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceSoftmaxKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
x
=
ctx
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
phi
::
Dense
Tensor
>
(
"Out"
);
auto
lod
=
x
->
lod
();
auto
lod
=
x
->
lod
();
auto
dims
=
x
->
dims
();
auto
dims
=
x
->
dims
();
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
lod
.
empty
(),
lod
.
empty
()
,
false
,
false
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"Input(X) phi::DenseTensor of SequenceSoftmax "
"Input(X) Tensor of SequenceSoftmax
operator does not contain "
"
operator does not contain "
"LoD information."
));
"LoD information."
));
const
size_t
level
=
lod
.
size
()
-
1
;
const
size_t
level
=
lod
.
size
()
-
1
;
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
...
@@ -138,10 +135,10 @@ template <typename DeviceContext, typename T>
...
@@ -138,10 +135,10 @@ template <typename DeviceContext, typename T>
class
SequenceSoftmaxGradKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SequenceSoftmaxGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
out
=
ctx
.
Input
<
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Input
<
phi
::
Dense
Tensor
>
(
"Out"
);
auto
*
out_grad
=
ctx
.
Input
<
LoD
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
out_grad
=
ctx
.
Input
<
phi
::
Dense
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
x
=
ctx
.
Input
<
LoD
Tensor
>
(
"X"
);
auto
*
x
=
ctx
.
Input
<
phi
::
Dense
Tensor
>
(
"X"
);
auto
*
x_grad
=
ctx
.
Output
<
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
x_grad
=
ctx
.
Output
<
phi
::
Dense
Tensor
>
(
framework
::
GradVarName
(
"X"
));
if
(
!
x_grad
)
{
if
(
!
x_grad
)
{
return
;
return
;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录