elementwise_min_op.h 2.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
F
fengjiayi 已提交
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

17
#include "paddle/fluid/operators/elementwise/elementwise_functor.h"
W
Wu Yi 已提交
18 19
#include "paddle/fluid/operators/elementwise/elementwise_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_function.h"
W
wanghuancoder 已提交
20

F
fengjiayi 已提交
21 22 23 24 25 26 27
namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class ElementwiseMinKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
C
chengduo 已提交
28 29 30
    auto* x = ctx.Input<framework::LoDTensor>("X");
    auto* y = ctx.Input<framework::LoDTensor>("Y");
    auto* z = ctx.Output<framework::LoDTensor>("Out");
C
chengduoZH 已提交
31 32 33

    z->mutable_data<T>(ctx.GetPlace());
    int axis = ctx.Attr<int>("axis");
C
chengduoZH 已提交
34 35
    ElementwiseComputeEx<MinFunctor<T>, DeviceContext, T>(ctx, x, y, axis,
                                                          MinFunctor<T>(), z);
F
fengjiayi 已提交
36 37 38 39
  }
};

template <typename T>
C
chengduoZH 已提交
40 41 42
struct MinGradDx {
  HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
    return dout * (x < y);
F
fengjiayi 已提交
43 44 45 46
  }
};

template <typename T>
C
chengduoZH 已提交
47 48 49
struct MinGradDy {
  HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
    return dout * (x >= y);
F
fengjiayi 已提交
50 51 52 53
  }
};

template <typename DeviceContext, typename T>
54
class ElementwiseMinGradKernel : public ElemwiseGradKernel<T> {
F
fengjiayi 已提交
55 56
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
57
    ElemwiseGradKernel<T>::Compute(ctx);
C
chengduoZH 已提交
58 59 60 61 62 63 64
    using Tensor = framework::Tensor;

    auto* x = ctx.Input<Tensor>("X");
    auto* y = ctx.Input<Tensor>("Y");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
65
    auto* out = dout;  // Fake out, not used
C
chengduoZH 已提交
66
    int axis = ctx.Attr<int>("axis");
C
chengduoZH 已提交
67 68
    ElemwiseGradCompute<DeviceContext, T, MinGradDx<T>, MinGradDy<T>>(
        ctx, *x, *y, *out, *dout, axis, dx, dy, MinGradDx<T>(), MinGradDy<T>());
F
fengjiayi 已提交
69 70 71 72
  }
};
}  // namespace operators
}  // namespace paddle