xlnet_modeling.py 46.5 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Hongkun Yu's avatar
Hongkun Yu 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

Hongkun Yu's avatar
Hongkun Yu committed
15
16
17
"""Keras layers of XLNet model in TF 2.0."""

import copy
Allen Wang's avatar
Allen Wang committed
18
import warnings
Hongkun Yu's avatar
Hongkun Yu committed
19
20

import tensorflow as tf
Hongkun Yu's avatar
Hongkun Yu committed
21
from official.legacy.xlnet import data_utils
Allen Wang's avatar
Allen Wang committed
22
from official.nlp.modeling import networks
Hongkun Yu's avatar
Hongkun Yu committed
23
24


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
25
26
def gelu(x):
  return tf.keras.activations.gelu(x, approximate=True)
Hongkun Yu's avatar
Hongkun Yu committed
27
28


Allen Wang's avatar
Allen Wang committed
29
30
31
32
33
34
35
36
37
38
39
40
def _get_initializer(flags):
  """Get variable initializer."""
  if flags.init_method == "uniform":
    initializer = tf.keras.initializers.RandomUniform(
        minval=-flags.init_range, maxval=flags.init_range)
  elif flags.init_method == "normal":
    initializer = tf.keras.initializers.RandomNormal(stddev=flags.init_std)
  else:
    raise ValueError("Initializer {} not supported".format(flags.init_method))
  return initializer


Hongkun Yu's avatar
Hongkun Yu committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def rel_shift(x, klen=-1):
  """Performs relative shift to form the relative attention score."""
  x_size = tf.shape(x)

  x = tf.reshape(x, [x_size[1], x_size[0], x_size[2], x_size[3]])
  x = tf.slice(x, [1, 0, 0, 0], [-1, -1, -1, -1])
  x = tf.reshape(x, [x_size[0], x_size[1] - 1, x_size[2], x_size[3]])
  x = tf.slice(x, [0, 0, 0, 0], [-1, klen, -1, -1])

  return x


def _create_mask(qlen, mlen, dtype=tf.float32, same_length=False):
  """Creates attention mask when single-side context allowed only."""
  attn_mask = tf.ones([qlen, qlen], dtype=dtype)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
56
57
  mask_u = tf.linalg.band_part(attn_mask, 0, -1)
  mask_dia = tf.linalg.band_part(attn_mask, 0, 0)
Hongkun Yu's avatar
Hongkun Yu committed
58
59
60
  attn_mask_pad = tf.zeros([qlen, mlen], dtype=dtype)
  ret = tf.concat([attn_mask_pad, mask_u - mask_dia], 1)
  if same_length:
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
61
    mask_l = tf.linalg.band_part(attn_mask, -1, 0)
Hongkun Yu's avatar
Hongkun Yu committed
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
    ret = tf.concat([ret[:, :qlen] + mask_l - mask_dia, ret[:, qlen:]], 1)

  return ret


def _cache_mem(curr_out, prev_mem, mem_len, reuse_len=None):
  """cache hidden states into memory."""

  if mem_len is None or mem_len == 0:
    return None
  else:
    if reuse_len is not None and reuse_len > 0:
      curr_out = curr_out[:reuse_len]

    if prev_mem is None:
      new_mem = curr_out[-mem_len:]
    else:
      new_mem = tf.concat([prev_mem, curr_out], 0)[-mem_len:]

  return tf.keras.backend.stop_gradient(new_mem)


def is_special_none_tensor(tensor):
  """Checks if a tensor is a special None Tensor."""
  return tensor.shape.ndims == 0 and tensor.dtype == tf.int32


Allen Wang's avatar
Allen Wang committed
89
@tf.keras.utils.register_keras_serializable(package="Text")
Allen Wang's avatar
Allen Wang committed
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
class RelativePositionEncoding(tf.keras.layers.Layer):
  """Creates a relative positional encoding.

  This layer creates a relative positional encoding as described in
  "Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context"
  (https://arxiv.org/abs/1901.02860).

  Rather than an absolute position embedding as in Transformer, this
  formulation represents position as the relative distance between tokens using
  sinusoidal positional embeddings.

  Note: This layer is currently experimental.

  Attributes:
    hidden_size: The dimensionality of the input embeddings.
  """

  def __init__(self, hidden_size, **kwargs):
    super(RelativePositionEncoding, self).__init__(**kwargs)
    self._hidden_size = hidden_size
    self._inv_freq = 1.0 / (10000.0**(
        tf.range(0, self._hidden_size, 2.0) / self._hidden_size))

  def call(self, pos_seq, batch_size=None):
    """Implements call() for the layer.

116
    Args:
Allen Wang's avatar
Allen Wang committed
117
118
119
120
121
122
123
124
125
      pos_seq: A 1-D `Tensor`
      batch_size: The optionally provided batch size that tiles the relative
        positional encoding.

    Returns:
      The relative positional encoding of shape:
        [len(pos_seq), batch_size, hidden_size] if batch_size is provided, else
        [len(pos_seq), 1, hidden_size].
    """
Allen Wang's avatar
Allen Wang committed
126
    sinusoid_input = tf.einsum("i,d->id", pos_seq, self._inv_freq)
Allen Wang's avatar
Allen Wang committed
127
    pos_emb = tf.concat([tf.sin(sinusoid_input), tf.cos(sinusoid_input)], -1)
Hongkun Yu's avatar
Hongkun Yu committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    pos_emb = pos_emb[:, None, :]

    if batch_size is not None:
      pos_emb = tf.tile(pos_emb, [1, batch_size, 1])
    return pos_emb


class RelativeAttention(tf.keras.layers.Layer):
  """Core calculations for relative attention."""

  def __init__(self, dropout_att, scale):
    super(RelativeAttention, self).__init__()
    self.scale = scale
    self.dropout_att = dropout_att

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""

    self.attention_probs_dropout = tf.keras.layers.Dropout(
        rate=self.dropout_att)

    super(RelativeAttention, self).build(unused_input_shapes)

Hongkun Yu's avatar
Hongkun Yu committed
151
152
  def call(self, q_head, k_head_h, v_head_h, k_head_r, seg_embed, seg_mat,
           r_w_bias, r_r_bias, r_s_bias, attn_mask):
Hongkun Yu's avatar
Hongkun Yu committed
153
154
155
    """Implements call() for the layer."""

    # content based attention score
Allen Wang's avatar
Allen Wang committed
156
    ac = tf.einsum("ibnd,jbnd->ijbn", q_head + r_w_bias, k_head_h)
Hongkun Yu's avatar
Hongkun Yu committed
157
158

    # position based attention score
Allen Wang's avatar
Allen Wang committed
159
    bd = tf.einsum("ibnd,jbnd->ijbn", q_head + r_r_bias, k_head_r)
Hongkun Yu's avatar
Hongkun Yu committed
160
161
162
163
164
165
    bd = rel_shift(bd, klen=tf.shape(ac)[1])

    # segment-based attention score
    if seg_mat is None:
      ef = 0
    else:
Allen Wang's avatar
Allen Wang committed
166
      ef = tf.einsum("ibnd,snd->isbn", q_head + r_s_bias, seg_embed)
Hongkun Yu's avatar
Hongkun Yu committed
167
168
169
170
171
      tgt_shape = tf.shape(bd)
      ef = tf.where(
          tf.broadcast_to(tf.expand_dims(seg_mat, 3), tgt_shape),
          tf.broadcast_to(ef[:, 1:, :, :], tgt_shape),
          tf.broadcast_to(ef[:, :1, :, :], tgt_shape))
Hongkun Yu's avatar
Hongkun Yu committed
172
173
174
175
176
177
178
179
180
181
182

    # merges attention scores and performs masking
    attn_score = (ac + bd + ef) * self.scale
    if attn_mask is not None:
      attn_score = attn_score - 1e30 * attn_mask

    # attention probability
    attn_prob = tf.nn.softmax(attn_score, 1)
    attn_prob = self.attention_probs_dropout(attn_prob)

    # attention output
Allen Wang's avatar
Allen Wang committed
183
    attn_vec = tf.einsum("ijbn,jbnd->ibnd", attn_prob, v_head_h)
Hongkun Yu's avatar
Hongkun Yu committed
184
185
186
187
188
189
190

    return attn_vec


class PositionwiseFF(tf.keras.layers.Layer):
  """Positionwise feed-forward layer."""

Hongkun Yu's avatar
Hongkun Yu committed
191
192
  def __init__(self, d_model, d_inner, dropout, kernel_initializer,
               activation_type, **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
193
194
195
196
197
198
199
200
201
    super(PositionwiseFF, self).__init__(**kwargs)
    self.d_model = d_model
    self.d_inner = d_inner
    self.dropout = dropout
    self.activation_type = activation_type
    self.kernel_initializer = kernel_initializer

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
Allen Wang's avatar
Allen Wang committed
202
    if self.activation_type == "relu":
Hongkun Yu's avatar
Hongkun Yu committed
203
      activation = tf.nn.relu
Allen Wang's avatar
Allen Wang committed
204
    elif self.activation_type == "gelu":
Hongkun Yu's avatar
Hongkun Yu committed
205
206
      activation = gelu
    else:
Allen Wang's avatar
Allen Wang committed
207
      raise (ValueError("Unsupported activation type {}".format(
Hongkun Yu's avatar
Hongkun Yu committed
208
209
210
211
212
213
          self.activation_type)))
    self.inner_projection_layer = (
        tf.keras.layers.Dense(
            units=self.d_inner,
            activation=activation,
            kernel_initializer=self.kernel_initializer,
Allen Wang's avatar
Allen Wang committed
214
            name="layer_1"))
Hongkun Yu's avatar
Hongkun Yu committed
215
216
217
218
    self.output_projection_layer = (
        tf.keras.layers.Dense(
            units=self.d_model,
            kernel_initializer=self.kernel_initializer,
Allen Wang's avatar
Allen Wang committed
219
            name="layer_2"))
Hongkun Yu's avatar
Hongkun Yu committed
220
    self.output_dropout = tf.keras.layers.Dropout(
Allen Wang's avatar
Allen Wang committed
221
        rate=self.dropout, name="drop_2")
Hongkun Yu's avatar
Hongkun Yu committed
222
223
    self.output_layer_norm = (
        tf.keras.layers.LayerNormalization(
Allen Wang's avatar
Allen Wang committed
224
            name="LayerNorm", axis=-1, epsilon=1e-12))
Hongkun Yu's avatar
Hongkun Yu committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    super(PositionwiseFF, self).build(unused_input_shapes)

  def call(self, inp):
    """Implements call() for the layer."""

    output = self.inner_projection_layer(inp)
    output = self.output_projection_layer(output)
    output = self.output_dropout(output)
    output = self.output_layer_norm(output + inp)
    return output


class EmbeddingLookup(tf.keras.layers.Layer):
  """Looks up words embeddings for id tensor."""

Hongkun Yu's avatar
Hongkun Yu committed
240
  def __init__(self, n_token, d_embed, initializer, **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
241
242
243
244
245
246
247
248
    super(EmbeddingLookup, self).__init__(**kwargs)
    self.n_token = n_token
    self.d_embed = d_embed
    self.initializer = initializer

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
    self.lookup_table = self.add_weight(
Allen Wang's avatar
Allen Wang committed
249
        "lookup_table",
Hongkun Yu's avatar
Hongkun Yu committed
250
251
252
253
254
255
256
        shape=[self.n_token, self.d_embed],
        initializer=self.initializer,
        dtype=self.dtype)

    super(EmbeddingLookup, self).build(unused_input_shapes)

  def call(self, inputs):
Hongkun Yu's avatar
Hongkun Yu committed
257
    return tf.nn.embedding_lookup(self.lookup_table, inputs)
Hongkun Yu's avatar
Hongkun Yu committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274


class RelativeMultiheadAttention(tf.keras.layers.Layer):
  """Multi-head attention with relative embedding."""

  def __init__(self, d_model, n_head, d_head, dropout, dropout_att,
               kernel_initializer, **kwargs):
    super(RelativeMultiheadAttention, self).__init__(**kwargs)
    self.d_model = d_model
    self.n_head = n_head
    self.d_head = d_head
    self.dropout = dropout
    self.dropout_att = dropout_att
    self.initializer = kernel_initializer

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
Hongkun Yu's avatar
Hongkun Yu committed
275
    self.scale = 1.0 / (self.d_head**0.5)
Hongkun Yu's avatar
Hongkun Yu committed
276
277

    self.output_layer_norm = tf.keras.layers.LayerNormalization(
Allen Wang's avatar
Allen Wang committed
278
        name="LayerNorm", axis=-1, epsilon=1e-12)
Hongkun Yu's avatar
Hongkun Yu committed
279
280

    self.kh_projection_layer = self.add_weight(
Allen Wang's avatar
Allen Wang committed
281
        "k/kernel",
Hongkun Yu's avatar
Hongkun Yu committed
282
283
284
        shape=[self.d_model, self.n_head, self.d_head],
        initializer=self.initializer)
    self.vh_projection_layer = self.add_weight(
Allen Wang's avatar
Allen Wang committed
285
        "v/kernel",
Hongkun Yu's avatar
Hongkun Yu committed
286
287
288
        shape=[self.d_model, self.n_head, self.d_head],
        initializer=self.initializer)
    self.kr_projection_layer = self.add_weight(
Allen Wang's avatar
Allen Wang committed
289
        "r/kernel",
Hongkun Yu's avatar
Hongkun Yu committed
290
291
292
        shape=[self.d_model, self.n_head, self.d_head],
        initializer=self.initializer)
    self.qh_projection_layer = self.add_weight(
Allen Wang's avatar
Allen Wang committed
293
        "q/kernel",
Hongkun Yu's avatar
Hongkun Yu committed
294
295
296
        shape=[self.d_model, self.n_head, self.d_head],
        initializer=self.initializer)

297
    self.relative_attention_layer = RelativeAttention(
Hongkun Yu's avatar
Hongkun Yu committed
298
299
300
        dropout_att=self.dropout_att, scale=self.scale)

    self.proj_o = self.add_weight(
Allen Wang's avatar
Allen Wang committed
301
        "o/kernel",
Hongkun Yu's avatar
Hongkun Yu committed
302
303
304
305
306
307
308
        shape=[self.d_model, self.n_head, self.d_head],
        initializer=self.initializer)

    self.attention_dropout = tf.keras.layers.Dropout(rate=self.dropout)

    super(RelativeMultiheadAttention, self).build(unused_input_shapes)

Hongkun Yu's avatar
Hongkun Yu committed
309
310
  def call(self, h, g, r, r_w_bias, r_r_bias, seg_mat, r_s_bias, seg_embed,
           attn_mask_h, attn_mask_g, mems, target_mapping):
Hongkun Yu's avatar
Hongkun Yu committed
311
312
313
314
315
316
317
318
    """Implements call() for the layer."""

    if mems is not None and mems.shape.ndims > 1:
      cat = tf.concat([mems, h], 0)
    else:
      cat = h

    # content heads
Allen Wang's avatar
Allen Wang committed
319
320
321
    q_head_h = tf.einsum("ibh,hnd->ibnd", h, self.qh_projection_layer)
    k_head_h = tf.einsum("ibh,hnd->ibnd", cat, self.kh_projection_layer)
    v_head_h = tf.einsum("ibh,hnd->ibnd", cat, self.vh_projection_layer)
Hongkun Yu's avatar
Hongkun Yu committed
322
323

    # positional heads
Allen Wang's avatar
Allen Wang committed
324
    k_head_r = tf.einsum("ibh,hnd->ibnd", r, self.kr_projection_layer)
Hongkun Yu's avatar
Hongkun Yu committed
325
326

    # core attention ops
Hongkun Yu's avatar
Hongkun Yu committed
327
328
329
330
    attn_vec_h = self.relative_attention_layer(q_head_h, k_head_h, v_head_h,
                                               k_head_r, seg_embed, seg_mat,
                                               r_w_bias, r_r_bias, r_s_bias,
                                               attn_mask_h)
Hongkun Yu's avatar
Hongkun Yu committed
331
332

    # post processing
Allen Wang's avatar
Allen Wang committed
333
    output_h = tf.einsum("ibnd,hnd->ibh", attn_vec_h, self.proj_o)
334
335
    output_h = self.attention_dropout(output_h)
    output_h = self.output_layer_norm(output_h + h)
Hongkun Yu's avatar
Hongkun Yu committed
336

337
338
339
    output_g = None
    if g is not None:  # enable two-stream attention
      # g-stream
Allen Wang's avatar
Allen Wang committed
340
      q_head_g = tf.einsum("ibh,hnd->ibnd", g, self.qh_projection_layer)
341
      if target_mapping is not None:
Allen Wang's avatar
Allen Wang committed
342
        q_head_g = tf.einsum("mbnd,mlb->lbnd", q_head_g, target_mapping)
Hongkun Yu's avatar
Hongkun Yu committed
343
344
345
346
        attn_vec_g = self.relative_attention_layer(q_head_g, k_head_h, v_head_h,
                                                   k_head_r, seg_embed, seg_mat,
                                                   r_w_bias, r_r_bias, r_s_bias,
                                                   attn_mask_g)
Allen Wang's avatar
Allen Wang committed
347
        attn_vec_g = tf.einsum("lbnd,mlb->mbnd", attn_vec_g, target_mapping)
Hongkun Yu's avatar
Hongkun Yu committed
348

349
      else:
Hongkun Yu's avatar
Hongkun Yu committed
350
351
352
353
        attn_vec_g = self.relative_attention_layer(q_head_g, k_head_h, v_head_h,
                                                   k_head_r, seg_embed, seg_mat,
                                                   r_w_bias, r_r_bias, r_s_bias,
                                                   attn_mask_g)
Hongkun Yu's avatar
Hongkun Yu committed
354

355
      # post processing
Allen Wang's avatar
Allen Wang committed
356
      output_g = tf.einsum("ibnd,hnd->ibh", attn_vec_g, self.proj_o)
357
358
359
360
      output_g = self.attention_dropout(output_g)
      output_g = self.output_layer_norm(output_g + g)

    return (output_h, output_g)
Hongkun Yu's avatar
Hongkun Yu committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384


class TransformerXLModel(tf.keras.layers.Layer):
  """Defines a Transformer-XL computation graph with additional support for XLNet."""

  def __init__(self,
               n_token,
               n_layer,
               d_model,
               n_head,
               d_head,
               d_inner,
               dropout,
               dropout_att,
               attn_type,
               bi_data,
               is_training,
               initializer,
               mem_len=None,
               same_length=False,
               clamp_len=-1,
               untie_r=False,
               use_tpu=True,
               reuse_len=None,
Allen Wang's avatar
Allen Wang committed
385
               ff_activation="relu",
Hongkun Yu's avatar
Hongkun Yu committed
386
               use_cls_mask=False,
Hongkun Yu's avatar
Hongkun Yu committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
               **kwargs):
    """Initializes TransformerXLModel.

    Args:
      n_token: int, the number of tokens in vocabulary.
      n_layer: int, the number of layers.
      d_model: int, the hidden size.
      n_head: int, the number of attention heads.
      d_head: int, the dimension size of each attention head.
      d_inner: int, the hidden size in feed-forward layers.
      dropout: float, dropout rate.
      dropout_att: float, dropout rate on attention probabilities.
      attn_type: str, "uni" or "bi".
      bi_data: bool, whether to use bidirectional input pipeline. Usually set to
        True during pretraining and False during finetuning.
      is_training: bool, whether in training mode.
      initializer: A tf initializer.
      mem_len: int, the number of tokens to cache.
      same_length: bool, whether to use the same attention length for each
        token.
      clamp_len: int, clamp all relative distances larger than clamp_len. -1
        means no clamping.
      untie_r: bool, whether to untie the biases in attention.
      use_tpu: bool, whether TPUs are used.
      reuse_len: int, the number of tokens in the currect batch to be cached and
        reused in the future.
      ff_activation: str, "relu" or "gelu".
Hongkun Yu's avatar
Hongkun Yu committed
414
      use_cls_mask: bool, whether to introduce cls mask.
Hongkun Yu's avatar
Hongkun Yu committed
415
416
417
418
      **kwargs: Other parameters.
    """

    super(TransformerXLModel, self).__init__(**kwargs)
Allen Wang's avatar
Allen Wang committed
419
420
421
    warnings.warn(
        "`TransformerXLModel` is deprecated, please use `XLNetBase` instead",
        DeprecationWarning, stacklevel=2)
Hongkun Yu's avatar
Hongkun Yu committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

    self.n_token = n_token
    self.initializer = initializer
    self.attn_type = attn_type
    self.n_layer = n_layer
    self.d_model = d_model
    self.n_head = n_head
    self.d_head = d_head
    self.d_inner = d_inner
    self.ff_activation = ff_activation
    self.untie_r = untie_r
    self.use_tpu = use_tpu
    self.dropout = dropout
    self.dropout_att = dropout_att

    self.mem_len = mem_len
    self.reuse_len = reuse_len
    self.bi_data = bi_data
    self.clamp_len = clamp_len
    self.same_length = same_length
Hongkun Yu's avatar
Hongkun Yu committed
442
    self.use_cls_mask = use_cls_mask
Hongkun Yu's avatar
Hongkun Yu committed
443
444
445

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
Hongkun Yu's avatar
Hongkun Yu committed
446
    self.tf_float = tf.float32
Hongkun Yu's avatar
Hongkun Yu committed
447

Hongkun Yu's avatar
Hongkun Yu committed
448
449
450
451
452
    self.embedding_lookup = EmbeddingLookup(
        n_token=self.n_token,
        d_embed=self.d_model,
        initializer=self.initializer,
        dtype=self.tf_float,
Allen Wang's avatar
Allen Wang committed
453
        name="word_embedding")
Hongkun Yu's avatar
Hongkun Yu committed
454
455
456
457
458
459
460

    self.h_dropout = tf.keras.layers.Dropout(rate=self.dropout)
    self.g_dropout = tf.keras.layers.Dropout(rate=self.dropout)

    if self.untie_r:
      self.r_w_bias = (
          self.add_weight(
Allen Wang's avatar
Allen Wang committed
461
              "r_w_bias",
Hongkun Yu's avatar
Hongkun Yu committed
462
463
464
465
466
              shape=[self.n_layer, self.n_head, self.d_head],
              dtype=self.tf_float,
              initializer=self.initializer))
      self.r_r_bias = (
          self.add_weight(
Allen Wang's avatar
Allen Wang committed
467
              "r_r_bias",
Hongkun Yu's avatar
Hongkun Yu committed
468
469
470
471
472
              shape=[self.n_layer, self.n_head, self.d_head],
              dtype=self.tf_float,
              initializer=self.initializer))
      self.r_s_bias = (
          self.add_weight(
Allen Wang's avatar
Allen Wang committed
473
              "r_s_bias",
Hongkun Yu's avatar
Hongkun Yu committed
474
475
476
477
478
479
              shape=[self.n_layer, self.n_head, self.d_head],
              dtype=self.tf_float,
              initializer=self.initializer))
    else:
      self.r_w_bias = (
          self.add_weight(
Allen Wang's avatar
Allen Wang committed
480
              "r_w_bias",
Hongkun Yu's avatar
Hongkun Yu committed
481
482
483
484
485
              shape=[self.n_head, self.d_head],
              dtype=self.tf_float,
              initializer=self.initializer))
      self.r_r_bias = (
          self.add_weight(
Allen Wang's avatar
Allen Wang committed
486
              "r_r_bias",
Hongkun Yu's avatar
Hongkun Yu committed
487
488
489
490
491
              shape=[self.n_head, self.d_head],
              dtype=self.tf_float,
              initializer=self.initializer))
      self.r_s_bias = (
          self.add_weight(
Allen Wang's avatar
Allen Wang committed
492
              "r_s_bias", [self.n_head, self.d_head],
Hongkun Yu's avatar
Hongkun Yu committed
493
494
495
496
              dtype=self.tf_float,
              initializer=self.initializer))

    self.seg_embed = self.add_weight(
Allen Wang's avatar
Allen Wang committed
497
        "seg_embed", [self.n_layer, 2, self.n_head, self.d_head],
Hongkun Yu's avatar
Hongkun Yu committed
498
499
        dtype=self.tf_float,
        initializer=self.initializer)
Hongkun Yu's avatar
Hongkun Yu committed
500

Hongkun Yu's avatar
Hongkun Yu committed
501
    self.mask_emb = self.add_weight(
Allen Wang's avatar
Allen Wang committed
502
        "mask_emb/mask_emb", shape=[1, 1, self.d_model], dtype=self.tf_float)
Hongkun Yu's avatar
Hongkun Yu committed
503
504

    self.emb_dropout = tf.keras.layers.Dropout(rate=self.dropout)
Allen Wang's avatar
Allen Wang committed
505
506
    self.fwd_position_embedding = RelativePositionEncoding(self.d_model)
    self.bwd_position_embedding = RelativePositionEncoding(self.d_model)
Hongkun Yu's avatar
Hongkun Yu committed
507
508
509
510
511
512
513
514
515
516
517
518

    self.rel_multihead_layers = []
    self.h_positionwise_ffn_layers = []
    for i in range(self.n_layer):
      self.rel_multihead_layers.append(
          RelativeMultiheadAttention(
              d_model=self.d_model,
              dropout=self.dropout,
              n_head=self.n_head,
              d_head=self.d_head,
              dropout_att=self.dropout_att,
              kernel_initializer=self.initializer,
Allen Wang's avatar
Allen Wang committed
519
              name="layer_%d/rel_attn" % (i)))
Hongkun Yu's avatar
Hongkun Yu committed
520
521
522
523
524
525
      self.h_positionwise_ffn_layers.append(
          PositionwiseFF(
              d_model=self.d_model,
              d_inner=self.d_inner,
              dropout=self.dropout,
              kernel_initializer=self.initializer,
Hongkun Yu's avatar
Hongkun Yu committed
526
              activation_type=self.ff_activation,
Allen Wang's avatar
Allen Wang committed
527
              name="layer_%d/ff" % (i)))
Hongkun Yu's avatar
Hongkun Yu committed
528
529
530
531
532
533
534
535
536
537
538
539

    self.output_dropout = tf.keras.layers.Dropout(rate=self.dropout)

    super(TransformerXLModel, self).build(unused_input_shapes)

  def __call__(self,
               inp_k,
               seg_id=None,
               input_mask=None,
               mems=None,
               perm_mask=None,
               target_mapping=None,
540
541
               inp_q=None,
               **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
542
543
    # Uses dict to feed inputs into call() in order to keep mems as a python
    # list.
Hongkun Yu's avatar
Hongkun Yu committed
544
    inputs = {
Allen Wang's avatar
Allen Wang committed
545
546
547
548
549
550
551
        "inp_k": inp_k,
        "seg_id": seg_id,
        "input_mask": input_mask,
        "mems": mems,
        "perm_mask": perm_mask,
        "target_mapping": target_mapping,
        "inp_q": inp_q
Hongkun Yu's avatar
Hongkun Yu committed
552
    }
553
    return super(TransformerXLModel, self).__call__(inputs, **kwargs)
Hongkun Yu's avatar
Hongkun Yu committed
554
555
556

  def call(self, inputs):
    """Implements call() for the layer."""
Allen Wang's avatar
Allen Wang committed
557
558
559
560
561
562
563
    inp_k = inputs["inp_k"]
    seg_id = inputs["seg_id"]
    input_mask = inputs["input_mask"]
    mems = inputs["mems"]
    perm_mask = inputs["perm_mask"]
    target_mapping = inputs["target_mapping"]
    inp_q = inputs["inp_q"]
Hongkun Yu's avatar
Hongkun Yu committed
564
565
566
567
568
569
570
571
572
573
574
575

    new_mems = []

    bsz = tf.shape(inp_k)[1]

    qlen = inp_k.shape.as_list()[0]

    mlen = mems[0].shape.as_list()[0] if mems is not None else 0
    klen = mlen + qlen

    ##### Attention mask
    # causal attention mask
Allen Wang's avatar
Allen Wang committed
576
    if self.attn_type == "uni":
Hongkun Yu's avatar
Hongkun Yu committed
577
578
579
      attn_mask = _create_mask(qlen, mlen, self.tf_float, self.same_length)
      # pylint: enable=protected-access
      attn_mask = attn_mask[:, :, None, None]
Allen Wang's avatar
Allen Wang committed
580
    elif self.attn_type == "bi":
Hongkun Yu's avatar
Hongkun Yu committed
581
582
      attn_mask = None
    else:
Allen Wang's avatar
Allen Wang committed
583
      raise ValueError("Unsupported attention type: {}".format(self.attn_type))
Hongkun Yu's avatar
Hongkun Yu committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610

    # data mask: input mask & perm mask
    if input_mask is not None and perm_mask is not None:
      data_mask = input_mask[None] + perm_mask

    elif input_mask is not None and perm_mask is None:
      data_mask = input_mask[None]
    elif input_mask is None and perm_mask is not None:
      data_mask = perm_mask
    else:
      data_mask = None

    if data_mask is not None:
      # all mems can be attended to
      mems_mask = tf.zeros([tf.shape(data_mask)[0], mlen, bsz],
                           dtype=self.tf_float)
      data_mask = tf.concat([mems_mask, data_mask], 1)
      if attn_mask is None:
        attn_mask = data_mask[:, :, :, None]
      else:
        attn_mask += data_mask[:, :, :, None]

    if attn_mask is not None:
      attn_mask = tf.cast(attn_mask > 0, dtype=self.tf_float)

    if attn_mask is not None:
      non_tgt_mask = -tf.eye(qlen, dtype=self.tf_float)
Hongkun Yu's avatar
Hongkun Yu committed
611
612
613
614
      non_tgt_mask = tf.concat(
          [tf.zeros([qlen, mlen], dtype=self.tf_float), non_tgt_mask], axis=-1)
      non_tgt_mask = tf.cast(
          (attn_mask + non_tgt_mask[:, :, None, None]) > 0, dtype=self.tf_float)
Hongkun Yu's avatar
Hongkun Yu committed
615
616
617
    else:
      non_tgt_mask = None

Hongkun Yu's avatar
Hongkun Yu committed
618
    word_emb_k = self.embedding_lookup(inp_k)
Hongkun Yu's avatar
Hongkun Yu committed
619
620
621
622
623
624
625
626
627
628

    if inp_q is not None:
      if target_mapping is not None:
        word_emb_q = tf.tile(self.mask_emb,
                             [tf.shape(target_mapping)[0], bsz, 1])
      else:
        inp_q_ext = inp_q[:, :, None]
        word_emb_q = inp_q_ext * self.mask_emb + (1 - inp_q_ext) * word_emb_k

    output_h = self.h_dropout(word_emb_k)
629
    output_g = None
Hongkun Yu's avatar
Hongkun Yu committed
630
631
632
633
634
635
636
637
638
639
    if inp_q is not None:
      output_g = self.g_dropout(word_emb_q)

    ##### Segment embedding
    if seg_id is not None:

      # Convert `seg_id` to one-hot `seg_mat`

      mem_pad = tf.zeros([mlen, bsz], dtype=tf.int32)

Hongkun Yu's avatar
Hongkun Yu committed
640
      cat_id = tf.concat([mem_pad, seg_id], 0)
Hongkun Yu's avatar
Hongkun Yu committed
641

Hongkun Yu's avatar
Hongkun Yu committed
642
643
644
645
646
647
648
649
650
651
      if self.use_cls_mask:
        # `1` indicates not in the same segment [qlen x klen x bsz]
        # seg_id: [qlen x bsz] & cat_id: [klen x bsz]
        cls_mat = tf.logical_or(
            tf.equal(seg_id, tf.constant([data_utils.SEG_ID_CLS]))[:, None],
            tf.equal(cat_id, tf.constant([data_utils.SEG_ID_CLS]))[None, :])
        seg_mat = tf.equal(seg_id[:, None], cat_id[None, :])
        seg_mat = tf.logical_or(cls_mat, seg_mat)
      else:
        seg_mat = tf.logical_not(tf.equal(seg_id[:, None], cat_id[None, :]))
Hongkun Yu's avatar
Hongkun Yu committed
652
653
654
655
656
657
658
659
    else:
      seg_mat = None

    dtype = self.tf_float
    freq_seq = tf.range(0, self.d_model, 2.0)
    if dtype is not None and dtype != tf.float32:
      freq_seq = tf.cast(freq_seq, dtype=self.dtype)

Allen Wang's avatar
Allen Wang committed
660
    if self.attn_type == "bi":
Hongkun Yu's avatar
Hongkun Yu committed
661
      beg, end = klen, -qlen
Allen Wang's avatar
Allen Wang committed
662
    elif self.attn_type == "uni":
Hongkun Yu's avatar
Hongkun Yu committed
663
664
      beg, end = klen, -1
    else:
Allen Wang's avatar
Allen Wang committed
665
      raise ValueError("Unknown `attn_type` {}.".format(self.attn_type))
Hongkun Yu's avatar
Hongkun Yu committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681

    if self.bi_data:
      fwd_pos_seq = tf.range(beg, end, -1.0)
      bwd_pos_seq = tf.range(-beg, -end, 1.0)

      if dtype is not None and dtype != tf.float32:
        fwd_pos_seq = tf.cast(fwd_pos_seq, dtype=dtype)
        bwd_pos_seq = tf.cast(bwd_pos_seq, dtype=dtype)

      if self.clamp_len > 0:
        fwd_pos_seq = tf.clip_by_value(fwd_pos_seq, -self.clamp_len,
                                       self.clamp_len)
        bwd_pos_seq = tf.clip_by_value(bwd_pos_seq, -self.clamp_len,
                                       self.clamp_len)

      if bsz is not None:
Hongkun Yu's avatar
Hongkun Yu committed
682
683
        fwd_pos_emb = self.fwd_position_embedding(fwd_pos_seq, bsz // 2)
        bwd_pos_emb = self.bwd_position_embedding(bwd_pos_seq, bsz // 2)
Hongkun Yu's avatar
Hongkun Yu committed
684
685
686
687
688
689
690
691
692
693
      else:
        fwd_pos_emb = self.fwd_position_embedding(fwd_pos_seq, None)
        bwd_pos_emb = self.bwd_position_embedding(bwd_pos_seq, None)

      pos_emb = tf.concat([fwd_pos_emb, bwd_pos_emb], axis=1)
    else:
      fwd_pos_seq = tf.range(beg, end, -1.0)
      if dtype is not None and dtype != tf.float32:
        fwd_pos_seq = tf.cast(fwd_pos_seq, dtype=dtype)
      if self.clamp_len > 0:
Hongkun Yu's avatar
Hongkun Yu committed
694
695
        fwd_pos_seq = tf.clip_by_value(fwd_pos_seq, -self.clamp_len,
                                       self.lamp_len)
Hongkun Yu's avatar
Hongkun Yu committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716

      pos_emb = self.fwd_position_embedding(fwd_pos_seq, bsz)

    pos_emb = self.emb_dropout(pos_emb)

    if mems is None:
      mems = [None] * self.n_layer
    for i in range(self.n_layer):
      # cache new mems
      new_mems.append(
          _cache_mem(output_h, mems[i], self.mem_len, self.reuse_len))
      # pylint: enable=protected-access

      # segment bias
      if seg_id is None:
        r_s_bias_i = None
        seg_embed_i = None
      else:
        r_s_bias_i = self.r_s_bias if not self.untie_r else self.r_s_bias[i]
        seg_embed_i = self.seg_embed[i]

717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
      ffn_layer = self.h_positionwise_ffn_layers[i]
      attention_layer = self.rel_multihead_layers[i]
      output_h, output_g = attention_layer(
          h=output_h,
          g=output_g,
          r=pos_emb,
          r_w_bias=self.r_w_bias if not self.untie_r else self.r_w_bias[i],
          r_r_bias=self.r_r_bias if not self.untie_r else self.r_r_bias[i],
          seg_mat=seg_mat,
          r_s_bias=r_s_bias_i,
          seg_embed=seg_embed_i,
          attn_mask_h=non_tgt_mask,
          attn_mask_g=attn_mask,
          mems=mems[i],
          target_mapping=target_mapping)
      output_h = ffn_layer(output_h)
      if output_g is not None:
        output_g = ffn_layer(output_g)
Hongkun Yu's avatar
Hongkun Yu committed
735
736

    if inp_q is not None:
Hongkun Yu's avatar
Hongkun Yu committed
737
      output = output_g
Hongkun Yu's avatar
Hongkun Yu committed
738
    else:
Hongkun Yu's avatar
Hongkun Yu committed
739
      output = output_h
Hongkun Yu's avatar
Hongkun Yu committed
740
741
742
743
744
745
746
747
748
749
750

    return output, new_mems, None


class PretrainingXLNetModel(tf.keras.Model):
  """XLNet keras model combined with pretraining LM loss layer.

  See the original paper: https://arxiv.org/pdf/1906.08237.pdf

  """

Allen Wang's avatar
Allen Wang committed
751
752
  def __init__(self, use_proj, xlnet_config, run_config, use_legacy_mask=True,
               **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
753
754
755
756
    super(PretrainingXLNetModel, self).__init__(**kwargs)
    self.run_config = run_config
    self.initializer = _get_initializer(run_config)
    self.xlnet_config = copy.deepcopy(xlnet_config)
Allen Wang's avatar
Allen Wang committed
757
    self._use_legacy_mask = use_legacy_mask
Hongkun Yu's avatar
Hongkun Yu committed
758

Allen Wang's avatar
Allen Wang committed
759
760
    self.xlnet_model = networks.XLNetBase(
        vocab_size=self.xlnet_config.n_token,
Hongkun Yu's avatar
Hongkun Yu committed
761
        initializer=self.initializer,
Allen Wang's avatar
Allen Wang committed
762
763
764
765
766
767
768
769
770
771
772
773
774
        attention_type="bi",
        num_layers=self.xlnet_config.n_layer,
        hidden_size=self.xlnet_config.d_model,
        num_attention_heads=self.xlnet_config.n_head,
        head_size=self.xlnet_config.d_head,
        inner_size=self.xlnet_config.d_inner,
        two_stream=True,
        tie_attention_biases=not self.xlnet_config.untie_r,
        inner_activation=self.xlnet_config.ff_activation,
        dropout_rate=self.run_config.dropout,
        attention_dropout_rate=self.run_config.dropout_att,
        memory_length=self.run_config.mem_len,
        reuse_length=self.run_config.reuse_len,
Hongkun Yu's avatar
Hongkun Yu committed
775
        bi_data=self.run_config.bi_data,
Allen Wang's avatar
Allen Wang committed
776
        clamp_length=self.run_config.clamp_len,
Hongkun Yu's avatar
Hongkun Yu committed
777
        use_cls_mask=self.run_config.use_cls_mask,
Allen Wang's avatar
Allen Wang committed
778
779
        name="xlnet_model")

Hongkun Yu's avatar
Hongkun Yu committed
780
    self.lmloss_layer = LMLossLayer(
Allen Wang's avatar
Allen Wang committed
781
782
        vocab_size=self.xlnet_config.n_token,
        hidden_size=self.xlnet_config.d_model,
Hongkun Yu's avatar
Hongkun Yu committed
783
784
785
        initializer=self.initializer,
        tie_weight=True,
        bi_data=self.run_config.bi_data,
Allen Wang's avatar
Allen Wang committed
786
        use_one_hot=self.run_config.use_tpu,
Hongkun Yu's avatar
Hongkun Yu committed
787
        use_proj=use_proj,
Allen Wang's avatar
Allen Wang committed
788
        name="lm_loss")
Hongkun Yu's avatar
Hongkun Yu committed
789
790
791
792

  def call(self, features):
    """Implements call() for the layer."""

Allen Wang's avatar
Allen Wang committed
793
794
795
    input_ids = features["input_ids"]
    masked_tokens = features["input_q"]
    seg_ids = features["seg_id"]
Allen Wang's avatar
Allen Wang committed
796
    if self._use_legacy_mask:
797
798
      # Legacy input mask assumes `real` values are 0 and `padding`
      # values are 1.
Allen Wang's avatar
Allen Wang committed
799
800
801
      perm_mask = 1 - features["perm_mask"]
    else:
      perm_mask = features["perm_mask"]
Allen Wang's avatar
Allen Wang committed
802
    target_mapping = features["target_mapping"]
Hongkun Yu's avatar
Hongkun Yu committed
803
804

    # target for LM loss
Allen Wang's avatar
Allen Wang committed
805
    target = features["target"]
Hongkun Yu's avatar
Hongkun Yu committed
806
807

    # target mask for LM loss
Allen Wang's avatar
Allen Wang committed
808
    tgt_mask = features["target_mask"]
Hongkun Yu's avatar
Hongkun Yu committed
809

Allen Wang's avatar
Allen Wang committed
810
    mems = features.get("mems", None)
Hongkun Yu's avatar
Hongkun Yu committed
811

Allen Wang's avatar
Allen Wang committed
812
813
814
    model_output, self.new_mems = self.xlnet_model(
        input_ids=input_ids,
        segment_ids=seg_ids,
Hongkun Yu's avatar
Hongkun Yu committed
815
        input_mask=None,
Allen Wang's avatar
Allen Wang committed
816
817
        state=mems,
        permutation_mask=perm_mask,
Hongkun Yu's avatar
Hongkun Yu committed
818
        target_mapping=target_mapping,
Allen Wang's avatar
Allen Wang committed
819
        masked_tokens=masked_tokens)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
820
    lm_loss, _ = self.lmloss_layer(
Allen Wang's avatar
Allen Wang committed
821
        hidden=model_output,
Hongkun Yu's avatar
Hongkun Yu committed
822
        target=target,
Allen Wang's avatar
Allen Wang committed
823
        lookup_table=self.xlnet_model.get_embedding_lookup_table(),
Hongkun Yu's avatar
Hongkun Yu committed
824
825
        target_mask=tgt_mask)
    self.add_loss(lm_loss)
Allen Wang's avatar
Allen Wang committed
826
    return self.new_mems, model_output
Hongkun Yu's avatar
Hongkun Yu committed
827
828
829
830
831
832
833
834
835


class ClassificationXLNetModel(tf.keras.Model):
  """XLNet keras model combined with classification loss layer.

  See the original paper: https://arxiv.org/pdf/1906.08237.pdf

  """

Allen Wang's avatar
Allen Wang committed
836
837
  def __init__(self, xlnet_config, run_config, n_class, summary_type,
               use_legacy_mask=True, **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
838
    super(ClassificationXLNetModel, self).__init__(**kwargs)
Allen Wang's avatar
Allen Wang committed
839
840
841
    warnings.warn(
        "`ClassificationXLNetModel` is deprecated, please use `XLNetClassifier`"
        "instead.", DeprecationWarning, stacklevel=2)
Hongkun Yu's avatar
Hongkun Yu committed
842
843
844
    self.run_config = run_config
    self.initializer = _get_initializer(run_config)
    self.xlnet_config = copy.deepcopy(xlnet_config)
Allen Wang's avatar
Allen Wang committed
845
    self._use_legacy_mask = use_legacy_mask
Hongkun Yu's avatar
Hongkun Yu committed
846

Allen Wang's avatar
Allen Wang committed
847
848
    self.xlnet_model = networks.XLNetBase(
        vocab_size=self.xlnet_config.n_token,
Hongkun Yu's avatar
Hongkun Yu committed
849
        initializer=self.initializer,
Allen Wang's avatar
Allen Wang committed
850
851
852
853
854
855
856
857
858
859
860
861
862
        attention_type="bi",
        num_layers=self.xlnet_config.n_layer,
        hidden_size=self.xlnet_config.d_model,
        num_attention_heads=self.xlnet_config.n_head,
        head_size=self.xlnet_config.d_head,
        inner_size=self.xlnet_config.d_inner,
        two_stream=False,
        tie_attention_biases=not self.xlnet_config.untie_r,
        inner_activation=self.xlnet_config.ff_activation,
        dropout_rate=self.run_config.dropout,
        attention_dropout_rate=self.run_config.dropout_att,
        memory_length=self.run_config.mem_len,
        reuse_length=self.run_config.reuse_len,
Hongkun Yu's avatar
Hongkun Yu committed
863
        bi_data=self.run_config.bi_data,
Allen Wang's avatar
Allen Wang committed
864
865
866
        clamp_length=self.run_config.clamp_len,
        use_cls_mask=False,
        name="xlnet_model")
Hongkun Yu's avatar
Hongkun Yu committed
867
868

    self.summarization_layer = Summarization(
Allen Wang's avatar
Allen Wang committed
869
870
871
872
873
        hidden_size=self.xlnet_config.d_model,
        num_attention_heads=self.xlnet_config.n_head,
        head_size=self.xlnet_config.d_head,
        dropout_rate=self.run_config.dropout,
        attention_dropout_rate=self.run_config.dropout_att,
Hongkun Yu's avatar
Hongkun Yu committed
874
875
        initializer=self.initializer,
        use_proj=True,
Hongkun Yu's avatar
Hongkun Yu committed
876
        summary_type=summary_type,
Allen Wang's avatar
Allen Wang committed
877
        name="sequence_summary")
Hongkun Yu's avatar
Hongkun Yu committed
878
879

    self.cl_loss_layer = ClassificationLossLayer(
Allen Wang's avatar
Allen Wang committed
880
        n_class=n_class, initializer=self.initializer, name="classification")
Hongkun Yu's avatar
Hongkun Yu committed
881
882
883

  def call(self, features):
    """Implements call() for the layer."""
Allen Wang's avatar
Allen Wang committed
884
    batch_size_per_core = tf.shape(features["input_ids"])[0]
Hongkun Yu's avatar
Hongkun Yu committed
885

Allen Wang's avatar
Allen Wang committed
886
887
    input_ids = features["input_ids"]
    segment_ids = features["segment_ids"]
Allen Wang's avatar
Allen Wang committed
888
    if self._use_legacy_mask:
889
890
      # Legacy input mask assumes `real` values are 0 and `padding`
      # values are 1.
Allen Wang's avatar
Allen Wang committed
891
892
893
      input_mask = 1 - features["input_mask"]
    else:
      input_mask = features["input_mask"]
Hongkun Yu's avatar
Hongkun Yu committed
894

Allen Wang's avatar
Allen Wang committed
895
    label = tf.reshape(features["label_ids"], [batch_size_per_core])
Hongkun Yu's avatar
Hongkun Yu committed
896

Allen Wang's avatar
Allen Wang committed
897
    mems = features.get("mems", None)
Hongkun Yu's avatar
Hongkun Yu committed
898

Allen Wang's avatar
Allen Wang committed
899
900
    attention_output, new_mems = (
        self.xlnet_model(input_ids, segment_ids, input_mask, mems))
Hongkun Yu's avatar
Hongkun Yu committed
901

Allen Wang's avatar
Allen Wang committed
902
    summary = self.summarization_layer(attention_output)
Hongkun Yu's avatar
Hongkun Yu committed
903
    per_example_loss, logits = self.cl_loss_layer(hidden=summary, labels=label)
Hongkun Yu's avatar
Hongkun Yu committed
904
    self.add_loss(tf.keras.backend.mean(per_example_loss))
Hongkun Yu's avatar
Hongkun Yu committed
905
    return new_mems, logits
Hongkun Yu's avatar
Hongkun Yu committed
906
907
908
909
910


class LMLossLayer(tf.keras.layers.Layer):
  """Layer computing cross entropy loss for language modeling."""

Hongkun Yu's avatar
Hongkun Yu committed
911
  def __init__(self,
Allen Wang's avatar
Allen Wang committed
912
913
               vocab_size,
               hidden_size,
Hongkun Yu's avatar
Hongkun Yu committed
914
915
916
               initializer,
               tie_weight=False,
               bi_data=True,
Allen Wang's avatar
Allen Wang committed
917
               use_one_hot=False,
Hongkun Yu's avatar
Hongkun Yu committed
918
919
               use_proj=False,
               **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
920
921
922
    """Constructs LMLoss layer.

    Args:
Allen Wang's avatar
Allen Wang committed
923
924
      vocab_size: Number of tokens in vocabulary.
      hidden_size: The dimension of model hidden state.
Hongkun Yu's avatar
Hongkun Yu committed
925
926
      initializer: Initializer used for parameters.
      tie_weight: Whether to share weights between embedding lookup layer and
Hongkun Yu's avatar
Hongkun Yu committed
927
928
929
        next-token prediction layer.
      bi_data: Whether to use bidirectional input pipeline. Usually set to True
        during pretraining and False during finetuning.
Allen Wang's avatar
Allen Wang committed
930
931
      use_one_hot: bool, whether to use one hot encodings. This should be used
        when TPUs are used.
Hongkun Yu's avatar
Hongkun Yu committed
932
      use_proj: bool, whether to add a projection layer before LM prediction.
Hongkun Yu's avatar
Hongkun Yu committed
933
934
935
      **kwargs: Other parameters.
    """
    super(LMLossLayer, self).__init__(**kwargs)
Allen Wang's avatar
Allen Wang committed
936
937
    self.vocab_size = vocab_size
    self.hidden_size = hidden_size
Hongkun Yu's avatar
Hongkun Yu committed
938
939
940
941
    self.initializer = initializer

    self.tie_weight = tie_weight
    self.bi_data = bi_data
Allen Wang's avatar
Allen Wang committed
942
    self.use_one_hot = use_one_hot
Hongkun Yu's avatar
Hongkun Yu committed
943
    self.use_proj = use_proj
Hongkun Yu's avatar
Hongkun Yu committed
944
945
946

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
Hongkun Yu's avatar
Hongkun Yu committed
947
948
    if self.use_proj:
      self.proj_layer = tf.keras.layers.Dense(
Allen Wang's avatar
Allen Wang committed
949
          units=self.hidden_size,
Hongkun Yu's avatar
Hongkun Yu committed
950
951
          kernel_initializer=self.initializer,
          activation=gelu,
Allen Wang's avatar
Allen Wang committed
952
          name="lm_projection/dense")
Hongkun Yu's avatar
Hongkun Yu committed
953
      self.proj_layer_norm = tf.keras.layers.LayerNormalization(
Allen Wang's avatar
Allen Wang committed
954
          axis=-1, epsilon=1e-12, name="lm_projection/LayerNorm")
Hongkun Yu's avatar
Hongkun Yu committed
955
    if not self.tie_weight:
Hongkun Yu's avatar
Hongkun Yu committed
956
      self.softmax_w = self.add_weight(
Allen Wang's avatar
Allen Wang committed
957
958
          "weight",
          shape=[self.vocab_size, self.hidden_size],
Hongkun Yu's avatar
Hongkun Yu committed
959
          initializer=self.initializer)
Hongkun Yu's avatar
Hongkun Yu committed
960

Hongkun Yu's avatar
Hongkun Yu committed
961
    self.softmax_b = self.add_weight(
Allen Wang's avatar
Allen Wang committed
962
        "bias", shape=[self.vocab_size], initializer=tf.zeros_initializer())
Hongkun Yu's avatar
Hongkun Yu committed
963
964
965

    super(LMLossLayer, self).build(unused_input_shapes)

Hongkun Yu's avatar
Hongkun Yu committed
966
  def call(self, hidden, target, lookup_table, target_mask):
Hongkun Yu's avatar
Hongkun Yu committed
967
    """Implements call() for the layer."""
Hongkun Yu's avatar
Hongkun Yu committed
968
969
    if self.use_proj:
      hidden = self.proj_layer_norm(self.proj_layer(hidden))
Hongkun Yu's avatar
Hongkun Yu committed
970
    if self.tie_weight:
Allen Wang's avatar
Allen Wang committed
971
      logits = tf.einsum("ibd,nd->ibn", hidden, lookup_table) + self.softmax_b
Hongkun Yu's avatar
Hongkun Yu committed
972
    else:
Allen Wang's avatar
Allen Wang committed
973
      logits = tf.einsum("ibd,nd->ibn", hidden, self.softmax_w) + self.softmax_b
Hongkun Yu's avatar
Hongkun Yu committed
974

Allen Wang's avatar
Allen Wang committed
975
976
    if self.use_one_hot:
      one_hot_target = tf.one_hot(target, self.vocab_size, dtype=logits.dtype)
Hongkun Yu's avatar
Hongkun Yu committed
977
978
      loss = -tf.reduce_sum(tf.nn.log_softmax(logits) * one_hot_target, -1)
    else:
Hongkun Yu's avatar
Hongkun Yu committed
979
980
      loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
          labels=target, logits=logits)
Hongkun Yu's avatar
Hongkun Yu committed
981

Hongkun Yu's avatar
Hongkun Yu committed
982
    total_loss = tf.reduce_sum(loss * target_mask) / tf.reduce_sum(target_mask)
Hongkun Yu's avatar
Hongkun Yu committed
983

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
984
    return total_loss, logits
Hongkun Yu's avatar
Hongkun Yu committed
985
986
987
988
989
990


class Summarization(tf.keras.layers.Layer):
  """The layer to pool the output from XLNet model into a vector."""

  def __init__(self,
Allen Wang's avatar
Allen Wang committed
991
992
993
994
995
               hidden_size,
               num_attention_heads,
               head_size,
               dropout_rate,
               attention_dropout_rate,
Hongkun Yu's avatar
Hongkun Yu committed
996
997
               initializer,
               use_proj=True,
Allen Wang's avatar
Allen Wang committed
998
               summary_type="last",
Hongkun Yu's avatar
Hongkun Yu committed
999
1000
1001
1002
               **kwargs):
    """Constructs Summarization layer.

    Args:
Allen Wang's avatar
Allen Wang committed
1003
1004
1005
1006
1007
      hidden_size: int, the dimension of model hidden state.
      num_attention_heads: int, the number of attention heads.
      head_size: int, the dimension size of each attention head.
      dropout_rate: float, dropout rate.
      attention_dropout_rate: float, dropout rate on attention probabilities.
Hongkun Yu's avatar
Hongkun Yu committed
1008
1009
1010
1011
1012
1013
      initializer: Initializer used for parameters.
      use_proj: bool, whether to use projection layer for summarization.
      summary_type: Method used to summarize a sequence into a compact vector.
      **kwargs: Other parameters.
    """
    super(Summarization, self).__init__(**kwargs)
Allen Wang's avatar
Allen Wang committed
1014
1015
1016
    self.hidden_size = hidden_size
    self.num_attention_heads = num_attention_heads
    self.head_size = head_size
Hongkun Yu's avatar
Hongkun Yu committed
1017
1018
    self.initializer = initializer

Allen Wang's avatar
Allen Wang committed
1019
1020
    self.dropout_rate = dropout_rate
    self.attention_dropout_rate = attention_dropout_rate
Hongkun Yu's avatar
Hongkun Yu committed
1021
1022
1023
1024
1025
1026
1027
    self.use_proj = use_proj
    self.summary_type = summary_type

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
    if self.use_proj:
      self.proj_layer = tf.keras.layers.Dense(
Allen Wang's avatar
Allen Wang committed
1028
          units=self.hidden_size,
Hongkun Yu's avatar
Hongkun Yu committed
1029
1030
          kernel_initializer=self.initializer,
          activation=tf.nn.tanh,
Allen Wang's avatar
Allen Wang committed
1031
1032
          name="summary")
    self.dropout_layer = tf.keras.layers.Dropout(rate=self.dropout_rate)
Hongkun Yu's avatar
Hongkun Yu committed
1033
1034
1035
1036
1037

    super(Summarization, self).build(unused_input_shapes)

  def call(self, inputs):
    """Implements call() for the layer."""
Allen Wang's avatar
Allen Wang committed
1038
1039
1040
1041
    if self.summary_type == "last":
      summary = inputs[:, -1, :]
    elif self.summary_type == "first":
      summary = inputs[:, 0, :]
Hongkun Yu's avatar
Hongkun Yu committed
1042
    else:
Allen Wang's avatar
Allen Wang committed
1043
      raise ValueError("Invalid summary type provided: %s" % self.summary_type)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1044
1045
    if self.use_proj:
      summary = self.proj_layer(summary)
Hongkun Yu's avatar
Hongkun Yu committed
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
    summary = self.dropout_layer(summary)
    return summary


class ClassificationLossLayer(tf.keras.layers.Layer):
  """Layer computing cross entropy loss for classification task."""

  def __init__(self, n_class, initializer, **kwargs):
    """Constructs Summarization layer.

    Args:
      n_class: Number of tokens in vocabulary.
      initializer: Initializer used for parameters.
      **kwargs: Other parameters.
    """
    super(ClassificationLossLayer, self).__init__(**kwargs)

    self.n_class = n_class
    self.initializer = initializer

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
    self.proj_layer = tf.keras.layers.Dense(
Allen Wang's avatar
Allen Wang committed
1069
        units=self.n_class, kernel_initializer=self.initializer, name="logit")
Hongkun Yu's avatar
Hongkun Yu committed
1070
1071
1072

    super(ClassificationLossLayer, self).build(unused_input_shapes)

Hongkun Yu's avatar
Hongkun Yu committed
1073
  def call(self, hidden, labels):
Hongkun Yu's avatar
Hongkun Yu committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
    """Implements call() for the layer."""

    logits = self.proj_layer(hidden)
    one_hot_target = tf.one_hot(labels, self.n_class, dtype=hidden.dtype)  # pytype: disable=attribute-error
    loss = -tf.reduce_sum(tf.nn.log_softmax(logits) * one_hot_target, -1)

    return loss, logits


class QAXLNetModel(tf.keras.Model):
  """XLNet keras model combined with question answering loss layer.

  See the original paper: https://arxiv.org/pdf/1906.08237.pdf

  """

  def __init__(self, xlnet_config, run_config, start_n_top, end_n_top,
Allen Wang's avatar
Allen Wang committed
1091
               use_legacy_mask=True, **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
1092
    super(QAXLNetModel, self).__init__(**kwargs)
Allen Wang's avatar
Allen Wang committed
1093
1094
1095
    warnings.warn(
        "`QAXLNetModel` is deprecated, please use `XLNetSpanLabeler` instead.",
        DeprecationWarning, stacklevel=2)
Hongkun Yu's avatar
Hongkun Yu committed
1096
1097
1098
    self.run_config = run_config
    self.initializer = _get_initializer(run_config)
    self.xlnet_config = copy.deepcopy(xlnet_config)
Allen Wang's avatar
Allen Wang committed
1099
    self._use_legacy_mask = use_legacy_mask
Hongkun Yu's avatar
Hongkun Yu committed
1100

Allen Wang's avatar
Allen Wang committed
1101
1102
    self.xlnet_model = networks.XLNetBase(
        vocab_size=self.xlnet_config.n_token,
Hongkun Yu's avatar
Hongkun Yu committed
1103
        initializer=self.initializer,
Allen Wang's avatar
Allen Wang committed
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
        attention_type="bi",
        num_layers=self.xlnet_config.n_layer,
        hidden_size=self.xlnet_config.d_model,
        num_attention_heads=self.xlnet_config.n_head,
        head_size=self.xlnet_config.d_head,
        inner_size=self.xlnet_config.d_inner,
        tie_attention_biases=not self.xlnet_config.untie_r,
        inner_activation=self.xlnet_config.ff_activation,
        dropout_rate=self.run_config.dropout,
        attention_dropout_rate=self.run_config.dropout_att,
        two_stream=False,
        memory_length=self.run_config.mem_len,
        reuse_length=self.run_config.reuse_len,
Hongkun Yu's avatar
Hongkun Yu committed
1117
        bi_data=self.run_config.bi_data,
Allen Wang's avatar
Allen Wang committed
1118
1119
1120
        clamp_length=self.run_config.clamp_len,
        use_cls_mask=False,
        name="xlnet_model")
Hongkun Yu's avatar
Hongkun Yu committed
1121
1122

    self.qa_loss_layer = QALossLayer(
Allen Wang's avatar
Allen Wang committed
1123
        hidden_size=self.xlnet_config.d_model,
Hongkun Yu's avatar
Hongkun Yu committed
1124
1125
1126
        start_n_top=start_n_top,
        end_n_top=end_n_top,
        initializer=self.initializer,
Allen Wang's avatar
Allen Wang committed
1127
1128
        dropout_rate=self.run_config.dropout,
        name="qa_loss_layer")
Hongkun Yu's avatar
Hongkun Yu committed
1129
1130
1131
1132

  def call(self, features, training=False):
    """Implements call() for the layer."""

Allen Wang's avatar
Allen Wang committed
1133
1134
    input_ids = features["input_ids"]
    segment_ids = features["segment_ids"]
Allen Wang's avatar
Allen Wang committed
1135
    if self._use_legacy_mask:
1136
1137
      # Legacy input mask assumes `real` values are 0 and `padding`
      # values are 1.
Allen Wang's avatar
Allen Wang committed
1138
1139
1140
      input_mask = 1 - features["input_mask"]
    else:
      input_mask = features["input_mask"]
Hongkun Yu's avatar
Hongkun Yu committed
1141

Allen Wang's avatar
Allen Wang committed
1142
1143
    cls_index = tf.reshape(features["cls_index"], [-1])
    p_mask = features["p_mask"]
Hongkun Yu's avatar
Hongkun Yu committed
1144

Allen Wang's avatar
Allen Wang committed
1145
1146
    attention_output, new_mems = (
        self.xlnet_model(input_ids, segment_ids, input_mask))
Hongkun Yu's avatar
Hongkun Yu committed
1147
1148
1149

    if training:
      loss, logits = self.qa_loss_layer(
Allen Wang's avatar
Allen Wang committed
1150
          hidden=attention_output,
Hongkun Yu's avatar
Hongkun Yu committed
1151
1152
          p_mask=p_mask,
          cls_index=cls_index,
Allen Wang's avatar
Allen Wang committed
1153
1154
1155
          start_positions=features["start_positions"],
          end_positions=features["end_positions"],
          is_impossible=features["is_impossible"])
Hongkun Yu's avatar
Hongkun Yu committed
1156
      self.add_loss(loss)
Hongkun Yu's avatar
Hongkun Yu committed
1157
      return new_mems, logits
Hongkun Yu's avatar
Hongkun Yu committed
1158
1159
    else:
      results = self.qa_loss_layer(
Allen Wang's avatar
Allen Wang committed
1160
          hidden=attention_output, p_mask=p_mask, cls_index=cls_index)
Hongkun Yu's avatar
Hongkun Yu committed
1161
1162
1163
1164
      return results


class QALossLayer(tf.keras.layers.Layer):
Hongkun Yu's avatar
Hongkun Yu committed
1165
  """Layer computing position and regression loss for question answering task."""
Hongkun Yu's avatar
Hongkun Yu committed
1166

Allen Wang's avatar
Allen Wang committed
1167
1168
  def __init__(self, hidden_size, start_n_top, end_n_top, initializer,
               dropout_rate, **kwargs):
Hongkun Yu's avatar
Hongkun Yu committed
1169
1170
1171
    """Constructs Summarization layer.

    Args:
Allen Wang's avatar
Allen Wang committed
1172
      hidden_size: Int, the hidden size.
Hongkun Yu's avatar
Hongkun Yu committed
1173
1174
1175
      start_n_top: Beam size for span start.
      end_n_top: Beam size for span end.
      initializer: Initializer used for parameters.
Allen Wang's avatar
Allen Wang committed
1176
      dropout_rate: float, dropout rate.
Hongkun Yu's avatar
Hongkun Yu committed
1177
1178
1179
      **kwargs: Other parameters.
    """
    super(QALossLayer, self).__init__(**kwargs)
Allen Wang's avatar
Allen Wang committed
1180
    self.hidden_size = hidden_size
Hongkun Yu's avatar
Hongkun Yu committed
1181
1182
1183
    self.start_n_top = start_n_top
    self.end_n_top = end_n_top
    self.initializer = initializer
Allen Wang's avatar
Allen Wang committed
1184
    self.dropout_rate = dropout_rate
Hongkun Yu's avatar
Hongkun Yu committed
1185
1186
1187
1188

  def build(self, unused_input_shapes):
    """Implements build() for the layer."""
    self.start_logits_proj_layer = tf.keras.layers.Dense(
Allen Wang's avatar
Allen Wang committed
1189
        units=1, kernel_initializer=self.initializer, name="start_logits/dense")
Hongkun Yu's avatar
Hongkun Yu committed
1190
    self.end_logits_proj_layer0 = tf.keras.layers.Dense(
Allen Wang's avatar
Allen Wang committed
1191
        units=self.hidden_size,
Hongkun Yu's avatar
Hongkun Yu committed
1192
1193
        kernel_initializer=self.initializer,
        activation=tf.nn.tanh,
Allen Wang's avatar
Allen Wang committed
1194
        name="end_logits/dense_0")
Hongkun Yu's avatar
Hongkun Yu committed
1195
    self.end_logits_proj_layer1 = tf.keras.layers.Dense(
Allen Wang's avatar
Allen Wang committed
1196
        units=1, kernel_initializer=self.initializer, name="end_logits/dense_1")
Hongkun Yu's avatar
Hongkun Yu committed
1197
    self.end_logits_layer_norm = tf.keras.layers.LayerNormalization(
Allen Wang's avatar
Allen Wang committed
1198
        axis=-1, epsilon=1e-12, name="end_logits/LayerNorm")
Hongkun Yu's avatar
Hongkun Yu committed
1199
    self.answer_class_proj_layer0 = tf.keras.layers.Dense(
Allen Wang's avatar
Allen Wang committed
1200
        units=self.hidden_size,
Hongkun Yu's avatar
Hongkun Yu committed
1201
1202
        kernel_initializer=self.initializer,
        activation=tf.nn.tanh,
Allen Wang's avatar
Allen Wang committed
1203
        name="answer_class/dense_0")
Hongkun Yu's avatar
Hongkun Yu committed
1204
1205
1206
1207
    self.answer_class_proj_layer1 = tf.keras.layers.Dense(
        units=1,
        kernel_initializer=self.initializer,
        use_bias=False,
Allen Wang's avatar
Allen Wang committed
1208
1209
        name="answer_class/dense_1")
    self.ans_feature_dropout = tf.keras.layers.Dropout(rate=self.dropout_rate)
Hongkun Yu's avatar
Hongkun Yu committed
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
    super(QALossLayer, self).build(unused_input_shapes)

  def __call__(self, hidden, p_mask, cls_index, **kwargs):
    return super(QALossLayer, self).__call__(
        (hidden, p_mask, cls_index, kwargs))

  def call(self, inputs, training=False):
    """Implements call() for the layer."""
    hidden, p_mask, cls_index, kwargs = inputs
    return_dict = {}
Allen Wang's avatar
Allen Wang committed
1220
    seq_len = tf.shape(hidden)[1]
Hongkun Yu's avatar
Hongkun Yu committed
1221

Allen Wang's avatar
Allen Wang committed
1222
    hidden = tf.transpose(hidden, [1, 0, 2])
Hongkun Yu's avatar
Hongkun Yu committed
1223
1224
1225
1226
1227
    start_logits = self.start_logits_proj_layer(hidden)
    start_logits = tf.transpose(tf.squeeze(start_logits, -1), [1, 0])
    start_logits_masked = start_logits * (1 - p_mask) - 1e30 * p_mask
    start_log_probs = tf.nn.log_softmax(start_logits_masked, -1)
    if training:
Allen Wang's avatar
Allen Wang committed
1228
1229
1230
      start_positions = kwargs["start_positions"]
      end_positions = kwargs["end_positions"]
      is_impossible = kwargs["is_impossible"]
Hongkun Yu's avatar
Hongkun Yu committed
1231
1232
1233
      start_positions = tf.reshape(start_positions, [-1])
      start_index = tf.one_hot(
          start_positions, depth=seq_len, axis=-1, dtype=tf.float32)
Allen Wang's avatar
Allen Wang committed
1234
      start_features = tf.einsum("lbh,bl->bh", hidden, start_index)
Hongkun Yu's avatar
Hongkun Yu committed
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
      start_features = tf.tile(start_features[None], [seq_len, 1, 1])
      end_logits = self.end_logits_proj_layer0(
          tf.concat([hidden, start_features], axis=-1))

      end_logits = self.end_logits_layer_norm(end_logits)

      end_logits = self.end_logits_proj_layer1(end_logits)
      end_logits = tf.transpose(tf.squeeze(end_logits, -1), [1, 0])
      end_logits_masked = end_logits * (1 - p_mask) - 1e30 * p_mask
      end_log_probs = tf.nn.log_softmax(end_logits_masked, -1)
    else:
      # during inference, compute the end logits based on beam search

      start_top_log_probs, start_top_index = tf.nn.top_k(
          start_log_probs, k=self.start_n_top)
      start_index = tf.one_hot(
          start_top_index, depth=seq_len, axis=-1, dtype=tf.float32)
Allen Wang's avatar
Allen Wang committed
1252
      start_features = tf.einsum("lbh,bkl->bkh", hidden, start_index)
Hongkun Yu's avatar
Hongkun Yu committed
1253
1254
1255
1256
      end_input = tf.tile(hidden[:, :, None], [1, 1, self.start_n_top, 1])
      start_features = tf.tile(start_features[None], [seq_len, 1, 1, 1])
      end_input = tf.concat([end_input, start_features], axis=-1)
      end_logits = self.end_logits_proj_layer0(end_input)
Allen Wang's avatar
Allen Wang committed
1257
      end_logits = tf.reshape(end_logits, [seq_len, -1, self.hidden_size])
Hongkun Yu's avatar
Hongkun Yu committed
1258
1259
1260
      end_logits = self.end_logits_layer_norm(end_logits)

      end_logits = tf.reshape(end_logits,
Allen Wang's avatar
Allen Wang committed
1261
                              [seq_len, -1, self.start_n_top, self.hidden_size])
Hongkun Yu's avatar
Hongkun Yu committed
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276

      end_logits = self.end_logits_proj_layer1(end_logits)
      end_logits = tf.reshape(end_logits, [seq_len, -1, self.start_n_top])
      end_logits = tf.transpose(end_logits, [1, 2, 0])
      end_logits_masked = end_logits * (
          1 - p_mask[:, None]) - 1e30 * p_mask[:, None]
      end_log_probs = tf.nn.log_softmax(end_logits_masked, -1)
      end_top_log_probs, end_top_index = tf.nn.top_k(
          end_log_probs, k=self.end_n_top)
      end_top_log_probs = tf.reshape(end_top_log_probs,
                                     [-1, self.start_n_top * self.end_n_top])
      end_top_index = tf.reshape(end_top_index,
                                 [-1, self.start_n_top * self.end_n_top])

    if training:
Allen Wang's avatar
Allen Wang committed
1277
1278
      return_dict["start_log_probs"] = start_log_probs
      return_dict["end_log_probs"] = end_log_probs
Hongkun Yu's avatar
Hongkun Yu committed
1279
    else:
Allen Wang's avatar
Allen Wang committed
1280
1281
1282
1283
      return_dict["start_top_log_probs"] = start_top_log_probs
      return_dict["start_top_index"] = start_top_index
      return_dict["end_top_log_probs"] = end_top_log_probs
      return_dict["end_top_index"] = end_top_index
Hongkun Yu's avatar
Hongkun Yu committed
1284
1285
1286
1287
    # an additional layer to predict answerability

    # get the representation of CLS
    cls_index = tf.one_hot(cls_index, seq_len, axis=-1, dtype=tf.float32)
Allen Wang's avatar
Allen Wang committed
1288
    cls_feature = tf.einsum("lbh,bl->bh", hidden, cls_index)
Hongkun Yu's avatar
Hongkun Yu committed
1289
1290

    # get the representation of START
Allen Wang's avatar
Allen Wang committed
1291
1292
    start_p = tf.nn.softmax(start_logits_masked, axis=-1, name="softmax_start")
    start_feature = tf.einsum("lbh,bl->bh", hidden, start_p)
Hongkun Yu's avatar
Hongkun Yu committed
1293
1294
1295
1296
1297
1298

    ans_feature = tf.concat([start_feature, cls_feature], -1)
    ans_feature = self.answer_class_proj_layer0(ans_feature)
    ans_feature = self.ans_feature_dropout(ans_feature)
    cls_logits = self.answer_class_proj_layer1(ans_feature)
    cls_logits = tf.squeeze(cls_logits, -1)
Allen Wang's avatar
Allen Wang committed
1299
    return_dict["cls_logits"] = cls_logits
Hongkun Yu's avatar
Hongkun Yu committed
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322

    if not training:
      return return_dict

    def compute_loss(log_probs, positions):
      one_hot_positions = tf.one_hot(positions, depth=seq_len, dtype=tf.float32)

      loss = -tf.reduce_sum(one_hot_positions * log_probs, axis=-1)
      loss = tf.reduce_mean(loss)
      return loss

    start_loss = compute_loss(start_log_probs, start_positions)
    end_loss = compute_loss(end_log_probs, end_positions)

    total_loss = (start_loss + end_loss) * 0.5

    is_impossible = tf.reshape(is_impossible, [-1])
    regression_loss = tf.nn.sigmoid_cross_entropy_with_logits(
        labels=is_impossible, logits=cls_logits)
    regression_loss = tf.reduce_mean(regression_loss)

    total_loss += regression_loss * 0.5
    return total_loss, cls_logits