Skip to content

[XPU][PHI Kernels] add int_with_ll quantization for conv kernels #54827

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

Merged
merged 2 commits into from
Jun 28, 2023
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
59 changes: 55 additions & 4 deletions paddle/phi/kernels/xpu/conv_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void ConvGradKernel(const Context& dev_ctx,
}
}
int fccal_type = FCCalcType<XPUT>();
if (fccal_type == 1) {
if (fccal_type == XPUFCCalcType::FC_INT32) {
int r = xpu::conv2d_grad<XPUT, XPUT, XPUT, int>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -132,7 +132,7 @@ void ConvGradKernel(const Context& dev_ctx,
is_nchw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d_grad");

} else if (fccal_type == 2) {
} else if (fccal_type == XPUFCCalcType::FC_FLOAT) {
int r = xpu::conv2d_grad<XPUT, XPUT, XPUT, float>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -157,6 +157,31 @@ void ConvGradKernel(const Context& dev_ctx,
is_nchw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d_grad");

} else if (fccal_type == XPUFCCalcType::FC_INT32_WITH_LL) {
int r =
xpu::conv2d_grad<XPUT, XPUT, XPUT, int_with_ll_t>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
output_grad_data,
input_grad_data,
filter_grad_data_ptr,
batch_size,
img_c,
img_h,
img_w,
f,
ksize,
strides,
paddings,
dilations,
groups,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
is_nchw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d_grad");
} else {
int r = xpu::conv2d_grad<XPUT, XPUT, XPUT, int16_t>(dev_ctx.x_context(),
input_data,
Expand Down Expand Up @@ -305,7 +330,7 @@ void Conv3DGradKernel(const Context& dev_ctx,
}
}
int fccal_type = FCCalcType<XPUT>();
if (fccal_type == 1) {
if (fccal_type == XPUFCCalcType::FC_INT32) {
int r = xpu::conv3d_grad<XPUT, XPUT, XPUT, int>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -330,7 +355,7 @@ void Conv3DGradKernel(const Context& dev_ctx,
nullptr,
is_ncdhw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv3d_grad");
} else if (fccal_type == 2) {
} else if (fccal_type == XPUFCCalcType::FC_FLOAT) {
int r = xpu::conv3d_grad<XPUT, XPUT, XPUT, float>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -355,6 +380,32 @@ void Conv3DGradKernel(const Context& dev_ctx,
nullptr,
is_ncdhw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv3d_grad");
} else if (fccal_type == XPUFCCalcType::FC_INT32_WITH_LL) {
int r =
xpu::conv3d_grad<XPUT, XPUT, XPUT, int_with_ll_t>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
output_grad_data,
input_grad_data,
filter_grad_data_ptr,
batch_size,
img_c,
img_d,
img_h,
img_w,
f,
ksize,
strides,
paddings,
dilations,
groups,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
is_ncdhw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv3d_grad");
} else {
int r = xpu::conv3d_grad<XPUT, XPUT, XPUT, int16_t>(dev_ctx.x_context(),
input_data,
Expand Down
49 changes: 45 additions & 4 deletions paddle/phi/kernels/xpu/conv_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void ConvKernel(const Context& dev_ctx,
}

int fccal_type = FCCalcType<XPUT>();
if (fccal_type == 1) {
if (fccal_type == XPUFCCalcType::FC_INT32) {
int r = xpu::conv2d<XPUT, XPUT, XPUT, int>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -109,7 +109,7 @@ void ConvKernel(const Context& dev_ctx,
nullptr,
is_nchw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d");
} else if (fccal_type == 2) {
} else if (fccal_type == XPUFCCalcType::FC_FLOAT) {
int r = xpu::conv2d<XPUT, XPUT, XPUT, float>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -129,6 +129,26 @@ void ConvKernel(const Context& dev_ctx,
nullptr,
is_nchw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d");
} else if (fccal_type == XPUFCCalcType::FC_INT32_WITH_LL) {
int r = xpu::conv2d<XPUT, XPUT, XPUT, int_with_ll_t>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
output_data,
batch_size,
img_c,
img_h,
img_w,
f,
ksize,
strides,
paddings,
dilations,
groups,
nullptr,
nullptr,
nullptr,
is_nchw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d");
} else {
int r = xpu::conv2d<XPUT, XPUT, XPUT, int16_t>(dev_ctx.x_context(),
input_data,
Expand Down Expand Up @@ -239,7 +259,7 @@ void Conv3DKernel(const Context& dev_ctx,
}

int fccal_type = FCCalcType<XPUT>();
if (fccal_type == 1) {
if (fccal_type == XPUFCCalcType::FC_INT32) {
int r = xpu::conv3d<XPUT, XPUT, XPUT, int>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -260,7 +280,7 @@ void Conv3DKernel(const Context& dev_ctx,
nullptr,
is_ncdhw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv3d");
} else if (fccal_type == 2) {
} else if (fccal_type == XPUFCCalcType::FC_FLOAT) {
int r = xpu::conv3d<XPUT, XPUT, XPUT, float>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
Expand All @@ -282,6 +302,27 @@ void Conv3DKernel(const Context& dev_ctx,
is_ncdhw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv3d");

} else if (fccal_type == XPUFCCalcType::FC_INT32_WITH_LL) {
int r = xpu::conv3d<XPUT, XPUT, XPUT, int_with_ll_t>(dev_ctx.x_context(),
input_data,
filter_data_ptr,
output_data,
batch_size,
img_c,
img_d,
img_h,
img_w,
f,
ksize,
strides,
paddings,
dilations,
groups,
nullptr,
nullptr,
nullptr,
is_ncdhw);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv3d");
} else {
int r = xpu::conv3d<XPUT, XPUT, XPUT, int16_t>(dev_ctx.x_context(),
input_data,
Expand Down
53 changes: 53 additions & 0 deletions paddle/phi/kernels/xpu/conv_transpose_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "paddle/phi/kernels/conv_transpose_kernel.h"

#include "glog/logging.h"

#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/cpu/conv_util.h"
Expand Down Expand Up @@ -122,6 +124,57 @@ void Conv2dTransposeKernel(const Context& ctx,
nullptr,
true);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d_transpose_v2");
} else if (fccal_type == XPUFCCalcType::FC_INT32_WITH_LL) {
if (output_size.size()) {
VLOG(4) << "int_with_ll quantization is not supported when output_size "
"is specified, "
<< "use int31 instead";
int r = xpu::conv2d_transpose_v2<float, float, float, int32_t>(
ctx.x_context(),
x.data<float>(),
filter_.data<float>(),
out->data<float>(),
batch_size,
img_yc,
img_xh,
img_xw,
img_xc,
ksize,
strides,
paddings_,
dilations_,
groups,
nullptr,
nullptr,
nullptr,
true);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d_transpose_v2");
} else {
// xpu::conv2d_transpose_v2 do not support int_with_ll now
// use xpu::conv2d_transpose
int img_yh = static_cast<int>(x.dims()[2]);
int img_yw = static_cast<int>(x.dims()[3]);
int r = xpu::conv2d_transpose<float, float, float, int_with_ll_t>(
ctx.x_context(),
x.data<float>(),
filter_.data<float>(),
out->data<float>(),
batch_size,
img_yc,
img_yh,
img_yw,
img_xc,
ksize,
strides,
paddings_,
dilations_,
groups,
nullptr,
nullptr,
nullptr,
true);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d_transpose");
}
} else {
int r = xpu::conv2d_transpose_v2<XPUT, XPUT, XPUT, int16_t>(
ctx.x_context(),
Expand Down