Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into develop
commit
98ffde410d
@ -0,0 +1,113 @@
|
||||
// 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"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class OverflowOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
OverflowOp(const std::string &type, const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: OperatorWithKernel(type, inputs, outputs, attrs) {}
|
||||
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInputs("X"), "Inputs(X) should not be null");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||
"Output(Out) of OverflowOp should not be null.");
|
||||
|
||||
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>()) {
|
||||
dtype = framework::ToDataType(x_var->Get<framework::LoDTensor>().type());
|
||||
} else if (x_var->IsType<framework::SelectedRows>()) {
|
||||
dtype = framework::ToDataType(
|
||||
x_var->Get<framework::SelectedRows>().value().type());
|
||||
} else {
|
||||
PADDLE_THROW("Cannot find the input data type by all input data");
|
||||
}
|
||||
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(
|
||||
Overflow operator.
|
||||
|
||||
$$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.
|
||||
)DOC",
|
||||
GetName(), GetComments()));
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual std::string GetName() const = 0;
|
||||
virtual std::string GetComments() const = 0;
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
#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(op_type, ops::OverflowOp, \
|
||||
ops::_##op_type##OverflowOpMaker, \
|
||||
paddle::framework::EmptyGradOpMaker)
|
||||
|
||||
#define REGISTER_OVERFLOW_CPU_KERNEL(op_type, functor) \
|
||||
REGISTER_OP_CPU_KERNEL( \
|
||||
op_type, ops::OverflowKernel<paddle::platform::CPUDeviceContext, int, \
|
||||
ops::functor>, \
|
||||
ops::OverflowKernel<paddle::platform::CPUDeviceContext, float, \
|
||||
ops::functor>, \
|
||||
ops::OverflowKernel<paddle::platform::CPUDeviceContext, double, \
|
||||
ops::functor>);
|
||||
|
||||
REGISTER_OP_MAKER(isinf, "isinf(X)");
|
||||
REGISTER_OP_MAKER(isnan, "isnan(X)");
|
||||
REGISTER_OP_MAKER(isfinite, "isfinite(X)");
|
||||
FOR_EACH_KERNEL_FUNCTOR(REGISTER_OVERFLOW_CPU_KERNEL);
|
@ -0,0 +1,33 @@
|
||||
// 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.
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/fluid/operators/isfinite_op.h"
|
||||
#include "paddle/fluid/platform/float16.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
namespace plat = paddle::platform;
|
||||
|
||||
#define REGISTER_OVERFLOW_CUDA_KERNEL(op_type, functor) \
|
||||
REGISTER_OP_CUDA_KERNEL( \
|
||||
op_type, ops::OverflowKernel<paddle::platform::CUDADeviceContext, int, \
|
||||
ops::functor>, \
|
||||
ops::OverflowKernel<paddle::platform::CUDADeviceContext, float, \
|
||||
ops::functor>, \
|
||||
ops::OverflowKernel<paddle::platform::CUDADeviceContext, double, \
|
||||
ops::functor>, \
|
||||
ops::OverflowKernel<paddle::platform::CUDADeviceContext, plat::float16, \
|
||||
ops::functor>);
|
||||
|
||||
FOR_EACH_KERNEL_FUNCTOR(REGISTER_OVERFLOW_CUDA_KERNEL);
|
@ -0,0 +1,71 @@
|
||||
// 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 <vector>
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
#include "paddle/fluid/framework/tensor_util.h"
|
||||
#include "paddle/fluid/platform/float16.h"
|
||||
#include "paddle/fluid/platform/transform.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
struct InfinityFunctor {
|
||||
void operator()(const framework::Tensor& tensor, framework::Tensor* out) {
|
||||
framework::TensorContainsInf(tensor, out);
|
||||
}
|
||||
};
|
||||
|
||||
struct NANFunctor {
|
||||
void operator()(const framework::Tensor& tensor, framework::Tensor* out) {
|
||||
framework::TensorContainsNAN(tensor, out);
|
||||
}
|
||||
};
|
||||
|
||||
struct IsfiniteFunctor {
|
||||
void operator()(const framework::Tensor& tensor, framework::Tensor* out) {
|
||||
framework::TensorIsfinite(tensor, out);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename DeviceContext, typename T, typename Functor>
|
||||
class OverflowKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
virtual void Compute(const framework::ExecutionContext& ctx) const {
|
||||
auto* x = ctx.InputVar("X");
|
||||
auto* out = ctx.Output<framework::Tensor>("Out");
|
||||
out->mutable_data<T>(ctx.GetPlace());
|
||||
Functor functor;
|
||||
if (x->IsType<framework::LoDTensor>()) {
|
||||
auto* in = ctx.Input<framework::Tensor>("X");
|
||||
functor(*in, out);
|
||||
} else if (x->IsType<framework::SelectedRows>()) {
|
||||
auto& in = ctx.Input<framework::SelectedRows>("X")->value();
|
||||
functor(in, out);
|
||||
} else {
|
||||
PADDLE_THROW("Unsupported input type.");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
#define FOR_EACH_KERNEL_FUNCTOR(__macro) \
|
||||
__macro(isinf, InfinityFunctor); \
|
||||
__macro(isnan, NANFunctor); \
|
||||
__macro(isfinite, IsfiniteFunctor);
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue