eigen.h 3.6 KB
Newer Older
Y
Yi Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

Y
Yi Wang 已提交
17
#include "paddle/framework/tensor.h"
Y
Yi Wang 已提交
18 19 20 21 22 23 24 25
#include "unsupported/Eigen/CXX11/Tensor"

namespace paddle {
namespace framework {

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

  static Type From(const DDim& dims) {
    PADDLE_ENFORCE(arity(dims) == D, "D must match arity(DDim)");
    Type ret;
Y
Yi Wang 已提交
31
    for (int d = 0; d < arity(dims); d++) {
Y
Yi Wang 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
      ret[d] = dims[d];
    }
    return ret;
  }
};

// Interpret paddle::platform::Tensor as EigenTensor and EigenConstTensor.
template <typename T, size_t D, typename IndexType = Eigen::DenseIndex>
struct EigenTensor {
  using Type = Eigen::TensorMap<Eigen::Tensor<T, D, Eigen::RowMajor, IndexType>,
                                Eigen::Aligned>;

  using ConstType =
      Eigen::TensorMap<Eigen::Tensor<const T, D, Eigen::RowMajor, IndexType>,
Y
Yi Wang 已提交
46
                       Eigen::Aligned>;
Y
Yi Wang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

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

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

  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_);
  }
};

// Interpret paddle::platform::Tensor as EigenVecotr and EigenConstVector.
template <typename T, typename IndexType = Eigen::DenseIndex>
struct EigenVector {
Y
Yi Wang 已提交
66 67
  using Type = Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>,
                                Eigen::Aligned>;
Y
Yi Wang 已提交
68

Y
Yi Wang 已提交
69
  using ConstType =
Y
Yi Wang 已提交
70 71
      Eigen::TensorMap<Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>,
                       Eigen::Aligned>;
Q
qijun 已提交
72
  // From is to transfer a one dimension Tensor into a one dimension EigenVector
Y
Yi Wang 已提交
73 74
  static Type From(Tensor& tensor) { return EigenTensor<T, 1>::From(tensor); }

Q
qijun 已提交
75 76 77 78 79 80
  // Flatten is to reshape a Tensor into a one dimension EigenVector
  static Type Flatten(Tensor& tensor) {
    return EigenTensor<T, 1>::From(
        tensor, make_ddim({static_cast<int>(product(tensor.dims_))}));
  }

Y
Yi Wang 已提交
81 82 83
  static ConstType From(const Tensor& tensor) {
    return EigenTensor<T, 1>::From(tensor);
  }
Q
qijun 已提交
84 85 86 87 88

  static ConstType Flatten(const Tensor& tensor) {
    return EigenTensor<T, 1>::From(
        tensor, make_ddim({static_cast<int>(product(tensor.dims_))}));
  }
Y
Yi Wang 已提交
89 90 91 92 93
};

// Interpret paddle::platform::Tensor as EigenMatrix and EigenConstMatrix.
template <typename T, typename IndexType = Eigen::DenseIndex>
struct EigenMatrix {
Y
Yi Wang 已提交
94 95
  using Type = Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, IndexType>,
                                Eigen::Aligned>;
Y
Yi Wang 已提交
96

Y
Yi Wang 已提交
97
  using ConstType =
Y
Yi Wang 已提交
98 99 100 101 102 103 104 105 106 107 108 109
      Eigen::TensorMap<Eigen::Tensor<const T, 2, Eigen::RowMajor, IndexType>,
                       Eigen::Aligned>;

  static Type From(Tensor& tensor) { return EigenTensor<T, 2>::From(tensor); }

  static ConstType From(const Tensor& tensor) {
    return EigenTensor<T, 2>::From(tensor);
  }
};

}  // namespace framework
}  // namespace paddle