SubNestedSequenceLayer.cpp 3.9 KB
Newer Older
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 28 29 30 31 32 33
/* 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:
C
caoying03 已提交
34 35 36
  void calSelectedCols(const MatrixPtr scores,
                       const int* seqStartPos,
                       const int* subSeqStartPos);
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  void buildOutputSeqInfo();

  std::vector<int> outSeqStartInfo_;
  std::vector<int> outSubSeqStartInfo_;

  MatrixPtr scoreOverInputSeq_;

  // 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;
}

C
caoying03 已提交
60 61 62
void SubNestedSequenceLayer::calSelectedCols(const MatrixPtr selected_indices,
                                             const int* seqStartPos,
                                             const int* subSeqStartPos) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  selectedRows_.clear();
  outSubSeqStartInfo_.resize(1, 0);
  outSeqStartInfo_.resize(1, 0);
}

void SubNestedSequenceLayer::buildOutputSeqInfo() {
  Argument& output = getOutput();

  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);
C
caoying03 已提交
84

85
  const Argument& inputSeq = getInput(0);
C
caoying03 已提交
86 87 88 89
  const MatrixPtr selected_indices = getInputValue(1);
  CHECK(inputSeq.hasSubseq()) << "The first input of SubNestSequence layer "
                              << "must be a nested sequence.";
  CHECK_EQ(inputSeq.getNumSequences(), selected_indices->getHeight());
90

C
caoying03 已提交
91 92 93
  calSelectedCols(selected_indices,
                  inputSeq.sequenceStartPositions->getMutableData(false),
                  inputSeq.subSequenceStartPositions->getMutableData(false));
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

  resetOutput(selectedRows_.size(), getSize());
  buildOutputSeqInfo();

  if (useGpu_) {
    rowIndice_ = IVector::create(selectedRows_.size(), useGpu_);
    rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size());
  } else {
    rowIndice_ =
        IVector::create(selectedRows_.data(), selectedRows_.size(), useGpu_);
  }

  getOutputValue()->selectRows(*getInputValue(0), *rowIndice_);
}

void SubNestedSequenceLayer::backward(const UpdateCallback& callback) {
C
caoying03 已提交
110
  MatrixPtr inputSeqGrad = getInputGrad(0);
111 112
  MatrixPtr outputGrad = getOutputGrad();

C
caoying03 已提交
113
  if (inputSeqGrad) outputGrad->addToRows(*inputSeqGrad, *rowIndice_);
114 115 116
}

}  // namespace paddle