commit
74d3ca8bab
@ -0,0 +1,29 @@
|
||||
/* 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 "paddle/framework/op_info.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
static OpInfoMap* g_op_info_map = nullptr;
|
||||
|
||||
OpInfoMap& OpInfoMap::Instance() {
|
||||
if (g_op_info_map == nullptr) {
|
||||
g_op_info_map = new OpInfoMap();
|
||||
}
|
||||
return *g_op_info_map;
|
||||
}
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,101 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "paddle/framework/attribute.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
class OperatorBase;
|
||||
using VariableNameMap = std::map<std::string, std::vector<std::string>>;
|
||||
|
||||
using OpCreator = std::function<OperatorBase*(
|
||||
const std::string& /*type*/, const VariableNameMap& /*inputs*/,
|
||||
const VariableNameMap& /*outputs*/, const AttributeMap& /*attrs*/)>;
|
||||
|
||||
struct OpInfo {
|
||||
OpCreator creator_;
|
||||
std::string grad_op_type_;
|
||||
OpProto* proto_;
|
||||
OpAttrChecker* checker_;
|
||||
|
||||
bool HasOpProtoAndChecker() const {
|
||||
return proto_ != nullptr && checker_ != nullptr;
|
||||
}
|
||||
|
||||
const OpProto& Proto() const {
|
||||
PADDLE_ENFORCE_NOT_NULL(proto_, "Operator Proto has not been registered");
|
||||
PADDLE_ENFORCE(proto_->IsInitialized(),
|
||||
"Operator Proto must be initialized in op info");
|
||||
return *proto_;
|
||||
}
|
||||
|
||||
const OpAttrChecker& Checker() const {
|
||||
PADDLE_ENFORCE_NOT_NULL(checker_,
|
||||
"Operator Checker has not been registered");
|
||||
return *checker_;
|
||||
}
|
||||
|
||||
const OpCreator& Creator() const {
|
||||
PADDLE_ENFORCE_NOT_NULL(creator_,
|
||||
"Operator Creator has not been registered");
|
||||
return creator_;
|
||||
}
|
||||
|
||||
bool HasGradientOp() const { return !grad_op_type_.empty(); }
|
||||
};
|
||||
|
||||
class OpInfoMap {
|
||||
public:
|
||||
static OpInfoMap& Instance();
|
||||
|
||||
OpInfoMap(const OpInfoMap& o) = delete;
|
||||
OpInfoMap(OpInfoMap&& o) = delete;
|
||||
OpInfoMap& operator=(const OpInfoMap& o) = delete;
|
||||
OpInfoMap& operator=(OpInfoMap&& o) = delete;
|
||||
|
||||
bool Has(const std::string& op_type) const {
|
||||
return map_.find(op_type) != map_.end();
|
||||
}
|
||||
|
||||
void Insert(const std::string& type, const OpInfo& info) {
|
||||
PADDLE_ENFORCE(!Has(type), "Operator %s has been registered", type);
|
||||
map_.insert({type, info});
|
||||
}
|
||||
|
||||
const OpInfo& Get(const std::string& type) const {
|
||||
auto it = map_.find(type);
|
||||
PADDLE_ENFORCE(it != map_.end(), "Operator %s are not found", type);
|
||||
return it->second;
|
||||
}
|
||||
|
||||
template <typename Callback>
|
||||
void IterAllInfo(Callback callback) {
|
||||
for (auto& it : map_) {
|
||||
callback(it.first, it.second);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
OpInfoMap() = default;
|
||||
std::unordered_map<std::string, const OpInfo> map_;
|
||||
};
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,220 @@
|
||||
/* 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 SequenceSliceLayer : public Layer {
|
||||
public:
|
||||
explicit SequenceSliceLayer(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:
|
||||
/*
|
||||
* TODO(caoying)
|
||||
* In PaddePaddle, currently all matrices are real number types,
|
||||
* but the second and the (optional) third input which are some
|
||||
* selected indices of the give sequence to trim the sequence, are actually
|
||||
* filled with int types so that storing int types information in real number
|
||||
* matrices is very dangerous, since real numbers will be convered to int
|
||||
* types. If a user fills this matrix himself, invalid data may occor.
|
||||
*/
|
||||
|
||||
MatrixPtr startIdsOnCpu_;
|
||||
MatrixPtr endIdsOnCpu_;
|
||||
|
||||
std::vector<int> selectedRows_;
|
||||
IVectorPtr rowIndice_;
|
||||
std::vector<std::vector<int>> inputSeqInfoVec_;
|
||||
std::vector<int> outSubSeqStartPos_;
|
||||
std::vector<int> outSeqStartPos_;
|
||||
|
||||
void checkInputs();
|
||||
void copySliceIdsToCpu();
|
||||
void calSelectedRows(const MatrixPtr starts, const MatrixPtr ends);
|
||||
};
|
||||
|
||||
REGISTER_LAYER(seq_slice, SequenceSliceLayer);
|
||||
|
||||
bool SequenceSliceLayer::init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) {
|
||||
/* Initialize the basic parent class */
|
||||
Layer::init(layerMap, parameterMap);
|
||||
CHECK_GE(inputLayers_.size(), 2U);
|
||||
CHECK_LE(inputLayers_.size(), 3U);
|
||||
|
||||
setNeedSequenceInfo(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SequenceSliceLayer::checkInputs() {
|
||||
const Argument& inputSeq = getInput(0);
|
||||
CHECK(inputSeq.hasSeq()) << "The first input of sequence slice layer "
|
||||
<< "must be a sequence.";
|
||||
const MatrixPtr indices1 = getInputValue(1);
|
||||
CHECK_EQ(static_cast<size_t>(indices1->getHeight()),
|
||||
inputSeq.hasSubseq() ? inputSeq.getNumSubSequences()
|
||||
: inputSeq.getNumSequences())
|
||||
<< "Height of the second input should be equal to number of sequence "
|
||||
<< "in the first input.";
|
||||
if (inputLayers_.size() == 3) {
|
||||
const MatrixPtr indices2 = getInputValue(2);
|
||||
CHECK_EQ(indices2->getHeight(), indices1->getHeight())
|
||||
<< "start indices and end indices should have the same height.";
|
||||
CHECK_EQ(indices2->getWidth(), indices1->getWidth())
|
||||
<< "start indices and end indices should have the same Width.";
|
||||
}
|
||||
}
|
||||
|
||||
void SequenceSliceLayer::copySliceIdsToCpu() {
|
||||
const MatrixPtr indices1 = getInputValue(1);
|
||||
if (inputLayers_.size() == 2U) {
|
||||
if (config_.select_first()) {
|
||||
Matrix::resizeOrCreate(startIdsOnCpu_,
|
||||
indices1->getHeight(),
|
||||
indices1->getWidth(),
|
||||
false /* trans */,
|
||||
false /* useGpu */);
|
||||
startIdsOnCpu_->copyFrom(*indices1);
|
||||
endIdsOnCpu_ = nullptr;
|
||||
} else {
|
||||
Matrix::resizeOrCreate(endIdsOnCpu_,
|
||||
indices1->getHeight(),
|
||||
indices1->getWidth(),
|
||||
false /* trans */,
|
||||
false /* useGpu */);
|
||||
endIdsOnCpu_->copyFrom(*indices1);
|
||||
startIdsOnCpu_ = nullptr;
|
||||
}
|
||||
} else if (inputLayers_.size() == 3U) {
|
||||
Matrix::resizeOrCreate(startIdsOnCpu_,
|
||||
indices1->getHeight(),
|
||||
indices1->getWidth(),
|
||||
false /* trans */,
|
||||
false /* useGpu */);
|
||||
startIdsOnCpu_->copyFrom(*indices1);
|
||||
|
||||
const MatrixPtr indices2 = getInputValue(2);
|
||||
Matrix::resizeOrCreate(endIdsOnCpu_,
|
||||
indices2->getHeight(),
|
||||
indices2->getWidth(),
|
||||
false /* trans */,
|
||||
false /* useGpu */);
|
||||
endIdsOnCpu_->copyFrom(*indices2);
|
||||
}
|
||||
}
|
||||
|
||||
void SequenceSliceLayer::calSelectedRows(const MatrixPtr starts,
|
||||
const MatrixPtr ends) {
|
||||
CHECK(starts || ends) << "At least one of the start or end indices "
|
||||
<< "should be given.";
|
||||
|
||||
outSeqStartPos_.resize(1, 0);
|
||||
outSubSeqStartPos_.resize(1, 0);
|
||||
selectedRows_.clear();
|
||||
|
||||
size_t beamSize = starts ? starts->getWidth() : ends->getWidth();
|
||||
size_t rowIdx = 0;
|
||||
for (size_t i = 0; i < inputSeqInfoVec_.size(); ++i) {
|
||||
for (size_t j = 0; j < inputSeqInfoVec_[i].size() - 1; ++j) {
|
||||
for (size_t k = 0; k < beamSize; ++k) {
|
||||
if (starts && starts->getElement(rowIdx, k) == -1.) break;
|
||||
if (ends && ends->getElement(rowIdx, k) == -1.) break;
|
||||
|
||||
int begPos = inputSeqInfoVec_[i][j];
|
||||
if (starts) begPos += starts->getElement(rowIdx, k);
|
||||
|
||||
int endPos = inputSeqInfoVec_[i][j + 1] - 1;
|
||||
if (ends) endPos = inputSeqInfoVec_[i][j] + ends->getElement(rowIdx, k);
|
||||
|
||||
int seqLen = endPos - begPos + 1;
|
||||
CHECK_GT(seqLen, 0U);
|
||||
for (int m = begPos; m <= endPos; ++m) selectedRows_.push_back(m);
|
||||
inputSeqInfoVec_.size() > 1
|
||||
? outSubSeqStartPos_.push_back(outSubSeqStartPos_.back() + seqLen)
|
||||
: outSeqStartPos_.push_back(outSeqStartPos_.back() + seqLen);
|
||||
}
|
||||
rowIdx++;
|
||||
}
|
||||
if (inputSeqInfoVec_.size() > 1)
|
||||
outSeqStartPos_.push_back(outSubSeqStartPos_.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, outSeqStartPos_.size(), false);
|
||||
output_.sequenceStartPositions->copyFrom(
|
||||
outSeqStartPos_.data(), outSeqStartPos_.size(), false);
|
||||
|
||||
if (inputSeqInfoVec_.size() > 1) {
|
||||
ICpuGpuVector::resizeOrCreate(
|
||||
output_.subSequenceStartPositions, outSubSeqStartPos_.size(), false);
|
||||
output_.subSequenceStartPositions->copyFrom(
|
||||
outSubSeqStartPos_.data(), outSubSeqStartPos_.size(), false);
|
||||
}
|
||||
}
|
||||
|
||||
void SequenceSliceLayer::forward(PassType passType) {
|
||||
Layer::forward(passType);
|
||||
checkInputs();
|
||||
|
||||
const Argument& inputSeq = getInput(0);
|
||||
inputSeqInfoVec_.clear();
|
||||
Argument::reorganizeSeqInfo(inputSeq.sequenceStartPositions,
|
||||
inputSeq.subSequenceStartPositions,
|
||||
inputSeqInfoVec_);
|
||||
if (!useGpu_) {
|
||||
if (inputLayers_.size() == 2U) {
|
||||
startIdsOnCpu_ = config_.select_first() ? getInputValue(1) : nullptr;
|
||||
endIdsOnCpu_ = config_.select_first() ? nullptr : getInputValue(1);
|
||||
} else if (inputLayers_.size() == 3U) {
|
||||
startIdsOnCpu_ = getInputValue(1);
|
||||
endIdsOnCpu_ = getInputValue(2);
|
||||
}
|
||||
} else
|
||||
copySliceIdsToCpu();
|
||||
|
||||
// calculate the selected row indices in a batch,
|
||||
// and build the output sequence information.
|
||||
calSelectedRows(startIdsOnCpu_ ? startIdsOnCpu_ : nullptr,
|
||||
endIdsOnCpu_ ? endIdsOnCpu_ : nullptr);
|
||||
|
||||
resetOutput(selectedRows_.size(), getSize());
|
||||
|
||||
getOutputValue()->selectRows(*getInputValue(0), *rowIndice_);
|
||||
}
|
||||
|
||||
void SequenceSliceLayer::backward(const UpdateCallback& callback) {
|
||||
getOutputGrad()->addToRows(*getInputGrad(0), *rowIndice_);
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,223 @@
|
||||
/* Copyright (c) 2016 Baidu, Inc. 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 "ModelConfig.pb.h"
|
||||
#include "paddle/gserver/layers/DataLayer.h"
|
||||
#include "paddle/trainer/Trainer.h"
|
||||
|
||||
#include "LayerGradUtil.h"
|
||||
#include "paddle/testing/TestUtil.h"
|
||||
|
||||
using namespace paddle; // NOLINT
|
||||
using namespace std; // NOLINT
|
||||
|
||||
DECLARE_int32(gpu_id);
|
||||
DECLARE_bool(thread_local_rand_use_global_seed);
|
||||
|
||||
const int MAX_SEQ_NUM = 17;
|
||||
const int MAX_SEQ_LEN = 23;
|
||||
const int MAX_BEAM_SIZE = 13;
|
||||
|
||||
vector<real> randSampling(real range, int n) {
|
||||
CHECK_GE(range, n);
|
||||
vector<real> num(range);
|
||||
iota(begin(num), end(num), 0.);
|
||||
if (range == n) return num;
|
||||
|
||||
random_shuffle(begin(num), end(num));
|
||||
num.resize(n);
|
||||
sort(begin(num), end(num));
|
||||
return num;
|
||||
}
|
||||
|
||||
void genSeqInfo(vector<int>& seqStartPos, vector<int>& subSeqStartPos) {
|
||||
seqStartPos.resize(1, 0);
|
||||
subSeqStartPos.resize(1, 0);
|
||||
|
||||
srand((size_t)(time(NULL)));
|
||||
int seqNum = 1 + (rand() % MAX_SEQ_NUM);
|
||||
for (int i = 0; i < seqNum; ++i) {
|
||||
int subSeqNum = 1 + (rand() % MAX_SEQ_NUM);
|
||||
for (int j = 0; j < subSeqNum; ++j)
|
||||
subSeqStartPos.push_back(subSeqStartPos.back() +
|
||||
(1 + (rand() % MAX_SEQ_LEN)));
|
||||
seqStartPos.push_back(subSeqStartPos.back());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
generate start indices according to sequence start positions.
|
||||
*/
|
||||
void genStarts(vector<int>& seqStartPos,
|
||||
vector<vector<real>>& starts,
|
||||
size_t beamSize) {
|
||||
starts.clear();
|
||||
starts.resize(seqStartPos.size() - 1, vector<real>(beamSize, -1.));
|
||||
|
||||
for (size_t i = 0; i < seqStartPos.size() - 1; ++i) {
|
||||
int seqLen = seqStartPos[i + 1] - seqStartPos[i];
|
||||
vector<real> randStarts =
|
||||
randSampling(seqLen, min(seqLen, static_cast<int>(beamSize)));
|
||||
copy(begin(randStarts), end(randStarts), begin(starts[i]));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
generate end indices according to sequence start positions and start indices.
|
||||
*/
|
||||
void genEnds(vector<int>& seqStartPos,
|
||||
vector<vector<real>>& starts,
|
||||
vector<vector<real>>& ends,
|
||||
size_t beamSize) {
|
||||
CHECK_EQ(seqStartPos.size() - 1, starts.size());
|
||||
ends.clear();
|
||||
ends.resize(seqStartPos.size() - 1, vector<real>(beamSize, -1.));
|
||||
|
||||
for (size_t i = 0; i < starts.size(); ++i) {
|
||||
for (size_t j = 0; j < starts[i].size(); ++j) {
|
||||
int seqLen = seqStartPos[i + 1] - seqStartPos[i];
|
||||
CHECK_GE(seqLen - 1, starts[i][j]);
|
||||
if (starts[i][j] == -1.) break;
|
||||
if (starts[i][j] == (seqLen - 1)) {
|
||||
ends[i][j] = starts[i][j];
|
||||
} else {
|
||||
ends[i][j] = starts[i][j] + randSampling(seqLen - starts[i][j], 1)[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void genTestData(vector<int>& seqStartPos,
|
||||
vector<int>& subSeqStartPos,
|
||||
vector<vector<real>>& starts,
|
||||
vector<vector<real>>& ends,
|
||||
bool hasSubseq) {
|
||||
size_t beamSize = 1 + (rand() % MAX_BEAM_SIZE);
|
||||
genSeqInfo(seqStartPos, subSeqStartPos);
|
||||
|
||||
genStarts(hasSubseq ? subSeqStartPos : seqStartPos, starts, beamSize);
|
||||
genEnds(hasSubseq ? subSeqStartPos : seqStartPos, starts, ends, beamSize);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void flatten2dVector(vector<vector<T>>& inVec, vector<T>& outVec) {
|
||||
size_t totalSize{0};
|
||||
for (auto const& items : inVec) totalSize += items.size();
|
||||
outVec.reserve(totalSize);
|
||||
|
||||
for (auto& items : inVec)
|
||||
move(items.begin(), items.end(), back_inserter(outVec));
|
||||
}
|
||||
|
||||
void testSeqSliceLayer(bool hasSubseq,
|
||||
bool useGpu,
|
||||
vector<int>& seqStartPos,
|
||||
vector<int>& subSeqStartPos,
|
||||
vector<vector<real>>& starts,
|
||||
vector<vector<real>>& ends) {
|
||||
// layer size is not crutial for this layer,
|
||||
// so here use a small layer size in the unittest.
|
||||
const size_t layerSize{4};
|
||||
TestConfig config;
|
||||
config.layerConfig.set_type("seq_slice");
|
||||
config.layerConfig.set_size(layerSize);
|
||||
|
||||
// add the first input
|
||||
MatrixPtr seqInputPtr =
|
||||
Matrix::create(hasSubseq ? subSeqStartPos.back() : seqStartPos.back(),
|
||||
layerSize,
|
||||
false,
|
||||
false);
|
||||
seqInputPtr->randomizeUniform();
|
||||
|
||||
if (hasSubseq) {
|
||||
config.inputDefs.push_back({INPUT_SELF_DEFINE_DATA,
|
||||
"seq_input",
|
||||
seqInputPtr,
|
||||
seqStartPos,
|
||||
subSeqStartPos});
|
||||
} else {
|
||||
config.inputDefs.push_back(
|
||||
{INPUT_SELF_DEFINE_DATA, "seq_input", seqInputPtr, seqStartPos});
|
||||
}
|
||||
config.layerConfig.add_inputs();
|
||||
|
||||
// add start indices
|
||||
if (starts.size()) {
|
||||
vector<real> startsToVec;
|
||||
flatten2dVector(starts, startsToVec);
|
||||
|
||||
MatrixPtr startMatrixPtr =
|
||||
Matrix::create(starts.size(), starts[0].size(), false, false);
|
||||
startMatrixPtr->copyFrom(startsToVec.data(), startsToVec.size());
|
||||
|
||||
config.inputDefs.push_back(
|
||||
{INPUT_SELF_DEFINE_DATA, "starts", startMatrixPtr});
|
||||
config.layerConfig.add_inputs();
|
||||
config.layerConfig.set_select_first(true);
|
||||
}
|
||||
|
||||
// add end indices
|
||||
if (ends.size()) {
|
||||
vector<real> endsToVec;
|
||||
flatten2dVector(ends, endsToVec);
|
||||
|
||||
MatrixPtr endMatrixPtr =
|
||||
Matrix::create(ends.size(), ends[0].size(), false, false);
|
||||
endMatrixPtr->copyFrom(endsToVec.data(), endsToVec.size());
|
||||
|
||||
config.inputDefs.push_back({INPUT_SELF_DEFINE_DATA, "ends", endMatrixPtr});
|
||||
config.layerConfig.add_inputs();
|
||||
config.layerConfig.set_select_first(false);
|
||||
}
|
||||
|
||||
testLayerGrad(config, "seq_slice", /*batchSize*/ 100, false, useGpu, false);
|
||||
}
|
||||
|
||||
TEST(Layer, SeqSliceLayer) {
|
||||
vector<int> seqStartPos;
|
||||
vector<int> subSeqStartPos;
|
||||
vector<vector<real>> starts;
|
||||
vector<vector<real>> ends;
|
||||
|
||||
std::vector<bool> mode = {false};
|
||||
#ifndef PADDLE_ONLY_CPU
|
||||
mode.push_back(true);
|
||||
#endif
|
||||
genSeqInfo(seqStartPos, subSeqStartPos);
|
||||
for (bool hasSubseq : {true, false}) {
|
||||
LOG(INFO) << "hasSubSeq : " << hasSubseq;
|
||||
genTestData(seqStartPos, subSeqStartPos, starts, ends, hasSubseq);
|
||||
for (bool useGpu : mode) {
|
||||
vector<vector<real>> tmp;
|
||||
testSeqSliceLayer(
|
||||
hasSubseq, useGpu, seqStartPos, subSeqStartPos, tmp, ends);
|
||||
testSeqSliceLayer(
|
||||
hasSubseq, useGpu, seqStartPos, subSeqStartPos, starts, tmp);
|
||||
testSeqSliceLayer(
|
||||
hasSubseq, useGpu, seqStartPos, subSeqStartPos, starts, ends);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
initMain(argc, argv);
|
||||
hl_start();
|
||||
hl_init(FLAGS_gpu_id);
|
||||
FLAGS_thread_local_rand_use_global_seed = true;
|
||||
srand(1);
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/* 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 "paddle/operators/gather_op.h"
|
||||
#include "paddle/framework/ddim.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class GatherOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
int batch_size = ctx.Input<Tensor>("Index")->dims()[0];
|
||||
PADDLE_ENFORCE_GE(batch_size, 0, "Batch size must be >0");
|
||||
framework::DDim output_dims(ctx.Input<Tensor>("X")->dims());
|
||||
output_dims[0] = batch_size;
|
||||
ctx.Output<Tensor>("Out")->Resize(output_dims);
|
||||
}
|
||||
};
|
||||
|
||||
class GatherGradOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
auto X_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
|
||||
auto X = ctx.Input<Tensor>("X");
|
||||
|
||||
X_grad->Resize(X->dims());
|
||||
}
|
||||
};
|
||||
|
||||
class GatherOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
GatherOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "The source input of gather op");
|
||||
AddInput("Index", "The index input of gather op");
|
||||
AddOutput("Out", "The output of add op");
|
||||
AddComment(R"DOC(
|
||||
Gather Operator by selecting from the first axis,
|
||||
|
||||
Out = X[Index]
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP(gather, ops::GatherOp, ops::GatherOpMaker, gather_grad,
|
||||
ops::GatherGradOp);
|
||||
REGISTER_OP_CPU_KERNEL(gather,
|
||||
ops::GatherOpKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
gather_grad,
|
||||
ops::GatherGradientOpKernel<paddle::platform::CPUPlace, float>);
|
@ -0,0 +1,20 @@
|
||||
/* 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. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/operators/gather_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(gather,
|
||||
ops::GatherOpKernel<paddle::platform::GPUPlace, float>);
|
@ -0,0 +1,53 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
#include "gather.h"
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "scatter.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
|
||||
template <typename Place, typename T>
|
||||
class GatherOpKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &ctx) const override {
|
||||
auto *X = ctx.Input<Tensor>("X");
|
||||
auto *Index = ctx.Input<Tensor>("Index");
|
||||
auto *Y = ctx.Output<Tensor>("Out");
|
||||
|
||||
Y->mutable_data<T>(ctx.GetPlace());
|
||||
Gather<T>(ctx.GetPlace(), X, Index, Y);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Place, typename T>
|
||||
class GatherGradientOpKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &ctx) const override {
|
||||
auto *Index = ctx.Input<Tensor>("Index");
|
||||
auto *dX = ctx.Output<Tensor>(framework::GradVarName("X"));
|
||||
auto *dO = ctx.Input<Tensor>(framework::GradVarName("Out"));
|
||||
|
||||
dX->mutable_data<T>(ctx.GetPlace());
|
||||
ScatterUpdate<T>(ctx.GetPlace(), dO, Index, dX);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue