sequence_unpad_op.h 3.6 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 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 <vector>
18

Y
Yibing Liu 已提交
19 20 21
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/operators/math/sequence_padding.h"
22
#include "paddle/phi/kernels/funcs/math_function.h"
Y
Yibing Liu 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;
using LoD = framework::LoD;

template <typename DeviceContext, typename T>
class SequenceUnpadOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x_t = ctx.Input<LoDTensor>("X");
    auto* len_t = ctx.Input<LoDTensor>("Length");
    auto* out_t = ctx.Output<LoDTensor>("Out");

38 39 40
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
    framework::Tensor seq_len_cpu =
        ctx.AllocateTmpTensor<T, DeviceContext>(len_t->dims(), dev_ctx);
Y
Yibing Liu 已提交
41
    if (platform::is_gpu_place(ctx.GetPlace())) {
42 43
      seq_len_cpu.mutable_data<int64_t>(platform::CPUPlace());
      framework::TensorCopySync(*len_t, platform::CPUPlace(), &seq_len_cpu);
Y
Yibing Liu 已提交
44
    } else {
45
      seq_len_cpu = *len_t;
Y
Yibing Liu 已提交
46 47
    }

48 49
    const int64_t* seq_len_ptr = seq_len_cpu.data<int64_t>();
    int64_t batch_size = len_t->dims()[0];
Y
Yibing Liu 已提交
50
    std::vector<size_t> out_lod0(batch_size + 1, 0);
51 52
    for (int64_t i = 0; i < batch_size; ++i) {
      out_lod0[i + 1] = out_lod0[i] + static_cast<size_t>(seq_len_ptr[i]);
Y
Yibing Liu 已提交
53 54 55 56 57 58 59 60 61
    }

    framework::LoD out_lod;
    out_lod.push_back(out_lod0);
    out_t->set_lod(out_lod);
    std::vector<int64_t> out_dims_vec{static_cast<int64_t>(out_lod0.back())};
    if (x_t->dims().size() == 2) {
      out_dims_vec.push_back(1);
    } else {
T
Tao Luo 已提交
62
      for (int i = 2; i < x_t->dims().size(); ++i) {
Y
Yibing Liu 已提交
63 64 65
        out_dims_vec.push_back(x_t->dims()[i]);
      }
    }
66
    out_t->Resize(phi::make_ddim(out_dims_vec));
Y
Yibing Liu 已提交
67

W
wawltor 已提交
68 69 70
    // after set the lod of output, allocate the memory
    out_t->mutable_data<T>(ctx.GetPlace());

Y
Yibing Liu 已提交
71 72
    int64_t padded_length = x_t->dims()[1];
    math::UnpaddingLoDTensorFunctor<DeviceContext, T>()(
H
Hui Zhang 已提交
73
        dev_ctx, *x_t, out_t, padded_length, 0, false, math::kBatchLengthWidth);
Y
Yibing Liu 已提交
74 75 76 77 78 79 80 81 82 83 84 85
  }
};

template <typename DeviceContext, typename T>
class SequenceUnpadGradOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* d_x = ctx.Output<LoDTensor>(framework::GradVarName("X"));
    if (d_x) {
      const auto* d_out = ctx.Input<LoDTensor>(framework::GradVarName("Out"));
      d_x->mutable_data<T>(ctx.GetPlace());

86
      int padded_length = d_x->dims()[1];
Y
Yibing Liu 已提交
87 88 89 90

      LoDTensor zero_pads;
      zero_pads.Resize({1, 1});
      zero_pads.mutable_data<T>(ctx.GetPlace());
91
      phi::funcs::SetConstant<DeviceContext, T> set_zero;
Y
Yibing Liu 已提交
92 93 94 95 96
      auto& dev_ctx = ctx.template device_context<DeviceContext>();
      set_zero(dev_ctx, &zero_pads, static_cast<T>(0));

      math::PaddingLoDTensorFunctor<DeviceContext, T>()(
          ctx.template device_context<DeviceContext>(), *d_out, d_x, zero_pads,
H
Hui Zhang 已提交
97
          padded_length, 0, false, math::kBatchLengthWidth);
Y
Yibing Liu 已提交
98 99 100 101 102 103
    }
  }
};

}  // namespace operators
}  // namespace paddle