Skip to content

Commit

Permalink
【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxP…
Browse files Browse the repository at this point in the history
…ool3d 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
  • Loading branch information
megemini authored Jan 12, 2024
1 parent 600fc2f commit fcb2137
Show file tree
Hide file tree
Showing 27 changed files with 3,885 additions and 2 deletions.
20 changes: 20 additions & 0 deletions paddle/phi/api/yaml/backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,26 @@
data_type : out_grad
no_need_buffer : x

- backward_op : fractional_max_pool2d_grad
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)
args : (Tensor x, Tensor mask, Tensor out_grad, int[] output_size, int[] kernel_size, float random_u, bool return_mask)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : fractional_max_pool2d_grad

- backward_op : fractional_max_pool3d_grad
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)
args : (Tensor x, Tensor mask, Tensor out_grad, int[] output_size, int[] kernel_size, float random_u, bool return_mask)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : fractional_max_pool3d_grad

- backward_op : frame_grad
forward : frame(Tensor x, int frame_length, int hop_length, int axis=-1) -> Tensor(out)
args : (Tensor x, Tensor out_grad, int frame_length, int hop_length, int axis)
Expand Down
18 changes: 18 additions & 0 deletions paddle/phi/api/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,24 @@
func: fold
backward: fold_grad

- op : fractional_max_pool2d
args : (Tensor x, int[] output_size, int[] kernel_size = {0, 0}, float random_u = 0.0, bool return_mask = true)
output : Tensor(out), Tensor(mask)
infer_meta :
func : FractionalMaxPoolInferMeta
kernel :
func : fractional_max_pool2d
backward : fractional_max_pool2d_grad

- op : fractional_max_pool3d
args : (Tensor x, int[] output_size, int[] kernel_size = {0, 0, 0}, float random_u = 0.0, bool return_mask = true)
output : Tensor(out), Tensor(mask)
infer_meta :
func : FractionalMaxPoolInferMeta
kernel :
func : fractional_max_pool3d
backward : fractional_max_pool3d_grad

- op : frame
args : (Tensor x, int frame_length, int hop_length, int axis=-1)
output : Tensor(out)
Expand Down
38 changes: 38 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,44 @@ void FoldInferMeta(const MetaTensor& x,
}
}

void FractionalMaxPoolInferMeta(const MetaTensor& x,
const std::vector<int>& output_size,
const std::vector<int>& kernel_size,
float random_u,
bool return_mask,
MetaTensor* out,
MetaTensor* mask,
MetaConfig config) {
std::vector<int> output_size_ = output_size;

auto x_dims = x.dims();

PADDLE_ENFORCE_EQ(
(x_dims.size() == 4 || x_dims.size() == 5),
true,
errors::InvalidArgument("Pooling intput should be 4-D or "
"5-D tensor but received %dD-Tensor",
x_dims.size()));

PADDLE_ENFORCE_EQ(
x_dims.size() - output_size_.size(),
2U,
errors::InvalidArgument(
"The input size %d minus the output size %d should equal to 2.",
x_dims.size(),
output_size_.size()));

std::vector<int64_t> output_shape({x_dims[0], x_dims[1]});
output_shape.insert(
output_shape.end(), output_size_.begin(), output_size_.end());

out->set_dims(common::make_ddim(output_shape));
out->set_dtype(x.dtype());

mask->set_dims(common::make_ddim(output_shape));
mask->set_dtype(phi::CppTypeToDataType<int>::Type());
}

void FrameInferMeta(const MetaTensor& x,
int frame_length,
int hop_length,
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ void FoldInferMeta(const MetaTensor& x,
const std::vector<int>& dilations,
MetaTensor* out);

void FractionalMaxPoolInferMeta(const MetaTensor& x,
const std::vector<int>& output_size,
const std::vector<int>& kernel_size,
float random_u,
bool return_mask,
MetaTensor* out,
MetaTensor* mask,
MetaConfig config = MetaConfig());

void FrameInferMeta(const MetaTensor& x,
int frame_length,
int hop_length,
Expand Down
20 changes: 20 additions & 0 deletions paddle/phi/kernels/cpu/pool_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ PD_REGISTER_KERNEL(max_pool3d_with_index_grad,
double) {
kernel->InputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
}

PD_REGISTER_KERNEL(fractional_max_pool2d_grad,
CPU,
ALL_LAYOUT,
phi::FractionalMaxPool2dGradKernel,
float,
double,
phi::dtype::float16) {
kernel->InputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
}

PD_REGISTER_KERNEL(fractional_max_pool3d_grad,
CPU,
ALL_LAYOUT,
phi::FractionalMaxPool3dGradKernel,
float,
double,
phi::dtype::float16) {
kernel->InputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
}
20 changes: 20 additions & 0 deletions paddle/phi/kernels/cpu/pool_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ PD_REGISTER_KERNEL(max_pool3d_with_index,
double) {
kernel->OutputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
}

PD_REGISTER_KERNEL(fractional_max_pool2d,
CPU,
ALL_LAYOUT,
phi::FractionalMaxPool2dKernel,
float,
double,
phi::dtype::float16) {
kernel->OutputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
}

PD_REGISTER_KERNEL(fractional_max_pool3d,
CPU,
ALL_LAYOUT,
phi::FractionalMaxPool3dKernel,
float,
double,
phi::dtype::float16) {
kernel->OutputAt(1).SetDataType(phi::CppTypeToDataType<int>::Type());
}
Loading

0 comments on commit fcb2137

Please sign in to comment.