173 lines
6.7 KiB
Python
173 lines
6.7 KiB
Python
# coding=utf-8
|
|
# Copyright 2018 Google AI, Google Brain and Carnegie Mellon University Authors and the HuggingFace Inc. team.
|
|
# Copyright (c) 2018, NVIDIA CORPORATION. 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.
|
|
""" XLNet configuration """
|
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
|
|
|
import json
|
|
import logging
|
|
import sys
|
|
from io import open
|
|
|
|
from .configuration_utils import PretrainedConfig
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
|
'xlnet-base-cased': "https://s3.amazonaws.com/models.huggingface.co/bert/xlnet-base-cased-config.json",
|
|
'xlnet-large-cased': "https://s3.amazonaws.com/models.huggingface.co/bert/xlnet-large-cased-config.json",
|
|
}
|
|
|
|
|
|
class XLNetConfig(PretrainedConfig):
|
|
"""Configuration class to store the configuration of a ``XLNetModel``.
|
|
|
|
Args:
|
|
vocab_size_or_config_json_file: Vocabulary size of ``inputs_ids`` in ``XLNetModel``.
|
|
d_model: Size of the encoder layers and the pooler layer.
|
|
n_layer: Number of hidden layers in the Transformer encoder.
|
|
n_head: Number of attention heads for each attention layer in
|
|
the Transformer encoder.
|
|
d_inner: The size of the "intermediate" (i.e., feed-forward)
|
|
layer in the Transformer encoder.
|
|
ff_activation: The non-linear activation function (function or string) in the
|
|
encoder and pooler. If string, "gelu", "relu" and "swish" are supported.
|
|
untie_r: untie relative position biases
|
|
attn_type: 'bi' for XLNet, 'uni' for Transformer-XL
|
|
|
|
dropout: The dropout probabilitiy for all fully connected
|
|
layers in the embeddings, encoder, and pooler.
|
|
dropatt: The dropout ratio for the attention
|
|
probabilities.
|
|
initializer_range: The sttdev of the truncated_normal_initializer for
|
|
initializing all weight matrices.
|
|
layer_norm_eps: The epsilon used by LayerNorm.
|
|
|
|
dropout: float, dropout rate.
|
|
dropatt: float, dropout rate on attention probabilities.
|
|
init: str, the initialization scheme, either "normal" or "uniform".
|
|
init_range: float, initialize the parameters with a uniform distribution
|
|
in [-init_range, init_range]. Only effective when init="uniform".
|
|
init_std: float, initialize the parameters with a normal distribution
|
|
with mean 0 and stddev init_std. Only effective when init="normal".
|
|
mem_len: int, the number of tokens to cache.
|
|
reuse_len: int, the number of tokens in the currect batch to be cached
|
|
and reused in the future.
|
|
bi_data: bool, whether to use bidirectional input pipeline.
|
|
Usually set to True during pretraining and False during finetuning.
|
|
clamp_len: int, clamp all relative distances larger than clamp_len.
|
|
-1 means no clamping.
|
|
same_length: bool, whether to use the same attention length for each token.
|
|
finetuning_task: name of the glue task on which the model was fine-tuned if any
|
|
"""
|
|
pretrained_config_archive_map = XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP
|
|
|
|
def __init__(self,
|
|
vocab_size_or_config_json_file=32000,
|
|
d_model=1024,
|
|
n_layer=24,
|
|
n_head=16,
|
|
d_inner=4096,
|
|
ff_activation="gelu",
|
|
untie_r=True,
|
|
attn_type="bi",
|
|
|
|
initializer_range=0.02,
|
|
layer_norm_eps=1e-12,
|
|
|
|
dropout=0.1,
|
|
mem_len=None,
|
|
reuse_len=None,
|
|
bi_data=False,
|
|
clamp_len=-1,
|
|
same_length=False,
|
|
|
|
finetuning_task=None,
|
|
num_labels=2,
|
|
summary_type='last',
|
|
summary_use_proj=True,
|
|
summary_activation='tanh',
|
|
summary_last_dropout=0.1,
|
|
start_n_top=5,
|
|
end_n_top=5,
|
|
**kwargs):
|
|
"""Constructs XLNetConfig.
|
|
"""
|
|
super(XLNetConfig, self).__init__(**kwargs)
|
|
|
|
if isinstance(vocab_size_or_config_json_file, str) or (sys.version_info[0] == 2
|
|
and isinstance(vocab_size_or_config_json_file, unicode)):
|
|
with open(vocab_size_or_config_json_file, "r", encoding='utf-8') as reader:
|
|
json_config = json.loads(reader.read())
|
|
for key, value in json_config.items():
|
|
setattr(config, key, value)
|
|
elif isinstance(vocab_size_or_config_json_file, int):
|
|
self.n_token = vocab_size_or_config_json_file
|
|
self.d_model = d_model
|
|
self.n_layer = n_layer
|
|
self.n_head = n_head
|
|
assert d_model % n_head == 0
|
|
self.d_head = d_model // n_head
|
|
self.ff_activation = ff_activation
|
|
self.d_inner = d_inner
|
|
self.untie_r = untie_r
|
|
self.attn_type = attn_type
|
|
|
|
self.initializer_range = initializer_range
|
|
self.layer_norm_eps = layer_norm_eps
|
|
|
|
self.dropout = dropout
|
|
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
|
|
|
|
self.finetuning_task = finetuning_task
|
|
self.num_labels = num_labels
|
|
self.summary_type = summary_type
|
|
self.summary_use_proj = summary_use_proj
|
|
self.summary_activation = summary_activation
|
|
self.summary_last_dropout = summary_last_dropout
|
|
self.start_n_top = start_n_top
|
|
self.end_n_top = end_n_top
|
|
else:
|
|
raise ValueError("First argument must be either a vocabulary size (int)"
|
|
" or the path to a pretrained model config file (str)")
|
|
|
|
@property
|
|
def max_position_embeddings(self):
|
|
return -1
|
|
|
|
@property
|
|
def vocab_size(self):
|
|
return self.n_token
|
|
|
|
@vocab_size.setter
|
|
def vocab_size(self, value):
|
|
self.n_token = value
|
|
|
|
@property
|
|
def hidden_size(self):
|
|
return self.d_model
|
|
|
|
@property
|
|
def num_attention_heads(self):
|
|
return self.n_head
|
|
|
|
@property
|
|
def num_hidden_layers(self):
|
|
return self.n_layer
|