Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Phi]move reduce_min/any/all kernel #40374

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions paddle/fluid/operators/reduce_ops/reduce_all_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

#include "paddle/fluid/operators/reduce_ops/reduce_all_op.h"

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace framework {
class OpDesc;
Expand All @@ -28,9 +32,17 @@ class CPUDeviceContext;
} // namespace platform
} // namespace paddle

DECLARE_INFER_SHAPE_FUNCTOR(reduce_all, ReduceAllInferShapeFunctor,
PD_INFER_META(phi::ReduceInferMetaBase));
class ReduceAllOpMaker : public ops::ReduceOpMaker {
protected:
virtual std::string GetName() const { return "reduce_all"; }
virtual std::string GetOpType() const { return "Reduce reduce_all"; }
};
// kernel's device type is decided by input tensor place, to be consistent with
// compare and logical ops
REGISTER_REDUCE_OP_WITHOUT_GRAD(reduce_all, UseInputPlace);
REGISTER_OP_CPU_KERNEL(reduce_all,
ops::BoolReduceKernel<paddle::platform::CPUDeviceContext,
bool, ops::AllFunctor>);
REGISTER_OPERATOR(
reduce_all, ops::ReduceOpUseInputPlace, ReduceAllOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
ReduceAllInferShapeFunctor);
19 changes: 0 additions & 19 deletions paddle/fluid/operators/reduce_ops/reduce_all_op.cu

This file was deleted.

20 changes: 16 additions & 4 deletions paddle/fluid/operators/reduce_ops/reduce_any_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include "paddle/fluid/operators/reduce_ops/reduce_any_op.h"

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"
namespace paddle {
namespace framework {
class OpDesc;
Expand All @@ -28,9 +31,18 @@ class CPUDeviceContext;
} // namespace platform
} // namespace paddle

DECLARE_INFER_SHAPE_FUNCTOR(reduce_any, ReduceAnyInferShapeFunctor,
PD_INFER_META(phi::ReduceInferMetaBase));

class ReduceAnyOpMaker : public ops::ReduceOpMaker {
protected:
virtual std::string GetName() const { return "reduce_any"; }
virtual std::string GetOpType() const { return "Reduce reduce_any"; }
};
// kernel's device type is decided by input tensor place, to be consistent with
// compare and logical ops
REGISTER_REDUCE_OP_WITHOUT_GRAD(reduce_any, UseInputPlace);
REGISTER_OP_CPU_KERNEL(reduce_any,
ops::BoolReduceKernel<paddle::platform::CPUDeviceContext,
bool, ops::AnyFunctor>);
REGISTER_OPERATOR(
reduce_any, ops::ReduceOpUseInputPlace, ReduceAnyOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
ReduceAnyInferShapeFunctor);
20 changes: 0 additions & 20 deletions paddle/fluid/operators/reduce_ops/reduce_any_op.cu

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace p = paddle::platform;

using Tensor = paddle::framework::Tensor;

USE_OP(reduce_any);
USE_OP_ITSELF(reduce_any);
USE_OP_DEVICE_KERNEL(reduce_any, NPU);

template <typename T>
Expand Down
31 changes: 22 additions & 9 deletions paddle/fluid/operators/reduce_ops/reduce_min_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,28 @@

#include "paddle/fluid/operators/reduce_ops/reduce_min_max_op.h"

REGISTER_REDUCE_OP(reduce_min);
REGISTER_OP_CPU_KERNEL(
reduce_min, ops::ReduceKernel<paddle::platform::CPUDeviceContext, float,
ops::MinFunctor>,
ops::ReduceKernel<paddle::platform::CPUDeviceContext, double,
ops::MinFunctor>,
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int, ops::MinFunctor>,
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int64_t,
ops::MinFunctor>);
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"

namespace ops = paddle::operators;

class ReduceMinOpMaker : public ops::ReduceOpMaker {
protected:
virtual std::string GetName() const { return "reduce_min"; }
virtual std::string GetOpType() const { return "Reduce reduce_min"; }
};

DECLARE_INFER_SHAPE_FUNCTOR(reduce_min, ReduceMinInferShapeFunctor,
PD_INFER_META(phi::ReduceInferMetaBase));

REGISTER_OPERATOR(
reduce_min, ops::ReduceOp, ReduceMinOpMaker,
paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>,
ReduceMinInferShapeFunctor);
REGISTER_OPERATOR(reduce_min_grad, ops::ReduceGradOp)

REGISTER_OP_CPU_KERNEL(
reduce_min_grad, ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
float, ops::MaxOrMinGradFunctor>,
Expand Down
23 changes: 0 additions & 23 deletions paddle/fluid/operators/reduce_ops/reduce_min_op.cu

This file was deleted.

3 changes: 3 additions & 0 deletions paddle/phi/core/compat/op_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const std::unordered_set<std::string> deprecated_op_names({"diag",
"matmul_grad_grad",
"mean",
"max",
"min",
"any",
"all",
"reshape",
"reshape_grad",
"expand",
Expand Down
25 changes: 25 additions & 0 deletions paddle/phi/kernels/cpu/reduce.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,29 @@ void Reduce(const DeviceContext& dev_ctx,
}
}

template <typename DeviceContext, typename OutT, typename Functor>
void BoolReduceKernel(const DeviceContext& dev_ctx,
const phi::DenseTensor& input,
const std::vector<int64_t>& dims,
bool keep_dim,
bool reduce_all,
phi::DenseTensor* output) {
dev_ctx.template Alloc<OutT>(output);

// The dims has full dim, set the reduce_all is True
const auto& input_dim_size = input.dims().size();
std::set<int> dims_set(dims.begin(), dims.end());
bool full_dim = true;
for (auto i = 0; i < input_dim_size; i++) {
if (dims_set.find(i) == dims_set.end()) {
full_dim = false;
break;
}
}
reduce_all = (reduce_all || full_dim);

ReduceKernelImpl<DeviceContext, bool, OutT, Functor>(
dev_ctx, input, output, dims, keep_dim, reduce_all);
}

} // namespace phi
37 changes: 37 additions & 0 deletions paddle/phi/kernels/cpu/reduce_all_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022 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/phi/kernels/reduce_all_kernel.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/cpu/reduce.h"
#include "paddle/phi/kernels/funcs/reduce_functor.h"

namespace phi {

template <typename T, typename Context>
void AllRawKernel(const Context& dev_ctx,
const DenseTensor& x,
const std::vector<int64_t>& dims,
bool keep_dim,
bool reduce_all,
DenseTensor* out) {
phi::BoolReduceKernel<CPUContext, T, phi::funcs::AllFunctor>(
dev_ctx, x, dims, keep_dim, reduce_all, out);
}

} // namespace phi

PD_REGISTER_KERNEL(all_raw, CPU, ALL_LAYOUT, phi::AllRawKernel, bool) {}
37 changes: 37 additions & 0 deletions paddle/phi/kernels/cpu/reduce_any_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022 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/phi/kernels/reduce_any_kernel.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/cpu/reduce.h"
#include "paddle/phi/kernels/funcs/reduce_functor.h"

namespace phi {

template <typename T, typename Context>
void AnyRawKernel(const Context& dev_ctx,
const DenseTensor& x,
const std::vector<int64_t>& dims,
bool keep_dim,
bool reduce_all,
DenseTensor* out) {
phi::BoolReduceKernel<CPUContext, T, phi::funcs::AnyFunctor>(
dev_ctx, x, dims, keep_dim, reduce_all, out);
}

} // namespace phi

PD_REGISTER_KERNEL(any_raw, CPU, ALL_LAYOUT, phi::AnyRawKernel, bool) {}
39 changes: 39 additions & 0 deletions paddle/phi/kernels/cpu/reduce_min_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2022 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/phi/kernels/reduce_min_kernel.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/cpu/reduce.h"
#include "paddle/phi/kernels/funcs/reduce_functor.h"

namespace phi {

template <typename T, typename Context>
void MinRawKernel(const Context& dev_ctx,
const DenseTensor& x,
const std::vector<int64_t>& dims,
bool keep_dim,
bool reduce_all,
DenseTensor* out) {
auto out_dtype = x.dtype();
phi::Reduce<CPUContext, T, phi::funcs::MinFunctor>(
dev_ctx, x, reduce_all, dims, keep_dim, out_dtype, out);
}

} // namespace phi

PD_REGISTER_KERNEL(
min_raw, CPU, ALL_LAYOUT, phi::MinRawKernel, float, double, int, int64_t) {}
24 changes: 24 additions & 0 deletions paddle/phi/kernels/funcs/reduce_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,29 @@ struct MaxFunctor {
}
};

//////// Min Functor ///////
struct MinFunctor {
template <typename DeviceContext, typename X, typename Y, typename Dim>
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
y->device(place) = x->minimum(dim);
}
};

//////// All Functor ///////
struct AllFunctor {
template <typename DeviceContext, typename X, typename Y, typename Dim>
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
y->device(place) = x->all(dim);
}
};

//////// Any Functor ///////
struct AnyFunctor {
template <typename DeviceContext, typename X, typename Y, typename Dim>
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
y->device(place) = x->any(dim);
}
};

} // namespace funcs
} // namespace phi
Loading