isfinite_v2_op.cc 5.1 KB
Newer Older
J
Jack Zhou 已提交
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 <string>
W
wanghuancoder 已提交
16

17 18
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
J
Jack Zhou 已提交
19
#include "paddle/fluid/operators/common_infer_shape_functions.h"
20 21
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"
W
wanghuancoder 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

namespace paddle {
namespace framework {
class InferShapeContext;
class OpDesc;
template <typename T>
class EmptyGradOpMaker;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
namespace operators {
template <typename DeviceContext, typename T, typename Functor>
class OverflowKernel;
}  // namespace operators
}  // namespace paddle
J
Jack Zhou 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

namespace plat = paddle::platform;

namespace paddle {
namespace operators {

class OverflowV2Op : public framework::OperatorWithKernel {
 public:
  OverflowV2Op(const std::string &type,
               const framework::VariableNameMap &inputs,
               const framework::VariableNameMap &outputs,
               const framework::AttributeMap &attrs)
      : OperatorWithKernel(type, inputs, outputs, attrs) {}

 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>()) {
58 59
      dtype = framework::TransToProtoVarType(
          x_var->Get<framework::LoDTensor>().dtype());
60
    } else if (x_var->IsType<phi::SelectedRows>()) {
61
      dtype = framework::TransToProtoVarType(
62
          x_var->Get<phi::SelectedRows>().value().dtype());
J
Jack Zhou 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    } else {
      PADDLE_THROW(plat::errors::InvalidArgument(
          "Cannot find the input data type by all input data"));
    }
    return framework::OpKernelType(framework::proto::VarType::Type(dtype),
                                   ctx.GetPlace());
  }
};

class OverflowV2OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor) The input tensors of overflowv2 operator.");
    AddOutput("Out",
              "(Tensor) The output tensor of overflowv2 operator. "
              "Same size compare to input tensor");
    AddComment(string::Sprintf(R"DOC(
Overflow %s operator.

82
$$Out = any(X)$$
J
Jack Zhou 已提交
83 84 85 86 87 88

Check whether each element of X is Inf or Nan, return the bool result of each
element of X as a tensor.

%s
)DOC",
89 90
                               GetName(),
                               GetComments()));
J
Jack Zhou 已提交
91 92 93 94 95 96 97 98 99 100 101
  }

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

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
102 103
DECLARE_INFER_SHAPE_FUNCTOR(isinf_v2,
                            IsinfInferShapeFunctor,
104 105
                            PD_INFER_META(phi::IsfiniteInferMeta));

106 107
DECLARE_INFER_SHAPE_FUNCTOR(isnan_v2,
                            IsnanInferShapeFunctor,
108 109
                            PD_INFER_META(phi::IsfiniteInferMeta));

110 111
DECLARE_INFER_SHAPE_FUNCTOR(isfinite_v2,
                            IsfiniteInferShapeFunctor,
112
                            PD_INFER_META(phi::IsfiniteInferMeta));
J
Jack Zhou 已提交
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127
#define REGISTER_V2OP_MAKER(op_type, comment)           \
  namespace paddle {                                    \
  namespace operators {                                 \
  class _##op_type##OverflowV2OpMaker                   \
      : public ::paddle::operators::OverflowV2OpMaker { \
   protected:                                           \
    std::string GetName() const { return #op_type; }    \
    std::string GetComments() const { return comment; } \
  };                                                    \
  }                                                     \
  }

REGISTER_V2OP_MAKER(isinf_v2, "isinfv2(X)")
REGISTER_V2OP_MAKER(isnan_v2, "isnanv2(X)")
J
Jack Zhou 已提交
128 129
REGISTER_V2OP_MAKER(isfinite_v2, "isfinitev2(X)");

130
REGISTER_OPERATOR(
131 132 133
    isinf_v2,
    ops::OverflowV2Op,
    ops::_isinf_v2OverflowV2OpMaker,
134
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
135 136
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    IsinfInferShapeFunctor);
137 138

REGISTER_OPERATOR(
139 140 141
    isnan_v2,
    ops::OverflowV2Op,
    ops::_isnan_v2OverflowV2OpMaker,
142
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
143 144
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    IsnanInferShapeFunctor);
145 146

REGISTER_OPERATOR(
147 148 149
    isfinite_v2,
    ops::OverflowV2Op,
    ops::_isfinite_v2OverflowV2OpMaker,
150
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
151 152
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    IsfiniteInferShapeFunctor);