Skip to content
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
1 change: 1 addition & 0 deletions paddle/fluid/operators/p_norm_op_xpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class P_NormXPUKernel : public framework::OpKernel<T> {
XPUType* zeros = RAII_GUARD.alloc_l3_or_gm<XPUType>(1);
PADDLE_ENFORCE_XDNN_NOT_NULL(zeros);
r = xpu::constant(dev_ctx.x_context(), zeros, 1, 0.0f);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "constant");
std::vector<int> zeros_dim(1, 1);

bool* tmp2_x = RAII_GUARD.alloc_l3_or_gm<bool>(m * t * n);
Expand Down
133 changes: 91 additions & 42 deletions paddle/fluid/operators/pool_op_xpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ class PoolXPUKernel : public framework::OpKernel<T> {
2,
platform::errors::InvalidArgument(
"The Pool2d XPU OP only support 2 dimension pooling!"));
PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1),
true,
platform::errors::InvalidArgument(
"The Pool2d XPU OP does not support (adaptive == "
"true && output_size != 1)"));

int* index_data = nullptr;
bool global_pooling = context.Attr<bool>("global_pooling") ||
(adaptive && (ksize[0] * ksize[1] == 1));
Expand All @@ -80,6 +76,9 @@ class PoolXPUKernel : public framework::OpKernel<T> {
const int in_h = in_x->dims()[2];
const int in_w = in_x->dims()[3];

const int out_h = out->dims()[2];
const int out_w = out->dims()[3];

framework::DDim data_dims;

data_dims = phi::slice_ddim(in_x->dims(), 2, in_x->dims().size());
Expand All @@ -90,45 +89,79 @@ class PoolXPUKernel : public framework::OpKernel<T> {
data_dims,
strides,
ksize);

if (ceil_mode) {
paddings[1] += (strides[0] - 1);
paddings[3] += (strides[1] - 1);
int in_h_ceil = (out_h - 1) * strides[0] + ksize[0] - 2 * paddings[0];
int in_w_ceil = (out_w - 1) * strides[1] + ksize[1] - 2 * paddings[2];

paddings[1] += (in_h_ceil - in_h);
paddings[3] += (in_w_ceil - in_w);
}

auto input = reinterpret_cast<const XPUType*>(in_x->data<T>());
out->mutable_data<T>(context.GetPlace());
auto output = reinterpret_cast<XPUType*>(out->data<T>());
auto& dev_ctx = context.template device_context<DeviceContext>();
int r = xpu::Error_t::SUCCESS;
if (pooling_type == "max") {
r = xpu::max_pool2d<XPUType>(dev_ctx.x_context(),
input,
output,
index_data,
n,
c,
in_h,
in_w,
ksize,
strides,
paddings,
true);
} else if (pooling_type == "avg") {
r = xpu::avg_pool2d<XPUType>(dev_ctx.x_context(),
input,
output,
n,
c,
in_h,
in_w,
ksize,
strides,
paddings,
!exclusive,
true);
if (!adaptive) {
if (pooling_type == "max") {
r = xpu::max_pool2d<XPUType>(dev_ctx.x_context(),
input,
output,
index_data,
n,
c,
in_h,
in_w,
ksize,
strides,
paddings,
true);
} else if (pooling_type == "avg") {
r = xpu::avg_pool2d<XPUType>(dev_ctx.x_context(),
input,
output,
n,
c,
in_h,
in_w,
ksize,
strides,
paddings,
!exclusive,
true);
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Unsupported pooling type for kunlun ", pooling_type));
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Unsupported pooling type for kunlun ", pooling_type));
if (pooling_type == "max") {
r = xpu::adaptive_max_pool2d<XPUType>(dev_ctx.x_context(),
input,
output,
index_data,
n,
c,
in_h,
in_w,
out_h,
out_w,
true);
} else if (pooling_type == "avg") {
r = xpu::adaptive_avg_pool2d<XPUType>(dev_ctx.x_context(),
input,
output,
n,
c,
in_h,
in_w,
out_h,
out_w,
true);
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Unsupported pooling type for kunlun ", pooling_type));
}
}
PADDLE_ENFORCE_EQ(r,
xpu::Error_t::SUCCESS,
Expand Down Expand Up @@ -167,11 +200,6 @@ class PoolGradXPUKernel : public framework::OpKernel<T> {
"dimension pooling!, but received "
"%d-dimension pool kernel size",
ksize.size()));
PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1),
true,
platform::errors::InvalidArgument(
"The Pool2d XPU OP does not support (adaptive == "
"true && output_size != 1)"));
bool global_pooling = context.Attr<bool>("global_pooling") ||
(adaptive && (ksize[0] * ksize[1] == 1));
if (global_pooling) {
Expand All @@ -188,6 +216,16 @@ class PoolGradXPUKernel : public framework::OpKernel<T> {
const int in_h = in_x->dims()[2];
const int in_w = in_x->dims()[3];

const int out_h = out->dims()[2];
const int out_w = out->dims()[3];

PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1) ||
(in_h % out_h == 0 && in_w % out_w == 0),
true,
platform::errors::InvalidArgument(
"The Pool2d XPU OP does not support (adaptive == "
"true && output_size != 1)"));

framework::DDim data_dims;

data_dims = phi::slice_ddim(in_x->dims(), 2, in_x->dims().size());
Expand All @@ -199,8 +237,11 @@ class PoolGradXPUKernel : public framework::OpKernel<T> {
strides,
ksize);
if (ceil_mode) {
paddings[1] += (strides[0] - 1);
paddings[3] += (strides[1] - 1);
int in_h_ceil = (out_h - 1) * strides[0] + ksize[0] - 2 * paddings[0];
int in_w_ceil = (out_w - 1) * strides[1] + ksize[1] - 2 * paddings[2];

paddings[1] += (in_h_ceil - in_h);
paddings[3] += (in_w_ceil - in_w);
}

auto input = reinterpret_cast<const XPUType*>(in_x->data<T>());
Expand All @@ -210,6 +251,14 @@ class PoolGradXPUKernel : public framework::OpKernel<T> {
auto input_grad = reinterpret_cast<XPUType*>(in_x_grad->data<T>());
auto& dev_ctx = context.template device_context<DeviceContext>();
int r = xpu::Error_t::SUCCESS;
if (adaptive && in_h % out_h == 0 && in_w % out_w == 0) {
strides = {in_h / out_h, in_w / out_w};
int kh = in_h - (out_h - 1) * strides[0];
int kw = in_w - (out_w - 1) * strides[1];
ksize = {kh, kw};
paddings = {0, 0, 0, 0};
}

if (pooling_type == "max") {
r = xpu::max_pool2d_grad<XPUType>(dev_ctx.x_context(),
input,
Expand Down
Loading