提交 b08ae0b1 编写于 作者: C chengduoZH

fix code format and doc

上级 7942984f
...@@ -16,34 +16,36 @@ limitations under the License. */ ...@@ -16,34 +16,36 @@ limitations under the License. */
#include "paddle/framework/eigen.h" #include "paddle/framework/eigen.h"
#include "paddle/framework/lod_tensor.h" #include "paddle/framework/lod_tensor.h"
#include "paddle/framework/tensor.h"
#include "paddle/operators/math/im2col.h" #include "paddle/operators/math/im2col.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
namespace math { namespace math {
using Tensor = framework::Tensor;
using LoDTensor = framework::LoDTensor;
template <typename T, int MajorType = Eigen::RowMajor, template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex> typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>; using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
/* /*
* \brief Context projection concatenate features in adjacent time steps in * \brief Context projection concatenates features in adjacent time-steps in
* a sequence. The i-th row of the output is the concatenation of * a sequence. The i-th row of the output is the concatenation of
* context_length rows of the input. The context_length rows are the * context_length rows of the input. The context_length rows are the
* consecutive rows from the i+shift_start row. * consecutive rows from the i+shift_start row.
* ContextProjectGradFunctor is the inverse process of ContextProjectFunctor. * ContextProjectGradFunctor is the inverse process of ContextProjectFunctor.
*
* \param in Input data. * \param in Input data.
* \param Shape The shape of Input data, * \param Shape The shape of Input data:
* [minibatch, input_hidden_size]. * [mini-batch, input_hidden_size].
* *
* \param padding_data Padding data. * \param padding_data Padding data.
* \param Shape The shape of Padding data, * \param Shape The shape of Padding data:
* [up_pad + down_pad, input_hidden_size]. * [up_pad + down_pad, input_hidden_size].
* *
* \param col Col data. * \param col Col data.
* \param Shape The shape of Col data, * \param Shape The shape of Col data:
* [minibatch, context_length * input_hidden_size]. * [mini-batch, context_length * input_hidden_size].
* *
* For a mini-batch of 2 variable lengths sentences, containing 3, and 1 * For a mini-batch of 2 variable lengths sentences, containing 3, and 1
* time-steps: * time-steps:
...@@ -63,7 +65,7 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>; ...@@ -63,7 +65,7 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
* - Case1: * - Case1:
* If context_start is -1 and padding_trainable is false, we use zero to pad * If context_start is -1 and padding_trainable is false, we use zero to pad
* instead of learned weight to pad, * instead of learned weight to pad,
* and the context_lenth is 3, the output (Out) is: * and the context_length is 3, the output (Out) is:
* *
* Out =[[0, 0, a1, a2, b1, b2; * Out =[[0, 0, a1, a2, b1, b2;
* a1, a2, b1, b2, c1, c2; * a1, a2, b1, b2, c1, c2;
...@@ -73,7 +75,7 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>; ...@@ -73,7 +75,7 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
* - Case2: * - Case2:
* If context_start is -1 and padding_trainable is true, we use learned weight * If context_start is -1 and padding_trainable is true, we use learned weight
* to pad, * to pad,
* and the context_lenth is 3, the output (Out) is: * and the context_length is 3, the output (Out) is:
* *
* Out = [[w1, w2, a1, a2, b1, b2; * Out = [[w1, w2, a1, a2, b1, b2;
* a1, a2, b1, b2, c1, c2; * a1, a2, b1, b2, c1, c2;
...@@ -85,16 +87,13 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>; ...@@ -85,16 +87,13 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
template <typename Place, typename T> template <typename Place, typename T>
class ContextProjectFunctor { class ContextProjectFunctor {
public: public:
void operator()(const platform::DeviceContext& context, void operator()(const platform::DeviceContext& context, const LoDTensor& in,
const framework::LoDTensor& in, const Tensor& padding_data, Tensor& col,
const framework::Tensor& padding_data, framework::Tensor& col,
bool padding_trainable, int context_start, int context_length, bool padding_trainable, int context_start, int context_length,
int context_stride, int up_pad, int down_pad) { int context_stride, int up_pad, int down_pad) {
auto lod_level_0 = in.lod()[0]; auto lod_level_0 = in.lod()[0];
paddle::operators::math::Im2ColFunctor< math::Im2ColFunctor<math::ColFormat::kOCF, Place, float> im2col_ocf;
paddle::operators::math::ColFormat::kOCF, Place, float>
im2col_ocf;
int input_row_begin, input_row_end; int input_row_begin, input_row_end;
int sequence_height, sequence_width; int sequence_height, sequence_width;
...@@ -106,19 +105,18 @@ class ContextProjectFunctor { ...@@ -106,19 +105,18 @@ class ContextProjectFunctor {
: static_cast<int>(lod_level_0[i]); : static_cast<int>(lod_level_0[i]);
input_row_end = static_cast<int>(lod_level_0[i + 1]); input_row_end = static_cast<int>(lod_level_0[i + 1]);
framework::Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]), Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
static_cast<int>(lod_level_0[i + 1])); static_cast<int>(lod_level_0[i + 1]));
sequence_height = static_cast<int>(out_t.dims()[0]); sequence_height = static_cast<int>(out_t.dims()[0]);
if (input_row_begin < input_row_end) { if (input_row_begin < input_row_end) {
framework::Tensor in_t = in.Slice(input_row_begin, input_row_end); Tensor in_t = in.Slice(input_row_begin, input_row_end);
std::vector<int64_t> output_shape( std::vector<int64_t> output_shape(
{sequence_height, 1, 1, context_length, {sequence_height, 1, 1, context_length,
sequence_width}); // output_height, output_width, sequence_width}); // output_height, output_width,
// input_channels, filter_height, filter_width // input_channels, filter_height, filter_width
out_t.Resize(framework::make_ddim(output_shape)); out_t.Resize(framework::make_ddim(output_shape));
std::vector<int64_t> input_shape( std::vector<int64_t> input_shape(
...@@ -134,8 +132,7 @@ class ContextProjectFunctor { ...@@ -134,8 +132,7 @@ class ContextProjectFunctor {
} }
if (padding_trainable) { if (padding_trainable) {
for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) { for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) {
framework::Tensor out_t = Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
col.Slice(static_cast<int>(lod_level_0[i]),
static_cast<int>(lod_level_0[i + 1])); static_cast<int>(lod_level_0[i + 1]));
sequence_height = static_cast<int>(out_t.dims()[0]); sequence_height = static_cast<int>(out_t.dims()[0]);
...@@ -150,10 +147,9 @@ class ContextProjectFunctor { ...@@ -150,10 +147,9 @@ class ContextProjectFunctor {
for (int k = 0; k < padding_rows; ++k) { for (int k = 0; k < padding_rows; ++k) {
int padding_size = int padding_size =
k + context_length < up_pad ? context_length : up_pad - k; k + context_length < up_pad ? context_length : up_pad - k;
framework::Tensor out_t_sub = out_t.Slice( Tensor out_t_sub = out_t.Slice(k * context_length,
k * context_length, k * context_length + padding_size); k * context_length + padding_size);
framework::Tensor w_sub = padding_data.Slice(k, k + padding_size); Tensor w_sub = padding_data.Slice(k, k + padding_size);
// in this block, using EigenVector<T>::Flatten is ok too.
auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub); auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub);
auto w_sub_e = EigenMatrix<T>::From(w_sub); auto w_sub_e = EigenMatrix<T>::From(w_sub);
out_t_sub_e.device(*context.GetEigenDevice<Place>()) = w_sub_e; out_t_sub_e.device(*context.GetEigenDevice<Place>()) = w_sub_e;
...@@ -180,10 +176,11 @@ class ContextProjectFunctor { ...@@ -180,10 +176,11 @@ class ContextProjectFunctor {
} }
if (padding_begin > 0 || sequence_height == context_start) if (padding_begin > 0 || sequence_height == context_start)
padding_idx = padding_begin + t; padding_idx = padding_begin + t;
framework::Tensor out_t_sub = out_t.Slice(
Tensor out_t_sub = out_t.Slice(
(down_pad_begin_row + t) * context_length - padding_size, (down_pad_begin_row + t) * context_length - padding_size,
(down_pad_begin_row + t) * context_length); (down_pad_begin_row + t) * context_length);
framework::Tensor w_sub = padding_data.Slice( Tensor w_sub = padding_data.Slice(
up_pad + padding_idx, up_pad + padding_idx + padding_size); up_pad + padding_idx, up_pad + padding_idx + padding_size);
auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub); auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub);
auto w_sub_e = EigenMatrix<T>::From(w_sub); auto w_sub_e = EigenMatrix<T>::From(w_sub);
...@@ -199,16 +196,13 @@ class ContextProjectFunctor { ...@@ -199,16 +196,13 @@ class ContextProjectFunctor {
template <typename Place, typename T> template <typename Place, typename T>
class ContextProjectGradFunctor { class ContextProjectGradFunctor {
public: public:
void operator()(const platform::DeviceContext& context, void operator()(const platform::DeviceContext& context, LoDTensor& in,
framework::LoDTensor& in, framework::Tensor& padding_data, Tensor& padding_data, Tensor& col, bool padding_trainable,
framework::Tensor& col, bool padding_trainable,
int context_start, int context_length, int context_stride, int context_start, int context_length, int context_stride,
int up_pad, int down_pad, bool input_grad, bool pad_grad) { int up_pad, int down_pad, bool input_grad, bool pad_grad) {
auto lod_level_0 = in.lod()[0]; auto lod_level_0 = in.lod()[0];
paddle::operators::math::Col2ImFunctor< math::Col2ImFunctor<math::ColFormat::kOCF, Place, float> col2im_ocf;
paddle::operators::math::ColFormat::kOCF, Place, float>
col2im_ocf;
int input_row_begin, input_row_end; int input_row_begin, input_row_end;
int sequence_height, sequence_width; int sequence_height, sequence_width;
...@@ -221,20 +215,18 @@ class ContextProjectGradFunctor { ...@@ -221,20 +215,18 @@ class ContextProjectGradFunctor {
: static_cast<int>(lod_level_0[i]); : static_cast<int>(lod_level_0[i]);
input_row_end = static_cast<int>(lod_level_0[i + 1]); input_row_end = static_cast<int>(lod_level_0[i + 1]);
framework::Tensor out_t = Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
col.Slice(static_cast<int>(lod_level_0[i]),
static_cast<int>(lod_level_0[i + 1])); static_cast<int>(lod_level_0[i + 1]));
sequence_height = static_cast<int>(out_t.dims()[0]); sequence_height = static_cast<int>(out_t.dims()[0]);
if (input_row_begin < input_row_end) { if (input_row_begin < input_row_end) {
framework::Tensor in_t = in.Slice(input_row_begin, input_row_end); Tensor in_t = in.Slice(input_row_begin, input_row_end);
std::vector<int64_t> output_shape( std::vector<int64_t> output_shape(
{sequence_height, 1, 1, context_length, {sequence_height, 1, 1, context_length,
sequence_width}); // output_height, output_width, sequence_width}); // output_height, output_width,
// input_channels, filter_height, filter_width // input_channels, filter_height, filter_width
out_t.Resize(framework::make_ddim(output_shape)); out_t.Resize(framework::make_ddim(output_shape));
std::vector<int64_t> input_shape( std::vector<int64_t> input_shape(
...@@ -252,8 +244,7 @@ class ContextProjectGradFunctor { ...@@ -252,8 +244,7 @@ class ContextProjectGradFunctor {
if (pad_grad) { if (pad_grad) {
if (padding_trainable) { if (padding_trainable) {
for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) { for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) {
framework::Tensor out_t = Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
col.Slice(static_cast<int>(lod_level_0[i]),
static_cast<int>(lod_level_0[i + 1])); static_cast<int>(lod_level_0[i + 1]));
sequence_height = static_cast<int>(out_t.dims()[0]); sequence_height = static_cast<int>(out_t.dims()[0]);
...@@ -266,10 +257,9 @@ class ContextProjectGradFunctor { ...@@ -266,10 +257,9 @@ class ContextProjectGradFunctor {
for (int k = 0; k < padding_rows; ++k) { for (int k = 0; k < padding_rows; ++k) {
int padding_size = int padding_size =
k + context_length < up_pad ? context_length : up_pad - k; k + context_length < up_pad ? context_length : up_pad - k;
framework::Tensor out_t_sub = out_t.Slice( Tensor out_t_sub = out_t.Slice(k * context_length,
k * context_length, k * context_length + padding_size); k * context_length + padding_size);
framework::Tensor w_sub = padding_data.Slice(k, k + padding_size); Tensor w_sub = padding_data.Slice(k, k + padding_size);
// in this block, using EigenVector<T>::Flatten is ok too.
auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub); auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub);
auto w_sub_e = EigenMatrix<T>::From(w_sub); auto w_sub_e = EigenMatrix<T>::From(w_sub);
w_sub_e.device(*context.GetEigenDevice<Place>()) = w_sub_e.device(*context.GetEigenDevice<Place>()) =
...@@ -298,10 +288,11 @@ class ContextProjectGradFunctor { ...@@ -298,10 +288,11 @@ class ContextProjectGradFunctor {
} }
if (padding_begin > 0 || sequence_height == context_start) if (padding_begin > 0 || sequence_height == context_start)
padding_idx = padding_begin + t; padding_idx = padding_begin + t;
framework::Tensor out_t_sub = out_t.Slice(
Tensor out_t_sub = out_t.Slice(
(down_pad_begin_row + t) * context_length - padding_size, (down_pad_begin_row + t) * context_length - padding_size,
(down_pad_begin_row + t) * context_length); (down_pad_begin_row + t) * context_length);
framework::Tensor w_sub = padding_data.Slice( Tensor w_sub = padding_data.Slice(
up_pad + padding_idx, up_pad + padding_idx + padding_size); up_pad + padding_idx, up_pad + padding_idx + padding_size);
auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub); auto out_t_sub_e = EigenMatrix<T>::From(out_t_sub);
auto w_sub_e = EigenMatrix<T>::From(w_sub); auto w_sub_e = EigenMatrix<T>::From(w_sub);
......
...@@ -31,18 +31,19 @@ class SequenceConvOp : public framework::OperatorWithKernel { ...@@ -31,18 +31,19 @@ class SequenceConvOp : public framework::OperatorWithKernel {
"Output(Out) of SequenceConvOp should not be null."); "Output(Out) of SequenceConvOp should not be null.");
int context_length = ctx->Attrs().Get<int>("contextLength"); int context_length = ctx->Attrs().Get<int>("contextLength");
bool padding_trainable = ctx->Attrs().Get<bool>("paddingTrainable");
int context_start = ctx->Attrs().Get<int>("contextStart"); int context_start = ctx->Attrs().Get<int>("contextStart");
auto in_dims = ctx->GetInputDim("X"); auto in_dims = ctx->GetInputDim("X");
auto filter_dims = ctx->GetInputDim("Filter"); auto filter_dims = ctx->GetInputDim("Filter");
PADDLE_ENFORCE(ctx->Attrs().Get<int>("contextStride") == 1,
"Currently, SequenceConvOp only supports contextStride=1.");
PADDLE_ENFORCE(in_dims.size() == 2 && filter_dims.size() == 2, PADDLE_ENFORCE(in_dims.size() == 2 && filter_dims.size() == 2,
"Input(X, Filter) should be 2-D tensor."); "Input(X, Filter) should be 2-D tensor.");
PADDLE_ENFORCE(filter_dims[0] == context_length * in_dims[1], PADDLE_ENFORCE(filter_dims[0] == context_length * in_dims[1],
"Filter's height should be context_length * " "Filter's height should be context_length * "
"number_of_input_features ."); "input_hidden_size .");
if (padding_trainable) { if (ctx->Attrs().Get<bool>("paddingTrainable")) {
PADDLE_ENFORCE( PADDLE_ENFORCE(
ctx->HasInput("PaddingData"), ctx->HasInput("PaddingData"),
"Input(PaddingData) of SequenceConvOp should not be null."); "Input(PaddingData) of SequenceConvOp should not be null.");
...@@ -88,6 +89,7 @@ class SequenceConvGradOp : public framework::OperatorWithKernel { ...@@ -88,6 +89,7 @@ class SequenceConvGradOp : public framework::OperatorWithKernel {
} }
if (ctx->HasOutput(framework::GradVarName("X"))) { if (ctx->HasOutput(framework::GradVarName("X"))) {
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
ctx->ShareLoD(framework::GradVarName("X"), "X");
} }
if (ctx->HasOutput(framework::GradVarName("Filter"))) { if (ctx->HasOutput(framework::GradVarName("Filter"))) {
ctx->SetOutputDim(framework::GradVarName("Filter"), ctx->SetOutputDim(framework::GradVarName("Filter"),
...@@ -105,13 +107,13 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -105,13 +107,13 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
"X", "X",
"(LoDTensor) the input(X) is a LodTensor, which support " "(LoDTensor) the input(X) is a LodTensor, which support "
"variable-time length input sequence. The underlying tensor in " "variable-time length input sequence. The underlying tensor in "
"this LoDTensor is a matrix with shape (T, D), where, T is the " "this LoDTensor is a matrix with shape (T, N), where, T is the "
"total time steps in this mini-batch, D is the input feature size."); "total time steps in this mini-batch, N is the input_hidden_size.");
AddInput("PaddingData", AddInput("PaddingData",
"(Tensor, optional) the input(PaddingData) is an optional " "(Tensor, optional) the input(PaddingData) is an optional "
"parameter, and it is learnable. " "parameter, and it is learnable. "
"This is a tensor with shape (N, D), where N is the " "This is a tensor with shape (P, N), where P is the "
"top_pad + bottom_pad, D is the input feature size. In order to " "top_pad + bottom_pad, N is the input_hidden_size. In order to "
"ensure the equal length of sequence before and after " "ensure the equal length of sequence before and after "
"convolution, it is necessary to fill the top and bottom of each " "convolution, it is necessary to fill the top and bottom of each "
"sequence according to context_length, context_stride and " "sequence according to context_length, context_stride and "
...@@ -120,17 +122,17 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -120,17 +122,17 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
AddInput( AddInput(
"Filter", "Filter",
"(Tensor) the input(Filter) is an learnable parameter." "(Tensor) the input(Filter) is an learnable parameter."
"This is a tensor with shape (N, D), where N is the " "This is a tensor with shape (K, M), where K is the "
"context_length * input_hidden_size, D is the output feature size."); "context_length * input_hidden_size, M is the output feature size.");
AddOutput( AddOutput(
"Out", "Out",
"(LoDTensor) the output(Out) is a LodTensor, which support " "(LoDTensor) 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 LoDTensor is a matrix with shape (T, D), where, T is the " "this LoDTensor is a matrix with shape (T, M), where, T is the "
"total time steps in this mini-batch, D is the output feature size."); "total time steps in this mini-batch, M is the output feature size.");
AddAttr<bool>("paddingTrainable", AddAttr<bool>("paddingTrainable",
"(bool, default false) the padding data of SequenceConvOp " "(bool, default:false) the padding data of SequenceConvOp "
"is trainable or not.") "is trainable or not.")
.SetDefault(false); .SetDefault(false);
AddAttr<int>("contextLength", AddAttr<int>("contextLength",
...@@ -138,7 +140,7 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -138,7 +140,7 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
"height of the convolution kernel.") "height of the convolution kernel.")
.GreaterThan(0); .GreaterThan(0);
AddAttr<int>("contextStart", AddAttr<int>("contextStart",
"(int, default 0) the contextStart of SequenceConvOp " "(int, default:0) the contextStart of SequenceConvOp "
"represents the beginning of the convolution of the number of " "represents the beginning of the convolution of the number of "
"rows of sequence, which can be negative. The negative number " "rows of sequence, which can be negative. The negative number "
"means to pad contextStart time-steps of zeros or learnable " "means to pad contextStart time-steps of zeros or learnable "
...@@ -147,7 +149,7 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -147,7 +149,7 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
"instance.") "instance.")
.SetDefault(0); .SetDefault(0);
AddAttr<int>("contextStride", AddAttr<int>("contextStride",
"(int, default 1) the contextStride of SequenceConvOp " "(int, default:1) the contextStride of SequenceConvOp "
"represents the stride length of convolution kernel. " "represents the stride length of convolution kernel. "
"Currently, SequenceConvOp only supports" "Currently, SequenceConvOp only supports"
"contextStride=1.") "contextStride=1.")
...@@ -156,7 +158,7 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -156,7 +158,7 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
AddComment(R"DOC( AddComment(R"DOC(
SequenceConvOp performs convolution operation on features of SequenceConvOp performs convolution operation on features of
context_length time-steps of each instance. contextLength time-steps of each instance.
The convolution operation calculates the output based on the input, filter The convolution operation calculates the output based on the input, filter
and strides, paddings parameters. The size of each dimension of the and strides, paddings parameters. The size of each dimension of the
parameters is checked in the infer-shape. In order to ensure the equal parameters is checked in the infer-shape. In order to ensure the equal
......
...@@ -40,7 +40,6 @@ class SequenceConvKernel : public framework::OpKernel<T> { ...@@ -40,7 +40,6 @@ 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");
// InferShape by in_lod
PADDLE_ENFORCE_EQ(in->lod().size(), 1UL, PADDLE_ENFORCE_EQ(in->lod().size(), 1UL,
"Only support one level sequence now."); "Only support one level sequence now.");
...@@ -51,20 +50,17 @@ class SequenceConvKernel : public framework::OpKernel<T> { ...@@ -51,20 +50,17 @@ class SequenceConvKernel : public framework::OpKernel<T> {
int up_pad = std::max(0, -context_start); int up_pad = std::max(0, -context_start);
int down_pad = std::max(0, context_start + context_length - 1); int down_pad = std::max(0, context_start + context_length - 1);
int sequence_width; int sequence_width = static_cast<int>(in->dims()[1]);
sequence_width = static_cast<int>(in->dims()[1]);
// 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}; context_length * sequence_width};
Tensor col; Tensor col;
col.mutable_data<T>(col_shape, context.GetPlace()); col.mutable_data<T>(col_shape, context.GetPlace());
math::SetConstant<Place, T> set_zero;
// Because if padding_trainable is false, padding data should be zeros. // Because if padding_trainable is false, padding data should be zeros.
math::SetConstant<Place, T> set_zero;
set_zero(context.device_context(), &col, static_cast<T>(0)); set_zero(context.device_context(), &col, static_cast<T>(0));
paddle::operators::math::ContextProjectFunctor<Place, T> math::ContextProjectFunctor<Place, T> seq_project_functor;
seq_project_functor;
seq_project_functor(context.device_context(), *in, *padding_data, col, seq_project_functor(context.device_context(), *in, *padding_data, col,
padding_trainable, context_start, context_length, padding_trainable, context_start, context_length,
...@@ -79,8 +75,8 @@ template <typename Place, typename T> ...@@ -79,8 +75,8 @@ template <typename Place, 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* out_g = context.Input<LoDTensor>(framework::GradVarName("Out"));
auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X")); auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X"));
auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out"));
auto* filter_g = context.Output<Tensor>(framework::GradVarName("Filter")); auto* filter_g = context.Output<Tensor>(framework::GradVarName("Filter"));
auto* padding_data_g = auto* padding_data_g =
context.Output<Tensor>(framework::GradVarName("PaddingData")); context.Output<Tensor>(framework::GradVarName("PaddingData"));
...@@ -113,10 +109,8 @@ class SequenceConvGradKernel : public framework::OpKernel<T> { ...@@ -113,10 +109,8 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
math::matmul<Place, T>(context.device_context(), *out_g, false, *filter, math::matmul<Place, T>(context.device_context(), *out_g, false, *filter,
true, T(1.0), &col, T(1.0)); true, T(1.0), &col, T(1.0));
} }
paddle::operators::math::ContextProjectFunctor<Place, T> math::ContextProjectFunctor<Place, T> seq_project_functor;
seq_project_functor; math::ContextProjectGradFunctor<Place, T> seq_project_grad_functor;
paddle::operators::math::ContextProjectGradFunctor<Place, T>
seq_project_grad_functor;
if (in_g) { if (in_g) {
in_g->mutable_data<T>(context.GetPlace()); in_g->mutable_data<T>(context.GetPlace());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册