sequence_padding.cc 5.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yiqun Liu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/math/sequence_padding.h"
Y
Yiqun Liu 已提交
16 17 18 19 20

namespace paddle {
namespace operators {
namespace math {

Y
yangyaming 已提交
21
template <typename T>
22
void CopyDataCPU(framework::LoDTensor* seq_tensor,
Y
yangyaming 已提交
23 24
                 framework::Tensor* pad_tensor,
                 const framework::Vector<size_t>& seq_offset,
25
                 const int64_t& max_seq_len, const int64_t& seq_width,
Y
yangyaming 已提交
26 27
                 bool seq_to_pad, bool norm_by_len,
                 OutputLayout output_layout) {
28
  T* seq_data = seq_tensor->data<T>();
Y
yangyaming 已提交
29
  T* pad_data = pad_tensor->data<T>();
30

Y
yangyaming 已提交
31
  int64_t seq_num = seq_offset.size() - 1;
32 33

  for (int64_t i = 0; i < seq_num; ++i) {
Y
yangyaming 已提交
34 35
    int64_t seq_start = seq_offset[i];
    int64_t seq_len = seq_offset[i + 1] - seq_start;
36 37 38
    T scale = norm_by_len ? (1.0f / static_cast<T>(seq_len)) : 1.0f;
    for (int64_t j = 0; j < seq_len; ++j) {
      for (int64_t k = 0; k < seq_width; ++k) {
Y
yangyaming 已提交
39 40 41 42
        size_t pad_data_idx = 0;
        size_t seq_data_idx = (seq_start + j) * seq_width + k;
        if (output_layout == kBatchLengthWidth) {
          pad_data_idx = (i * max_seq_len + j) * seq_width + k;
43
        } else {
Y
yangyaming 已提交
44
          pad_data_idx = (j * seq_num + i) * seq_width + k;
45
        }
Y
yangyaming 已提交
46 47
        if (seq_to_pad) {
          pad_data[pad_data_idx] = seq_data[seq_data_idx] * scale;
Y
Yiqun Liu 已提交
48
        } else {
Y
yangyaming 已提交
49
          seq_data[seq_data_idx] = pad_data[pad_data_idx] * scale;
Y
Yiqun Liu 已提交
50 51 52 53
        }
      }
    }
  }
54 55
}

Y
yangyaming 已提交
56 57
template <typename T>
class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
58 59 60
 public:
  void operator()(const platform::CPUDeviceContext& context,
                  const framework::LoDTensor& seq_tensor,
Y
yangyaming 已提交
61 62 63 64 65
                  framework::Tensor* pad_tensor,
                  T pad_value = static_cast<T>(0), bool norm_by_times = false,
                  size_t lod_level = 0,
                  OutputLayout output_layout = kBatchLengthWidth) {
    CheckLoD(seq_tensor, lod_level);
66 67

    auto& lod = seq_tensor.lod();
Y
yangyaming 已提交
68
    auto& seq_offset = framework::ToAbsOffset(lod)[lod_level];
69

Y
yangyaming 已提交
70 71 72 73 74
    auto seq_tensor_dims = seq_tensor.dims();
    auto pad_tensor_dims = pad_tensor->dims();
    int64_t max_seq_len = MaximumSequenceLength(seq_offset);
    int64_t seq_num = seq_offset.size() - 1;
    int64_t seq_width = seq_tensor.numel() / seq_tensor_dims[0];
75

Y
yangyaming 已提交
76 77
    CheckDims(seq_tensor_dims, seq_offset.back(), pad_tensor_dims, max_seq_len,
              seq_num, seq_width, output_layout);
78

Y
yangyaming 已提交
79
    T* pad_data = pad_tensor->data<T>();
80

Y
yangyaming 已提交
81
    memset(pad_data, pad_value, max_seq_len * seq_num * seq_width * sizeof(T));
82

Y
yangyaming 已提交
83 84 85
    CopyDataCPU<T>(const_cast<framework::LoDTensor*>(&seq_tensor), pad_tensor,
                   seq_offset, max_seq_len, seq_width, true /* seq_to_pad */,
                   norm_by_times, output_layout);
86
  }
Y
Yiqun Liu 已提交
87 88
};

Y
yangyaming 已提交
89 90
template <typename T>
class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
Y
Yiqun Liu 已提交
91 92
 public:
  void operator()(const platform::CPUDeviceContext& context,
93
                  framework::LoDTensor* seq_tensor,
Y
yangyaming 已提交
94 95 96 97
                  const framework::Tensor& pad_tensor,
                  bool norm_by_times = false, size_t lod_level = 0,
                  OutputLayout output_layout = kBatchLengthWidth) {
    CheckLoD(*seq_tensor, lod_level);
98 99

    auto& lod = seq_tensor->lod();
Y
yangyaming 已提交
100
    auto& seq_offset = framework::ToAbsOffset(lod)[lod_level];
101

Y
yangyaming 已提交
102 103 104 105 106
    auto& seq_tensor_dims = seq_tensor->dims();
    auto& pad_tensor_dims = pad_tensor.dims();
    int64_t max_seq_len = MaximumSequenceLength(seq_offset);
    int64_t seq_num = seq_offset.size() - 1;
    int64_t seq_width = seq_tensor->numel() / seq_tensor_dims[0];
107

Y
yangyaming 已提交
108 109
    CheckDims(seq_tensor_dims, seq_offset.back(), pad_tensor_dims, max_seq_len,
              seq_num, seq_width, output_layout);
110 111 112 113

    T* seq_data = seq_tensor->data<T>();
    memset(seq_data, static_cast<T>(0), seq_tensor->numel() * sizeof(T));

Y
yangyaming 已提交
114 115 116
    CopyDataCPU<T>(seq_tensor, const_cast<framework::Tensor*>(&pad_tensor),
                   seq_offset, max_seq_len, seq_width, false /* seq_to_pad */,
                   norm_by_times, output_layout);
Y
Yiqun Liu 已提交
117 118 119
  }
};

Y
yangyaming 已提交
120 121 122 123 124 125 126 127 128
template class PaddingLoDTensorFunctor<platform::CPUDeviceContext, int>;
template class PaddingLoDTensorFunctor<platform::CPUDeviceContext, int64_t>;
template class PaddingLoDTensorFunctor<platform::CPUDeviceContext, float>;
template class PaddingLoDTensorFunctor<platform::CPUDeviceContext, double>;

template class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, int>;
template class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, int64_t>;
template class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, float>;
template class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, double>;
Y
Yiqun Liu 已提交
129 130 131 132

}  // namespace math
}  // namespace operators
}  // namespace paddle