mlm.py 4.6 KB
Newer Older
X
xixiaoyao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# -*- coding: UTF-8 -*-
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import paddle.fluid as fluid
from paddlepalm.interface import task_paradigm
from paddle.fluid import layers
X
xixiaoyao 已提交
19
from paddlepalm.backbone.utils.transformer import pre_process_layer
X
xixiaoyao 已提交
20 21 22 23 24 25 26

class TaskParadigm(task_paradigm):
    '''
    matching
    '''
    def __init__(self, config, phase, backbone_config=None):
        self._is_training = phase == 'train'
X
xixiaoyao 已提交
27
        self._emb_size = backbone_config['hidden_size']
X
xixiaoyao 已提交
28 29 30 31 32 33 34
        self._hidden_size = backbone_config['hidden_size']
        self._vocab_size = backbone_config['vocab_size']
        self._hidden_act = backbone_config['hidden_act']
        self._initializer_range = backbone_config['initializer_range']
    
    @property
    def inputs_attrs(self):
X
xixiaoyao 已提交
35 36
        reader = {
            "mask_label": [[-1, 1], 'int64'],
X
xixiaoyao 已提交
37
            "batchsize_x_seqlen": [[1], 'int64'],
X
xixiaoyao 已提交
38 39 40
            "mask_pos": [[-1, 1], 'int64']}
        if not self._is_training:
            del reader['mask_label']
X
xixiaoyao 已提交
41
            del reader['batchsize_x_seqlen']
X
xixiaoyao 已提交
42 43 44
        bb = {
            "encoder_outputs": [[-1, -1, self._hidden_size], 'float32'],
            "embedding_table": [[-1, self._vocab_size, self._emb_size], 'float32']}
X
xixiaoyao 已提交
45 46 47 48 49 50 51
        return {'reader': reader, 'backbone': bb}

    @property
    def outputs_attrs(self):
        if self._is_training:
            return {"loss": [[1], 'float32']}
        else:
X
xixiaoyao 已提交
52
            return {"logits": [[-1], 'float32']}
X
xixiaoyao 已提交
53 54

    def build(self, inputs):
X
xixiaoyao 已提交
55 56
        if self._is_training:
            mask_label = inputs["reader"]["mask_label"] 
X
xixiaoyao 已提交
57 58
            # 多任务学习时才需要引入这个,防止其他run其他任务时导致seqlen过小,gather超范围
            batchsize_x_seqlen = inputs["reader"]["batchsize_x_seqlen"] 
X
xixiaoyao 已提交
59
        mask_pos = inputs["reader"]["mask_pos"] 
X
xixiaoyao 已提交
60
        word_emb = inputs["backbone"]["embedding_table"]
X
xixiaoyao 已提交
61 62 63 64 65 66 67
        enc_out = inputs["backbone"]["encoder_outputs"]

        emb_size = word_emb.shape[-1]

        _param_initializer = fluid.initializer.TruncatedNormal(
            scale=self._initializer_range)

X
xixiaoyao 已提交
68 69 70 71 72 73
        if self._is_training:
            # 多任务训练时才需要引入这个,防止其他run其他任务时导致seqlen过小,gather超范围
            #mask_pos = fluid.layers.cast(x=mask_pos, dtype='int32')
            mask_pos = fluid.layers.elementwise_min(mask_pos, batchsize_x_seqlen)

        #print(fluid.default_main_program().blocks[0].vars)
X
xixiaoyao 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

        reshaped_emb_out = fluid.layers.reshape(
            x=enc_out, shape=[-1, emb_size])

        # extract masked tokens' feature
        mask_feat = fluid.layers.gather(input=reshaped_emb_out, index=mask_pos)

        # transform: fc
        mask_trans_feat = fluid.layers.fc(
            input=mask_feat,
            size=emb_size,
            act=self._hidden_act,
            param_attr=fluid.ParamAttr(
                name='mask_lm_trans_fc.w_0',
                initializer=_param_initializer),
            bias_attr=fluid.ParamAttr(name='mask_lm_trans_fc.b_0'))
        # transform: layer norm
        mask_trans_feat = pre_process_layer(
            mask_trans_feat, 'n', name='mask_lm_trans')

        mask_lm_out_bias_attr = fluid.ParamAttr(
            name="mask_lm_out_fc.b_0",
            initializer=fluid.initializer.Constant(value=0.0))

        # print fluid.default_main_program().global_block()

        # fc_out = fluid.layers.matmul(
        #     x=mask_trans_feat,
        #     y=fluid.default_main_program().global_block().var(
        #         _word_emb_name),
        #     transpose_y=True)

        fc_out = fluid.layers.matmul(
            x=mask_trans_feat,
            y=word_emb,
            transpose_y=True)
        fc_out += fluid.layers.create_parameter(
            shape=[self._vocab_size],
            dtype='float32',
            attr=mask_lm_out_bias_attr,
            is_bias=True)

        if self._is_training:
X
xixiaoyao 已提交
117 118 119
            mask_lm_loss = fluid.layers.softmax_with_cross_entropy(
                logits=fc_out, label=mask_label)
            loss = fluid.layers.mean(mask_lm_loss)
X
xixiaoyao 已提交
120 121
            return {'loss': loss}
        else:
X
xixiaoyao 已提交
122
            return {'logits': fc_out}
X
xixiaoyao 已提交
123 124