Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
c3de69a9
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c3de69a9
编写于
8月 17, 2018
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Complete sequence_pad_op and its CPU kernel. Add unittests
上级
17152510
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
234 addition
and
135 deletion
+234
-135
paddle/fluid/operators/math/sequence_padding.cc
paddle/fluid/operators/math/sequence_padding.cc
+17
-7
paddle/fluid/operators/math/sequence_padding.h
paddle/fluid/operators/math/sequence_padding.h
+0
-3
paddle/fluid/operators/sequence_pad_op.cc
paddle/fluid/operators/sequence_pad_op.cc
+54
-51
paddle/fluid/operators/sequence_pad_op.cu
paddle/fluid/operators/sequence_pad_op.cu
+8
-2
paddle/fluid/operators/sequence_pad_op.h
paddle/fluid/operators/sequence_pad_op.h
+21
-72
python/paddle/fluid/tests/unittests/test_sequence_pad_op.py
python/paddle/fluid/tests/unittests/test_sequence_pad_op.py
+134
-0
未找到文件。
paddle/fluid/operators/math/sequence_padding.cc
浏览文件 @
c3de69a9
...
@@ -70,9 +70,10 @@ class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
...
@@ -70,9 +70,10 @@ class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
std
::
vector
<
T
>
pad_value
=
{
0
},
int
pad_seq_len
=
-
1
,
std
::
vector
<
T
>
pad_value
=
{
0
},
int
pad_seq_len
=
-
1
,
int
lod_level
=
0
,
bool
norm_by_times
=
false
,
int
lod_level
=
0
,
bool
norm_by_times
=
false
,
const
PadLayout
layout
=
kBatchLengthWidth
)
{
const
PadLayout
layout
=
kBatchLengthWidth
)
{
auto
seq_offsets
=
framework
::
ToAbsOffset
(
seq_tensor
.
lod
())[
lod_level
];
auto
seq_lod
=
seq_tensor
.
lod
();
auto
seq_tensor_dims
=
seq_tensor
.
dims
();
const
auto
seq_offsets
=
framework
::
ToAbsOffset
(
seq_lod
)[
lod_level
];
auto
pad_tensor_dims
=
pad_tensor
->
dims
();
const
auto
&
seq_tensor_dims
=
seq_tensor
.
dims
();
const
auto
&
pad_tensor_dims
=
pad_tensor
->
dims
();
if
(
pad_seq_len
==
-
1
)
{
if
(
pad_seq_len
==
-
1
)
{
pad_seq_len
=
MaximumSequenceLength
(
seq_offsets
);
pad_seq_len
=
MaximumSequenceLength
(
seq_offsets
);
}
}
...
@@ -91,12 +92,21 @@ class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
...
@@ -91,12 +92,21 @@ class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
// fill padding value
// fill padding value
T
*
pad_data
=
pad_tensor
->
data
<
T
>
();
T
*
pad_data
=
pad_tensor
->
data
<
T
>
();
for
(
int
i
=
0
;
i
<
pad_tensor
->
numel
()
/
step_width
;
++
i
)
{
for
(
int
i
=
0
;
i
<
pad_tensor
->
numel
()
;
i
+=
step_width
)
{
memcpy
(
pad_data
,
pad_value
.
data
(),
step_width
*
sizeof
(
T
));
memcpy
(
pad_data
+
i
,
pad_value
.
data
(),
step_width
*
sizeof
(
T
));
}
}
CopyValidData
<
T
>
(
pad_tensor
,
&
seq_tensor
,
seq_offsets
,
pad_seq_len
,
CopyValidData
<
T
>
(
pad_tensor
,
&
seq_tensor
,
seq_offsets
,
pad_seq_len
,
step_width
,
norm_by_times
,
kSeqToPad
,
layout
);
step_width
,
norm_by_times
,
kSeqToPad
,
layout
);
// Set pad_tensor's lod info if possible
if
(
layout
==
kBatchLengthWidth
)
{
framework
::
LoD
pad_lod
(
seq_lod
.
begin
()
+
lod_level
,
seq_lod
.
end
());
for
(
size_t
i
=
0
;
i
<
pad_lod
[
0
].
size
();
++
i
)
{
pad_lod
[
0
][
i
]
=
i
*
pad_seq_len
;
}
pad_tensor
->
set_lod
(
pad_lod
);
}
}
}
};
};
...
@@ -109,8 +119,8 @@ class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
...
@@ -109,8 +119,8 @@ class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
int
lod_level
=
0
,
bool
norm_by_times
=
false
,
int
lod_level
=
0
,
bool
norm_by_times
=
false
,
const
PadLayout
&
layout
=
kBatchLengthWidth
)
{
const
PadLayout
&
layout
=
kBatchLengthWidth
)
{
auto
seq_offsets
=
framework
::
ToAbsOffset
(
seq_tensor
->
lod
())[
lod_level
];
auto
seq_offsets
=
framework
::
ToAbsOffset
(
seq_tensor
->
lod
())[
lod_level
];
auto
seq_tensor_dims
=
seq_tensor
->
dims
();
const
auto
&
seq_tensor_dims
=
seq_tensor
->
dims
();
auto
pad_tensor_dims
=
pad_tensor
.
dims
();
const
auto
&
pad_tensor_dims
=
pad_tensor
.
dims
();
if
(
pad_seq_len
==
-
1
)
{
if
(
pad_seq_len
==
-
1
)
{
pad_seq_len
=
MaximumSequenceLength
(
seq_offsets
);
pad_seq_len
=
MaximumSequenceLength
(
seq_offsets
);
}
}
...
...
paddle/fluid/operators/math/sequence_padding.h
浏览文件 @
c3de69a9
...
@@ -44,9 +44,6 @@ inline static void CheckDims(const framework::DDim& seq_tensor_dims,
...
@@ -44,9 +44,6 @@ inline static void CheckDims(const framework::DDim& seq_tensor_dims,
"Value of 1st dimension of the sequence tensor should be "
"Value of 1st dimension of the sequence tensor should be "
"equal to sum of lengths of all sequences."
);
"equal to sum of lengths of all sequences."
);
PADDLE_ENFORCE
(
seq_tensor_dims
.
size
()
==
1
||
seq_tensor_dims
.
size
()
==
2
,
"seq_tensor's rank should be 1 or 2."
);
PADDLE_ENFORCE
(
seq_tensor_dims
.
size
()
+
1
==
pad_tensor_dims
.
size
()
||
PADDLE_ENFORCE
(
seq_tensor_dims
.
size
()
+
1
==
pad_tensor_dims
.
size
()
||
seq_tensor_dims
.
size
()
==
pad_tensor_dims
.
size
(),
seq_tensor_dims
.
size
()
==
pad_tensor_dims
.
size
(),
"pad_tensor's rank should be 1 greater than seq_tensor's "
"pad_tensor's rank should be 1 greater than seq_tensor's "
...
...
paddle/fluid/operators/sequence_pad_op.cc
浏览文件 @
c3de69a9
...
@@ -21,82 +21,85 @@ class SequencePadOp : public framework::OperatorWithKernel {
...
@@ -21,82 +21,85 @@ class SequencePadOp : public framework::OperatorWithKernel {
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of SequencePadOp should not be null."
);
"Input(X) of SequencePadOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"PadValue"
),
"Input(PadValue) of SequencePadOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of SequencePadOp should not be null."
);
"Output(Out) of SequencePadOp should not be null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_GE
(
x_dims
.
size
(),
2
,
"The rank of Input(x) can't be less than 2."
);
auto
time_step_dims
=
framework
::
slice_ddim
(
x_dims
,
1
,
x_dims
.
size
());
auto
pad_value_dims
=
ctx
->
GetInputDim
(
"PadValue"
);
PADDLE_ENFORCE
(
pad_value_dims
==
framework
::
make_ddim
({
1
})
||
pad_value_dims
==
time_step_dims
,
"The Input(PadValue) must be a scalar or a tensor whose "
"shape equals to time steps in sequences"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
2
,
int
batch_dim_size
=
-
1
;
"Only support 2-D tensor, rank of Input(X) should be 2."
);
int
lod_level
=
ctx
->
Attrs
().
Get
<
int
>
(
"lod_level"
);
int64_t
max_len
=
-
1
;
int64_t
seq_num
=
-
1
;
int
x_lod_size
=
-
1
;
if
(
ctx
->
IsRuntime
())
{
if
(
ctx
->
IsRuntime
())
{
// run time
framework
::
Variable
*
x_var
=
framework
::
Variable
*
x_var
=
boost
::
get
<
framework
::
Variable
*>
(
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
boost
::
get
<
framework
::
Variable
*>
(
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
const
auto
&
x_lod
=
x_var
->
Get
<
LoDTensor
>
().
lod
();
auto
&
x_lod
=
x_var
->
Get
<
LoDTensor
>
().
lod
();
PADDLE_ENFORCE
(
!
x_lod
.
empty
(),
"The Input(X) must hold lod info."
);
const
auto
&
x_lod_0
=
x_lod
[
0
];
x_lod_size
=
x_lod
.
size
();
PADDLE_ENFORCE_GE
(
x_lod_0
.
size
(),
2
,
"The Input(X)'s lod info is corrupted."
);
auto
x_abs_offset
=
framework
::
ToAbsOffset
(
x_lod
)[
lod_level
];
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
static_cast
<
int64_t
>
(
x_lod_0
.
back
()),
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
static_cast
<
int64_t
>
(
x_abs_offset
.
back
()),
"The Input(X)'s lod info mismatches the actual tensor shape."
);
"The first dimension of `X` should be equal to sum "
"of all sequences' length."
);
int
seq_num
=
x_lod_0
.
size
()
-
1
;
int
max_seq_len
=
math
::
MaximumSequenceLength
(
x_lod_0
);
seq_num
=
x_abs_offset
.
size
()
-
1
;
int
padded_length
=
ctx
->
Attrs
().
Get
<
int
>
(
"padded_length"
);
if
(
padded_length
==
-
1
)
{
for
(
int64_t
i
=
1
;
i
<=
seq_num
;
++
i
)
{
padded_length
=
max_seq_len
;
int64_t
seq_len
=
x_abs_offset
[
i
]
-
x_abs_offset
[
i
-
1
];
max_len
=
max_len
<
seq_len
?
seq_len
:
max_len
;
}
}
PADDLE_ENFORCE_GE
(
padded_length
,
max_seq_len
,
"The Attr(padded_length) must be -1 or an int greater "
"than the length of the longest original sequence."
);
batch_dim_size
=
padded_length
*
seq_num
;
}
else
{
}
else
{
// compile time
framework
::
VarDesc
*
x_desc
=
framework
::
VarDesc
*
x_desc
=
boost
::
get
<
framework
::
VarDesc
*>
(
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
boost
::
get
<
framework
::
VarDesc
*>
(
ctx
->
GetInputVarPtrs
(
"X"
)[
0
]);
x_lod_size
=
x_desc
->
GetLoDLevel
();
PADDLE_ENFORCE_GE
(
x_desc
->
GetLoDLevel
(),
1
);
}
PADDLE_ENFORCE
(
lod_level
>=
0
&&
lod_level
<
x_lod_size
,
"Invalid `lod_level` which should be at least 0 and less "
"than maximum lod level of `X`"
);
ctx
->
SetOutputDim
(
"Out"
,
{
seq_num
,
max_len
,
x_dims
[
1
]});
}
}
protected:
auto
out_dims
=
x_dims
;
framework
::
OpKernelType
GetExpectedKernelType
(
out_dims
[
0
]
=
batch_dim_size
;
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
ctx
->
SetOutputDim
(
"Out"
,
out_dims
);
return
framework
::
OpKernelType
(
framework
::
ToDataType
(
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
)
->
type
()),
ctx
.
device_context
());
}
}
};
};
class
SequencePadOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
SequencePadOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
SequencePadOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
void
Make
()
override
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
AddInput
(
"X"
,
"(LoDTensor, default LoDTensor<float>) Input variable which "
"(LoDTensor, default LoDTensor<float>) Input variable which "
"should contain lod information. Length of each sequence would "
"should contain lod information."
);
"be computed from the most bottom level lod."
);
AddInput
(
"PadValue"
,
AddOutput
(
"Out"
,
"(LoDTensor), this Tensor holds values that will be fill into "
"(Tensor) Output variable which would be a common tensor "
"padded steps. It can be a scalar or a tensor whose shape equals "
"without lod. Each sequence would be padded to the maximum "
"to time steps in sequences. If it's a scalar, it will be "
"length."
);
"automatically broadcasted to the shape of time step."
);
AddAttr
<
float
>
(
"lod_level"
,
AddOutput
(
"(int, default 0) Specify which level lod to referred to."
);
"Out"
,
AddAttr
<
float
>
(
"pad_value"
,
"(LoDTensor) The output vairable, which contains padded sequences."
);
"(float, default 0.0) Specify which value to be padded to "
AddAttr
<
int
>
(
"the end of each sequence."
);
"padded_length"
,
"The length of padded sequences. It can be setted to -1 or "
"any positive int. When it is -1, all sequences will be padded up to "
"the length of the longest one among them; when it a certain positive "
"value, it must be greater than the length of the longest original "
"sequence."
)
.
SetDefault
(
-
1
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
)DOC"
);
)DOC"
);
...
...
paddle/fluid/operators/sequence_pad_op.cu
浏览文件 @
c3de69a9
...
@@ -17,7 +17,13 @@ limitations under the License. */
...
@@ -17,7 +17,13 @@ limitations under the License. */
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
REGISTER_OP_CUDA_KERNEL
(
sequence_pad
,
sequence_pad
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
SequencePadOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
);
REGISTER_OP_CUDA_KERNEL
(
REGISTER_OP_CUDA_KERNEL
(
sequence_pad_grad
,
sequence_pad_grad
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
SequencePadGradOpKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
);
paddle/fluid/operators/sequence_pad_op.h
浏览文件 @
c3de69a9
...
@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
...
@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#pragma once
#pragma once
#include <vector>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/math_function.h"
...
@@ -24,68 +26,24 @@ namespace operators {
...
@@ -24,68 +26,24 @@ namespace operators {
using
LoDTensor
=
framework
::
LoDTensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
using
LoD
=
framework
::
LoD
;
using
LoD
=
framework
::
LoD
;
template
<
typename
DeviceContext
,
typename
T
>
struct
CopyFunctor
{
LoDTensor
*
lod_tensor_
;
LoDTensor
*
pad_tensor_
;
const
LoD
&
ref_lod_
;
const
DeviceContext
&
ctx_
;
bool
is_lod_to_pad_
;
CopyFunctor
(
LoDTensor
*
lod_tensor
,
const
LoD
&
ref_lod
,
LoDTensor
*
pad_tensor
,
const
DeviceContext
&
ctx
,
bool
is_lod_to_pad
)
:
lod_tensor_
(
lod_tensor
),
pad_tensor_
(
pad_tensor
),
ref_lod_
(
ref_lod
),
ctx_
(
ctx
),
is_lod_to_pad_
(
is_lod_to_pad
)
{}
void
operator
()()
const
{
/*
auto seq_num = ref_lod_.size() - 1;
auto max_len = pad_tensor_->dims()[0] / seq_num;
PADDLE_ENFORCE_EQ(max_len * seq_num, pad_tensor_->dims()[0],
"First dimension of padded tensor should be equal to "
"maximum sequence length mulplied by sequence number.");
for (size_t i = 1; i < ref_lod_.size(); ++i) {
auto seq_start = ref_lod_[i - 1];
auto seq_end = ref_lod_[i];
auto pad_start = (i - 1) * max_len;
auto pad_end = pad_start + (seq_end - seq_start);
auto sub_lod_tensor = lod_tensor_->Slice(seq_start, seq_end);
auto sub_pad_tensor = pad_tensor_->Slice(pad_start, pad_end);
if (is_lod_to_pad_) {
framework::TensorCopy(sub_lod_tensor, ctx.GetPlace(), &sub_pad_tensor);
} else {
framework::TensorCopy(sub_pad_tensor, ctx.GetPlace(), &sub_lod_tensor);
}
}
*/
}
};
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
<
LoDTensor
>
(
"X"
);
auto* x = ctx.Input<LoDTensor>("X");
auto
*
out
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
auto* out_ptr = ctx.Output<LoDTensor>("Out");
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out_ptr->mutable_data<T>(ctx.GetPlace());
// Resize();
const
auto
*
pad_value
=
ctx
.
Input
<
LoDTensor
>
(
"PadValue"
);
const
T
*
pad_value_data
=
pad_value
->
data
<
T
>
();
std
::
vector
<
T
>
pad_value_vec
(
pad_value_data
,
pad_value_data
+
pad_value
->
numel
());
T pad_value = static_cast<T>(ctx.Attr<float>("pad_value")
);
int
padded_length
=
ctx
.
Attr
<
int
>
(
"padded_length"
);
math
::
PaddingLoDTensorFunctor
<
DeviceContext
,
T
>
()(
math
::
PaddingLoDTensorFunctor
<
DeviceContext
,
T
>
()(
ctx.template device_context<DeviceContext>(), *x, *, false);
ctx
.
template
device_context
<
DeviceContext
>(),
*
x
,
out
,
pad_value_vec
,
padded_length
,
0
,
false
,
math
::
kBatchLengthWidth
);
math::SetConstant<DeviceContext, T> set_func;
set_func(ctx.template device_context<DeviceContext>(), out_ptr, pad_value);
*/
}
}
};
};
...
@@ -93,26 +51,17 @@ template <typename DeviceContext, typename T>
...
@@ -93,26 +51,17 @@ 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
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto* x_ptr = ctx.Input<LoDTensor>("X");
if
(
d_x
)
{
auto* g_out_ptr = ctx.Input<LoDTensor>(framework::GradVarName("Out"));
const
auto
*
d_out
=
ctx
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto* g_x_ptr = ctx.Output<LoDTensor>(framework::GradVarName("X"));
d_x
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
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();
int
padded_length
=
ctx
.
Attr
<
int
>
(
"padded_length"
);
auto& x_last_level_lod = x_lod[x_lod.size() - 1];
CopyFunctor copy_func<DeviceContext, T>(g_out_ptr,
math
::
UnpaddingLoDTensorFunctor
<
DeviceContext
,
T
>
()(
x_last_level_lod,
ctx
.
template
device_context
<
DeviceContext
>(),
*
d_out
,
d_x
,
g_x_ptr,
padded_length
,
0
,
false
,
math
::
kBatchLengthWidth
);
ctx,
}
false);
copy_func();
*/
}
}
};
};
...
...
python/paddle/fluid/tests/unittests/test_sequence_pad_op.py
0 → 100644
浏览文件 @
c3de69a9
# 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.
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestSequencePadOp
(
OpTest
):
def
set_attr
(
self
):
self
.
x_shape
=
[
12
,
4
]
self
.
x_len_lod
=
[[
2
,
3
,
4
,
3
]]
self
.
pad_value
=
[
1.0
]
self
.
padded_length
=
-
1
self
.
dtype
=
'float32'
def
set_data
(
self
):
x_data
=
np
.
random
.
uniform
(
0.1
,
0.5
,
self
.
x_shape
).
astype
(
self
.
dtype
)
pad_value_data
=
np
.
array
(
self
.
pad_value
).
astype
(
self
.
dtype
)
self
.
inputs
=
{
'X'
:
(
x_data
,
self
.
x_len_lod
),
'PadValue'
:
pad_value_data
}
self
.
attrs
=
{
'padded_length'
:
self
.
padded_length
}
def
compute
(
self
):
# get padded length
padded_length
=
self
.
padded_length
x_len_lod_0
=
self
.
x_len_lod
[
0
]
if
padded_length
==
-
1
:
max_seq_len
=
0
for
l
in
x_len_lod_0
:
max_seq_len
=
max
(
max_seq_len
,
l
)
padded_length
=
max_seq_len
# do padding
x_data
=
self
.
inputs
[
'X'
][
0
]
pad_value_data
=
self
.
inputs
[
'PadValue'
]
if
pad_value_data
.
shape
==
(
1
,
):
pad_value_data
=
np
.
broadcast_to
(
pad_value_data
,
shape
=
x_data
.
shape
[
1
:])
padded_sequences
=
[]
start_idx
=
0
for
l
in
x_len_lod_0
:
end_idx
=
start_idx
+
l
seq
=
x_data
[
start_idx
:
end_idx
]
to_pad_len
=
padded_length
-
l
for
_
in
range
(
to_pad_len
):
seq
=
np
.
append
(
seq
,
pad_value_data
[
np
.
newaxis
,
:],
axis
=
0
)
padded_sequences
.
append
(
seq
)
start_idx
=
end_idx
out_len_lod
=
self
.
x_len_lod
[:]
out_len_lod_0
=
[
padded_length
]
*
len
(
x_len_lod_0
)
out_len_lod
[
0
]
=
out_len_lod_0
out_data
=
np
.
concatenate
(
padded_sequences
,
axis
=
0
)
self
.
outputs
=
{
'Out'
:
(
out_data
,
out_len_lod
)}
def
setUp
(
self
):
self
.
op_type
=
'sequence_pad'
self
.
set_attr
()
self
.
set_data
()
self
.
compute
()
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"X"
],
"Out"
)
class
TestSequencePadOp2
(
TestSequencePadOp
):
def
set_attr
(
self
):
self
.
x_shape
=
[
12
,
4
]
self
.
x_len_lod
=
[[
2
,
3
,
4
,
3
]]
self
.
pad_value
=
[
1.0
,
2.0
,
3.0
,
4.0
]
self
.
padded_length
=
-
1
self
.
dtype
=
'float32'
class
TestSequencePadOp3
(
TestSequencePadOp
):
def
set_attr
(
self
):
self
.
x_shape
=
[
12
,
4
]
self
.
x_len_lod
=
[[
2
,
3
,
4
,
3
]]
self
.
pad_value
=
[
1.0
]
self
.
padded_length
=
7
self
.
dtype
=
'float32'
class
TestSequencePadOp4
(
TestSequencePadOp
):
def
set_attr
(
self
):
self
.
x_shape
=
[
12
,
4
]
self
.
x_len_lod
=
[[
2
,
3
,
4
,
3
]]
self
.
pad_value
=
[
1.0
,
2.0
,
3.0
,
4.0
]
self
.
padded_length
=
7
self
.
dtype
=
'float32'
class
TestSequencePadOp5
(
TestSequencePadOp
):
def
set_attr
(
self
):
self
.
x_shape
=
[
12
,
2
,
2
]
self
.
x_len_lod
=
[[
2
,
3
,
4
,
3
]]
self
.
pad_value
=
[
1.0
]
self
.
padded_length
=
-
1
self
.
dtype
=
'float32'
class
TestSequencePadOp6
(
TestSequencePadOp
):
def
set_attr
(
self
):
self
.
x_shape
=
[
12
,
2
,
2
]
self
.
x_len_lod
=
[[
2
,
3
,
4
,
3
]]
self
.
pad_value
=
[[
1.0
,
2.0
],
[
3.0
,
4.0
]]
self
.
padded_length
=
-
1
self
.
dtype
=
'float32'
class
TestSequencePadOp7
(
TestSequencePadOp
):
def
set_attr
(
self
):
self
.
x_shape
=
[
12
,
2
,
2
]
self
.
x_len_lod
=
[[
2
,
3
,
4
,
3
]]
self
.
pad_value
=
[
1.0
]
self
.
padded_length
=
7
self
.
dtype
=
'float32'
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录