| 22 | |
| 23 | template <typename T, typename Context> |
| 24 | void UnfoldGradKernel(const Context& dev_ctx, |
| 25 | const DenseTensor& x, |
| 26 | const DenseTensor& out_grad, |
| 27 | const std::vector<int>& kernel_sizes_, |
| 28 | const std::vector<int>& strides_, |
| 29 | const std::vector<int>& paddings_, |
| 30 | const std::vector<int>& dilations_, |
| 31 | DenseTensor* x_grad) { |
| 32 | using XPUType = typename XPUTypeTrait<T>::Type; |
| 33 | dev_ctx.template Alloc<T>(x_grad); |
| 34 | if (x_grad->numel() == 0) { |
| 35 | return; |
| 36 | } |
| 37 | const std::string data_format = DataLayoutToString(x.layout()); |
| 38 | bool is_nchw = data_format == "NCHW"; |
| 39 | PADDLE_ENFORCE_EQ(is_nchw, |
| 40 | true, |
| 41 | common::errors::PreconditionNotMet( |
| 42 | "Unfold grad op only supports datalayout == NCHW")); |
| 43 | |
| 44 | auto x_dims = x_grad->dims(); |
| 45 | int64_t n = x_dims[0]; |
| 46 | int64_t c = x_dims[1]; |
| 47 | int64_t h = x_dims[2]; |
| 48 | int64_t w = x_dims[3]; |
| 49 | std::vector<int64_t> kernel_sizes(kernel_sizes_.begin(), kernel_sizes_.end()); |
| 50 | std::vector<int64_t> strides(strides_.begin(), strides_.end()); |
| 51 | std::vector<int64_t> paddings(paddings_.begin(), paddings_.end()); |
| 52 | std::vector<int64_t> dilations(dilations_.begin(), dilations_.end()); |
| 53 | |
| 54 | int64_t out_height = funcs::CalcOutputSize(x_dims[2], |
| 55 | kernel_sizes[0], |
| 56 | dilations[0], |
| 57 | paddings[0], |
| 58 | paddings[2], |
| 59 | strides[0]); |
| 60 | int64_t out_width = funcs::CalcOutputSize(x_dims[3], |
| 61 | kernel_sizes[1], |
| 62 | dilations[1], |
| 63 | paddings[1], |
| 64 | paddings[3], |
| 65 | strides[1]); |
| 66 | |
| 67 | xpu::ctx_guard RAII_GUARD(dev_ctx.x_context()); |
| 68 | XPUType* out_grad_trans = |
| 69 | RAII_GUARD.alloc_l3_or_gm<XPUType>(out_grad.numel()); |
| 70 | |
| 71 | int r = xpu::transpose( |
| 72 | dev_ctx.x_context(), |
| 73 | reinterpret_cast<const XPUType*>(out_grad.data<T>()), |
| 74 | out_grad_trans, |
| 75 | {n, c, kernel_sizes[0], kernel_sizes[1], out_height, out_width}, |
| 76 | {0, 4, 5, 1, 2, 3}); |
| 77 | PADDLE_ENFORCE_XDNN_SUCCESS(r, "transpose"); |
| 78 | |
| 79 | r = xpu::col2im(dev_ctx.x_context(), |
| 80 | out_grad_trans, |
| 81 | reinterpret_cast<XPUType*>(x_grad->data<T>()), |
nothing calls this directly
no test coverage detected