text_layers.py 31.8 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Chen Chen's avatar
Chen Chen committed
2
3
4
5
6
7
8
9
10
11
12
13
#
# 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.
Frederick Liu's avatar
Frederick Liu committed
14

Chen Chen's avatar
Chen Chen committed
15
"""Keras Layers for BERT-specific preprocessing."""
16
# pylint: disable=g-import-not-at-top
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
17
from typing import Any, Dict, List, Mapping, Optional, Text, Union
Chen Chen's avatar
Chen Chen committed
18
19
20
21
22

from absl import logging
import tensorflow as tf

try:
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
23
  # pytype: disable=import-error
24
25
  import tensorflow_text as text
  from tensorflow_text.python.ops import bert_tokenizer
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
26
  # pytype: enable=import-error
Chen Chen's avatar
Chen Chen committed
27
28
except ImportError:
  text = None
29
  bert_tokenizer = None
Chen Chen's avatar
Chen Chen committed
30
31
32
except tf.errors.NotFoundError as e:
  logging.warn("Encountered error when importing tensorflow_text: %s", e)
  text = None
33
  bert_tokenizer = None
Chen Chen's avatar
Chen Chen committed
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


def _check_if_tf_text_installed():
  if text is None:
    raise ImportError("import tensorflow_text failed, please install "
                      "'tensorflow-text-nightly'.")


def _truncate_row_lengths(ragged_tensor: tf.RaggedTensor,
                          new_lengths: tf.Tensor) -> tf.RaggedTensor:
  """Truncates the rows of `ragged_tensor` to the given row lengths."""
  new_lengths = tf.broadcast_to(new_lengths,
                                ragged_tensor.bounding_shape()[0:1])
  def fn(x):
    row, new_length = x
    return row[0:new_length]
  fn_dtype = tf.RaggedTensorSpec(dtype=ragged_tensor.dtype,
                                 ragged_rank=ragged_tensor.ragged_rank - 1)
  result = tf.map_fn(fn, (ragged_tensor, new_lengths), dtype=fn_dtype)
  # Work around broken shape propagation: without this, result has unknown rank.
  flat_values_shape = [None] * ragged_tensor.flat_values.shape.rank
  result = result.with_flat_values(
      tf.ensure_shape(result.flat_values, flat_values_shape))

  return result


class BertTokenizer(tf.keras.layers.Layer):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
62
  """Wraps TF.Text's BertTokenizer with pre-defined vocab as a Keras Layer.
Chen Chen's avatar
Chen Chen committed
63
64

  Attributes:
65
66
67
    tokenize_with_offsets: If true, calls
      `text.BertTokenizer.tokenize_with_offsets()` instead of plain
      `text.BertTokenizer.tokenize()` and outputs a triple of
68
69
      `(tokens, start_offsets, limit_offsets)`.
    raw_table_access: An object with methods `.lookup(keys) and `.size()`
Chen Chen's avatar
Chen Chen committed
70
      that operate on the raw lookup table of tokens. It can be used to
71
      look up special token synbols like `[MASK]`.
Chen Chen's avatar
Chen Chen committed
72
73
74
75
  """

  def __init__(self, *,
               vocab_file: str,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
76
               lower_case: Optional[bool] = None,
Chen Chen's avatar
Chen Chen committed
77
               tokenize_with_offsets: bool = False,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
78
               tokenizer_kwargs: Optional[Mapping[Text, Any]] = None,
Chen Chen's avatar
Chen Chen committed
79
               **kwargs):
80
    """Initialize a `BertTokenizer` layer.
Chen Chen's avatar
Chen Chen committed
81
82
83
84
85

    Args:
      vocab_file: A Python string with the path of the vocabulary file.
        This is a text file with newline-separated wordpiece tokens.
        This layer initializes a lookup table from it that gets used with
86
        `text.BertTokenizer`.
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
87
      lower_case: Optional boolean forwarded to `text.BertTokenizer`.
Chen Chen's avatar
Chen Chen committed
88
89
        If true, input text is converted to lower case (where applicable)
        before tokenization. This must be set to match the way in which
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
90
91
        the `vocab_file` was created. If passed, this overrides whatever value
        may have been passed in `tokenizer_kwargs`.
Chen Chen's avatar
Chen Chen committed
92
      tokenize_with_offsets: A Python boolean. If true, this layer calls
93
94
95
96
        `text.BertTokenizer.tokenize_with_offsets()` instead of plain
        `text.BertTokenizer.tokenize()` and outputs a triple of
        `(tokens, start_offsets, limit_offsets)`
        insead of just tokens.
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
97
98
      tokenizer_kwargs: Optional mapping with keyword arguments to forward to
        `text.BertTokenizer`'s constructor.
99
      **kwargs: Standard arguments to `Layer()`.
Chen Chen's avatar
Chen Chen committed
100
101

    Raises:
102
      ImportError: If importing `tensorflow_text` failed.
Chen Chen's avatar
Chen Chen committed
103
104
105
106
    """
    _check_if_tf_text_installed()

    self.tokenize_with_offsets = tokenize_with_offsets
107
    # TODO(b/177326279): Stop storing the vocab table initializer as an
108
    # attribute when https://github.com/tensorflow/tensorflow/issues/46456
109
110
111
112
113
114
115
116
    # has been fixed in the TensorFlow versions of the TF Hub users that load
    # a SavedModel created from this layer. Due to that issue, loading such a
    # SavedModel forgets to add .vocab_table._initializer as a trackable
    # dependency of .vocab_table, so that saving it again to a second SavedModel
    # (e.g., the final model built using TF Hub) does not properly track
    # the ._vocab_table._initializer._filename as an Asset.
    self._vocab_table, self._vocab_initializer_donotuse = (
        self._create_vocab_table_and_initializer(vocab_file))
Chen Chen's avatar
Chen Chen committed
117
118
119
    self._special_tokens_dict = self._create_special_tokens_dict(
        self._vocab_table, vocab_file)
    super().__init__(**kwargs)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
120
121
122
123
124
    tokenizer_kwargs = dict(tokenizer_kwargs or {})
    if lower_case is not None:
      tokenizer_kwargs["lower_case"] = lower_case
    self._bert_tokenizer = text.BertTokenizer(self._vocab_table,
                                              **tokenizer_kwargs)
Chen Chen's avatar
Chen Chen committed
125
126
127
128
129

  @property
  def vocab_size(self):
    return self._vocab_table.size()

130
  def _create_vocab_table_and_initializer(self, vocab_file):
Chen Chen's avatar
Chen Chen committed
131
132
133
134
    vocab_initializer = tf.lookup.TextFileInitializer(
        vocab_file,
        key_dtype=tf.string, key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
        value_dtype=tf.int64, value_index=tf.lookup.TextFileIndex.LINE_NUMBER)
135
136
    vocab_table = tf.lookup.StaticHashTable(vocab_initializer, default_value=-1)
    return vocab_table, vocab_initializer
Chen Chen's avatar
Chen Chen committed
137
138

  def call(self, inputs: tf.Tensor):
139
    """Calls `text.BertTokenizer` on inputs.
Chen Chen's avatar
Chen Chen committed
140
141

    Args:
142
      inputs: A string Tensor of shape `(batch_size,)`.
Chen Chen's avatar
Chen Chen committed
143
144

    Returns:
145
146
      One or three of `RaggedTensors` if `tokenize_with_offsets` is False or
      True, respectively. These are
147
148
149
150
151
152
153
154
        tokens: A `RaggedTensor` of shape
          `[batch_size, (words), (pieces_per_word)]`
          and type int32. `tokens[i,j,k]` contains the k-th wordpiece of the
          j-th word in the i-th input.
        start_offsets, limit_offsets: If `tokenize_with_offsets` is True,
          RaggedTensors of type int64 with the same indices as tokens.
          Element `[i,j,k]` contains the byte offset at the start, or past the
          end, resp., for the k-th wordpiece of the j-th word in the i-th input.
Chen Chen's avatar
Chen Chen committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    """
    # Prepare to reshape the result to work around broken shape inference.
    batch_size = tf.shape(inputs)[0]
    def _reshape(rt):
      values = rt.values
      row_splits = rt.row_splits
      row_splits = tf.reshape(row_splits, [batch_size + 1])
      return tf.RaggedTensor.from_row_splits(values, row_splits)

    # Call the tokenizer.
    if self.tokenize_with_offsets:
      tokens, start_offsets, limit_offsets = (
          self._bert_tokenizer.tokenize_with_offsets(inputs))
      tokens = tf.cast(tokens, dtype=tf.int32)
      return _reshape(tokens), _reshape(start_offsets), _reshape(limit_offsets)
    else:
      tokens = self._bert_tokenizer.tokenize(inputs)
      tokens = tf.cast(tokens, dtype=tf.int32)
      return _reshape(tokens)

  def get_config(self):
    # Skip in tf.saved_model.save(); fail if called direcly.
177
    raise NotImplementedError("TODO(b/170480226): implement")
Chen Chen's avatar
Chen Chen committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

  def get_special_tokens_dict(self):
    """Returns dict of token ids, keyed by standard names for their purpose.

    Returns:
      A dict from Python strings to Python integers. Each key is a standard
      name for a special token describing its use. (For example, "padding_id"
      is what BERT traditionally calls "[PAD]" but others may call "<pad>".)
      The corresponding value is the integer token id. If a special token
      is not found, its entry is omitted from the dict.

      The supported keys and tokens are:
        * start_of_sequence_id: looked up from "[CLS]"
        * end_of_segment_id: looked up from "[SEP]"
        * padding_id: looked up form "[PAD]"
        * mask_id: looked up from "[MASK]"
194
        * vocab_size: one past the largest token id used
Chen Chen's avatar
Chen Chen committed
195
196
197
198
199
200
201
202
203
204
205
206
    """
    return self._special_tokens_dict

  def _create_special_tokens_dict(self, vocab_table, vocab_file):
    special_tokens = dict(start_of_sequence_id="[CLS]",
                          end_of_segment_id="[SEP]",
                          padding_id="[PAD]",
                          mask_id="[MASK]")
    with tf.init_scope():
      if tf.executing_eagerly():
        special_token_ids = vocab_table.lookup(
            tf.constant(list(special_tokens.values()), tf.string))
207
        vocab_size = vocab_table.size()
Chen Chen's avatar
Chen Chen committed
208
209
210
211
212
213
214
      else:
        # A blast from the past: non-eager init context while building Model.
        # This can happen with Estimator or tf.compat.v1.disable_v2_behavior().
        logging.warning(
            "Non-eager init context; computing "
            "BertTokenizer's special_tokens_dict in tf.compat.v1.Session")
        with tf.Graph().as_default():
215
216
          local_vocab_table, _ = self._create_vocab_table_and_initializer(
              vocab_file)
Chen Chen's avatar
Chen Chen committed
217
218
          special_token_ids_tensor = local_vocab_table.lookup(
              tf.constant(list(special_tokens.values()), tf.string))
219
          vocab_size_tensor = local_vocab_table.size()
Chen Chen's avatar
Chen Chen committed
220
221
222
          init_ops = [tf.compat.v1.initialize_all_tables()]
          with tf.compat.v1.Session() as sess:
            sess.run(init_ops)
223
224
225
226
227
            special_token_ids, vocab_size = sess.run(
                [special_token_ids_tensor, vocab_size_tensor])
      result = dict(
          vocab_size=int(vocab_size)  # Numpy to Python.
      )
Chen Chen's avatar
Chen Chen committed
228
      for k, v in zip(special_tokens, special_token_ids):
229
        v = int(v)
Chen Chen's avatar
Chen Chen committed
230
231
232
233
234
235
236
237
238
        if v >= 0:
          result[k] = v
        else:
          logging.warning("Could not find %s as token \"%s\" in vocab file %s",
                          k, special_tokens[k], vocab_file)
    return result


class SentencepieceTokenizer(tf.keras.layers.Layer):
239
  """Wraps `tf_text.SentencepieceTokenizer` as a Keras Layer.
Chen Chen's avatar
Chen Chen committed
240
241
242

  Attributes:
    tokenize_with_offsets: If true, calls
243
244
245
      `SentencepieceTokenizer.tokenize_with_offsets()`
      instead of plain `.tokenize()` and outputs a triple of
      `(tokens, start_offsets, limit_offsets)`.
Chen Chen's avatar
Chen Chen committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  """

  def __init__(self,
               *,
               lower_case: bool,
               model_file_path: Optional[str] = None,
               model_serialized_proto: Optional[str] = None,
               tokenize_with_offsets: bool = False,
               nbest_size: int = 0,
               alpha: float = 1.0,
               strip_diacritics: bool = False,
               **kwargs):
    """Initializes a SentencepieceTokenizer layer.

    Args:
      lower_case: A Python boolean indicating whether to lowercase the string
        before tokenization. NOTE: New models are encouraged to build `*_cf`
        (case folding) normalization into the Sentencepiece model itself and
        avoid this extra step.
      model_file_path: A Python string with the path of the sentencepiece model.
        Exactly one of `model_file_path` and `model_serialized_proto` can be
        specified. In either case, the Keras model config for this layer will
        store the actual proto (not a filename passed here).
      model_serialized_proto: The sentencepiece model serialized proto string.
      tokenize_with_offsets: A Python boolean. If true, this layer calls
271
272
273
        `SentencepieceTokenizer.tokenize_with_offsets()` instead of
        plain `.tokenize()` and outputs a triple of
        `(tokens, start_offsets, limit_offsets)` insead of just tokens.
Chen Chen's avatar
Chen Chen committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        Note that when following `strip_diacritics` is set to True, returning
        offsets is not supported now.
      nbest_size: A scalar for sampling:
        nbest_size = {0,1}: No sampling is performed. (default)
        nbest_size > 1: samples from the nbest_size results.
        nbest_size < 0: assuming that nbest_size is infinite and samples
           from the all hypothesis (lattice) using
           forward-filtering-and-backward-sampling algorithm.
      alpha: A scalar for a smoothing parameter. Inverse temperature for
        probability rescaling.
      strip_diacritics: Whether to strip diacritics or not. Note that stripping
        diacritics requires additional text normalization and dropping bytes,
        which makes it impossible to keep track of the offsets now. Hence
        when `strip_diacritics` is set to True, we don't yet support
        `tokenize_with_offsets`. NOTE: New models are encouraged to put this
        into custom normalization rules for the Sentencepiece model itself to
        avoid this extra step and the limitation regarding offsets.
291
      **kwargs: standard arguments to `Layer()`.
Chen Chen's avatar
Chen Chen committed
292
293
294
295
296
297
298
299
300

    Raises:
      ImportError: if importing tensorflow_text failed.
    """
    _check_if_tf_text_installed()
    super().__init__(**kwargs)
    if bool(model_file_path) == bool(model_serialized_proto):
      raise ValueError("Exact one of `model_file_path` and "
                       "`model_serialized_proto` can be specified.")
301
    # TODO(b/181866850): Support tokenize_with_offsets for strip_diacritics=True
Chen Chen's avatar
Chen Chen committed
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
    if tokenize_with_offsets and strip_diacritics:
      raise ValueError("`tokenize_with_offsets` is not supported when "
                       "`strip_diacritics` is set to True.")
    if model_file_path:
      self._model_serialized_proto = tf.io.gfile.GFile(model_file_path,
                                                       "rb").read()
    else:
      self._model_serialized_proto = model_serialized_proto

    self._lower_case = lower_case
    self.tokenize_with_offsets = tokenize_with_offsets
    self._nbest_size = nbest_size
    self._alpha = alpha
    self._strip_diacritics = strip_diacritics
    self._tokenizer = self._create_tokenizer()
    self._special_tokens_dict = self._create_special_tokens_dict()

  def _create_tokenizer(self):
    return text.SentencepieceTokenizer(
        model=self._model_serialized_proto,
        out_type=tf.int32,
        nbest_size=self._nbest_size,
        alpha=self._alpha)

  @property
  def vocab_size(self):
    return self._tokenizer.vocab_size()

  def call(self, inputs: tf.Tensor):
331
    """Calls `text.SentencepieceTokenizer` on inputs.
Chen Chen's avatar
Chen Chen committed
332
333

    Args:
334
      inputs: A string Tensor of shape `(batch_size,)`.
Chen Chen's avatar
Chen Chen committed
335
336
337
338

    Returns:
      One or three of RaggedTensors if tokenize_with_offsets is False or True,
      respectively. These are
339
340
341
342
343
      tokens: A RaggedTensor of shape `[batch_size, (pieces)]` and type `int32`.
        `tokens[i,j]` contains the j-th piece in the i-th input.
      start_offsets, limit_offsets: If `tokenize_with_offsets` is True,
        RaggedTensors of type `int64` with the same indices as tokens.
        Element `[i,j]` contains the byte offset at the start, or past the
Chen Chen's avatar
Chen Chen committed
344
345
346
347
        end, resp., for the j-th piece in the i-th input.
    """
    if self._strip_diacritics:
      if self.tokenize_with_offsets:
348
349
        raise ValueError("`tokenize_with_offsets` is not supported yet when "
                         "`strip_diacritics` is set to True (b/181866850).")
Chen Chen's avatar
Chen Chen committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
      inputs = text.normalize_utf8(inputs, "NFD")
      inputs = tf.strings.regex_replace(inputs, r"\p{Mn}", "")

    if self._lower_case:
      inputs = text.case_fold_utf8(inputs)

    # Prepare to reshape the result to work around broken shape inference.
    batch_size = tf.shape(inputs)[0]
    def _reshape(rt):
      values = rt.values
      row_splits = rt.row_splits
      row_splits = tf.reshape(row_splits, [batch_size + 1])
      return tf.RaggedTensor.from_row_splits(values, row_splits)

    # Call the tokenizer.
    if self.tokenize_with_offsets:
      tokens, start_offsets, limit_offsets = (
          self._tokenizer.tokenize_with_offsets(inputs))
      return _reshape(tokens), _reshape(start_offsets), _reshape(limit_offsets)
    else:
      tokens = self._tokenizer.tokenize(inputs)
      return _reshape(tokens)

  def get_config(self):
374
375
    # Skip in tf.saved_model.save(); fail if called direcly.
    raise NotImplementedError("TODO(b/170480226): implement")
Chen Chen's avatar
Chen Chen committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

  def get_special_tokens_dict(self):
    """Returns dict of token ids, keyed by standard names for their purpose.

    Returns:
      A dict from Python strings to Python integers. Each key is a standard
      name for a special token describing its use. (For example, "padding_id"
      is what Sentencepiece calls "<pad>" but others may call "[PAD]".)
      The corresponding value is the integer token id. If a special token
      is not found, its entry is omitted from the dict.

      The supported keys and tokens are:
        * start_of_sequence_id: looked up from "[CLS]"
        * end_of_segment_id: looked up from "[SEP]"
        * padding_id: looked up from "<pad>"
        * mask_id: looked up from "[MASK]"
392
        * vocab_size: one past the largest token id used
Chen Chen's avatar
Chen Chen committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    """
    return self._special_tokens_dict

  def _create_special_tokens_dict(self):
    special_tokens = dict(
        start_of_sequence_id=b"[CLS]",
        end_of_segment_id=b"[SEP]",
        padding_id=b"<pad>",
        mask_id=b"[MASK]")
    with tf.init_scope():
      if tf.executing_eagerly():
        special_token_ids = self._tokenizer.string_to_id(
            tf.constant(list(special_tokens.values()), tf.string))
        inverse_tokens = self._tokenizer.id_to_string(special_token_ids)
407
        vocab_size = self._tokenizer.vocab_size()
Chen Chen's avatar
Chen Chen committed
408
409
410
411
412
413
414
415
416
417
418
419
      else:
        # A blast from the past: non-eager init context while building Model.
        # This can happen with Estimator or tf.compat.v1.disable_v2_behavior().
        logging.warning(
            "Non-eager init context; computing SentencepieceTokenizer's "
            "special_tokens_dict in tf.compat.v1.Session")
        with tf.Graph().as_default():
          local_tokenizer = self._create_tokenizer()
          special_token_ids_tensor = local_tokenizer.string_to_id(
              tf.constant(list(special_tokens.values()), tf.string))
          inverse_tokens_tensor = local_tokenizer.id_to_string(
              special_token_ids_tensor)
420
          vocab_size_tensor = local_tokenizer.vocab_size()
Chen Chen's avatar
Chen Chen committed
421
          with tf.compat.v1.Session() as sess:
422
423
424
425
426
427
            special_token_ids, inverse_tokens, vocab_size = sess.run(
                [special_token_ids_tensor, inverse_tokens_tensor,
                 vocab_size_tensor])
      result = dict(
          vocab_size=int(vocab_size)  # Numpy to Python.
      )
Chen Chen's avatar
Chen Chen committed
428
429
430
431
      for name, token_id, inverse_token in zip(special_tokens,
                                               special_token_ids,
                                               inverse_tokens):
        if special_tokens[name] == inverse_token:
432
          result[name] = int(token_id)
Chen Chen's avatar
Chen Chen committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
        else:
          logging.warning(
              "Could not find %s as token \"%s\" in sentencepiece model, "
              "got \"%s\"", name, special_tokens[name], inverse_token)
    return result


class BertPackInputs(tf.keras.layers.Layer):
  """Packs tokens into model inputs for BERT."""

  def __init__(self,
               seq_length,
               *,
               start_of_sequence_id=None,
               end_of_segment_id=None,
               padding_id=None,
               special_tokens_dict=None,
               truncator="round_robin",
               **kwargs):
452
    """Initializes with a target `seq_length`, relevant token ids and truncator.
Chen Chen's avatar
Chen Chen committed
453
454
455
456
457
458
459
460
461
462
463
464

    Args:
      seq_length: The desired output length. Must not exceed the max_seq_length
        that was fixed at training time for the BERT model receiving the inputs.
      start_of_sequence_id: The numeric id of the token that is to be placed
        at the start of each sequence (called "[CLS]" for BERT).
      end_of_segment_id: The numeric id of the token that is to be placed
        at the end of each input segment (called "[SEP]" for BERT).
      padding_id: The numeric id of the token that is to be placed into the
        unused positions after the last segment in the sequence
        (called "[PAD]" for BERT).
      special_tokens_dict: Optionally, a dict from Python strings to Python
465
466
        integers that contains values for `start_of_sequence_id`,
        `end_of_segment_id` and `padding_id`. (Further values in the dict are
Chen Chen's avatar
Chen Chen committed
467
468
469
        silenty ignored.) If this is passed, separate *_id arguments must be
        omitted.
      truncator: The algorithm to truncate a list of batched segments to fit a
470
471
        per-example length limit. The value can be either `round_robin` or
        `waterfall`:
Chen Chen's avatar
Chen Chen committed
472
473
474
475
476
477
478
479
480
          (1) For "round_robin" algorithm, available space is assigned
          one token at a time in a round-robin fashion to the inputs that still
          need some, until the limit is reached. It currently only supports
          one or two segments.
          (2) For "waterfall" algorithm, the allocation of the budget is done
            using a "waterfall" algorithm that allocates quota in a
            left-to-right manner and fills up the buckets until we run out of
            budget. It support arbitrary number of segments.

481
      **kwargs: standard arguments to `Layer()`.
Chen Chen's avatar
Chen Chen committed
482
483

    Raises:
484
      ImportError: if importing `tensorflow_text` failed.
Chen Chen's avatar
Chen Chen committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
    """
    _check_if_tf_text_installed()
    super().__init__(**kwargs)
    self.seq_length = seq_length
    if truncator not in ("round_robin", "waterfall"):
      raise ValueError("Only 'round_robin' and 'waterfall' algorithms are "
                       "supported, but got %s" % truncator)
    self.truncator = truncator
    self._init_token_ids(
        start_of_sequence_id=start_of_sequence_id,
        end_of_segment_id=end_of_segment_id,
        padding_id=padding_id,
        special_tokens_dict=special_tokens_dict)

  def _init_token_ids(
      self, *,
      start_of_sequence_id,
      end_of_segment_id,
      padding_id,
      special_tokens_dict):
    usage = ("Must pass either all of start_of_sequence_id, end_of_segment_id, "
             "padding_id as arguments, or else a special_tokens_dict "
             "with those keys.")
    special_tokens_args = [start_of_sequence_id, end_of_segment_id, padding_id]
    if special_tokens_dict is None:
      if any(x is None for x in special_tokens_args):
        return ValueError(usage)
      self.start_of_sequence_id = int(start_of_sequence_id)
      self.end_of_segment_id = int(end_of_segment_id)
      self.padding_id = int(padding_id)
    else:
      if any(x is not None for x in special_tokens_args):
        return ValueError(usage)
      self.start_of_sequence_id = int(
          special_tokens_dict["start_of_sequence_id"])
      self.end_of_segment_id = int(special_tokens_dict["end_of_segment_id"])
      self.padding_id = int(special_tokens_dict["padding_id"])

  def get_config(self) -> Dict[str, Any]:
    config = super().get_config()
    config["seq_length"] = self.seq_length
    config["start_of_sequence_id"] = self.start_of_sequence_id
    config["end_of_segment_id"] = self.end_of_segment_id
    config["padding_id"] = self.padding_id
    config["truncator"] = self.truncator
    return config

  def call(self, inputs: Union[tf.RaggedTensor, List[tf.RaggedTensor]]):
    """Adds special tokens to pack a list of segments into BERT input Tensors.

    Args:
      inputs: A Python list of one or two RaggedTensors, each with the batched
        values one input segment. The j-th segment of the i-th input example
        consists of slice `inputs[j][i, ...]`.

    Returns:
      A nest of Tensors for use as input to the BERT TransformerEncoder.
    """
    # BertPackInputsSavedModelWrapper relies on only calling bert_pack_inputs()
    return BertPackInputs.bert_pack_inputs(
        inputs, self.seq_length,
        start_of_sequence_id=self.start_of_sequence_id,
        end_of_segment_id=self.end_of_segment_id,
        padding_id=self.padding_id,
        truncator=self.truncator)

  @staticmethod
  def bert_pack_inputs(inputs: Union[tf.RaggedTensor, List[tf.RaggedTensor]],
                       seq_length: Union[int, tf.Tensor],
                       start_of_sequence_id: Union[int, tf.Tensor],
                       end_of_segment_id: Union[int, tf.Tensor],
                       padding_id: Union[int, tf.Tensor],
                       truncator="round_robin"):
    """Freestanding equivalent of the BertPackInputs layer."""
    _check_if_tf_text_installed()
    # Sanitize inputs.
    if not isinstance(inputs, (list, tuple)):
      inputs = [inputs]
    if not inputs:
      raise ValueError("At least one input is required for packing")
    input_ranks = [rt.shape.rank for rt in inputs]
    if None in input_ranks or len(set(input_ranks)) > 1:
      raise ValueError("All inputs for packing must have the same known rank, "
                       "found ranks " + ",".join(input_ranks))
    # Flatten inputs to [batch_size, (tokens)].
    if input_ranks[0] > 2:
      inputs = [rt.merge_dims(1, -1) for rt in inputs]
    # In case inputs weren't truncated (as they should have been),
    # fall back to some ad-hoc truncation.
    num_special_tokens = len(inputs) + 1
    if truncator == "round_robin":
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
576
577
      trimmed_segments = text.RoundRobinTrimmer(seq_length -
                                                num_special_tokens).trim(inputs)
Chen Chen's avatar
Chen Chen committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
    elif truncator == "waterfall":
      trimmed_segments = text.WaterfallTrimmer(
          seq_length - num_special_tokens).trim(inputs)
    else:
      raise ValueError("Unsupported truncator: %s" % truncator)
    # Combine segments.
    segments_combined, segment_ids = text.combine_segments(
        trimmed_segments,
        start_of_sequence_id=start_of_sequence_id,
        end_of_segment_id=end_of_segment_id)
    # Pad to dense Tensors.
    input_word_ids, _ = text.pad_model_inputs(segments_combined, seq_length,
                                              pad_value=padding_id)
    input_type_ids, input_mask = text.pad_model_inputs(segment_ids, seq_length,
                                                       pad_value=0)
    # Work around broken shape inference.
    output_shape = tf.stack([
        inputs[0].nrows(out_type=tf.int32),  # batch_size
        tf.cast(seq_length, dtype=tf.int32)])
    def _reshape(t):
      return tf.reshape(t, output_shape)
    # Assemble nest of input tensors as expected by BERT TransformerEncoder.
    return dict(input_word_ids=_reshape(input_word_ids),
                input_mask=_reshape(input_mask),
                input_type_ids=_reshape(input_type_ids))
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738


class FastWordpieceBertTokenizer(tf.keras.layers.Layer):
  """A bert tokenizer keras layer using text.FastWordpieceTokenizer.

  See details: "Fast WordPiece Tokenization" (https://arxiv.org/abs/2012.15524)
  """

  def __init__(self,
               *,
               vocab_file: str,
               lower_case: bool,
               tokenize_with_offsets: bool = False,
               **kwargs):
    """Initializes a FastWordpieceBertTokenizer layer.

    Args:
      vocab_file: A Python string with the path of the vocabulary file. This is
        a text file with newline-separated wordpiece tokens. This layer loads
        a list of tokens from it to create text.FastWordpieceTokenizer.
      lower_case: A Python boolean forwarded to text.BasicTokenizer. If true,
        input text is converted to lower case (where applicable) before
        tokenization. This must be set to match the way in which the vocab_file
        was created.
      tokenize_with_offsets: A Python boolean. If true, this layer calls
        FastWordpieceTokenizer.tokenize_with_offsets() instead of plain
        .tokenize() and outputs a triple of (tokens, start_offsets,
        limit_offsets) insead of just tokens.
      **kwargs: standard arguments to Layer().
    """
    super().__init__(**kwargs)
    logging.info("Initialize a FastWordpieceBertTokenizer.")
    self.tokenize_with_offsets = tokenize_with_offsets
    self._basic_tokenizer = bert_tokenizer.BasicTokenizer(lower_case=lower_case)

    # Read the vocab file into a list of tokens to create `fast_wp_tokenizer`.
    self._vocab = [line.rstrip() for line in tf.io.gfile.GFile(vocab_file)]
    self._fast_wp_tokenizer = text.FastWordpieceTokenizer(
        vocab=self._vocab, token_out_type=tf.int32, no_pretokenization=True)
    self._special_tokens_dict = self._create_special_tokens_dict()

  @property
  def vocab_size(self):
    return len(self._vocab)

  def get_config(self):
    # Skip in tf.saved_model.save(); fail if called direcly.
    # We cannot just put the original, user-supplied vocab file name into
    # the config, because the path has to change as the SavedModel is copied
    # around.
    raise NotImplementedError("Not implemented yet.")

  def get_special_tokens_dict(self):
    """Returns dict of token ids, keyed by standard names for their purpose.

    Returns:
      A dict from Python strings to Python integers. Each key is a standard
      name for a special token describing its use. (For example, "padding_id"
      is what BERT traditionally calls "[PAD]" but others may call "<pad>".)
      The corresponding value is the integer token id. If a special token
      is not found, its entry is omitted from the dict.

      The supported keys and tokens are:
        * start_of_sequence_id: looked up from "[CLS]"
        * end_of_segment_id: looked up from "[SEP]"
        * padding_id: looked up form "[PAD]"
        * mask_id: looked up from "[MASK]"
        * vocab_size: one past the largest token id used
    """
    return self._special_tokens_dict

  def _create_special_tokens_dict(self):
    """Creates dict of token ids, keyed by standard names for their purpose."""
    special_tokens = {"vocab_size": self.vocab_size}

    def add_special_token(key, token):
      try:
        token_id = self._vocab.index(token)
        special_tokens[key] = token_id
      except ValueError:
        # Similar as nlp.modeling.layers.BertTokenizer, if a special token
        # is not found, its entry is omitted from the dict.
        logging.warning("Could not find %s as token \"%s\" in vocab file", key,
                        token)

    add_special_token("start_of_sequence_id", "[CLS]")
    add_special_token("end_of_segment_id", "[SEP]")
    add_special_token("padding_id", "[PAD]")
    add_special_token("mask_id", "[MASK]")
    return special_tokens

  def _tokenize_with_offsets(self, text_input: tf.Tensor):
    tokens, begin, _ = self._basic_tokenizer.tokenize_with_offsets(text_input)
    wordpieces, wp_begin, wp_end = (
        self._fast_wp_tokenizer.tokenize_with_offsets(tokens))
    begin_expanded = tf.expand_dims(begin, axis=2)
    final_begin = begin_expanded + wp_begin
    final_end = begin_expanded + wp_end
    return wordpieces, final_begin, final_end

  def _tokenize(self, text_input: tf.Tensor):
    tokens = self._basic_tokenizer.tokenize(text_input)
    return self._fast_wp_tokenizer.tokenize(tokens)

  def call(self, inputs: tf.Tensor):
    """Calls text.BertTokenizer on inputs.

    Args:
      inputs: A string Tensor of shape [batch_size].

    Returns:
      One or three of RaggedTensors if tokenize_with_offsets is False or True,
      respectively. These are
      tokens: A RaggedTensor of shape [batch_size, (words), (pieces_per_word)]
        and type int32. tokens[i,j,k] contains the k-th wordpiece of the
        j-th word in the i-th input.
      start_offsets, limit_offsets: If tokenize_with_offsets is True,
        RaggedTensors of type int64 with the same indices as tokens.
        Element [i,j,k] contains the byte offset at the start, or past the
        end, resp., for the k-th wordpiece of the j-th word in the i-th input.
    """
    # Prepare to reshape the result to work around broken shape inference.
    batch_size = tf.shape(inputs)[0]

    def _reshape(rt):
      values = rt.values
      row_splits = rt.row_splits
      row_splits = tf.reshape(row_splits, [batch_size + 1])
      return tf.RaggedTensor.from_row_splits(values, row_splits)

    if self.tokenize_with_offsets:
      tokens, start_offsets, limit_offsets = self._tokenize_with_offsets(inputs)
      return _reshape(tokens), _reshape(start_offsets), _reshape(limit_offsets)
    else:
      tokens = self._tokenize(inputs)
      return _reshape(tokens)