KnutJaegersberg commited on
Commit
4cb29e1
1 Parent(s): 9432a56

Upload 11 files

Browse files
config.json ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/run/media/knut/HD/huggingface models/language models/llama-alternatives/LLongMA-3b/",
3
+ "architectures": [
4
+ "LlamaForCausalLM"
5
+ ],
6
+ "auto_map": {
7
+ "AutoConfig": "configuration_llama.LlamaConfig",
8
+ "AutoModel": "modeling_llama.LlamaModel",
9
+ "AutoModelForCausalLM": "modeling_llama.LlamaForCausalLM",
10
+ "AutoModelForSequenceClassification": "modeling_llama.LlamaForSequenceClassification"
11
+ },
12
+ "bos_token_id": 1,
13
+ "eos_token_id": 2,
14
+ "hidden_act": "silu",
15
+ "hidden_size": 3200,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 8640,
18
+ "max_position_embeddings": 8192,
19
+ "model_type": "llama",
20
+ "num_attention_heads": 32,
21
+ "num_hidden_layers": 26,
22
+ "num_key_value_heads": 32,
23
+ "pad_token_id": 0,
24
+ "pretraining_tp": 1,
25
+ "rms_norm_eps": 1e-06,
26
+ "rope_scaling": {
27
+ "factor": 4.0,
28
+ "type": "linear"
29
+ },
30
+ "tie_word_embeddings": false,
31
+ "torch_dtype": "float32",
32
+ "transformers_version": "4.32.0.dev0",
33
+ "use_cache": true,
34
+ "vocab_size": 32000
35
+ }
configuration_llama.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 transformers.configuration_utils import PretrainedConfig
23
+ from transformers.utils import logging
24
+
25
+
26
+ logger = logging.get_logger(__name__)
27
+
28
+ LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP = {}
29
+
30
+
31
+ class LlamaConfig(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 encoder.
51
+ num_attention_heads (`int`, *optional*, defaults to 32):
52
+ Number of attention heads for each attention layer in the Transformer encoder.
53
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
54
+ The non-linear activation function (function or string) in the decoder.
55
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
56
+ The maximum sequence length that this model might ever be used with. Typically set this to something large
57
+ just in case (e.g., 512 or 1024 or 2048).
58
+ initializer_range (`float`, *optional*, defaults to 0.02):
59
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
60
+ rms_norm_eps (`float`, *optional*, defaults to 1e-12):
61
+ The epsilon used by the rms normalization layers.
62
+ use_cache (`bool`, *optional*, defaults to `True`):
63
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
64
+ relevant if `config.is_decoder=True`.
65
+ tie_word_embeddings(`bool`, *optional*, defaults to `False`):
66
+ Whether to tie weight embeddings
67
+ rope_scaling (`Dict`, *optional*):
68
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports three scaling
69
+ strategies: linear and dynamic. Their scaling factor must be an float greater than 1. The expected format
70
+ is `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
71
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
72
+ these scaling strategies behave:
73
+ https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
74
+ experimental feature, subject to breaking API changes in future versions.
75
+
76
+ Example:
77
+
78
+ ```python
79
+ >>> from transformers import LlamaModel, LlamaConfig
80
+
81
+ >>> # Initializing a LLaMA llama-7b style configuration
82
+ >>> configuration = LlamaConfig()
83
+
84
+ >>> # Initializing a model from the llama-7b style configuration
85
+ >>> model = LlamaModel(configuration)
86
+
87
+ >>> # Accessing the model configuration
88
+ >>> configuration = model.config
89
+ ```"""
90
+ model_type = "llama"
91
+ keys_to_ignore_at_inference = ["past_key_values"]
92
+
93
+ def __init__(
94
+ self,
95
+ vocab_size=32000,
96
+ hidden_size=4096,
97
+ intermediate_size=11008,
98
+ num_hidden_layers=32,
99
+ num_attention_heads=32,
100
+ hidden_act="silu",
101
+ max_position_embeddings=2048,
102
+ initializer_range=0.02,
103
+ rms_norm_eps=1e-6,
104
+ use_cache=True,
105
+ pad_token_id=0,
106
+ bos_token_id=1,
107
+ eos_token_id=2,
108
+ tie_word_embeddings=False,
109
+ rope_scaling=None,
110
+ **kwargs,
111
+ ):
112
+ self.vocab_size = vocab_size
113
+ self.max_position_embeddings = max_position_embeddings
114
+ self.hidden_size = hidden_size
115
+ self.intermediate_size = intermediate_size
116
+ self.num_hidden_layers = num_hidden_layers
117
+ self.num_attention_heads = num_attention_heads
118
+ self.hidden_act = hidden_act
119
+ self.initializer_range = initializer_range
120
+ self.rms_norm_eps = rms_norm_eps
121
+ self.use_cache = use_cache
122
+ self.rope_scaling = rope_scaling
123
+ self._rope_scaling_validation()
124
+
125
+ super().__init__(
126
+ pad_token_id=pad_token_id,
127
+ bos_token_id=bos_token_id,
128
+ eos_token_id=eos_token_id,
129
+ tie_word_embeddings=tie_word_embeddings,
130
+ **kwargs,
131
+ )
132
+
133
+ def _rope_scaling_validation(self):
134
+ """
135
+ Validate the `rope_scaling` configuration.
136
+ """
137
+ if self.rope_scaling is None:
138
+ return
139
+
140
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
141
+ raise ValueError(
142
+ "`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, "
143
+ f"got {self.rope_scaling}"
144
+ )
145
+ rope_scaling_type = self.rope_scaling.get("type", None)
146
+ rope_scaling_factor = self.rope_scaling.get("factor", None)
147
+ if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic", "ntk"]:
148
+ raise ValueError(
149
+ f"`rope_scaling`'s name field must be one of ['linear', 'dynamic', 'ntk'], got {rope_scaling_type}"
150
+ )
151
+ if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
152
+ raise ValueError(f"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}")
generation_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "pad_token_id": 0,
6
+ "transformers_version": "4.32.0.dev0"
7
+ }
model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:19f3e54fbea157dbb1e79e776d021cb4924964baef0d333c08f67c0feb316bae
3
+ size 9990650632
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8655c8eb275fb7010665c7defc78af488c1324d10ed075e067fcf8e9c27547cb
3
+ size 3715271008
model.safetensors.index.json ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 13705894400
4
+ },
5
+ "weight_map": {
6
+ "lm_head.weight": "model-00002-of-00002.safetensors",
7
+ "model.embed_tokens.weight": "model-00001-of-00002.safetensors",
8
+ "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors",
9
+ "model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
10
+ "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
11
+ "model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
12
+ "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
13
+ "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
14
+ "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
15
+ "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
16
+ "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
17
+ "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors",
18
+ "model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
19
+ "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
20
+ "model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
21
+ "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
22
+ "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
23
+ "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
24
+ "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
25
+ "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
26
+ "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors",
27
+ "model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
28
+ "model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
29
+ "model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
30
+ "model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
31
+ "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
32
+ "model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
33
+ "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
34
+ "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
35
+ "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors",
36
+ "model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
37
+ "model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
38
+ "model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
39
+ "model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
40
+ "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
41
+ "model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
42
+ "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
43
+ "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
44
+ "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors",
45
+ "model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
46
+ "model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
47
+ "model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
48
+ "model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
49
+ "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
50
+ "model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
51
+ "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
52
+ "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
53
+ "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors",
54
+ "model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
55
+ "model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
56
+ "model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
57
+ "model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
58
+ "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
59
+ "model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
60
+ "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
61
+ "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
62
+ "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors",
63
+ "model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
64
+ "model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
65
+ "model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
66
+ "model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
67
+ "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
68
+ "model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
69
+ "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
70
+ "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
71
+ "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors",
72
+ "model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
73
+ "model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
74
+ "model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
75
+ "model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
76
+ "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
77
+ "model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
78
+ "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
79
+ "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
80
+ "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors",
81
+ "model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
82
+ "model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
83
+ "model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
84
+ "model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
85
+ "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
86
+ "model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
87
+ "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
88
+ "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
89
+ "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors",
90
+ "model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
91
+ "model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
92
+ "model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
93
+ "model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
94
+ "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
95
+ "model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
96
+ "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
97
+ "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
98
+ "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors",
99
+ "model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
100
+ "model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
101
+ "model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
102
+ "model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
103
+ "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
104
+ "model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
105
+ "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
106
+ "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
107
+ "model.layers.19.input_layernorm.weight": "model-00002-of-00002.safetensors",
108
+ "model.layers.19.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
109
+ "model.layers.19.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
110
+ "model.layers.19.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
111
+ "model.layers.19.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
112
+ "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
113
+ "model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
114
+ "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
115
+ "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
116
+ "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors",
117
+ "model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
118
+ "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
119
+ "model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
120
+ "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
121
+ "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
122
+ "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
123
+ "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
124
+ "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
125
+ "model.layers.20.input_layernorm.weight": "model-00002-of-00002.safetensors",
126
+ "model.layers.20.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
127
+ "model.layers.20.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
128
+ "model.layers.20.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
129
+ "model.layers.20.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
130
+ "model.layers.20.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
131
+ "model.layers.20.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
132
+ "model.layers.20.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
133
+ "model.layers.20.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
134
+ "model.layers.21.input_layernorm.weight": "model-00002-of-00002.safetensors",
135
+ "model.layers.21.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
136
+ "model.layers.21.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
137
+ "model.layers.21.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
138
+ "model.layers.21.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
139
+ "model.layers.21.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
140
+ "model.layers.21.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
141
+ "model.layers.21.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
142
+ "model.layers.21.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
143
+ "model.layers.22.input_layernorm.weight": "model-00002-of-00002.safetensors",
144
+ "model.layers.22.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
145
+ "model.layers.22.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
146
+ "model.layers.22.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
147
+ "model.layers.22.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
148
+ "model.layers.22.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
149
+ "model.layers.22.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
150
+ "model.layers.22.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
151
+ "model.layers.22.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
152
+ "model.layers.23.input_layernorm.weight": "model-00002-of-00002.safetensors",
153
+ "model.layers.23.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
154
+ "model.layers.23.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
155
+ "model.layers.23.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
156
+ "model.layers.23.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
157
+ "model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
158
+ "model.layers.23.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
159
+ "model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
160
+ "model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
161
+ "model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors",
162
+ "model.layers.24.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
163
+ "model.layers.24.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
164
+ "model.layers.24.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
165
+ "model.layers.24.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
166
+ "model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
167
+ "model.layers.24.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
168
+ "model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
169
+ "model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
170
+ "model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors",
171
+ "model.layers.25.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
172
+ "model.layers.25.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
173
+ "model.layers.25.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
174
+ "model.layers.25.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
175
+ "model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
176
+ "model.layers.25.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
177
+ "model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
178
+ "model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
179
+ "model.layers.3.input_layernorm.weight": "model-00001-of-00002.safetensors",
180
+ "model.layers.3.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
181
+ "model.layers.3.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
182
+ "model.layers.3.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
183
+ "model.layers.3.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
184
+ "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
185
+ "model.layers.3.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
186
+ "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
187
+ "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
188
+ "model.layers.4.input_layernorm.weight": "model-00001-of-00002.safetensors",
189
+ "model.layers.4.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
190
+ "model.layers.4.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
191
+ "model.layers.4.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
192
+ "model.layers.4.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
193
+ "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
194
+ "model.layers.4.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
195
+ "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
196
+ "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
197
+ "model.layers.5.input_layernorm.weight": "model-00001-of-00002.safetensors",
198
+ "model.layers.5.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
199
+ "model.layers.5.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
200
+ "model.layers.5.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
201
+ "model.layers.5.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
202
+ "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
203
+ "model.layers.5.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
204
+ "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
205
+ "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
206
+ "model.layers.6.input_layernorm.weight": "model-00001-of-00002.safetensors",
207
+ "model.layers.6.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
208
+ "model.layers.6.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
209
+ "model.layers.6.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
210
+ "model.layers.6.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
211
+ "model.layers.6.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
212
+ "model.layers.6.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
213
+ "model.layers.6.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
214
+ "model.layers.6.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
215
+ "model.layers.7.input_layernorm.weight": "model-00001-of-00002.safetensors",
216
+ "model.layers.7.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
217
+ "model.layers.7.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
218
+ "model.layers.7.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
219
+ "model.layers.7.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
220
+ "model.layers.7.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
221
+ "model.layers.7.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
222
+ "model.layers.7.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
223
+ "model.layers.7.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
224
+ "model.layers.8.input_layernorm.weight": "model-00001-of-00002.safetensors",
225
+ "model.layers.8.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
226
+ "model.layers.8.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
227
+ "model.layers.8.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
228
+ "model.layers.8.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
229
+ "model.layers.8.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
230
+ "model.layers.8.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
231
+ "model.layers.8.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
232
+ "model.layers.8.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
233
+ "model.layers.9.input_layernorm.weight": "model-00001-of-00002.safetensors",
234
+ "model.layers.9.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
235
+ "model.layers.9.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
236
+ "model.layers.9.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
237
+ "model.layers.9.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
238
+ "model.layers.9.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
239
+ "model.layers.9.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
240
+ "model.layers.9.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
241
+ "model.layers.9.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
242
+ "model.norm.weight": "model-00002-of-00002.safetensors"
243
+ }
244
+ }
modeling_llama.py ADDED
@@ -0,0 +1,1016 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 .configuration_llama import LlamaConfig
34
+
35
+
36
+ logger = logging.get_logger(__name__)
37
+
38
+ _CONFIG_FOR_DOC = "LlamaConfig"
39
+
40
+
41
+ # Copied from transformers.models.bart.modeling_bart._make_causal_mask
42
+ def _make_causal_mask(
43
+ input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
44
+ ):
45
+ """
46
+ Make causal mask used for bi-directional self-attention.
47
+ """
48
+ bsz, tgt_len = input_ids_shape
49
+ mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device)
50
+ mask_cond = torch.arange(mask.size(-1), device=device)
51
+ mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
52
+ mask = mask.to(dtype)
53
+
54
+ if past_key_values_length > 0:
55
+ mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
56
+ return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
57
+
58
+
59
+ # Copied from transformers.models.bart.modeling_bart._expand_mask
60
+ def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
61
+ """
62
+ Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
63
+ """
64
+ bsz, src_len = mask.size()
65
+ tgt_len = tgt_len if tgt_len is not None else src_len
66
+
67
+ expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
68
+
69
+ inverted_mask = 1.0 - expanded_mask
70
+
71
+ return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
72
+
73
+ def _find_correction_factor(num_rotations, dim, base=10000, max_position_embeddings=2048):
74
+ return (dim * math.log(max_position_embeddings/(num_rotations * 2 * math.pi)))/(2 * math.log(base)) #Inverse dim formula to find number of rotations
75
+
76
+ def _find_correction_range(low_rot, high_rot, dim, base=10000, max_position_embeddings=2048):
77
+ low = math.floor(_find_correction_factor(low_rot, dim, base, max_position_embeddings))
78
+ high = math.ceil(_find_correction_factor(high_rot, dim, base, max_position_embeddings))
79
+ return max(low, 0), min(high, dim-1) #Clamp values just in case
80
+
81
+ def _linear_ramp_mask(min, max, dim):
82
+ if min == max:
83
+ max += 0.001 #Prevent singularity
84
+
85
+ linear_func = (torch.arange(dim, dtype=torch.float32) - min) / (max - min)
86
+ ramp_func = torch.clamp(linear_func, 0, 1)
87
+ return ramp_func
88
+
89
+ def _find_newbase_ntk(dim, base=10000, scale=1):
90
+ return base * scale ** (dim / (dim-2))
91
+
92
+ def _ntk_build_inv_freq(dim, base, scaling_factor, ntk_factor, extrapolation_factor, original_max_position_embeddings, device):
93
+ #Interpolation constants found experimentally for LLaMA (might not be totally optimal though)
94
+ #Do not change unless there is a good reason for doing so!
95
+ beta_0 = 1.25
96
+ beta_1 = 0.75
97
+ gamma_0 = 16
98
+ gamma_1 = 2
99
+
100
+ #Three RoPE extrapolation/interpolation methods
101
+ inv_freq_base = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
102
+ inv_freq_linear = 1.0 / (scaling_factor * (base ** (torch.arange(0, dim, 2).float().to(device) / dim)))
103
+ inv_freq_ntk = 1.0 / (_find_newbase_ntk(dim, base, scaling_factor) ** (torch.arange(0, dim, 2).float().to(device) / dim))
104
+
105
+ current_dtype = inv_freq_ntk.dtype
106
+ current_device = inv_freq_ntk.device
107
+
108
+ #Combine NTK and Linear
109
+ low, high = _find_correction_range(beta_0, beta_1, dim, base, original_max_position_embeddings)
110
+ inv_freq_mask = (1 - _linear_ramp_mask(low, high, dim // 2).type(current_dtype).to(current_device)) * ntk_factor
111
+ inv_freq = inv_freq_linear * (1 - inv_freq_mask) + inv_freq_ntk * inv_freq_mask
112
+
113
+ #Combine Extrapolation and NTK and Linear
114
+ low, high = _find_correction_range(gamma_0, gamma_1, dim, base, original_max_position_embeddings)
115
+ inv_freq_mask = (1 - _linear_ramp_mask(low, high, dim // 2).type(current_dtype).to(current_device)) * extrapolation_factor
116
+ inv_freq = inv_freq * (1 - inv_freq_mask) + inv_freq_base * inv_freq_mask
117
+
118
+ class LlamaRMSNorm(nn.Module):
119
+ def __init__(self, hidden_size, eps=1e-6):
120
+ """
121
+ LlamaRMSNorm is equivalent to T5LayerNorm
122
+ """
123
+ super().__init__()
124
+ self.weight = nn.Parameter(torch.ones(hidden_size))
125
+ self.variance_epsilon = eps
126
+
127
+ def forward(self, hidden_states):
128
+ input_dtype = hidden_states.dtype
129
+ variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
130
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
131
+
132
+ return (self.weight * hidden_states).to(input_dtype)
133
+
134
+
135
+ class LlamaRotaryEmbedding(torch.nn.Module):
136
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
137
+ super().__init__()
138
+
139
+ self.dim = dim
140
+ self.max_position_embeddings = max_position_embeddings
141
+ self.base = base
142
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
143
+ self.register_buffer("inv_freq", inv_freq)
144
+
145
+ # Build here to make `torch.jit.trace` work.
146
+ self._set_cos_sin_cache(
147
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
148
+ )
149
+
150
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
151
+ self.max_seq_len_cached = seq_len
152
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
153
+
154
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
155
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
156
+ emb = torch.cat((freqs, freqs), dim=-1)
157
+ self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
158
+ self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
159
+
160
+ def forward(self, x, seq_len=None):
161
+ # x: [bs, num_attention_heads, seq_len, head_size]
162
+ if seq_len > self.max_seq_len_cached:
163
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
164
+
165
+ return (
166
+ self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
167
+ self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
168
+ )
169
+
170
+
171
+ class LlamaLinearScalingRotaryEmbedding(LlamaRotaryEmbedding):
172
+ """LlamaRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
173
+
174
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
175
+ self.scaling_factor = scaling_factor
176
+ super().__init__(dim, max_position_embeddings, base, device)
177
+
178
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
179
+ self.max_seq_len_cached = seq_len
180
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
181
+ t = t / self.scaling_factor
182
+
183
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
184
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
185
+ emb = torch.cat((freqs, freqs), dim=-1)
186
+ self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
187
+ self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
188
+
189
+
190
+ class LlamaDynamicNTKScalingRotaryEmbedding(LlamaRotaryEmbedding):
191
+ """LlamaRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
192
+
193
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
194
+ self.scaling_factor = scaling_factor
195
+ super().__init__(dim, max_position_embeddings, base, device)
196
+
197
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
198
+ self.max_seq_len_cached = seq_len
199
+
200
+ if seq_len > self.max_position_embeddings:
201
+ base = self.base * (
202
+ (self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
203
+ ) ** (self.dim / (self.dim - 2))
204
+ inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
205
+ self.register_buffer("inv_freq", inv_freq)
206
+
207
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
208
+
209
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
210
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
211
+ emb = torch.cat((freqs, freqs), dim=-1)
212
+ self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
213
+ self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
214
+
215
+ class LlamaNTKRotaryEmbedding(LlamaRotaryEmbedding):
216
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0, ntk_factor=1.0, extrapolation_factor=1.0, original_max_position_embeddings=2048):
217
+ super().__init__(dim, max_position_embeddings, base, device)
218
+
219
+ self.dim = dim
220
+ self.max_position_embeddings = max_position_embeddings
221
+ self.base = base
222
+
223
+ inv_freq = _ntk_build_inv_freq(dim, base, scaling_factor, ntk_factor, extrapolation_factor, original_max_position_embeddings, device)
224
+ self.register_buffer("inv_freq", inv_freq)
225
+
226
+ # Build here to make `torch.jit.trace` work.
227
+ self._set_cos_sin_cache(
228
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
229
+ )
230
+
231
+ def rotate_half(x):
232
+ """Rotates half the hidden dims of the input."""
233
+ x1 = x[..., : x.shape[-1] // 2]
234
+ x2 = x[..., x.shape[-1] // 2 :]
235
+ return torch.cat((-x2, x1), dim=-1)
236
+
237
+
238
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
239
+ # The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
240
+ cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
241
+ sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
242
+ cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
243
+ sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
244
+ q_embed = (q * cos) + (rotate_half(q) * sin)
245
+ k_embed = (k * cos) + (rotate_half(k) * sin)
246
+ return q_embed, k_embed
247
+
248
+
249
+ class LlamaMLP(nn.Module):
250
+ def __init__(
251
+ self,
252
+ hidden_size: int,
253
+ intermediate_size: int,
254
+ hidden_act: str,
255
+ ):
256
+ super().__init__()
257
+ self.gate_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
258
+ self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False)
259
+ self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
260
+ self.act_fn = ACT2FN[hidden_act]
261
+
262
+ def forward(self, x):
263
+ return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
264
+
265
+
266
+ class LlamaAttention(nn.Module):
267
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
268
+
269
+ def __init__(self, config: LlamaConfig):
270
+ super().__init__()
271
+ self.config = config
272
+ self.hidden_size = config.hidden_size
273
+ self.num_heads = config.num_attention_heads
274
+ self.head_dim = self.hidden_size // self.num_heads
275
+ self.max_position_embeddings = config.max_position_embeddings
276
+
277
+ if (self.head_dim * self.num_heads) != self.hidden_size:
278
+ raise ValueError(
279
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
280
+ f" and `num_heads`: {self.num_heads})."
281
+ )
282
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
283
+ self.k_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
284
+ self.v_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
285
+ self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
286
+ self._init_rope()
287
+
288
+ def _init_rope(self):
289
+ if self.config.rope_scaling is None:
290
+ self.rotary_emb = LlamaRotaryEmbedding(self.head_dim, max_position_embeddings=self.max_position_embeddings)
291
+ else:
292
+ scaling_type = self.config.rope_scaling["type"]
293
+ scaling_factor = self.config.rope_scaling["factor"]
294
+ if scaling_type == "linear":
295
+ self.rotary_emb = LlamaLinearScalingRotaryEmbedding(
296
+ self.head_dim, max_position_embeddings=self.max_position_embeddings, scaling_factor=scaling_factor
297
+ )
298
+ elif scaling_type == "dynamic":
299
+ self.rotary_emb = LlamaDynamicNTKScalingRotaryEmbedding(
300
+ self.head_dim, max_position_embeddings=self.max_position_embeddings, scaling_factor=scaling_factor
301
+ )
302
+ elif scaling_type == "ntk":
303
+ self.rotary_emb = LlamaNTKRotaryEmbedding(
304
+ self.head_dim, max_position_embeddings=self.max_position_embeddings, scaling_factor=scaling_factor
305
+ )
306
+ else:
307
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
308
+
309
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
310
+ return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
311
+
312
+ def forward(
313
+ self,
314
+ hidden_states: torch.Tensor,
315
+ attention_mask: Optional[torch.Tensor] = None,
316
+ position_ids: Optional[torch.LongTensor] = None,
317
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
318
+ output_attentions: bool = False,
319
+ use_cache: bool = False,
320
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
321
+ bsz, q_len, _ = hidden_states.size()
322
+
323
+ query_states = self.q_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
324
+ key_states = self.k_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
325
+ value_states = self.v_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
326
+
327
+ kv_seq_len = key_states.shape[-2]
328
+ if past_key_value is not None:
329
+ kv_seq_len += past_key_value[0].shape[-2]
330
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
331
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
332
+ # [bsz, nh, t, hd]
333
+
334
+ if past_key_value is not None:
335
+ # reuse k, v, self_attention
336
+ key_states = torch.cat([past_key_value[0], key_states], dim=2)
337
+ value_states = torch.cat([past_key_value[1], value_states], dim=2)
338
+
339
+ past_key_value = (key_states, value_states) if use_cache else None
340
+
341
+ attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
342
+
343
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
344
+ raise ValueError(
345
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
346
+ f" {attn_weights.size()}"
347
+ )
348
+
349
+ if attention_mask is not None:
350
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
351
+ raise ValueError(
352
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
353
+ )
354
+ attn_weights = attn_weights + attention_mask
355
+ dtype_min = torch.tensor(
356
+ torch.finfo(attn_weights.dtype).min, device=attn_weights.device, dtype=attn_weights.dtype
357
+ )
358
+ attn_weights = torch.max(attn_weights, dtype_min)
359
+
360
+ # upcast attention to fp32
361
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
362
+ attn_output = torch.matmul(attn_weights, value_states)
363
+
364
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
365
+ raise ValueError(
366
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
367
+ f" {attn_output.size()}"
368
+ )
369
+
370
+ attn_output = attn_output.transpose(1, 2)
371
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
372
+
373
+ attn_output = self.o_proj(attn_output)
374
+
375
+ if not output_attentions:
376
+ attn_weights = None
377
+
378
+ return attn_output, attn_weights, past_key_value
379
+
380
+
381
+ class LlamaDecoderLayer(nn.Module):
382
+ def __init__(self, config: LlamaConfig):
383
+ super().__init__()
384
+ self.hidden_size = config.hidden_size
385
+ self.self_attn = LlamaAttention(config=config)
386
+ self.mlp = LlamaMLP(
387
+ hidden_size=self.hidden_size,
388
+ intermediate_size=config.intermediate_size,
389
+ hidden_act=config.hidden_act,
390
+ )
391
+ self.input_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
392
+ self.post_attention_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
393
+
394
+ def forward(
395
+ self,
396
+ hidden_states: torch.Tensor,
397
+ attention_mask: Optional[torch.Tensor] = None,
398
+ position_ids: Optional[torch.LongTensor] = None,
399
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
400
+ output_attentions: Optional[bool] = False,
401
+ use_cache: Optional[bool] = False,
402
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
403
+ """
404
+ Args:
405
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
406
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
407
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
408
+ output_attentions (`bool`, *optional*):
409
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
410
+ returned tensors for more detail.
411
+ use_cache (`bool`, *optional*):
412
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
413
+ (see `past_key_values`).
414
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
415
+ """
416
+
417
+ residual = hidden_states
418
+
419
+ hidden_states = self.input_layernorm(hidden_states)
420
+
421
+ # Self Attention
422
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
423
+ hidden_states=hidden_states,
424
+ attention_mask=attention_mask,
425
+ position_ids=position_ids,
426
+ past_key_value=past_key_value,
427
+ output_attentions=output_attentions,
428
+ use_cache=use_cache,
429
+ )
430
+ hidden_states = residual + hidden_states
431
+
432
+ # Fully Connected
433
+ residual = hidden_states
434
+ hidden_states = self.post_attention_layernorm(hidden_states)
435
+ hidden_states = self.mlp(hidden_states)
436
+ hidden_states = residual + hidden_states
437
+
438
+ outputs = (hidden_states,)
439
+
440
+ if output_attentions:
441
+ outputs += (self_attn_weights,)
442
+
443
+ if use_cache:
444
+ outputs += (present_key_value,)
445
+
446
+ return outputs
447
+
448
+
449
+ LLAMA_START_DOCSTRING = r"""
450
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
451
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
452
+ etc.)
453
+
454
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
455
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
456
+ and behavior.
457
+
458
+ Parameters:
459
+ config ([`LlamaConfig`]):
460
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
461
+ load the weights associated with the model, only the configuration. Check out the
462
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
463
+ """
464
+
465
+
466
+ @add_start_docstrings(
467
+ "The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
468
+ LLAMA_START_DOCSTRING,
469
+ )
470
+ class LlamaPreTrainedModel(PreTrainedModel):
471
+ config_class = LlamaConfig
472
+ base_model_prefix = "model"
473
+ supports_gradient_checkpointing = True
474
+ _no_split_modules = ["LlamaDecoderLayer"]
475
+ _skip_keys_device_placement = "past_key_values"
476
+
477
+ def _init_weights(self, module):
478
+ std = self.config.initializer_range
479
+ if isinstance(module, nn.Linear):
480
+ module.weight.data.normal_(mean=0.0, std=std)
481
+ if module.bias is not None:
482
+ module.bias.data.zero_()
483
+ elif isinstance(module, nn.Embedding):
484
+ module.weight.data.normal_(mean=0.0, std=std)
485
+ if module.padding_idx is not None:
486
+ module.weight.data[module.padding_idx].zero_()
487
+
488
+ def _set_gradient_checkpointing(self, module, value=False):
489
+ if isinstance(module, LlamaModel):
490
+ module.gradient_checkpointing = value
491
+
492
+
493
+ LLAMA_INPUTS_DOCSTRING = r"""
494
+ Args:
495
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
496
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
497
+ it.
498
+
499
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
500
+ [`PreTrainedTokenizer.__call__`] for details.
501
+
502
+ [What are input IDs?](../glossary#input-ids)
503
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
504
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
505
+
506
+ - 1 for tokens that are **not masked**,
507
+ - 0 for tokens that are **masked**.
508
+
509
+ [What are attention masks?](../glossary#attention-mask)
510
+
511
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
512
+ [`PreTrainedTokenizer.__call__`] for details.
513
+
514
+ If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
515
+ `past_key_values`).
516
+
517
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
518
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
519
+ information on the default strategy.
520
+
521
+ - 1 indicates the head is **not masked**,
522
+ - 0 indicates the head is **masked**.
523
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
524
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
525
+ config.n_positions - 1]`.
526
+
527
+ [What are position IDs?](../glossary#position-ids)
528
+ past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
529
+ Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
530
+ `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
531
+ `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
532
+
533
+ Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
534
+ blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
535
+
536
+ If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
537
+ don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
538
+ `decoder_input_ids` of shape `(batch_size, sequence_length)`.
539
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
540
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
541
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
542
+ model's internal embedding lookup matrix.
543
+ use_cache (`bool`, *optional*):
544
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
545
+ `past_key_values`).
546
+ output_attentions (`bool`, *optional*):
547
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
548
+ tensors for more detail.
549
+ output_hidden_states (`bool`, *optional*):
550
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
551
+ more detail.
552
+ return_dict (`bool`, *optional*):
553
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
554
+ """
555
+
556
+
557
+ @add_start_docstrings(
558
+ "The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
559
+ LLAMA_START_DOCSTRING,
560
+ )
561
+ class LlamaModel(LlamaPreTrainedModel):
562
+ """
563
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`LlamaDecoderLayer`]
564
+
565
+ Args:
566
+ config: LlamaConfig
567
+ """
568
+
569
+ def __init__(self, config: LlamaConfig):
570
+ super().__init__(config)
571
+ self.padding_idx = config.pad_token_id
572
+ self.vocab_size = config.vocab_size
573
+
574
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
575
+ self.layers = nn.ModuleList([LlamaDecoderLayer(config) for _ in range(config.num_hidden_layers)])
576
+ self.norm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
577
+
578
+ self.gradient_checkpointing = False
579
+ # Initialize weights and apply final processing
580
+ self.post_init()
581
+
582
+ def get_input_embeddings(self):
583
+ return self.embed_tokens
584
+
585
+ def set_input_embeddings(self, value):
586
+ self.embed_tokens = value
587
+
588
+ # Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
589
+ def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
590
+ # create causal mask
591
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
592
+ combined_attention_mask = None
593
+ if input_shape[-1] > 1:
594
+ combined_attention_mask = _make_causal_mask(
595
+ input_shape,
596
+ inputs_embeds.dtype,
597
+ device=inputs_embeds.device,
598
+ past_key_values_length=past_key_values_length,
599
+ )
600
+
601
+ if attention_mask is not None:
602
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
603
+ expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
604
+ inputs_embeds.device
605
+ )
606
+ combined_attention_mask = (
607
+ expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
608
+ )
609
+
610
+ return combined_attention_mask
611
+
612
+ @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
613
+ def forward(
614
+ self,
615
+ input_ids: torch.LongTensor = None,
616
+ attention_mask: Optional[torch.Tensor] = None,
617
+ position_ids: Optional[torch.LongTensor] = None,
618
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
619
+ inputs_embeds: Optional[torch.FloatTensor] = None,
620
+ use_cache: Optional[bool] = None,
621
+ output_attentions: Optional[bool] = None,
622
+ output_hidden_states: Optional[bool] = None,
623
+ return_dict: Optional[bool] = None,
624
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
625
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
626
+ output_hidden_states = (
627
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
628
+ )
629
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
630
+
631
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
632
+
633
+ # retrieve input_ids and inputs_embeds
634
+ if input_ids is not None and inputs_embeds is not None:
635
+ raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
636
+ elif input_ids is not None:
637
+ batch_size, seq_length = input_ids.shape
638
+ elif inputs_embeds is not None:
639
+ batch_size, seq_length, _ = inputs_embeds.shape
640
+ else:
641
+ raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
642
+
643
+ seq_length_with_past = seq_length
644
+ past_key_values_length = 0
645
+
646
+ if past_key_values is not None:
647
+ past_key_values_length = past_key_values[0][0].shape[2]
648
+ seq_length_with_past = seq_length_with_past + past_key_values_length
649
+
650
+ if position_ids is None:
651
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
652
+ position_ids = torch.arange(
653
+ past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
654
+ )
655
+ position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
656
+ else:
657
+ position_ids = position_ids.view(-1, seq_length).long()
658
+
659
+ if inputs_embeds is None:
660
+ inputs_embeds = self.embed_tokens(input_ids)
661
+ # embed positions
662
+ if attention_mask is None:
663
+ attention_mask = torch.ones(
664
+ (batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
665
+ )
666
+ attention_mask = self._prepare_decoder_attention_mask(
667
+ attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
668
+ )
669
+
670
+ hidden_states = inputs_embeds
671
+
672
+ if self.gradient_checkpointing and self.training:
673
+ if use_cache:
674
+ logger.warning_once(
675
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
676
+ )
677
+ use_cache = False
678
+
679
+ # decoder layers
680
+ all_hidden_states = () if output_hidden_states else None
681
+ all_self_attns = () if output_attentions else None
682
+ next_decoder_cache = () if use_cache else None
683
+
684
+ for idx, decoder_layer in enumerate(self.layers):
685
+ if output_hidden_states:
686
+ all_hidden_states += (hidden_states,)
687
+
688
+ past_key_value = past_key_values[idx] if past_key_values is not None else None
689
+
690
+ if self.gradient_checkpointing and self.training:
691
+
692
+ def create_custom_forward(module):
693
+ def custom_forward(*inputs):
694
+ # None for past_key_value
695
+ return module(*inputs, output_attentions, None)
696
+
697
+ return custom_forward
698
+
699
+ layer_outputs = torch.utils.checkpoint.checkpoint(
700
+ create_custom_forward(decoder_layer),
701
+ hidden_states,
702
+ attention_mask,
703
+ position_ids,
704
+ None,
705
+ )
706
+ else:
707
+ layer_outputs = decoder_layer(
708
+ hidden_states,
709
+ attention_mask=attention_mask,
710
+ position_ids=position_ids,
711
+ past_key_value=past_key_value,
712
+ output_attentions=output_attentions,
713
+ use_cache=use_cache,
714
+ )
715
+
716
+ hidden_states = layer_outputs[0]
717
+
718
+ if use_cache:
719
+ next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
720
+
721
+ if output_attentions:
722
+ all_self_attns += (layer_outputs[1],)
723
+
724
+ hidden_states = self.norm(hidden_states)
725
+
726
+ # add hidden states from the last decoder layer
727
+ if output_hidden_states:
728
+ all_hidden_states += (hidden_states,)
729
+
730
+ next_cache = next_decoder_cache if use_cache else None
731
+ if not return_dict:
732
+ return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
733
+ return BaseModelOutputWithPast(
734
+ last_hidden_state=hidden_states,
735
+ past_key_values=next_cache,
736
+ hidden_states=all_hidden_states,
737
+ attentions=all_self_attns,
738
+ )
739
+
740
+
741
+ class LlamaForCausalLM(LlamaPreTrainedModel):
742
+ _tied_weights_keys = ["lm_head.weight"]
743
+
744
+ def __init__(self, config):
745
+ super().__init__(config)
746
+ self.model = LlamaModel(config)
747
+
748
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
749
+
750
+ # Initialize weights and apply final processing
751
+ self.post_init()
752
+
753
+ def get_input_embeddings(self):
754
+ return self.model.embed_tokens
755
+
756
+ def set_input_embeddings(self, value):
757
+ self.model.embed_tokens = value
758
+
759
+ def get_output_embeddings(self):
760
+ return self.lm_head
761
+
762
+ def set_output_embeddings(self, new_embeddings):
763
+ self.lm_head = new_embeddings
764
+
765
+ def set_decoder(self, decoder):
766
+ self.model = decoder
767
+
768
+ def get_decoder(self):
769
+ return self.model
770
+
771
+ @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
772
+ @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
773
+ def forward(
774
+ self,
775
+ input_ids: torch.LongTensor = None,
776
+ attention_mask: Optional[torch.Tensor] = None,
777
+ position_ids: Optional[torch.LongTensor] = None,
778
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
779
+ inputs_embeds: Optional[torch.FloatTensor] = None,
780
+ labels: Optional[torch.LongTensor] = None,
781
+ use_cache: Optional[bool] = None,
782
+ output_attentions: Optional[bool] = None,
783
+ output_hidden_states: Optional[bool] = None,
784
+ return_dict: Optional[bool] = None,
785
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
786
+ r"""
787
+ Args:
788
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
789
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
790
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
791
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
792
+
793
+ Returns:
794
+
795
+ Example:
796
+
797
+ ```python
798
+ >>> from transformers import AutoTokenizer, LlamaForCausalLM
799
+
800
+ >>> model = LlamaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
801
+ >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
802
+
803
+ >>> prompt = "Hey, are you conscious? Can you talk to me?"
804
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
805
+
806
+ >>> # Generate
807
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
808
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
809
+ "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
810
+ ```"""
811
+
812
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
813
+ output_hidden_states = (
814
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
815
+ )
816
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
817
+
818
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
819
+ outputs = self.model(
820
+ input_ids=input_ids,
821
+ attention_mask=attention_mask,
822
+ position_ids=position_ids,
823
+ past_key_values=past_key_values,
824
+ inputs_embeds=inputs_embeds,
825
+ use_cache=use_cache,
826
+ output_attentions=output_attentions,
827
+ output_hidden_states=output_hidden_states,
828
+ return_dict=return_dict,
829
+ )
830
+
831
+ hidden_states = outputs[0]
832
+ logits = self.lm_head(hidden_states)
833
+
834
+ loss = None
835
+ if labels is not None:
836
+ # Shift so that tokens < n predict n
837
+ shift_logits = logits[..., :-1, :].contiguous()
838
+ shift_labels = labels[..., 1:].contiguous()
839
+ # Flatten the tokens
840
+ loss_fct = CrossEntropyLoss()
841
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
842
+ shift_labels = shift_labels.view(-1)
843
+ # Enable model parallelism
844
+ shift_labels = shift_labels.to(shift_logits.device)
845
+ loss = loss_fct(shift_logits, shift_labels)
846
+
847
+ if not return_dict:
848
+ output = (logits,) + outputs[1:]
849
+ return (loss,) + output if loss is not None else output
850
+
851
+ return CausalLMOutputWithPast(
852
+ loss=loss,
853
+ logits=logits,
854
+ past_key_values=outputs.past_key_values,
855
+ hidden_states=outputs.hidden_states,
856
+ attentions=outputs.attentions,
857
+ )
858
+
859
+ def prepare_inputs_for_generation(
860
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
861
+ ):
862
+ if past_key_values:
863
+ input_ids = input_ids[:, -1:]
864
+
865
+ position_ids = kwargs.get("position_ids", None)
866
+ if attention_mask is not None and position_ids is None:
867
+ # create position_ids on the fly for batch generation
868
+ position_ids = attention_mask.long().cumsum(-1) - 1
869
+ position_ids.masked_fill_(attention_mask == 0, 1)
870
+ if past_key_values:
871
+ position_ids = position_ids[:, -1].unsqueeze(-1)
872
+
873
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
874
+ if inputs_embeds is not None and past_key_values is None:
875
+ model_inputs = {"inputs_embeds": inputs_embeds}
876
+ else:
877
+ model_inputs = {"input_ids": input_ids}
878
+
879
+ model_inputs.update(
880
+ {
881
+ "position_ids": position_ids,
882
+ "past_key_values": past_key_values,
883
+ "use_cache": kwargs.get("use_cache"),
884
+ "attention_mask": attention_mask,
885
+ }
886
+ )
887
+ return model_inputs
888
+
889
+ @staticmethod
890
+ def _reorder_cache(past_key_values, beam_idx):
891
+ reordered_past = ()
892
+ for layer_past in past_key_values:
893
+ reordered_past += (
894
+ tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
895
+ )
896
+ return reordered_past
897
+
898
+
899
+ @add_start_docstrings(
900
+ """
901
+ The LLaMa Model transformer with a sequence classification head on top (linear layer).
902
+
903
+ [`LlamaForSequenceClassification`] uses the last token in order to do the classification, as other causal models
904
+ (e.g. GPT-2) do.
905
+
906
+ Since it does classification on the last token, it requires to know the position of the last token. If a
907
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
908
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
909
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
910
+ each row of the batch).
911
+ """,
912
+ LLAMA_START_DOCSTRING,
913
+ )
914
+ class LlamaForSequenceClassification(LlamaPreTrainedModel):
915
+ def __init__(self, config):
916
+ super().__init__(config)
917
+ self.num_labels = config.num_labels
918
+ self.model = LlamaModel(config)
919
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
920
+
921
+ # Initialize weights and apply final processing
922
+ self.post_init()
923
+
924
+ def get_input_embeddings(self):
925
+ return self.model.embed_tokens
926
+
927
+ def set_input_embeddings(self, value):
928
+ self.model.embed_tokens = value
929
+
930
+ @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
931
+ def forward(
932
+ self,
933
+ input_ids: torch.LongTensor = None,
934
+ attention_mask: Optional[torch.Tensor] = None,
935
+ position_ids: Optional[torch.LongTensor] = None,
936
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
937
+ inputs_embeds: Optional[torch.FloatTensor] = None,
938
+ labels: Optional[torch.LongTensor] = None,
939
+ use_cache: Optional[bool] = None,
940
+ output_attentions: Optional[bool] = None,
941
+ output_hidden_states: Optional[bool] = None,
942
+ return_dict: Optional[bool] = None,
943
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
944
+ r"""
945
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
946
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
947
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
948
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
949
+ """
950
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
951
+
952
+ transformer_outputs = self.model(
953
+ input_ids,
954
+ attention_mask=attention_mask,
955
+ position_ids=position_ids,
956
+ past_key_values=past_key_values,
957
+ inputs_embeds=inputs_embeds,
958
+ use_cache=use_cache,
959
+ output_attentions=output_attentions,
960
+ output_hidden_states=output_hidden_states,
961
+ return_dict=return_dict,
962
+ )
963
+ hidden_states = transformer_outputs[0]
964
+ logits = self.score(hidden_states)
965
+
966
+ if input_ids is not None:
967
+ batch_size = input_ids.shape[0]
968
+ else:
969
+ batch_size = inputs_embeds.shape[0]
970
+
971
+ if self.config.pad_token_id is None and batch_size != 1:
972
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
973
+ if self.config.pad_token_id is None:
974
+ sequence_lengths = -1
975
+ else:
976
+ if input_ids is not None:
977
+ sequence_lengths = (torch.ne(input_ids, self.config.pad_token_id).sum(-1) - 1).to(logits.device)
978
+ else:
979
+ sequence_lengths = -1
980
+
981
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
982
+
983
+ loss = None
984
+ if labels is not None:
985
+ labels = labels.to(logits.device)
986
+ if self.config.problem_type is None:
987
+ if self.num_labels == 1:
988
+ self.config.problem_type = "regression"
989
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
990
+ self.config.problem_type = "single_label_classification"
991
+ else:
992
+ self.config.problem_type = "multi_label_classification"
993
+
994
+ if self.config.problem_type == "regression":
995
+ loss_fct = MSELoss()
996
+ if self.num_labels == 1:
997
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
998
+ else:
999
+ loss = loss_fct(pooled_logits, labels)
1000
+ elif self.config.problem_type == "single_label_classification":
1001
+ loss_fct = CrossEntropyLoss()
1002
+ loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
1003
+ elif self.config.problem_type == "multi_label_classification":
1004
+ loss_fct = BCEWithLogitsLoss()
1005
+ loss = loss_fct(pooled_logits, labels)
1006
+ if not return_dict:
1007
+ output = (pooled_logits,) + transformer_outputs[1:]
1008
+ return ((loss,) + output) if loss is not None else output
1009
+
1010
+ return SequenceClassifierOutputWithPast(
1011
+ loss=loss,
1012
+ logits=pooled_logits,
1013
+ past_key_values=transformer_outputs.past_key_values,
1014
+ hidden_states=transformer_outputs.hidden_states,
1015
+ attentions=transformer_outputs.attentions,
1016
+ )
pytorch_model.bin.index.json ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 13705894400
4
+ },
5
+ "weight_map": {
6
+ "lm_head.weight": "pytorch_model-00002-of-00002.bin",
7
+ "model.embed_tokens.weight": "pytorch_model-00001-of-00002.bin",
8
+ "model.layers.0.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
9
+ "model.layers.0.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
10
+ "model.layers.0.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
11
+ "model.layers.0.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
12
+ "model.layers.0.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
13
+ "model.layers.0.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
14
+ "model.layers.0.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
15
+ "model.layers.0.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
16
+ "model.layers.0.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
17
+ "model.layers.1.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
18
+ "model.layers.1.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
19
+ "model.layers.1.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
20
+ "model.layers.1.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
21
+ "model.layers.1.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
22
+ "model.layers.1.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
23
+ "model.layers.1.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
24
+ "model.layers.1.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
25
+ "model.layers.1.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
26
+ "model.layers.10.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
27
+ "model.layers.10.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
28
+ "model.layers.10.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
29
+ "model.layers.10.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
30
+ "model.layers.10.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
31
+ "model.layers.10.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
32
+ "model.layers.10.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
33
+ "model.layers.10.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
34
+ "model.layers.10.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
35
+ "model.layers.11.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
36
+ "model.layers.11.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
37
+ "model.layers.11.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
38
+ "model.layers.11.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
39
+ "model.layers.11.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
40
+ "model.layers.11.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
41
+ "model.layers.11.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
42
+ "model.layers.11.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
43
+ "model.layers.11.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
44
+ "model.layers.12.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
45
+ "model.layers.12.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
46
+ "model.layers.12.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
47
+ "model.layers.12.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
48
+ "model.layers.12.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
49
+ "model.layers.12.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
50
+ "model.layers.12.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
51
+ "model.layers.12.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
52
+ "model.layers.12.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
53
+ "model.layers.13.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
54
+ "model.layers.13.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
55
+ "model.layers.13.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
56
+ "model.layers.13.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
57
+ "model.layers.13.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
58
+ "model.layers.13.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
59
+ "model.layers.13.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
60
+ "model.layers.13.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
61
+ "model.layers.13.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
62
+ "model.layers.14.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
63
+ "model.layers.14.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
64
+ "model.layers.14.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
65
+ "model.layers.14.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
66
+ "model.layers.14.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
67
+ "model.layers.14.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
68
+ "model.layers.14.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
69
+ "model.layers.14.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
70
+ "model.layers.14.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
71
+ "model.layers.15.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
72
+ "model.layers.15.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
73
+ "model.layers.15.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
74
+ "model.layers.15.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
75
+ "model.layers.15.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
76
+ "model.layers.15.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
77
+ "model.layers.15.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
78
+ "model.layers.15.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
79
+ "model.layers.15.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
80
+ "model.layers.16.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
81
+ "model.layers.16.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
82
+ "model.layers.16.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
83
+ "model.layers.16.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
84
+ "model.layers.16.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
85
+ "model.layers.16.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
86
+ "model.layers.16.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
87
+ "model.layers.16.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
88
+ "model.layers.16.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
89
+ "model.layers.17.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
90
+ "model.layers.17.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
91
+ "model.layers.17.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
92
+ "model.layers.17.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
93
+ "model.layers.17.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
94
+ "model.layers.17.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
95
+ "model.layers.17.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
96
+ "model.layers.17.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
97
+ "model.layers.17.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
98
+ "model.layers.18.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
99
+ "model.layers.18.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
100
+ "model.layers.18.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
101
+ "model.layers.18.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
102
+ "model.layers.18.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
103
+ "model.layers.18.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
104
+ "model.layers.18.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
105
+ "model.layers.18.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
106
+ "model.layers.18.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
107
+ "model.layers.19.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
108
+ "model.layers.19.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
109
+ "model.layers.19.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
110
+ "model.layers.19.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
111
+ "model.layers.19.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
112
+ "model.layers.19.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
113
+ "model.layers.19.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
114
+ "model.layers.19.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
115
+ "model.layers.19.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
116
+ "model.layers.2.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
117
+ "model.layers.2.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
118
+ "model.layers.2.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
119
+ "model.layers.2.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
120
+ "model.layers.2.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
121
+ "model.layers.2.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
122
+ "model.layers.2.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
123
+ "model.layers.2.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
124
+ "model.layers.2.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
125
+ "model.layers.20.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
126
+ "model.layers.20.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
127
+ "model.layers.20.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
128
+ "model.layers.20.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
129
+ "model.layers.20.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
130
+ "model.layers.20.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
131
+ "model.layers.20.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
132
+ "model.layers.20.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
133
+ "model.layers.20.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
134
+ "model.layers.21.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
135
+ "model.layers.21.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
136
+ "model.layers.21.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
137
+ "model.layers.21.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
138
+ "model.layers.21.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
139
+ "model.layers.21.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
140
+ "model.layers.21.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
141
+ "model.layers.21.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
142
+ "model.layers.21.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
143
+ "model.layers.22.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
144
+ "model.layers.22.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
145
+ "model.layers.22.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
146
+ "model.layers.22.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
147
+ "model.layers.22.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
148
+ "model.layers.22.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
149
+ "model.layers.22.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
150
+ "model.layers.22.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
151
+ "model.layers.22.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
152
+ "model.layers.23.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
153
+ "model.layers.23.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
154
+ "model.layers.23.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
155
+ "model.layers.23.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
156
+ "model.layers.23.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
157
+ "model.layers.23.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
158
+ "model.layers.23.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
159
+ "model.layers.23.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
160
+ "model.layers.23.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
161
+ "model.layers.24.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
162
+ "model.layers.24.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
163
+ "model.layers.24.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
164
+ "model.layers.24.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
165
+ "model.layers.24.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
166
+ "model.layers.24.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
167
+ "model.layers.24.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
168
+ "model.layers.24.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
169
+ "model.layers.24.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
170
+ "model.layers.25.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
171
+ "model.layers.25.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
172
+ "model.layers.25.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
173
+ "model.layers.25.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
174
+ "model.layers.25.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
175
+ "model.layers.25.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
176
+ "model.layers.25.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
177
+ "model.layers.25.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
178
+ "model.layers.25.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
179
+ "model.layers.3.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
180
+ "model.layers.3.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
181
+ "model.layers.3.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
182
+ "model.layers.3.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
183
+ "model.layers.3.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
184
+ "model.layers.3.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
185
+ "model.layers.3.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
186
+ "model.layers.3.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
187
+ "model.layers.3.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
188
+ "model.layers.4.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
189
+ "model.layers.4.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
190
+ "model.layers.4.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
191
+ "model.layers.4.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
192
+ "model.layers.4.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
193
+ "model.layers.4.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
194
+ "model.layers.4.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
195
+ "model.layers.4.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
196
+ "model.layers.4.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
197
+ "model.layers.5.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
198
+ "model.layers.5.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
199
+ "model.layers.5.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
200
+ "model.layers.5.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
201
+ "model.layers.5.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
202
+ "model.layers.5.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
203
+ "model.layers.5.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
204
+ "model.layers.5.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
205
+ "model.layers.5.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
206
+ "model.layers.6.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
207
+ "model.layers.6.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
208
+ "model.layers.6.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
209
+ "model.layers.6.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
210
+ "model.layers.6.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
211
+ "model.layers.6.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
212
+ "model.layers.6.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
213
+ "model.layers.6.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
214
+ "model.layers.6.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
215
+ "model.layers.7.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
216
+ "model.layers.7.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
217
+ "model.layers.7.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
218
+ "model.layers.7.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
219
+ "model.layers.7.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
220
+ "model.layers.7.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
221
+ "model.layers.7.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
222
+ "model.layers.7.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
223
+ "model.layers.7.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
224
+ "model.layers.8.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
225
+ "model.layers.8.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
226
+ "model.layers.8.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
227
+ "model.layers.8.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
228
+ "model.layers.8.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
229
+ "model.layers.8.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
230
+ "model.layers.8.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
231
+ "model.layers.8.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
232
+ "model.layers.8.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
233
+ "model.layers.9.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
234
+ "model.layers.9.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
235
+ "model.layers.9.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
236
+ "model.layers.9.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
237
+ "model.layers.9.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
238
+ "model.layers.9.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
239
+ "model.layers.9.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
240
+ "model.layers.9.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
241
+ "model.layers.9.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
242
+ "model.norm.weight": "pytorch_model-00002-of-00002.bin"
243
+ }
244
+ }
special_tokens_map.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": true,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "unk_token": {
17
+ "content": "<unk>",
18
+ "lstrip": false,
19
+ "normalized": true,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ }
23
+ }
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ab1b681ec7fc02fed5edd3026687d7a692a918c4dd8e150ca2e3994a6229843b
3
+ size 534194
tokenizer_config.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "bos_token": {
5
+ "__type": "AddedToken",
6
+ "content": "<s>",
7
+ "lstrip": false,
8
+ "normalized": true,
9
+ "rstrip": false,
10
+ "single_word": false
11
+ },
12
+ "clean_up_tokenization_spaces": false,
13
+ "eos_token": {
14
+ "__type": "AddedToken",
15
+ "content": "</s>",
16
+ "lstrip": false,
17
+ "normalized": true,
18
+ "rstrip": false,
19
+ "single_word": false
20
+ },
21
+ "model_max_length": 8192,
22
+ "pad_token": null,
23
+ "sp_model_kwargs": {},
24
+ "tokenizer_class": "LlamaTokenizer",
25
+ "unk_token": {
26
+ "__type": "AddedToken",
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": true,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }