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

namespace paddle {
namespace operators {

23
using framework::LoDTensor;
W
wanghaoshuang 已提交
24 25 26 27 28

template <typename T, size_t D, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;

29 30 31 32 33 34 35 36 37 38 39 40 41
template <typename Place, typename T, size_t D>
void ClipFunction(const framework::ExecutionContext& context) {
  auto max = context.op().Attr<float>("max");
  auto min = context.op().Attr<float>("min");
  auto* x = context.Input<LoDTensor>("X");
  auto* out = context.Output<LoDTensor>("Out");
  out->mutable_data<T>(context.GetPlace());
  auto x_tensor = EigenTensor<T, D>::From(*x);
  auto out_tensor = EigenTensor<T, D>::From(*out);
  auto place = context.GetEigenDevice<Place>();
  out_tensor.device(place) = x_tensor.cwiseMin(max).cwiseMax(min);
}

W
wanghaoshuang 已提交
42 43 44 45
template <typename Place, typename T>
class ClipKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    int rank = context.Input<LoDTensor>("X")->dims().size();
    switch (rank) {
      case 1:
        ClipFunction<Place, T, 1>(context);
        break;
      case 2:
        ClipFunction<Place, T, 2>(context);
        break;
      case 3:
        ClipFunction<Place, T, 3>(context);
        break;
      case 4:
        ClipFunction<Place, T, 4>(context);
        break;
      case 5:
        ClipFunction<Place, T, 5>(context);
        break;
      case 6:
        ClipFunction<Place, T, 6>(context);
        break;
      default:
        PADDLE_THROW(
            "PadOp only support tensors with no more than 6 dimensions.");
    }
W
wanghaoshuang 已提交
70 71 72 73 74 75 76
  }
};

template <typename T>
class ClipGradKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
W
wanghaoshuang 已提交
77 78
    auto max = context.op().Attr<float>("max");
    auto min = context.op().Attr<float>("min");
79 80 81
    auto* d_out = context.Input<LoDTensor>(framework::GradVarName("Out"));
    auto* d_x = context.Output<LoDTensor>(framework::GradVarName("X"));
    auto* x = context.Input<LoDTensor>("X");
W
wanghaoshuang 已提交
82
    auto dims = d_x->dims();
83
    int64_t count = d_out->numel();
W
wanghaoshuang 已提交
84 85 86 87
    auto d_x_data = d_x->mutable_data<T>(context.GetPlace());
    auto d_out_data = d_out->data<T>();
    auto x_data = x->data<T>();
    for (int i = 0; i < count; ++i) {
88 89 90 91 92
      if (x_data[i] > min && x_data[i] < max) {
        d_x_data[i] = d_out_data[i];
      } else {
        d_x_data[i] = 0;
      }
W
wanghaoshuang 已提交
93 94 95 96 97 98
    }
  }
};

}  // namespace operators
}  // namespace paddle