Skip to content

Commit

Permalink
[phi]migrate fmax,fmin kernel to phi
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxly committed Mar 10, 2022
1 parent 857069f commit 7fcfb98
Show file tree
Hide file tree
Showing 17 changed files with 536 additions and 352 deletions.
83 changes: 0 additions & 83 deletions paddle/fluid/operators/elementwise/elementwise_functor.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* Copyright (c) 2021 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.
Expand Down Expand Up @@ -90,86 +87,6 @@ struct MinFunctor {
template <typename T>
using Complex = paddle::platform::complex<T>;

// Fmax
template <typename T>
struct FMaxFunctor {
inline HOSTDEVICE T operator()(const T a, const T b) const {
return std::fmax(a, b);
}
};

template <>
struct FMaxFunctor<paddle::platform::float16> {
inline HOSTDEVICE paddle::platform::float16 operator()(
const paddle::platform::float16 a,
const paddle::platform::float16 b) const {
float float_a = static_cast<float>(a);
float float_b = static_cast<float>(b);
auto result = std::fmax(float_a, float_b);
return static_cast<paddle::platform::float16>(result);
}
};

template <>
struct FMaxFunctor<int> {
inline HOSTDEVICE int operator()(const int a, const int b) const {
float float_a = static_cast<float>(a);
float float_b = static_cast<float>(b);
auto result = std::fmax(float_a, float_b);
return std::lrint(result);
}
};

template <>
struct FMaxFunctor<int64_t> {
inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const {
double double_a = static_cast<double>(a);
double double_b = static_cast<double>(b);
auto result = std::fmax(double_a, double_b);
return std::llrint(result);
}
};

// Fmin
template <typename T>
struct FMinFunctor {
inline HOSTDEVICE T operator()(const T a, const T b) const {
return std::fmin(a, b);
}
};

template <>
struct FMinFunctor<paddle::platform::float16> {
inline HOSTDEVICE paddle::platform::float16 operator()(
const paddle::platform::float16 a,
const paddle::platform::float16 b) const {
float float_a = static_cast<float>(a);
float float_b = static_cast<float>(b);
auto result = std::fmin(float_a, float_b);
return static_cast<paddle::platform::float16>(result);
}
};

template <>
struct FMinFunctor<int> {
inline HOSTDEVICE int operator()(const int a, const int b) const {
float float_a = static_cast<float>(a);
float float_b = static_cast<float>(b);
auto result = std::fmin(float_a, float_b);
return std::lrint(result);
}
};

template <>
struct FMinFunctor<int64_t> {
inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const {
double double_a = static_cast<double>(a);
double double_b = static_cast<double>(b);
auto result = std::fmin(double_a, double_b);
return std::llrint(result);
}
};

template <typename T>
struct MinGradXFunctor {
inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const {
Expand Down
18 changes: 0 additions & 18 deletions paddle/fluid/operators/elementwise/elementwise_max_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,3 @@ REGISTER_OPERATOR(elementwise_fmax, ops::ElementwiseOp,
ops::ElementwiseFMaxGradOpMaker<paddle::imperative::OpBase>);

REGISTER_OPERATOR(elementwise_fmax_grad, ops::ElementwiseOpGrad);

REGISTER_OP_CPU_KERNEL(
elementwise_fmax,
ops::ElementwiseFMaxKernel<paddle::platform::CPUDeviceContext, float>,
ops::ElementwiseFMaxKernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMaxKernel<paddle::platform::CPUDeviceContext, double>,
ops::ElementwiseFMaxKernel<paddle::platform::CPUDeviceContext, int>,
ops::ElementwiseFMaxKernel<paddle::platform::CPUDeviceContext, int64_t>);
REGISTER_OP_CPU_KERNEL(
elementwise_fmax_grad,
ops::ElementwiseFMaxGradKernel<paddle::platform::CPUDeviceContext, float>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CPUDeviceContext, double>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CPUDeviceContext, int>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CPUDeviceContext,
int64_t>);
18 changes: 0 additions & 18 deletions paddle/fluid/operators/elementwise/elementwise_max_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,3 @@ REGISTER_OP_CUDA_KERNEL(
ops::ElementwiseMaxGradKernel<paddle::platform::CUDADeviceContext, int>,
ops::ElementwiseMaxGradKernel<paddle::platform::CUDADeviceContext,
int64_t>);

REGISTER_OP_CUDA_KERNEL(
elementwise_fmax,
ops::ElementwiseFMaxKernel<paddle::platform::CUDADeviceContext, float>,
ops::ElementwiseFMaxKernel<paddle::platform::CUDADeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMaxKernel<paddle::platform::CUDADeviceContext, double>,
ops::ElementwiseFMaxKernel<paddle::platform::CUDADeviceContext, int>,
ops::ElementwiseFMaxKernel<paddle::platform::CUDADeviceContext, int64_t>);
REGISTER_OP_CUDA_KERNEL(
elementwise_fmax_grad,
ops::ElementwiseFMaxGradKernel<paddle::platform::CUDADeviceContext, float>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CUDADeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CUDADeviceContext, double>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CUDADeviceContext, int>,
ops::ElementwiseFMaxGradKernel<paddle::platform::CUDADeviceContext,
int64_t>);
98 changes: 0 additions & 98 deletions paddle/fluid/operators/elementwise/elementwise_max_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,6 @@ class ElementwiseMaxKernel : public framework::OpKernel<T> {
}
};

template <typename DeviceContext, typename T>
class ElementwiseFMaxKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto* x = ctx.Input<framework::LoDTensor>("X");
auto* y = ctx.Input<framework::LoDTensor>("Y");
auto* z = ctx.Output<framework::LoDTensor>("Out");

z->mutable_data<T>(ctx.GetPlace());
int axis = ctx.Attr<int>("axis");
ElementwiseComputeEx<FMaxFunctor<T>, DeviceContext, T>(ctx, x, y, axis,
FMaxFunctor<T>(), z);
}
};

template <typename T>
struct MaxGradDx {
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
Expand Down Expand Up @@ -104,88 +89,5 @@ class ElementwiseMaxGradKernel : public ElemwiseGradKernel<T> {
}
};

template <typename T>
struct FMaxGradDx {
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
return dout * static_cast<T>((x >= y) || isnan(y));
}
};

template <>
struct FMaxGradDx<paddle::platform::float16> {
HOSTDEVICE paddle::platform::float16 operator()(
paddle::platform::float16 x, paddle::platform::float16 y,
paddle::platform::float16 out, paddle::platform::float16 dout) const {
return dout * static_cast<paddle::platform::float16>(
(x >= y) || paddle::platform::isnan(y));
}
};

template <>
struct FMaxGradDx<int> {
HOSTDEVICE int operator()(int x, int y, int out, int dout) const {
return dout * static_cast<int>((x >= y));
}
};

template <>
struct FMaxGradDx<int64_t> {
HOSTDEVICE int64_t operator()(int64_t x, int64_t y, int64_t out,
int64_t dout) const {
return dout * static_cast<int64_t>((x >= y));
}
};

template <typename T>
struct FMaxGradDy {
HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
return dout * static_cast<T>(!((x >= y) || isnan(y)));
}
};

template <>
struct FMaxGradDy<paddle::platform::float16> {
HOSTDEVICE paddle::platform::float16 operator()(
paddle::platform::float16 x, paddle::platform::float16 y,
paddle::platform::float16 out, paddle::platform::float16 dout) const {
return dout * static_cast<paddle::platform::float16>(
!((x >= y) || paddle::platform::isnan(y)));
}
};

template <>
struct FMaxGradDy<int64_t> {
HOSTDEVICE int64_t operator()(int64_t x, int64_t y, int64_t out,
int64_t dout) const {
return dout * static_cast<int64_t>(!((x >= y)));
}
};

template <>
struct FMaxGradDy<int> {
HOSTDEVICE int operator()(int x, int y, int out, int dout) const {
return dout * static_cast<int>(!((x >= y)));
}
};

template <typename DeviceContext, typename T>
class ElementwiseFMaxGradKernel : public ElemwiseGradKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
ElemwiseGradKernel<T>::Compute(ctx);
using Tensor = framework::Tensor;

auto* x = ctx.Input<Tensor>("X");
auto* y = ctx.Input<Tensor>("Y");
auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
auto* out = dout; // Fake out, not used
int axis = ctx.Attr<int>("axis");
ElemwiseGradCompute<DeviceContext, T, FMaxGradDx<T>, FMaxGradDy<T>>(
ctx, *x, *y, *out, *dout, axis, dx, dy, FMaxGradDx<T>(),
FMaxGradDy<T>());
}
};
} // namespace operators
} // namespace paddle
18 changes: 0 additions & 18 deletions paddle/fluid/operators/elementwise/elementwise_min_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,3 @@ REGISTER_OPERATOR(elementwise_fmin, ops::ElementwiseOp,
ops::ElementwiseFMinGradOpMaker<paddle::imperative::OpBase>);

REGISTER_OPERATOR(elementwise_fmin_grad, ops::ElementwiseOpGrad);

REGISTER_OP_CPU_KERNEL(
elementwise_fmin,
ops::ElementwiseFMinKernel<paddle::platform::CPUDeviceContext, float>,
ops::ElementwiseFMinKernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMinKernel<paddle::platform::CPUDeviceContext, double>,
ops::ElementwiseFMinKernel<paddle::platform::CPUDeviceContext, int>,
ops::ElementwiseFMinKernel<paddle::platform::CPUDeviceContext, int64_t>);
REGISTER_OP_CPU_KERNEL(
elementwise_fmin_grad,
ops::ElementwiseFMinGradKernel<paddle::platform::CPUDeviceContext, float>,
ops::ElementwiseFMinGradKernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMinGradKernel<paddle::platform::CPUDeviceContext, double>,
ops::ElementwiseFMinGradKernel<paddle::platform::CPUDeviceContext, int>,
ops::ElementwiseFMinGradKernel<paddle::platform::CPUDeviceContext,
int64_t>);
18 changes: 0 additions & 18 deletions paddle/fluid/operators/elementwise/elementwise_min_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,3 @@ REGISTER_OP_CUDA_KERNEL(
ops::ElementwiseMinGradKernel<paddle::platform::CUDADeviceContext, int>,
ops::ElementwiseMinGradKernel<paddle::platform::CUDADeviceContext,
int64_t>);

REGISTER_OP_CUDA_KERNEL(
elementwise_fmin,
ops::ElementwiseFMinKernel<paddle::platform::CUDADeviceContext, float>,
ops::ElementwiseFMinKernel<paddle::platform::CUDADeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMinKernel<paddle::platform::CUDADeviceContext, double>,
ops::ElementwiseFMinKernel<paddle::platform::CUDADeviceContext, int>,
ops::ElementwiseFMinKernel<paddle::platform::CUDADeviceContext, int64_t>);
REGISTER_OP_CUDA_KERNEL(
elementwise_fmin_grad,
ops::ElementwiseFMinGradKernel<paddle::platform::CUDADeviceContext, float>,
ops::ElementwiseFMinGradKernel<paddle::platform::CUDADeviceContext,
paddle::platform::float16>,
ops::ElementwiseFMinGradKernel<paddle::platform::CUDADeviceContext, double>,
ops::ElementwiseFMinGradKernel<paddle::platform::CUDADeviceContext, int>,
ops::ElementwiseFMinGradKernel<paddle::platform::CUDADeviceContext,
int64_t>);
Loading

1 comment on commit 7fcfb98

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on 7fcfb98 Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕵️ CI failures summary

🔍 PR: #40140 Commit ID: 7fcfb98 contains failed CI.

🔹 Failed: PR-CI-Coverage

Unknown Failed
2022-03-11 11:32:32 + export CACHE_DIR=/home/data/cfs/.cache/coverage
2022-03-11 11:32:32 + CACHE_DIR=/home/data/cfs/.cache/coverage
2022-03-11 11:32:32 + export CCACHE_DIR=/home/data/cfs/.ccache/coverage
2022-03-11 11:32:32 + CCACHE_DIR=/home/data/cfs/.ccache/coverage
2022-03-11 11:32:32 + cd /mnt
2022-03-11 11:32:32 + rm -rf Paddle.tar.gz
2022-03-11 11:32:32 + rm -rf paddle
2022-03-11 11:32:32 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
2022-03-11 11:32:32 SLF4J: Defaulting to no-operation (NOP) logger implementation
2022-03-11 11:32:32 SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2022-03-11 11:32:36 + cp /home/data/cfs/coverage/40140/7fcfb9808c7bdbbf301aa4d4fe2d4a1a21c7fc23/Paddle.tar.gz .
2022-03-11 11:33:00 + tar '--use-compress-program=pigz -1' -xpf Paddle.tar.gz
2022-03-11 11:33:00 tar: Removing leading `/' from member names
2022-03-11 11:35:20 pigz: abort: corrupted input -- invalid deflate data:
2022-03-11 11:35:20 tar: Unexpected EOF in archive
2022-03-11 11:35:20 tar: Unexpected EOF in archive
2022-03-11 11:35:20 tar: Error is not recoverable: exiting now
2022-03-11 11:35:20 {build code state=2}
2022-03-11 11:35:30 kill agent BUILD_CODE_FAIL

🔹 Failed: PR-CI-OP-benchmark

Unknown Failed
2022-03-11 12:57:59 [check_op_benchmark_result.py:153] [ERROR] Check speed result with case "floor_divide_0 (forward)" failed.
2022-03-11 12:57:59 [check_op_benchmark_result.py:153] [ERROR] Check speed result with case "log_softmax_1 (backward)" failed.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:255] [INFO] run function check_CHANGE_OP_MAP
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "lu_unpack"(paddle/fluid/operators/lu_unpack_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "elementwise"(paddle/phi/kernels/gpu/elementwise_kernel.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "svd"(paddle/fluid/operators/svd_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "qr"(paddle/fluid/operators/qr_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "complex"(paddle/fluid/operators/complex_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "math"(paddle/phi/kernels/gpu/math_kernel.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "matrix_rank"(paddle/fluid/operators/matrix_rank_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "lstsq"(paddle/fluid/operators/lstsq_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "lu"(paddle/fluid/operators/lu_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "eigvalsh"(paddle/fluid/operators/eigvalsh_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "where"(paddle/phi/kernels/gpu/where_kernel.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:262] [WARNING] Missing test script of "dirichlet"(paddle/fluid/operators/dirichlet_op.cu) in benchmark.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:266] [INFO] See https://github.com/PaddlePaddle/Paddle/wiki/PR-CI-OP-benchmark-Manual for details.
2022-03-11 12:57:59 [tools/ci_op_benchmark.sh:267] [INFO] Or you can apply for one RD (Avin0323(Recommend), Xreki, luotao1) approval to pass this PR.
2022-03-11 12:57:59 {build code state=8}
2022-03-11 12:58:09 kill agent BUILD_CODE_FAIL

Please sign in to comment.