compound_functors.h 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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

#include <string>
#include <unordered_set>
#include <vector>

namespace paddle {
namespace operators {
namespace math {

C
chengduo 已提交
25
// Z = BinaryFunctor(X, UnaryFunctor(Y))
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
template <typename T, typename BinaryFunctor, typename UnaryFunctor>
struct BinaryCompoundFunctor {
  BinaryCompoundFunctor(const BinaryFunctor func1, const UnaryFunctor func2)
      : func1_(func1), func2_(func2) {}

  inline HOSTDEVICE T GetOut(T x, T y) { return func1_(x, func2_(y)); }

  inline HOSTDEVICE T GetOutUseIntermediateOut(T x, T intermediat_out) {
    return func1_(x, intermediat_out);
  }

  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return func2_(y); }

  BinaryFunctor func1_;
  UnaryFunctor func2_;
};

C
chengduo 已提交
43
// Z = UnaryFunctor(BinaryFunctor(X, Y))
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
template <typename T, typename UnaryFunctor, typename BinaryFunctor>
struct UnaryCompoundFunctor {
  UnaryCompoundFunctor(const UnaryFunctor func1, const BinaryFunctor func2)
      : func1_(func1), func2_(func2) {}

  inline HOSTDEVICE T GetOut(T x, T y) { return func1_(func2_(x, y)); }

  inline HOSTDEVICE T GetOutUseIntermediateOut(T x, T intermediat_out) {
    return func1_(intermediat_out);
  }

  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return func2_(x, y); }

  UnaryFunctor func1_;
  BinaryFunctor func2_;
};

C
chengduo 已提交
61
// Z = BinaryFunctor(X, UnaryFunctor(Y))
62 63 64 65 66 67
template <typename T, typename DBinaryFun, typename UnaryFun>
struct BinaryCompoundGradDxFunctor {
  BinaryCompoundGradDxFunctor(const DBinaryFun &d_binary_fun,
                              const UnaryFun &unary_fun)
      : d_binary_fun_(d_binary_fun), unary_fun_(unary_fun) {}

C
chengduo 已提交
68
  inline HOSTDEVICE T Recompute(T x, T y, T out, T dout) {
69 70 71
    return dout * d_binary_fun_.Dx(x, unary_fun_(y));
  }

C
chengduo 已提交
72 73
  inline HOSTDEVICE T UseIntermediateOut(T x, T y, T intermediate_out, T out,
                                         T dout) {
74 75 76
    return dout * d_binary_fun_.Dx(x, intermediate_out);
  }

77 78
  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return unary_fun_(y); }

79 80 81 82 83
 private:
  DBinaryFun d_binary_fun_;
  UnaryFun unary_fun_;
};

C
chengduo 已提交
84
// Z = BinaryFunctor(X, UnaryFunctor(Y))
85
template <typename T, typename DBinaryFun, typename UnaryFun,
C
chengduo 已提交
86
          typename DUnaryFun, bool InPlace>
87 88 89 90 91 92 93 94
struct BinaryCompoundGradDyFunctor {
  BinaryCompoundGradDyFunctor(const DBinaryFun &d_binary_fun,
                              const UnaryFun &unary_fun,
                              const DUnaryFun &d_unary_fun)
      : d_binary_fun_(d_binary_fun),
        unary_fun_(unary_fun),
        d_unary_fun_(d_unary_fun) {}

C
chengduo 已提交
95 96
  inline HOSTDEVICE T Recompute(T x, T y, T out, T dout) {
    return dout * d_binary_fun_.Dy(x, unary_fun_(y)) * d_unary_fun_.UseX(y);
97 98
  }

C
chengduo 已提交
99 100 101 102 103 104 105 106 107
  inline HOSTDEVICE T UseIntermediateOut(T x, T y, T intermediate_out, T out,
                                         T dout) {
    if (InPlace) {
      return dout * d_binary_fun_.Dy(x, intermediate_out) *
             d_unary_fun_.UseOut(intermediate_out);
    } else {
      return dout * d_binary_fun_.Dy(x, intermediate_out) *
             d_unary_fun_.UseXAndOut(y, intermediate_out);
    }
108 109
  }

110 111
  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return unary_fun_(y); }

112 113 114 115 116 117
 private:
  DBinaryFun d_binary_fun_;
  UnaryFun unary_fun_;
  DUnaryFun d_unary_fun_;
};

C
chengduo 已提交
118
// Z = UnaryFunctor(BinaryFunctor(X, Y))
119
template <typename T, typename DUnaryFun, typename BinaryFun,
C
chengduo 已提交
120
          typename DBinaryFun, bool InPlace>
121 122 123 124 125 126 127 128
struct UnaryCompoundGradDxFunctor {
  UnaryCompoundGradDxFunctor(const DUnaryFun &d_unary_fun,
                             const BinaryFun &binary_fun,
                             const DBinaryFun &d_binary_fun)
      : d_unary_fun_(d_unary_fun),
        binary_fun_(binary_fun),
        d_binary_fun_(d_binary_fun) {}

C
chengduo 已提交
129
  inline HOSTDEVICE T Recompute(T x, T y, T out, T dout) {
130
    T base;
C
chengduo 已提交
131 132
    if (InPlace) {
      base = dout * d_unary_fun_.UseOut(out);
133
    } else {
C
chengduo 已提交
134
      base = dout * d_unary_fun_.UseXAndOut(binary_fun_(x, y), out);
135 136 137 138
    }
    return base * d_binary_fun_.Dx(x, y);
  }

C
chengduo 已提交
139 140
  inline HOSTDEVICE T UseIntermediateOut(T x, T y, T intermediate_out, T out,
                                         T dout) {
141
    T base;
C
chengduo 已提交
142 143
    if (InPlace) {
      base = dout * d_unary_fun_.UseOut(out);
144
    } else {
C
chengduo 已提交
145
      base = dout * d_unary_fun_.UseXAndOut(intermediate_out, out);
146 147 148 149
    }
    return base * d_binary_fun_.Dx(x, y);
  }

150 151
  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return binary_fun_(x, y); }

152 153 154 155 156 157
 private:
  DUnaryFun d_unary_fun_;
  BinaryFun binary_fun_;
  DBinaryFun d_binary_fun_;
};

C
chengduo 已提交
158
// Z = UnaryFunctor(BinaryFunctor(X, Y))
159
template <typename T, typename DUnaryFun, typename BinaryFun,
C
chengduo 已提交
160
          typename DBinaryFun, bool InPlace>
161 162 163 164 165 166 167 168
struct UnaryCompoundGradDyFunctor {
  UnaryCompoundGradDyFunctor(const DUnaryFun &d_unary_fun,
                             const BinaryFun &binary_fun,
                             const DBinaryFun &d_binary_fun)
      : d_unary_fun_(d_unary_fun),
        binary_fun_(binary_fun),
        d_binary_fun_(d_binary_fun) {}

C
chengduo 已提交
169
  inline HOSTDEVICE T Recompute(T x, T y, T out, T dout) {
170
    T base;
C
chengduo 已提交
171 172
    if (InPlace) {
      base = dout * d_unary_fun_.UseOut(out);
173
    } else {
C
chengduo 已提交
174
      base = dout * d_unary_fun_.UseXAndOut(binary_fun_(x, y), out);
175 176 177 178
    }
    return base * d_binary_fun_.Dy(x, y);
  }

C
chengduo 已提交
179 180
  inline HOSTDEVICE T UseIntermediateOut(T x, T y, T intermediate_out, T out,
                                         T dout) {
181
    T base;
C
chengduo 已提交
182 183
    if (InPlace) {
      base = dout * d_unary_fun_.UseOut(out);
184
    } else {
C
chengduo 已提交
185
      base = dout * d_unary_fun_.UseXAndOut(intermediate_out, out);
186 187 188 189
    }
    return base * d_binary_fun_.Dy(x, y);
  }

190 191
  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return binary_fun_(x, y); }

192 193 194 195 196 197
 private:
  DUnaryFun d_unary_fun_;
  BinaryFun binary_fun_;
  DBinaryFun d_binary_fun_;
};

C
chengduo 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
// Z = BinaryFunctor(X, UnaryFunctor(Y))
template <typename T, typename DBinaryFun, typename UnaryFun>
struct BinaryCompoundGradDIntermedaiteOutFunctor {
  BinaryCompoundGradDIntermedaiteOutFunctor(const DBinaryFun &d_binary_fun,
                                            const UnaryFun &unary_fun)
      : d_binary_fun_(d_binary_fun), unary_fun_(unary_fun) {}

  inline HOSTDEVICE T Recompute(T x, T y, T out, T dout) {
    return dout * d_binary_fun_.Dy(x, unary_fun_(y));
  }

  inline HOSTDEVICE T UseIntermediateOut(T x, T intermediate_out, T out,
                                         T dout) {
    return dout * d_binary_fun_.Dy(x, intermediate_out);
  }

214 215
  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return unary_fun_(y); }

C
chengduo 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
 private:
  DBinaryFun d_binary_fun_;
  UnaryFun unary_fun_;
};

// Z = UnaryFunctor(BinaryFunctor(X, Y))
template <typename T, typename DUnaryFun, typename BinaryFun, bool InPlace>
struct UnaryCompoundGradDIntermediateFunctor {
  UnaryCompoundGradDIntermediateFunctor(const DUnaryFun &d_unary_fun,
                                        const BinaryFun &binary_fun)
      : d_unary_fun_(d_unary_fun), binary_fun_(binary_fun) {}

  inline HOSTDEVICE T Recompute(T x, T y, T out, T dout) {
    if (InPlace) {
      return dout * d_unary_fun_.UseOut(out);
    } else {
      return dout * d_unary_fun_.UseXAndOut(binary_fun_(x, y), out);
    }
  }

  inline HOSTDEVICE T UseIntermediateOut(T x, T intermediate_out, T out,
                                         T dout) {
    if (InPlace) {
      return dout * d_unary_fun_.UseOut(out);
    } else {
      return dout * d_unary_fun_.UseXAndOut(intermediate_out, out);
    }
  }

245 246
  inline HOSTDEVICE T GetIntermediateOut(T x, T y) { return binary_fun_(x, y); }

C
chengduo 已提交
247 248 249 250 251
 private:
  DUnaryFun d_unary_fun_;
  BinaryFun binary_fun_;
};

252 253 254
}  // namespace math
}  // namespace operators
}  // namespace paddle