TensorApply.h 6.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13

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. */
H
hedaoyuan 已提交
14 15 16 17 18 19 20 21

#pragma once

namespace paddle {

/**
 * \brief The tensor evaluator classes.
 */
H
hedaoyuan 已提交
22
template <typename Derived, class T>
H
hedaoyuan 已提交
23 24 25
class TensorApply {
public:
  explicit INLINE TensorApply(const Derived& p)
H
hedaoyuan 已提交
26 27 28 29 30
      : data_(p.data_),
        stride_(p.stride_),
        height_(p.height_),
        width_(p.width_),
        useGpu_(p.useGpu_) {}
H
hedaoyuan 已提交
31

H
hedaoyuan 已提交
32 33 34 35
  INLINE T apply(int i, int j) const { return data_[i * stride_ + j]; }
  INLINE T apply(int index) const { return data_[index]; }
  INLINE T& applyRef(int i, int j) { return data_[i * stride_ + j]; }
  INLINE T& applyRef(int index) { return data_[index]; }
H
hedaoyuan 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

  INLINE size_t getWidth() const { return width_; }
  INLINE size_t getHeight() const { return height_; }
  INLINE bool isContiguous() const { return stride_ == width_ || height_ == 1; }
  INLINE bool useGpu() const { return useGpu_; }

  T* data_;
  size_t stride_;
  size_t height_;
  size_t width_;
  bool useGpu_;
};

/**
 * \brief The tensor evaluator classes.
 * evaluator for rvalues
 */
H
hedaoyuan 已提交
53
template <typename Derived, class T>
H
hedaoyuan 已提交
54 55 56
class TensorApply<const Derived, T> {
public:
  explicit INLINE TensorApply(const Derived& p)
H
hedaoyuan 已提交
57 58 59 60 61
      : data_(p.data_),
        stride_(p.stride_),
        height_(p.height_),
        width_(p.width_),
        useGpu_(p.useGpu_) {}
H
hedaoyuan 已提交
62

H
hedaoyuan 已提交
63 64
  INLINE T apply(int i, int j) const { return data_[i * stride_ + j]; }
  INLINE T apply(int index) const { return data_[index]; }
H
hedaoyuan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77

  INLINE size_t getWidth() const { return width_; }
  INLINE size_t getHeight() const { return height_; }
  INLINE bool isContiguous() const { return stride_ == width_ || height_ == 1; }
  INLINE bool useGpu() const { return useGpu_; }

  const T* data_;
  size_t stride_;
  size_t height_;
  size_t width_;
  bool useGpu_;
};

H
hedaoyuan 已提交
78
template <typename Derived, class T>
H
hedaoyuan 已提交
79 80 81
class TensorApply<const TensorExpression<Derived, T>, T> {
public:
  explicit TensorApply(const TensorExpression<Derived, T>& expr)
H
hedaoyuan 已提交
82
      : expr_(expr.derived()) {}
H
hedaoyuan 已提交
83

H
hedaoyuan 已提交
84 85
  INLINE T apply(int i, int j) const { return expr_.apply(i, j); }
  INLINE T apply(int index) const { return expr_.apply(index); }
H
hedaoyuan 已提交
86 87 88 89 90 91 92 93 94 95 96 97

  INLINE size_t getWidth() const { return expr_.getWidth(); }
  INLINE size_t getHeight() const { return expr_.getHeight(); }
  INLINE bool isContiguous() const { return expr_.isContiguous(); }
  INLINE bool useGpu() const { return expr_.useGpu(); }

  TensorApply<const Derived, T> expr_;
};

/**
 * \brief The unary expression evaluator classes.
 */
H
hedaoyuan 已提交
98
template <class OP, typename ArgType, class T>
H
hedaoyuan 已提交
99 100 101
class TensorApply<const TensorUnaryOp<OP, ArgType, T>, T> {
public:
  explicit INLINE TensorApply(const TensorUnaryOp<OP, ArgType, T>& expr)
H
hedaoyuan 已提交
102
      : op_(expr.op_), expr_(expr.expr_) {}
H
hedaoyuan 已提交
103

H
hedaoyuan 已提交
104 105
  INLINE T apply(int i, int j) const { return op_(expr_.apply(i, j)); }
  INLINE T apply(int index) const { return op_(expr_.apply(index)); }
H
hedaoyuan 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118

  INLINE size_t getWidth() const { return expr_.getWidth(); }
  INLINE size_t getHeight() const { return expr_.getHeight(); }
  INLINE bool isContiguous() const { return expr_.isContiguous(); }
  INLINE bool useGpu() const { return expr_.useGpu(); }

  const OP op_;
  TensorApply<ArgType, T> expr_;
};

/**
 * \brief The binary expression evaluator classes.
 */
H
hedaoyuan 已提交
119
template <class OP, typename LhsType, typename RhsType, class T>
H
hedaoyuan 已提交
120 121 122
class TensorApply<const TensorBinaryOp<OP, LhsType, RhsType, T>, T> {
public:
  explicit INLINE TensorApply(
H
hedaoyuan 已提交
123
      const TensorBinaryOp<OP, LhsType, RhsType, T>& expr)
H
hedaoyuan 已提交
124
      : op_(expr.op_), lhs_(expr.lhs_), rhs_(expr.rhs_) {
H
hedaoyuan 已提交
125 126 127 128 129
#ifndef __CUDA_ARCH__
    CHECK_EQ(lhs_.getWidth(), rhs_.getWidth());
    CHECK_EQ(lhs_.getHeight(), rhs_.getHeight());
    CHECK_EQ(lhs_.useGpu(), rhs_.useGpu());
#endif
H
hedaoyuan 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  }

  INLINE T apply(int i, int j) const {
    return op_(lhs_.apply(i, j), rhs_.apply(i, j));
  }
  INLINE T apply(int index) const {
    return op_(lhs_.apply(index), rhs_.apply(index));
  }

  INLINE size_t getWidth() const { return lhs_.getWidth(); }
  INLINE size_t getHeight() const { return rhs_.getHeight(); }
  INLINE bool isContiguous() const {
    return lhs_.isContiguous() && rhs_.isContiguous();
  }
  INLINE bool useGpu() const { return lhs_.useGpu(); }

  const OP op_;
  TensorApply<LhsType, T> lhs_;
  TensorApply<RhsType, T> rhs_;
};

/**
 * \brief The ternary expression evaluator classes.
 */
H
hedaoyuan 已提交
154
template <typename ArgType1, typename ArgType2, typename ArgType3, class T>
H
hedaoyuan 已提交
155 156 157
class TensorApply<const TensorTernaryOp<ArgType1, ArgType2, ArgType3, T>, T> {
public:
  explicit INLINE TensorApply(
H
hedaoyuan 已提交
158 159 160 161 162 163 164 165 166 167
      const TensorTernaryOp<ArgType1, ArgType2, ArgType3, T>& expr)
      : expr1_(expr.expr1_), expr2_(expr.expr2_), expr3_(expr.expr3_) {
#ifndef __CUDA_ARCH__
    CHECK_EQ(expr1_.getWidth(), expr2_.getWidth());
    CHECK_EQ(expr1_.getWidth(), expr3_.getWidth());
    CHECK_EQ(expr1_.getHeight(), expr2_.getHeight());
    CHECK_EQ(expr1_.getHeight(), expr3_.getHeight());
    CHECK_EQ(expr1_.useGpu(), expr2_.useGpu());
    CHECK_EQ(expr1_.useGpu(), expr3_.useGpu());
#endif
H
hedaoyuan 已提交
168 169 170 171 172 173 174 175 176 177 178 179
  }

  INLINE T apply(int i, int j) const {
    return expr1_.apply(i, j) ? expr2_.apply(i, j) : expr3_.apply(i, j);
  }
  INLINE T apply(int index) const {
    return expr1_.apply(index) ? expr2_.apply(index) : expr3_.apply(index);
  }

  INLINE size_t getWidth() const { return expr1_.getWidth(); }
  INLINE size_t getHeight() const { return expr1_.getHeight(); }
  INLINE bool isContiguous() const {
H
hedaoyuan 已提交
180 181
    return expr1_.isContiguous() && expr2_.isContiguous() &&
           expr3_.isContiguous();
H
hedaoyuan 已提交
182 183 184 185 186 187 188 189 190 191 192
  }
  INLINE bool useGpu() const { return expr1_.useGpu(); }

  TensorApply<ArgType1, T> expr1_;
  TensorApply<ArgType2, T> expr2_;
  TensorApply<ArgType3, T> expr3_;
};

/**
 * \brief The const expression evaluator classes.
 */
H
hedaoyuan 已提交
193
template <class OP, typename ArgType, class T>
H
hedaoyuan 已提交
194 195 196
class TensorApply<const TensorConstant<OP, ArgType, T>, T> {
public:
  explicit INLINE TensorApply(const TensorConstant<OP, ArgType, T>& expr)
H
hedaoyuan 已提交
197
      : op_(expr.op_), expr_(expr.expr_) {}
H
hedaoyuan 已提交
198

H
hedaoyuan 已提交
199 200
  INLINE T apply(int i, int j) const { return op_(i, j); }
  INLINE T apply(int index) const { return op_(index); }
H
hedaoyuan 已提交
201 202 203 204 205 206 207 208 209 210 211

  INLINE size_t getWidth() const { return expr_.getWidth(); }
  INLINE size_t getHeight() const { return expr_.getHeight(); }
  INLINE bool isContiguous() const { return true; }
  INLINE bool useGpu() const { return expr_.useGpu(); }

  const OP op_;
  TensorApply<ArgType, T> expr_;
};

}  // namespace paddle