Upload folder using huggingface_hub
Browse files- config.json +31 -0
- configuration_btlm.py +160 -0
- modeling_btlm.py +1528 -0
- params_train_to_hf.json +19 -0
- pytorch_model-00001-of-00001.bin +3 -0
- pytorch_model.bin.index.json +396 -0
- register_btlm.py +9 -0
- tokenizer.json +0 -0
config.json
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "MBZUAI-LLM/SlimPajama-DC-6",
|
3 |
+
"architectures": [
|
4 |
+
"BTLMLMHeadModel"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "MBZUAI-LLM/SlimPajama-DC-6--configuration_btlm.BTLMConfig",
|
8 |
+
"AutoModel": "MBZUAI-LLM/SlimPajama-DC-6--modeling_btlm.BTLMModel",
|
9 |
+
"AutoModelForCausalLM": "MBZUAI-LLM/SlimPajama-DC-6--modeling_btlm.BTLMLMHeadModel"
|
10 |
+
},
|
11 |
+
"model_type": "btlm",
|
12 |
+
"eos_token_id": 0,
|
13 |
+
"pad_token_id": 0,
|
14 |
+
"attn_pdrop": 0.0,
|
15 |
+
"scale_attn_weights": true,
|
16 |
+
"resid_pdrop": 0.0,
|
17 |
+
"embeddings_scale": 14.6,
|
18 |
+
"n_inner": 5461,
|
19 |
+
"n_embd": 2048,
|
20 |
+
"layer_norm_epsilon": 1e-05,
|
21 |
+
"n_positions": 2048,
|
22 |
+
"activation_function": "swiglu",
|
23 |
+
"n_head": 16,
|
24 |
+
"n_layer": 24,
|
25 |
+
"width_scale": 0.2775,
|
26 |
+
"position_embedding_type": "alibi",
|
27 |
+
"scale_qk_dot_by_d": true,
|
28 |
+
"tie_word_embeddings": true,
|
29 |
+
"vocab_size": 50277,
|
30 |
+
"embd_pdrop": 0.0
|
31 |
+
}
|
configuration_btlm.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2023 The OpenAI Team Authors and HuggingFace Inc. team.
|
3 |
+
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
4 |
+
# Copyright 2023 Cerebras Systems.
|
5 |
+
#
|
6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
+
# you may not use this file except in compliance with the License.
|
8 |
+
# You may obtain a copy of the License at
|
9 |
+
#
|
10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11 |
+
#
|
12 |
+
# Unless required by applicable law or agreed to in writing, software
|
13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
+
# See the License for the specific language governing permissions and
|
16 |
+
# limitations under the License.
|
17 |
+
""" BTLM configuration"""
|
18 |
+
|
19 |
+
from transformers.configuration_utils import PretrainedConfig
|
20 |
+
from transformers.utils import logging
|
21 |
+
|
22 |
+
|
23 |
+
logger = logging.get_logger(__name__)
|
24 |
+
|
25 |
+
BTLM_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
26 |
+
"cerebras/BTLM-2.7B": "https://huggingface.co/cerebras/BTLM-2.7B/resolve/main/config.json",
|
27 |
+
}
|
28 |
+
|
29 |
+
class BTLMConfig(PretrainedConfig):
|
30 |
+
"""
|
31 |
+
This is the configuration class to store the configuration of a [`BTLMModel`]. It is used to
|
32 |
+
instantiate a BTLM model according to the specified arguments, defining the model architecture.
|
33 |
+
|
34 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
35 |
+
documentation from [`PretrainedConfig`] for more information.
|
36 |
+
|
37 |
+
|
38 |
+
Args:
|
39 |
+
vocab_size (`int`, *optional*, defaults to 50257):
|
40 |
+
Vocabulary size of the BTLM model. Defines the number of different tokens that can be represented by the
|
41 |
+
`inputs_ids` passed when calling [`BTLMModel`].
|
42 |
+
n_positions (`int`, *optional*, defaults to 1024):
|
43 |
+
The maximum sequence length that this model might ever be used with. Typically set this to something large
|
44 |
+
just in case (e.g., 512 or 1024 or 2048).
|
45 |
+
n_embd (`int`, *optional*, defaults to 768):
|
46 |
+
Dimensionality of the embeddings and hidden states.
|
47 |
+
n_layer (`int`, *optional*, defaults to 12):
|
48 |
+
Number of hidden layers in the Transformer encoder.
|
49 |
+
n_head (`int`, *optional*, defaults to 12):
|
50 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
51 |
+
n_inner (`int`, *optional*, defaults to None):
|
52 |
+
Dimensionality of the inner feed-forward layers. `None` will set it to 4 times n_embd
|
53 |
+
activation_function (`str`, *optional*, defaults to `"gelu"`):
|
54 |
+
Activation function, to be selected in the list `["relu", "silu", "gelu", "tanh", "gelu_new", "swiglu"]`.
|
55 |
+
resid_pdrop (`float`, *optional*, defaults to 0.1):
|
56 |
+
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
|
57 |
+
embd_pdrop (`float`, *optional*, defaults to 0.1):
|
58 |
+
The dropout ratio for the embeddings.
|
59 |
+
attn_pdrop (`float`, *optional*, defaults to 0.1):
|
60 |
+
The dropout ratio for the attention.
|
61 |
+
layer_norm_epsilon (`float`, *optional*, defaults to 1e-5):
|
62 |
+
The epsilon to use in the layer normalization layers.
|
63 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
64 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
65 |
+
scale_attn_weights (`bool`, *optional*, defaults to `True`):
|
66 |
+
Scale attention weights by dividing by sqrt(hidden_size)..
|
67 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
68 |
+
Whether or not the model should return the last key/values attentions (not used by all models).
|
69 |
+
scale_attn_by_inverse_layer_idx (`bool`, *optional*, defaults to `False`):
|
70 |
+
Whether to additionally scale attention weights by `1 / layer_idx + 1`.
|
71 |
+
reorder_and_upcast_attn (`bool`, *optional*, defaults to `False`):
|
72 |
+
Whether to scale keys (K) prior to computing attention (dot-product) and upcast attention
|
73 |
+
dot-product/softmax to float() when training with mixed precision.
|
74 |
+
position_embedding_type (`str`, *optional*, defaults to `"learned"`):
|
75 |
+
Positional embedding can be either `"alibi"` or `"learned"`.
|
76 |
+
width_scale (`float`, *optional*, defaults to 1.0):
|
77 |
+
muP parameter to scale output logits and initializers. Calculated as (`d_model,0 / d_model`),
|
78 |
+
where `d_model` is the model's width and `d_model,0` is the proxy model's width.
|
79 |
+
embeddings_scale (`float`, *optional*, defaults to 1.0):
|
80 |
+
muP parameter to scale token and position embeddings.
|
81 |
+
scale_qk_dot_by_d (`bool`, *optional*, defaults to `False`):
|
82 |
+
Scale attention weights by dividing by hidden_size instead of sqrt(hidden_size).
|
83 |
+
Need to set scale_attn_weights to `True` as well.
|
84 |
+
|
85 |
+
Example:
|
86 |
+
|
87 |
+
```python
|
88 |
+
>>> from transformers import BTLMConfig, BTLMModel
|
89 |
+
|
90 |
+
>>> # Initializing a BTLM configuration
|
91 |
+
>>> configuration = BTLMConfig()
|
92 |
+
|
93 |
+
>>> # Initializing a model (with random weights) from the configuration
|
94 |
+
>>> model = BTLMModel(configuration)
|
95 |
+
|
96 |
+
>>> # Accessing the model configuration
|
97 |
+
>>> configuration = model.config
|
98 |
+
```"""
|
99 |
+
|
100 |
+
model_type = "btlm"
|
101 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
102 |
+
attribute_map = {
|
103 |
+
"hidden_size": "n_embd",
|
104 |
+
"max_position_embeddings": "n_positions",
|
105 |
+
"num_attention_heads": "n_head",
|
106 |
+
"num_hidden_layers": "n_layer",
|
107 |
+
}
|
108 |
+
|
109 |
+
def __init__(
|
110 |
+
self,
|
111 |
+
vocab_size=50257,
|
112 |
+
n_positions=1024,
|
113 |
+
n_embd=768,
|
114 |
+
n_layer=12,
|
115 |
+
n_head=12,
|
116 |
+
n_inner=None,
|
117 |
+
activation_function="gelu_new",
|
118 |
+
resid_pdrop=0.1,
|
119 |
+
embd_pdrop=0.1,
|
120 |
+
attn_pdrop=0.1,
|
121 |
+
layer_norm_epsilon=1e-5,
|
122 |
+
initializer_range=0.02,
|
123 |
+
scale_attn_weights=True,
|
124 |
+
use_cache=True,
|
125 |
+
bos_token_id=50256,
|
126 |
+
eos_token_id=50256,
|
127 |
+
scale_attn_by_inverse_layer_idx=False,
|
128 |
+
reorder_and_upcast_attn=False,
|
129 |
+
position_embedding_type="learned",
|
130 |
+
width_scale=1.0,
|
131 |
+
embeddings_scale=1.0,
|
132 |
+
scale_qk_dot_by_d=False,
|
133 |
+
**kwargs,
|
134 |
+
):
|
135 |
+
self.vocab_size = vocab_size
|
136 |
+
self.n_positions = n_positions
|
137 |
+
self.n_embd = n_embd
|
138 |
+
self.n_layer = n_layer
|
139 |
+
self.n_head = n_head
|
140 |
+
self.n_inner = n_inner
|
141 |
+
self.activation_function = activation_function
|
142 |
+
self.resid_pdrop = resid_pdrop
|
143 |
+
self.embd_pdrop = embd_pdrop
|
144 |
+
self.attn_pdrop = attn_pdrop
|
145 |
+
self.layer_norm_epsilon = layer_norm_epsilon
|
146 |
+
self.initializer_range = initializer_range
|
147 |
+
self.scale_attn_weights = scale_attn_weights
|
148 |
+
self.use_cache = use_cache
|
149 |
+
self.scale_attn_by_inverse_layer_idx = scale_attn_by_inverse_layer_idx
|
150 |
+
self.reorder_and_upcast_attn = reorder_and_upcast_attn
|
151 |
+
|
152 |
+
self.bos_token_id = bos_token_id
|
153 |
+
self.eos_token_id = eos_token_id
|
154 |
+
|
155 |
+
self.position_embedding_type = position_embedding_type
|
156 |
+
self.width_scale = width_scale
|
157 |
+
self.embeddings_scale = embeddings_scale
|
158 |
+
self.scale_qk_dot_by_d = scale_qk_dot_by_d
|
159 |
+
|
160 |
+
super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
modeling_btlm.py
ADDED
@@ -0,0 +1,1528 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2023 The OpenAI Team Authors and HuggingFace Inc. team.
|
3 |
+
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
4 |
+
# Copyright 2023 Cerebras Systems.
|
5 |
+
#
|
6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
+
# you may not use this file except in compliance with the License.
|
8 |
+
# You may obtain a copy of the License at
|
9 |
+
#
|
10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11 |
+
#
|
12 |
+
# Unless required by applicable law or agreed to in writing, software
|
13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
+
# See the License for the specific language governing permissions and
|
16 |
+
# limitations under the License.
|
17 |
+
""" PyTorch BTLM model."""
|
18 |
+
|
19 |
+
import math
|
20 |
+
import os
|
21 |
+
import warnings
|
22 |
+
from typing import Optional, Tuple, Union
|
23 |
+
|
24 |
+
import torch
|
25 |
+
from torch import Tensor, nn
|
26 |
+
from torch.cuda.amp import autocast
|
27 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
28 |
+
|
29 |
+
from transformers.activations import ACT2FN
|
30 |
+
from transformers.modeling_outputs import (
|
31 |
+
BaseModelOutputWithPastAndCrossAttentions,
|
32 |
+
CausalLMOutputWithCrossAttentions,
|
33 |
+
QuestionAnsweringModelOutput,
|
34 |
+
SequenceClassifierOutputWithPast,
|
35 |
+
TokenClassifierOutput,
|
36 |
+
)
|
37 |
+
from transformers.modeling_utils import PreTrainedModel
|
38 |
+
from transformers.pytorch_utils import Conv1D, find_pruneable_heads_and_indices, prune_conv1d_layer
|
39 |
+
from transformers.utils import (
|
40 |
+
add_code_sample_docstrings,
|
41 |
+
add_start_docstrings,
|
42 |
+
add_start_docstrings_to_model_forward,
|
43 |
+
logging,
|
44 |
+
)
|
45 |
+
from transformers.utils.model_parallel_utils import assert_device_map, get_device_map
|
46 |
+
# from .configuration_btlm import BTLMConfig
|
47 |
+
from configuration_btlm import BTLMConfig
|
48 |
+
|
49 |
+
|
50 |
+
logger = logging.get_logger(__name__)
|
51 |
+
|
52 |
+
_CHECKPOINT_FOR_DOC = "cerebras/BTLM-2.7B"
|
53 |
+
_CONFIG_FOR_DOC = "BTLMConfig"
|
54 |
+
|
55 |
+
BTLM_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
56 |
+
"cerebras/BTLM-2.7B",
|
57 |
+
# See all BTLM models at https://huggingface.co/models?filter=btlm
|
58 |
+
]
|
59 |
+
|
60 |
+
|
61 |
+
class SwiGLUActivation(nn.Module):
|
62 |
+
def forward(self, x1: Tensor, x2: Tensor) -> Tensor:
|
63 |
+
return x1 * nn.functional.silu(x2)
|
64 |
+
|
65 |
+
|
66 |
+
class AlibiPositionEmbeddingLayer(nn.Module):
|
67 |
+
def __init__(self, num_heads):
|
68 |
+
super(AlibiPositionEmbeddingLayer, self).__init__()
|
69 |
+
|
70 |
+
self.num_heads = num_heads
|
71 |
+
slopes = torch.tensor(
|
72 |
+
AlibiPositionEmbeddingLayer._get_alibi_slopes(num_heads)
|
73 |
+
).unsqueeze(-1)
|
74 |
+
self.slopes = nn.parameter.Parameter(slopes, requires_grad=False)
|
75 |
+
|
76 |
+
def forward(self, seq_length, key_length, cached_qk_len):
|
77 |
+
context_position = torch.arange(
|
78 |
+
cached_qk_len, cached_qk_len + seq_length, device=self.slopes.device
|
79 |
+
)[:, None]
|
80 |
+
memory_position = torch.arange(
|
81 |
+
key_length + cached_qk_len, device=self.slopes.device
|
82 |
+
)[None, :]
|
83 |
+
relative_position = memory_position - context_position
|
84 |
+
relative_position = torch.abs(relative_position).unsqueeze(0).expand(self.num_heads, -1, -1)
|
85 |
+
alibi = (self.slopes * -1.0).unsqueeze(1) * relative_position
|
86 |
+
return alibi
|
87 |
+
|
88 |
+
@staticmethod
|
89 |
+
def _get_alibi_slopes(n):
|
90 |
+
def get_slopes_power_of_2(n):
|
91 |
+
start = 2 ** (-(2 ** -(math.log2(n) - 3)))
|
92 |
+
ratio = start
|
93 |
+
return [start * ratio ** i for i in range(n)]
|
94 |
+
|
95 |
+
if math.log2(n).is_integer():
|
96 |
+
return get_slopes_power_of_2(
|
97 |
+
n
|
98 |
+
) # In the paper, we only train models that have 2^a heads for some a. This function has
|
99 |
+
else: # some good properties that only occur when the input is a power of 2. To maintain that even
|
100 |
+
closest_power_of_2 = 2 ** math.floor(
|
101 |
+
math.log2(n)
|
102 |
+
) # when the number of heads is not a power of 2, we use this workaround.
|
103 |
+
return (
|
104 |
+
get_slopes_power_of_2(closest_power_of_2)
|
105 |
+
+ AlibiPositionEmbeddingLayer._get_alibi_slopes(
|
106 |
+
2 * closest_power_of_2
|
107 |
+
)[0::2][: n - closest_power_of_2]
|
108 |
+
)
|
109 |
+
|
110 |
+
|
111 |
+
def load_tf_weights_in_btlm(model, config, btlm_checkpoint_path):
|
112 |
+
"""Load tf checkpoints in a pytorch model"""
|
113 |
+
try:
|
114 |
+
import re
|
115 |
+
|
116 |
+
import tensorflow as tf
|
117 |
+
except ImportError:
|
118 |
+
logger.error(
|
119 |
+
"Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see "
|
120 |
+
"https://www.tensorflow.org/install/ for installation instructions."
|
121 |
+
)
|
122 |
+
raise
|
123 |
+
tf_path = os.path.abspath(btlm_checkpoint_path)
|
124 |
+
logger.info(f"Converting TensorFlow checkpoint from {tf_path}")
|
125 |
+
# Load weights from TF model
|
126 |
+
init_vars = tf.train.list_variables(tf_path)
|
127 |
+
names = []
|
128 |
+
arrays = []
|
129 |
+
for name, shape in init_vars:
|
130 |
+
logger.info(f"Loading TF weight {name} with shape {shape}")
|
131 |
+
array = tf.train.load_variable(tf_path, name)
|
132 |
+
names.append(name)
|
133 |
+
arrays.append(array.squeeze())
|
134 |
+
|
135 |
+
for name, array in zip(names, arrays):
|
136 |
+
name = name[6:] # skip "model/"
|
137 |
+
name = name.split("/")
|
138 |
+
pointer = model
|
139 |
+
for m_name in name:
|
140 |
+
if re.fullmatch(r"[A-Za-z]+\d+", m_name):
|
141 |
+
scope_names = re.split(r"(\d+)", m_name)
|
142 |
+
else:
|
143 |
+
scope_names = [m_name]
|
144 |
+
if scope_names[0] == "w" or scope_names[0] == "g":
|
145 |
+
pointer = getattr(pointer, "weight")
|
146 |
+
elif scope_names[0] == "b":
|
147 |
+
pointer = getattr(pointer, "bias")
|
148 |
+
elif scope_names[0] == "wpe" or scope_names[0] == "wte":
|
149 |
+
pointer = getattr(pointer, scope_names[0])
|
150 |
+
pointer = getattr(pointer, "weight")
|
151 |
+
else:
|
152 |
+
pointer = getattr(pointer, scope_names[0])
|
153 |
+
if len(scope_names) >= 2:
|
154 |
+
num = int(scope_names[1])
|
155 |
+
pointer = pointer[num]
|
156 |
+
try:
|
157 |
+
assert (
|
158 |
+
pointer.shape == array.shape
|
159 |
+
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
|
160 |
+
except AssertionError as e:
|
161 |
+
e.args += (pointer.shape, array.shape)
|
162 |
+
raise
|
163 |
+
logger.info(f"Initialize PyTorch weight {name}")
|
164 |
+
pointer.data = torch.from_numpy(array)
|
165 |
+
return model
|
166 |
+
|
167 |
+
|
168 |
+
class BTLMAttention(nn.Module):
|
169 |
+
def __init__(self, config, is_cross_attention=False, layer_idx=None):
|
170 |
+
super().__init__()
|
171 |
+
|
172 |
+
max_positions = config.max_position_embeddings
|
173 |
+
self.register_buffer(
|
174 |
+
"bias",
|
175 |
+
torch.tril(torch.ones((max_positions, max_positions), dtype=torch.bool)).view(
|
176 |
+
1, 1, max_positions, max_positions
|
177 |
+
),
|
178 |
+
persistent=False,
|
179 |
+
)
|
180 |
+
self.register_buffer("masked_bias", torch.tensor(-1e4), persistent=False)
|
181 |
+
|
182 |
+
self.embed_dim = config.hidden_size
|
183 |
+
self.num_heads = config.num_attention_heads
|
184 |
+
self.head_dim = self.embed_dim // self.num_heads
|
185 |
+
self.split_size = self.embed_dim
|
186 |
+
if self.head_dim * self.num_heads != self.embed_dim:
|
187 |
+
raise ValueError(
|
188 |
+
f"`embed_dim` must be divisible by num_heads (got `embed_dim`: {self.embed_dim} and `num_heads`:"
|
189 |
+
f" {self.num_heads})."
|
190 |
+
)
|
191 |
+
|
192 |
+
self.scale_attn_weights = config.scale_attn_weights
|
193 |
+
self.is_cross_attention = is_cross_attention
|
194 |
+
|
195 |
+
# Layer-wise attention scaling, reordering, and upcasting
|
196 |
+
self.scale_attn_by_inverse_layer_idx = config.scale_attn_by_inverse_layer_idx
|
197 |
+
self.layer_idx = layer_idx
|
198 |
+
self.reorder_and_upcast_attn = config.reorder_and_upcast_attn
|
199 |
+
|
200 |
+
if self.is_cross_attention:
|
201 |
+
self.c_attn = Conv1D(2 * self.embed_dim, self.embed_dim)
|
202 |
+
self.q_attn = Conv1D(self.embed_dim, self.embed_dim)
|
203 |
+
else:
|
204 |
+
self.c_attn = Conv1D(3 * self.embed_dim, self.embed_dim)
|
205 |
+
self.c_proj = Conv1D(self.embed_dim, self.embed_dim)
|
206 |
+
|
207 |
+
self.attn_dropout = nn.Dropout(config.attn_pdrop)
|
208 |
+
self.resid_dropout = nn.Dropout(config.resid_pdrop)
|
209 |
+
|
210 |
+
self.pruned_heads = set()
|
211 |
+
|
212 |
+
self.attn_scale_power = 1.0 if config.scale_qk_dot_by_d else 0.5
|
213 |
+
|
214 |
+
def prune_heads(self, heads):
|
215 |
+
if len(heads) == 0:
|
216 |
+
return
|
217 |
+
heads, index = find_pruneable_heads_and_indices(heads, self.num_heads, self.head_dim, self.pruned_heads)
|
218 |
+
index_attn = torch.cat([index, index + self.split_size, index + (2 * self.split_size)])
|
219 |
+
|
220 |
+
# Prune conv1d layers
|
221 |
+
self.c_attn = prune_conv1d_layer(self.c_attn, index_attn, dim=1)
|
222 |
+
self.c_proj = prune_conv1d_layer(self.c_proj, index, dim=0)
|
223 |
+
|
224 |
+
# Update hyper params
|
225 |
+
self.split_size = (self.split_size // self.num_heads) * (self.num_heads - len(heads))
|
226 |
+
self.num_heads = self.num_heads - len(heads)
|
227 |
+
self.pruned_heads = self.pruned_heads.union(heads)
|
228 |
+
|
229 |
+
def _attn(self, query, key, value, attention_mask=None, head_mask=None, position_bias=None):
|
230 |
+
attn_weights = torch.matmul(query, key.transpose(-1, -2))
|
231 |
+
|
232 |
+
if self.scale_attn_weights:
|
233 |
+
attn_weights = attn_weights / torch.full(
|
234 |
+
[], value.size(-1) ** self.attn_scale_power, dtype=attn_weights.dtype, device=attn_weights.device
|
235 |
+
)
|
236 |
+
|
237 |
+
# Layer-wise attention scaling
|
238 |
+
if self.scale_attn_by_inverse_layer_idx:
|
239 |
+
attn_weights = attn_weights / float(self.layer_idx + 1)
|
240 |
+
|
241 |
+
if not self.is_cross_attention:
|
242 |
+
# if only "normal" attention layer implements causal mask
|
243 |
+
query_length, key_length = query.size(-2), key.size(-2)
|
244 |
+
causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length]
|
245 |
+
mask_value = torch.finfo(attn_weights.dtype).min
|
246 |
+
# Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`.
|
247 |
+
# Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device`
|
248 |
+
mask_value = torch.full([], mask_value, dtype=attn_weights.dtype).to(attn_weights.device)
|
249 |
+
attn_weights = torch.where(causal_mask, attn_weights.to(attn_weights.dtype), mask_value)
|
250 |
+
|
251 |
+
if attention_mask is not None:
|
252 |
+
# Apply the attention mask
|
253 |
+
attn_weights = attn_weights + attention_mask
|
254 |
+
|
255 |
+
if position_bias is not None:
|
256 |
+
attn_weights += position_bias.type_as(attn_weights).unsqueeze(0)
|
257 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1)
|
258 |
+
|
259 |
+
# Downcast (if necessary) back to V's dtype (if in mixed-precision) -- No-Op otherwise
|
260 |
+
attn_weights = attn_weights.type(value.dtype)
|
261 |
+
attn_weights = self.attn_dropout(attn_weights)
|
262 |
+
|
263 |
+
# Mask heads if we want to
|
264 |
+
if head_mask is not None:
|
265 |
+
attn_weights = attn_weights * head_mask
|
266 |
+
|
267 |
+
attn_output = torch.matmul(attn_weights, value)
|
268 |
+
|
269 |
+
return attn_output, attn_weights
|
270 |
+
|
271 |
+
def _upcast_and_reordered_attn(self, query, key, value, attention_mask=None, head_mask=None, position_bias=None):
|
272 |
+
# Use `torch.baddbmm` (a bit more efficient w/ alpha param for scaling -- from Megatron-LM)
|
273 |
+
bsz, num_heads, q_seq_len, dk = query.size()
|
274 |
+
_, _, k_seq_len, _ = key.size()
|
275 |
+
|
276 |
+
# Preallocate attn_weights for `baddbmm`
|
277 |
+
attn_weights = torch.empty(bsz * num_heads, q_seq_len, k_seq_len, dtype=torch.float32, device=query.device)
|
278 |
+
|
279 |
+
# Compute Scale Factor
|
280 |
+
scale_factor = 1.0
|
281 |
+
if self.scale_attn_weights:
|
282 |
+
scale_factor /= float(value.size(-1)) ** self.attn_scale_power
|
283 |
+
|
284 |
+
if self.scale_attn_by_inverse_layer_idx:
|
285 |
+
scale_factor /= float(self.layer_idx + 1)
|
286 |
+
|
287 |
+
# Upcast (turn off autocast) and reorder (Scale K by 1 / root(dk))
|
288 |
+
with autocast(enabled=False):
|
289 |
+
q, k = query.reshape(-1, q_seq_len, dk), key.transpose(-1, -2).reshape(-1, dk, k_seq_len)
|
290 |
+
attn_weights = torch.baddbmm(attn_weights, q.float(), k.float(), beta=0, alpha=scale_factor)
|
291 |
+
attn_weights = attn_weights.reshape(bsz, num_heads, q_seq_len, k_seq_len)
|
292 |
+
|
293 |
+
if not self.is_cross_attention:
|
294 |
+
# if only "normal" attention layer implements causal mask
|
295 |
+
query_length, key_length = query.size(-2), key.size(-2)
|
296 |
+
causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length]
|
297 |
+
mask_value = torch.finfo(attn_weights.dtype).min
|
298 |
+
# Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`.
|
299 |
+
# Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device`
|
300 |
+
mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device)
|
301 |
+
attn_weights = torch.where(causal_mask, attn_weights, mask_value)
|
302 |
+
|
303 |
+
if attention_mask is not None:
|
304 |
+
# Apply the attention mask
|
305 |
+
attn_weights = attn_weights + attention_mask
|
306 |
+
|
307 |
+
if position_bias is not None:
|
308 |
+
attn_weights += position_bias.type_as(attn_weights).unsqueeze(0)
|
309 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1)
|
310 |
+
|
311 |
+
# Downcast (if necessary) back to V's dtype (if in mixed-precision) -- No-Op if otherwise
|
312 |
+
if attn_weights.dtype != torch.float32:
|
313 |
+
raise RuntimeError("Error with upcasting, attn_weights does not have dtype torch.float32")
|
314 |
+
attn_weights = attn_weights.type(value.dtype)
|
315 |
+
attn_weights = self.attn_dropout(attn_weights)
|
316 |
+
|
317 |
+
# Mask heads if we want to
|
318 |
+
if head_mask is not None:
|
319 |
+
attn_weights = attn_weights * head_mask
|
320 |
+
|
321 |
+
attn_output = torch.matmul(attn_weights, value)
|
322 |
+
|
323 |
+
return attn_output, attn_weights
|
324 |
+
|
325 |
+
def _split_heads(self, tensor, num_heads, attn_head_size):
|
326 |
+
"""
|
327 |
+
Splits hidden_size dim into attn_head_size and num_heads
|
328 |
+
"""
|
329 |
+
new_shape = tensor.size()[:-1] + (num_heads, attn_head_size)
|
330 |
+
tensor = tensor.view(new_shape)
|
331 |
+
return tensor.permute(0, 2, 1, 3) # (batch, head, seq_length, head_features)
|
332 |
+
|
333 |
+
def _merge_heads(self, tensor, num_heads, attn_head_size):
|
334 |
+
"""
|
335 |
+
Merges attn_head_size dim and num_attn_heads dim into hidden_size
|
336 |
+
"""
|
337 |
+
tensor = tensor.permute(0, 2, 1, 3).contiguous()
|
338 |
+
new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
|
339 |
+
return tensor.view(new_shape)
|
340 |
+
|
341 |
+
def forward(
|
342 |
+
self,
|
343 |
+
hidden_states: Optional[Tuple[torch.FloatTensor]],
|
344 |
+
layer_past: Optional[Tuple[torch.Tensor]] = None,
|
345 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
346 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
347 |
+
encoder_hidden_states: Optional[torch.Tensor] = None,
|
348 |
+
encoder_attention_mask: Optional[torch.FloatTensor] = None,
|
349 |
+
use_cache: Optional[bool] = False,
|
350 |
+
output_attentions: Optional[bool] = False,
|
351 |
+
position_bias: Optional[torch.FloatTensor] = None,
|
352 |
+
) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]:
|
353 |
+
if encoder_hidden_states is not None:
|
354 |
+
if not hasattr(self, "q_attn"):
|
355 |
+
raise ValueError(
|
356 |
+
"If class is used as cross attention, the weights `q_attn` have to be defined. "
|
357 |
+
"Please make sure to instantiate class with `BTLMAttention(..., is_cross_attention=True)`."
|
358 |
+
)
|
359 |
+
|
360 |
+
query = self.q_attn(hidden_states)
|
361 |
+
key, value = self.c_attn(encoder_hidden_states).split(self.split_size, dim=2)
|
362 |
+
attention_mask = encoder_attention_mask
|
363 |
+
else:
|
364 |
+
query, key, value = self.c_attn(hidden_states).split(self.split_size, dim=2)
|
365 |
+
|
366 |
+
query = self._split_heads(query, self.num_heads, self.head_dim)
|
367 |
+
key = self._split_heads(key, self.num_heads, self.head_dim)
|
368 |
+
value = self._split_heads(value, self.num_heads, self.head_dim)
|
369 |
+
|
370 |
+
if layer_past is not None:
|
371 |
+
past_key, past_value = layer_past
|
372 |
+
key = torch.cat((past_key, key), dim=-2)
|
373 |
+
value = torch.cat((past_value, value), dim=-2)
|
374 |
+
|
375 |
+
if use_cache is True:
|
376 |
+
present = (key, value)
|
377 |
+
else:
|
378 |
+
present = None
|
379 |
+
|
380 |
+
if self.reorder_and_upcast_attn:
|
381 |
+
attn_output, attn_weights = self._upcast_and_reordered_attn(query, key, value, attention_mask, head_mask, position_bias)
|
382 |
+
else:
|
383 |
+
attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask, position_bias)
|
384 |
+
|
385 |
+
attn_output = self._merge_heads(attn_output, self.num_heads, self.head_dim)
|
386 |
+
attn_output = self.c_proj(attn_output)
|
387 |
+
attn_output = self.resid_dropout(attn_output)
|
388 |
+
|
389 |
+
outputs = (attn_output, present)
|
390 |
+
if output_attentions:
|
391 |
+
outputs += (attn_weights,)
|
392 |
+
|
393 |
+
return outputs # a, present, (attentions)
|
394 |
+
|
395 |
+
|
396 |
+
class BTLMMLP(nn.Module):
|
397 |
+
def __init__(self, intermediate_size, config):
|
398 |
+
super().__init__()
|
399 |
+
embed_dim = config.hidden_size
|
400 |
+
self.swiglu = config.activation_function == "swiglu"
|
401 |
+
self.c_fc = Conv1D(intermediate_size, embed_dim)
|
402 |
+
self.c_fc2 = Conv1D(intermediate_size, embed_dim) if self.swiglu else None
|
403 |
+
self.c_proj = Conv1D(embed_dim, intermediate_size)
|
404 |
+
self.act = SwiGLUActivation() if self.swiglu else ACT2FN[config.activation_function]
|
405 |
+
self.dropout = nn.Dropout(config.resid_pdrop)
|
406 |
+
|
407 |
+
def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor:
|
408 |
+
if self.swiglu:
|
409 |
+
hidden_states2 = self.c_fc2(hidden_states)
|
410 |
+
hidden_states = self.c_fc(hidden_states)
|
411 |
+
hidden_states = self.act(hidden_states, hidden_states2) if self.swiglu else self.act(hidden_states)
|
412 |
+
hidden_states = self.c_proj(hidden_states)
|
413 |
+
hidden_states = self.dropout(hidden_states)
|
414 |
+
return hidden_states
|
415 |
+
|
416 |
+
|
417 |
+
class BTLMBlock(nn.Module):
|
418 |
+
def __init__(self, config, layer_idx=None):
|
419 |
+
super().__init__()
|
420 |
+
hidden_size = config.hidden_size
|
421 |
+
inner_dim = config.n_inner if config.n_inner is not None else 4 * hidden_size
|
422 |
+
|
423 |
+
self.ln_1 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
|
424 |
+
self.attn = BTLMAttention(config, layer_idx=layer_idx)
|
425 |
+
self.ln_2 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
|
426 |
+
|
427 |
+
if config.add_cross_attention:
|
428 |
+
self.crossattention = BTLMAttention(config, is_cross_attention=True, layer_idx=layer_idx)
|
429 |
+
self.ln_cross_attn = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
|
430 |
+
|
431 |
+
self.mlp = BTLMMLP(inner_dim, config)
|
432 |
+
|
433 |
+
def forward(
|
434 |
+
self,
|
435 |
+
hidden_states: Optional[Tuple[torch.FloatTensor]],
|
436 |
+
layer_past: Optional[Tuple[torch.Tensor]] = None,
|
437 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
438 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
439 |
+
encoder_hidden_states: Optional[torch.Tensor] = None,
|
440 |
+
encoder_attention_mask: Optional[torch.FloatTensor] = None,
|
441 |
+
use_cache: Optional[bool] = False,
|
442 |
+
output_attentions: Optional[bool] = False,
|
443 |
+
position_bias: Optional[torch.FloatTensor] = None,
|
444 |
+
) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]:
|
445 |
+
residual = hidden_states
|
446 |
+
hidden_states = self.ln_1(hidden_states)
|
447 |
+
attn_outputs = self.attn(
|
448 |
+
hidden_states,
|
449 |
+
layer_past=layer_past,
|
450 |
+
attention_mask=attention_mask,
|
451 |
+
head_mask=head_mask,
|
452 |
+
use_cache=use_cache,
|
453 |
+
output_attentions=output_attentions,
|
454 |
+
position_bias=position_bias,
|
455 |
+
)
|
456 |
+
attn_output = attn_outputs[0] # output_attn: a, present, (attentions)
|
457 |
+
outputs = attn_outputs[1:]
|
458 |
+
# residual connection
|
459 |
+
hidden_states = attn_output + residual
|
460 |
+
|
461 |
+
if encoder_hidden_states is not None:
|
462 |
+
# add one self-attention block for cross-attention
|
463 |
+
if not hasattr(self, "crossattention"):
|
464 |
+
raise ValueError(
|
465 |
+
f"If `encoder_hidden_states` are passed, {self} has to be instantiated with "
|
466 |
+
"cross-attention layers by setting `config.add_cross_attention=True`"
|
467 |
+
)
|
468 |
+
residual = hidden_states
|
469 |
+
hidden_states = self.ln_cross_attn(hidden_states)
|
470 |
+
cross_attn_outputs = self.crossattention(
|
471 |
+
hidden_states,
|
472 |
+
attention_mask=attention_mask,
|
473 |
+
head_mask=head_mask,
|
474 |
+
encoder_hidden_states=encoder_hidden_states,
|
475 |
+
encoder_attention_mask=encoder_attention_mask,
|
476 |
+
output_attentions=output_attentions,
|
477 |
+
position_bias=position_bias,
|
478 |
+
)
|
479 |
+
attn_output = cross_attn_outputs[0]
|
480 |
+
# residual connection
|
481 |
+
hidden_states = residual + attn_output
|
482 |
+
outputs = outputs + cross_attn_outputs[2:] # add cross attentions if we output attention weights
|
483 |
+
|
484 |
+
residual = hidden_states
|
485 |
+
hidden_states = self.ln_2(hidden_states)
|
486 |
+
feed_forward_hidden_states = self.mlp(hidden_states)
|
487 |
+
# residual connection
|
488 |
+
hidden_states = residual + feed_forward_hidden_states
|
489 |
+
|
490 |
+
if use_cache:
|
491 |
+
outputs = (hidden_states,) + outputs
|
492 |
+
else:
|
493 |
+
outputs = (hidden_states,) + outputs[1:]
|
494 |
+
|
495 |
+
return outputs # hidden_states, present, (attentions, cross_attentions)
|
496 |
+
|
497 |
+
|
498 |
+
class BTLMPreTrainedModel(PreTrainedModel):
|
499 |
+
"""
|
500 |
+
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
|
501 |
+
models.
|
502 |
+
"""
|
503 |
+
|
504 |
+
config_class = BTLMConfig
|
505 |
+
load_tf_weights = load_tf_weights_in_btlm
|
506 |
+
base_model_prefix = "transformer"
|
507 |
+
is_parallelizable = True
|
508 |
+
supports_gradient_checkpointing = True
|
509 |
+
_no_split_modules = ["BTLMBlock"]
|
510 |
+
_skip_keys_device_placement = "past_key_values"
|
511 |
+
|
512 |
+
def __init__(self, *inputs, **kwargs):
|
513 |
+
super().__init__(*inputs, **kwargs)
|
514 |
+
|
515 |
+
def _init_weights(self, module):
|
516 |
+
"""Initialize the weights."""
|
517 |
+
mup_init_scale = math.sqrt(self.config.width_scale)
|
518 |
+
if isinstance(module, (nn.Linear, Conv1D)):
|
519 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
520 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
521 |
+
module.weight.data.normal_(mean=0.0, std=(self.config.initializer_range * mup_init_scale))
|
522 |
+
if module.bias is not None:
|
523 |
+
module.bias.data.zero_()
|
524 |
+
elif isinstance(module, nn.Embedding):
|
525 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
526 |
+
if module.padding_idx is not None:
|
527 |
+
module.weight.data[module.padding_idx].zero_()
|
528 |
+
elif isinstance(module, nn.LayerNorm):
|
529 |
+
module.bias.data.zero_()
|
530 |
+
module.weight.data.fill_(1.0)
|
531 |
+
|
532 |
+
# Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme:
|
533 |
+
# > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale
|
534 |
+
# > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers.
|
535 |
+
# > -- GPT-2 :: https://openai.com/blog/better-language-models/
|
536 |
+
#
|
537 |
+
# Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py
|
538 |
+
for name, p in module.named_parameters():
|
539 |
+
if name == "c_proj.weight":
|
540 |
+
# Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block
|
541 |
+
stddev = self.config.initializer_range * mup_init_scale / math.sqrt(2 * self.config.n_layer)
|
542 |
+
p.data.normal_(mean=0.0, std=stddev)
|
543 |
+
|
544 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
545 |
+
if isinstance(module, BTLMModel):
|
546 |
+
module.gradient_checkpointing = value
|
547 |
+
|
548 |
+
|
549 |
+
BTLM_START_DOCSTRING = r"""
|
550 |
+
|
551 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
552 |
+
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
553 |
+
etc.)
|
554 |
+
|
555 |
+
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
556 |
+
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
557 |
+
and behavior.
|
558 |
+
|
559 |
+
Parameters:
|
560 |
+
config ([`BTLMConfig`]): Model configuration class with all the parameters of the model.
|
561 |
+
Initializing with a config file does not load the weights associated with the model, only the
|
562 |
+
configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
563 |
+
"""
|
564 |
+
|
565 |
+
BTLM_INPUTS_DOCSTRING = r"""
|
566 |
+
Args:
|
567 |
+
input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
|
568 |
+
`input_ids_length` = `sequence_length` if `past_key_values` is `None` else
|
569 |
+
`past_key_values[0][0].shape[-2]` (`sequence_length` of input past key value states). Indices of input
|
570 |
+
sequence tokens in the vocabulary.
|
571 |
+
|
572 |
+
If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
|
573 |
+
`input_ids`.
|
574 |
+
|
575 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
576 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
577 |
+
|
578 |
+
[What are input IDs?](../glossary#input-ids)
|
579 |
+
past_key_values (`Tuple[Tuple[torch.Tensor]]` of length `config.n_layers`):
|
580 |
+
Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see
|
581 |
+
`past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have
|
582 |
+
their past given to this model should not be passed as `input_ids` as they have already been computed.
|
583 |
+
attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
584 |
+
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
585 |
+
|
586 |
+
- 1 for tokens that are **not masked**,
|
587 |
+
- 0 for tokens that are **masked**.
|
588 |
+
|
589 |
+
If `past_key_values` is used, `attention_mask` needs to contain the masking strategy that was used for
|
590 |
+
`past_key_values`. In other words, the `attention_mask` always has to have the length:
|
591 |
+
`len(past_key_values) + len(input_ids)`
|
592 |
+
|
593 |
+
[What are attention masks?](../glossary#attention-mask)
|
594 |
+
token_type_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`, *optional*):
|
595 |
+
Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
|
596 |
+
1]`:
|
597 |
+
|
598 |
+
- 0 corresponds to a *sentence A* token,
|
599 |
+
- 1 corresponds to a *sentence B* token.
|
600 |
+
|
601 |
+
[What are token type IDs?](../glossary#token-type-ids)
|
602 |
+
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
603 |
+
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
604 |
+
config.max_position_embeddings - 1]`.
|
605 |
+
|
606 |
+
[What are position IDs?](../glossary#position-ids)
|
607 |
+
head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
|
608 |
+
Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:
|
609 |
+
|
610 |
+
- 1 indicates the head is **not masked**,
|
611 |
+
- 0 indicates the head is **masked**.
|
612 |
+
|
613 |
+
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
614 |
+
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
|
615 |
+
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
|
616 |
+
model's internal embedding lookup matrix.
|
617 |
+
|
618 |
+
If `past_key_values` is used, optionally only the last `inputs_embeds` have to be input (see
|
619 |
+
`past_key_values`).
|
620 |
+
use_cache (`bool`, *optional*):
|
621 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
|
622 |
+
`past_key_values`).
|
623 |
+
output_attentions (`bool`, *optional*):
|
624 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
|
625 |
+
tensors for more detail.
|
626 |
+
output_hidden_states (`bool`, *optional*):
|
627 |
+
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
|
628 |
+
more detail.
|
629 |
+
return_dict (`bool`, *optional*):
|
630 |
+
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
|
631 |
+
"""
|
632 |
+
PARALLELIZE_DOCSTRING = r"""
|
633 |
+
This is an experimental feature and is a subject to change at a moment's notice.
|
634 |
+
|
635 |
+
Uses a device map to distribute attention modules of the model across several devices. If no device map is given,
|
636 |
+
it will evenly distribute blocks across all devices.
|
637 |
+
|
638 |
+
Args:
|
639 |
+
device_map (`Dict[int, list]`, optional, defaults to None):
|
640 |
+
A dictionary that maps attention modules to devices. Note that the embedding module and LMHead are always
|
641 |
+
automatically mapped to the first device (for esoteric reasons). That means that the first device should
|
642 |
+
have fewer attention modules mapped to it than other devices. For reference, the gpt2 models have the
|
643 |
+
following number of attention modules:
|
644 |
+
|
645 |
+
- gpt2: 12
|
646 |
+
- gpt2-medium: 24
|
647 |
+
- gpt2-large: 36
|
648 |
+
- gpt2-xl: 48
|
649 |
+
|
650 |
+
Example:
|
651 |
+
|
652 |
+
```python
|
653 |
+
# Here is an example of a device map on a machine with 4 GPUs using gpt2-xl, which has a total of 48 attention modules:
|
654 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2-xl")
|
655 |
+
device_map = {
|
656 |
+
0: [0, 1, 2, 3, 4, 5, 6, 7, 8],
|
657 |
+
1: [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
|
658 |
+
2: [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34],
|
659 |
+
3: [35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],
|
660 |
+
}
|
661 |
+
model.parallelize(device_map)
|
662 |
+
```
|
663 |
+
"""
|
664 |
+
DEPARALLELIZE_DOCSTRING = r"""
|
665 |
+
Moves the model to cpu from a model parallel state.
|
666 |
+
|
667 |
+
Example:
|
668 |
+
|
669 |
+
```python
|
670 |
+
# On a 4 GPU machine with gpt2-large:
|
671 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2-large")
|
672 |
+
device_map = {
|
673 |
+
0: [0, 1, 2, 3, 4, 5, 6, 7],
|
674 |
+
1: [8, 9, 10, 11, 12, 13, 14, 15],
|
675 |
+
2: [16, 17, 18, 19, 20, 21, 22, 23],
|
676 |
+
3: [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
|
677 |
+
}
|
678 |
+
model.parallelize(device_map) # Splits the model across several devices
|
679 |
+
model.deparallelize() # Put the model back on cpu and cleans memory by calling torch.cuda.empty_cache()
|
680 |
+
```
|
681 |
+
"""
|
682 |
+
|
683 |
+
|
684 |
+
@add_start_docstrings(
|
685 |
+
"The bare BTLM Model transformer outputting raw hidden-states without any specific head on top.",
|
686 |
+
BTLM_START_DOCSTRING,
|
687 |
+
)
|
688 |
+
class BTLMModel(BTLMPreTrainedModel):
|
689 |
+
_keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.bias", r"h\.\d+\.attn\.masked_bias"]
|
690 |
+
_keys_to_ignore_on_load_missing = [r"attn.masked_bias", r"h\.\d+\.attn\.masked_bias", r"h\.\d+\.attn\.bias"]
|
691 |
+
|
692 |
+
def __init__(self, config):
|
693 |
+
super().__init__(config)
|
694 |
+
|
695 |
+
self.embed_dim = config.hidden_size
|
696 |
+
|
697 |
+
self.wte = nn.Embedding(config.vocab_size, self.embed_dim)
|
698 |
+
self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim) if config.position_embedding_type != "alibi" else None
|
699 |
+
self.embeddings_scale = config.embeddings_scale
|
700 |
+
|
701 |
+
self.drop = nn.Dropout(config.embd_pdrop)
|
702 |
+
self.h = nn.ModuleList([BTLMBlock(config, layer_idx=i) for i in range(config.num_hidden_layers)])
|
703 |
+
self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
|
704 |
+
|
705 |
+
self.relative_pe = AlibiPositionEmbeddingLayer(config.num_attention_heads) if config.position_embedding_type == "alibi" else None
|
706 |
+
|
707 |
+
# Model parallel
|
708 |
+
self.model_parallel = False
|
709 |
+
self.device_map = None
|
710 |
+
self.gradient_checkpointing = False
|
711 |
+
|
712 |
+
# Initialize weights and apply final processing
|
713 |
+
self.post_init()
|
714 |
+
|
715 |
+
@add_start_docstrings(PARALLELIZE_DOCSTRING)
|
716 |
+
def parallelize(self, device_map=None):
|
717 |
+
# Check validity of device_map
|
718 |
+
warnings.warn(
|
719 |
+
"`BTLMModel.parallelize` is deprecated and will be removed in v5 of Transformers, you should load your"
|
720 |
+
" model with `device_map='balanced'` in the call to `from_pretrained`. You can also provide your own"
|
721 |
+
" `device_map` but it needs to be a dictionary module_name to device, so for instance {'h.0': 0, 'h.1': 1,"
|
722 |
+
" ...}",
|
723 |
+
FutureWarning,
|
724 |
+
)
|
725 |
+
self.device_map = (
|
726 |
+
get_device_map(len(self.h), range(torch.cuda.device_count())) if device_map is None else device_map
|
727 |
+
)
|
728 |
+
assert_device_map(self.device_map, len(self.h))
|
729 |
+
self.model_parallel = True
|
730 |
+
self.first_device = "cpu" if "cpu" in self.device_map.keys() else "cuda:" + str(min(self.device_map.keys()))
|
731 |
+
self.last_device = "cuda:" + str(max(self.device_map.keys()))
|
732 |
+
self.wte = self.wte.to(self.first_device)
|
733 |
+
if self.wpe is not None:
|
734 |
+
self.wpe = self.wpe.to(self.first_device)
|
735 |
+
# Load onto devices
|
736 |
+
for k, v in self.device_map.items():
|
737 |
+
for block in v:
|
738 |
+
cuda_device = "cuda:" + str(k)
|
739 |
+
self.h[block] = self.h[block].to(cuda_device)
|
740 |
+
# ln_f to last
|
741 |
+
self.ln_f = self.ln_f.to(self.last_device)
|
742 |
+
|
743 |
+
@add_start_docstrings(DEPARALLELIZE_DOCSTRING)
|
744 |
+
def deparallelize(self):
|
745 |
+
warnings.warn(
|
746 |
+
"Like `parallelize`, `deparallelize` is deprecated and will be removed in v5 of Transformers.",
|
747 |
+
FutureWarning,
|
748 |
+
)
|
749 |
+
self.model_parallel = False
|
750 |
+
self.device_map = None
|
751 |
+
self.first_device = "cpu"
|
752 |
+
self.last_device = "cpu"
|
753 |
+
self.wte = self.wte.to("cpu")
|
754 |
+
if self.wpe is not None:
|
755 |
+
self.wpe = self.wpe.to("cpu")
|
756 |
+
for index in range(len(self.h)):
|
757 |
+
self.h[index] = self.h[index].to("cpu")
|
758 |
+
self.ln_f = self.ln_f.to("cpu")
|
759 |
+
torch.cuda.empty_cache()
|
760 |
+
|
761 |
+
def get_input_embeddings(self):
|
762 |
+
return self.wte
|
763 |
+
|
764 |
+
def set_input_embeddings(self, new_embeddings):
|
765 |
+
self.wte = new_embeddings
|
766 |
+
|
767 |
+
def _prune_heads(self, heads_to_prune):
|
768 |
+
"""
|
769 |
+
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer}
|
770 |
+
"""
|
771 |
+
for layer, heads in heads_to_prune.items():
|
772 |
+
self.h[layer].attn.prune_heads(heads)
|
773 |
+
|
774 |
+
@add_start_docstrings_to_model_forward(BTLM_INPUTS_DOCSTRING)
|
775 |
+
@add_code_sample_docstrings(
|
776 |
+
checkpoint=_CHECKPOINT_FOR_DOC,
|
777 |
+
output_type=BaseModelOutputWithPastAndCrossAttentions,
|
778 |
+
config_class=_CONFIG_FOR_DOC,
|
779 |
+
)
|
780 |
+
def forward(
|
781 |
+
self,
|
782 |
+
input_ids: Optional[torch.LongTensor] = None,
|
783 |
+
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
|
784 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
785 |
+
token_type_ids: Optional[torch.LongTensor] = None,
|
786 |
+
position_ids: Optional[torch.LongTensor] = None,
|
787 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
788 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
789 |
+
encoder_hidden_states: Optional[torch.Tensor] = None,
|
790 |
+
encoder_attention_mask: Optional[torch.FloatTensor] = None,
|
791 |
+
use_cache: Optional[bool] = None,
|
792 |
+
output_attentions: Optional[bool] = None,
|
793 |
+
output_hidden_states: Optional[bool] = None,
|
794 |
+
return_dict: Optional[bool] = None,
|
795 |
+
) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]:
|
796 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
797 |
+
output_hidden_states = (
|
798 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
799 |
+
)
|
800 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
801 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
802 |
+
|
803 |
+
if input_ids is not None and inputs_embeds is not None:
|
804 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
805 |
+
elif input_ids is not None:
|
806 |
+
input_shape = input_ids.size()
|
807 |
+
input_ids = input_ids.view(-1, input_shape[-1])
|
808 |
+
batch_size = input_ids.shape[0]
|
809 |
+
elif inputs_embeds is not None:
|
810 |
+
input_shape = inputs_embeds.size()[:-1]
|
811 |
+
batch_size = inputs_embeds.shape[0]
|
812 |
+
else:
|
813 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
814 |
+
|
815 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
816 |
+
|
817 |
+
if token_type_ids is not None:
|
818 |
+
token_type_ids = token_type_ids.view(-1, input_shape[-1])
|
819 |
+
if position_ids is not None:
|
820 |
+
position_ids = position_ids.view(-1, input_shape[-1])
|
821 |
+
|
822 |
+
if past_key_values is None:
|
823 |
+
past_length = 0
|
824 |
+
past_key_values = tuple([None] * len(self.h))
|
825 |
+
else:
|
826 |
+
past_length = past_key_values[0][0].size(-2)
|
827 |
+
if position_ids is None:
|
828 |
+
position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device)
|
829 |
+
position_ids = position_ids.unsqueeze(0).view(-1, input_shape[-1])
|
830 |
+
|
831 |
+
# BTLMAttention mask.
|
832 |
+
if attention_mask is not None:
|
833 |
+
if batch_size <= 0:
|
834 |
+
raise ValueError("batch_size has to be defined and > 0")
|
835 |
+
attention_mask = attention_mask.view(batch_size, -1)
|
836 |
+
# We create a 3D attention mask from a 2D tensor mask.
|
837 |
+
# Sizes are [batch_size, 1, 1, to_seq_length]
|
838 |
+
# So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length]
|
839 |
+
# this attention mask is more simple than the triangular masking of causal attention
|
840 |
+
# used in OpenAI GPT, we just need to prepare the broadcast dimension here.
|
841 |
+
attention_mask = attention_mask[:, None, None, :]
|
842 |
+
|
843 |
+
# Since attention_mask is 1.0 for positions we want to attend and 0.0 for
|
844 |
+
# masked positions, this operation will create a tensor which is 0.0 for
|
845 |
+
# positions we want to attend and the dtype's smallest value for masked positions.
|
846 |
+
# Since we are adding it to the raw scores before the softmax, this is
|
847 |
+
# effectively the same as removing these entirely.
|
848 |
+
attention_mask = attention_mask.to(dtype=self.dtype) # fp16 compatibility
|
849 |
+
attention_mask = (1.0 - attention_mask) * torch.finfo(self.dtype).min
|
850 |
+
|
851 |
+
# If a 2D or 3D attention mask is provided for the cross-attention
|
852 |
+
# we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
|
853 |
+
if self.config.add_cross_attention and encoder_hidden_states is not None:
|
854 |
+
encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states.size()
|
855 |
+
encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length)
|
856 |
+
if encoder_attention_mask is None:
|
857 |
+
encoder_attention_mask = torch.ones(encoder_hidden_shape, device=device)
|
858 |
+
encoder_attention_mask = self.invert_attention_mask(encoder_attention_mask)
|
859 |
+
else:
|
860 |
+
encoder_attention_mask = None
|
861 |
+
|
862 |
+
# Prepare head mask if needed
|
863 |
+
# 1.0 in head_mask indicate we keep the head
|
864 |
+
# attention_probs has shape bsz x n_heads x N x N
|
865 |
+
# head_mask has shape n_layer x batch x n_heads x N x N
|
866 |
+
head_mask = self.get_head_mask(head_mask, self.config.n_layer)
|
867 |
+
|
868 |
+
if inputs_embeds is None:
|
869 |
+
inputs_embeds = self.wte(input_ids)
|
870 |
+
if self.wpe is not None:
|
871 |
+
position_embeds = self.wpe(position_ids)
|
872 |
+
hidden_states = inputs_embeds + position_embeds
|
873 |
+
else:
|
874 |
+
hidden_states = inputs_embeds
|
875 |
+
hidden_states *= torch.tensor(
|
876 |
+
float(self.embeddings_scale), dtype=hidden_states.dtype, device=hidden_states.device
|
877 |
+
)
|
878 |
+
|
879 |
+
if token_type_ids is not None:
|
880 |
+
token_type_embeds = self.wte(token_type_ids)
|
881 |
+
hidden_states = hidden_states + token_type_embeds
|
882 |
+
|
883 |
+
hidden_states = self.drop(hidden_states)
|
884 |
+
|
885 |
+
if self.relative_pe is not None:
|
886 |
+
length = input_ids.shape[1]
|
887 |
+
cached_kv_length = 0
|
888 |
+
cached_kv = past_key_values[0]
|
889 |
+
if cached_kv is not None:
|
890 |
+
cached_kv_length = cached_kv[0].shape[-2]
|
891 |
+
position_bias = self.relative_pe(length, length, cached_kv_length)
|
892 |
+
else:
|
893 |
+
position_bias = None
|
894 |
+
|
895 |
+
output_shape = input_shape + (hidden_states.size(-1),)
|
896 |
+
|
897 |
+
if self.gradient_checkpointing and self.training:
|
898 |
+
if use_cache:
|
899 |
+
logger.warning_once(
|
900 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
901 |
+
)
|
902 |
+
use_cache = False
|
903 |
+
|
904 |
+
presents = () if use_cache else None
|
905 |
+
all_self_attentions = () if output_attentions else None
|
906 |
+
all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None
|
907 |
+
all_hidden_states = () if output_hidden_states else None
|
908 |
+
for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)):
|
909 |
+
# Model parallel
|
910 |
+
if self.model_parallel:
|
911 |
+
torch.cuda.set_device(hidden_states.device)
|
912 |
+
# Ensure layer_past is on same device as hidden_states (might not be correct)
|
913 |
+
if layer_past is not None:
|
914 |
+
layer_past = tuple(past_state.to(hidden_states.device) for past_state in layer_past)
|
915 |
+
# Ensure that attention_mask is always on the same device as hidden_states
|
916 |
+
if attention_mask is not None:
|
917 |
+
attention_mask = attention_mask.to(hidden_states.device)
|
918 |
+
if isinstance(head_mask, torch.Tensor):
|
919 |
+
head_mask = head_mask.to(hidden_states.device)
|
920 |
+
if output_hidden_states:
|
921 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
922 |
+
|
923 |
+
if self.gradient_checkpointing and self.training:
|
924 |
+
|
925 |
+
def create_custom_forward(module):
|
926 |
+
def custom_forward(*inputs):
|
927 |
+
# None for past_key_value
|
928 |
+
return module(*inputs, use_cache, output_attentions)
|
929 |
+
|
930 |
+
return custom_forward
|
931 |
+
|
932 |
+
outputs = torch.utils.checkpoint.checkpoint(
|
933 |
+
create_custom_forward(block),
|
934 |
+
hidden_states,
|
935 |
+
None,
|
936 |
+
attention_mask,
|
937 |
+
head_mask[i],
|
938 |
+
encoder_hidden_states,
|
939 |
+
encoder_attention_mask,
|
940 |
+
)
|
941 |
+
else:
|
942 |
+
outputs = block(
|
943 |
+
hidden_states,
|
944 |
+
layer_past=layer_past,
|
945 |
+
attention_mask=attention_mask,
|
946 |
+
head_mask=head_mask[i],
|
947 |
+
encoder_hidden_states=encoder_hidden_states,
|
948 |
+
encoder_attention_mask=encoder_attention_mask,
|
949 |
+
use_cache=use_cache,
|
950 |
+
output_attentions=output_attentions,
|
951 |
+
position_bias=position_bias,
|
952 |
+
)
|
953 |
+
|
954 |
+
hidden_states = outputs[0]
|
955 |
+
if use_cache is True:
|
956 |
+
presents = presents + (outputs[1],)
|
957 |
+
|
958 |
+
if output_attentions:
|
959 |
+
all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
|
960 |
+
if self.config.add_cross_attention:
|
961 |
+
all_cross_attentions = all_cross_attentions + (outputs[3 if use_cache else 2],)
|
962 |
+
|
963 |
+
# Model Parallel: If it's the last layer for that device, put things on the next device
|
964 |
+
if self.model_parallel:
|
965 |
+
for k, v in self.device_map.items():
|
966 |
+
if i == v[-1] and "cuda:" + str(k) != self.last_device:
|
967 |
+
hidden_states = hidden_states.to("cuda:" + str(k + 1))
|
968 |
+
|
969 |
+
hidden_states = self.ln_f(hidden_states)
|
970 |
+
|
971 |
+
hidden_states = hidden_states.view(output_shape)
|
972 |
+
# Add last hidden state
|
973 |
+
if output_hidden_states:
|
974 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
975 |
+
|
976 |
+
if not return_dict:
|
977 |
+
return tuple(
|
978 |
+
v
|
979 |
+
for v in [hidden_states, presents, all_hidden_states, all_self_attentions, all_cross_attentions]
|
980 |
+
if v is not None
|
981 |
+
)
|
982 |
+
|
983 |
+
return BaseModelOutputWithPastAndCrossAttentions(
|
984 |
+
last_hidden_state=hidden_states,
|
985 |
+
past_key_values=presents,
|
986 |
+
hidden_states=all_hidden_states,
|
987 |
+
attentions=all_self_attentions,
|
988 |
+
cross_attentions=all_cross_attentions,
|
989 |
+
)
|
990 |
+
|
991 |
+
|
992 |
+
@add_start_docstrings(
|
993 |
+
"""
|
994 |
+
The BTLM Model transformer with a language modeling head on top (linear layer with weights tied to the input
|
995 |
+
embeddings).
|
996 |
+
""",
|
997 |
+
BTLM_START_DOCSTRING,
|
998 |
+
)
|
999 |
+
class BTLMLMHeadModel(BTLMPreTrainedModel):
|
1000 |
+
_keys_to_ignore_on_load_missing = [r"lm_head.weight"]
|
1001 |
+
_keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.masked_bias", r"h\.\d+\.attn\.bias"]
|
1002 |
+
|
1003 |
+
def __init__(self, config):
|
1004 |
+
super().__init__(config)
|
1005 |
+
self.transformer = BTLMModel(config)
|
1006 |
+
self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
|
1007 |
+
self.output_logits_scale = config.width_scale
|
1008 |
+
|
1009 |
+
# Model parallel
|
1010 |
+
self.model_parallel = False
|
1011 |
+
self.device_map = None
|
1012 |
+
|
1013 |
+
# Initialize weights and apply final processing
|
1014 |
+
self.post_init()
|
1015 |
+
|
1016 |
+
@add_start_docstrings(PARALLELIZE_DOCSTRING)
|
1017 |
+
def parallelize(self, device_map=None):
|
1018 |
+
warnings.warn(
|
1019 |
+
"`BTLMLMHeadModel.parallelize` is deprecated and will be removed in v5 of Transformers, you should load"
|
1020 |
+
" your model with `device_map='balanced'` in the call to `from_pretrained`. You can also provide your own"
|
1021 |
+
" `device_map` but it needs to be a dictionary module_name to device, so for instance {'transformer.h.0':"
|
1022 |
+
" 0, 'transformer.h.1': 1, ...}",
|
1023 |
+
FutureWarning,
|
1024 |
+
)
|
1025 |
+
self.device_map = (
|
1026 |
+
get_device_map(len(self.transformer.h), range(torch.cuda.device_count()))
|
1027 |
+
if device_map is None
|
1028 |
+
else device_map
|
1029 |
+
)
|
1030 |
+
assert_device_map(self.device_map, len(self.transformer.h))
|
1031 |
+
self.transformer.parallelize(self.device_map)
|
1032 |
+
self.lm_head = self.lm_head.to(self.transformer.first_device)
|
1033 |
+
self.model_parallel = True
|
1034 |
+
|
1035 |
+
@add_start_docstrings(DEPARALLELIZE_DOCSTRING)
|
1036 |
+
def deparallelize(self):
|
1037 |
+
warnings.warn(
|
1038 |
+
"Like `parallelize`, `deparallelize` is deprecated and will be removed in v5 of Transformers.",
|
1039 |
+
FutureWarning,
|
1040 |
+
)
|
1041 |
+
self.transformer.deparallelize()
|
1042 |
+
self.transformer = self.transformer.to("cpu")
|
1043 |
+
self.lm_head = self.lm_head.to("cpu")
|
1044 |
+
self.model_parallel = False
|
1045 |
+
torch.cuda.empty_cache()
|
1046 |
+
|
1047 |
+
def get_output_embeddings(self):
|
1048 |
+
return self.lm_head
|
1049 |
+
|
1050 |
+
def set_output_embeddings(self, new_embeddings):
|
1051 |
+
self.lm_head = new_embeddings
|
1052 |
+
|
1053 |
+
def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs):
|
1054 |
+
token_type_ids = kwargs.get("token_type_ids", None)
|
1055 |
+
# only last token for inputs_ids if past is defined in kwargs
|
1056 |
+
if past_key_values:
|
1057 |
+
input_ids = input_ids[:, -1].unsqueeze(-1)
|
1058 |
+
if token_type_ids is not None:
|
1059 |
+
token_type_ids = token_type_ids[:, -1].unsqueeze(-1)
|
1060 |
+
|
1061 |
+
attention_mask = kwargs.get("attention_mask", None)
|
1062 |
+
position_ids = kwargs.get("position_ids", None)
|
1063 |
+
|
1064 |
+
if attention_mask is not None and position_ids is None:
|
1065 |
+
# create position_ids on the fly for batch generation
|
1066 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
1067 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
1068 |
+
if past_key_values:
|
1069 |
+
position_ids = position_ids[:, -1].unsqueeze(-1)
|
1070 |
+
else:
|
1071 |
+
position_ids = None
|
1072 |
+
|
1073 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
1074 |
+
if inputs_embeds is not None and past_key_values is None:
|
1075 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
1076 |
+
else:
|
1077 |
+
model_inputs = {"input_ids": input_ids}
|
1078 |
+
|
1079 |
+
model_inputs.update(
|
1080 |
+
{
|
1081 |
+
"past_key_values": past_key_values,
|
1082 |
+
"use_cache": kwargs.get("use_cache"),
|
1083 |
+
"position_ids": position_ids,
|
1084 |
+
"attention_mask": attention_mask,
|
1085 |
+
"token_type_ids": token_type_ids,
|
1086 |
+
}
|
1087 |
+
)
|
1088 |
+
return model_inputs
|
1089 |
+
|
1090 |
+
@add_start_docstrings_to_model_forward(BTLM_INPUTS_DOCSTRING)
|
1091 |
+
@add_code_sample_docstrings(
|
1092 |
+
checkpoint=_CHECKPOINT_FOR_DOC,
|
1093 |
+
output_type=CausalLMOutputWithCrossAttentions,
|
1094 |
+
config_class=_CONFIG_FOR_DOC,
|
1095 |
+
)
|
1096 |
+
def forward(
|
1097 |
+
self,
|
1098 |
+
input_ids: Optional[torch.LongTensor] = None,
|
1099 |
+
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
|
1100 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
1101 |
+
token_type_ids: Optional[torch.LongTensor] = None,
|
1102 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1103 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
1104 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1105 |
+
encoder_hidden_states: Optional[torch.Tensor] = None,
|
1106 |
+
encoder_attention_mask: Optional[torch.FloatTensor] = None,
|
1107 |
+
labels: Optional[torch.LongTensor] = None,
|
1108 |
+
use_cache: Optional[bool] = None,
|
1109 |
+
output_attentions: Optional[bool] = None,
|
1110 |
+
output_hidden_states: Optional[bool] = None,
|
1111 |
+
return_dict: Optional[bool] = None,
|
1112 |
+
) -> Union[Tuple, CausalLMOutputWithCrossAttentions]:
|
1113 |
+
r"""
|
1114 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
1115 |
+
Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
|
1116 |
+
`labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
|
1117 |
+
are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
|
1118 |
+
"""
|
1119 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1120 |
+
|
1121 |
+
transformer_outputs = self.transformer(
|
1122 |
+
input_ids,
|
1123 |
+
past_key_values=past_key_values,
|
1124 |
+
attention_mask=attention_mask,
|
1125 |
+
token_type_ids=token_type_ids,
|
1126 |
+
position_ids=position_ids,
|
1127 |
+
head_mask=head_mask,
|
1128 |
+
inputs_embeds=inputs_embeds,
|
1129 |
+
encoder_hidden_states=encoder_hidden_states,
|
1130 |
+
encoder_attention_mask=encoder_attention_mask,
|
1131 |
+
use_cache=use_cache,
|
1132 |
+
output_attentions=output_attentions,
|
1133 |
+
output_hidden_states=output_hidden_states,
|
1134 |
+
return_dict=return_dict,
|
1135 |
+
)
|
1136 |
+
hidden_states = transformer_outputs[0]
|
1137 |
+
|
1138 |
+
# Set device for model parallelism
|
1139 |
+
if self.model_parallel:
|
1140 |
+
torch.cuda.set_device(self.transformer.first_device)
|
1141 |
+
hidden_states = hidden_states.to(self.lm_head.weight.device)
|
1142 |
+
|
1143 |
+
lm_logits = self.lm_head(hidden_states)
|
1144 |
+
lm_logits *= torch.tensor(
|
1145 |
+
float(self.output_logits_scale), dtype=lm_logits.dtype, device=lm_logits.device
|
1146 |
+
)
|
1147 |
+
|
1148 |
+
loss = None
|
1149 |
+
if labels is not None:
|
1150 |
+
# move labels to correct device to enable model parallelism
|
1151 |
+
labels = labels.to(lm_logits.device)
|
1152 |
+
# Shift so that tokens < n predict n
|
1153 |
+
shift_logits = lm_logits[..., :-1, :].contiguous()
|
1154 |
+
shift_labels = labels[..., 1:].contiguous()
|
1155 |
+
# Flatten the tokens
|
1156 |
+
loss_fct = CrossEntropyLoss()
|
1157 |
+
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
|
1158 |
+
|
1159 |
+
if not return_dict:
|
1160 |
+
output = (lm_logits,) + transformer_outputs[1:]
|
1161 |
+
return ((loss,) + output) if loss is not None else output
|
1162 |
+
|
1163 |
+
return CausalLMOutputWithCrossAttentions(
|
1164 |
+
loss=loss,
|
1165 |
+
logits=lm_logits,
|
1166 |
+
past_key_values=transformer_outputs.past_key_values,
|
1167 |
+
hidden_states=transformer_outputs.hidden_states,
|
1168 |
+
attentions=transformer_outputs.attentions,
|
1169 |
+
cross_attentions=transformer_outputs.cross_attentions,
|
1170 |
+
)
|
1171 |
+
|
1172 |
+
@staticmethod
|
1173 |
+
def _reorder_cache(
|
1174 |
+
past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor
|
1175 |
+
) -> Tuple[Tuple[torch.Tensor]]:
|
1176 |
+
"""
|
1177 |
+
This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or
|
1178 |
+
[`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct
|
1179 |
+
beam_idx at every generation step.
|
1180 |
+
"""
|
1181 |
+
return tuple(
|
1182 |
+
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past)
|
1183 |
+
for layer_past in past_key_values
|
1184 |
+
)
|
1185 |
+
|
1186 |
+
|
1187 |
+
@add_start_docstrings(
|
1188 |
+
"""
|
1189 |
+
The BTLM Model transformer with a sequence classification head on top (linear layer).
|
1190 |
+
|
1191 |
+
[`BTLMForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
1192 |
+
(e.g. GPT-1) do.
|
1193 |
+
|
1194 |
+
Since it does classification on the last token, it requires to know the position of the last token. If a
|
1195 |
+
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
1196 |
+
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
1197 |
+
padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
|
1198 |
+
each row of the batch).
|
1199 |
+
""",
|
1200 |
+
BTLM_START_DOCSTRING,
|
1201 |
+
)
|
1202 |
+
class BTLMForSequenceClassification(BTLMPreTrainedModel):
|
1203 |
+
_keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.bias", r"h\.\d+\.attn\.masked_bias"]
|
1204 |
+
_keys_to_ignore_on_load_missing = [r"h\.\d+\.attn\.masked_bias", r"lm_head.weight"]
|
1205 |
+
|
1206 |
+
def __init__(self, config):
|
1207 |
+
super().__init__(config)
|
1208 |
+
self.num_labels = config.num_labels
|
1209 |
+
self.transformer = BTLMModel(config)
|
1210 |
+
self.score = nn.Linear(config.n_embd, self.num_labels, bias=False)
|
1211 |
+
self.output_logits_scale = config.width_scale
|
1212 |
+
|
1213 |
+
# Model parallel
|
1214 |
+
self.model_parallel = False
|
1215 |
+
self.device_map = None
|
1216 |
+
|
1217 |
+
# Initialize weights and apply final processing
|
1218 |
+
self.post_init()
|
1219 |
+
|
1220 |
+
@add_start_docstrings_to_model_forward(BTLM_INPUTS_DOCSTRING)
|
1221 |
+
@add_code_sample_docstrings(
|
1222 |
+
checkpoint="microsoft/DialogRPT-updown",
|
1223 |
+
output_type=SequenceClassifierOutputWithPast,
|
1224 |
+
config_class=_CONFIG_FOR_DOC,
|
1225 |
+
)
|
1226 |
+
def forward(
|
1227 |
+
self,
|
1228 |
+
input_ids: Optional[torch.LongTensor] = None,
|
1229 |
+
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
|
1230 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
1231 |
+
token_type_ids: Optional[torch.LongTensor] = None,
|
1232 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1233 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
1234 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1235 |
+
labels: Optional[torch.LongTensor] = None,
|
1236 |
+
use_cache: Optional[bool] = None,
|
1237 |
+
output_attentions: Optional[bool] = None,
|
1238 |
+
output_hidden_states: Optional[bool] = None,
|
1239 |
+
return_dict: Optional[bool] = None,
|
1240 |
+
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
1241 |
+
r"""
|
1242 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
1243 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
1244 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
1245 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
1246 |
+
"""
|
1247 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1248 |
+
|
1249 |
+
transformer_outputs = self.transformer(
|
1250 |
+
input_ids,
|
1251 |
+
past_key_values=past_key_values,
|
1252 |
+
attention_mask=attention_mask,
|
1253 |
+
token_type_ids=token_type_ids,
|
1254 |
+
position_ids=position_ids,
|
1255 |
+
head_mask=head_mask,
|
1256 |
+
inputs_embeds=inputs_embeds,
|
1257 |
+
use_cache=use_cache,
|
1258 |
+
output_attentions=output_attentions,
|
1259 |
+
output_hidden_states=output_hidden_states,
|
1260 |
+
return_dict=return_dict,
|
1261 |
+
)
|
1262 |
+
hidden_states = transformer_outputs[0]
|
1263 |
+
logits = self.score(hidden_states)
|
1264 |
+
logits *= torch.tensor(
|
1265 |
+
float(self.output_logits_scale), dtype=logits.dtype, device=logits.device
|
1266 |
+
)
|
1267 |
+
|
1268 |
+
if input_ids is not None:
|
1269 |
+
batch_size, sequence_length = input_ids.shape[:2]
|
1270 |
+
else:
|
1271 |
+
batch_size, sequence_length = inputs_embeds.shape[:2]
|
1272 |
+
|
1273 |
+
assert (
|
1274 |
+
self.config.pad_token_id is not None or batch_size == 1
|
1275 |
+
), "Cannot handle batch sizes > 1 if no padding token is defined."
|
1276 |
+
if self.config.pad_token_id is None:
|
1277 |
+
sequence_lengths = -1
|
1278 |
+
else:
|
1279 |
+
if input_ids is not None:
|
1280 |
+
sequence_lengths = (torch.ne(input_ids, self.config.pad_token_id).sum(-1) - 1).to(logits.device)
|
1281 |
+
else:
|
1282 |
+
sequence_lengths = -1
|
1283 |
+
logger.warning(
|
1284 |
+
f"{self.__class__.__name__} will not detect padding tokens in `inputs_embeds`. Results may be "
|
1285 |
+
"unexpected if using padding tokens in conjunction with `inputs_embeds.`"
|
1286 |
+
)
|
1287 |
+
|
1288 |
+
pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
|
1289 |
+
|
1290 |
+
loss = None
|
1291 |
+
if labels is not None:
|
1292 |
+
if self.config.problem_type is None:
|
1293 |
+
if self.num_labels == 1:
|
1294 |
+
self.config.problem_type = "regression"
|
1295 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
1296 |
+
self.config.problem_type = "single_label_classification"
|
1297 |
+
else:
|
1298 |
+
self.config.problem_type = "multi_label_classification"
|
1299 |
+
|
1300 |
+
if self.config.problem_type == "regression":
|
1301 |
+
loss_fct = MSELoss()
|
1302 |
+
if self.num_labels == 1:
|
1303 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
1304 |
+
else:
|
1305 |
+
loss = loss_fct(pooled_logits, labels)
|
1306 |
+
elif self.config.problem_type == "single_label_classification":
|
1307 |
+
loss_fct = CrossEntropyLoss()
|
1308 |
+
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
1309 |
+
elif self.config.problem_type == "multi_label_classification":
|
1310 |
+
loss_fct = BCEWithLogitsLoss()
|
1311 |
+
loss = loss_fct(pooled_logits, labels)
|
1312 |
+
if not return_dict:
|
1313 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
1314 |
+
return ((loss,) + output) if loss is not None else output
|
1315 |
+
|
1316 |
+
return SequenceClassifierOutputWithPast(
|
1317 |
+
loss=loss,
|
1318 |
+
logits=pooled_logits,
|
1319 |
+
past_key_values=transformer_outputs.past_key_values,
|
1320 |
+
hidden_states=transformer_outputs.hidden_states,
|
1321 |
+
attentions=transformer_outputs.attentions,
|
1322 |
+
)
|
1323 |
+
|
1324 |
+
|
1325 |
+
@add_start_docstrings(
|
1326 |
+
"""
|
1327 |
+
BTLM Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for
|
1328 |
+
Named-Entity-Recognition (NER) tasks.
|
1329 |
+
""",
|
1330 |
+
BTLM_START_DOCSTRING,
|
1331 |
+
)
|
1332 |
+
class BTLMForTokenClassification(BTLMPreTrainedModel):
|
1333 |
+
def __init__(self, config):
|
1334 |
+
super().__init__(config)
|
1335 |
+
self.num_labels = config.num_labels
|
1336 |
+
|
1337 |
+
self.transformer = BTLMModel(config)
|
1338 |
+
if hasattr(config, "classifier_dropout") and config.classifier_dropout is not None:
|
1339 |
+
classifier_dropout = config.classifier_dropout
|
1340 |
+
elif hasattr(config, "hidden_dropout") and config.hidden_dropout is not None:
|
1341 |
+
classifier_dropout = config.hidden_dropout
|
1342 |
+
else:
|
1343 |
+
classifier_dropout = 0.1
|
1344 |
+
self.dropout = nn.Dropout(classifier_dropout)
|
1345 |
+
self.classifier = nn.Linear(config.hidden_size, config.num_labels)
|
1346 |
+
self.output_logits_scale = config.width_scale
|
1347 |
+
|
1348 |
+
# Model parallel
|
1349 |
+
self.model_parallel = False
|
1350 |
+
self.device_map = None
|
1351 |
+
|
1352 |
+
# Initialize weights and apply final processing
|
1353 |
+
self.post_init()
|
1354 |
+
|
1355 |
+
@add_start_docstrings_to_model_forward(BTLM_INPUTS_DOCSTRING)
|
1356 |
+
# fmt: off
|
1357 |
+
@add_code_sample_docstrings(
|
1358 |
+
checkpoint="brad1141/gpt2-finetuned-comp2",
|
1359 |
+
output_type=TokenClassifierOutput,
|
1360 |
+
config_class=_CONFIG_FOR_DOC,
|
1361 |
+
expected_loss=0.25,
|
1362 |
+
expected_output=["Lead", "Lead", "Lead", "Position", "Lead", "Lead", "Lead", "Lead", "Lead", "Lead", "Lead", "Lead"],
|
1363 |
+
)
|
1364 |
+
# fmt: on
|
1365 |
+
def forward(
|
1366 |
+
self,
|
1367 |
+
input_ids: Optional[torch.LongTensor] = None,
|
1368 |
+
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
|
1369 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
1370 |
+
token_type_ids: Optional[torch.LongTensor] = None,
|
1371 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1372 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
1373 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1374 |
+
labels: Optional[torch.LongTensor] = None,
|
1375 |
+
use_cache: Optional[bool] = None,
|
1376 |
+
output_attentions: Optional[bool] = None,
|
1377 |
+
output_hidden_states: Optional[bool] = None,
|
1378 |
+
return_dict: Optional[bool] = None,
|
1379 |
+
) -> Union[Tuple, TokenClassifierOutput]:
|
1380 |
+
r"""
|
1381 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
1382 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
1383 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
1384 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
1385 |
+
"""
|
1386 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1387 |
+
|
1388 |
+
transformer_outputs = self.transformer(
|
1389 |
+
input_ids,
|
1390 |
+
past_key_values=past_key_values,
|
1391 |
+
attention_mask=attention_mask,
|
1392 |
+
token_type_ids=token_type_ids,
|
1393 |
+
position_ids=position_ids,
|
1394 |
+
head_mask=head_mask,
|
1395 |
+
inputs_embeds=inputs_embeds,
|
1396 |
+
use_cache=use_cache,
|
1397 |
+
output_attentions=output_attentions,
|
1398 |
+
output_hidden_states=output_hidden_states,
|
1399 |
+
return_dict=return_dict,
|
1400 |
+
)
|
1401 |
+
|
1402 |
+
hidden_states = transformer_outputs[0]
|
1403 |
+
hidden_states = self.dropout(hidden_states)
|
1404 |
+
logits = self.classifier(hidden_states)
|
1405 |
+
logits *= torch.tensor(
|
1406 |
+
float(self.output_logits_scale), dtype=logits.dtype, device=logits.device
|
1407 |
+
)
|
1408 |
+
|
1409 |
+
loss = None
|
1410 |
+
if labels is not None:
|
1411 |
+
labels = labels.to(logits.device)
|
1412 |
+
loss_fct = CrossEntropyLoss()
|
1413 |
+
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
|
1414 |
+
|
1415 |
+
if not return_dict:
|
1416 |
+
output = (logits,) + transformer_outputs[2:]
|
1417 |
+
return ((loss,) + output) if loss is not None else output
|
1418 |
+
|
1419 |
+
return TokenClassifierOutput(
|
1420 |
+
loss=loss,
|
1421 |
+
logits=logits,
|
1422 |
+
hidden_states=transformer_outputs.hidden_states,
|
1423 |
+
attentions=transformer_outputs.attentions,
|
1424 |
+
)
|
1425 |
+
|
1426 |
+
|
1427 |
+
@add_start_docstrings(
|
1428 |
+
"""
|
1429 |
+
The BTLM Model transformer with a span classification head on top for extractive question-answering tasks like
|
1430 |
+
SQuAD (a linear layer on top of the hidden-states output to compute `span start logits` and `span end logits`).
|
1431 |
+
""",
|
1432 |
+
BTLM_START_DOCSTRING,
|
1433 |
+
)
|
1434 |
+
class BTLMForQuestionAnswering(BTLMPreTrainedModel):
|
1435 |
+
_keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.bias", r"h\.\d+\.attn\.masked_bias"]
|
1436 |
+
_keys_to_ignore_on_load_missing = [r"h\.\d+\.attn\.masked_bias", r"h\.\d+\.attn\.bias", r"lm_head.weight"]
|
1437 |
+
|
1438 |
+
def __init__(self, config):
|
1439 |
+
super().__init__(config)
|
1440 |
+
self.num_labels = config.num_labels
|
1441 |
+
self.transformer = BTLMModel(config)
|
1442 |
+
self.qa_outputs = nn.Linear(config.hidden_size, 2)
|
1443 |
+
self.output_logits_scale = config.width_scale
|
1444 |
+
|
1445 |
+
# Model parallel
|
1446 |
+
self.model_parallel = False
|
1447 |
+
self.device_map = None
|
1448 |
+
self.gradient_checkpointing = False
|
1449 |
+
|
1450 |
+
# Initialize weights and apply final processing
|
1451 |
+
self.post_init()
|
1452 |
+
|
1453 |
+
def forward(
|
1454 |
+
self,
|
1455 |
+
input_ids: Optional[torch.LongTensor] = None,
|
1456 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
1457 |
+
token_type_ids: Optional[torch.LongTensor] = None,
|
1458 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1459 |
+
head_mask: Optional[torch.FloatTensor] = None,
|
1460 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1461 |
+
start_positions: Optional[torch.LongTensor] = None,
|
1462 |
+
end_positions: Optional[torch.LongTensor] = None,
|
1463 |
+
output_attentions: Optional[bool] = None,
|
1464 |
+
output_hidden_states: Optional[bool] = None,
|
1465 |
+
return_dict: Optional[bool] = None,
|
1466 |
+
) -> Union[Tuple, QuestionAnsweringModelOutput]:
|
1467 |
+
r"""
|
1468 |
+
start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
1469 |
+
Labels for position (index) of the start of the labelled span for computing the token classification loss.
|
1470 |
+
Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
|
1471 |
+
are not taken into account for computing the loss.
|
1472 |
+
end_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
1473 |
+
Labels for position (index) of the end of the labelled span for computing the token classification loss.
|
1474 |
+
Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
|
1475 |
+
are not taken into account for computing the loss.
|
1476 |
+
"""
|
1477 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1478 |
+
|
1479 |
+
outputs = self.transformer(
|
1480 |
+
input_ids,
|
1481 |
+
attention_mask=attention_mask,
|
1482 |
+
token_type_ids=token_type_ids,
|
1483 |
+
position_ids=position_ids,
|
1484 |
+
head_mask=head_mask,
|
1485 |
+
inputs_embeds=inputs_embeds,
|
1486 |
+
output_attentions=output_attentions,
|
1487 |
+
output_hidden_states=output_hidden_states,
|
1488 |
+
return_dict=return_dict,
|
1489 |
+
)
|
1490 |
+
|
1491 |
+
sequence_output = outputs[0]
|
1492 |
+
|
1493 |
+
logits = self.qa_outputs(sequence_output)
|
1494 |
+
logits *= torch.tensor(
|
1495 |
+
float(self.output_logits_scale), dtype=logits.dtype, device=logits.device
|
1496 |
+
)
|
1497 |
+
start_logits, end_logits = logits.split(1, dim=-1)
|
1498 |
+
start_logits = start_logits.squeeze(-1).contiguous()
|
1499 |
+
end_logits = end_logits.squeeze(-1).contiguous()
|
1500 |
+
|
1501 |
+
total_loss = None
|
1502 |
+
if start_positions is not None and end_positions is not None:
|
1503 |
+
# If we are on multi-GPU, split add a dimension
|
1504 |
+
if len(start_positions.size()) > 1:
|
1505 |
+
start_positions = start_positions.squeeze(-1).to(start_logits.device)
|
1506 |
+
if len(end_positions.size()) > 1:
|
1507 |
+
end_positions = end_positions.squeeze(-1).to(end_logits.device)
|
1508 |
+
# sometimes the start/end positions are outside our model inputs, we ignore these terms
|
1509 |
+
ignored_index = start_logits.size(1)
|
1510 |
+
start_positions = start_positions.clamp(0, ignored_index)
|
1511 |
+
end_positions = end_positions.clamp(0, ignored_index)
|
1512 |
+
|
1513 |
+
loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
|
1514 |
+
start_loss = loss_fct(start_logits, start_positions)
|
1515 |
+
end_loss = loss_fct(end_logits, end_positions)
|
1516 |
+
total_loss = (start_loss + end_loss) / 2
|
1517 |
+
|
1518 |
+
if not return_dict:
|
1519 |
+
output = (start_logits, end_logits) + outputs[2:]
|
1520 |
+
return ((total_loss,) + output) if total_loss is not None else output
|
1521 |
+
|
1522 |
+
return QuestionAnsweringModelOutput(
|
1523 |
+
loss=total_loss,
|
1524 |
+
start_logits=start_logits,
|
1525 |
+
end_logits=end_logits,
|
1526 |
+
hidden_states=outputs.hidden_states,
|
1527 |
+
attentions=outputs.attentions,
|
1528 |
+
)
|
params_train_to_hf.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"attn_pdrop": 0.0,
|
3 |
+
"scale_attn_weights": true,
|
4 |
+
"resid_pdrop": 0.0,
|
5 |
+
"embeddings_scale": 14.6,
|
6 |
+
"n_inner": 5461,
|
7 |
+
"n_embd": 2048,
|
8 |
+
"layer_norm_epsilon": 1e-05,
|
9 |
+
"n_positions": 2048,
|
10 |
+
"activation_function": "swiglu",
|
11 |
+
"n_head": 16,
|
12 |
+
"n_layer": 24,
|
13 |
+
"width_scale": 0.2775,
|
14 |
+
"position_embedding_type": "alibi",
|
15 |
+
"scale_qk_dot_by_d": true,
|
16 |
+
"tie_word_embeddings": true,
|
17 |
+
"vocab_size": 50277,
|
18 |
+
"embd_pdrop": 0.0
|
19 |
+
}
|
pytorch_model-00001-of-00001.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b7b5a35de43b7d7be36805ec8607e55c06e0534c15f044913132cf5685d6d886
|
3 |
+
size 5759005258
|
pytorch_model.bin.index.json
ADDED
@@ -0,0 +1,396 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"metadata": {
|
3 |
+
"total_size": 5758877792
|
4 |
+
},
|
5 |
+
"weight_map": {
|
6 |
+
"transformer.wte.weight": "pytorch_model-00001-of-00001.bin",
|
7 |
+
"transformer.relative_pe.slopes": "pytorch_model-00001-of-00001.bin",
|
8 |
+
"transformer.h.0.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
9 |
+
"transformer.h.0.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
10 |
+
"transformer.h.0.attn.bias": "pytorch_model-00001-of-00001.bin",
|
11 |
+
"transformer.h.0.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
12 |
+
"transformer.h.0.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
13 |
+
"transformer.h.0.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
14 |
+
"transformer.h.0.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
15 |
+
"transformer.h.0.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
16 |
+
"transformer.h.0.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
17 |
+
"transformer.h.0.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
18 |
+
"transformer.h.0.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
19 |
+
"transformer.h.0.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
20 |
+
"transformer.h.0.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
21 |
+
"transformer.h.0.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
22 |
+
"transformer.h.0.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
23 |
+
"transformer.h.0.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
24 |
+
"transformer.h.1.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
25 |
+
"transformer.h.1.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
26 |
+
"transformer.h.1.attn.bias": "pytorch_model-00001-of-00001.bin",
|
27 |
+
"transformer.h.1.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
28 |
+
"transformer.h.1.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
29 |
+
"transformer.h.1.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
30 |
+
"transformer.h.1.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
31 |
+
"transformer.h.1.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
32 |
+
"transformer.h.1.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
33 |
+
"transformer.h.1.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
34 |
+
"transformer.h.1.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
35 |
+
"transformer.h.1.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
36 |
+
"transformer.h.1.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
37 |
+
"transformer.h.1.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
38 |
+
"transformer.h.1.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
39 |
+
"transformer.h.1.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
40 |
+
"transformer.h.2.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
41 |
+
"transformer.h.2.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
42 |
+
"transformer.h.2.attn.bias": "pytorch_model-00001-of-00001.bin",
|
43 |
+
"transformer.h.2.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
44 |
+
"transformer.h.2.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
45 |
+
"transformer.h.2.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
46 |
+
"transformer.h.2.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
47 |
+
"transformer.h.2.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
48 |
+
"transformer.h.2.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
49 |
+
"transformer.h.2.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
50 |
+
"transformer.h.2.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
51 |
+
"transformer.h.2.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
52 |
+
"transformer.h.2.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
53 |
+
"transformer.h.2.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
54 |
+
"transformer.h.2.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
55 |
+
"transformer.h.2.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
56 |
+
"transformer.h.3.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
57 |
+
"transformer.h.3.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
58 |
+
"transformer.h.3.attn.bias": "pytorch_model-00001-of-00001.bin",
|
59 |
+
"transformer.h.3.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
60 |
+
"transformer.h.3.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
61 |
+
"transformer.h.3.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
62 |
+
"transformer.h.3.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
63 |
+
"transformer.h.3.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
64 |
+
"transformer.h.3.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
65 |
+
"transformer.h.3.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
66 |
+
"transformer.h.3.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
67 |
+
"transformer.h.3.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
68 |
+
"transformer.h.3.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
69 |
+
"transformer.h.3.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
70 |
+
"transformer.h.3.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
71 |
+
"transformer.h.3.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
72 |
+
"transformer.h.4.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
73 |
+
"transformer.h.4.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
74 |
+
"transformer.h.4.attn.bias": "pytorch_model-00001-of-00001.bin",
|
75 |
+
"transformer.h.4.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
76 |
+
"transformer.h.4.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
77 |
+
"transformer.h.4.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
78 |
+
"transformer.h.4.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
79 |
+
"transformer.h.4.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
80 |
+
"transformer.h.4.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
81 |
+
"transformer.h.4.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
82 |
+
"transformer.h.4.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
83 |
+
"transformer.h.4.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
84 |
+
"transformer.h.4.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
85 |
+
"transformer.h.4.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
86 |
+
"transformer.h.4.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
87 |
+
"transformer.h.4.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
88 |
+
"transformer.h.5.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
89 |
+
"transformer.h.5.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
90 |
+
"transformer.h.5.attn.bias": "pytorch_model-00001-of-00001.bin",
|
91 |
+
"transformer.h.5.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
92 |
+
"transformer.h.5.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
93 |
+
"transformer.h.5.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
94 |
+
"transformer.h.5.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
95 |
+
"transformer.h.5.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
96 |
+
"transformer.h.5.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
97 |
+
"transformer.h.5.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
98 |
+
"transformer.h.5.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
99 |
+
"transformer.h.5.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
100 |
+
"transformer.h.5.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
101 |
+
"transformer.h.5.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
102 |
+
"transformer.h.5.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
103 |
+
"transformer.h.5.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
104 |
+
"transformer.h.6.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
105 |
+
"transformer.h.6.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
106 |
+
"transformer.h.6.attn.bias": "pytorch_model-00001-of-00001.bin",
|
107 |
+
"transformer.h.6.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
108 |
+
"transformer.h.6.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
109 |
+
"transformer.h.6.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
110 |
+
"transformer.h.6.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
111 |
+
"transformer.h.6.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
112 |
+
"transformer.h.6.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
113 |
+
"transformer.h.6.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
114 |
+
"transformer.h.6.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
115 |
+
"transformer.h.6.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
116 |
+
"transformer.h.6.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
117 |
+
"transformer.h.6.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
118 |
+
"transformer.h.6.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
119 |
+
"transformer.h.6.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
120 |
+
"transformer.h.7.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
121 |
+
"transformer.h.7.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
122 |
+
"transformer.h.7.attn.bias": "pytorch_model-00001-of-00001.bin",
|
123 |
+
"transformer.h.7.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
124 |
+
"transformer.h.7.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
125 |
+
"transformer.h.7.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
126 |
+
"transformer.h.7.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
127 |
+
"transformer.h.7.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
128 |
+
"transformer.h.7.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
129 |
+
"transformer.h.7.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
130 |
+
"transformer.h.7.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
131 |
+
"transformer.h.7.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
132 |
+
"transformer.h.7.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
133 |
+
"transformer.h.7.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
134 |
+
"transformer.h.7.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
135 |
+
"transformer.h.7.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
136 |
+
"transformer.h.8.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
137 |
+
"transformer.h.8.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
138 |
+
"transformer.h.8.attn.bias": "pytorch_model-00001-of-00001.bin",
|
139 |
+
"transformer.h.8.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
140 |
+
"transformer.h.8.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
141 |
+
"transformer.h.8.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
142 |
+
"transformer.h.8.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
143 |
+
"transformer.h.8.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
144 |
+
"transformer.h.8.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
145 |
+
"transformer.h.8.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
146 |
+
"transformer.h.8.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
147 |
+
"transformer.h.8.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
148 |
+
"transformer.h.8.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
149 |
+
"transformer.h.8.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
150 |
+
"transformer.h.8.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
151 |
+
"transformer.h.8.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
152 |
+
"transformer.h.9.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
153 |
+
"transformer.h.9.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
154 |
+
"transformer.h.9.attn.bias": "pytorch_model-00001-of-00001.bin",
|
155 |
+
"transformer.h.9.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
156 |
+
"transformer.h.9.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
157 |
+
"transformer.h.9.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
158 |
+
"transformer.h.9.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
159 |
+
"transformer.h.9.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
160 |
+
"transformer.h.9.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
161 |
+
"transformer.h.9.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
162 |
+
"transformer.h.9.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
163 |
+
"transformer.h.9.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
164 |
+
"transformer.h.9.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
165 |
+
"transformer.h.9.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
166 |
+
"transformer.h.9.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
167 |
+
"transformer.h.9.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
168 |
+
"transformer.h.10.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
169 |
+
"transformer.h.10.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
170 |
+
"transformer.h.10.attn.bias": "pytorch_model-00001-of-00001.bin",
|
171 |
+
"transformer.h.10.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
172 |
+
"transformer.h.10.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
173 |
+
"transformer.h.10.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
174 |
+
"transformer.h.10.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
175 |
+
"transformer.h.10.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
176 |
+
"transformer.h.10.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
177 |
+
"transformer.h.10.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
178 |
+
"transformer.h.10.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
179 |
+
"transformer.h.10.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
180 |
+
"transformer.h.10.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
181 |
+
"transformer.h.10.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
182 |
+
"transformer.h.10.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
183 |
+
"transformer.h.10.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
184 |
+
"transformer.h.11.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
185 |
+
"transformer.h.11.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
186 |
+
"transformer.h.11.attn.bias": "pytorch_model-00001-of-00001.bin",
|
187 |
+
"transformer.h.11.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
188 |
+
"transformer.h.11.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
189 |
+
"transformer.h.11.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
190 |
+
"transformer.h.11.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
191 |
+
"transformer.h.11.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
192 |
+
"transformer.h.11.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
193 |
+
"transformer.h.11.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
194 |
+
"transformer.h.11.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
195 |
+
"transformer.h.11.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
196 |
+
"transformer.h.11.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
197 |
+
"transformer.h.11.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
198 |
+
"transformer.h.11.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
199 |
+
"transformer.h.11.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
200 |
+
"transformer.h.12.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
201 |
+
"transformer.h.12.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
202 |
+
"transformer.h.12.attn.bias": "pytorch_model-00001-of-00001.bin",
|
203 |
+
"transformer.h.12.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
204 |
+
"transformer.h.12.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
205 |
+
"transformer.h.12.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
206 |
+
"transformer.h.12.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
207 |
+
"transformer.h.12.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
208 |
+
"transformer.h.12.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
209 |
+
"transformer.h.12.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
210 |
+
"transformer.h.12.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
211 |
+
"transformer.h.12.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
212 |
+
"transformer.h.12.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
213 |
+
"transformer.h.12.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
214 |
+
"transformer.h.12.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
215 |
+
"transformer.h.12.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
216 |
+
"transformer.h.13.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
217 |
+
"transformer.h.13.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
218 |
+
"transformer.h.13.attn.bias": "pytorch_model-00001-of-00001.bin",
|
219 |
+
"transformer.h.13.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
220 |
+
"transformer.h.13.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
221 |
+
"transformer.h.13.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
222 |
+
"transformer.h.13.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
223 |
+
"transformer.h.13.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
224 |
+
"transformer.h.13.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
225 |
+
"transformer.h.13.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
226 |
+
"transformer.h.13.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
227 |
+
"transformer.h.13.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
228 |
+
"transformer.h.13.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
229 |
+
"transformer.h.13.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
230 |
+
"transformer.h.13.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
231 |
+
"transformer.h.13.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
232 |
+
"transformer.h.14.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
233 |
+
"transformer.h.14.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
234 |
+
"transformer.h.14.attn.bias": "pytorch_model-00001-of-00001.bin",
|
235 |
+
"transformer.h.14.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
236 |
+
"transformer.h.14.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
237 |
+
"transformer.h.14.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
238 |
+
"transformer.h.14.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
239 |
+
"transformer.h.14.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
240 |
+
"transformer.h.14.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
241 |
+
"transformer.h.14.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
242 |
+
"transformer.h.14.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
243 |
+
"transformer.h.14.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
244 |
+
"transformer.h.14.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
245 |
+
"transformer.h.14.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
246 |
+
"transformer.h.14.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
247 |
+
"transformer.h.14.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
248 |
+
"transformer.h.15.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
249 |
+
"transformer.h.15.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
250 |
+
"transformer.h.15.attn.bias": "pytorch_model-00001-of-00001.bin",
|
251 |
+
"transformer.h.15.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
252 |
+
"transformer.h.15.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
253 |
+
"transformer.h.15.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
254 |
+
"transformer.h.15.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
255 |
+
"transformer.h.15.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
256 |
+
"transformer.h.15.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
257 |
+
"transformer.h.15.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
258 |
+
"transformer.h.15.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
259 |
+
"transformer.h.15.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
260 |
+
"transformer.h.15.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
261 |
+
"transformer.h.15.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
262 |
+
"transformer.h.15.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
263 |
+
"transformer.h.15.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
264 |
+
"transformer.h.16.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
265 |
+
"transformer.h.16.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
266 |
+
"transformer.h.16.attn.bias": "pytorch_model-00001-of-00001.bin",
|
267 |
+
"transformer.h.16.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
268 |
+
"transformer.h.16.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
269 |
+
"transformer.h.16.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
270 |
+
"transformer.h.16.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
271 |
+
"transformer.h.16.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
272 |
+
"transformer.h.16.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
273 |
+
"transformer.h.16.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
274 |
+
"transformer.h.16.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
275 |
+
"transformer.h.16.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
276 |
+
"transformer.h.16.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
277 |
+
"transformer.h.16.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
278 |
+
"transformer.h.16.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
279 |
+
"transformer.h.16.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
280 |
+
"transformer.h.17.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
281 |
+
"transformer.h.17.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
282 |
+
"transformer.h.17.attn.bias": "pytorch_model-00001-of-00001.bin",
|
283 |
+
"transformer.h.17.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
284 |
+
"transformer.h.17.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
285 |
+
"transformer.h.17.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
286 |
+
"transformer.h.17.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
287 |
+
"transformer.h.17.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
288 |
+
"transformer.h.17.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
289 |
+
"transformer.h.17.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
290 |
+
"transformer.h.17.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
291 |
+
"transformer.h.17.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
292 |
+
"transformer.h.17.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
293 |
+
"transformer.h.17.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
294 |
+
"transformer.h.17.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
295 |
+
"transformer.h.17.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
296 |
+
"transformer.h.18.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
297 |
+
"transformer.h.18.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
298 |
+
"transformer.h.18.attn.bias": "pytorch_model-00001-of-00001.bin",
|
299 |
+
"transformer.h.18.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
300 |
+
"transformer.h.18.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
301 |
+
"transformer.h.18.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
302 |
+
"transformer.h.18.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
303 |
+
"transformer.h.18.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
304 |
+
"transformer.h.18.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
305 |
+
"transformer.h.18.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
306 |
+
"transformer.h.18.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
307 |
+
"transformer.h.18.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
308 |
+
"transformer.h.18.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
309 |
+
"transformer.h.18.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
310 |
+
"transformer.h.18.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
311 |
+
"transformer.h.18.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
312 |
+
"transformer.h.19.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
313 |
+
"transformer.h.19.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
314 |
+
"transformer.h.19.attn.bias": "pytorch_model-00001-of-00001.bin",
|
315 |
+
"transformer.h.19.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
316 |
+
"transformer.h.19.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
317 |
+
"transformer.h.19.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
318 |
+
"transformer.h.19.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
319 |
+
"transformer.h.19.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
320 |
+
"transformer.h.19.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
321 |
+
"transformer.h.19.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
322 |
+
"transformer.h.19.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
323 |
+
"transformer.h.19.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
324 |
+
"transformer.h.19.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
325 |
+
"transformer.h.19.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
326 |
+
"transformer.h.19.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
327 |
+
"transformer.h.19.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
328 |
+
"transformer.h.20.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
329 |
+
"transformer.h.20.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
330 |
+
"transformer.h.20.attn.bias": "pytorch_model-00001-of-00001.bin",
|
331 |
+
"transformer.h.20.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
332 |
+
"transformer.h.20.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
333 |
+
"transformer.h.20.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
334 |
+
"transformer.h.20.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
335 |
+
"transformer.h.20.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
336 |
+
"transformer.h.20.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
337 |
+
"transformer.h.20.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
338 |
+
"transformer.h.20.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
339 |
+
"transformer.h.20.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
340 |
+
"transformer.h.20.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
341 |
+
"transformer.h.20.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
342 |
+
"transformer.h.20.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
343 |
+
"transformer.h.20.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
344 |
+
"transformer.h.21.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
345 |
+
"transformer.h.21.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
346 |
+
"transformer.h.21.attn.bias": "pytorch_model-00001-of-00001.bin",
|
347 |
+
"transformer.h.21.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
348 |
+
"transformer.h.21.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
349 |
+
"transformer.h.21.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
350 |
+
"transformer.h.21.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
351 |
+
"transformer.h.21.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
352 |
+
"transformer.h.21.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
353 |
+
"transformer.h.21.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
354 |
+
"transformer.h.21.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
355 |
+
"transformer.h.21.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
356 |
+
"transformer.h.21.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
357 |
+
"transformer.h.21.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
358 |
+
"transformer.h.21.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
359 |
+
"transformer.h.21.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
360 |
+
"transformer.h.22.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
361 |
+
"transformer.h.22.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
362 |
+
"transformer.h.22.attn.bias": "pytorch_model-00001-of-00001.bin",
|
363 |
+
"transformer.h.22.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
364 |
+
"transformer.h.22.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
365 |
+
"transformer.h.22.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
366 |
+
"transformer.h.22.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
367 |
+
"transformer.h.22.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
368 |
+
"transformer.h.22.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
369 |
+
"transformer.h.22.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
370 |
+
"transformer.h.22.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
371 |
+
"transformer.h.22.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
372 |
+
"transformer.h.22.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
373 |
+
"transformer.h.22.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
374 |
+
"transformer.h.22.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
375 |
+
"transformer.h.22.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
376 |
+
"transformer.h.23.attn.c_attn.weight": "pytorch_model-00001-of-00001.bin",
|
377 |
+
"transformer.h.23.attn.c_attn.bias": "pytorch_model-00001-of-00001.bin",
|
378 |
+
"transformer.h.23.attn.bias": "pytorch_model-00001-of-00001.bin",
|
379 |
+
"transformer.h.23.attn.masked_bias": "pytorch_model-00001-of-00001.bin",
|
380 |
+
"transformer.h.23.attn.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
381 |
+
"transformer.h.23.attn.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
382 |
+
"transformer.h.23.ln_1.weight": "pytorch_model-00001-of-00001.bin",
|
383 |
+
"transformer.h.23.ln_1.bias": "pytorch_model-00001-of-00001.bin",
|
384 |
+
"transformer.h.23.ln_2.weight": "pytorch_model-00001-of-00001.bin",
|
385 |
+
"transformer.h.23.ln_2.bias": "pytorch_model-00001-of-00001.bin",
|
386 |
+
"transformer.h.23.mlp.c_fc.weight": "pytorch_model-00001-of-00001.bin",
|
387 |
+
"transformer.h.23.mlp.c_fc.bias": "pytorch_model-00001-of-00001.bin",
|
388 |
+
"transformer.h.23.mlp.c_fc2.weight": "pytorch_model-00001-of-00001.bin",
|
389 |
+
"transformer.h.23.mlp.c_fc2.bias": "pytorch_model-00001-of-00001.bin",
|
390 |
+
"transformer.h.23.mlp.c_proj.weight": "pytorch_model-00001-of-00001.bin",
|
391 |
+
"transformer.h.23.mlp.c_proj.bias": "pytorch_model-00001-of-00001.bin",
|
392 |
+
"transformer.ln_f.weight": "pytorch_model-00001-of-00001.bin",
|
393 |
+
"transformer.ln_f.bias": "pytorch_model-00001-of-00001.bin",
|
394 |
+
"lm_head.weight": "pytorch_model-00001-of-00001.bin"
|
395 |
+
}
|
396 |
+
}
|
register_btlm.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoConfig, AutoModel, AutoModelForCausalLM, AutoTokenizer
|
2 |
+
from transformers.tokenization_utils_fast import PreTrainedTokenizerFast
|
3 |
+
from configuration_btlm import BTLMConfig
|
4 |
+
from modeling_btlm import BTLMModel, BTLMLMHeadModel
|
5 |
+
|
6 |
+
AutoConfig.register("btlm", BTLMConfig)
|
7 |
+
AutoModel.register(BTLMConfig, BTLMModel)
|
8 |
+
AutoModelForCausalLM.register(BTLMConfig, BTLMLMHeadModel)
|
9 |
+
AutoTokenizer.register(BTLMConfig, fast_tokenizer_class=PreTrainedTokenizerFast)
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|