clip_op.h 2.8 KB
Newer Older
W
wanghaoshuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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

#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
W
wanghaoshuang 已提交
19
#include "paddle/platform/transform.h"
W
wanghaoshuang 已提交
20 21 22 23

namespace paddle {
namespace operators {

W
wanghaoshuang 已提交
24 25
using framework::Tensor;
using platform::Transform;
W
wanghaoshuang 已提交
26

W
wanghaoshuang 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
template <typename T>
class ClipFunctor {
 public:
  explicit ClipFunctor(const T min, const T max) : min_(min), max_(max) {}
  HOSTDEVICE T operator()(const T& x) const {
    if (x < min_)
      return min_;
    else if (x > max_)
      return max_;
    else
      return x;
  }

 private:
  T min_;
  T max_;
};

template <typename T>
class ClipGradFunctor {
 public:
  explicit ClipGradFunctor(const T min, const T max) : min_(min), max_(max) {}
  HOSTDEVICE T operator()(const T& x, const T& y) const {
W
wanghaoshuang 已提交
50
    return (y > min_ && y < max_) ? x : 0;
W
wanghaoshuang 已提交
51
  }
W
wanghaoshuang 已提交
52

W
wanghaoshuang 已提交
53 54 55 56
 private:
  T min_;
  T max_;
};
57

W
wanghaoshuang 已提交
58
template <typename Place, typename T>
W
wanghaoshuang 已提交
59 60 61
class ClipKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
W
wanghaoshuang 已提交
62 63 64 65 66 67
    auto max = context.Attr<T>("max");
    auto min = context.Attr<T>("min");
    auto* x = context.Input<Tensor>("X");
    auto* out = context.Output<Tensor>("Out");
    T* out_data = out->mutable_data<T>(context.GetPlace());
    const T* x_data = x->data<T>();
W
wanghaoshuang 已提交
68
    int64_t numel = x->numel();
W
wanghaoshuang 已提交
69 70 71
    Transform<Place> trans;
    trans(context.device_context(), x_data, x_data + numel, out_data,
          ClipFunctor<T>(min, max));
W
wanghaoshuang 已提交
72 73 74
  }
};

W
wanghaoshuang 已提交
75
template <typename Place, typename T>
W
wanghaoshuang 已提交
76 77 78
class ClipGradKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
W
wanghaoshuang 已提交
79 80 81 82
    auto max = context.Attr<T>("max");
    auto min = context.Attr<T>("min");
    auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
    auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
W
wanghaoshuang 已提交
83
    if (d_x != nullptr) {
W
wanghaoshuang 已提交
84 85
      auto* x = context.Input<Tensor>("X");
      int64_t numel = d_out->numel();
W
wanghaoshuang 已提交
86
      auto* d_x_data = d_x->mutable_data<T>(context.GetPlace());
W
wanghaoshuang 已提交
87 88
      const T* d_out_data = d_out->data<T>();
      const T* x_data = x->data<T>();
W
wanghaoshuang 已提交
89 90 91
      Transform<Place> trans;
      trans(context.device_context(), d_out_data, d_out_data + numel, x_data,
            d_x_data, ClipGradFunctor<T>(min, max));
W
wanghaoshuang 已提交
92 93 94 95 96 97
    }
  }
};

}  // namespace operators
}  // namespace paddle