eigen.h 5.7 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 16

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

W
wanghuancoder 已提交
17 18
#include <stdint.h>

Y
Yi Wang 已提交
19
#include "paddle/fluid/framework/tensor.h"
20
#include "paddle/phi/kernels/funcs/eigen/extensions.h"
Y
Yi Wang 已提交
21 22 23 24 25 26 27 28
#include "unsupported/Eigen/CXX11/Tensor"

namespace paddle {
namespace framework {

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

  static Type From(const DDim& dims) {
32 33
    PADDLE_ENFORCE_EQ(arity(dims),
                      D,
34 35 36
                      platform::errors::InvalidArgument(
                          "Input dimension size should be equal to %d, but "
                          "received dimension size is %d.",
37 38
                          arity(dims),
                          D));
Y
Yi Wang 已提交
39
    Type ret;
Q
qijun 已提交
40
    for (int64_t d = 0; d < arity(dims); d++) {
Y
Yi Wang 已提交
41 42 43 44 45 46 47
      ret[d] = dims[d];
    }
    return ret;
  }
};

// Interpret paddle::platform::Tensor as EigenTensor and EigenConstTensor.
48 49 50
template <typename T,
          size_t D,
          int MajorType = Eigen::RowMajor,
51
          typename IndexType = Eigen::DenseIndex>
Y
Yi Wang 已提交
52
struct EigenTensor {
53 54 55
  // 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 已提交
56 57

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

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

D
dzhwinter 已提交
64
  static Type From(Tensor& tensor) {  // NOLINT
65
    return From(tensor, tensor.dims());
D
dzhwinter 已提交
66
  }  // NOLINT
Y
Yi Wang 已提交
67 68 69 70 71 72

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

  static ConstType From(const Tensor& tensor) {
73
    return From(tensor, tensor.dims());
Y
Yi Wang 已提交
74 75 76
  }
};

77 78
template <typename T,
          int MajorType = Eigen::RowMajor,
79
          typename IndexType = Eigen::DenseIndex>
F
WIP  
fengjiayi 已提交
80
struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
D
dzhwinter 已提交
81 82
  static typename EigenMatrix::Type Reshape(Tensor& tensor,  // NOLINT
                                            int num_col_dims) {
83
    int rank = tensor.dims().size();
84 85
    PADDLE_ENFORCE_EQ((num_col_dims > 0 && num_col_dims < rank),
                      true,
86 87 88
                      platform::errors::InvalidArgument(
                          "Input dimension number(num_col_dims) must be "
                          "between 0 and %d, but received number is %d.",
89 90
                          rank,
                          num_col_dims));
91
    return EigenMatrix::From(tensor,
92
                             phi::flatten_to_2d(tensor.dims(), num_col_dims));
F
WIP  
fengjiayi 已提交
93
  }
F
fengjiayi 已提交
94 95

  static typename EigenMatrix::ConstType Reshape(const Tensor& tensor,
F
fengjiayi 已提交
96
                                                 int num_col_dims) {
97
    int rank = tensor.dims().size();
98 99
    PADDLE_ENFORCE_EQ((num_col_dims > 0 && num_col_dims < rank),
                      true,
100 101 102
                      platform::errors::InvalidArgument(
                          "Input dimension number(num_col_dims) must be "
                          "between 0 and %d, but received number is %d.",
103 104
                          rank,
                          num_col_dims));
F
fengjiayi 已提交
105
    return EigenMatrix::From(tensor,
106
                             phi::flatten_to_2d(tensor.dims(), num_col_dims));
F
fengjiayi 已提交
107
  }
F
WIP  
fengjiayi 已提交
108
};
109

110 111
template <typename T,
          int MajorType = Eigen::RowMajor,
112 113
          typename IndexType = Eigen::DenseIndex>
struct EigenVector : public EigenTensor<T, 1, MajorType, IndexType> {
114
  // Flatten reshapes a Tensor into an EigenVector.
D
dzhwinter 已提交
115
  static typename EigenVector::Type Flatten(Tensor& tensor) {  // NOLINT
116
    return EigenVector::From(tensor, {product(tensor.dims())});
Q
qijun 已提交
117 118
  }

D
dzhwinter 已提交
119 120
  static typename EigenVector::ConstType Flatten(
      const Tensor& tensor) {  // NOLINT
121
    return EigenVector::From(tensor, {product(tensor.dims())});
Q
qijun 已提交
122
  }
Y
Yi Wang 已提交
123 124
};

125 126
template <typename T,
          int MajorType = Eigen::RowMajor,
L
liaogang 已提交
127 128 129 130 131 132 133 134
          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 已提交
135
  static Type From(Tensor& tensor) { return Type(tensor.data<T>()); }  // NOLINT
L
liaogang 已提交
136 137 138 139 140 141

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

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
// Define Tensor with 32-bit index.
template <typename T, int D, int MajorType = Eigen::RowMajor>
using Tensor32BitIndex =
    Eigen::TensorMap<Eigen::Tensor<T, D, MajorType, int>, Eigen::Aligned>;

template <typename DSizes>
Eigen::DSizes<int, DSizes::count> To32BitDims(const DSizes& in) {
  Eigen::DSizes<int, DSizes::count> out;
  for (int i = 0; i < DSizes::count; ++i) {
    out[i] = in[i];
  }
  return out;
}

template <typename EigenTensor>
Tensor32BitIndex<typename EigenTensor::Scalar, EigenTensor::NumIndices>
To32BitIndex(EigenTensor in) {
  using RetType =
      Tensor32BitIndex<typename EigenTensor::Scalar, EigenTensor::NumIndices>;
  return RetType(in.data(), To32BitDims(in.dimensions()));
}

Y
Yi Wang 已提交
164 165
}  // namespace framework
}  // namespace paddle