Skip to content

Commit d48172f

Browse files
authored
split reduce op into multiple libraries, accelerate the compiling (#11029)
* "split into multiple .ccl" * "refine file structure" * "refine files" * "remove the cmakelist" * "fix typo" * "fix typo" * fix ci
1 parent 5803115 commit d48172f

20 files changed

+789
-422
lines changed

paddle/fluid/framework/op_registry.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ class OpKernelRegistrar : public Registrar {
156156
/**
157157
* Macro to register OperatorKernel.
158158
*/
159-
#define REGISTER_OP_KERNEL(op_type, LIBRARY_TYPE, place_class, ...) \
159+
#define REGISTER_OP_KERNEL(op_type, library_type, place_class, ...) \
160160
STATIC_ASSERT_GLOBAL_NAMESPACE( \
161-
__reg_op_kernel_##op_type##_##LIBRARY_TYPE##__, \
161+
__reg_op_kernel_##op_type##_##library_type##__, \
162162
"REGISTER_OP_KERNEL must be called in global namespace"); \
163163
static ::paddle::framework::OpKernelRegistrar<place_class, __VA_ARGS__> \
164-
__op_kernel_registrar_##op_type##_##LIBRARY_TYPE##__(#op_type, \
165-
#LIBRARY_TYPE); \
166-
int TouchOpKernelRegistrar_##op_type##_##LIBRARY_TYPE() { \
167-
__op_kernel_registrar_##op_type##_##LIBRARY_TYPE##__.Touch(); \
164+
__op_kernel_registrar_##op_type##_##library_type##__(#op_type, \
165+
#library_type); \
166+
int TouchOpKernelRegistrar_##op_type##_##library_type() { \
167+
__op_kernel_registrar_##op_type##_##library_type##__.Touch(); \
168168
return 0; \
169169
}
170170

paddle/fluid/operators/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ function(op_library TARGET)
166166
# NOTE(*): activation use macro to regist the kernels, set use_op manually.
167167
if(${TARGET} STREQUAL "activation")
168168
file(APPEND ${pybind_file} "USE_OP(relu);\n")
169-
elseif(${TARGET} STREQUAL "reduce")
170-
file(APPEND ${pybind_file} "USE_OP(reduce_sum);\n")
171169
elseif(${TARGET} STREQUAL "fake_dequantize")
172170
file(APPEND ${pybind_file} "USE_OP(fake_dequantize_max_abs);\n")
173171
else()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/operators/reduce_min_max_op.h"
16+
17+
REGISTER_REDUCE_OP(reduce_max);
18+
REGISTER_OP_CPU_KERNEL(
19+
reduce_max, ops::ReduceKernel<paddle::platform::CPUDeviceContext, float,
20+
ops::MaxFunctor>,
21+
ops::ReduceKernel<paddle::platform::CPUDeviceContext, double,
22+
ops::MaxFunctor>,
23+
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int, ops::MaxFunctor>,
24+
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int64_t,
25+
ops::MaxFunctor>);
26+
REGISTER_OP_CPU_KERNEL(
27+
reduce_max_grad, ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
28+
float, ops::MaxOrMinGradFunctor>,
29+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, double,
30+
ops::MaxOrMinGradFunctor>,
31+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int,
32+
ops::MaxOrMinGradFunctor>,
33+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int64_t,
34+
ops::MaxOrMinGradFunctor>);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/operators/reduce_min_max_op.h"
16+
17+
REGISTER_OP_CUDA_KERNEL(reduce_max,
18+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
19+
float, ops::MaxFunctor>,
20+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
21+
double, ops::MaxFunctor>,
22+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
23+
int, ops::MaxFunctor>,
24+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
25+
int64_t, ops::MaxFunctor>);
26+
REGISTER_OP_CUDA_KERNEL(
27+
reduce_max_grad, ops::ReduceGradKernel<paddle::platform::CUDADeviceContext,
28+
float, ops::MaxOrMinGradFunctor>,
29+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, double,
30+
ops::MaxOrMinGradFunctor>,
31+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int,
32+
ops::MaxOrMinGradFunctor>,
33+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int64_t,
34+
ops::MaxOrMinGradFunctor>);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/operators/reduce_mean_op.h"
16+
17+
REGISTER_REDUCE_OP(reduce_mean);
18+
REGISTER_OP_CPU_KERNEL(reduce_mean,
19+
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
20+
float, ops::MeanFunctor>,
21+
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
22+
double, ops::MeanFunctor>,
23+
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
24+
int, ops::MeanFunctor>,
25+
ops::ReduceKernel<paddle::platform::CPUDeviceContext,
26+
int64_t, ops::MeanFunctor>);
27+
REGISTER_OP_CPU_KERNEL(reduce_mean_grad,
28+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
29+
float, ops::MeanGradFunctor>,
30+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
31+
double, ops::MeanGradFunctor>,
32+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
33+
int, ops::MeanGradFunctor>,
34+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
35+
int64_t, ops::MeanGradFunctor>);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/operators/reduce_mean_op.h"
16+
17+
REGISTER_OP_CUDA_KERNEL(reduce_mean,
18+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
19+
float, ops::MeanFunctor>,
20+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
21+
double, ops::MeanFunctor>,
22+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
23+
int, ops::MeanFunctor>,
24+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
25+
int64_t, ops::MeanFunctor>);
26+
REGISTER_OP_CUDA_KERNEL(
27+
reduce_mean_grad, ops::ReduceGradKernel<paddle::platform::CUDADeviceContext,
28+
float, ops::MeanGradFunctor>,
29+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, double,
30+
ops::MeanGradFunctor>,
31+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int,
32+
ops::MeanGradFunctor>,
33+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int64_t,
34+
ops::MeanGradFunctor>);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "paddle/fluid/operators/reduce_op.h"
18+
19+
namespace paddle {
20+
namespace operators {
21+
22+
struct MeanFunctor {
23+
template <typename DeviceContext, typename X, typename Y, typename Dim>
24+
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
25+
y->device(place) = x->mean(dim);
26+
}
27+
};
28+
29+
struct MeanGradFunctor {
30+
template <typename DeviceContext, typename X, typename Y, typename DX,
31+
typename DY, typename Dim>
32+
void operator()(const DeviceContext& place, X* x, Y* y, DX* dx, DY* dy,
33+
const Dim& dim, int size) {
34+
dx->device(place) = dy->broadcast(dim) / dx->constant(size);
35+
}
36+
};
37+
38+
} // namespace operators
39+
} // namespace paddle
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include "paddle/fluid/operators/reduce_op.h"
17+
18+
namespace paddle {
19+
namespace operators {
20+
21+
struct MaxFunctor {
22+
template <typename DeviceContext, typename X, typename Y, typename Dim>
23+
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
24+
y->device(place) = x->maximum(dim);
25+
}
26+
};
27+
28+
struct MinFunctor {
29+
template <typename DeviceContext, typename X, typename Y, typename Dim>
30+
void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) {
31+
y->device(place) = x->minimum(dim);
32+
}
33+
};
34+
35+
struct MaxOrMinGradFunctor {
36+
template <typename DeviceContext, typename X, typename Y, typename DX,
37+
typename DY, typename Dim>
38+
void operator()(const DeviceContext& place, X* x, Y* y, DX* dx, DY* dy,
39+
const Dim& dim, int size) {
40+
auto equals = (*x) == y->broadcast(dim);
41+
auto ones = dx->constant(1);
42+
auto zeros = dx->constant(0);
43+
// If there are multiple minimum or maximum elements, the subgradient of
44+
// each is the set [0, 1], and we pass gradient to all of them here.
45+
dx->device(place) = dy->broadcast(dim) * equals.select(ones, zeros);
46+
}
47+
};
48+
49+
} // namespace operators
50+
} // namespace paddle
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/operators/reduce_min_max_op.h"
16+
17+
REGISTER_REDUCE_OP(reduce_min);
18+
REGISTER_OP_CPU_KERNEL(
19+
reduce_min, ops::ReduceKernel<paddle::platform::CPUDeviceContext, float,
20+
ops::MinFunctor>,
21+
ops::ReduceKernel<paddle::platform::CPUDeviceContext, double,
22+
ops::MinFunctor>,
23+
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int, ops::MinFunctor>,
24+
ops::ReduceKernel<paddle::platform::CPUDeviceContext, int64_t,
25+
ops::MinFunctor>);
26+
REGISTER_OP_CPU_KERNEL(
27+
reduce_min_grad, ops::ReduceGradKernel<paddle::platform::CPUDeviceContext,
28+
float, ops::MaxOrMinGradFunctor>,
29+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, double,
30+
ops::MaxOrMinGradFunctor>,
31+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int,
32+
ops::MaxOrMinGradFunctor>,
33+
ops::ReduceGradKernel<paddle::platform::CPUDeviceContext, int64_t,
34+
ops::MaxOrMinGradFunctor>);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/operators/reduce_min_max_op.h"
16+
17+
REGISTER_OP_CUDA_KERNEL(reduce_min,
18+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
19+
float, ops::MinFunctor>,
20+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
21+
double, ops::MinFunctor>,
22+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
23+
int, ops::MinFunctor>,
24+
ops::ReduceKernel<paddle::platform::CUDADeviceContext,
25+
int64_t, ops::MinFunctor>);
26+
REGISTER_OP_CUDA_KERNEL(
27+
reduce_min_grad, ops::ReduceGradKernel<paddle::platform::CUDADeviceContext,
28+
float, ops::MaxOrMinGradFunctor>,
29+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, double,
30+
ops::MaxOrMinGradFunctor>,
31+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int,
32+
ops::MaxOrMinGradFunctor>,
33+
ops::ReduceGradKernel<paddle::platform::CUDADeviceContext, int64_t,
34+
ops::MaxOrMinGradFunctor>);

0 commit comments

Comments
 (0)