|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + You may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include "paddle/framework/data_layout.h" |
| 18 | +#include "paddle/framework/eigen.h" |
| 19 | +#include "paddle/framework/op_registry.h" |
| 20 | +#include "paddle/operators/math/im2col.h" |
| 21 | +#include "paddle/operators/math/math_function.h" |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace operators { |
| 25 | + |
| 26 | +using Tensor = framework::Tensor; |
| 27 | +using LoDTensor = framework::LoDTensor; |
| 28 | + |
| 29 | +inline int OutputSize(int input_size, int filter_size, int padding_0, |
| 30 | + int padding_1, int stride) { |
| 31 | + const int output_size = |
| 32 | + (input_size + padding_0 + padding_1 - filter_size) / stride + 1; |
| 33 | + return output_size; |
| 34 | +} |
| 35 | + |
| 36 | +template <typename DeviceContext, typename T> |
| 37 | +class Im2SequenceKernel : public framework::OpKernel<T> { |
| 38 | + public: |
| 39 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 40 | + const Tensor* in = ctx.Input<Tensor>("X"); |
| 41 | + LoDTensor* out = ctx.Output<LoDTensor>("Out"); |
| 42 | + out->mutable_data<T>(ctx.GetPlace()); |
| 43 | + // TODO(wanghaoshuang): Add layout checker after 'set_layout' |
| 44 | + // being available for python API |
| 45 | + // PADDLE_ENFORCE_EQ(in->layout(), framework::DataLayout::kNCHW, |
| 46 | + // "Input(X) layout must be NCHW"); |
| 47 | + auto in_dim = in->dims(); |
| 48 | + int batch_size = in_dim[0]; |
| 49 | + int img_channels = in_dim[1]; |
| 50 | + int img_height = in_dim[2]; |
| 51 | + int img_width = in_dim[3]; |
| 52 | + |
| 53 | + auto kernels = ctx.Attr<std::vector<int>>("kernels"); |
| 54 | + auto strides = ctx.Attr<std::vector<int>>("strides"); |
| 55 | + auto paddings = ctx.Attr<std::vector<int>>("paddings"); |
| 56 | + int output_height = OutputSize(img_height, kernels[0], paddings[0], |
| 57 | + paddings[2], strides[0]); |
| 58 | + int output_width = |
| 59 | + OutputSize(img_width, kernels[1], paddings[1], paddings[3], strides[1]); |
| 60 | + |
| 61 | + const std::vector<int> dilations({1, 1}); |
| 62 | + |
| 63 | + auto out_dims = out->dims(); |
| 64 | + out->Resize({batch_size, out->numel() / batch_size}); |
| 65 | + for (int i = 0; i < batch_size; i++) { |
| 66 | + const Tensor src = |
| 67 | + in->Slice(i, i + 1).Resize({img_channels, img_height, img_width}); |
| 68 | + Tensor dst = out->Slice(i, i + 1).Resize( |
| 69 | + {output_height, output_width, img_channels, kernels[0], kernels[1]}); |
| 70 | + |
| 71 | + math::Im2ColFunctor<math::ColFormat::kOCF, DeviceContext, T> f; |
| 72 | + auto& dev_ctx = ctx.template device_context<DeviceContext>(); |
| 73 | + f(dev_ctx, src, dilations, strides, paddings, &dst); |
| 74 | + } |
| 75 | + out->Resize(out_dims); |
| 76 | + |
| 77 | + // set lod information |
| 78 | + // TODO(wanghaoshuang): Move this to InferShape |
| 79 | + framework::LoD lod(1); |
| 80 | + lod[0].reserve(batch_size + 1); |
| 81 | + for (int i = 0, offset = 0; i < batch_size + 1; ++i) { |
| 82 | + lod[0][i] = offset; |
| 83 | + offset += output_height * output_width; |
| 84 | + } |
| 85 | + out->set_lod(lod); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +template <typename DeviceContext, typename T> |
| 90 | +class Im2SequenceGradKernel : public framework::OpKernel<T> { |
| 91 | + public: |
| 92 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 93 | + auto* in = ctx.Input<Tensor>("X"); |
| 94 | + Tensor* d_out = |
| 95 | + const_cast<Tensor*>(ctx.Input<Tensor>(framework::GradVarName("Out"))); |
| 96 | + auto* d_x = ctx.Output<Tensor>(framework::GradVarName("X")); |
| 97 | + d_x->mutable_data<T>(ctx.GetPlace()); |
| 98 | + |
| 99 | + auto x_v = framework::EigenVector<T>::Flatten(*d_x); |
| 100 | + auto& place = *ctx.template device_context<DeviceContext>().eigen_device(); |
| 101 | + x_v.device(place) = x_v.constant(0.0); |
| 102 | + |
| 103 | + auto in_dim = in->dims(); |
| 104 | + int batch_size = in_dim[0]; |
| 105 | + int img_channels = in_dim[1]; |
| 106 | + int img_height = in_dim[2]; |
| 107 | + int img_width = in_dim[3]; |
| 108 | + |
| 109 | + auto kernels = ctx.Attr<std::vector<int>>("kernels"); |
| 110 | + auto strides = ctx.Attr<std::vector<int>>("strides"); |
| 111 | + auto paddings = ctx.Attr<std::vector<int>>("paddings"); |
| 112 | + int output_height = OutputSize(img_height, kernels[0], paddings[0], |
| 113 | + paddings[2], strides[0]); |
| 114 | + int output_width = |
| 115 | + OutputSize(img_width, kernels[1], paddings[1], paddings[3], strides[1]); |
| 116 | + |
| 117 | + const std::vector<int> dilations({1, 1}); |
| 118 | + |
| 119 | + auto d_out_dims = d_out->dims(); |
| 120 | + d_out->Resize({batch_size, d_out->numel() / batch_size}); |
| 121 | + for (int i = 0; i < batch_size; i++) { |
| 122 | + Tensor dst = |
| 123 | + d_x->Slice(i, i + 1).Resize({img_channels, img_height, img_width}); |
| 124 | + const Tensor src = d_out->Slice(i, i + 1).Resize( |
| 125 | + {output_height, output_width, img_channels, kernels[0], kernels[1]}); |
| 126 | + math::Col2ImFunctor<math::ColFormat::kOCF, DeviceContext, T> f; |
| 127 | + auto& dev_ctx = ctx.template device_context<DeviceContext>(); |
| 128 | + f(dev_ctx, src, dilations, strides, paddings, &dst); |
| 129 | + } |
| 130 | + d_out->Resize(d_out_dims); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +} // namespace operators |
| 135 | +} // namespace paddle |
0 commit comments