Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stride pooling for seqlastin and seqfirstin #1701

Merged
merged 13 commits into from
Apr 13, 2017
24 changes: 14 additions & 10 deletions paddle/gserver/layers/SequenceLastInstanceLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ namespace paddle {
* Input: a sequence
* If SequenceLevel = kNonseq:
* Output: a sequence containing only the last instance of the input sequence
* If stride_ > 0:
* Output: a shorten sequence. The operation of getting last instance of a
* sequence is independently performed on every slice of the input
* sequence, which is obtained by sliding a window with the window
* size set to stride_.
* If SequenceLevel = kSeq:
* Check input sequence must has sub-sequence
* Output: a sequence containing only the last instance of each sub-sequence
Expand All @@ -37,6 +42,7 @@ class SequenceLastInstanceLayer : public SequencePoolLayer {
protected:
MatrixPtr tmpSrc_;
MatrixPtr tmpDest_;
std::vector<int> instanceIds_;

public:
explicit SequenceLastInstanceLayer(const LayerConfig& config)
Expand All @@ -54,6 +60,7 @@ REGISTER_LAYER(seqlastins, SequenceLastInstanceLayer);
bool SequenceLastInstanceLayer::init(const LayerMap& layerMap,
const ParameterMap& parameterMap) {
SequencePoolLayer::init(layerMap, parameterMap);
reversed_ = config_.select_first();

tmpSrc_ =
Matrix::create(nullptr, /* height= */ 1, 1, /* trans= */ false, useGpu_);
Expand All @@ -66,17 +73,19 @@ bool SequenceLastInstanceLayer::init(const LayerMap& layerMap,
void SequenceLastInstanceLayer::forward(PassType passType) {
SequencePoolLayer::forward(passType);

const int* starts = startPositions_->getData(false);
auto starts = (stride_ > 0) ? stridePositions_->getData()
: startPositions_->getData(false);
MatrixPtr inputValue = getInputValue(0);
MatrixPtr outputValue = getOutputValue();

{
AsyncGpuBlock asyncGpuBlock;
REGISTER_TIMER_INFO("SequenceLastInstanceLayerForward", getName().c_str());

instanceIds_.clear();
for (size_t seqId = 0; seqId < newBatchSize_; ++seqId) {
int insId =
config_.select_first() ? starts[seqId] : starts[seqId + 1] - 1;
int insId = reversed_ ? starts[seqId] : starts[seqId + 1] - 1;
instanceIds_.push_back(insId);

outputValue->subMatrix(seqId, 1, tmpDest_)
->assign(*(inputValue->subMatrix(insId, 1, tmpSrc_)));
Expand All @@ -96,18 +105,13 @@ void SequenceLastInstanceLayer::backward(const UpdateCallback& callback) {

MatrixPtr inputGrad = getInputGrad(0);
MatrixPtr outputGrad = getOutputGrad();
const int* starts = startPositions_->getData(false);
size_t numSequences = startPositions_->getSize() - 1;

if (inputGrad) {
AsyncGpuBlock asyncGpuBlock;
REGISTER_TIMER_INFO("SequenceLastInstanceLayerBackward", getName().c_str());

for (size_t seqId = 0; seqId < numSequences; ++seqId) {
int insId =
config_.select_first() ? starts[seqId] : starts[seqId + 1] - 1;

inputGrad->subMatrix(insId, 1, tmpDest_)
for (size_t seqId = 0; seqId < newBatchSize_; ++seqId) {
inputGrad->subMatrix(instanceIds_[seqId], 1, tmpDest_)
->add(*(outputGrad->subMatrix(seqId, 1, tmpSrc_)));
}
}
Expand Down
12 changes: 10 additions & 2 deletions paddle/gserver/layers/SequencePoolLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bool SequencePoolLayer::init(const LayerMap& layerMap,
} else {
LOG(FATAL) << "Unknown trans_type: " << config_.trans_type();
}
stride_ = config_.seq_pool_stride();
setNeedSequenceInfo(false);
return true;
}
Expand All @@ -55,8 +56,6 @@ void SequencePoolLayer::forward(PassType passType) {
CHECK_EQ(starts->getData()[newBatchSize_], input.getBatchSize());
CHECK_EQ(newBatchSize_, starts->getSize() - 1);

resetOutput(newBatchSize_, dim);

/* If type_ = kNonSeq, both seq has or not has sub-seq degrade to a non-seq,
* thus, in this case, output_ has no sequenceStartPositions.
* If type_ = kSeq, seq has sub-seq degrades to a seq, thus, only in this
Expand All @@ -67,6 +66,15 @@ void SequencePoolLayer::forward(PassType passType) {
<< "when trans_type = seq, input must hasSubseq";
output_.degradeSequence(input);
}
if (stride_ > 0) {
CHECK_EQ(input.hasSubseq(), 0UL)
<< "sequence stride pooling is invalid for hasSubseq now";
output_.poolSequenceWithStride(
input, stride_, &stridePositions_, reversed_);
newBatchSize_ = stridePositions_->getSize() - 1;
}

resetOutput(newBatchSize_, dim);
}

void SequencePoolLayer::backward(const UpdateCallback& callback) {
Expand Down
9 changes: 9 additions & 0 deletions paddle/gserver/layers/SequencePoolLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ namespace paddle {
* Output: output size is the number of input sequences (NOT input instances)
* output[i] = seqlastin/average/max_{for each instance in this
* sequence}{input[i]}
* If stride_ > 0:
* Check input sequence must not have sub-sequence
* Output: a shorten sequence, pooling is performed upon a small local
* area
* If SequenceLevel = kSeq:
* Check input sequence must has sub-sequence
* Output: output size is the number of input sub-sequences
Expand All @@ -42,6 +46,11 @@ class SequencePoolLayer : public Layer {
enum SequenceLevel { kNonSeq = 0, kSeq = 1 };
size_t newBatchSize_;
ICpuGpuVectorPtr startPositions_;
int stride_;
// Store the start position of each window.
IVectorPtr stridePositions_;
// Whether the input sequence is reversed or not.
bool reversed_ = false;

public:
explicit SequencePoolLayer(const LayerConfig& config) : Layer(config) {}
Expand Down
38 changes: 26 additions & 12 deletions paddle/gserver/tests/test_LayerGrad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,14 @@ TEST(Layer, ExpandLayer) {
testExpandLayer("seq", true); // seq expand to hasSubseq
}

void testDegradeLayer(bool hasSubseq, string layer_type, string trans_type) {
void testDegradeLayer(bool hasSubseq,
string layer_type,
string trans_type,
int stride) {
TestConfig config;
config.layerConfig.set_type(layer_type);
config.layerConfig.set_size(10);
config.layerConfig.set_seq_pool_stride(stride);
config.biasSize = 0;

config.inputDefs.push_back(
Expand All @@ -827,36 +831,46 @@ void testDegradeLayer(bool hasSubseq, string layer_type, string trans_type) {
if (layer_type == "average") {
for (auto strategy : {"average", "sum", "squarerootn"}) {
LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type
<< " average_strategy=" << strategy;
<< " average_strategy=" << strategy
<< " seq_pool_stride=" << stride;
config.layerConfig.set_average_strategy(strategy);
testDegradeLayerGrad(config, layer_type);
}
} else {
LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type;
LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type
<< " seq_pool_stride=" << stride;
testDegradeLayerGrad(config, layer_type);
}
}

TEST(Layer, MaxLayer) {
testDegradeLayer(false, "max", "non-seq"); // seq max to non-seq
testDegradeLayer(true, "max", "non-seq"); // hasSubseq max to non-seq
testDegradeLayer(true, "max", "seq"); // hasSubseq max to seq
testDegradeLayer(false, "max", "non-seq", -1); // seq max to non-seq
testDegradeLayer(true, "max", "non-seq", -1); // hasSubseq max to non-seq
testDegradeLayer(true, "max", "seq", -1); // hasSubseq max to seq
}

TEST(Layer, SequenceLastInstanceLayer) {
testDegradeLayer(false,
"seqlastins",
"non-seq"); // seq seqlastins to non-seq
"non-seq",
-1); // seq seqlastins to non-seq
testDegradeLayer(false,
"seqlastins",
"non-seq",
5); // seq seqlastins to a shorten seq, stride window = 5
testDegradeLayer(true,
"seqlastins",
"non-seq"); // hasSubseq seqlastins to non-seq
testDegradeLayer(true, "seqlastins", "seq"); // hasSubseq seqlastins to seq
"non-seq",
-1); // hasSubseq seqlastins to non-seq
testDegradeLayer(
true, "seqlastins", "seq", -1); // hasSubseq seqlastins to seq
}

TEST(Layer, AverageLayer) {
testDegradeLayer(false, "average", "non-seq"); // seq average to non-seq
testDegradeLayer(true, "average", "non-seq"); // hasSubseq average to non-seq
testDegradeLayer(true, "average", "seq"); // hasSubseq average to seq
testDegradeLayer(false, "average", "non-seq", -1); // seq average to non-seq
testDegradeLayer(
true, "average", "non-seq", -1); // hasSubseq average to non-seq
testDegradeLayer(true, "average", "seq", -1); // hasSubseq average to seq
}

TEST(Layer, SequenceConcatLayer) {
Expand Down
43 changes: 43 additions & 0 deletions paddle/parameter/Argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,49 @@ void Argument::degradeSequence(const Argument& input) {
tgtBuf[numSequences] = numSubSequences;
}

void Argument::poolSequenceWithStride(const Argument& input,
size_t stride,
IVectorPtr* stridePostions,
bool reversed) {
// If input.sequenceStartPositions = [0, 9, 14, 17, 30] and stride = 5,
// then sequenceStartPositions = [0, 2, 3, 4, 7].
// If reversed = false, stridePostions = [0, 5, 9, 14, 17, 22, 27, 30];
// else reversed = true, stridePostions = [0, 4, 9, 14, 17, 20, 25, 30]

CHECK(input.sequenceStartPositions);
CHECK_EQ(input.hasSubseq(), 0UL);
CHECK_GT(stride, 0) << "stride must larger than 0";
size_t numSequences = input.getNumSequences();
ICpuGpuVector::resizeOrCreate(
sequenceStartPositions, numSequences + 1, false);
const int* starts = input.sequenceStartPositions->getData(false);
int* tgtBuf = sequenceStartPositions->getMutableData(false);
// first index of target sequence and stride positions are both 0
tgtBuf[0] = 0;
std::vector<int> stridePos;
for (size_t seqId = 0; seqId < numSequences; ++seqId) {
size_t seqLength = starts[seqId + 1] - starts[seqId];
stridePos.emplace_back(starts[seqId]);
if (seqLength == 0) {
// empty sequence
tgtBuf[seqId + 1] = tgtBuf[seqId];
} else {
int size = ceil((float)seqLength / stride);
tgtBuf[seqId + 1] = tgtBuf[seqId] + size;
for (int i = 0; i < size - 1; ++i) {
int cur = reversed ? starts[seqId + 1] - (size - 1 - i) * stride
: stridePos.back() + stride;
stridePos.emplace_back(cur);
}
}
}
stridePos.emplace_back(starts[numSequences]);
int size = stridePos.size();
CHECK_EQ(size - 1, tgtBuf[numSequences]);
IVector::resizeOrCreate(*stridePostions, size, false);
(*stridePostions)->copyFrom(stridePos.data(), size);
}

void Argument::getValueString(
std::unordered_map<std::string, std::string>* out) const {
if (value) {
Expand Down
9 changes: 9 additions & 0 deletions paddle/parameter/Argument.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ struct Argument {
*/
void degradeSequence(const Argument& input);

/*
After pooling with stride n (n is smaller than sequence length),
a long sequence will be shorten.
This function is invalid for sequence having sub-sequence.
*/
void poolSequenceWithStride(const Argument& input,
size_t stride,
IVectorPtr* stridePositions,
bool reversed = false);
/**
* @brief getValueString will return the argument's output in string. There
* are several kinds of output. The keys of output dictionary are 'value',
Expand Down
1 change: 1 addition & 0 deletions paddle/parameter/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_simple_unittest(test_common)
add_simple_unittest(test_argument)
57 changes: 57 additions & 0 deletions paddle/parameter/tests/test_argument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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 <gtest/gtest.h>
#include <paddle/parameter/Argument.h>

using namespace paddle; // NOLINT

TEST(Argument, poolSequenceWithStride) {
Argument input, output;
ICpuGpuVector::resizeOrCreate(input.sequenceStartPositions, 5, false);
int* inStart = input.sequenceStartPositions->getMutableData(false);
inStart[0] = 0;
inStart[1] = 9;
inStart[2] = 14;
inStart[3] = 17;
inStart[4] = 30;

int strideResult[] = {0, 5, 9, 14, 17, 22, 27, 30};
int strideResultReversed[] = {0, 4, 9, 14, 17, 20, 25, 30};

for (auto reversed : {false, true}) {
IVectorPtr stridePositions;
output.poolSequenceWithStride(
input, 5 /* stride */, &stridePositions, reversed);

const int* outStart = output.sequenceStartPositions->getData(false);
CHECK_EQ(outStart[0], 0);
CHECK_EQ(outStart[1], 2);
CHECK_EQ(outStart[2], 3);
CHECK_EQ(outStart[3], 4);
CHECK_EQ(outStart[4], 7);

CHECK_EQ(stridePositions->getSize(), 8);
auto result = reversed ? strideResultReversed : strideResult;
for (int i = 0; i < 8; i++) {
CHECK_EQ(stridePositions->getData()[i], result[i]);
}
}
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
initMain(argc, argv);
return RUN_ALL_TESTS();
}
5 changes: 5 additions & 0 deletions proto/ModelConfig.proto
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ message LayerConfig {

// blank label used in ctc loss
optional uint32 blank = 52 [default = 0];

// stride parameter for seqlastins layer, AverageLayer, MaxLayer, which
// controls the scope of pooling operation. can be set > 0.
// leave empty or set to -1 to disable this stride pooling.
optional int32 seq_pool_stride = 53 [default = -1];
}

message EvaluatorConfig {
Expand Down
18 changes: 13 additions & 5 deletions python/paddle/trainer/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,7 @@ def __init__(self,
active_type='linear',
trans_type='non-seq',
bias=False,
stride=-1,
**xargs):
super(SequenceLastInstanceLayer, self).__init__(
name,
Expand All @@ -2495,10 +2496,11 @@ def __init__(self,
**xargs)
config_assert(
len(inputs) == 1, 'SequenceLastInstanceLayer must have 1 input')
if trans_type == 'seq':
config_assert(stride == -1, 'subseq does not support stride window')
self.config.trans_type = trans_type
for input_index in xrange(len(self.inputs)):
input_layer = self.get_input_layer(input_index)
self.set_layer_size(input_layer.size)
self.config.seq_pool_stride = stride
self.set_layer_size(self.get_input_layer(0).size)
self.create_bias_parameter(bias, self.config.size)


Expand All @@ -2510,10 +2512,16 @@ def __init__(self,
active_type='linear',
trans_type='non-seq',
bias=False,
stride=-1,
**xargs):
super(SequenceFirstInstanceLayer, self).__init__(
name, inputs=inputs, active_type=active_type, bias=bias, **xargs)
self.config.trans_type = trans_type
name,
inputs=inputs,
active_type=active_type,
trans_type=trans_type,
bias=bias,
stride=stride,
**xargs)
self.config.select_first = True


Expand Down
Loading