sequence_padding.cc 4.8 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 {

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
template <typename T, PaddingLayout padding_layout>
void CopyDataCPU(framework::LoDTensor* seq_tensor,
                 framework::Tensor* padding_tensor,
                 const framework::Vector<size_t>& abs_offset,
                 const int64_t& max_seq_len, const int64_t& seq_width,
                 bool seq_to_padding, bool norm_by_len) {
  T* seq_data = seq_tensor->data<T>();
  T* padding_data = padding_tensor->data<T>();

  int64_t seq_num = abs_offset.size() - 1;

  for (int64_t i = 0; i < seq_num; ++i) {
    int64_t seq_start = abs_offset[i];
    int64_t seq_len = abs_offset[i + 1] - seq_start;

    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) {
        size_t padding_offset = 0;
        if (padding_layout == BATCH_LENGTH_WIDTH) {
          padding_offset = (i * max_seq_len * seq_width) + j * seq_width + k;
        } else {
          padding_offset = (j * seq_num * seq_width) + i * seq_width + k;
        }
        if (seq_to_padding) {
          padding_data[padding_offset] =
              seq_data[(seq_start + j) * seq_width + k] * scale;
Y
Yiqun Liu 已提交
49
        } else {
50 51
          seq_data[(seq_start + j) * seq_width + k] =
              padding_data[padding_offset] * scale;
Y
Yiqun Liu 已提交
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 80 81 82 83 84 85 86 87 88 89
}

template <typename T, PaddingLayout padding_layout>
class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T, padding_layout> {
 public:
  void operator()(const platform::CPUDeviceContext& context,
                  const framework::LoDTensor& seq_tensor,
                  framework::Tensor* padding_tensor,
                  T padding_value = static_cast<T>(0),
                  bool norm_by_times = false, size_t lod_level = 0) {
    ValidateLoD(seq_tensor, lod_level);

    auto& lod = seq_tensor.lod();
    auto& abs_offset = framework::ToAbsOffset(lod)[lod_level];

    auto seq_dims = seq_tensor.dims();
    auto padding_dims = padding_tensor->dims();
    int64_t max_seq_len = MaximumSequenceLength(lod, lod_level);
    int64_t seq_num = abs_offset.size() - 1;
    int64_t seq_width = seq_tensor.numel() / seq_dims[0];
    int64_t numel = max_seq_len * seq_num * seq_width;

    ValidateShape(seq_dims, abs_offset.back(), padding_dims, max_seq_len,
                  seq_num, seq_width, padding_layout);

    T* padding_data = padding_tensor->data<T>();

    memset(padding_data, padding_value, numel * sizeof(T));

    CopyDataCPU<T, padding_layout>(
        const_cast<framework::LoDTensor*>(&seq_tensor), padding_tensor,
        abs_offset, max_seq_len, seq_width, true /* seq_to_padding */,
        norm_by_times);
  }
Y
Yiqun Liu 已提交
90 91
};

92 93
template <typename T, PaddingLayout padding_layout>
class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, T, padding_layout> {
Y
Yiqun Liu 已提交
94 95
 public:
  void operator()(const platform::CPUDeviceContext& context,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                  framework::LoDTensor* seq_tensor,
                  const framework::Tensor& padding_tensor,
                  bool norm_by_times = false, size_t lod_level = 0) {
    ValidateLoD(*seq_tensor, lod_level);

    auto& lod = seq_tensor->lod();
    auto& abs_offset = framework::ToAbsOffset(lod)[lod_level];

    auto& seq_dims = seq_tensor->dims();
    auto& padding_dims = padding_tensor.dims();
    int64_t max_seq_len = MaximumSequenceLength(lod, lod_level);
    int64_t seq_num = abs_offset.size() - 1;
    int64_t seq_width = seq_tensor->numel() / seq_dims[0];

    ValidateShape(seq_dims, abs_offset.back(), padding_dims, max_seq_len,
                  seq_num, seq_width, padding_layout);

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

    CopyDataCPU<T, padding_layout>(
        seq_tensor, const_cast<framework::Tensor*>(&padding_tensor), abs_offset,
        max_seq_len, seq_width, false /* seq_to_padding */, norm_by_times);
Y
Yiqun Liu 已提交
119 120 121
  }
};

122 123 124 125
template class PaddingLoDTensorFunctor<platform::CPUDeviceContext, float,
                                       LENGTH_BATCH_WIDTH>;
template class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, float,
                                         LENGTH_BATCH_WIDTH>;
Y
Yiqun Liu 已提交
126 127 128 129

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