-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add a nest sequence select layer. #3297
Changes from all commits
c0ecd5c
4b39f92
d3e3014
6e291b3
00b6d26
83ce2dc
29fa73b
ffafc5c
42c102a
94686c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* 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 "Layer.h" | ||
#include "paddle/math/Matrix.h" | ||
#include "paddle/math/Vector.h" | ||
#include "paddle/utils/Logging.h" | ||
#include "paddle/utils/Stat.h" | ||
|
||
namespace paddle { | ||
|
||
class SubNestedSequenceLayer : public Layer { | ||
public: | ||
explicit SubNestedSequenceLayer(const LayerConfig& config) : Layer(config) {} | ||
|
||
bool init(const LayerMap& layerMap, | ||
const ParameterMap& parameterMap) override; | ||
|
||
void forward(PassType passType) override; | ||
void backward(const UpdateCallback& callback = nullptr) override; | ||
|
||
private: | ||
/* | ||
* This functions generates the indices of rows in a batch according to the | ||
* indices of selected sub-sequence in each sequence. | ||
* | ||
* Examples: | ||
* selectedIndices: | ||
* [ | ||
* [0, 1, -1], | ||
* [0, 1, 2], | ||
* [0, -1, -1], | ||
* [0, 2, 3], | ||
* ] | ||
* inputSeqInfo: | ||
* [ | ||
* [0,3,4], | ||
* [4,5,7,10,15], | ||
* [15,20], | ||
* [20,22,23,25,28] | ||
* ] | ||
* | ||
* ths output is saved to private member rowIndice_; | ||
* [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||
* 16,17,18,19,20,21,22,23,24,25,26,27] | ||
*/ | ||
|
||
void calSelectedCols(const MatrixPtr selectedIndices, | ||
const std::vector<std::vector<int>>& inputSeqInfo); | ||
|
||
// if the second input of this layer is on GPU memory, copy it to CPU memory. | ||
MatrixPtr selIdsCpu_; | ||
|
||
// reorganized sequenceStartPositions and subSequenceStartPositions | ||
// into a 2d vector to facilitate the sequence selection process. | ||
std::vector<std::vector<int>> inputSeqInfoVec_; | ||
|
||
// the final selected row indices in a batch, | ||
// rowIdx_ and selectedRows_ actually share a same memory. | ||
IVectorPtr rowIndice_; | ||
std::vector<int> selectedRows_; | ||
}; | ||
|
||
REGISTER_LAYER(sub_nested_seq, SubNestedSequenceLayer); | ||
|
||
bool SubNestedSequenceLayer::init(const LayerMap& layerMap, | ||
const ParameterMap& parameterMap) { | ||
/* Initialize the basic parent class */ | ||
Layer::init(layerMap, parameterMap); | ||
CHECK_EQ(2U, inputLayers_.size()); | ||
setNeedSequenceInfo(false); | ||
return true; | ||
} | ||
|
||
void SubNestedSequenceLayer::calSelectedCols( | ||
const MatrixPtr selectedIndices, | ||
const std::vector<std::vector<int>>& inputSeqInfo) { | ||
selectedRows_.clear(); | ||
|
||
std::vector<int> outSeqStartInfo(1, 0); | ||
std::vector<int> outSubSeqStartInfo(1, 0); | ||
|
||
size_t seqNum = selectedIndices->getHeight(); | ||
size_t beamSize = selectedIndices->getWidth(); | ||
for (size_t i = 0; i < seqNum; ++i) { | ||
for (size_t j = 0; j < beamSize; ++j) { | ||
if (selectedIndices->getElement(i, j) == -1.) break; | ||
int selSubSeqIdx = selectedIndices->getElement(i, j); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 95行挪到96行后面:不用访问两次selectedIndices了。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里比较的是一个float。 |
||
CHECK_GT(inputSeqInfoVec_[i].size() - 1, selSubSeqIdx); | ||
|
||
size_t subSeqLen = inputSeqInfoVec_[i][selSubSeqIdx + 1] - | ||
inputSeqInfoVec_[i][selSubSeqIdx]; | ||
for (size_t k = 0; k < subSeqLen; ++k) | ||
selectedRows_.push_back(inputSeqInfoVec_[i][selSubSeqIdx] + k); | ||
outSubSeqStartInfo.push_back(outSubSeqStartInfo.back() + subSeqLen); | ||
} | ||
outSeqStartInfo.push_back(outSubSeqStartInfo.back()); | ||
} | ||
|
||
if (useGpu_) { | ||
rowIndice_ = IVector::create(selectedRows_.size(), useGpu_); | ||
rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size()); | ||
} else { | ||
rowIndice_ = | ||
IVector::create(selectedRows_.data(), selectedRows_.size(), useGpu_); | ||
} | ||
|
||
// create the sequence information for the output. | ||
ICpuGpuVector::resizeOrCreate( | ||
output_.sequenceStartPositions, outSeqStartInfo.size(), false); | ||
output_.sequenceStartPositions->copyFrom( | ||
outSeqStartInfo.data(), outSeqStartInfo.size(), false); | ||
|
||
ICpuGpuVector::resizeOrCreate( | ||
output_.subSequenceStartPositions, outSubSeqStartInfo.size(), false); | ||
output_.subSequenceStartPositions->copyFrom( | ||
outSubSeqStartInfo.data(), outSubSeqStartInfo.size(), false); | ||
} | ||
|
||
void SubNestedSequenceLayer::forward(PassType passType) { | ||
Layer::forward(passType); | ||
|
||
const Argument& inputSeq = getInput(0); | ||
CHECK(inputSeq.hasSubseq()) << "The first input of SubNestSequence layer " | ||
<< "must be a nested sequence."; | ||
const MatrixPtr selectedIndices = getInputValue(1); | ||
CHECK_EQ(inputSeq.getNumSequences(), selectedIndices->getHeight()); | ||
|
||
if (dynamic_cast<GpuMatrix*>(selectedIndices.get())) { | ||
/* | ||
* Currently, the second input for this layer is generated by | ||
* kmax_sequence_score_layer whose output is always stored on CPU, | ||
* or a data_layer which canbe on GPU. | ||
* | ||
* If the second input is on GPU, copy it to CPU memory, because this | ||
* input always uses very few memory, and operations related to it are | ||
* all logic control, not computations. | ||
*/ | ||
Matrix::resizeOrCreate(selIdsCpu_, | ||
selectedIndices->getHeight(), | ||
selectedIndices->getWidth(), | ||
false /* trans */, | ||
false /* useGpu */); | ||
selIdsCpu_->copyFrom(*selectedIndices); | ||
} else { | ||
selIdsCpu_ = selectedIndices; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 132-150行需要移入calSelectedCols函数么,selIdsCpu也是一个临时变量,除了在calSelectedCols里面用,其他地方没有用到。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
|
||
Argument::reorganizeSeqInfo(inputSeq.sequenceStartPositions, | ||
inputSeq.subSequenceStartPositions, | ||
inputSeqInfoVec_); | ||
calSelectedCols(selIdsCpu_, inputSeqInfoVec_); | ||
|
||
resetOutput(selectedRows_.size(), getSize()); | ||
getOutputValue()->selectRows(*getInputValue(0), *rowIndice_); | ||
} | ||
|
||
void SubNestedSequenceLayer::backward(const UpdateCallback& callback) { | ||
MatrixPtr inputSeqGrad = getInputGrad(0); | ||
MatrixPtr outputGrad = getOutputGrad(); | ||
|
||
if (inputSeqGrad) outputGrad->addToRows(*inputSeqGrad, *rowIndice_); | ||
} | ||
|
||
} // namespace paddle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
55行的示例不太好,rowIndice是连续的输出,有更好的示例么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,这几个layer 都是为了做 beam training 添加的,一定会整体联调,再完善一遍注释。