eigen.h 4.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yi Wang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
D
dzhwinter 已提交
16 17 18 19 20
// logging.h and windows.h conflict
#define GLOG_NO_ABBREVIATED_SEVERITIES
// solve static linking error in windows
// https://github.com/google/glog/issues/301
#define GOOGLE_GLOG_DLL_DECL
Y
Yi Wang 已提交
21

Y
Yi Wang 已提交
22
#include "paddle/fluid/framework/tensor.h"
Y
Yi Wang 已提交
23 24 25 26 27 28 29 30
#include "unsupported/Eigen/CXX11/Tensor"

namespace paddle {
namespace framework {

// EigenDim converts paddle::platform::DDim into Eigen::DSizes.
template <int D>
struct EigenDim {
Q
qijun 已提交
31
  using Type = Eigen::DSizes<Eigen::DenseIndex, D>;
Y
Yi Wang 已提交
32 33 34 35

  static Type From(const DDim& dims) {
    PADDLE_ENFORCE(arity(dims) == D, "D must match arity(DDim)");
    Type ret;
Q
qijun 已提交
36
    for (int64_t d = 0; d < arity(dims); d++) {
Y
Yi Wang 已提交
37 38 39 40 41 42 43
      ret[d] = dims[d];
    }
    return ret;
  }
};

// Interpret paddle::platform::Tensor as EigenTensor and EigenConstTensor.
44 45
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
Y
Yi Wang 已提交
46
struct EigenTensor {
47 48 49
  // TODO(qijun) Now, default type in unaligned, and we will make a benchmark on
  // the speed of aligned and unaligned version in future.
  using Type = Eigen::TensorMap<Eigen::Tensor<T, D, MajorType, IndexType>>;
Y
Yi Wang 已提交
50 51

  using ConstType =
52
      Eigen::TensorMap<Eigen::Tensor<const T, D, MajorType, IndexType>>;
Y
Yi Wang 已提交
53

D
dzhwinter 已提交
54
  static Type From(Tensor& tensor, DDim dims) {  // NOLINT
Y
Yi Wang 已提交
55 56 57
    return Type(tensor.data<T>(), EigenDim<D>::From(dims));
  }

D
dzhwinter 已提交
58 59 60
  static Type From(Tensor& tensor) {  // NOLINT
    return From(tensor, tensor.dims_);
  }  // NOLINT
Y
Yi Wang 已提交
61 62 63 64 65 66 67 68 69 70

  static ConstType From(const Tensor& tensor, DDim dims) {
    return ConstType(tensor.data<T>(), EigenDim<D>::From(dims));
  }

  static ConstType From(const Tensor& tensor) {
    return From(tensor, tensor.dims_);
  }
};

71 72
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
F
WIP  
fengjiayi 已提交
73
struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
D
dzhwinter 已提交
74 75
  static typename EigenMatrix::Type Reshape(Tensor& tensor,  // NOLINT
                                            int num_col_dims) {
F
WIP  
fengjiayi 已提交
76
    int rank = tensor.dims_.size();
F
fengjiayi 已提交
77 78
    PADDLE_ENFORCE(num_col_dims > 0 && num_col_dims < rank,
                   "`num_col_dims` must be between (0, rank_of_tensor).");
79
    return EigenMatrix::From(tensor,
F
fengjiayi 已提交
80
                             flatten_to_2d(tensor.dims(), num_col_dims));
F
WIP  
fengjiayi 已提交
81
  }
F
fengjiayi 已提交
82 83

  static typename EigenMatrix::ConstType Reshape(const Tensor& tensor,
F
fengjiayi 已提交
84
                                                 int num_col_dims) {
F
fengjiayi 已提交
85
    int rank = tensor.dims_.size();
F
fengjiayi 已提交
86 87
    PADDLE_ENFORCE(num_col_dims > 0 && num_col_dims < rank,
                   "`num_col_dims` must be between (0, rank_of_tensor).");
F
fengjiayi 已提交
88
    return EigenMatrix::From(tensor,
F
fengjiayi 已提交
89
                             flatten_to_2d(tensor.dims(), num_col_dims));
F
fengjiayi 已提交
90
  }
F
WIP  
fengjiayi 已提交
91
};
92

93 94 95
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
struct EigenVector : public EigenTensor<T, 1, MajorType, IndexType> {
96
  // Flatten reshapes a Tensor into an EigenVector.
D
dzhwinter 已提交
97
  static typename EigenVector::Type Flatten(Tensor& tensor) {  // NOLINT
F
fengjiayi 已提交
98
    return EigenVector::From(tensor, {product(tensor.dims_)});
Q
qijun 已提交
99 100
  }

D
dzhwinter 已提交
101 102
  static typename EigenVector::ConstType Flatten(
      const Tensor& tensor) {  // NOLINT
F
fengjiayi 已提交
103
    return EigenVector::From(tensor, {product(tensor.dims_)});
Q
qijun 已提交
104
  }
Y
Yi Wang 已提交
105 106
};

L
liaogang 已提交
107 108 109 110 111 112 113 114 115
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
struct EigenScalar {
  // Scalar tensor (implemented as a rank-0 tensor) of scalar type T.
  using Type = Eigen::TensorMap<
      Eigen::TensorFixedSize<T, Eigen::Sizes<>, MajorType, IndexType>>;
  using ConstType = Eigen::TensorMap<
      Eigen::TensorFixedSize<const T, Eigen::Sizes<>, MajorType, IndexType>>;

D
dzhwinter 已提交
116
  static Type From(Tensor& tensor) { return Type(tensor.data<T>()); }  // NOLINT
L
liaogang 已提交
117 118 119 120 121 122

  static ConstType From(const Tensor& tensor) {
    return ConstType(tensor.data<T>());
  }
};

Y
Yi Wang 已提交
123 124
}  // namespace framework
}  // namespace paddle