seq_expand_op.h 2.7 KB
Newer Older
W
wanghaoshuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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. */

#pragma once

#include "hl_cuda.h"
#include "paddle/framework/op_registry.h"

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;

template <typename Place, typename T>
class SeqExpandKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
    const T* x_data = x->data<T>();
    T* out_data = out->mutable_data<T>(context.GetPlace());
    size_t repeat = static_cast<size_t>(context.Attr<int>("repeat"));

    if (repeat != 0) {
      if (x->lod().size() == 0) {
W
wanghaoshuang 已提交
37
        std::vector<size_t> level0;
W
wanghaoshuang 已提交
38 39 40
        for (size_t i = 0; i <= x->dims()[0]; i++) {
          level0.push_back(i * repeat);
        }
W
wanghaoshuang 已提交
41
        framework::LoD out_lod;
W
wanghaoshuang 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        out_lod.push_back(level0);
        out->set_lod(out_lod);
      }
    }
    auto out_dim = out->dims();
    size_t element_len = framework::product(out_dim) / out_dim[0];
    std::vector<int> cpy_map(out_dim[0]);
    if (x->lod().size() == 0) {
      auto lod = out->lod();
      for (int i = 0; i < lod.size() - 1; ++i) {
        for (int j = lod[0][i]; i < lod[0][i + 1]; ++j) {
          cpy_map[j] = i;
        }
      }
    }
W
wanghaoshuang 已提交
57
    if (platform::is_cpu_place(context.GetPlace())) {
W
wanghaoshuang 已提交
58 59 60 61 62 63
      for (int i = 0; i < out_dim[0]; ++i) {
        memcpy(out_data + element_len * i, x_data + element_len * cpy_map[i],
               sizeof(T) * element_len);
      }
    } else {
      for (int i = 0; i < out_dim[0]; ++i) {
W
wanghaoshuang 已提交
64 65
        hl_memcpy(out_data + element_len * i,
                  const_cast<T*>(x_data) + element_len * cpy_map[i],
W
wanghaoshuang 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
                  sizeof(T) * element_len);
      }
    }
  }
};

template <typename Place, typename T>
class SeqExpandGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    // auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
    // auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
    // d_x->mutable_data<T>(context.GetPlace());
  }
};

}  // namespace operators
}  // namespace paddle