Skip to content

Commit

Permalink
follow comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chengduoZH committed Oct 26, 2017
1 parent dcb3da5 commit 99c6f44
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 104 deletions.
2 changes: 1 addition & 1 deletion paddle/operators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ op_library(softmax_with_cross_entropy_op DEPS cross_entropy softmax)
op_library(sum_op DEPS net_op)
op_library(pool_op DEPS pooling)
op_library(pool_with_index_op DEPS pooling)
op_library(sequence_conv_op DEPS sequence_project)
op_library(sequence_conv_op DEPS context_project)
op_library(lstm_op DEPS sequence2batch lstm_compute)

list(REMOVE_ITEM GENERAL_OPS ${DEPS_OPS})
Expand Down
4 changes: 2 additions & 2 deletions paddle/operators/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if(WITH_GPU)
nv_library(cross_entropy SRCS cross_entropy.cc cross_entropy.cu DEPS operator)
nv_library(pooling SRCS pooling.cc pooling.cu DEPS device_context)
nv_library(vol2col SRCS vol2col.cc vol2col.cu DEPS device_context)
nv_library(sequence_project SRCS sequence_project.cc sequence_project.cu DEPS device_context)
nv_library(context_project SRCS context_project.cc context_project.cu DEPS device_context)
nv_library(sequence2batch SRCS sequence2batch.cc sequence2batch.cu DEPS device_context)
nv_library(lstm_compute SRCS lstm_compute.cc lstm_compute.cu DEPS device_context activation_functions)
else()
Expand All @@ -19,7 +19,7 @@ else()
cc_library(cross_entropy SRCS cross_entropy.cc DEPS operator)
cc_library(pooling SRCS pooling.cc DEPS device_context)
cc_library(vol2col SRCS vol2col.cc DEPS device_context)
cc_library(sequence_project SRCS sequence_project.cc DEPS device_context)
cc_library(context_project SRCS context_project.cc DEPS device_context)
cc_library(sequence2batch SRCS sequence2batch.cc DEPS device_context)
cc_library(lstm_compute SRCS lstm_compute.cc DEPS device_context activation_functions)
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/operators/math/sequence_project.h"
#include "paddle/operators/math/context_project.h"

namespace paddle {
namespace operators {
namespace math {

template class SequenceProjectFunctor<platform::CPUPlace, float>;
template class SequenceProjectFunctor<platform::CPUPlace, double>;
template class ContextProjectFunctor<platform::CPUPlace, float>;
template class ContextProjectFunctor<platform::CPUPlace, double>;

} // namespace math
} // namespace operators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ limitations under the License. */

#define EIGEN_USE_GPU

#include "paddle/operators/math/sequence_project.h"
#include "paddle/operators/math/context_project.h"

namespace paddle {
namespace operators {
namespace math {

template class SequenceProjectFunctor<platform::GPUPlace, float>;
template class SequenceProjectFunctor<platform::GPUPlace, double>;
template class ContextProjectFunctor<platform::GPUPlace, float>;
template class ContextProjectFunctor<platform::GPUPlace, double>;

} // namespace math
} // namespace operators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,29 @@ namespace paddle {
namespace operators {
namespace math {

// template <typename T, int MajorType = Eigen::RowMajor,
// typename IndexType = Eigen::DenseIndex>
// using EigenVector = framework::EigenVector<T, MajorType, IndexType>;

template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
/*
* \brief SequenceProject projects features of context_length time-steps of each
* instance.
*
* \brief Context projection concatenate features in adjacent time steps in
* 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
* consecutive rows from the i+shift_start row.
* \param in Input data.
* \param inShape The shape of Input data,
* \param Shape The shape of Input data,
* [minibatch, number_of_input_features].
* \param inShape A float LoDTensor.
* \param type A float LoDTensor.
*
* \param padding_data Padding data.
* \param inShape The shape of Padding data,
* \param Shape The shape of Padding data,
* [up_pad + down_pad, number_of_input_features].
* \param inShape A float LoDTensor.
* \param type A float Tensor.
*
* \param col Col data.
* \param inShape The shape of Col data,
* [minibatch, 1].
* \param inShape A float LoDTensor.
* \param Shape The shape of Col data,
* [minibatch, context_length * number_of_input_features].
* \param type A float Tensor.
*
* For a mini-batch of 2 variable lengths sentences, containing 3, and 1
* time-steps:
Expand Down Expand Up @@ -87,7 +85,7 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
*/

template <typename Place, typename T>
class SequenceProjectFunctor {
class ContextProjectFunctor {
public:
void operator()(const platform::DeviceContext& context,
framework::LoDTensor& in, framework::Tensor& padding_data,
Expand Down Expand Up @@ -147,8 +145,7 @@ class SequenceProjectFunctor {
/*stride_height*/ context_stride, /*stride_width*/ 1,
up_pad, down_pad, 0, 0);
}
out_t.Resize(framework::make_ddim(
{sequence_height, context_length * sequence_width}));
out_t.Resize({sequence_height, context_length * sequence_width});
}
}
}
Expand All @@ -162,8 +159,7 @@ class SequenceProjectFunctor {
sequence_height = static_cast<int>(out_t.dims()[0]);

// add up trainable data
out_t.Resize(framework::make_ddim(
{sequence_height * context_length, sequence_width}));
out_t.Resize({sequence_height * context_length, sequence_width});

if (up_pad > 0) { // add up pad
int padding_rows = std::min(
Expand Down Expand Up @@ -223,8 +219,7 @@ class SequenceProjectFunctor {
}
}
}
out_t.Resize(framework::make_ddim(
{sequence_height, context_length * sequence_width}));
out_t.Resize({sequence_height, context_length * sequence_width});
}
}
}
Expand Down
68 changes: 40 additions & 28 deletions paddle/operators/sequence_conv_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ class SequenceConvOp : public framework::OperatorWithKernel {
auto filter_dims = ctx->GetInputDim("Filter");
PADDLE_ENFORCE(in_dims.size() == 2 && filter_dims.size() == 2,
"Input(X, Filter) should be 2-D tensor.");
PADDLE_ENFORCE(
filter_dims[0] == context_length && filter_dims[1] == in_dims[1],
"Filter's shape should be (context_length x "
"number_of_input_features).");
PADDLE_ENFORCE(filter_dims[0] == context_length * in_dims[1],
"Filter's height should be context_length * "
"number_of_input_features .");

if (padding_trainable) {
PADDLE_ENFORCE(
Expand All @@ -66,8 +65,9 @@ class SequenceConvOp : public framework::OperatorWithKernel {
"and 'context_length'.");
}

in_dims[1] = 1;
in_dims[1] = filter_dims[1];
ctx->SetOutputDim("Out", in_dims);
ctx->ShareLoD("X", "Out");
}
};

Expand Down Expand Up @@ -101,35 +101,51 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
SequenceConvOpMaker(framework::OpProto* proto,
framework::OpAttrChecker* op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("X",
"(A float LoDTensor) the input of SequenceConvOp, a vector of "
"2-D matrix of size (minibatch, number_of_input_features).");
AddInput(
"X",
"(LoDTensor) the input(X) is a LodTensor, which support "
"variable-time length input sequence. The underlying tensor in "
"this LoDTensor is a matrix with shape (T, D), where, T is the "
"total time steps in this mini-batch, D is the input feature size.");
AddInput("PaddingData",
"(Tensor) the input of SequenceConvOp, a vector of "
"2-D matrix of size (up_pad + down_pad, "
"number_of_input_features). ")
"(Tensor, optional) the input(PaddingData) is an optional "
"parameter, and it is learnable. "
"This is a tensor with shape (N, D), where N is the "
"top_pad + bottom_pad, D is the input feature size. In order to "
"ensure the equal length of sequence before and after "
"convolution, it is necessary to fill the top and bottom of each "
"sequence according to context_length, context_stride and "
"context_start")
.AsDispensable();
AddInput("Filter",
"(Tensor) the input of SequenceConvOp, a vector of "
"2-D matrix of size (context_length x number_of_input_features).");
AddOutput("Out",
"(A float LoDTensor) the output of SequenceConvOp, a vector "
"of 2-D matrix of size (minibatch, 1).");
"(Tensor) the input(Filter) is an learnable parameter."
"This is a tensor with shape (N, D), where N is the "
"context_length, D is the output feature size.");
AddOutput(
"Out",
"(LoDTensor) the output(Out) is a LodTensor, which support "
"variable-time length output sequence. The underlying tensor in "
"this LoDTensor is a matrix with shape (T, D), where, T is the "
"total time steps in this mini-batch, D is the output feature size.");

AddAttr<bool>("padding_trainable",
"(bool, default false) the padding data of SequenceConvOp "
"is trainable or not.")
.SetDefault(false);
AddAttr<int>("context_length",
"(int, default 3) the context_length of SequenceConvOp.")
"(int, default 3) the context_length of SequenceConvOp is the "
"height of the convolution kernel.")
.SetDefault(3)
.GreaterThan(0);
AddAttr<int>("context_start",
"(int, default 0) the context_start of SequenceConvOp.")
"(int, default 0) the context_start of SequenceConvOp "
"represents the beginning of the convolution of the number of "
"rows of sequence, which can be negative.")
.SetDefault(0);
AddAttr<int>("context_stride",
"(int, default 1) the context_stride of SequenceConvOp. "
"Currently, sequence_project_op only support "
"(int, default 1) the context_stride of SequenceConvOp "
"represents the step length of convolution. "
"Currently, SequenceConvOp only supports"
"context_stride=1.")
.SetDefault(1)
.GreaterThan(0);
Expand All @@ -139,14 +155,10 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
context_length time-steps of each instance.
The convolution operation calculates the output based on the input, filter
and strides, paddings parameters. The size of each dimension of the
parameters is checked in the infer-shape.
Example:
Input:
X shape: (minibatch, number_of_input_features)
Filter shape: (context_length, number_of_input_features)
Output:
Out shape: (minibatch, 1)
parameters is checked in the infer-shape. In order to ensure the equal
length of sequence before and after convolution, it is necessary to fill
the top and bottom of each sequence according to context_length,
context_stride and context_start.
)DOC");
}
};
Expand Down
Loading

0 comments on commit 99c6f44

Please sign in to comment.