MKLPackedRecurrentLayer.cpp 4.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
T
tensor-tang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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 "MKLPackedRecurrentLayer.h"

namespace paddle {

REGISTER_LAYER(mkl_packed_recurrent, MKLPackedRecurrentLayer);

bool MKLPackedRecurrentLayer::init(const LayerMap& layerMap,
                                   const ParameterMap& parameterMap) {
T
tensor-tang 已提交
23 24 25 26 27 28
  if (!RecurrentLayer::init(layerMap, parameterMap)) return false;
  packed_weight_.reset(new MKLPackedWeight(weight_->getW()));
  packed_weight_->pack();
  if (needGradient_) {
    packed_weightT_.reset(new MKLPackedWeight(weight_->getW(), true));
    packed_weightT_->pack();
T
tensor-tang 已提交
29 30 31 32 33
  }
  return true;
}

void MKLPackedRecurrentLayer::backward(const UpdateCallback& callback) {
T
tensor-tang 已提交
34 35 36 37
  RecurrentLayer::backward(callback);
  packed_weight_->pack();
  if (needGradient_) {
    packed_weightT_->pack();
T
tensor-tang 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  }
}

void MKLPackedRecurrentLayer::forwardBatch(int batchSize,
                                           size_t numSequences,
                                           const int* starts) {
  if (!batchValue_) {
    batchValue_.reset(new SequenceToBatch(useGpu_));
  }

  batchValue_->resizeOrCreateBatch(batchSize, numSequences, starts, reversed_);

  batchValue_->copyFromSeq(*output_.value);

  {
    REGISTER_TIMER_INFO("RecurrentFwBatch", getName().c_str());
    /* forward one batch */
    for (size_t n = 0; n < batchValue_->getNumBatch(); n++) {
T
tensor-tang 已提交
56
      MatrixPtr batchValue = batchValue_->getBatchValue(n);
T
tensor-tang 已提交
57 58

      if (n != 0) {
T
tensor-tang 已提交
59 60
        MatrixPtr preBatchValue =
            batchValue_->getBatchValue(n - 1, batchValue->getHeight());
T
tensor-tang 已提交
61

62
        packed_weight_->gemm_compute(preBatchValue, batchValue);
T
tensor-tang 已提交
63
      }
T
tensor-tang 已提交
64 65 66
      Argument arg;
      arg.value = batchValue;
      activation_->forward(arg).check();
T
tensor-tang 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    }
  }
  batchValue_->copyBackSeq(*output_.value);
}

void MKLPackedRecurrentLayer::backwardBatch(int batchSize,
                                            size_t numSequences,
                                            const int* starts) {
  if (!batchGrad_) {
    batchGrad_.reset(new SequenceToBatch(useGpu_));
  }
  batchGrad_->shareIndexWith(*batchValue_);

  size_t numBatch = batchGrad_->getNumBatch();
  bool backwardByBatch = numBatch < numSequences;

  batchGrad_->copyFromSeq(*output_.grad);
  {
    REGISTER_TIMER_INFO("RecurrentBwData", getName().c_str());
    /* backward one batch */
    for (int n = (int)numBatch - 1; n >= 0; n--) {
T
tensor-tang 已提交
88 89 90
      MatrixPtr batchGrad = batchGrad_->getBatchValue(n);
      MatrixPtr batchValue =
          batchValue_->getBatchValue(n, batchGrad->getHeight());
T
tensor-tang 已提交
91 92

      Argument arg;
T
tensor-tang 已提交
93 94
      arg.value = batchValue;
      arg.grad = batchGrad;
T
tensor-tang 已提交
95 96 97
      activation_->backward(arg).check();

      if (n != 0) {
T
tensor-tang 已提交
98
        batchValue = batchGrad_->getBatchValue(n - 1, batchGrad->getHeight());
99
        packed_weightT_->gemm_compute(batchGrad, batchValue);
T
tensor-tang 已提交
100 101 102 103 104
      }

      if (backwardByBatch && weight_->getWGrad()) {
        if (n != 0) {
          /* backward weight */
T
tensor-tang 已提交
105 106 107 108
          batchValue =
              batchValue_->getBatchValue(n - 1, batchGrad->getHeight());
          weight_->getWGrad()->mul(
              *batchValue->getTranspose(), *batchGrad, 1, 1);
T
tensor-tang 已提交
109 110 111 112 113 114 115 116 117 118 119
        }
      }
    }
  }

  batchGrad_->copyBackSeq(*output_.grad);

  if (!backwardByBatch && weight_->getWGrad()) {
    REGISTER_TIMER_INFO("RecurrentBwWeight", getName().c_str());
    for (size_t seq = 0; seq < numSequences; ++seq) {
      int len = starts[seq + 1] - starts[seq];
T
tensor-tang 已提交
120 121 122 123 124 125 126 127
      weight_->getWGrad()->mul(
          *output_.value
               ->subMatrix(reversed_ ? starts[seq] + 1 : starts[seq], len - 1)
               ->getTranspose(),
          *output_.grad->subMatrix(reversed_ ? starts[seq] : starts[seq] + 1,
                                   len - 1),
          1,
          1);
T
tensor-tang 已提交
128 129 130 131 132
    }
  }
}

}  // namespace paddle