omkarthawakar
commited on
Commit
•
7962ed1
1
Parent(s):
c8d9ddc
initial upload
Browse filesUploaded Model Files
- added_tokens.json +5 -0
- config.json +31 -0
- configuration_mobillama.py +182 -0
- generation_config.json +6 -0
- modeling_mobillama.py +868 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +11 -0
- tokenizer.model +3 -0
- tokenizer_config.json +48 -0
- trainer_state.json +34 -0
- training_args.bin +3 -0
added_tokens.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"</s>": 2,
|
3 |
+
"<s>": 1,
|
4 |
+
"<unk>": 0
|
5 |
+
}
|
config.json
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "MobiLlama",
|
3 |
+
"architectures": [
|
4 |
+
"MobiLlamaForCausalLM"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoModelForCausalLM": "modeling_mobillama.MobiLlamaForCausalLM"
|
8 |
+
},
|
9 |
+
"attention_bias": false,
|
10 |
+
"attention_dropout": 0.0,
|
11 |
+
"bos_token_id": 1,
|
12 |
+
"eos_token_id": 2,
|
13 |
+
"hidden_act": "silu",
|
14 |
+
"hidden_size": 2048,
|
15 |
+
"initializer_range": 0.02,
|
16 |
+
"intermediate_size": 5632,
|
17 |
+
"max_position_embeddings": 2048,
|
18 |
+
"model_type": "llama",
|
19 |
+
"num_attention_heads": 32,
|
20 |
+
"num_hidden_layers": 22,
|
21 |
+
"num_key_value_heads": 4,
|
22 |
+
"pretraining_tp": 1,
|
23 |
+
"rms_norm_eps": 1e-05,
|
24 |
+
"rope_scaling": null,
|
25 |
+
"rope_theta": 10000.0,
|
26 |
+
"tie_word_embeddings": false,
|
27 |
+
"torch_dtype": "float32",
|
28 |
+
"transformers_version": "4.36.1",
|
29 |
+
"use_cache": true,
|
30 |
+
"vocab_size": 32000
|
31 |
+
}
|
configuration_mobillama.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
""" LLaMA model configuration"""
|
21 |
+
|
22 |
+
from ...configuration_utils import PretrainedConfig
|
23 |
+
from ...utils import logging
|
24 |
+
|
25 |
+
|
26 |
+
logger = logging.get_logger(__name__)
|
27 |
+
|
28 |
+
LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
|
29 |
+
|
30 |
+
|
31 |
+
class MobiLlamaConfig(PretrainedConfig):
|
32 |
+
r"""
|
33 |
+
This is the configuration class to store the configuration of a [`LlamaModel`]. It is used to instantiate an LLaMA
|
34 |
+
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
|
35 |
+
defaults will yield a similar configuration to that of the LLaMA-7B.
|
36 |
+
|
37 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
38 |
+
documentation from [`PretrainedConfig`] for more information.
|
39 |
+
|
40 |
+
|
41 |
+
Args:
|
42 |
+
vocab_size (`int`, *optional*, defaults to 32000):
|
43 |
+
Vocabulary size of the LLaMA model. Defines the number of different tokens that can be represented by the
|
44 |
+
`inputs_ids` passed when calling [`LlamaModel`]
|
45 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
46 |
+
Dimension of the hidden representations.
|
47 |
+
intermediate_size (`int`, *optional*, defaults to 11008):
|
48 |
+
Dimension of the MLP representations.
|
49 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
50 |
+
Number of hidden layers in the Transformer decoder.
|
51 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
52 |
+
Number of attention heads for each attention layer in the Transformer decoder.
|
53 |
+
num_key_value_heads (`int`, *optional*):
|
54 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
55 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
56 |
+
`num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
57 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
58 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
59 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
|
60 |
+
`num_attention_heads`.
|
61 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
62 |
+
The non-linear activation function (function or string) in the decoder.
|
63 |
+
max_position_embeddings (`int`, *optional*, defaults to 2048):
|
64 |
+
The maximum sequence length that this model might ever be used with. Llama 1 supports up to 2048 tokens,
|
65 |
+
Llama 2 up to 4096, CodeLlama up to 16384.
|
66 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
67 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
68 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
69 |
+
The epsilon used by the rms normalization layers.
|
70 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
71 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
72 |
+
relevant if `config.is_decoder=True`.
|
73 |
+
pad_token_id (`int`, *optional*):
|
74 |
+
Padding token id.
|
75 |
+
bos_token_id (`int`, *optional*, defaults to 1):
|
76 |
+
Beginning of stream token id.
|
77 |
+
eos_token_id (`int`, *optional*, defaults to 2):
|
78 |
+
End of stream token id.
|
79 |
+
pretraining_tp (`int`, *optional*, defaults to 1):
|
80 |
+
Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
|
81 |
+
document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is
|
82 |
+
necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
|
83 |
+
issue](https://github.com/pytorch/pytorch/issues/76232).
|
84 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
85 |
+
Whether to tie weight embeddings
|
86 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
87 |
+
The base period of the RoPE embeddings.
|
88 |
+
rope_scaling (`Dict`, *optional*):
|
89 |
+
Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
|
90 |
+
strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
|
91 |
+
`{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
|
92 |
+
`max_position_embeddings` to the expected new maximum. See the following thread for more information on how
|
93 |
+
these scaling strategies behave:
|
94 |
+
https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
|
95 |
+
experimental feature, subject to breaking API changes in future versions.
|
96 |
+
attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
|
97 |
+
Whether to use a bias in the query, key, value and output projection layers during self-attention.
|
98 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
99 |
+
The dropout ratio for the attention probabilities.
|
100 |
+
|
101 |
+
```python
|
102 |
+
|
103 |
+
```"""
|
104 |
+
|
105 |
+
model_type = "mobillama"
|
106 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
107 |
+
|
108 |
+
def __init__(
|
109 |
+
self,
|
110 |
+
vocab_size=32000,
|
111 |
+
hidden_size=4096,
|
112 |
+
intermediate_size=11008,
|
113 |
+
num_hidden_layers=32,
|
114 |
+
num_attention_heads=32,
|
115 |
+
num_key_value_heads=None,
|
116 |
+
hidden_act="silu",
|
117 |
+
max_position_embeddings=2048,
|
118 |
+
initializer_range=0.02,
|
119 |
+
rms_norm_eps=1e-6,
|
120 |
+
use_cache=True,
|
121 |
+
pad_token_id=None,
|
122 |
+
bos_token_id=1,
|
123 |
+
eos_token_id=2,
|
124 |
+
pretraining_tp=1,
|
125 |
+
tie_word_embeddings=False,
|
126 |
+
rope_theta=10000.0,
|
127 |
+
rope_scaling=None,
|
128 |
+
attention_bias=False,
|
129 |
+
attention_dropout=0.0,
|
130 |
+
**kwargs,
|
131 |
+
):
|
132 |
+
self.vocab_size = vocab_size
|
133 |
+
self.max_position_embeddings = max_position_embeddings
|
134 |
+
self.hidden_size = hidden_size
|
135 |
+
self.intermediate_size = intermediate_size
|
136 |
+
self.num_hidden_layers = num_hidden_layers
|
137 |
+
self.num_attention_heads = num_attention_heads
|
138 |
+
|
139 |
+
# for backward compatibility
|
140 |
+
if num_key_value_heads is None:
|
141 |
+
num_key_value_heads = num_attention_heads
|
142 |
+
|
143 |
+
self.num_key_value_heads = num_key_value_heads
|
144 |
+
self.hidden_act = hidden_act
|
145 |
+
self.initializer_range = initializer_range
|
146 |
+
self.rms_norm_eps = rms_norm_eps
|
147 |
+
self.pretraining_tp = pretraining_tp
|
148 |
+
self.use_cache = use_cache
|
149 |
+
self.rope_theta = rope_theta
|
150 |
+
self.rope_scaling = rope_scaling
|
151 |
+
self._rope_scaling_validation()
|
152 |
+
self.attention_bias = attention_bias
|
153 |
+
self.attention_dropout = attention_dropout
|
154 |
+
|
155 |
+
super().__init__(
|
156 |
+
pad_token_id=pad_token_id,
|
157 |
+
bos_token_id=bos_token_id,
|
158 |
+
eos_token_id=eos_token_id,
|
159 |
+
tie_word_embeddings=tie_word_embeddings,
|
160 |
+
**kwargs,
|
161 |
+
)
|
162 |
+
|
163 |
+
def _rope_scaling_validation(self):
|
164 |
+
"""
|
165 |
+
Validate the `rope_scaling` configuration.
|
166 |
+
"""
|
167 |
+
if self.rope_scaling is None:
|
168 |
+
return
|
169 |
+
|
170 |
+
if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
|
171 |
+
raise ValueError(
|
172 |
+
"`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, "
|
173 |
+
f"got {self.rope_scaling}"
|
174 |
+
)
|
175 |
+
rope_scaling_type = self.rope_scaling.get("type", None)
|
176 |
+
rope_scaling_factor = self.rope_scaling.get("factor", None)
|
177 |
+
if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
|
178 |
+
raise ValueError(
|
179 |
+
f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
|
180 |
+
)
|
181 |
+
if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
|
182 |
+
raise ValueError(f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}")
|
generation_config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 1,
|
4 |
+
"eos_token_id": 2,
|
5 |
+
"transformers_version": "4.34.0"
|
6 |
+
}
|
modeling_mobillama.py
ADDED
@@ -0,0 +1,868 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
""" PyTorch LLaMA model."""
|
21 |
+
import math
|
22 |
+
from typing import List, Optional, Tuple, Union
|
23 |
+
|
24 |
+
import torch
|
25 |
+
import torch.utils.checkpoint
|
26 |
+
from torch import nn
|
27 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
28 |
+
|
29 |
+
from transformers.activations import ACT2FN
|
30 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
|
31 |
+
from transformers.modeling_utils import PreTrainedModel
|
32 |
+
from transformers.utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
|
33 |
+
from transformers.models.llama.configuration_llama import LlamaConfig
|
34 |
+
|
35 |
+
# from .configuration_mobillama import MobiLlamaConfig
|
36 |
+
|
37 |
+
from flash_attn import flash_attn_func
|
38 |
+
|
39 |
+
|
40 |
+
logger = logging.get_logger(__name__)
|
41 |
+
|
42 |
+
_CONFIG_FOR_DOC = "LlamaConfig"
|
43 |
+
|
44 |
+
|
45 |
+
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
|
46 |
+
def _make_causal_mask(
|
47 |
+
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
48 |
+
):
|
49 |
+
"""
|
50 |
+
Make causal mask used for bi-directional self-attention.
|
51 |
+
"""
|
52 |
+
bsz, tgt_len = input_ids_shape
|
53 |
+
mask = torch.full((tgt_len, tgt_len), torch.tensor(torch.finfo(dtype).min, device=device), device=device)
|
54 |
+
mask_cond = torch.arange(mask.size(-1), device=device)
|
55 |
+
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
|
56 |
+
mask = mask.to(dtype)
|
57 |
+
|
58 |
+
if past_key_values_length > 0:
|
59 |
+
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
|
60 |
+
return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
|
61 |
+
|
62 |
+
|
63 |
+
# Copied from transformers.models.bart.modeling_bart._expand_mask
|
64 |
+
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
|
65 |
+
"""
|
66 |
+
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
|
67 |
+
"""
|
68 |
+
bsz, src_len = mask.size()
|
69 |
+
tgt_len = tgt_len if tgt_len is not None else src_len
|
70 |
+
|
71 |
+
expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
|
72 |
+
|
73 |
+
inverted_mask = 1.0 - expanded_mask
|
74 |
+
|
75 |
+
return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
|
76 |
+
|
77 |
+
|
78 |
+
class MobiLlamaRMSNorm(nn.Module):
|
79 |
+
def __init__(self, hidden_size, eps=1e-6):
|
80 |
+
"""
|
81 |
+
MobiLlamaRMSNorm is equivalent to T5LayerNorm
|
82 |
+
"""
|
83 |
+
super().__init__()
|
84 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
85 |
+
self.variance_epsilon = eps
|
86 |
+
|
87 |
+
def forward(self, hidden_states):
|
88 |
+
input_dtype = hidden_states.dtype
|
89 |
+
variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
|
90 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
91 |
+
|
92 |
+
return (self.weight * hidden_states).to(input_dtype)
|
93 |
+
|
94 |
+
|
95 |
+
class MobiLlamaRotaryEmbedding(torch.nn.Module):
|
96 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
97 |
+
super().__init__()
|
98 |
+
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
|
99 |
+
self.register_buffer("inv_freq", inv_freq)
|
100 |
+
|
101 |
+
# Build here to make `torch.jit.trace` work.
|
102 |
+
self.max_seq_len_cached = max_position_embeddings
|
103 |
+
t = torch.arange(self.max_seq_len_cached, device=self.inv_freq.device, dtype=self.inv_freq.dtype)
|
104 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
105 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
106 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
107 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :], persistent=False)
|
108 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)
|
109 |
+
|
110 |
+
def forward(self, x, seq_len=None):
|
111 |
+
# x: [bs, num_attention_heads, seq_len, head_size]
|
112 |
+
# This `if` block is unlikely to be run after we build sin/cos in `__init__`. Keep the logic here just in case.
|
113 |
+
if seq_len > self.max_seq_len_cached:
|
114 |
+
self.max_seq_len_cached = seq_len
|
115 |
+
t = torch.arange(self.max_seq_len_cached, device=x.device, dtype=self.inv_freq.dtype)
|
116 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
117 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
118 |
+
emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
|
119 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :], persistent=False)
|
120 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)
|
121 |
+
return (
|
122 |
+
self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
123 |
+
self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
124 |
+
)
|
125 |
+
|
126 |
+
|
127 |
+
def rotate_half(x):
|
128 |
+
"""Rotates half the hidden dims of the input."""
|
129 |
+
x1 = x[..., : x.shape[-1] // 2]
|
130 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
131 |
+
return torch.cat((-x2, x1), dim=-1)
|
132 |
+
|
133 |
+
|
134 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
|
135 |
+
# The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
|
136 |
+
cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
|
137 |
+
sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
|
138 |
+
cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
139 |
+
sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
140 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
141 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
142 |
+
return q_embed, k_embed
|
143 |
+
|
144 |
+
|
145 |
+
class MobiLlamaMLP(nn.Module):
|
146 |
+
def __init__(
|
147 |
+
self,
|
148 |
+
hidden_size: int,
|
149 |
+
intermediate_size: int,
|
150 |
+
hidden_act: str,
|
151 |
+
):
|
152 |
+
super().__init__()
|
153 |
+
self.gate_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
|
154 |
+
self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False)
|
155 |
+
self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
|
156 |
+
self.act_fn = ACT2FN[hidden_act]
|
157 |
+
|
158 |
+
def forward(self, x):
|
159 |
+
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
160 |
+
|
161 |
+
|
162 |
+
class MobiLlamaAttention(nn.Module):
|
163 |
+
"""Multi-headed attention from 'Attention Is All You Need' paper"""
|
164 |
+
|
165 |
+
def __init__(self, config: LlamaConfig):
|
166 |
+
super().__init__()
|
167 |
+
self.config = config
|
168 |
+
self.hidden_size = config.hidden_size
|
169 |
+
self.num_heads = config.num_attention_heads
|
170 |
+
self.head_dim = self.hidden_size // self.num_heads
|
171 |
+
self.max_position_embeddings = config.max_position_embeddings
|
172 |
+
|
173 |
+
if (self.head_dim * self.num_heads) != self.hidden_size:
|
174 |
+
raise ValueError(
|
175 |
+
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
176 |
+
f" and `num_heads`: {self.num_heads})."
|
177 |
+
)
|
178 |
+
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
179 |
+
self.k_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
180 |
+
self.v_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
181 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
182 |
+
self.rotary_emb = MobiLlamaRotaryEmbedding(self.head_dim, max_position_embeddings=self.max_position_embeddings)
|
183 |
+
|
184 |
+
def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
|
185 |
+
return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
|
186 |
+
|
187 |
+
def forward(
|
188 |
+
self,
|
189 |
+
hidden_states: torch.Tensor,
|
190 |
+
attention_mask: Optional[torch.Tensor] = None,
|
191 |
+
position_ids: Optional[torch.LongTensor] = None,
|
192 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
193 |
+
output_attentions: bool = False,
|
194 |
+
use_cache: bool = False,
|
195 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
196 |
+
bsz, q_len, _ = hidden_states.size()
|
197 |
+
|
198 |
+
query_states = self.q_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
199 |
+
key_states = self.k_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
200 |
+
value_states = self.v_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
201 |
+
|
202 |
+
kv_seq_len = key_states.shape[-2]
|
203 |
+
if past_key_value is not None:
|
204 |
+
kv_seq_len += past_key_value[0].shape[-2]
|
205 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
206 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
207 |
+
# [bsz, nh, t, hd]
|
208 |
+
|
209 |
+
if past_key_value is not None:
|
210 |
+
# reuse k, v, self_attention
|
211 |
+
key_states = torch.cat([past_key_value[0], key_states], dim=2)
|
212 |
+
value_states = torch.cat([past_key_value[1], value_states], dim=2)
|
213 |
+
|
214 |
+
past_key_value = (key_states, value_states) if use_cache else None
|
215 |
+
|
216 |
+
attn_output = flash_attn_func(
|
217 |
+
q=query_states.transpose(1, 2).to(torch.bfloat16),
|
218 |
+
k=key_states.transpose(1, 2).to(torch.bfloat16),
|
219 |
+
v=value_states.transpose(1, 2).to(torch.bfloat16),
|
220 |
+
causal=True)
|
221 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
222 |
+
attn_output = attn_output.to(query_states.dtype)
|
223 |
+
|
224 |
+
attn_output = self.o_proj(attn_output)
|
225 |
+
|
226 |
+
# if not output_attentions:
|
227 |
+
# attn_weights = None
|
228 |
+
assert not output_attentions
|
229 |
+
attn_weights = None
|
230 |
+
|
231 |
+
return attn_output, attn_weights, past_key_value
|
232 |
+
|
233 |
+
|
234 |
+
class MobiLlamaDecoderLayer(nn.Module):
|
235 |
+
def __init__(self, config: LlamaConfig):
|
236 |
+
super().__init__()
|
237 |
+
self.hidden_size = config.hidden_size
|
238 |
+
self.self_attn = MobiLlamaAttention(config=config)
|
239 |
+
self.mlp = MobiLlamaMLP(
|
240 |
+
hidden_size=self.hidden_size,
|
241 |
+
intermediate_size=config.intermediate_size,
|
242 |
+
hidden_act=config.hidden_act,
|
243 |
+
)
|
244 |
+
self.input_layernorm = MobiLlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
245 |
+
self.post_attention_layernorm = MobiLlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
246 |
+
|
247 |
+
def forward(
|
248 |
+
self,
|
249 |
+
hidden_states: torch.Tensor,
|
250 |
+
attention_mask: Optional[torch.Tensor] = None,
|
251 |
+
position_ids: Optional[torch.LongTensor] = None,
|
252 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
253 |
+
output_attentions: Optional[bool] = False,
|
254 |
+
use_cache: Optional[bool] = False,
|
255 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
256 |
+
"""
|
257 |
+
Args:
|
258 |
+
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
259 |
+
attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
|
260 |
+
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
|
261 |
+
output_attentions (`bool`, *optional*):
|
262 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
263 |
+
returned tensors for more detail.
|
264 |
+
use_cache (`bool`, *optional*):
|
265 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
266 |
+
(see `past_key_values`).
|
267 |
+
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
|
268 |
+
"""
|
269 |
+
|
270 |
+
residual = hidden_states
|
271 |
+
|
272 |
+
hidden_states = self.input_layernorm(hidden_states)
|
273 |
+
|
274 |
+
# Self Attention
|
275 |
+
hidden_states, self_attn_weights, present_key_value = self.self_attn(
|
276 |
+
hidden_states=hidden_states,
|
277 |
+
attention_mask=attention_mask,
|
278 |
+
position_ids=position_ids,
|
279 |
+
past_key_value=past_key_value,
|
280 |
+
output_attentions=output_attentions,
|
281 |
+
use_cache=use_cache,
|
282 |
+
)
|
283 |
+
hidden_states = residual + hidden_states
|
284 |
+
|
285 |
+
# Fully Connected
|
286 |
+
residual = hidden_states
|
287 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
288 |
+
hidden_states = self.mlp(hidden_states)
|
289 |
+
hidden_states = residual + hidden_states
|
290 |
+
|
291 |
+
outputs = (hidden_states,)
|
292 |
+
|
293 |
+
if output_attentions:
|
294 |
+
outputs += (self_attn_weights,)
|
295 |
+
|
296 |
+
if use_cache:
|
297 |
+
outputs += (present_key_value,)
|
298 |
+
|
299 |
+
return outputs
|
300 |
+
|
301 |
+
|
302 |
+
MOBILLAMA_START_DOCSTRING = r"""
|
303 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
304 |
+
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
305 |
+
etc.)
|
306 |
+
|
307 |
+
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
308 |
+
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
309 |
+
and behavior.
|
310 |
+
|
311 |
+
Parameters:
|
312 |
+
config ([`LlamaConfig`]):
|
313 |
+
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
314 |
+
load the weights associated with the model, only the configuration. Check out the
|
315 |
+
[`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
316 |
+
"""
|
317 |
+
|
318 |
+
|
319 |
+
@add_start_docstrings(
|
320 |
+
"The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
|
321 |
+
MOBILLAMA_START_DOCSTRING,
|
322 |
+
)
|
323 |
+
class MobiLlamaPreTrainedModel(PreTrainedModel):
|
324 |
+
config_class = LlamaConfig
|
325 |
+
base_model_prefix = "model"
|
326 |
+
supports_gradient_checkpointing = True
|
327 |
+
_no_split_modules = ["MobiLlamaDecoderLayer"]
|
328 |
+
_skip_keys_device_placement = "past_key_values"
|
329 |
+
_keys_to_ignore_on_load_unexpected = [r"decoder\.version"]
|
330 |
+
|
331 |
+
def _init_weights(self, module):
|
332 |
+
std = self.config.initializer_range
|
333 |
+
if isinstance(module, nn.Linear):
|
334 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
335 |
+
if module.bias is not None:
|
336 |
+
module.bias.data.zero_()
|
337 |
+
elif isinstance(module, nn.Embedding):
|
338 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
339 |
+
if module.padding_idx is not None:
|
340 |
+
module.weight.data[module.padding_idx].zero_()
|
341 |
+
|
342 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
343 |
+
if isinstance(module, MobiLlamaModel):
|
344 |
+
module.gradient_checkpointing = value
|
345 |
+
|
346 |
+
|
347 |
+
MOBILLAMA_INPUTS_DOCSTRING = r"""
|
348 |
+
Args:
|
349 |
+
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
350 |
+
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
351 |
+
it.
|
352 |
+
|
353 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
354 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
355 |
+
|
356 |
+
[What are input IDs?](../glossary#input-ids)
|
357 |
+
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
358 |
+
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
359 |
+
|
360 |
+
- 1 for tokens that are **not masked**,
|
361 |
+
- 0 for tokens that are **masked**.
|
362 |
+
|
363 |
+
[What are attention masks?](../glossary#attention-mask)
|
364 |
+
|
365 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
366 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
367 |
+
|
368 |
+
If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
|
369 |
+
`past_key_values`).
|
370 |
+
|
371 |
+
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
372 |
+
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
373 |
+
information on the default strategy.
|
374 |
+
|
375 |
+
- 1 indicates the head is **not masked**,
|
376 |
+
- 0 indicates the head is **masked**.
|
377 |
+
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
378 |
+
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
379 |
+
config.n_positions - 1]`.
|
380 |
+
|
381 |
+
[What are position IDs?](../glossary#position-ids)
|
382 |
+
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
|
383 |
+
Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
|
384 |
+
`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
|
385 |
+
`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
|
386 |
+
|
387 |
+
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
388 |
+
blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
|
389 |
+
|
390 |
+
If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
|
391 |
+
don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
|
392 |
+
`decoder_input_ids` of shape `(batch_size, sequence_length)`.
|
393 |
+
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
394 |
+
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
|
395 |
+
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
|
396 |
+
model's internal embedding lookup matrix.
|
397 |
+
use_cache (`bool`, *optional*):
|
398 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
|
399 |
+
`past_key_values`).
|
400 |
+
output_attentions (`bool`, *optional*):
|
401 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
|
402 |
+
tensors for more detail.
|
403 |
+
output_hidden_states (`bool`, *optional*):
|
404 |
+
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
|
405 |
+
more detail.
|
406 |
+
return_dict (`bool`, *optional*):
|
407 |
+
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
|
408 |
+
"""
|
409 |
+
|
410 |
+
|
411 |
+
@add_start_docstrings(
|
412 |
+
"The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
|
413 |
+
MOBILLAMA_START_DOCSTRING,
|
414 |
+
)
|
415 |
+
class MobiLlamaModel(MobiLlamaPreTrainedModel):
|
416 |
+
"""
|
417 |
+
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MobiLlamaDecoderLayer`]
|
418 |
+
|
419 |
+
Args:
|
420 |
+
config: LlamaConfig
|
421 |
+
"""
|
422 |
+
|
423 |
+
def __init__(self, config: LlamaConfig):
|
424 |
+
super().__init__(config)
|
425 |
+
self.padding_idx = config.pad_token_id
|
426 |
+
self.vocab_size = config.vocab_size
|
427 |
+
|
428 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
429 |
+
self.layers = nn.ModuleList([MobiLlamaDecoderLayer(config) for _ in range(config.num_hidden_layers)])
|
430 |
+
self.norm = MobiLlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
431 |
+
|
432 |
+
self.gradient_checkpointing = False
|
433 |
+
# Initialize weights and apply final processing
|
434 |
+
self.post_init()
|
435 |
+
|
436 |
+
def get_input_embeddings(self):
|
437 |
+
return self.embed_tokens
|
438 |
+
|
439 |
+
def set_input_embeddings(self, value):
|
440 |
+
self.embed_tokens = value
|
441 |
+
|
442 |
+
# Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
|
443 |
+
def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
|
444 |
+
# create causal mask
|
445 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
446 |
+
combined_attention_mask = None
|
447 |
+
if input_shape[-1] > 1:
|
448 |
+
combined_attention_mask = _make_causal_mask(
|
449 |
+
input_shape,
|
450 |
+
inputs_embeds.dtype,
|
451 |
+
device=inputs_embeds.device,
|
452 |
+
past_key_values_length=past_key_values_length,
|
453 |
+
)
|
454 |
+
|
455 |
+
if attention_mask is not None:
|
456 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
457 |
+
expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
|
458 |
+
inputs_embeds.device
|
459 |
+
)
|
460 |
+
combined_attention_mask = (
|
461 |
+
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
|
462 |
+
)
|
463 |
+
|
464 |
+
return combined_attention_mask
|
465 |
+
|
466 |
+
@add_start_docstrings_to_model_forward(MOBILLAMA_INPUTS_DOCSTRING)
|
467 |
+
def forward(
|
468 |
+
self,
|
469 |
+
input_ids: torch.LongTensor = None,
|
470 |
+
attention_mask: Optional[torch.Tensor] = None,
|
471 |
+
position_ids: Optional[torch.LongTensor] = None,
|
472 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
473 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
474 |
+
use_cache: Optional[bool] = None,
|
475 |
+
output_attentions: Optional[bool] = None,
|
476 |
+
output_hidden_states: Optional[bool] = None,
|
477 |
+
return_dict: Optional[bool] = None,
|
478 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
479 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
480 |
+
output_hidden_states = (
|
481 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
482 |
+
)
|
483 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
484 |
+
|
485 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
486 |
+
|
487 |
+
# retrieve input_ids and inputs_embeds
|
488 |
+
if input_ids is not None and inputs_embeds is not None:
|
489 |
+
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
|
490 |
+
elif input_ids is not None:
|
491 |
+
batch_size, seq_length = input_ids.shape
|
492 |
+
elif inputs_embeds is not None:
|
493 |
+
batch_size, seq_length, _ = inputs_embeds.shape
|
494 |
+
else:
|
495 |
+
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
|
496 |
+
|
497 |
+
seq_length_with_past = seq_length
|
498 |
+
past_key_values_length = 0
|
499 |
+
|
500 |
+
if past_key_values is not None:
|
501 |
+
past_key_values_length = past_key_values[0][0].shape[2]
|
502 |
+
seq_length_with_past = seq_length_with_past + past_key_values_length
|
503 |
+
|
504 |
+
if position_ids is None:
|
505 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
506 |
+
position_ids = torch.arange(
|
507 |
+
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
|
508 |
+
)
|
509 |
+
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
|
510 |
+
else:
|
511 |
+
position_ids = position_ids.view(-1, seq_length).long()
|
512 |
+
|
513 |
+
if inputs_embeds is None:
|
514 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
515 |
+
# embed positions
|
516 |
+
if attention_mask is None:
|
517 |
+
attention_mask = torch.ones(
|
518 |
+
(batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
|
519 |
+
)
|
520 |
+
attention_mask = self._prepare_decoder_attention_mask(
|
521 |
+
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
|
522 |
+
)
|
523 |
+
|
524 |
+
hidden_states = inputs_embeds
|
525 |
+
|
526 |
+
if self.gradient_checkpointing and self.training:
|
527 |
+
if use_cache:
|
528 |
+
logger.warning_once(
|
529 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
530 |
+
)
|
531 |
+
use_cache = False
|
532 |
+
|
533 |
+
# decoder layers
|
534 |
+
all_hidden_states = () if output_hidden_states else None
|
535 |
+
all_self_attns = () if output_attentions else None
|
536 |
+
next_decoder_cache = () if use_cache else None
|
537 |
+
|
538 |
+
for idx, decoder_layer in enumerate(self.layers):
|
539 |
+
if output_hidden_states:
|
540 |
+
all_hidden_states += (hidden_states,)
|
541 |
+
|
542 |
+
past_key_value = past_key_values[idx] if past_key_values is not None else None
|
543 |
+
|
544 |
+
if self.gradient_checkpointing and self.training:
|
545 |
+
|
546 |
+
def create_custom_forward(module):
|
547 |
+
def custom_forward(*inputs):
|
548 |
+
# None for past_key_value
|
549 |
+
return module(*inputs, output_attentions, None)
|
550 |
+
|
551 |
+
return custom_forward
|
552 |
+
|
553 |
+
layer_outputs = torch.utils.checkpoint.checkpoint(
|
554 |
+
create_custom_forward(decoder_layer),
|
555 |
+
hidden_states,
|
556 |
+
attention_mask,
|
557 |
+
position_ids,
|
558 |
+
None,
|
559 |
+
)
|
560 |
+
else:
|
561 |
+
layer_outputs = decoder_layer(
|
562 |
+
hidden_states,
|
563 |
+
attention_mask=attention_mask,
|
564 |
+
position_ids=position_ids,
|
565 |
+
past_key_value=past_key_value,
|
566 |
+
output_attentions=output_attentions,
|
567 |
+
use_cache=use_cache,
|
568 |
+
)
|
569 |
+
|
570 |
+
hidden_states = layer_outputs[0]
|
571 |
+
|
572 |
+
if use_cache:
|
573 |
+
next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
|
574 |
+
|
575 |
+
if output_attentions:
|
576 |
+
all_self_attns += (layer_outputs[1],)
|
577 |
+
|
578 |
+
hidden_states = self.norm(hidden_states)
|
579 |
+
|
580 |
+
# add hidden states from the last decoder layer
|
581 |
+
if output_hidden_states:
|
582 |
+
all_hidden_states += (hidden_states,)
|
583 |
+
|
584 |
+
next_cache = next_decoder_cache if use_cache else None
|
585 |
+
if not return_dict:
|
586 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
|
587 |
+
return BaseModelOutputWithPast(
|
588 |
+
last_hidden_state=hidden_states,
|
589 |
+
past_key_values=next_cache,
|
590 |
+
hidden_states=all_hidden_states,
|
591 |
+
attentions=all_self_attns,
|
592 |
+
)
|
593 |
+
|
594 |
+
|
595 |
+
class MobiLlamaForCausalLM(MobiLlamaPreTrainedModel):
|
596 |
+
def __init__(self, config):
|
597 |
+
super().__init__(config)
|
598 |
+
self.model = MobiLlamaModel(config)
|
599 |
+
|
600 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
601 |
+
|
602 |
+
# Initialize weights and apply final processing
|
603 |
+
self.post_init()
|
604 |
+
|
605 |
+
def get_input_embeddings(self):
|
606 |
+
return self.model.embed_tokens
|
607 |
+
|
608 |
+
def set_input_embeddings(self, value):
|
609 |
+
self.model.embed_tokens = value
|
610 |
+
|
611 |
+
def get_output_embeddings(self):
|
612 |
+
return self.lm_head
|
613 |
+
|
614 |
+
def set_output_embeddings(self, new_embeddings):
|
615 |
+
self.lm_head = new_embeddings
|
616 |
+
|
617 |
+
def set_decoder(self, decoder):
|
618 |
+
self.model = decoder
|
619 |
+
|
620 |
+
def get_decoder(self):
|
621 |
+
return self.model
|
622 |
+
|
623 |
+
@add_start_docstrings_to_model_forward(MOBILLAMA_INPUTS_DOCSTRING)
|
624 |
+
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
|
625 |
+
def forward(
|
626 |
+
self,
|
627 |
+
input_ids: torch.LongTensor = None,
|
628 |
+
attention_mask: Optional[torch.Tensor] = None,
|
629 |
+
position_ids: Optional[torch.LongTensor] = None,
|
630 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
631 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
632 |
+
labels: Optional[torch.LongTensor] = None,
|
633 |
+
use_cache: Optional[bool] = None,
|
634 |
+
output_attentions: Optional[bool] = None,
|
635 |
+
output_hidden_states: Optional[bool] = None,
|
636 |
+
return_dict: Optional[bool] = None,
|
637 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
638 |
+
r"""
|
639 |
+
Args:
|
640 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
641 |
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
642 |
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
643 |
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
644 |
+
|
645 |
+
Returns:
|
646 |
+
|
647 |
+
Example:
|
648 |
+
|
649 |
+
```python
|
650 |
+
>>> from transformers import AutoTokenizer, MobiLlamaForCausalLM
|
651 |
+
|
652 |
+
>>> model = MobiLlamaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
653 |
+
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
654 |
+
|
655 |
+
>>> prompt = "Hey, are you consciours? Can you talk to me?"
|
656 |
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
657 |
+
|
658 |
+
>>> # Generate
|
659 |
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
660 |
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
661 |
+
"Hey, are you consciours? Can you talk to me?\nI'm not consciours, but I can talk to you."
|
662 |
+
```"""
|
663 |
+
|
664 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
665 |
+
output_hidden_states = (
|
666 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
667 |
+
)
|
668 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
669 |
+
|
670 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
671 |
+
outputs = self.model(
|
672 |
+
input_ids=input_ids,
|
673 |
+
attention_mask=attention_mask,
|
674 |
+
position_ids=position_ids,
|
675 |
+
past_key_values=past_key_values,
|
676 |
+
inputs_embeds=inputs_embeds,
|
677 |
+
use_cache=use_cache,
|
678 |
+
output_attentions=output_attentions,
|
679 |
+
output_hidden_states=output_hidden_states,
|
680 |
+
return_dict=return_dict,
|
681 |
+
)
|
682 |
+
|
683 |
+
hidden_states = outputs[0]
|
684 |
+
logits = self.lm_head(hidden_states)
|
685 |
+
|
686 |
+
loss = None
|
687 |
+
if labels is not None:
|
688 |
+
# Shift so that tokens < n predict n
|
689 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
690 |
+
shift_labels = labels[..., 1:].contiguous()
|
691 |
+
# Flatten the tokens
|
692 |
+
loss_fct = CrossEntropyLoss()
|
693 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
694 |
+
shift_labels = shift_labels.view(-1)
|
695 |
+
# Enable model parallelism
|
696 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
697 |
+
loss = loss_fct(shift_logits, shift_labels)
|
698 |
+
|
699 |
+
if not return_dict:
|
700 |
+
output = (logits,) + outputs[1:]
|
701 |
+
return (loss,) + output if loss is not None else output
|
702 |
+
|
703 |
+
return CausalLMOutputWithPast(
|
704 |
+
loss=loss,
|
705 |
+
logits=logits,
|
706 |
+
past_key_values=outputs.past_key_values,
|
707 |
+
hidden_states=outputs.hidden_states,
|
708 |
+
attentions=outputs.attentions,
|
709 |
+
)
|
710 |
+
|
711 |
+
def prepare_inputs_for_generation(
|
712 |
+
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
|
713 |
+
):
|
714 |
+
if past_key_values:
|
715 |
+
input_ids = input_ids[:, -1:]
|
716 |
+
|
717 |
+
position_ids = kwargs.get("position_ids", None)
|
718 |
+
if attention_mask is not None and position_ids is None:
|
719 |
+
# create position_ids on the fly for batch generation
|
720 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
721 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
722 |
+
if past_key_values:
|
723 |
+
position_ids = position_ids[:, -1].unsqueeze(-1)
|
724 |
+
|
725 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
726 |
+
if inputs_embeds is not None and past_key_values is None:
|
727 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
728 |
+
else:
|
729 |
+
model_inputs = {"input_ids": input_ids}
|
730 |
+
|
731 |
+
model_inputs.update(
|
732 |
+
{
|
733 |
+
"position_ids": position_ids,
|
734 |
+
"past_key_values": past_key_values,
|
735 |
+
"use_cache": kwargs.get("use_cache"),
|
736 |
+
"attention_mask": attention_mask,
|
737 |
+
}
|
738 |
+
)
|
739 |
+
return model_inputs
|
740 |
+
|
741 |
+
@staticmethod
|
742 |
+
def _reorder_cache(past_key_values, beam_idx):
|
743 |
+
reordered_past = ()
|
744 |
+
for layer_past in past_key_values:
|
745 |
+
reordered_past += (tuple(past_state.index_select(0, beam_idx) for past_state in layer_past),)
|
746 |
+
return reordered_past
|
747 |
+
|
748 |
+
|
749 |
+
@add_start_docstrings(
|
750 |
+
"""
|
751 |
+
The LLaMa Model transformer with a sequence classification head on top (linear layer).
|
752 |
+
|
753 |
+
[`MobiLlamaForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
754 |
+
(e.g. GPT-2) do.
|
755 |
+
|
756 |
+
Since it does classification on the last token, it requires to know the position of the last token. If a
|
757 |
+
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
758 |
+
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
759 |
+
padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
|
760 |
+
each row of the batch).
|
761 |
+
""",
|
762 |
+
MOBILLAMA_START_DOCSTRING,
|
763 |
+
)
|
764 |
+
class MobiLlamaForSequenceClassification(MobiLlamaPreTrainedModel):
|
765 |
+
_keys_to_ignore_on_load_missing = [r"lm_head.weight"]
|
766 |
+
|
767 |
+
def __init__(self, config):
|
768 |
+
super().__init__(config)
|
769 |
+
self.num_labels = config.num_labels
|
770 |
+
self.model = MobiLlamaModel(config)
|
771 |
+
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
772 |
+
|
773 |
+
# Initialize weights and apply final processing
|
774 |
+
self.post_init()
|
775 |
+
|
776 |
+
def get_input_embeddings(self):
|
777 |
+
return self.model.embed_tokens
|
778 |
+
|
779 |
+
def set_input_embeddings(self, value):
|
780 |
+
self.model.embed_tokens = value
|
781 |
+
|
782 |
+
@add_start_docstrings_to_model_forward(MOBILLAMA_INPUTS_DOCSTRING)
|
783 |
+
def forward(
|
784 |
+
self,
|
785 |
+
input_ids: torch.LongTensor = None,
|
786 |
+
attention_mask: Optional[torch.Tensor] = None,
|
787 |
+
position_ids: Optional[torch.LongTensor] = None,
|
788 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
789 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
790 |
+
labels: Optional[torch.LongTensor] = 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, SequenceClassifierOutputWithPast]:
|
796 |
+
r"""
|
797 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
798 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
799 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
800 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
801 |
+
"""
|
802 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
803 |
+
|
804 |
+
transformer_outputs = self.model(
|
805 |
+
input_ids,
|
806 |
+
attention_mask=attention_mask,
|
807 |
+
position_ids=position_ids,
|
808 |
+
past_key_values=past_key_values,
|
809 |
+
inputs_embeds=inputs_embeds,
|
810 |
+
use_cache=use_cache,
|
811 |
+
output_attentions=output_attentions,
|
812 |
+
output_hidden_states=output_hidden_states,
|
813 |
+
return_dict=return_dict,
|
814 |
+
)
|
815 |
+
hidden_states = transformer_outputs[0]
|
816 |
+
logits = self.score(hidden_states)
|
817 |
+
|
818 |
+
if input_ids is not None:
|
819 |
+
batch_size = input_ids.shape[0]
|
820 |
+
else:
|
821 |
+
batch_size = inputs_embeds.shape[0]
|
822 |
+
|
823 |
+
if self.config.pad_token_id is None and batch_size != 1:
|
824 |
+
raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
|
825 |
+
if self.config.pad_token_id is None:
|
826 |
+
sequence_lengths = -1
|
827 |
+
else:
|
828 |
+
if input_ids is not None:
|
829 |
+
sequence_lengths = (torch.ne(input_ids, self.config.pad_token_id).sum(-1) - 1).to(logits.device)
|
830 |
+
else:
|
831 |
+
sequence_lengths = -1
|
832 |
+
|
833 |
+
pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
|
834 |
+
|
835 |
+
loss = None
|
836 |
+
if labels is not None:
|
837 |
+
labels = labels.to(logits.device)
|
838 |
+
if self.config.problem_type is None:
|
839 |
+
if self.num_labels == 1:
|
840 |
+
self.config.problem_type = "regression"
|
841 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
842 |
+
self.config.problem_type = "single_label_classification"
|
843 |
+
else:
|
844 |
+
self.config.problem_type = "multi_label_classification"
|
845 |
+
|
846 |
+
if self.config.problem_type == "regression":
|
847 |
+
loss_fct = MSELoss()
|
848 |
+
if self.num_labels == 1:
|
849 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
850 |
+
else:
|
851 |
+
loss = loss_fct(pooled_logits, labels)
|
852 |
+
elif self.config.problem_type == "single_label_classification":
|
853 |
+
loss_fct = CrossEntropyLoss()
|
854 |
+
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
855 |
+
elif self.config.problem_type == "multi_label_classification":
|
856 |
+
loss_fct = BCEWithLogitsLoss()
|
857 |
+
loss = loss_fct(pooled_logits, labels)
|
858 |
+
if not return_dict:
|
859 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
860 |
+
return ((loss,) + output) if loss is not None else output
|
861 |
+
|
862 |
+
return SequenceClassifierOutputWithPast(
|
863 |
+
loss=loss,
|
864 |
+
logits=pooled_logits,
|
865 |
+
past_key_values=transformer_outputs.past_key_values,
|
866 |
+
hidden_states=transformer_outputs.hidden_states,
|
867 |
+
attentions=transformer_outputs.attentions,
|
868 |
+
)
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2a1571eed0302ec9b9fcbe2898b8bb8b4a1aff660125fb6d99594edca9ecf079
|
3 |
+
size 2523131782
|
special_tokens_map.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"additional_special_tokens": [
|
3 |
+
"<unk>",
|
4 |
+
"<s>",
|
5 |
+
"</s>"
|
6 |
+
],
|
7 |
+
"bos_token": "<s>",
|
8 |
+
"eos_token": "</s>",
|
9 |
+
"pad_token": "<unk>",
|
10 |
+
"unk_token": "<unk>"
|
11 |
+
}
|
tokenizer.model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347
|
3 |
+
size 499723
|
tokenizer_config.json
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_bos_token": true,
|
3 |
+
"add_eos_token": false,
|
4 |
+
"added_tokens_decoder": {
|
5 |
+
"0": {
|
6 |
+
"content": "<unk>",
|
7 |
+
"lstrip": false,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false,
|
11 |
+
"special": true
|
12 |
+
},
|
13 |
+
"1": {
|
14 |
+
"content": "<s>",
|
15 |
+
"lstrip": false,
|
16 |
+
"normalized": false,
|
17 |
+
"rstrip": false,
|
18 |
+
"single_word": false,
|
19 |
+
"special": true
|
20 |
+
},
|
21 |
+
"2": {
|
22 |
+
"content": "</s>",
|
23 |
+
"lstrip": false,
|
24 |
+
"normalized": false,
|
25 |
+
"rstrip": false,
|
26 |
+
"single_word": false,
|
27 |
+
"special": true
|
28 |
+
}
|
29 |
+
},
|
30 |
+
"additional_special_tokens": [
|
31 |
+
"<unk>",
|
32 |
+
"<s>",
|
33 |
+
"</s>"
|
34 |
+
],
|
35 |
+
"bos_token": "<s>",
|
36 |
+
"clean_up_tokenization_spaces": false,
|
37 |
+
"eos_token": "</s>",
|
38 |
+
"legacy": false,
|
39 |
+
"model_max_length": 2048,
|
40 |
+
"pad_token": "<unk>",
|
41 |
+
"padding_side": "right",
|
42 |
+
"sp_model_kwargs": {},
|
43 |
+
"spaces_between_special_tokens": false,
|
44 |
+
"tokenizer_class": "LlamaTokenizer",
|
45 |
+
"tokenizer_file": "/mnt/beegfs/fahad.khan/FastChat/weights/tinyllama_1.2b/tokenizer.json",
|
46 |
+
"unk_token": "<unk>",
|
47 |
+
"use_default_system_prompt": false
|
48 |
+
}
|
trainer_state.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"best_metric": null,
|
3 |
+
"best_model_checkpoint": null,
|
4 |
+
"epoch": 2.862254025044723,
|
5 |
+
"eval_steps": 500,
|
6 |
+
"global_step": 100,
|
7 |
+
"is_hyper_param_search": false,
|
8 |
+
"is_local_process_zero": true,
|
9 |
+
"is_world_process_zero": true,
|
10 |
+
"log_history": [
|
11 |
+
{
|
12 |
+
"epoch": 2.86,
|
13 |
+
"learning_rate": 2e-05,
|
14 |
+
"loss": 0.7948,
|
15 |
+
"step": 100
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"epoch": 2.86,
|
19 |
+
"step": 100,
|
20 |
+
"total_flos": 463566557675520.0,
|
21 |
+
"train_loss": 0.0,
|
22 |
+
"train_runtime": 8.6291,
|
23 |
+
"train_samples_per_second": 30362.446,
|
24 |
+
"train_steps_per_second": 7.301
|
25 |
+
}
|
26 |
+
],
|
27 |
+
"logging_steps": 100,
|
28 |
+
"max_steps": 63,
|
29 |
+
"num_train_epochs": 3,
|
30 |
+
"save_steps": 50,
|
31 |
+
"total_flos": 463566557675520.0,
|
32 |
+
"trial_name": null,
|
33 |
+
"trial_params": null
|
34 |
+
}
|
training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6d0bad28a5e5e91cecbaa7d852802e8635111200fdedbc94e3d43ab55fc55a2e
|
3 |
+
size 6840
|