MKLPackedRecurrentLayer.h 1.8 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 "MKLPackedWeight.h"
#include "RecurrentLayer.h"
T
tensor-tang 已提交
19 20 21 22 23 24

DECLARE_bool(rnn_use_batch);

namespace paddle {

/**
25 26
 * @brief MKLPackedRecurrentLayer is almost the same with RecurrentLayer
 * but is optimized with MKL cblas packed gemm.
T
tensor-tang 已提交
27 28
 * More details:
 * https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/mkl/mkl_packed.md
T
tensor-tang 已提交
29 30
 */

T
tensor-tang 已提交
31
class MKLPackedRecurrentLayer : public RecurrentLayer {
T
tensor-tang 已提交
32
public:
T
tensor-tang 已提交
33 34
  explicit MKLPackedRecurrentLayer(const LayerConfig& config)
      : RecurrentLayer(config) {}
T
tensor-tang 已提交
35 36 37 38 39 40 41

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

  void backward(const UpdateCallback& callback) override;

protected:
T
tensor-tang 已提交
42 43 44
  void forwardBatch(int batchSize,
                    size_t numSequences,
                    const int* starts) override;
T
tensor-tang 已提交
45

T
tensor-tang 已提交
46 47 48
  void backwardBatch(int batchSize,
                     size_t numSequences,
                     const int* starts) override;
T
tensor-tang 已提交
49 50

protected:
51
  /// packed_weight_ contains same data with
T
tensor-tang 已提交
52
  /// RecurrentLayer::weight_ but is packed
T
tensor-tang 已提交
53
  std::unique_ptr<MKLPackedWeight> packed_weight_;
T
tensor-tang 已提交
54
  /// packed_weightT_ is the transposition matrix of packed_weight_
T
tensor-tang 已提交
55
  std::unique_ptr<MKLPackedWeight> packed_weightT_;
T
tensor-tang 已提交
56
};
T
tensor-tang 已提交
57 58

}  // namespace paddle