MKLPackedRecurrentLayer.h 2.3 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

T
tensor-tang 已提交
15 16
#pragma once

T
tensor-tang 已提交
17 18
#include <gflags/gflags.h>
#include "Layer.h"
T
tensor-tang 已提交
19 20
#include "MKLPackedWeight.h"
#include "RecurrentLayer.h"
T
tensor-tang 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "SequenceToBatch.h"
#include "paddle/utils/Stat.h"

DECLARE_bool(rnn_use_batch);

namespace paddle {

/**
 * @brief MKLPackedRecurrentLayer takes 1 input layer. The output size is the
 * same with
 * input layer.
 * For each sequence [start, end] it performs the following computation:
 * \f[
 *    out_{i} = act(in_{i})     \      \      \text{for} \ i = start \\
 *    out_{i} = act(in_{i} + out_{i-1} * W) \ \ \text{for} \ start < i <= end
 *
 * \f]
 * If reversed is true, the order is reversed:
 * \f[
 *   out_{i} = act(in_{i})           \    \   \text{for} \ i = end  \\
 *   out_{i} = act(in_{i} + out_{i+1} * W) \ \ \text{for} \ start <= i < end
 * \f]
 * There are two methods to calculate rnn. One way is to compute rnn one
 * sequence by one sequence. The other way is to reorganize the input
 * into batches, then compute rnn one batch by one batch. Users can select
 * them by rnn_use_batch flag.
 */

T
tensor-tang 已提交
49
class MKLPackedRecurrentLayer : public RecurrentLayer {
T
tensor-tang 已提交
50
public:
T
tensor-tang 已提交
51 52
  explicit MKLPackedRecurrentLayer(const LayerConfig& config)
      : RecurrentLayer(config) {}
T
tensor-tang 已提交
53 54 55 56 57 58 59

  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override;

  void backward(const UpdateCallback& callback) override;

protected:
T
tensor-tang 已提交
60 61 62
  void forwardBatch(int batchSize,
                    size_t numSequences,
                    const int* starts) override;
T
tensor-tang 已提交
63

T
tensor-tang 已提交
64 65 66
  void backwardBatch(int batchSize,
                     size_t numSequences,
                     const int* starts) override;
T
tensor-tang 已提交
67 68

protected:
T
tensor-tang 已提交
69 70
  std::unique_ptr<MKLPackedWeight> packed_weight_;
  std::unique_ptr<MKLPackedWeight> packed_weightT_;
T
tensor-tang 已提交
71
};
T
tensor-tang 已提交
72 73

}  // namespace paddle