ernie.py 10.5 KB
Newer Older
T
tianxin04 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   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.
"""Ernie model."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
C
chenxuyi 已提交
19 20
from __future__ import unicode_literals
from __future__ import absolute_import
T
tianxin04 已提交
21 22

import json
23
import six
C
chenxuyi 已提交
24
import logging
T
tianxin04 已提交
25
import paddle.fluid as fluid
C
chenxuyi 已提交
26
from io import open
27

T
tianxin04 已提交
28 29
from model.transformer_encoder import encoder, pre_process_layer

C
chenxuyi 已提交
30
log = logging.getLogger(__name__)
T
tianxin04 已提交
31 32 33 34 35 36 37

class ErnieConfig(object):
    def __init__(self, config_path):
        self._config_dict = self._parse(config_path)

    def _parse(self, config_path):
        try:
C
chenxuyi 已提交
38
            with open(config_path, 'r', encoding='utf8') as json_file:
T
tianxin04 已提交
39 40 41 42 43 44 45 46
                config_dict = json.load(json_file)
        except Exception:
            raise IOError("Error in parsing Ernie model config file '%s'" %
                          config_path)
        else:
            return config_dict

    def __getitem__(self, key):
T
tianxin 已提交
47
        return self._config_dict.get(key, None)
T
tianxin04 已提交
48 49 50

    def print_config(self):
        for arg, value in sorted(six.iteritems(self._config_dict)):
C
chenxuyi 已提交
51 52
            log.info('%s: %s' % (arg, value))
        log.info('------------------------------------------------')
T
tianxin04 已提交
53 54 55 56 57 58 59


class ErnieModel(object):
    def __init__(self,
                 src_ids,
                 position_ids,
                 sentence_ids,
T
tianxin 已提交
60
                 task_ids,
Y
Yibing Liu 已提交
61
                 input_mask,
T
tianxin04 已提交
62 63 64 65 66 67 68 69 70
                 config,
                 weight_sharing=True,
                 use_fp16=False):

        self._emb_size = config['hidden_size']
        self._n_layer = config['num_hidden_layers']
        self._n_head = config['num_attention_heads']
        self._voc_size = config['vocab_size']
        self._max_position_seq_len = config['max_position_embeddings']
T
tianxin 已提交
71 72 73 74 75 76 77 78
        if config['sent_type_vocab_size']:
            self._sent_types = config['sent_type_vocab_size']
        else:
            self._sent_types = config['type_vocab_size']

        self._use_task_id = config['use_task_id']
        if self._use_task_id:
            self._task_types = config['task_type_vocab_size']
T
tianxin04 已提交
79 80 81 82 83 84 85 86
        self._hidden_act = config['hidden_act']
        self._prepostprocess_dropout = config['hidden_dropout_prob']
        self._attention_dropout = config['attention_probs_dropout_prob']
        self._weight_sharing = weight_sharing

        self._word_emb_name = "word_embedding"
        self._pos_emb_name = "pos_embedding"
        self._sent_emb_name = "sent_embedding"
T
tianxin 已提交
87
        self._task_emb_name = "task_embedding"
T
tianxin04 已提交
88
        self._dtype = "float16" if use_fp16 else "float32"
C
chenxuyi 已提交
89
        self._emb_dtype = 'float32'
T
tianxin04 已提交
90

C
cclauss 已提交
91
        # Initialize all weigths by truncated normal initializer, and all biases
T
tianxin04 已提交
92 93 94 95
        # will be initialized by constant zero by default.
        self._param_initializer = fluid.initializer.TruncatedNormal(
            scale=config['initializer_range'])

T
tianxin 已提交
96 97
        self._build_model(src_ids, position_ids, sentence_ids, task_ids,
                          input_mask)
T
tianxin04 已提交
98

T
tianxin 已提交
99 100
    def _build_model(self, src_ids, position_ids, sentence_ids, task_ids,
                     input_mask):
T
tianxin04 已提交
101 102 103 104
        # padding id in vocabulary must be set to 0
        emb_out = fluid.layers.embedding(
            input=src_ids,
            size=[self._voc_size, self._emb_size],
T
tianxin 已提交
105
            dtype=self._emb_dtype,
T
tianxin04 已提交
106 107 108
            param_attr=fluid.ParamAttr(
                name=self._word_emb_name, initializer=self._param_initializer),
            is_sparse=False)
C
chenxuyi 已提交
109
        
T
tianxin04 已提交
110 111 112
        position_emb_out = fluid.layers.embedding(
            input=position_ids,
            size=[self._max_position_seq_len, self._emb_size],
T
tianxin 已提交
113
            dtype=self._emb_dtype,
T
tianxin04 已提交
114 115 116 117 118 119
            param_attr=fluid.ParamAttr(
                name=self._pos_emb_name, initializer=self._param_initializer))

        sent_emb_out = fluid.layers.embedding(
            sentence_ids,
            size=[self._sent_types, self._emb_size],
T
tianxin 已提交
120
            dtype=self._emb_dtype,
T
tianxin04 已提交
121 122 123 124 125 126
            param_attr=fluid.ParamAttr(
                name=self._sent_emb_name, initializer=self._param_initializer))

        emb_out = emb_out + position_emb_out
        emb_out = emb_out + sent_emb_out

T
tianxin 已提交
127 128 129 130 131 132 133 134 135 136 137
        if self._use_task_id:
            task_emb_out = fluid.layers.embedding(
                task_ids,
                size=[self._task_types, self._emb_size],
                dtype=self._emb_dtype,
                param_attr=fluid.ParamAttr(
                    name=self._task_emb_name,
                    initializer=self._param_initializer))

            emb_out = emb_out + task_emb_out

T
tianxin04 已提交
138 139 140
        emb_out = pre_process_layer(
            emb_out, 'nd', self._prepostprocess_dropout, name='pre_encoder')

C
chenxuyi 已提交
141
        if self._dtype == 'float16':
T
tianxin 已提交
142
            emb_out = fluid.layers.cast(x=emb_out, dtype=self._dtype)
Y
Yibing Liu 已提交
143 144 145
            input_mask = fluid.layers.cast(x=input_mask, dtype=self._dtype)
        self_attn_mask = fluid.layers.matmul(
            x=input_mask, y=input_mask, transpose_y=True)
T
tianxin04 已提交
146

Y
Yibing Liu 已提交
147
        self_attn_mask = fluid.layers.scale(
Y
Yibing Liu 已提交
148
            x=self_attn_mask, scale=10000.0, bias=-1.0, bias_after_scale=False)
T
tianxin04 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        n_head_self_attn_mask = fluid.layers.stack(
            x=[self_attn_mask] * self._n_head, axis=1)
        n_head_self_attn_mask.stop_gradient = True

        self._enc_out = encoder(
            enc_input=emb_out,
            attn_bias=n_head_self_attn_mask,
            n_layer=self._n_layer,
            n_head=self._n_head,
            d_key=self._emb_size // self._n_head,
            d_value=self._emb_size // self._n_head,
            d_model=self._emb_size,
            d_inner_hid=self._emb_size * 4,
            prepostprocess_dropout=self._prepostprocess_dropout,
            attention_dropout=self._attention_dropout,
            relu_dropout=0,
            hidden_act=self._hidden_act,
            preprocess_cmd="",
            postprocess_cmd="dan",
            param_initializer=self._param_initializer,
            name='encoder')
C
chenxuyi 已提交
170
        if self._dtype == 'float16':
C
chenxuyi 已提交
171 172 173
            self._enc_out = fluid.layers.cast(
                x=self._enc_out, dtype=self._emb_dtype)

T
tianxin04 已提交
174 175 176 177

    def get_sequence_output(self):
        return self._enc_out

Y
Yibing Liu 已提交
178
    def get_pooled_output(self):
T
tianxin04 已提交
179
        """Get the first feature of each sequence for classification"""
Y
Yibing Liu 已提交
180 181
        next_sent_feat = fluid.layers.slice(
            input=self._enc_out, axes=[1], starts=[0], ends=[1])
T
tianxin04 已提交
182 183 184 185 186 187 188 189 190
        next_sent_feat = fluid.layers.fc(
            input=next_sent_feat,
            size=self._emb_size,
            act="tanh",
            param_attr=fluid.ParamAttr(
                name="pooled_fc.w_0", initializer=self._param_initializer),
            bias_attr="pooled_fc.b_0")
        return next_sent_feat

T
tianxin 已提交
191
    def get_lm_output(self, mask_label, mask_pos):
T
tianxin04 已提交
192 193 194 195 196
        """Get the loss & accuracy for pretraining"""

        mask_pos = fluid.layers.cast(x=mask_pos, dtype='int32')

        # extract the first token feature in each sentence
T
tianxin 已提交
197
        self.next_sent_feat = self.get_pooled_output()
Y
Yibing Liu 已提交
198 199
        reshaped_emb_out = fluid.layers.reshape(
            x=self._enc_out, shape=[-1, self._emb_size])
T
tianxin04 已提交
200
        # extract masked tokens' feature
Y
Yibing Liu 已提交
201
        mask_feat = fluid.layers.gather(input=reshaped_emb_out, index=mask_pos)
T
tianxin04 已提交
202 203 204 205 206 207 208 209 210 211

        # transform: fc
        mask_trans_feat = fluid.layers.fc(
            input=mask_feat,
            size=self._emb_size,
            act=self._hidden_act,
            param_attr=fluid.ParamAttr(
                name='mask_lm_trans_fc.w_0',
                initializer=self._param_initializer),
            bias_attr=fluid.ParamAttr(name='mask_lm_trans_fc.b_0'))
C
chenxuyi 已提交
212
        
T
tianxin 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225
        # transform: layer norm 
        mask_trans_feat = fluid.layers.layer_norm(
            mask_trans_feat,
            begin_norm_axis=len(mask_trans_feat.shape) - 1,
            param_attr=fluid.ParamAttr(
                name='mask_lm_trans_layer_norm_scale',
                initializer=fluid.initializer.Constant(1.)),
            bias_attr=fluid.ParamAttr(
                name='mask_lm_trans_layer_norm_bias',
                initializer=fluid.initializer.Constant(1.)))
        # transform: layer norm 
        #mask_trans_feat = pre_process_layer(
        #    mask_trans_feat, 'n', name='mask_lm_trans')
T
tianxin04 已提交
226 227 228 229 230 231 232 233 234 235 236 237

        mask_lm_out_bias_attr = fluid.ParamAttr(
            name="mask_lm_out_fc.b_0",
            initializer=fluid.initializer.Constant(value=0.0))
        if self._weight_sharing:
            fc_out = fluid.layers.matmul(
                x=mask_trans_feat,
                y=fluid.default_main_program().global_block().var(
                    self._word_emb_name),
                transpose_y=True)
            fc_out += fluid.layers.create_parameter(
                shape=[self._voc_size],
T
tianxin 已提交
238
                dtype=self._emb_dtype,
T
tianxin04 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
                attr=mask_lm_out_bias_attr,
                is_bias=True)

        else:
            fc_out = fluid.layers.fc(input=mask_trans_feat,
                                     size=self._voc_size,
                                     param_attr=fluid.ParamAttr(
                                         name="mask_lm_out_fc.w_0",
                                         initializer=self._param_initializer),
                                     bias_attr=mask_lm_out_bias_attr)

        mask_lm_loss = fluid.layers.softmax_with_cross_entropy(
            logits=fc_out, label=mask_label)
        mean_mask_lm_loss = fluid.layers.mean(mask_lm_loss)

T
tianxin 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267
        return mean_mask_lm_loss

    def get_task_output(self, task, task_labels):
        task_fc_out = fluid.layers.fc(input=self.next_sent_feat,
                                      size=task["num_labels"],
                                      param_attr=fluid.ParamAttr(
                                          name=task["task_name"] + "_fc.w_0",
                                          initializer=self._param_initializer),
                                      bias_attr=task["task_name"] + "_fc.b_0")
        task_loss, task_softmax = fluid.layers.softmax_with_cross_entropy(
            logits=task_fc_out, label=task_labels, return_softmax=True)
        task_acc = fluid.layers.accuracy(input=task_softmax, label=task_labels)
        mean_task_loss = fluid.layers.mean(task_loss)
        return mean_task_loss, task_acc