| 417 | return vit_enc_out, vit_seq_len, n_head_self_attn_mask, checkpoints |
| 418 | |
| 419 | def text_encode(self, text_input, text_mask, gather_idx=None, decoding_step=False): |
| 420 | assert text_mask is not None, "text_mask should not be none" |
| 421 | if decoding_step: |
| 422 | text_self_attn_mask = text_mask |
| 423 | else: |
| 424 | text_self_attn_mask = paddle.matmul(x=paddle.transpose(text_mask, perm=[0, 2, 1]), y=text_mask) |
| 425 | |
| 426 | self_attn_mask = paddle.scale( |
| 427 | x=text_self_attn_mask, scale=1e4, bias=-1.0, bias_after_scale=False) |
| 428 | n_head_self_attn_mask = paddle.stack( |
| 429 | x=[self_attn_mask] * self._n_head, axis=1) |
| 430 | n_head_self_attn_mask.stop_gradient = True |
| 431 | |
| 432 | # text part |
| 433 | text_emb = paddle.static.nn.embedding( |
| 434 | input=text_input["text.word_embedding"], |
| 435 | size=[self._text_emb_vocab_size["text.word_embedding"], self._emb_size], |
| 436 | dtype=self._emb_dtype, |
| 437 | param_attr=paddle.ParamAttr( |
| 438 | name='text.word_embedding', |
| 439 | trainable=self._text_encoder_trainable, |
| 440 | initializer=self._param_initializer)) |
| 441 | text_emb = paddle.squeeze(text_emb, axis=2) # (batch_size, seq_len, emb_dim) |
| 442 | |
| 443 | pos_emb = paddle.static.nn.embedding( |
| 444 | input=text_input["text.pos_embedding"], |
| 445 | size=[self._text_emb_vocab_size["text.pos_embedding"], self._emb_size], |
| 446 | dtype=self._emb_dtype, |
| 447 | param_attr=paddle.ParamAttr( |
| 448 | name='text.pos_embedding', |
| 449 | trainable=self._text_encoder_trainable, |
| 450 | initializer=self._param_initializer)) |
| 451 | pos_emb = paddle.squeeze(pos_emb, axis=2) # (batch_size, seq_len, emb_dim) |
| 452 | |
| 453 | emb_out = text_emb + pos_emb |
| 454 | emb_out = text_pre_process_layer( |
| 455 | emb_out, 'nd', self._text_prepostprocess_dropout, |
| 456 | name="text.pre_encoder", trainable=self._text_encoder_trainable) |
| 457 | |
| 458 | text_enc_out, checkpoints = text_encoder( |
| 459 | enc_input=emb_out, |
| 460 | attn_bias=n_head_self_attn_mask, |
| 461 | enc_layers=self.text_enc_layers, |
| 462 | n_head=self._text_n_head, |
| 463 | d_key=self._emb_size // self._text_n_head, |
| 464 | d_value=self._emb_size // self._text_n_head, |
| 465 | d_model=self._emb_size, |
| 466 | d_inner_hid=self._emb_size * 4, |
| 467 | prepostprocess_dropout=self._text_prepostprocess_dropout, |
| 468 | attention_dropout=self._text_attention_dropout, |
| 469 | relu_dropout=0, |
| 470 | hidden_act=self._text_hidden_act, |
| 471 | preprocess_cmd="", |
| 472 | postprocess_cmd="dan", |
| 473 | param_initializer=self._param_initializer, |
| 474 | name='text.encoder', |
| 475 | caches=self.text_caches, |
| 476 | gather_idx=gather_idx, |