parent
437debf40e
commit
e7940141ce
@ -1,136 +1,100 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/operators/sequence_concat_op.h"
|
||||
#include <vector>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class SequenceConcatOp : public framework::OperatorWithKernel {
|
||||
class SeqConcatOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInputs("X"),
|
||||
"Inputs(X) of SequenceConcatOp should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||
"Output(Out) of SequenceConcatOp should not be null.");
|
||||
const size_t level = static_cast<size_t>(ctx->Attrs().Get<int>("level"));
|
||||
const size_t axis = static_cast<size_t>(ctx->Attrs().Get<int>("axis"));
|
||||
PADDLE_ENFORCE(level == 0UL || level == 1UL,
|
||||
"The sequence_concat operator only accepts sequence "
|
||||
"or a nested sequence as its input.");
|
||||
auto ins_dims = ctx->GetInputsDim("X");
|
||||
framework::DDim out_dims = ins_dims[0];
|
||||
const size_t n = ins_dims.size();
|
||||
for (size_t i = 1; i < n; ++i) {
|
||||
out_dims[axis] += ins_dims[i][axis];
|
||||
}
|
||||
ctx->SetOutputDim("Out", out_dims);
|
||||
void Make() override {
|
||||
AddInput("X", "The inputs of sequence concat op").AsDuplicable();
|
||||
AddOutput("Out", "The output of sequence concat op");
|
||||
AddComment(
|
||||
"Sequence Concat Op\n"
|
||||
"It will concat LoD tensors by its sequence information.\n"
|
||||
"For example:\n"
|
||||
" LoD of X1 = [0, 3, 7]\n"
|
||||
" LoD of X2 = [0, 7, 9]\n"
|
||||
" Result LoD is [0, (3+7), (7+9)]\n"
|
||||
" i.e.[0, 10, 16]\n");
|
||||
}
|
||||
};
|
||||
|
||||
class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
class SeqConcatShapeInferer : public framework::InferShapeBase {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("X",
|
||||
"(LodTensorArray) Input is a vector of LoDTensor, "
|
||||
"each of which is a variable-length sequence or nested sequence.")
|
||||
.AsDuplicable();
|
||||
AddOutput("Out",
|
||||
"(LoDTensor), Variable-length output of "
|
||||
"sequence_concat Op.");
|
||||
AddAttr<int>("axis",
|
||||
"(int, default 0) "
|
||||
"The axis along which the inputs will be joined. "
|
||||
"If axis is 0, the inputs will be joined with LoD index.")
|
||||
.SetDefault(0);
|
||||
AddAttr<int>("level",
|
||||
"(int, default 0) "
|
||||
"The level at which the inputs will be joined. "
|
||||
"If the level is 0, the inputs will be joined at the nested "
|
||||
"sequence level. "
|
||||
"If the level is 1, the inputs will be joined at the "
|
||||
"sequence level. "
|
||||
"The level should be less than the level number of inputs.")
|
||||
.SetDefault(0);
|
||||
AddComment(R"DOC(
|
||||
The sequence_concat operator concatenates multiple LoDTensors.
|
||||
It only supports sequence (LoD Tensor with level number is 1)
|
||||
or a nested sequence (LoD tensor with level number is 2) as its input.
|
||||
- Case1:
|
||||
If the axis is other than 0(here, axis is 1 and level is 1),
|
||||
each input should have the same LoD information and the LoD
|
||||
information of the output keeps the same as the input.
|
||||
|
||||
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
|
||||
LoD(x1) = {{0,2,4}, {0,1,2,3,4}}; Dims(x1) = (4,4,4)
|
||||
LoD(Out) = {{0,2,4}, {0,1,2,3,4}}; Dims(Out) = (4,7,4)
|
||||
|
||||
- Case2:
|
||||
If the axis is 0(here, leve is 0), the inputs are concatenated along
|
||||
time steps, the LoD information of the output need to re-compute.
|
||||
The LoD information of level-1 should be same.
|
||||
|
||||
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
|
||||
LoD(x1) = {{0,2,4}, {0,1,3,5,7}}; Dims(x1) = (7,3,4)
|
||||
LoD(Out) = {{0,2,4}, {0,2,5,8,11}}; Dims(Out) = (11,3,4)
|
||||
|
||||
- Case3:
|
||||
If the axis is 0(here, level is 1).
|
||||
|
||||
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
|
||||
LoD(x1) = {{0,3,4}, {0,1,3,5,7}}; Dims(x1) = (7,3,4)
|
||||
LoD(Out) = {{0,5,8}, {0,1,2,3,5,7,8,9,11}}; Dims(Out) = (11,3,4)
|
||||
|
||||
- Case4:
|
||||
If the LoD number is 1, axis is 0, level is 0
|
||||
|
||||
LoD(x0) = {{0,1,2,3,4}}; Dims(x0) = (4,3,4)
|
||||
LoD(x1) = {{0,1,3,5,7}}; Dims(x1) = (7,3,4)
|
||||
LoD(Out) = {{0,2,5,8,11}}; Dims(Out) = (11,3,4)
|
||||
|
||||
NOTE: The levels of all the inputs should be the same.
|
||||
)DOC");
|
||||
void operator()(framework::InferShapeContext *context) const override {
|
||||
try {
|
||||
PADDLE_ENFORCE(context->HasInputs("X"));
|
||||
PADDLE_ENFORCE(context->HasOutput("Out"));
|
||||
|
||||
auto x_dims = context->GetInputsDim("X");
|
||||
int64_t batch_size = 0;
|
||||
int64_t feature_size = 0;
|
||||
std::vector<int64_t> out_dims;
|
||||
for (auto &x_dim : x_dims) {
|
||||
if (out_dims.empty()) {
|
||||
out_dims = framework::vectorize(x_dim);
|
||||
}
|
||||
batch_size += x_dim[0];
|
||||
if (feature_size == 0) {
|
||||
feature_size = framework::product(x_dim) / x_dim[0];
|
||||
} else {
|
||||
PADDLE_ENFORCE_EQ(
|
||||
feature_size, framework::product(x_dim) / x_dim[0],
|
||||
"Inputs of sequence concat must have same feature size");
|
||||
}
|
||||
}
|
||||
if (batch_size < 0) {
|
||||
batch_size = -1; // Normalize batch size for compile time.
|
||||
}
|
||||
out_dims[0] = batch_size;
|
||||
context->SetOutputDim("Out", framework::make_ddim(out_dims));
|
||||
if (!context->IsRuntime()) { // Runtime LoD infershape will be computed
|
||||
// in Kernel.
|
||||
context->ShareLoD("X", "Out");
|
||||
}
|
||||
} catch (...) {
|
||||
PADDLE_THROW("Unknown error");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class SequenceConcatGradOp : public framework::OperatorWithKernel {
|
||||
class SeqConcatGradShapeInferer : public framework::InferShapeBase {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
|
||||
"The gradient of Out should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutputs(framework::GradVarName("X")),
|
||||
"The gradient of X should not be null.");
|
||||
ctx->SetOutputsDim(framework::GradVarName("X"), ctx->GetInputsDim("X"));
|
||||
void operator()(framework::InferShapeContext *context) const override {
|
||||
context->SetOutputsDim(framework::GradVarName("X"),
|
||||
context->GetInputsDim("X"));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OPERATOR(sequence_concat, ops::SequenceConcatOp,
|
||||
ops::SequenceConcatOpMaker,
|
||||
paddle::framework::DefaultGradOpDescMaker<
|
||||
false> /* set false to disable empty grad */);
|
||||
REGISTER_OPERATOR(sequence_concat_grad, ops::SequenceConcatGradOp);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
sequence_concat,
|
||||
ops::SequenceConcatOpKernel<paddle::platform::CPUDeviceContext, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
sequence_concat_grad,
|
||||
ops::SequenceConcatGradOpKernel<paddle::platform::CPUDeviceContext, float>);
|
||||
namespace op = paddle::operators;
|
||||
|
||||
REGISTER_OPERATOR(sequence_concat, paddle::framework::OperatorWithKernel,
|
||||
op::SeqConcatOpMaker, op::SeqConcatShapeInferer,
|
||||
paddle::framework::DefaultGradOpDescMaker<false>);
|
||||
template <typename T>
|
||||
using Kernel = op::SeqConcatKernel<paddle::platform::CPUDeviceContext, T>;
|
||||
REGISTER_OP_CPU_KERNEL(sequence_concat, Kernel<float>, Kernel<double>);
|
||||
REGISTER_OPERATOR(sequence_concat_grad, paddle::framework::OperatorWithKernel,
|
||||
op::SeqConcatGradShapeInferer);
|
||||
template <typename T>
|
||||
using GradKernel =
|
||||
op::SeqConcatGradKernel<paddle::platform::CPUDeviceContext, T>;
|
||||
REGISTER_OP_CPU_KERNEL(sequence_concat_grad, GradKernel<float>,
|
||||
GradKernel<double>);
|
||||
|
@ -1,23 +1,26 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/operators/sequence_concat_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
sequence_concat,
|
||||
ops::SequenceConcatOpKernel<paddle::platform::CUDADeviceContext, float>);
|
||||
REGISTER_OP_CUDA_KERNEL(sequence_concat_grad,
|
||||
ops::SequenceConcatGradOpKernel<
|
||||
paddle::platform::CUDADeviceContext, float>);
|
||||
template <typename T>
|
||||
using Kernel =
|
||||
paddle::operators::SeqConcatKernel<paddle::platform::CUDADeviceContext, T>;
|
||||
REGISTER_OP_CUDA_KERNEL(sequence_concat, Kernel<float>, Kernel<double>);
|
||||
template <typename T>
|
||||
using GradKernel =
|
||||
paddle::operators::SeqConcatGradKernel<paddle::platform::CUDADeviceContext,
|
||||
T>;
|
||||
REGISTER_OP_CUDA_KERNEL(sequence_concat_grad, GradKernel<float>,
|
||||
GradKernel<double>);
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,45 @@
|
||||
# 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.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestSequenceConcat(OpTest):
|
||||
def setUp(self):
|
||||
x1 = np.random.random(size=(10, 80))
|
||||
lod1 = [7, 3]
|
||||
x2 = np.random.random(size=(20, 80))
|
||||
lod2 = [12, 8]
|
||||
|
||||
out = np.concatenate((x1[0:lod1[0]], x2[0:lod2[0]], x1[lod1[0]:],
|
||||
x2[lod2[0]:]))
|
||||
out_lod = [19, 11]
|
||||
|
||||
self.op_type = "sequence_concat"
|
||||
self.inputs = {'X': [("x1", (x1, [lod1])), ("x2", (x2, [lod2]))]}
|
||||
self.outputs = {"Out": (out, [out_lod])}
|
||||
|
||||
def test_output(self):
|
||||
self.check_output(1e-3)
|
||||
|
||||
def test_dx(self):
|
||||
self.check_grad(inputs_to_check=['x1', 'x2'], output_names="Out")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue