isfinite_op.cc 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include "paddle/fluid/operators/isfinite_op.h"
W
wanghuancoder 已提交
16

17
#include <string>
W
wanghuancoder 已提交
18 19 20 21 22 23 24 25 26 27 28 29

namespace paddle {
namespace framework {
class InferShapeContext;
class OpDesc;
template <typename T>
class EmptyGradOpMaker;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
}  // namespace paddle
30 31 32 33 34 35

namespace paddle {
namespace operators {

class OverflowOp : public framework::OperatorWithKernel {
 public:
36 37
  OverflowOp(const std::string &type,
             const framework::VariableNameMap &inputs,
38 39 40 41 42
             const framework::VariableNameMap &outputs,
             const framework::AttributeMap &attrs)
      : OperatorWithKernel(type, inputs, outputs, attrs) {}

  void InferShape(framework::InferShapeContext *ctx) const override {
43 44
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "isfinite");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "isfinite");
45 46 47 48 49 50 51 52 53 54

    ctx->SetOutputDim("Out", {1});
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    int dtype = -1;
    auto *x_var = ctx.InputVar("X");
    if (x_var->IsType<framework::LoDTensor>()) {
55 56
      dtype = framework::TransToProtoVarType(
          x_var->Get<framework::LoDTensor>().type());
57
    } else if (x_var->IsType<phi::SelectedRows>()) {
58
      dtype = framework::TransToProtoVarType(
59
          x_var->Get<phi::SelectedRows>().value().type());
60
    } else {
61
      PADDLE_ENFORCE_EQ(
62 63
          true,
          false,
64 65 66
          platform::errors::InvalidArgument(
              "The input type mismatch, the type of Input(X) must be Tensor or "
              "SelectedRows, please check your input."));
67 68 69 70 71 72 73 74 75 76 77 78 79 80
    }
    return framework::OpKernelType(framework::proto::VarType::Type(dtype),
                                   ctx.GetPlace());
  }
};

class OverflowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor) The input tensors of overflow operator.");
    AddOutput("Out",
              "(Tensor) 1-dim tensor, contains a bool scalar. The output "
              "tensor of overflow operator.");
    AddComment(string::Sprintf(R"DOC(
Y
Yan Chunwei 已提交
81
Overflow %s operator.
82 83 84 85 86 87 88 89

$$Out = any(X)$$

If any X contains Inf or Nan, the Out will generate a indicator.
Out = Inf if any X contains Inf,
Out = Nan if any X contains Nan,
Out = 0 if no Inf/Nan detected.
If X contains both Inf/Nan, it will return the first indicator it meeted.
Y
Yan Chunwei 已提交
90 91

%s
92
)DOC",
93 94
                               GetName(),
                               GetComments()));
95 96 97 98 99 100 101 102 103 104 105 106
  }

 protected:
  virtual std::string GetName() const = 0;
  virtual std::string GetComments() const = 0;
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

H
hong 已提交
107 108 109 110 111 112 113 114 115 116 117 118
#define REGISTER_OP_MAKER(op_type, comment)                           \
  namespace paddle {                                                  \
  namespace operators {                                               \
  class _##op_type##OverflowOpMaker                                   \
      : public ::paddle::operators::OverflowOpMaker {                 \
   protected:                                                         \
    std::string GetName() const { return #op_type; }                  \
    std::string GetComments() const { return comment; }               \
  };                                                                  \
  }                                                                   \
  }                                                                   \
  REGISTER_OPERATOR(                                                  \
119 120 121
      op_type,                                                        \
      ops::OverflowOp,                                                \
      ops::_##op_type##OverflowOpMaker,                               \
H
hong 已提交
122 123
      paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>, \
      paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>)
124 125 126 127

REGISTER_OP_MAKER(isinf, "isinf(X)");
REGISTER_OP_MAKER(isnan, "isnan(X)");
REGISTER_OP_MAKER(isfinite, "isfinite(X)");
128

L
Leo Chen 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
REGISTER_OP_CPU_KERNEL(
    isinf,
    ops::OverflowKernel<phi::CPUContext, int, ops::InfinityFunctor>,
    ops::OverflowKernel<phi::CPUContext, int64_t, ops::InfinityFunctor>,
    ops::OverflowKernel<phi::CPUContext, float, ops::InfinityFunctor>,
    ops::OverflowKernel<phi::CPUContext, double, ops::InfinityFunctor>);

REGISTER_OP_CPU_KERNEL(
    isnan,
    ops::OverflowKernel<phi::CPUContext, int, ops::NANFunctor>,
    ops::OverflowKernel<phi::CPUContext, int64_t, ops::NANFunctor>,
    ops::OverflowKernel<phi::CPUContext, float, ops::NANFunctor>,
    ops::OverflowKernel<phi::CPUContext, double, ops::NANFunctor>);

REGISTER_OP_CPU_KERNEL(
    isfinite,
    ops::OverflowKernel<phi::CPUContext, int, ops::IsfiniteFunctor>,
    ops::OverflowKernel<phi::CPUContext, int64_t, ops::IsfiniteFunctor>,
    ops::OverflowKernel<phi::CPUContext, float, ops::IsfiniteFunctor>,
    ops::OverflowKernel<phi::CPUContext, double, ops::IsfiniteFunctor>);