You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Paddle/paddle/fluid/operators/space_to_depth_op.h

128 lines
4.1 KiB

6 years ago
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
6 years ago
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
6 years ago
#ifndef PADDLE_FLUID_OPERATORS_SPACE_TO_DEPTH_OP_H_
#define PADDLE_FLUID_OPERATORS_SPACE_TO_DEPTH_OP_H_
#endif // PADDLE_FLUID_OPERATORS_SPACE_TO_DEPTH_OP_H_
6 years ago
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/for_range.h"
namespace paddle {
namespace operators {
template <typename T>
6 years ago
class space_to_depth_compute {
6 years ago
public:
6 years ago
HOSTDEVICE space_to_depth_compute(const T *x, int64_t w, int64_t h, int64_t c,
6 years ago
int64_t batch, int64_t blocksize,
6 years ago
int64_t forward, T *out)
6 years ago
: x_(x),
w_(w),
h_(h),
c_(c),
batch_(batch),
6 years ago
blocksize_(blocksize),
6 years ago
forward_(forward),
out_(out) {}
HOSTDEVICE void operator()(int64_t in_index) {
6 years ago
int64_t out_c = c_ / (blocksize_ * blocksize_);
6 years ago
// calculate each dim position with index of tensor
int64_t b = in_index / (c_ * h_ * w_);
int64_t k = (in_index % (c_ * h_ * w_)) / (h_ * w_);
int64_t j = ((in_index % (c_ * h_ * w_)) % (h_ * w_)) / w_;
int64_t i = ((in_index % (c_ * h_ * w_)) % (h_ * w_)) % w_;
int64_t c2 = k % out_c;
int64_t offset = k / out_c;
6 years ago
int64_t w2 = i * blocksize_ + offset % blocksize_;
int64_t h2 = j * blocksize_ + offset / blocksize_;
6 years ago
int64_t out_index =
6 years ago
w2 + w_ * blocksize_ * (h2 + h_ * blocksize_ * (c2 + out_c * b));
6 years ago
if (forward_)
out_[out_index] = x_[in_index];
else
out_[in_index] = x_[out_index];
}
private:
const T *x_;
6 years ago
int64_t w_, h_, c_, batch_, blocksize_, forward_;
6 years ago
T *out_;
};
template <typename DeviceContext, typename T>
6 years ago
class SpaceToDepthKernel : public framework::OpKernel<T> {
6 years ago
public:
void Compute(const framework::ExecutionContext &context) const override {
auto *out = context.Output<framework::LoDTensor>("Out");
auto *x = context.Input<framework::LoDTensor>("X");
6 years ago
auto blocksize = context.Attr<int64_t>("blocksize");
6 years ago
auto in_dims = x->dims();
out->mutable_data(context.GetPlace(), x->type());
auto out_dims = out->dims();
auto B = in_dims[0];
auto C = in_dims[1];
auto H = in_dims[2];
auto W = in_dims[3];
platform::ForRange<DeviceContext> for_range(
context.template device_context<DeviceContext>(),
static_cast<size_t>(x->numel()));
auto *x_data = x->data<T>();
auto *out_data = out->data<T>();
6 years ago
paddle::operators::space_to_depth_compute<T> computer(
x_data, W, H, C, B, blocksize, 1, out_data);
6 years ago
for_range(computer);
6 years ago
out->Resize(out_dims);
}
};
template <typename DeviceContext, typename T>
6 years ago
class SpaceToDepthGradKernel : public framework::OpKernel<T> {
6 years ago
public:
void Compute(const framework::ExecutionContext &context) const override {
auto *d_out =
context.Input<framework::LoDTensor>(framework::GradVarName("Out"));
auto *d_x =
context.Output<framework::LoDTensor>(framework::GradVarName("X"));
6 years ago
auto blocksize = context.Attr<int64_t>("blocksize");
6 years ago
auto in_dims = d_x->dims();
d_x->mutable_data(context.GetPlace(), d_out->type());
auto B = in_dims[0];
auto C = in_dims[1];
auto H = in_dims[2];
auto W = in_dims[3];
platform::ForRange<DeviceContext> for_range(
context.template device_context<DeviceContext>(),
static_cast<size_t>(d_x->numel()));
auto *dx_data = d_x->data<T>();
auto *dout_data = d_out->data<T>();
6 years ago
paddle::operators::space_to_depth_compute<T> computer(
dout_data, W, H, C, B, blocksize, 0, dx_data);
6 years ago
for_range(computer);
6 years ago
d_x->Resize(in_dims);
}
};
} // namespace operators
} // namespace paddle