sequence_expand_as_compute.h 2.3 KB
Newer Older
L
lhl960107 已提交
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
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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 <string>
#include <vector>
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
#include "lite/core/types.h"
#include "lite/fluid/eigen.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace x86 {

using Tensor = lite::Tensor;

template <typename T>
struct SequenceExpandFunctor {
H
huzhiqiang 已提交
32 33 34 35
  void operator()(
      const Tensor &x,
      const std::vector<uint64_t> &ref_lod, /*expand referenced lod*/
      Tensor *out) {
L
lhl960107 已提交
36 37 38 39 40 41 42
    int64_t hight = x.dims()[0];
    int64_t width = x.data_size() / hight;

    const T *in_data = x.data<T>();
    T *out_data = out->mutable_data<T, T>();

    for (int h_id = 0; h_id < hight; ++h_id) {
H
huzhiqiang 已提交
43
      uint64_t span = ref_lod[h_id + 1] - ref_lod[h_id];
L
lhl960107 已提交
44 45
      if (span == 0) continue;
      const T *src = in_data + h_id * width;
H
huzhiqiang 已提交
46
      for (uint64_t w_id = 0; w_id < width; ++w_id) {
L
lhl960107 已提交
47 48
        T ele = src[w_id];
        size_t offset = ref_lod[h_id] * width;
H
huzhiqiang 已提交
49
        for (uint64_t k = 0; k < span; ++k) {
L
lhl960107 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
          out_data[offset + k * width + w_id] = ele;
        }
      }
    }
  }
};

template <typename T>
class SequenceExpandAsCompute
    : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
 public:
  void Run() override {
    auto &param = *param_.get_mutable<operators::SequenceExpandAsParam>();

    auto *x = param.x;
    auto *y = param.y;
    auto *out = param.out;

    auto &y_lod = y->lod();
69 70
    CHECK_EQ(y_lod.size(), 1u);
    CHECK_GT(y_lod[0].size(), 1u);
L
lhl960107 已提交
71

H
huzhiqiang 已提交
72
    out->template mutable_data<T, T>();
L
lhl960107 已提交
73 74 75 76 77 78 79 80 81 82

    SequenceExpandFunctor<T> seq_espand_functor;
    seq_espand_functor(*x, y_lod[0], out);
  }
};

}  // namespace x86
}  // namespace kernels
}  // namespace lite
}  // namespace paddle