|
|
|
@ -42,34 +42,36 @@ class SppKernel : public framework::OpKernel<T> {
|
|
|
|
|
std::vector<int> strides({ksize_h, ksize_w});
|
|
|
|
|
std::vector<int> paddings({padding_h, padding_w});
|
|
|
|
|
// pooling output shape
|
|
|
|
|
framework::Tensor out_level;
|
|
|
|
|
std::vector<int64_t> output_shape_vec({in_x->dims()[0], in_x->dims()[1]});
|
|
|
|
|
output_shape_vec.push_back((input_h - ksize_h + 2 * padding_h) / ksize_h +
|
|
|
|
|
1);
|
|
|
|
|
output_shape_vec.push_back((input_w - ksize_w + 2 * padding_w) / ksize_w +
|
|
|
|
|
1);
|
|
|
|
|
framework::DDim output_shape(framework::make_ddim(output_shape_vec));
|
|
|
|
|
// flatten pooling output shape
|
|
|
|
|
int output_flatten_w = in_x->dims()[1] * bins * bins;
|
|
|
|
|
std::vector<int64_t> output_flatten_shape_vec(
|
|
|
|
|
{in_x->dims()[0], output_flatten_w});
|
|
|
|
|
framework::DDim output_flatten_shape(
|
|
|
|
|
framework::make_ddim(output_flatten_shape_vec));
|
|
|
|
|
framework::Tensor out_level;
|
|
|
|
|
framework::Tensor out_flatten_level;
|
|
|
|
|
out_level.mutable_data<T>(output_shape, context.GetPlace());
|
|
|
|
|
// pooling
|
|
|
|
|
math::Pool2dFunctor<Place, math::MaxPool<T>, T> pool_forward;
|
|
|
|
|
math::MaxPool<T> max_process;
|
|
|
|
|
pool_forward(context.device_context(), *in_x, ksize, strides, paddings,
|
|
|
|
|
max_process, &out_level);
|
|
|
|
|
// flatten pooling output shape
|
|
|
|
|
framework::Tensor out_flatten_level;
|
|
|
|
|
int output_flatten_w = in_x->dims()[1] * bins * bins;
|
|
|
|
|
std::vector<int64_t> output_flatten_shape_vec(
|
|
|
|
|
{in_x->dims()[0], output_flatten_w});
|
|
|
|
|
framework::DDim output_flatten_shape(
|
|
|
|
|
framework::make_ddim(output_flatten_shape_vec));
|
|
|
|
|
out_flatten_level.ShareDataWith(out_level);
|
|
|
|
|
out_flatten_level.Resize(output_flatten_shape);
|
|
|
|
|
auto in_stride = framework::stride(out_flatten_level.dims());
|
|
|
|
|
const T* src_data = out_flatten_level.data<T>();
|
|
|
|
|
StridedMemcpy<T>(context.device_context(), src_data, in_stride,
|
|
|
|
|
out_flatten_level.dims(), out_stride,
|
|
|
|
|
out->data<T>() + output_offset);
|
|
|
|
|
output_offset += out_flatten_level.dims()[1] * in_stride[1];
|
|
|
|
|
// concat
|
|
|
|
|
auto out_flatten_level_stride =
|
|
|
|
|
framework::stride(out_flatten_level.dims());
|
|
|
|
|
StridedMemcpy<T>(context.device_context(), out_flatten_level.data<T>(),
|
|
|
|
|
out_flatten_level_stride, out_flatten_level.dims(),
|
|
|
|
|
out_stride, out->data<T>() + output_offset);
|
|
|
|
|
output_offset +=
|
|
|
|
|
out_flatten_level.dims()[1] * out_flatten_level_stride[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -83,12 +85,11 @@ class SppGradKernel : public framework::OpKernel<T> {
|
|
|
|
|
context.Input<framework::Tensor>(framework::GradVarName("Out"));
|
|
|
|
|
framework::Tensor* in_x_grad =
|
|
|
|
|
context.Output<framework::Tensor>(framework::GradVarName("X"));
|
|
|
|
|
int pyramid_height = context.template Attr<int>("pyramid_height");
|
|
|
|
|
auto& device_ctx = context.device_context();
|
|
|
|
|
math::SetConstant<Place, T> zero;
|
|
|
|
|
in_x_grad->mutable_data<T>(context.GetPlace());
|
|
|
|
|
zero(device_ctx, in_x_grad, static_cast<T>(0));
|
|
|
|
|
int pyramid_height = context.template Attr<int>("pyramid_height");
|
|
|
|
|
auto outgrad_stride = framework::stride(out_grad->dims());
|
|
|
|
|
auto out_stride = framework::stride(out->dims());
|
|
|
|
|
int input_h = in_x->dims()[2];
|
|
|
|
|
int input_w = in_x->dims()[3];
|
|
|
|
@ -102,26 +103,17 @@ class SppGradKernel : public framework::OpKernel<T> {
|
|
|
|
|
std::vector<int> ksize({ksize_h, ksize_w});
|
|
|
|
|
std::vector<int> strides({ksize_h, ksize_w});
|
|
|
|
|
std::vector<int> paddings({padding_h, padding_w});
|
|
|
|
|
// split outgrad and get flatten
|
|
|
|
|
std::vector<int64_t> out_shape_vec({in_x->dims()[0], in_x->dims()[1]});
|
|
|
|
|
out_shape_vec.push_back((input_h - ksize_h + 2 * padding_h) / ksize_h +
|
|
|
|
|
1);
|
|
|
|
|
out_shape_vec.push_back((input_w - ksize_w + 2 * padding_w) / ksize_w +
|
|
|
|
|
1);
|
|
|
|
|
framework::DDim out_shape(framework::make_ddim(out_shape_vec));
|
|
|
|
|
// split out and outgrad ... to flatten
|
|
|
|
|
framework::Tensor out_flatten_level;
|
|
|
|
|
framework::Tensor outgrad_flatten_level;
|
|
|
|
|
int out_flatten_w = in_x->dims()[1] * bins * bins;
|
|
|
|
|
std::vector<int64_t> out_flatten_shape_vec(
|
|
|
|
|
{in_x->dims()[0], out_flatten_w});
|
|
|
|
|
framework::DDim out_flatten_shape(
|
|
|
|
|
framework::make_ddim(out_flatten_shape_vec));
|
|
|
|
|
framework::Tensor out_level;
|
|
|
|
|
framework::Tensor outgrad_level;
|
|
|
|
|
framework::Tensor out_flatten_level;
|
|
|
|
|
framework::Tensor outgrad_flatten_level;
|
|
|
|
|
out_flatten_level.mutable_data<T>(out_flatten_shape, context.GetPlace());
|
|
|
|
|
outgrad_flatten_level.mutable_data<T>(out_flatten_shape,
|
|
|
|
|
context.GetPlace());
|
|
|
|
|
|
|
|
|
|
auto flatten_stride = framework::stride(out_flatten_level.dims());
|
|
|
|
|
// memcpy
|
|
|
|
|
StridedMemcpy<T>(context.device_context(), out->data<T>() + out_offset,
|
|
|
|
@ -129,15 +121,24 @@ class SppGradKernel : public framework::OpKernel<T> {
|
|
|
|
|
out_flatten_level.data<T>());
|
|
|
|
|
|
|
|
|
|
StridedMemcpy<T>(context.device_context(),
|
|
|
|
|
out_grad->data<T>() + out_offset, outgrad_stride,
|
|
|
|
|
out_grad->data<T>() + out_offset, out_stride,
|
|
|
|
|
outgrad_flatten_level.dims(), flatten_stride,
|
|
|
|
|
outgrad_flatten_level.data<T>());
|
|
|
|
|
out_offset += out_flatten_level.dims()[1] * out_stride[1];
|
|
|
|
|
// flatten backward
|
|
|
|
|
// flatten backward to nchw
|
|
|
|
|
framework::Tensor out_level;
|
|
|
|
|
framework::Tensor outgrad_level;
|
|
|
|
|
std::vector<int64_t> out_shape_vec({in_x->dims()[0], in_x->dims()[1]});
|
|
|
|
|
out_shape_vec.push_back((input_h - ksize_h + 2 * padding_h) / ksize_h +
|
|
|
|
|
1);
|
|
|
|
|
out_shape_vec.push_back((input_w - ksize_w + 2 * padding_w) / ksize_w +
|
|
|
|
|
1);
|
|
|
|
|
framework::DDim out_shape(framework::make_ddim(out_shape_vec));
|
|
|
|
|
out_level.ShareDataWith(out_flatten_level);
|
|
|
|
|
out_level.Resize(out_shape);
|
|
|
|
|
outgrad_level.ShareDataWith(outgrad_flatten_level);
|
|
|
|
|
outgrad_level.Resize(out_shape);
|
|
|
|
|
// pooling backward
|
|
|
|
|
math::MaxPool2dGradFunctor<Place, T> pool2d_backward;
|
|
|
|
|
pool2d_backward(context.device_context(), *in_x, *&out_level,
|
|
|
|
|
*&outgrad_level, ksize, strides, paddings, in_x_grad);
|
|
|
|
|