// 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. #pragma once #include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/platform/place.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template class AllcloseKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { // get attrs float rtol = ctx.Attr("rtol"); float atol = ctx.Attr("atol"); bool equal_nan = ctx.Attr("equal_nan"); // get input/output auto* input = ctx.Input("Input"); auto* other = ctx.Input("Other"); auto* out = ctx.Output("Out"); out->mutable_data(ctx.GetPlace()); // get place auto& place = *ctx.template device_context().eigen_device(); auto input_v = framework::EigenVector::Flatten(*input); auto other_v = framework::EigenVector::Flatten(*other); auto out_v = framework::EigenScalar::From(*out); auto left = (input_v - other_v).abs(); auto right = static_cast(atol) + static_cast(rtol) * other_v.abs(); auto compare_res = left <= right; if (equal_nan) { auto input_nan = input_v.isnan(); auto other_nan = other_v.isnan(); out_v.device(place) = (input_nan == other_nan).all() && (compare_res != input_nan).all(); } else { out_v.device(place) = compare_res.all(); } } }; } // namespace operators } // namespace paddle