提交 fbfd24e6 编写于 作者: Y Yu Yang

revert CRFLayer, remove wrong gpu support

Change-Id: I636cf13af5becb1168bc9749266b55580c46f6c9
上级 6a873f50
...@@ -47,81 +47,40 @@ bool CRFLayer::init(const LayerMap& layerMap, ...@@ -47,81 +47,40 @@ bool CRFLayer::init(const LayerMap& layerMap,
// We don't need sequenceStartPositions because each sample of output_ is // We don't need sequenceStartPositions because each sample of output_ is
// for the cost of one sequence. // for the cost of one sequence.
setNeedSequenceInfo(false); setNeedSequenceInfo(false);
if (useGpu_) {
tmpCpuInput_.reserve(inputLayers_.size());
for (size_t i = 0; i < inputLayers_.size(); i++) {
tmpCpuInput_.push_back(Argument());
}
}
return true; return true;
} }
void CRFLayer::forward(PassType passType) { void CRFLayer::forward(PassType passType) {
Layer::forward(passType); Layer::forward(passType);
if (useGpu_) {
for (size_t i = 0; i < inputLayers_.size(); i++) {
tmpCpuInput_[i].resizeAndCopyFrom(getInput(i), false, HPPL_STREAM_1);
}
VectorPtr cpuParameterValue;
VectorPtr cpuParameterGradient;
cpuParameterValue =
Vector::create(parameter_->getBuf(PARAMETER_VALUE)->getSize(), false);
cpuParameterValue->
copyFrom(*parameter_->getBuf(PARAMETER_VALUE), HPPL_STREAM_1);
if (parameter_->getBuf(PARAMETER_GRADIENT)) {
cpuParameterGradient =
Vector::create(parameter_->getBuf(PARAMETER_GRADIENT)->getSize(),
false);
cpuParameterGradient->
copyFrom(*parameter_->getBuf(PARAMETER_GRADIENT), HPPL_STREAM_1);
} else {
cpuParameterGradient = nullptr;
}
forwardImp(tmpCpuInput_[0], tmpCpuInput_[1], cpuParameterValue,
cpuParameterGradient);
parameter_->getBuf(PARAMETER_VALUE)->copyFrom(*cpuParameterValue,
HPPL_STREAM_1);
if (parameter_->getBuf(PARAMETER_GRADIENT)) {
parameter_->getBuf(PARAMETER_GRADIENT)->copyFrom(*cpuParameterGradient,
HPPL_STREAM_1);
}
} else {
forwardImp(getInput(0), getInput(1), parameter_->getBuf(PARAMETER_VALUE),
parameter_->getBuf(PARAMETER_GRADIENT));
}
}
void CRFLayer::forwardImp(const Argument&output, CHECK(!useGpu_) << "GPU is not supported";
const Argument& label,
VectorPtr parameterValue, const Argument& output = getInput(0);
VectorPtr parameterGradient) { const Argument& label = getInput(1);
CHECK(label.sequenceStartPositions); CHECK(label.sequenceStartPositions);
CHECK(label.ids); CHECK(label.ids);
int batchSize = output.getBatchSize(); int batchSize = output.getBatchSize();
size_t numSequences = label.sequenceStartPositions->getSize() - 1; size_t numSequences = label.sequenceStartPositions->getSize() - 1;
resizeOutput(numSequences, 1); resizeOutput(numSequences, 1);
std::vector<real> out(numSequences);
const int* starts = label.sequenceStartPositions->getData(false); const int* starts = label.sequenceStartPositions->getData(false);
CHECK_EQ(starts[numSequences], batchSize); CHECK_EQ(starts[numSequences], batchSize);
VectorPtr cpuParameterValue;
VectorPtr cpuParameterGradient;
for (size_t i = 0; i < numSequences; ++i) { for (size_t i = 0; i < numSequences; ++i) {
if (i >= crfs_.size()) { if (i >= crfs_.size()) {
crfs_.emplace_back(numClasses_, crfs_.emplace_back(numClasses_,
parameterValue->getData(), parameter_->getBuf(PARAMETER_VALUE)->getData(),
parameterGradient parameter_->getBuf(PARAMETER_GRADIENT)
? parameterGradient->getData() ? parameter_->getBuf(PARAMETER_GRADIENT)->getData()
: nullptr); : nullptr);
} }
out[i] = crfs_[i].forward( output_.value->getData()[i] = crfs_[i].forward(
output.value->getData() + numClasses_ * starts[i], output.value->getData() + numClasses_ * starts[i],
label.ids->getData() + starts[i], starts[i + 1] - starts[i]); label.ids->getData() + starts[i], starts[i + 1] - starts[i]);
} }
output_.value->copyFrom(out.data(), numSequences);
if (weightLayer_) { if (weightLayer_) {
const MatrixPtr& weight = getInputValue(*weightLayer_); const MatrixPtr& weight = getInputValue(*weightLayer_);
getOutputValue()->dotMul(*getOutputValue(), *weight); getOutputValue()->dotMul(*getOutputValue(), *weight);
...@@ -129,22 +88,8 @@ void CRFLayer::forwardImp(const Argument&output, ...@@ -129,22 +88,8 @@ void CRFLayer::forwardImp(const Argument&output,
} }
void CRFLayer::backward(const UpdateCallback &callback) { void CRFLayer::backward(const UpdateCallback &callback) {
(void)callback; const Argument& output = getInput(0);
if (useGpu_) { const Argument& label = getInput(1);
backwardImp(callback, tmpCpuInput_[0], tmpCpuInput_[1]);
const_cast<Argument&>(getInput(0)).
resizeAndCopyFrom(tmpCpuInput_[0], true, HPPL_STREAM_1);
const_cast<Argument&>(getInput(1)).
resizeAndCopyFrom(tmpCpuInput_[1], true, HPPL_STREAM_1);
} else {
backwardImp(callback, getInput(0), getInput(1));
}
}
void CRFLayer::backwardImp(const UpdateCallback& callback,
const Argument&output,
const Argument& label) {
const int* starts = label.sequenceStartPositions->getData(false); const int* starts = label.sequenceStartPositions->getData(false);
int numSequences = label.sequenceStartPositions->getSize() - 1; int numSequences = label.sequenceStartPositions->getSize() - 1;
...@@ -159,9 +104,11 @@ void CRFLayer::backwardImp(const UpdateCallback& callback, ...@@ -159,9 +104,11 @@ void CRFLayer::backwardImp(const UpdateCallback& callback,
grad->mulScalar(weight); grad->mulScalar(weight);
} }
} }
if (coeff_ != real(1.0f)) { if (coeff_ != real(1.0f)) {
output.grad->mulScalar(coeff_); output.grad->mulScalar(coeff_);
} }
parameter_->incUpdate(callback); parameter_->incUpdate(callback);
} }
......
...@@ -32,11 +32,7 @@ public: ...@@ -32,11 +32,7 @@ public:
explicit CRFLayer(const LayerConfig& config) : Layer(config) {} explicit CRFLayer(const LayerConfig& config) : Layer(config) {}
virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap); virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
virtual void forward(PassType passType); virtual void forward(PassType passType);
void forwardImp(const Argument&output, const Argument& label,
VectorPtr parameterValue, VectorPtr parameterGradient);
virtual void backward(const UpdateCallback& callback); virtual void backward(const UpdateCallback& callback);
void backwardImp(const UpdateCallback& callback, const Argument&output,
const Argument& label);
protected: protected:
size_t numClasses_; size_t numClasses_;
...@@ -44,7 +40,6 @@ protected: ...@@ -44,7 +40,6 @@ protected:
std::vector<LinearChainCRF> crfs_; std::vector<LinearChainCRF> crfs_;
LayerPtr weightLayer_; // weight for each sequence LayerPtr weightLayer_; // weight for each sequence
real coeff_; // weight for the layer real coeff_; // weight for the layer
std::vector<Argument> tmpCpuInput_;
}; };
} // namespace paddle } // namespace paddle
...@@ -179,10 +179,9 @@ TEST(Layer, CRFLayer) { ...@@ -179,10 +179,9 @@ TEST(Layer, CRFLayer) {
config.layerConfig.add_inputs(); config.layerConfig.add_inputs();
config.layerConfig.add_inputs(); config.layerConfig.add_inputs();
for (auto useGpu : {false, true}) { // Not support GPU now
testLayerGrad(config, "crf", 100, /* trans */ false, /* useGpu */ useGpu, testLayerGrad(config, "crf", 100, /* trans */ false, /* useGpu */ false,
false /*useWeight*/, 0.03 /*epsilon*/); false /*useWeight*/, 0.03 /*epsilon*/);
}
} }
TEST(Layer, CTCLayer) { TEST(Layer, CTCLayer) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册