/
padding.py
213 lines (158 loc) · 6.77 KB
/
padding.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import annotations
from typing import Any, Optional, Sequence, Tuple, cast, final
import torch
from torch import Tensor
from fairseq2.data import Collater, SequenceData
from fairseq2.typing import Device
@final
class PaddingMask:
"""Represents a sequence padding mask."""
_seq_lens: Tensor
_batch_seq_len: int
_materialized: Optional[Tensor]
_materialized_float: Optional[Tensor]
def __init__(self, seq_lens: Tensor, batch_seq_len: int) -> None:
"""
:param seq_lens:
An array where each element represents the length of a sequence.
*Shape:* :math:`(N)`, where :math:`N` is the batch size.
:param batch_seq_len:
The sequence length of the mask.
"""
self._seq_lens = seq_lens
self._batch_seq_len = batch_seq_len
self._materialized = None
self._materialized_float = None
def materialize(self) -> Tensor:
"""Materialize the padding mask as a boolean tensor."""
if self._materialized is None:
self._materialized = to_padding_mask(self._seq_lens, self._batch_seq_len)
return self._materialized
def materialize_as(self, seqs: Tensor) -> Tensor:
"""Materialize the padding mask as a float tensor."""
if self._materialized_float is None:
bool_mask = self.materialize()
# (N, S)
mask = torch.zeros_like(bool_mask, dtype=seqs.dtype)
mask = torch.where(bool_mask, mask, -torch.inf)
self._materialized_float = mask
return self._materialized_float
def trim(self, size: int) -> PaddingMask:
"""Return a new trimmed padding mask.
:param size:
The amount by which to trim the sequences.
"""
return PaddingMask(self._seq_lens - size, self._batch_seq_len - size)
def to(self, device: Device) -> PaddingMask:
"""Perform device conversion.
:param device:
The target device.
"""
if self._seq_lens.device == device:
return self
return PaddingMask(self._seq_lens.to(device), self._batch_seq_len)
@property
def seq_lens(self) -> Tensor:
"""An array where each element represents the length of a sequence.
*Shape:* :math:`(N)`, where :math:`N` is the batch size."""
return self._seq_lens
def to_padding_mask(seq_lens: Tensor, batch_seq_len: int) -> Tensor:
"""Convert a sequence length array to a boolean padding mask tensor.
:param seq_lens:
An array where each element represents the length of a sequence. *Shape:*
:math:`(N)`, where :math:`N` is the batch size.
:param batch_seq_len:
The sequence length of the mask.
:returns:
The mask. *Shape:* :math:`(N,S)`, where :math:`N` is the batch size and
:math:`S` is the sequence length.
"""
batch_size = seq_lens.size(0)
# (N, S)
indices = torch.arange(batch_seq_len, device=seq_lens.device).expand(batch_size, -1)
# (N) -> (N, S)
lengths = seq_lens.unsqueeze(1).expand(-1, batch_seq_len)
return indices < lengths
def get_seq_lens(seqs: Tensor, padding_mask: Optional[PaddingMask]) -> Tensor:
"""Retrieve the sequence lengths of ``seqs``.
:param seqs:
The sequences. *Shape:* :math:`(N,S,*)`, where :math:`N` is the batch
size, :math:`S` is the sequence length, and :math:`*` is any number of
sequence-specific dimensions including none.
:param padding_mask:
The padding mask. *Shape:* :math:`(N,S)`, where :math:`N` is the batch
size and :math:`S` is the sequence length.
:returns:
An array where each element represents the length of the corresponding
sequence in ``seqs``. *Shape:* :math:`(N)`, where :math:`N` is the batch
size.
"""
if padding_mask is not None:
return padding_mask.seq_lens
return torch.full((seqs.size(0),), seqs.size(1), device=seqs.device)
def apply_padding_mask(
seqs: Tensor, padding_mask: Optional[PaddingMask], pad_value: Any = 0
) -> Tensor:
"""Apply the specified padding mask to ``seqs``.
:param seqs:
The sequences to mask. *Shape:* :math:`(N,S,*)`, where :math:`N` is the
batch size, :math:`S` is the sequence length, and :math:`*` is any
number of sequence-specific dimensions including none.
:param padding_mask:
The padding mask to apply. *Shape:* :math:`(N,S)`, where :math:`N` is
the batch size and :math:`S` is the sequence length.
:param pad_value:
The value for padded positions.
:returns:
The input sequences with mask applied. *Shape:* Same as ``seqs``.
"""
if padding_mask is None:
return seqs
m = padding_mask.materialize()
for _ in range(seqs.ndim - m.ndim):
m = m.unsqueeze(-1)
return seqs.where(m, pad_value)
def get_seqs_and_padding_mask(
data: SequenceData, device: Optional[Device] = None
) -> Tuple[Tensor, Optional[PaddingMask]]:
"""Return the sequences along with their padding mask from ``data``.
:returns:
- The sequences (i.e. `data["seqs"]`)
- The padding mask of the returned sequences.
"""
seqs = data["seqs"]
if device is not None:
seqs = seqs.to(device)
if not data["is_ragged"]:
return seqs, None
seq_lens = data["seq_lens"]
if device is not None:
seq_lens = seq_lens.to(device)
return seqs, PaddingMask(seq_lens, batch_seq_len=seqs.size(1))
def pad_seqs(
seqs: Sequence[Tensor], pad_value: int = 0, pad_to_multiple: int = 1
) -> Tuple[Tensor, Optional[PaddingMask]]:
"""Stack ``seqs`` along a new batch dimension and pad them to equal length.
:param seqs:
The list of variable length sequences. All elements in ``seqs`` are
expected to have the same shape except the first dimension.
:param pad_value:
The value for padded positions.
:param pad_to_multiple:
The sequence dimension is rounded up to the nearest multiple of the
specified value.
:returns:
- The padded sequence stack. *Shape:* :math:`(N,S,*)`, where :math:`N`
is the batch size, :math:`S` is the sequence length, and :math:`*` is
any number of sequence-specific dimensions including none.
- The padding mask of the sequence stack. *Shape:* :math:`(N,S)`, where
:math:`N` is the batch size and :math:`S` is the sequence length.
"""
collater = Collater(pad_value=pad_value, pad_to_multiple=pad_to_multiple)
seq_data = cast(SequenceData, collater(seqs))
return get_seqs_and_padding_mask(seq_data)