Skip to content

Commit fcb2137

Browse files
authored
【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API -kernel (#59847)
* [Init] add fractional max pool kernel and api * [Fix] pooling.cu seed offset * [Change] remove adaptive from fractional max pool * [Change] fractional max 2d gpu pooling.cu grad * [Change] fractional max 2d gpu pooling.cu grad with dim3 * [Change] use UnchangedInferMeta * [Change] test api with uint16 * [Change] wrap test disable_static * [Change] regiester float16/bfloat16 * [Change] remove bfloat16 from cpu kernrl * [Change] test dtypes in cpu and gpu * [Change] test_fractional_max_pool3d_2d/3d timeout to 30s * [Fix] resolve conflict * [Change] win32 cannot detect bfloat16 correctly * [Change] force set_device * [Add] test random_u is None * [Change] use kernel_size for overlapping mode * [Change] clean headers * [CodeStyle] pooling * [Change] rename op * [Change] rename func without index
1 parent 600fc2f commit fcb2137

27 files changed

+3885
-2
lines changed

paddle/phi/api/yaml/backward.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,26 @@
923923
data_type : out_grad
924924
no_need_buffer : x
925925

926+
- backward_op : fractional_max_pool2d_grad
927+
forward : fractional_max_pool2d(Tensor x, int[] output_size, int[] kernel_size = {0, 0}, float random_u = 0.0, bool return_mask = true) -> Tensor(out), Tensor(mask)
928+
args : (Tensor x, Tensor mask, Tensor out_grad, int[] output_size, int[] kernel_size, float random_u, bool return_mask)
929+
output : Tensor(x_grad)
930+
infer_meta :
931+
func : UnchangedInferMeta
932+
param : [x]
933+
kernel :
934+
func : fractional_max_pool2d_grad
935+
936+
- backward_op : fractional_max_pool3d_grad
937+
forward : fractional_max_pool3d(Tensor x, int[] output_size, int[] kernel_size = {0, 0, 0}, float random_u = 0.0, bool return_mask = true) -> Tensor(out), Tensor(mask)
938+
args : (Tensor x, Tensor mask, Tensor out_grad, int[] output_size, int[] kernel_size, float random_u, bool return_mask)
939+
output : Tensor(x_grad)
940+
infer_meta :
941+
func : UnchangedInferMeta
942+
param : [x]
943+
kernel :
944+
func : fractional_max_pool3d_grad
945+
926946
- backward_op : frame_grad
927947
forward : frame(Tensor x, int frame_length, int hop_length, int axis=-1) -> Tensor(out)
928948
args : (Tensor x, Tensor out_grad, int frame_length, int hop_length, int axis)

paddle/phi/api/yaml/ops.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,24 @@
10521052
func: fold
10531053
backward: fold_grad
10541054

1055+
- op : fractional_max_pool2d
1056+
args : (Tensor x, int[] output_size, int[] kernel_size = {0, 0}, float random_u = 0.0, bool return_mask = true)
1057+
output : Tensor(out), Tensor(mask)
1058+
infer_meta :
1059+
func : FractionalMaxPoolInferMeta
1060+
kernel :
1061+
func : fractional_max_pool2d
1062+
backward : fractional_max_pool2d_grad
1063+
1064+
- op : fractional_max_pool3d
1065+
args : (Tensor x, int[] output_size, int[] kernel_size = {0, 0, 0}, float random_u = 0.0, bool return_mask = true)
1066+
output : Tensor(out), Tensor(mask)
1067+
infer_meta :
1068+
func : FractionalMaxPoolInferMeta
1069+
kernel :
1070+
func : fractional_max_pool3d
1071+
backward : fractional_max_pool3d_grad
1072+
10551073
- op : frame
10561074
args : (Tensor x, int frame_length, int hop_length, int axis=-1)
10571075
output : Tensor(out)

paddle/phi/infermeta/unary.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,44 @@ void FoldInferMeta(const MetaTensor& x,
17291729
}
17301730
}
17311731

1732+
void FractionalMaxPoolInferMeta(const MetaTensor& x,
1733+
const std::vector<int>& output_size,
1734+
const std::vector<int>& kernel_size,
1735+
float random_u,
1736+
bool return_mask,
1737+
MetaTensor* out,
1738+
MetaTensor* mask,
1739+
MetaConfig config) {
1740+
std::vector<int> output_size_ = output_size;
1741+
1742+
auto x_dims = x.dims();
1743+
1744+
PADDLE_ENFORCE_EQ(
1745+
(x_dims.size() == 4 || x_dims.size() == 5),
1746+
true,
1747+
errors::InvalidArgument("Pooling intput should be 4-D or "
1748+
"5-D tensor but received %dD-Tensor",
1749+
x_dims.size()));
1750+
1751+
PADDLE_ENFORCE_EQ(
1752+
x_dims.size() - output_size_.size(),
1753+
2U,
1754+
errors::InvalidArgument(
1755+
"The input size %d minus the output size %d should equal to 2.",
1756+
x_dims.size(),
1757+
output_size_.size()));
1758+
1759+
std::vector<int64_t> output_shape({x_dims[0], x_dims[1]});
1760+
output_shape.insert(
1761+
output_shape.end(), output_size_.begin(), output_size_.end());
1762+
1763+
out->set_dims(common::make_ddim(output_shape));
1764+
out->set_dtype(x.dtype());
1765+
1766+
mask->set_dims(common::make_ddim(output_shape));
1767+
mask->set_dtype(phi::CppTypeToDataType<int>::Type());
1768+
}
1769+
17321770
void FrameInferMeta(const MetaTensor& x,
17331771
int frame_length,
17341772
int hop_length,

paddle/phi/infermeta/unary.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ void FoldInferMeta(const MetaTensor& x,
275275
const std::vector<int>& dilations,
276276
MetaTensor* out);
277277

278+
void FractionalMaxPoolInferMeta(const MetaTensor& x,
279+
const std::vector<int>& output_size,
280+
const std::vector<int>& kernel_size,
281+
float random_u,
282+
bool return_mask,
283+
MetaTensor* out,
284+
MetaTensor* mask,
285+
MetaConfig config = MetaConfig());
286+
278287
void FrameInferMeta(const MetaTensor& x,
279288
int frame_length,
280289
int hop_length,

paddle/phi/kernels/cpu/pool_grad_kernel.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ PD_REGISTER_KERNEL(max_pool3d_with_index_grad,
4444
double) {
4545
kernel->InputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
4646
}
47+
48+
PD_REGISTER_KERNEL(fractional_max_pool2d_grad,
49+
CPU,
50+
ALL_LAYOUT,
51+
phi::FractionalMaxPool2dGradKernel,
52+
float,
53+
double,
54+
phi::dtype::float16) {
55+
kernel->InputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
56+
}
57+
58+
PD_REGISTER_KERNEL(fractional_max_pool3d_grad,
59+
CPU,
60+
ALL_LAYOUT,
61+
phi::FractionalMaxPool3dGradKernel,
62+
float,
63+
double,
64+
phi::dtype::float16) {
65+
kernel->InputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
66+
}

paddle/phi/kernels/cpu/pool_kernel.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,23 @@ PD_REGISTER_KERNEL(max_pool3d_with_index,
3636
double) {
3737
kernel->OutputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
3838
}
39+
40+
PD_REGISTER_KERNEL(fractional_max_pool2d,
41+
CPU,
42+
ALL_LAYOUT,
43+
phi::FractionalMaxPool2dKernel,
44+
float,
45+
double,
46+
phi::dtype::float16) {
47+
kernel->OutputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
48+
}
49+
50+
PD_REGISTER_KERNEL(fractional_max_pool3d,
51+
CPU,
52+
ALL_LAYOUT,
53+
phi::FractionalMaxPool3dKernel,
54+
float,
55+
double,
56+
phi::dtype::float16) {
57+
kernel->OutputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
58+
}

0 commit comments

Comments
 (0)