allclose_op.cc 5.4 KB
Newer Older
Z
Zhen Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

H
huangxu96 已提交
15
#include <cmath>
16
#include <string>
17

C
Chen Weihang 已提交
18
#include "paddle/fluid/framework/infershape_utils.h"
Z
Zhen Wang 已提交
19
#include "paddle/fluid/framework/op_registry.h"
20
#include "paddle/fluid/framework/op_version_registry.h"
Z
Zhen Wang 已提交
21
#include "paddle/fluid/framework/operator.h"
H
huangxu96 已提交
22
#include "paddle/fluid/platform/enforce.h"
C
Chen Weihang 已提交
23 24
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/binary.h"
Z
Zhen Wang 已提交
25 26 27 28 29 30 31

namespace paddle {
namespace operators {

class AllcloseOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
32 33 34 35
    AddInput("Input",
             "The input tensor, it's data type should be float32, float64.");
    AddInput("Other",
             "The input tensor, it's data type should be float32, float64.");
36 37
    AddInput("Rtol", "The relative tolerance.").AsDispensable();
    AddInput("Atol", "The absolute tolerance.").AsDispensable();
38
    AddOutput("Out", "The output tensor, it's data type is bool.");
39 40 41 42 43 44
    AddAttr<std::string>("rtol",
                         "The relative tolerance. Default: :math:`1e-5` .")
        .SetDefault("1e-5");
    AddAttr<std::string>("atol",
                         "The absolute tolerance. Default: :math:`1e-8` .")
        .SetDefault("1e-8");
Z
Zhen Wang 已提交
45 46 47 48 49 50
    AddAttr<bool>("equal_nan",
                  "If :math:`True` , then two :math:`NaNs` will be "
                  "compared as equal. Default: :math:`False` .")
        .SetDefault(false);

    AddComment(R"DOC( 
51
This operator checks if all :math:`x` and :math:`y` satisfy the condition:
Z
Zhen Wang 已提交
52

53 54
.. math::
    \left| x - y \right| \leq atol + rtol \times \left| y \right|
Z
Zhen Wang 已提交
55

56
elementwise, for all elements of :math:`x` and :math:`y`. The behaviour of this
Z
Zhen Wang 已提交
57 58 59 60 61 62 63 64 65 66 67 68
operator is analogous to :math:`numpy.allclose`, namely that it returns :math:`True` if
two tensors are elementwise equal within a tolerance.
)DOC");
  }
};

class AllcloseOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  framework::OpKernelType GetExpectedKernelType(
H
huangxu96 已提交
69
      const framework::ExecutionContext& ctx) const override {
Z
Zhen Wang 已提交
70 71 72 73 74 75 76 77
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Input"),
        ctx.device_context());
  }
};

class AllcloseOpVarTypeInference : public framework::VarTypeInference {
 public:
H
huangxu96 已提交
78
  void operator()(framework::InferVarTypeContext* ctx) const override {
79
    ctx->SetOutputDataType("Out", framework::proto::VarType::BOOL);
Z
Zhen Wang 已提交
80 81 82 83 84 85 86
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
L
Leo Chen 已提交
87
using CPU = phi::CPUContext;
Z
Zhen Wang 已提交
88

89 90
DECLARE_INFER_SHAPE_FUNCTOR(allclose,
                            AllcloseInferShapeFunctor,
C
Chen Weihang 已提交
91
                            PD_INFER_META(phi::AllValueCompareInferMeta));
Z
Zhen Wang 已提交
92
REGISTER_OPERATOR(
93 94 95
    allclose,
    ops::AllcloseOp,
    ops::AllcloseOpMaker,
Z
Zhen Wang 已提交
96 97
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
98 99
    ops::AllcloseOpVarTypeInference,
    AllcloseInferShapeFunctor);
100 101 102 103 104 105 106 107 108 109 110 111 112

/* ==========================  register checkpoint ===========================*/
REGISTER_OP_VERSION(allclose)
    .AddCheckpoint(
        R"ROC(Upgrade allclose, add two new inputs [Rtol] and [Atol].)ROC",
        paddle::framework::compatible::OpVersionDesc()
            .NewInput("Rtol",
                      "The added input 'Rtol' is not"
                      "dispensable.")
            .NewInput("Atol",
                      "The added input 'Atol' is not"
                      "dispensable."))
    .AddCheckpoint(
113 114 115 116 117 118 119 120
        R"ROC(Delete two float attributes [rtol] and [atol], 
        then add 2 string attributes [atol, rtol]. Don't be surprised.
        This is because float cannot represent hight-precision
        floating-point values, and our framework doesn't support
        the use of double attributes. As a result, string instead
        of double is used here to represent high-precision
        floating-point values.
        )ROC",
121 122 123 124 125 126 127 128 129 130
        paddle::framework::compatible::OpVersionDesc()
            .DeleteAttr("rtol",
                        "The attribute 'rtol' is deleted."
                        "The reason why it is deleted is that"
                        "attributes do not support a float64 value"
                        "and it is changed to a tensor.")
            .DeleteAttr("atol",
                        "The attribute 'atol' is deleted."
                        "The reason why it is deleted is that"
                        "attributes do not support a float64 value"
131 132 133 134 135 136 137
                        "and it is changed to a tensor.")
            .NewAttr("rtol",
                     "(string) The relative tolerance. Default: :math:`1e-5` .",
                     std::string("1e-5"))
            .NewAttr("atol",
                     "(string) The absolute tolerance. Default: :math:`1e-8` .",
                     std::string("1e-8")));