dataset_utils.py 13.2 KB
Newer Older
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors, and NVIDIA.
#
# 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.
15
16
17
18
19
20


import collections
import numpy as np


Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
21
22
23
def build_training_sample(sample,
                          target_seq_length, max_seq_length,
                          vocab_id_list, vocab_id_to_token_dict,
24
                          cls_id, sep_id, mask_id, pad_id,
25
                          masked_lm_prob, np_rng):
26
27
28
29
    """Biuld training sample.

    Arguments:
        sample: A list of sentences in which each sentence is a list token ids.
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
30
31
32
        target_seq_length: Desired sequence length.
        max_seq_length: Maximum length of the sequence. All values are padded to
            this length.
33
34
35
36
37
38
39
        vocab_id_list: List of vocabulary ids. Used to pick a random id.
        vocab_id_to_token_dict: A dictionary from vocab ids to text tokens.
        cls_id: Start of example id.
        sep_id: Separator id.
        mask_id: Mask token id.
        pad_id: Padding token id.
        masked_lm_prob: Probability to mask tokens.
40
41
        np_rng: Random number genenrator. Note that this rng state should be
              numpy and not python since python randint is inclusive for
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
42
              the opper bound whereas the numpy one is exclusive.
43
44
45
46
    """

    # We assume that we have at least two sentences in the sample
    assert len(sample) > 1
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
47
    assert target_seq_length <= max_seq_length
48
49

    # Divide sample into two segments (A and B).
50
    tokens_a, tokens_b, is_next_random = get_a_and_b_segments(sample, np_rng)
51

Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
52
    # Truncate to `target_sequence_length`.
53
    max_num_tokens = target_seq_length
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
54
    truncated = truncate_segments(tokens_a, tokens_b, len(tokens_a),
55
                                  len(tokens_b), max_num_tokens, np_rng)
56
57
58
59
60
61
62
63
64

    # Build tokens and toketypes.
    tokens, tokentypes = create_tokens_and_tokentypes(tokens_a, tokens_b,
                                                      cls_id, sep_id)

    # Masking.
    max_predictions_per_seq = masked_lm_prob * max_num_tokens
    (tokens, masked_positions, masked_labels, _) = create_masked_lm_predictions(
        tokens, vocab_id_list, vocab_id_to_token_dict, masked_lm_prob,
65
        cls_id, sep_id, mask_id, max_predictions_per_seq, np_rng)
66
67

    # Padding.
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
68
    tokens_np, tokentypes_np, labels_np, padding_mask_np, loss_mask_np \
69
70
71
72
73
74
        = pad_and_convert_to_numpy(tokens, tokentypes, masked_positions,
                                   masked_labels, pad_id, max_seq_length)

    train_sample = {
        'text': tokens_np,
        'types': tokentypes_np,
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
75
        'labels': labels_np,
76
        'is_random': int(is_next_random),
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
77
78
        'loss_mask': loss_mask_np,
        'padding_mask': padding_mask_np,
79
        'truncated': int(truncated)}
80
81
82
    return train_sample


83
def get_a_and_b_segments(sample, np_rng):
84
85
86
87
88
89
90
91
92
93
94
    """Divide sample into a and b segments."""

    # Number of sentences in the sample.
    n_sentences = len(sample)
    # Make sure we always have two sentences.
    assert n_sentences > 1, 'make sure each sample has at least two sentences.'

    # First part:
    # `a_end` is how many sentences go into the `A`.
    a_end = 1
    if n_sentences >= 3:
95
96
        # Note that randin in numpy is exclusive.
        a_end = np_rng.randint(1, n_sentences)
97
98
99
100
101
102
103
104
105
106
107
    tokens_a = []
    for j in range(a_end):
        tokens_a.extend(sample[j])

    # Second part:
    tokens_b = []
    for j in range(a_end, n_sentences):
        tokens_b.extend(sample[j])

    # Random next:
    is_next_random = False
108
    if np_rng.random() < 0.5:
109
110
111
112
113
114
        is_next_random = True
        tokens_a, tokens_b = tokens_b, tokens_a

    return tokens_a, tokens_b, is_next_random


115
def truncate_segments(tokens_a, tokens_b, len_a, len_b, max_num_tokens, np_rng):
116
    """Truncates a pair of sequences to a maximum sequence length."""
117
    #print(len_a, len_b, max_num_tokens)
118
119
    assert len_a > 0
    assert len_b > 0
120
121
122
    if len_a + len_b <= max_num_tokens:
        return False
    while len_a + len_b > max_num_tokens:
123
124
125
126
127
128
        if len_a > len_b:
            len_a -= 1
            tokens = tokens_a
        else:
            len_b -= 1
            tokens = tokens_b
129
        if np_rng.random() < 0.5:
130
131
132
            del tokens[0]
        else:
            tokens.pop()
133
    return True
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

def create_tokens_and_tokentypes(tokens_a, tokens_b, cls_id, sep_id):
    """Merge segments A and B, add [CLS] and [SEP] and build tokentypes."""

    tokens = []
    tokentypes = []
    # [CLS].
    tokens.append(cls_id)
    tokentypes.append(0)
    # Segment A.
    for token in tokens_a:
        tokens.append(token)
        tokentypes.append(0)
    # [SEP].
    tokens.append(sep_id)
    tokentypes.append(0)
    # Segment B.
    for token in tokens_b:
        tokens.append(token)
        tokentypes.append(1)
    # [SEP].
    tokens.append(sep_id)
    tokentypes.append(1)

    return tokens, tokentypes


MaskedLmInstance = collections.namedtuple("MaskedLmInstance",
                                          ["index", "label"])


def is_start_piece(piece):
  """Check if the current word piece is the starting piece (BERT)."""
  # When a word has been split into
  # WordPieces, the first token does not have any marker and any subsequence
  # tokens are prefixed with ##. So whenever we see the ## token, we
  # append it to the previous set of word indexes.
  return not piece.startswith("##")


def create_masked_lm_predictions(tokens,
                                 vocab_id_list, vocab_id_to_token_dict,
                                 masked_lm_prob,
                                 cls_id, sep_id, mask_id,
                                 max_predictions_per_seq,
179
                                 np_rng,
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
                                 max_ngrams=3,
                                 do_whole_word_mask=True,
                                 favor_longer_ngram=False,
                                 do_permutation=False):
  """Creates the predictions for the masked LM objective.
  Note: Tokens here are vocab ids and not text tokens."""

  cand_indexes = []
  # Note(mingdachen): We create a list for recording if the piece is
  # the starting piece of current token, where 1 means true, so that
  # on-the-fly whole word masking is possible.
  token_boundary = [0] * len(tokens)

  for (i, token) in enumerate(tokens):
    if token == cls_id or token == sep_id:
      token_boundary[i] = 1
      continue
    # Whole Word Masking means that if we mask all of the wordpieces
    # corresponding to an original word.
    #
    # Note that Whole Word Masking does *not* change the training code
    # at all -- we still predict each WordPiece independently, softmaxed
    # over the entire vocabulary.
    if (do_whole_word_mask and len(cand_indexes) >= 1 and
        not is_start_piece(vocab_id_to_token_dict[token])):
      cand_indexes[-1].append(i)
    else:
      cand_indexes.append([i])
      if is_start_piece(vocab_id_to_token_dict[token]):
        token_boundary[i] = 1

  output_tokens = list(tokens)

  masked_lm_positions = []
  masked_lm_labels = []

  if masked_lm_prob == 0:
    return (output_tokens, masked_lm_positions,
            masked_lm_labels, token_boundary)

  num_to_predict = min(max_predictions_per_seq,
                       max(1, int(round(len(tokens) * masked_lm_prob))))

  # Note(mingdachen):
  # By default, we set the probilities to favor shorter ngram sequences.
  ngrams = np.arange(1, max_ngrams + 1, dtype=np.int64)
  pvals = 1. / np.arange(1, max_ngrams + 1)
  pvals /= pvals.sum(keepdims=True)

  if favor_longer_ngram:
    pvals = pvals[::-1]

  ngram_indexes = []
  for idx in range(len(cand_indexes)):
    ngram_index = []
    for n in ngrams:
      ngram_index.append(cand_indexes[idx:idx+n])
    ngram_indexes.append(ngram_index)

239
  np_rng.shuffle(ngram_indexes)
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

  masked_lms = []
  covered_indexes = set()
  for cand_index_set in ngram_indexes:
    if len(masked_lms) >= num_to_predict:
      break
    if not cand_index_set:
      continue
    # Note(mingdachen):
    # Skip current piece if they are covered in lm masking or previous ngrams.
    for index_set in cand_index_set[0]:
      for index in index_set:
        if index in covered_indexes:
          continue

255
256
257
    n = np_rng.choice(ngrams[:len(cand_index_set)],
                      p=pvals[:len(cand_index_set)] /
                      pvals[:len(cand_index_set)].sum(keepdims=True))
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    index_set = sum(cand_index_set[n - 1], [])
    n -= 1
    # Note(mingdachen):
    # Repeatedly looking for a candidate that does not exceed the
    # maximum number of predictions by trying shorter ngrams.
    while len(masked_lms) + len(index_set) > num_to_predict:
      if n == 0:
        break
      index_set = sum(cand_index_set[n - 1], [])
      n -= 1
    # If adding a whole-word mask would exceed the maximum number of
    # predictions, then just skip this candidate.
    if len(masked_lms) + len(index_set) > num_to_predict:
      continue
    is_any_index_covered = False
    for index in index_set:
      if index in covered_indexes:
        is_any_index_covered = True
        break
    if is_any_index_covered:
      continue
    for index in index_set:
      covered_indexes.add(index)

      masked_token = None
      # 80% of the time, replace with [MASK]
284
      if np_rng.random() < 0.8:
285
286
287
        masked_token = mask_id
      else:
        # 10% of the time, keep original
288
        if np_rng.random() < 0.5:
289
290
291
          masked_token = tokens[index]
        # 10% of the time, replace with random word
        else:
292
          masked_token = vocab_id_list[np_rng.randint(0, len(vocab_id_list))]
293
294
295
296
297
298

      output_tokens[index] = masked_token

      masked_lms.append(MaskedLmInstance(index=index, label=tokens[index]))
  assert len(masked_lms) <= num_to_predict

299
  np_rng.shuffle(ngram_indexes)
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

  select_indexes = set()
  if do_permutation:
    for cand_index_set in ngram_indexes:
      if len(select_indexes) >= num_to_predict:
        break
      if not cand_index_set:
        continue
      # Note(mingdachen):
      # Skip current piece if they are covered in lm masking or previous ngrams.
      for index_set in cand_index_set[0]:
        for index in index_set:
          if index in covered_indexes or index in select_indexes:
            continue

      n = np.random.choice(ngrams[:len(cand_index_set)],
                           p=pvals[:len(cand_index_set)] /
                           pvals[:len(cand_index_set)].sum(keepdims=True))
      index_set = sum(cand_index_set[n - 1], [])
      n -= 1

      while len(select_indexes) + len(index_set) > num_to_predict:
        if n == 0:
          break
        index_set = sum(cand_index_set[n - 1], [])
        n -= 1
      # If adding a whole-word mask would exceed the maximum number of
      # predictions, then just skip this candidate.
      if len(select_indexes) + len(index_set) > num_to_predict:
        continue
      is_any_index_covered = False
      for index in index_set:
        if index in covered_indexes or index in select_indexes:
          is_any_index_covered = True
          break
      if is_any_index_covered:
        continue
      for index in index_set:
        select_indexes.add(index)
    assert len(select_indexes) <= num_to_predict

    select_indexes = sorted(select_indexes)
    permute_indexes = list(select_indexes)
343
    np_rng.shuffle(permute_indexes)
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
    orig_token = list(output_tokens)

    for src_i, tgt_i in zip(select_indexes, permute_indexes):
      output_tokens[src_i] = orig_token[tgt_i]
      masked_lms.append(MaskedLmInstance(index=src_i, label=orig_token[src_i]))

  masked_lms = sorted(masked_lms, key=lambda x: x.index)

  for p in masked_lms:
    masked_lm_positions.append(p.index)
    masked_lm_labels.append(p.label)
  return (output_tokens, masked_lm_positions, masked_lm_labels, token_boundary)


def pad_and_convert_to_numpy(tokens, tokentypes, masked_positions,
                             masked_labels, pad_id, max_seq_length):
    """Pad sequences and convert them to numpy."""

    # Some checks.
    num_tokens = len(tokens)
    padding_length = max_seq_length - num_tokens
    assert padding_length >= 0
    assert len(tokentypes) == num_tokens
    assert len(masked_positions) == len(masked_labels)

    # Tokens and token types.
    filler = [pad_id]*padding_length
    tokens_np = np.array(tokens + filler, dtype=np.int64)
    tokentypes_np = np.array(tokentypes + filler, dtype=np.int64)

    # Padding mask.
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
375
376
    padding_mask_np = np.array([1]*num_tokens + [0]*padding_length,
                               dtype=np.int64)
377
378
379
380
381
382
383
384
385
386
387

    # Lables and loss mask.
    labels = [-1] * max_seq_length
    loss_mask = [0] * max_seq_length
    for i in range(len(masked_positions)):
        assert masked_positions[i] < num_tokens
        labels[masked_positions[i]] = masked_labels[i]
        loss_mask[masked_positions[i]] = 1
    labels_np = np.array(labels, dtype=np.int64)
    loss_mask_np = np.array(loss_mask, dtype=np.int64)

388
    return tokens_np, tokentypes_np, labels_np, padding_mask_np, loss_mask_np