MKLDNNMatrix.h 5.3 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:
33 34 35 36
  MKLDNNMatrix(CpuMatrixPtr m, mkldnn::memory::primitive_desc pd)
      : CpuMatrix(m->getData(), m->getHeight(), m->getWidth(), false),
        mkldnn::memory(pd, m->getData()),
        m_(m) {}
T
tensor-tang 已提交
37

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

40 41 42 43 44 45 46 47
  /**
   * 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 已提交
48
  static MKLDNNMatrixPtr create(
49
      MatrixPtr m,
T
tensor-tang 已提交
50 51 52 53 54
      mkldnn::memory::dims dims,
      mkldnn::memory::format fmt,
      mkldnn::engine& eg,
      mkldnn::memory::data_type dtype = mkldnn::memory::data_type::f32);

55 56 57 58 59 60 61 62 63 64 65 66 67
  /**
   * Create Memory descriptor.
   * default with any format and f32 dtype
   */
  static mkldnn::memory::desc createMemoryDesc(
      const mkldnn::memory::dims& dims,
      const mkldnn::memory::format& fmt = mkldnn::memory::format::any,
      const mkldnn::memory::data_type& dtype = mkldnn::memory::data_type::f32) {
    return mkldnn::memory::desc(dims, dtype, fmt);
  }

  /**
   * Create reorder primitive.
68 69 70 71 72 73
   * Create a mkldnn::reorder handle for converting src MKLDNNMatrix to dst.
   * checkData: for whether to check the data handle of src and dst is the same.
   *            if true, means check it and do not want support inplace reorder;
   *            otherwise do not check data which means the created reorder
   *            maybe inplace buffer and do not guarantee the logical is correct
   *            since not all format or conversion support inplace.
74 75 76 77 78 79
   */
  static std::shared_ptr<mkldnn::reorder> createReorder(
      const MKLDNNMatrixPtr& src,
      const MKLDNNMatrixPtr& dst,
      bool checkData = true);

80
public:
T
tensor-tang 已提交
81 82
  /**
   * Reorder this MKLDNNMatrix from other format.
T
refine  
tensor-tang 已提交
83 84 85
   * Support inplace reorder.
   * @note: this function would only reorder the data layout.
   *        will NOT change this original dim or format info
T
tensor-tang 已提交
86 87 88 89 90 91 92
   */
  void reorderDataFrom(const MKLDNNMatrixPtr& m,
                       memory::format srcFmt,
                       memory::dims targetDim);

  /**
   * Reorder this MKLDNNMatrix to other format.
T
refine  
tensor-tang 已提交
93 94 95
   * Support inplace reorder.
   * @note: this function would only reorder the data layout.
   *        will NOT change the dst dim or format info
T
tensor-tang 已提交
96 97 98 99 100
   */
  void reorderDataTo(const MKLDNNMatrixPtr& m,
                     memory::format dstFmt,
                     memory::dims targetDim);

101 102 103 104 105 106 107
  /**
   * Dimensionality reduction.
   * Change format "nchw --> nc" or "oihw --> oi" if the h and w are both 1
   */
  void downSpatial();

  /**
108
   * set the memory data handle.
109 110 111
   * Caution: This will not check the buffer size of the data,
   *          it should be coverd by user.
   */
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  void setData(real* data) {
    set_data_handle(data);
    CpuMatrix::setData(data);
    m_.reset();
  }

  /**
   * override Matrix::getData
   * check data before return
   */
  real* getData() override {
    CHECK_EQ((void*)data_, get_data_handle());
    return data_;
  }

  const real* getData() const override {
    CHECK_EQ((void*)data_, get_data_handle());
    return data_;
  }
131

T
tensor-tang 已提交
132
  /**
T
tensor-tang 已提交
133
   * Get primitive descriptor.
T
tensor-tang 已提交
134
   */
T
refine  
tensor-tang 已提交
135 136 137
  mkldnn::memory::primitive_desc getPrimitiveDesc() {
    return this->get_primitive_desc();
  }
T
tensor-tang 已提交
138

T
tensor-tang 已提交
139
  /**
T
tensor-tang 已提交
140
   * Get memory descriptor.
T
tensor-tang 已提交
141
   */
T
refine  
tensor-tang 已提交
142
  mkldnn::memory::desc getMemoryDesc() { return getPrimitiveDesc().desc(); }
T
tensor-tang 已提交
143 144

  /**
145
   * Get dimensions.
T
tensor-tang 已提交
146
   */
T
tensor-tang 已提交
147
  mkldnn::memory::dims getDims() {
T
refine  
tensor-tang 已提交
148
    mkldnn::memory::desc md = getMemoryDesc();
149 150
    const int* src = md.data.dims;
    int ndims = md.data.ndims;
T
tensor-tang 已提交
151 152 153 154 155 156 157
    mkldnn::memory::dims dst;
    dst.resize(ndims);
    for (int i = 0; i < ndims; ++i) {
      dst[i] = src[i];
    }
    return dst;
  }
T
tensor-tang 已提交
158

T
tensor-tang 已提交
159 160 161 162
  /**
   * Get format.
   */
  mkldnn::memory::format getFormat() {
T
refine  
tensor-tang 已提交
163
    return (mkldnn::memory::format)(getMemoryDesc().data.format);
T
tensor-tang 已提交
164 165 166
  }

  /**
167
   * Get memory data type.
T
tensor-tang 已提交
168
   */
169
  mkldnn::memory::data_type getDtype() {
T
refine  
tensor-tang 已提交
170
    return (mkldnn::memory::data_type)(getMemoryDesc().data.data_type);
171 172 173 174 175
  }

  /**
   * Get engine.
   */
T
refine  
tensor-tang 已提交
176
  mkldnn::engine getEngine() { return getPrimitiveDesc().get_engine(); }
T
tensor-tang 已提交
177 178 179

protected:
  /**
T
refine  
tensor-tang 已提交
180 181
   * Do reorder once.
   * Can support inplace.
T
tensor-tang 已提交
182 183 184 185 186 187
   */
  void reorderOnce(void* srcData,
                   void* dstData,
                   memory::format srcFmt,
                   memory::format dstFmt,
                   memory::dims dm);
188 189 190 191

private:
  // save the CpuMatrixPtr in case the buffer released outside
  CpuMatrixPtr m_;
T
tensor-tang 已提交
192 193 194
};

}  // namespace paddle