|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/operators/math/sequence_padding.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | +namespace math { |
| 20 | + |
| 21 | +template <typename T> |
| 22 | +class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> { |
| 23 | + public: |
| 24 | + void operator()(const platform::CPUDeviceContext& context, |
| 25 | + const framework::LoDTensor& seq, framework::Tensor& padding, |
| 26 | + bool norm_by_times) { |
| 27 | + auto lod = seq.lod(); |
| 28 | + PADDLE_ENFORCE_GT(lod.size(), 0UL, |
| 29 | + "The LoD of LoDTensor seq should not be null."); |
| 30 | + |
| 31 | + const size_t level = 0; |
| 32 | + framework::LoD abs_offset_lod = framework::ToAbsOffset(lod); |
| 33 | + |
| 34 | + auto seq_dims = seq.dims(); |
| 35 | + PADDLE_ENFORCE_EQ(seq_dims[0], abs_offset_lod[level].back(), |
| 36 | + "The first dimension of LoDTensor seq should be " |
| 37 | + "equal to the sum of all sequences's length."); |
| 38 | + |
| 39 | + auto padding_dims = padding.dims(); |
| 40 | + PADDLE_ENFORCE_EQ(padding_dims.size(), 3UL, |
| 41 | + "The input padding should be a 3-D Tensor of shape " |
| 42 | + "[max_sequence_length, num_sequences, sequence_width]."); |
| 43 | + |
| 44 | + const size_t max_sequence_length = MaximumSequenceLength(lod, level); |
| 45 | + PADDLE_ENFORCE_EQ(padding_dims[0], max_sequence_length, |
| 46 | + "The first dimension of Tensor padding should be the " |
| 47 | + "maximum length of all sequences in LoDTensor seq."); |
| 48 | + |
| 49 | + const size_t num_sequences = abs_offset_lod[level].size() - 1; |
| 50 | + PADDLE_ENFORCE_EQ(padding_dims[1], num_sequences, |
| 51 | + "The second dimension of Tensor padding should be the " |
| 52 | + "number of sequences in LoDTensor seq."); |
| 53 | + |
| 54 | + const size_t sequence_width = seq.numel() / seq_dims[0]; |
| 55 | + PADDLE_ENFORCE_EQ(padding_dims[2], sequence_width, |
| 56 | + "The third dimension of Tensor padding should be the " |
| 57 | + "width of sequence in LoDTensor seq."); |
| 58 | + |
| 59 | + const T* seq_data = seq.data<T>(); |
| 60 | + T* padding_data = padding.data<T>(); |
| 61 | + for (size_t i = 0; i < max_sequence_length; ++i) { |
| 62 | + for (size_t j = 0; j < num_sequences; ++j) { |
| 63 | + size_t start_pos = abs_offset_lod[level][j]; |
| 64 | + size_t sequence_length = abs_offset_lod[level][j + 1] - start_pos; |
| 65 | + if (i < sequence_length) { |
| 66 | + // i > 0 => sequence_length > 0 |
| 67 | + T scale = |
| 68 | + norm_by_times ? (1.0f / static_cast<T>(sequence_length)) : 1.0f; |
| 69 | + for (size_t k = 0; k < sequence_width; ++k) { |
| 70 | + padding_data[(i * num_sequences + j) * sequence_width + k] = |
| 71 | + seq_data[(start_pos + i) * sequence_width + k] * scale; |
| 72 | + } |
| 73 | + } else { |
| 74 | + memset(padding_data + (i * num_sequences + j) * sequence_width, 0, |
| 75 | + sequence_width * sizeof(T)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +template <typename T> |
| 83 | +class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, T> { |
| 84 | + public: |
| 85 | + void operator()(const platform::CPUDeviceContext& context, |
| 86 | + framework::LoDTensor& seq, const framework::Tensor& padding, |
| 87 | + bool norm_by_times) { |
| 88 | + auto lod = seq.lod(); |
| 89 | + PADDLE_ENFORCE_GT(lod.size(), 0UL, |
| 90 | + "The LoD of LoDTensor seq should not be null."); |
| 91 | + |
| 92 | + const size_t level = 0; |
| 93 | + framework::LoD abs_offset_lod = framework::ToAbsOffset(lod); |
| 94 | + |
| 95 | + auto seq_dims = seq.dims(); |
| 96 | + PADDLE_ENFORCE_EQ(seq_dims[0], abs_offset_lod[level].back(), |
| 97 | + "The first dimension of LoDTensor seq should be " |
| 98 | + "equal to the sum of all sequences's length."); |
| 99 | + |
| 100 | + auto padding_dims = padding.dims(); |
| 101 | + PADDLE_ENFORCE_EQ(padding_dims.size(), 3UL, |
| 102 | + "The input padding should be a 3-D Tensor of shape " |
| 103 | + "[max_sequnece_length, num_sequences, sequence_width]."); |
| 104 | + |
| 105 | + const size_t max_sequence_length = MaximumSequenceLength(lod, level); |
| 106 | + PADDLE_ENFORCE_EQ(padding_dims[0], max_sequence_length, |
| 107 | + "The first dimension of Tensor padding should be " |
| 108 | + "the maximum length of all sequences in LoDTensor seq."); |
| 109 | + |
| 110 | + const size_t num_sequences = abs_offset_lod[level].size() - 1; |
| 111 | + PADDLE_ENFORCE_EQ(padding_dims[1], num_sequences, |
| 112 | + "The second dimension of Tensor padding should be " |
| 113 | + "the number of sequences in LoDTensor seq."); |
| 114 | + |
| 115 | + const size_t sequence_width = seq.numel() / seq_dims[0]; |
| 116 | + PADDLE_ENFORCE_EQ(padding_dims[2], sequence_width, |
| 117 | + "The third dimension of Tensor padding should be the " |
| 118 | + "width of sequence in LoDTensor seq."); |
| 119 | + |
| 120 | + const T* padding_data = padding.data<T>(); |
| 121 | + T* seq_data = seq.data<T>(); |
| 122 | + for (size_t i = 0; i < num_sequences; ++i) { |
| 123 | + size_t start_pos = abs_offset_lod[level][i]; |
| 124 | + size_t sequence_length = abs_offset_lod[level][i + 1] - start_pos; |
| 125 | + for (size_t j = 0; j < sequence_length; ++j) { |
| 126 | + // sequence_width > j > 0 |
| 127 | + T scale = |
| 128 | + norm_by_times ? (1.0f / static_cast<T>(sequence_length)) : 1.0f; |
| 129 | + for (size_t k = 0; k < sequence_width; ++k) { |
| 130 | + seq_data[(start_pos + j) * sequence_width + k] = |
| 131 | + padding_data[(j * num_sequences + i) * sequence_width + k] * |
| 132 | + scale; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +template class PaddingLoDTensorFunctor<platform::CPUDeviceContext, float>; |
| 140 | +template class UnpaddingLoDTensorFunctor<platform::CPUDeviceContext, float>; |
| 141 | + |
| 142 | +} // namespace math |
| 143 | +} // namespace operators |
| 144 | +} // namespace paddle |
0 commit comments