MKLDNNMatrix.h 3.8 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2017 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. */

#pragma once

T
tensor-tang 已提交
17 18
#include <vector>
#include "Matrix.h"
T
tensor-tang 已提交
19 20 21 22 23 24 25 26 27 28 29 30
#include "mkldnn.hpp"
#include "paddle/parameter/Parameter.h"

namespace paddle {

class MKLDNNMatrix;
typedef std::shared_ptr<MKLDNNMatrix> MKLDNNMatrixPtr;

/**
 * @brief MKLDNN Matrix.
 *
 */
T
tensor-tang 已提交
31
class MKLDNNMatrix : public CpuMatrix, public mkldnn::memory {
T
tensor-tang 已提交
32
public:
T
tensor-tang 已提交
33 34 35 36 37
  MKLDNNMatrix(real* data,
               size_t height,
               size_t width,
               mkldnn::memory::primitive_desc pd)
      : CpuMatrix(data, height, width, false), mkldnn::memory(pd, data) {}
T
tensor-tang 已提交
38

T
tensor-tang 已提交
39 40
  ~MKLDNNMatrix() {}

41 42 43 44 45 46 47 48
  /**
   * Create MKLDNNMatrix from a MatrixPtr and memory primitive_desc
   */
  static MKLDNNMatrixPtr create(MatrixPtr m, mkldnn::memory::primitive_desc pd);

  /**
   * Create MKLDNNMatrix from a MatrixPtr and memory details info
   */
T
tensor-tang 已提交
49
  static MKLDNNMatrixPtr create(
50
      MatrixPtr m,
T
tensor-tang 已提交
51 52 53 54 55
      mkldnn::memory::dims dims,
      mkldnn::memory::format fmt,
      mkldnn::engine& eg,
      mkldnn::memory::data_type dtype = mkldnn::memory::data_type::f32);

56
public:
T
tensor-tang 已提交
57 58
  /**
   * Reorder this MKLDNNMatrix from other format.
T
refine  
tensor-tang 已提交
59 60 61
   * Support inplace reorder.
   * @note: this function would only reorder the data layout.
   *        will NOT change this original dim or format info
T
tensor-tang 已提交
62 63 64 65 66 67 68
   */
  void reorderDataFrom(const MKLDNNMatrixPtr& m,
                       memory::format srcFmt,
                       memory::dims targetDim);

  /**
   * Reorder this MKLDNNMatrix to other format.
T
refine  
tensor-tang 已提交
69 70 71
   * Support inplace reorder.
   * @note: this function would only reorder the data layout.
   *        will NOT change the dst dim or format info
T
tensor-tang 已提交
72 73 74 75 76
   */
  void reorderDataTo(const MKLDNNMatrixPtr& m,
                     memory::format dstFmt,
                     memory::dims targetDim);

77 78 79 80 81 82 83 84 85 86 87 88 89
  /**
   * Dimensionality reduction.
   * Change format "nchw --> nc" or "oihw --> oi" if the h and w are both 1
   */
  void downSpatial();

  /**
   * Update the memory data handle.
   * Caution: This will not check the buffer size of the data,
   *          it should be coverd by user.
   */
  void updateData(void* data) { set_data_handle(data); }

T
tensor-tang 已提交
90
  /**
T
tensor-tang 已提交
91
   * Get primitive descriptor.
T
tensor-tang 已提交
92
   */
T
refine  
tensor-tang 已提交
93 94 95
  mkldnn::memory::primitive_desc getPrimitiveDesc() {
    return this->get_primitive_desc();
  }
T
tensor-tang 已提交
96

T
tensor-tang 已提交
97
  /**
T
tensor-tang 已提交
98
   * Get memory descriptor.
T
tensor-tang 已提交
99
   */
T
refine  
tensor-tang 已提交
100
  mkldnn::memory::desc getMemoryDesc() { return getPrimitiveDesc().desc(); }
T
tensor-tang 已提交
101 102

  /**
103
   * Get dimensions.
T
tensor-tang 已提交
104
   */
T
tensor-tang 已提交
105
  mkldnn::memory::dims getDims() {
T
refine  
tensor-tang 已提交
106
    mkldnn::memory::desc md = getMemoryDesc();
107 108
    const int* src = md.data.dims;
    int ndims = md.data.ndims;
T
tensor-tang 已提交
109 110 111 112 113 114 115
    mkldnn::memory::dims dst;
    dst.resize(ndims);
    for (int i = 0; i < ndims; ++i) {
      dst[i] = src[i];
    }
    return dst;
  }
T
tensor-tang 已提交
116

T
tensor-tang 已提交
117 118 119 120
  /**
   * Get format.
   */
  mkldnn::memory::format getFormat() {
T
refine  
tensor-tang 已提交
121
    return (mkldnn::memory::format)(getMemoryDesc().data.format);
T
tensor-tang 已提交
122 123 124
  }

  /**
125
   * Get memory data type.
T
tensor-tang 已提交
126
   */
127
  mkldnn::memory::data_type getDtype() {
T
refine  
tensor-tang 已提交
128
    return (mkldnn::memory::data_type)(getMemoryDesc().data.data_type);
129 130 131 132 133
  }

  /**
   * Get engine.
   */
T
refine  
tensor-tang 已提交
134
  mkldnn::engine getEngine() { return getPrimitiveDesc().get_engine(); }
T
tensor-tang 已提交
135 136 137

protected:
  /**
T
refine  
tensor-tang 已提交
138 139
   * Do reorder once.
   * Can support inplace.
T
tensor-tang 已提交
140 141 142 143 144 145
   */
  void reorderOnce(void* srcData,
                   void* dstData,
                   memory::format srcFmt,
                   memory::format dstFmt,
                   memory::dims dm);
T
tensor-tang 已提交
146 147 148
};

}  // namespace paddle