reshape_op.h 3.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yibing Liu 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yibing Liu 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yibing Liu 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yibing Liu 已提交
14 15 16

#pragma once

Y
Yi Wang 已提交
17 18
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
Y
Yibing Liu 已提交
19 20 21 22

namespace paddle {
namespace operators {

Q
QI JUN 已提交
23
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
24
class ReshapeKernel : public framework::OpKernel<T> {
Y
Yibing Liu 已提交
25 26
 public:
  void Compute(const framework::ExecutionContext& ctx) const {
Y
Yibing Liu 已提交
27 28
    auto* out = ctx.Output<framework::Tensor>("Out");
    auto* in = ctx.Input<framework::Tensor>("X");
Y
ying 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    auto* shape = ctx.Input<framework::Tensor>("Shape");
    framework::DDim out_dims;
    if (shape) {
      std::vector<int64_t> output_shape;
      ValidateShape(*shape, framework::product(in->dims()), output_shape);

      for (auto d : output_shape) std::cout << d << " ";
      std::cout << std::endl;

      out_dims = framework::make_ddim(output_shape);
    } else {
      out_dims = out->dims();
    }

Y
Yibing Liu 已提交
44
    out->mutable_data<T>(ctx.GetPlace());
Y
Yi Wang 已提交
45
    framework::TensorCopy(*in, ctx.GetPlace(), ctx.device_context(), out);
Y
Yibing Liu 已提交
46 47
    out->Resize(out_dims);
  }
Y
ying 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

 private:
  void ValidateShape(const framework::Tensor& shape, const int64_t in_size,
                     std::vector<int64_t>& output_shape) const {
    std::vector<size_t> neg_dims_idx;
    const int unknown_index = -1;  // only one dimension canbe set to -1, whose
                                   // size will be automatically infered.

    const int64_t dimension = shape.dims()[1];
    std::cout << "dimension =" << dimension << std::endl;
    const T* shape_data = shape.data<T>();

    for (int64_t i = 0; i < dimension; ++i) {
      PADDLE_ENFORCE(shape_data[i] > 1 || shape_data[i] == unknown_index,
                     "Each input dimension of Attr(shape) must be positive, or "
                     "only one input dimension can be -1.");
      if (shape_data[i] == unknown_index) neg_dims_idx.push_back(i);
    }
    PADDLE_ENFORCE_LE(
        neg_dims_idx.size(), 1,
        "Only one input dimension of Attr(shape) can be unknown.");

    int64_t capacity = 1;
    output_shape.resize(dimension, 0);
    for (int64_t i = 0; i < dimension; ++i) {
      capacity *= shape_data[i];
      output_shape[i] = static_cast<int64_t>(shape_data[i]);
    }

    if (neg_dims_idx.size())
      output_shape[neg_dims_idx[0]] = in_size / (-capacity);
  }
Y
Yibing Liu 已提交
80 81
};

Q
QI JUN 已提交
82
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
83
class ReshapeGradKernel : public framework::OpKernel<T> {
Y
Yibing Liu 已提交
84 85
 public:
  void Compute(const framework::ExecutionContext& ctx) const {
Y
Yibing Liu 已提交
86 87
    auto* d_out = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto* d_x = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
Y
Yibing Liu 已提交
88 89 90
    d_x->mutable_data<T>(ctx.GetPlace());

    auto in_dims = d_x->dims();
Y
Yi Wang 已提交
91
    framework::TensorCopy(*d_out, ctx.GetPlace(), ctx.device_context(), d_x);
Y
Yibing Liu 已提交
92 93 94
    d_x->Resize(in_dims);
  }
};
H
Helin Wang 已提交
95 96
}  // namespace operators
}  // namespace paddle