voxmenthe commited on
Commit
5bdb30e
1 Parent(s): 3d29aff

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ license: other
5
+ tags:
6
+ - causal-lm
7
+ - mlx
8
+ datasets:
9
+ - HuggingFaceH4/ultrachat_200k
10
+ - allenai/ultrafeedback_binarized_cleaned
11
+ - meta-math/MetaMathQA
12
+ - WizardLM/WizardLM_evol_instruct_V2_196k
13
+ - openchat/openchat_sharegpt4_dataset
14
+ - LDJnr/Capybara
15
+ - Intel/orca_dpo_pairs
16
+ - hkust-nlp/deita-10k-v0
17
+ - Anthropic/hh-rlhf
18
+ - glaiveai/glaive-function-calling-v2
19
+ extra_gated_fields:
20
+ Name: text
21
+ Email: text
22
+ Country: text
23
+ Organization or Affiliation: text
24
+ I ALLOW Stability AI to email me about new model releases: checkbox
25
+ ---
26
+
27
+ # voxmenthe/stablelm-2-12b-chat-mlx-4bit
28
+ This model was converted to MLX format from [`stabilityai/stablelm-2-12b-chat`]() using mlx-lm version **0.8.0**.
29
+ Refer to the [original model card](https://huggingface.co/stabilityai/stablelm-2-12b-chat) for more details on the model.
30
+ ## Use with mlx
31
+
32
+ ```bash
33
+ pip install mlx-lm
34
+ ```
35
+
36
+ ```python
37
+ from mlx_lm import load, generate
38
+
39
+ model, tokenizer = load("voxmenthe/stablelm-2-12b-chat-mlx-4bit")
40
+ response = generate(model, tokenizer, prompt="hello", verbose=True)
41
+ ```
config.json ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_cross_attention": false,
3
+ "architectures": [
4
+ "StableLmForCausalLM"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_stablelm.StableLmConfig",
9
+ "AutoModelForCausalLM": "modeling_stablelm.StableLmForCausalLM"
10
+ },
11
+ "bad_words_ids": null,
12
+ "begin_suppress_tokens": null,
13
+ "bos_token_id": 100257,
14
+ "chunk_size_feed_forward": 0,
15
+ "cross_attention_hidden_size": null,
16
+ "decoder_start_token_id": null,
17
+ "diversity_penalty": 0.0,
18
+ "do_sample": false,
19
+ "early_stopping": false,
20
+ "encoder_no_repeat_ngram_size": 0,
21
+ "eos_token_id": 100257,
22
+ "exponential_decay_length_penalty": null,
23
+ "finetuning_task": null,
24
+ "forced_bos_token_id": null,
25
+ "forced_eos_token_id": null,
26
+ "hidden_act": "silu",
27
+ "hidden_dropout": 0.0,
28
+ "hidden_size": 5120,
29
+ "id2label": {
30
+ "0": "LABEL_0",
31
+ "1": "LABEL_1"
32
+ },
33
+ "initializer_range": 0.01,
34
+ "intermediate_size": 13824,
35
+ "is_decoder": false,
36
+ "is_encoder_decoder": false,
37
+ "label2id": {
38
+ "LABEL_0": 0,
39
+ "LABEL_1": 1
40
+ },
41
+ "layer_norm_eps": 1e-05,
42
+ "length_penalty": 1.0,
43
+ "max_length": 20,
44
+ "max_position_embeddings": 4096,
45
+ "min_length": 0,
46
+ "model_type": "stablelm",
47
+ "no_repeat_ngram_size": 0,
48
+ "num_attention_heads": 32,
49
+ "num_beam_groups": 1,
50
+ "num_beams": 1,
51
+ "num_hidden_layers": 40,
52
+ "num_key_value_heads": 8,
53
+ "num_return_sequences": 1,
54
+ "output_attentions": false,
55
+ "output_hidden_states": false,
56
+ "output_scores": false,
57
+ "pad_token_id": null,
58
+ "partial_rotary_factor": 0.25,
59
+ "prefix": null,
60
+ "problem_type": null,
61
+ "pruned_heads": {},
62
+ "qk_layernorm": true,
63
+ "quantization": {
64
+ "group_size": 64,
65
+ "bits": 4
66
+ },
67
+ "remove_invalid_values": false,
68
+ "repetition_penalty": 1.0,
69
+ "return_dict": true,
70
+ "return_dict_in_generate": false,
71
+ "rope_scaling": null,
72
+ "rope_theta": 10000,
73
+ "rotary_scaling_factor": 1.0,
74
+ "sep_token_id": null,
75
+ "suppress_tokens": null,
76
+ "task_specific_params": null,
77
+ "temperature": 1.0,
78
+ "tf_legacy_loss": false,
79
+ "tie_encoder_decoder": false,
80
+ "tie_word_embeddings": false,
81
+ "tokenizer_class": null,
82
+ "top_k": 50,
83
+ "top_p": 1.0,
84
+ "torch_dtype": "bfloat16",
85
+ "torchscript": false,
86
+ "transformers_version": "4.39.3",
87
+ "typical_p": 1.0,
88
+ "use_bfloat16": false,
89
+ "use_cache": true,
90
+ "use_norm_bias": false,
91
+ "use_parallel_residual": true,
92
+ "use_qkv_bias": false,
93
+ "vocab_size": 100352
94
+ }
configuration_stablelm.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 Stability AI and The HuggingFace Inc. team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """ StableLM model configuration """
15
+
16
+ from transformers.configuration_utils import PretrainedConfig
17
+ from transformers.utils import logging
18
+
19
+
20
+ logger = logging.get_logger(__name__)
21
+
22
+ STABLELM_PRETRAINED_CONFIG_ARCHIVE_MAP = {
23
+ "stabilityai/stablelm-3b-4e1t": "https://huggingface.co/stabilityai/stablelm-3b-4e1t/resolve/main/config.json",
24
+ # See all StableLM models at https://huggingface.co/models?filter=stablelm
25
+ }
26
+
27
+
28
+ class StableLmConfig(PretrainedConfig):
29
+ r"""
30
+ This is the configuration class to store the configuration of a [`~StableLmModel`].
31
+ It is used to instantiate an StableLM model according to the specified arguments, defining the model
32
+ architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of
33
+ the StableLM [stabilityai/stablelm-3b-4e1t](https://huggingface.co/stabilityai/stablelm-3b-4e1t) architecture.
34
+
35
+ Configuration objects inherit from [`PretrainedConfig`] and can be used
36
+ to control the model outputs. Read the documentation from [`PretrainedConfig`]
37
+ for more information.
38
+
39
+
40
+ Args:
41
+ vocab_size (`int`, *optional*, defaults to 50304):
42
+ Vocabulary size of the StableLM model. Defines the number of different tokens that
43
+ can be represented by the `inputs_ids` passed when calling [`StableLmModel`].
44
+ intermediate_size (`int`, *optional*, defaults to 6912):
45
+ Dimension of the MLP representations.
46
+ hidden_size (`int`, *optional*, defaults to 2560):
47
+ Number of hidden layers in the Transformer decoder.
48
+ num_hidden_layers (`int`, *optional*, defaults to 32):
49
+ Number of hidden layers in the Transformer decoder.
50
+ num_attention_heads (`int`, *optional*, defaults to 32):
51
+ Number of attention heads for each attention layer in the Transformer encoder.
52
+ num_key_value_heads (`int`, *optional*, defaults to 32):
53
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
54
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
55
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
56
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
57
+ by meanpooling all the original heads within that group. For more details checkout [this
58
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
59
+ `num_attention_heads`.
60
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
61
+ The non-linear activation function (function or string).
62
+ max_position_embeddings (`int`, *optional*, defaults to 4096):
63
+ The maximum sequence length that this model might ever be used with.
64
+ Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
65
+ initializer_range (`float`, *optional*, defaults to 0.02):
66
+ The standard deviation of the truncated_normal_initializer for initializing
67
+ all weight matrices.
68
+ layer_norm_eps (`float`, *optional*, defaults to 1e-05):
69
+ The epsilon used by the normalization layers.
70
+ use_cache (`bool`, *optional*, defaults to `True`):
71
+ Whether or not the model should return the last key/values attentions
72
+ (not used by all models). Only relevant if `config.is_decoder=True`.
73
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
74
+ Whether the model's input and output word embeddings should be tied.
75
+ rope_theta (`float`, *optional*, defaults to `10000.0`):
76
+ The base period of the RoPE embeddings.
77
+ rope_scaling (`Dict`, *optional*):
78
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
79
+ strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
80
+ `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
81
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
82
+ these scaling strategies behave:
83
+ https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This
84
+ is an experimental feature, subject to breaking API changes in future versions.
85
+ use_qkv_bias (`bool`, *optional*, defaults to `False`):
86
+ Whether or not the model should use bias for qkv layers.
87
+ qk_layernorm (`bool`, *optional*, defaults to `False`):
88
+ Whether or not to normalize, per head, the Queries and Keys after projecting the hidden states.
89
+ use_parallel_residual (`bool`, *optional*, defaults to `False`):
90
+ Whether to use a "parallel" formulation in each Transformer layer, which can provide a slight training
91
+ speedup at large scales.
92
+ hidden_dropout (`float`, *optional*, defaults to 0.0):
93
+ The dropout ratio after applying the MLP to the hidden states.
94
+ attention_dropout (`float`, *optional*, defaults to 0.0):
95
+ The dropout ratio for the attention probabilities.
96
+ partial_rotary_factor (`float`, *optional*, defaults to 0.25):
97
+ Percentage of the query and keys which will have rotary embedding.
98
+ bos_token_id (int, *optional*, defaults to 0):
99
+ The id of the `BOS` token in the vocabulary.
100
+ eos_token_id (int, *optional*, defaults to 0):
101
+ The id of the `EOS` token in the vocabulary.
102
+
103
+ Example:
104
+
105
+ ```python
106
+ >>> from transformers import StableLmModel, StableLmConfig
107
+
108
+ >>> # Initializing a StableLM stablelm-3b style configuration
109
+ >>> configuration = StableLmConfig()
110
+ ```"""
111
+
112
+ model_type = "stablelm"
113
+ keys_to_ignore_at_inference = ["past_key_values"]
114
+
115
+ def __init__(
116
+ self,
117
+ vocab_size=50304,
118
+ intermediate_size=6912,
119
+ hidden_size=2560,
120
+ num_hidden_layers=32,
121
+ num_attention_heads=32,
122
+ num_key_value_heads=32,
123
+ hidden_act="silu",
124
+ max_position_embeddings=4096,
125
+ initializer_range=0.02,
126
+ layer_norm_eps=1.0e-5,
127
+ use_cache=True,
128
+ tie_word_embeddings=False,
129
+ rope_theta=10_000,
130
+ rope_scaling=None,
131
+ use_qkv_bias=False,
132
+ qk_layernorm=False,
133
+ use_parallel_residual=False,
134
+ hidden_dropout=0.0,
135
+ attention_dropout=0.0,
136
+ partial_rotary_factor=0.25,
137
+ bos_token_id=0,
138
+ eos_token_id=0,
139
+ **kwargs,
140
+ ):
141
+ self.vocab_size = vocab_size
142
+ self.max_position_embeddings = max_position_embeddings
143
+
144
+ self.hidden_size = hidden_size
145
+ self.intermediate_size = intermediate_size
146
+ self.num_hidden_layers = num_hidden_layers
147
+ self.num_attention_heads = num_attention_heads
148
+ self.num_key_value_heads = num_key_value_heads
149
+ self.hidden_act = hidden_act
150
+
151
+ self.initializer_range = initializer_range
152
+ self.layer_norm_eps = layer_norm_eps
153
+ self.use_cache = use_cache
154
+ self.rope_theta = rope_theta
155
+ self.rope_scaling = rope_scaling
156
+ self.use_qkv_bias = use_qkv_bias
157
+ self.qk_layernorm = qk_layernorm
158
+ self.use_parallel_residual = use_parallel_residual
159
+ self.hidden_dropout = hidden_dropout
160
+ self.attention_dropout = attention_dropout
161
+ self.partial_rotary_factor = partial_rotary_factor
162
+ self._rope_scaling_validation()
163
+
164
+ super().__init__(
165
+ bos_token_id=bos_token_id,
166
+ eos_token_id=eos_token_id,
167
+ tie_word_embeddings=tie_word_embeddings,
168
+ **kwargs,
169
+ )
170
+
171
+ # Copied from transformers.models.llama.configuration_llama.LlamaConfig._rope_scaling_validation
172
+ def _rope_scaling_validation(self):
173
+ """
174
+ Validate the `rope_scaling` configuration.
175
+ """
176
+ if self.rope_scaling is None:
177
+ return
178
+
179
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
180
+ raise ValueError(
181
+ "`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, "
182
+ f"got {self.rope_scaling}"
183
+ )
184
+ rope_scaling_type = self.rope_scaling.get("type", None)
185
+ rope_scaling_factor = self.rope_scaling.get("factor", None)
186
+ if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
187
+ raise ValueError(
188
+ f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
189
+ )
190
+ if (
191
+ rope_scaling_factor is None
192
+ or not isinstance(rope_scaling_factor, float)
193
+ or rope_scaling_factor <= 1.0
194
+ ):
195
+ raise ValueError(
196
+ f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}"
197
+ )
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:36aaecdbcba991e750aa451742c9f0b1bc1d1b758586abd212292e94595d4016
3
+ size 5365407595
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3a2d2c9948614c9cba9229832d8937008c276977ff7e71c8786679806df3e61d
3
+ size 2204999055
model.safetensors.index.json ADDED
The diff for this file is too large to render. See raw diff
 
modeling_stablelm.py ADDED
@@ -0,0 +1,1608 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 EleutherAI and the HuggingFace Inc. team. All rights reserved.
2
+ #
3
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
4
+ # and OPT implementations in this library. It has been modified from its
5
+ # original forms to accommodate minor architectural differences compared
6
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ """ PyTorch StableLM model."""
20
+ import math
21
+ from typing import List, Optional, Tuple, Union
22
+
23
+ import torch
24
+ import torch.nn.functional as F
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.cache_utils import Cache, DynamicCache
31
+ from transformers.modeling_attn_mask_utils import (
32
+ _prepare_4d_causal_attention_mask,
33
+ _prepare_4d_causal_attention_mask_for_sdpa,
34
+ )
35
+ from transformers.modeling_outputs import (
36
+ BaseModelOutputWithPast,
37
+ CausalLMOutputWithPast,
38
+ SequenceClassifierOutputWithPast,
39
+ )
40
+ from transformers.modeling_utils import PreTrainedModel
41
+ from transformers.utils import (
42
+ add_start_docstrings,
43
+ add_start_docstrings_to_model_forward,
44
+ is_flash_attn_2_available,
45
+ is_flash_attn_greater_or_equal_2_10,
46
+ logging,
47
+ replace_return_docstrings,
48
+ )
49
+ from .configuration_stablelm import StableLmConfig
50
+
51
+
52
+ try:
53
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
54
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
55
+ except:
56
+ flash_attn_func, flash_attn_varlen_func = None, None
57
+ index_first_axis, pad_input, unpad_input = None, None, None
58
+
59
+
60
+ logger = logging.get_logger(__name__)
61
+
62
+ _CONFIG_FOR_DOC = "StableLmConfig"
63
+
64
+
65
+ # Copied from transformers.models.llama.modeling_llama._get_unpad_data
66
+ def _get_unpad_data(attention_mask):
67
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
68
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
69
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
70
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
71
+ return (
72
+ indices,
73
+ cu_seqlens,
74
+ max_seqlen_in_batch,
75
+ )
76
+
77
+
78
+ # Copied from transformers.models.mistral.modeling_mistral.MistralRotaryEmbedding with Mistral->StableLm
79
+ class StableLmRotaryEmbedding(nn.Module):
80
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
81
+ super().__init__()
82
+
83
+ self.dim = dim
84
+ self.max_position_embeddings = max_position_embeddings
85
+ self.base = base
86
+ inv_freq = 1.0 / (
87
+ self.base
88
+ ** (
89
+ torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device)
90
+ / self.dim
91
+ )
92
+ )
93
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
94
+
95
+ # Build here to make `torch.jit.trace` work.
96
+ self._set_cos_sin_cache(
97
+ seq_len=max_position_embeddings,
98
+ device=self.inv_freq.device,
99
+ dtype=torch.get_default_dtype(),
100
+ )
101
+
102
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
103
+ self.max_seq_len_cached = seq_len
104
+ t = torch.arange(
105
+ self.max_seq_len_cached, device=device, dtype=torch.int64
106
+ ).type_as(self.inv_freq)
107
+
108
+ freqs = torch.outer(t, self.inv_freq)
109
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
110
+ emb = torch.cat((freqs, freqs), dim=-1)
111
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
112
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
113
+
114
+ def forward(self, x, seq_len=None):
115
+ # x: [bs, num_attention_heads, seq_len, head_size]
116
+ if seq_len > self.max_seq_len_cached:
117
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
118
+
119
+ return (
120
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
121
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
122
+ )
123
+
124
+
125
+ # Copied from transformers.models.falcon.modeling_falcon.FalconLinearScalingRotaryEmbedding with Falcon->StableLm
126
+ class StableLmLinearScalingRotaryEmbedding(StableLmRotaryEmbedding):
127
+ """StableLmRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
128
+
129
+ def __init__(
130
+ self,
131
+ dim,
132
+ max_position_embeddings=2048,
133
+ base=10000,
134
+ device=None,
135
+ scaling_factor=1.0,
136
+ ):
137
+ self.scaling_factor = scaling_factor
138
+ super().__init__(dim, max_position_embeddings, base, device)
139
+
140
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
141
+ self.max_seq_len_cached = seq_len
142
+ t = torch.arange(
143
+ self.max_seq_len_cached, device=device, dtype=torch.int64
144
+ ).type_as(self.inv_freq)
145
+ t = t / self.scaling_factor
146
+
147
+ freqs = torch.outer(t, self.inv_freq)
148
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
149
+ emb = torch.cat((freqs, freqs), dim=-1)
150
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
151
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
152
+
153
+
154
+ # Copied from transformers.models.falcon.modeling_falcon.FalconDynamicNTKScalingRotaryEmbedding with Falcon->StableLm
155
+ class StableLmDynamicNTKScalingRotaryEmbedding(StableLmRotaryEmbedding):
156
+ """StableLmRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
157
+
158
+ def __init__(
159
+ self,
160
+ dim,
161
+ max_position_embeddings=2048,
162
+ base=10000,
163
+ device=None,
164
+ scaling_factor=1.0,
165
+ ):
166
+ self.scaling_factor = scaling_factor
167
+ super().__init__(dim, max_position_embeddings, base, device)
168
+
169
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
170
+ self.max_seq_len_cached = seq_len
171
+
172
+ if seq_len > self.max_position_embeddings:
173
+ base = self.base * (
174
+ (self.scaling_factor * seq_len / self.max_position_embeddings)
175
+ - (self.scaling_factor - 1)
176
+ ) ** (self.dim / (self.dim - 2))
177
+ inv_freq = 1.0 / (
178
+ base
179
+ ** (
180
+ torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device)
181
+ / self.dim
182
+ )
183
+ )
184
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
185
+
186
+ t = torch.arange(
187
+ self.max_seq_len_cached, device=device, dtype=torch.int64
188
+ ).type_as(self.inv_freq)
189
+
190
+ freqs = torch.outer(t, self.inv_freq)
191
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
192
+ emb = torch.cat((freqs, freqs), dim=-1)
193
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
194
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
195
+
196
+
197
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
198
+ def rotate_half(x):
199
+ """Rotates half the hidden dims of the input."""
200
+ x1 = x[..., : x.shape[-1] // 2]
201
+ x2 = x[..., x.shape[-1] // 2 :]
202
+ return torch.cat((-x2, x1), dim=-1)
203
+
204
+
205
+ # Copied from transformers.models.mistral.modeling_mistral.apply_rotary_pos_emb
206
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
207
+ """Applies Rotary Position Embedding to the query and key tensors.
208
+
209
+ Args:
210
+ q (`torch.Tensor`): The query tensor.
211
+ k (`torch.Tensor`): The key tensor.
212
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
213
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
214
+ position_ids (`torch.Tensor`):
215
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
216
+ used to pass offsetted position ids when working with a KV-cache.
217
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
218
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
219
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
220
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
221
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
222
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
223
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
224
+ Returns:
225
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
226
+ """
227
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
228
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
229
+ q_embed = (q * cos) + (rotate_half(q) * sin)
230
+ k_embed = (k * cos) + (rotate_half(k) * sin)
231
+ return q_embed, k_embed
232
+
233
+
234
+ # Copied from transformers.models.mistral.modeling_mistral.MistralMLP with Mistral->StableLm
235
+ class StableLmMLP(nn.Module):
236
+ def __init__(self, config):
237
+ super().__init__()
238
+ self.config = config
239
+ self.hidden_size = config.hidden_size
240
+ self.intermediate_size = config.intermediate_size
241
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
242
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
243
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
244
+ self.act_fn = ACT2FN[config.hidden_act]
245
+
246
+ def forward(self, x):
247
+ return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
248
+
249
+
250
+ class StableLmLayerNormPerHead(nn.Module):
251
+ def __init__(self, dim, num_heads, eps=1e-5, bias=False):
252
+ super().__init__()
253
+ self.dim = dim
254
+ self.num_heads = num_heads
255
+ self.norms = nn.ModuleList(
256
+ [nn.LayerNorm(dim, eps=eps, bias=bias) for _ in range(self.num_heads)]
257
+ )
258
+
259
+ def forward(self, hidden_states: torch.Tensor):
260
+ # Split along the num_heads axis to get per-head inputs
261
+ # [batch_size, num_heads, seq_len, head_dim] -> [batch_size, 1, seq_len, head_dim] * num_heads
262
+ states_per_heads = torch.split(hidden_states, 1, dim=1)
263
+ # Normalize and merge the heads back together
264
+ return torch.cat(
265
+ [
266
+ norm(hidden_states)
267
+ for norm, hidden_states in zip(self.norms, states_per_heads)
268
+ ],
269
+ dim=1,
270
+ )
271
+
272
+
273
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv
274
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
275
+ """
276
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
277
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
278
+ """
279
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
280
+ if n_rep == 1:
281
+ return hidden_states
282
+ hidden_states = hidden_states[:, :, None, :, :].expand(
283
+ batch, num_key_value_heads, n_rep, slen, head_dim
284
+ )
285
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
286
+
287
+
288
+ class StableLmAttention(nn.Module):
289
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
290
+
291
+ def __init__(self, config: StableLmConfig, layer_idx: Optional[int] = None):
292
+ super().__init__()
293
+ self.config = config
294
+ self.layer_idx = layer_idx
295
+ if layer_idx is None:
296
+ logger.warning_once(
297
+ f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will "
298
+ "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` "
299
+ "when creating this class."
300
+ )
301
+
302
+ self.hidden_size = config.hidden_size
303
+ self.num_heads = config.num_attention_heads
304
+ self.head_dim = self.hidden_size // self.num_heads
305
+ self.num_key_value_heads = config.num_key_value_heads
306
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
307
+ self.max_position_embeddings = config.max_position_embeddings
308
+ self.rope_theta = config.rope_theta
309
+ self.partial_rotary_factor = config.partial_rotary_factor
310
+ self.is_causal = True
311
+
312
+ if (self.head_dim * self.num_heads) != self.hidden_size:
313
+ raise ValueError(
314
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
315
+ f" and `num_heads`: {self.num_heads})."
316
+ )
317
+ self.q_proj = nn.Linear(
318
+ self.hidden_size, self.num_heads * self.head_dim, bias=config.use_qkv_bias
319
+ )
320
+ self.k_proj = nn.Linear(
321
+ self.hidden_size,
322
+ self.num_key_value_heads * self.head_dim,
323
+ bias=config.use_qkv_bias,
324
+ )
325
+ self.v_proj = nn.Linear(
326
+ self.hidden_size,
327
+ self.num_key_value_heads * self.head_dim,
328
+ bias=config.use_qkv_bias,
329
+ )
330
+ self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
331
+
332
+ self.qk_layernorm = config.qk_layernorm
333
+ if self.qk_layernorm:
334
+ self.q_layernorm = StableLmLayerNormPerHead(
335
+ self.head_dim, self.num_heads, eps=config.layer_norm_eps
336
+ )
337
+ self.k_layernorm = StableLmLayerNormPerHead(
338
+ self.head_dim, self.num_key_value_heads, eps=config.layer_norm_eps
339
+ )
340
+
341
+ self.attention_dropout = nn.Dropout(config.attention_dropout)
342
+ self._init_rope()
343
+
344
+ # Copied from transformers.models.persimmon.modeling_persimmon.PersimmonAttention._init_rope with Persimmon->StableLm
345
+ def _init_rope(self):
346
+ if self.config.rope_scaling is None:
347
+ self.rotary_emb = StableLmRotaryEmbedding(
348
+ int(self.partial_rotary_factor * self.head_dim),
349
+ max_position_embeddings=self.max_position_embeddings,
350
+ base=self.rope_theta,
351
+ )
352
+ else:
353
+ scaling_type = self.config.rope_scaling["type"]
354
+ scaling_factor = self.config.rope_scaling["factor"]
355
+ if scaling_type == "linear":
356
+ self.rotary_emb = StableLmLinearScalingRotaryEmbedding(
357
+ int(self.partial_rotary_factor * self.head_dim),
358
+ max_position_embeddings=self.max_position_embeddings,
359
+ scaling_factor=scaling_factor,
360
+ base=self.rope_theta,
361
+ )
362
+ elif scaling_type == "dynamic":
363
+ self.rotary_emb = StableLmDynamicNTKScalingRotaryEmbedding(
364
+ int(self.partial_rotary_factor * self.head_dim),
365
+ max_position_embeddings=self.max_position_embeddings,
366
+ scaling_factor=scaling_factor,
367
+ base=self.rope_theta,
368
+ )
369
+ else:
370
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
371
+
372
+ def forward(
373
+ self,
374
+ hidden_states: torch.Tensor,
375
+ attention_mask: Optional[torch.Tensor] = None,
376
+ position_ids: Optional[torch.LongTensor] = None,
377
+ past_key_value: Optional[Cache] = None,
378
+ output_attentions: bool = False,
379
+ use_cache: bool = False,
380
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
381
+ bsz, q_len, _ = hidden_states.size()
382
+
383
+ query_states = self.q_proj(hidden_states)
384
+ key_states = self.k_proj(hidden_states)
385
+ value_states = self.v_proj(hidden_states)
386
+
387
+ query_states = query_states.view(
388
+ bsz, q_len, self.num_heads, self.head_dim
389
+ ).transpose(1, 2)
390
+ key_states = key_states.view(
391
+ bsz, q_len, self.num_key_value_heads, self.head_dim
392
+ ).transpose(1, 2)
393
+ value_states = value_states.view(
394
+ bsz, q_len, self.num_key_value_heads, self.head_dim
395
+ ).transpose(1, 2)
396
+
397
+ if self.qk_layernorm:
398
+ query_states = self.q_layernorm(query_states)
399
+ key_states = self.k_layernorm(key_states)
400
+
401
+ kv_seq_len = key_states.shape[-2]
402
+ if past_key_value is not None:
403
+ if self.layer_idx is None:
404
+ raise ValueError(
405
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
406
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
407
+ "with a layer index."
408
+ )
409
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
410
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
411
+
412
+ # Partial rotary embedding
413
+ query_rot, query_pass = (
414
+ query_states[..., : self.rotary_emb.dim],
415
+ query_states[..., self.rotary_emb.dim :],
416
+ )
417
+ key_rot, key_pass = (
418
+ key_states[..., : self.rotary_emb.dim],
419
+ key_states[..., self.rotary_emb.dim :],
420
+ )
421
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
422
+ query_rot, key_rot = apply_rotary_pos_emb(
423
+ query_rot, key_rot, cos, sin, position_ids
424
+ )
425
+
426
+ # [batch_size, seq_length, num_heads, head_dim]
427
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
428
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
429
+
430
+ if past_key_value is not None:
431
+ # Specific to RoPE models with partial rotation
432
+ cache_kwargs = {
433
+ "sin": sin,
434
+ "cos": cos,
435
+ "partial_rotation_size": self.rotary_emb.dim,
436
+ }
437
+ key_states, value_states = past_key_value.update(
438
+ key_states, value_states, self.layer_idx, cache_kwargs
439
+ )
440
+
441
+ # Repeat k/v heads if n_kv_heads < n_heads
442
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
443
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
444
+
445
+ attn_weights = torch.matmul(
446
+ query_states, key_states.transpose(2, 3)
447
+ ) / math.sqrt(self.head_dim)
448
+
449
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
450
+ raise ValueError(
451
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
452
+ f" {attn_weights.size()}"
453
+ )
454
+
455
+ if attention_mask is not None:
456
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
457
+ raise ValueError(
458
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
459
+ )
460
+ attn_weights = attn_weights + attention_mask
461
+
462
+ # upcast attention to fp32
463
+ attn_weights = nn.functional.softmax(
464
+ attn_weights, dtype=torch.float32, dim=-1
465
+ ).to(query_states.dtype)
466
+ attn_weights = self.attention_dropout(attn_weights)
467
+
468
+ attn_output = torch.matmul(attn_weights, value_states)
469
+
470
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
471
+ raise ValueError(
472
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
473
+ f" {attn_output.size()}"
474
+ )
475
+
476
+ attn_output = attn_output.transpose(1, 2).contiguous()
477
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
478
+
479
+ attn_output = self.o_proj(attn_output)
480
+
481
+ if not output_attentions:
482
+ attn_weights = None
483
+
484
+ return attn_output, attn_weights, past_key_value
485
+
486
+
487
+ class StableLmSdpaAttention(StableLmAttention):
488
+ def forward(
489
+ self,
490
+ hidden_states: torch.Tensor,
491
+ attention_mask: Optional[torch.Tensor] = None,
492
+ position_ids: Optional[torch.LongTensor] = None,
493
+ past_key_value: Optional[Cache] = None,
494
+ output_attentions: bool = False,
495
+ use_cache: bool = False,
496
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
497
+ if output_attentions:
498
+ # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
499
+ logger.warning_once(
500
+ "StableLmModel is using StableLmSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
501
+ 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
502
+ )
503
+ return super().forward(
504
+ hidden_states=hidden_states,
505
+ attention_mask=attention_mask,
506
+ position_ids=position_ids,
507
+ past_key_value=past_key_value,
508
+ output_attentions=output_attentions,
509
+ use_cache=use_cache,
510
+ )
511
+
512
+ bsz, q_len, _ = hidden_states.size()
513
+
514
+ query_states = self.q_proj(hidden_states)
515
+ key_states = self.k_proj(hidden_states)
516
+ value_states = self.v_proj(hidden_states)
517
+
518
+ query_states = query_states.view(
519
+ bsz, q_len, self.num_heads, self.head_dim
520
+ ).transpose(1, 2)
521
+ key_states = key_states.view(
522
+ bsz, q_len, self.num_key_value_heads, self.head_dim
523
+ ).transpose(1, 2)
524
+ value_states = value_states.view(
525
+ bsz, q_len, self.num_key_value_heads, self.head_dim
526
+ ).transpose(1, 2)
527
+
528
+ if self.qk_layernorm:
529
+ query_states = self.q_layernorm(query_states)
530
+ key_states = self.k_layernorm(key_states)
531
+
532
+ kv_seq_len = key_states.shape[-2]
533
+ if past_key_value is not None:
534
+ if self.layer_idx is None:
535
+ raise ValueError(
536
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
537
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
538
+ "with a layer index."
539
+ )
540
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
541
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
542
+
543
+ # Partial rotary embedding
544
+ query_rot, query_pass = (
545
+ query_states[..., : self.rotary_emb.dim],
546
+ query_states[..., self.rotary_emb.dim :],
547
+ )
548
+ key_rot, key_pass = (
549
+ key_states[..., : self.rotary_emb.dim],
550
+ key_states[..., self.rotary_emb.dim :],
551
+ )
552
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
553
+ query_rot, key_rot = apply_rotary_pos_emb(
554
+ query_rot, key_rot, cos, sin, position_ids
555
+ )
556
+
557
+ # [batch_size, seq_length, num_heads, head_dim]
558
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
559
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
560
+
561
+ if past_key_value is not None:
562
+ # Specific to RoPE models with partial rotation
563
+ cache_kwargs = {
564
+ "sin": sin,
565
+ "cos": cos,
566
+ "partial_rotation_size": self.rotary_emb.dim,
567
+ }
568
+ key_states, value_states = past_key_value.update(
569
+ key_states, value_states, self.layer_idx, cache_kwargs
570
+ )
571
+
572
+ # Repeat k/v heads if n_kv_heads < n_heads
573
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
574
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
575
+
576
+ # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
577
+ # Reference: https://github.com/pytorch/pytorch/issues/112577.
578
+ if query_states.device.type == "cuda" and attention_mask is not None:
579
+ query_states = query_states.contiguous()
580
+ key_states = key_states.contiguous()
581
+ value_states = value_states.contiguous()
582
+
583
+ attn_output = torch.nn.functional.scaled_dot_product_attention(
584
+ query_states,
585
+ key_states,
586
+ value_states,
587
+ attn_mask=attention_mask,
588
+ dropout_p=self.attention_dropout.p if self.training else 0.0,
589
+ # The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
590
+ is_causal=self.is_causal and attention_mask is None and q_len > 1,
591
+ )
592
+
593
+ attn_output = attn_output.transpose(1, 2).contiguous()
594
+ attn_output = attn_output.view(bsz, q_len, self.hidden_size)
595
+
596
+ attn_output = self.o_proj(attn_output)
597
+
598
+ return attn_output, None, past_key_value
599
+
600
+
601
+ class StableLmFlashAttention2(StableLmAttention):
602
+ """
603
+ StableLM flash attention module. This module inherits from `StableLmAttention` as the weights of the module stays
604
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
605
+ flash attention and deal with padding tokens in case the input contains any of them.
606
+ """
607
+
608
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
609
+ def __init__(self, *args, **kwargs):
610
+ super().__init__(*args, **kwargs)
611
+
612
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
613
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignment, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
614
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
615
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
616
+
617
+ def forward(
618
+ self,
619
+ hidden_states: torch.Tensor,
620
+ attention_mask: Optional[torch.LongTensor] = None,
621
+ position_ids: Optional[torch.LongTensor] = None,
622
+ past_key_value: Optional[Cache] = None,
623
+ output_attentions: bool = False,
624
+ use_cache: bool = False,
625
+ **kwargs,
626
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
627
+ # StableLmFlashAttention2 attention does not support output_attentions
628
+
629
+ output_attentions = False
630
+
631
+ bsz, q_len, _ = hidden_states.size()
632
+
633
+ query_states = self.q_proj(hidden_states)
634
+ key_states = self.k_proj(hidden_states)
635
+ value_states = self.v_proj(hidden_states)
636
+
637
+ # Flash attention requires the input to have the shape
638
+ # batch_size x seq_length x head_dim x hidden_dim
639
+ # therefore we just need to keep the original shape
640
+ query_states = query_states.view(
641
+ bsz, q_len, self.num_heads, self.head_dim
642
+ ).transpose(1, 2)
643
+ key_states = key_states.view(
644
+ bsz, q_len, self.num_key_value_heads, self.head_dim
645
+ ).transpose(1, 2)
646
+ value_states = value_states.view(
647
+ bsz, q_len, self.num_key_value_heads, self.head_dim
648
+ ).transpose(1, 2)
649
+
650
+ if self.qk_layernorm:
651
+ query_states = self.q_layernorm(query_states)
652
+ key_states = self.k_layernorm(key_states)
653
+
654
+ kv_seq_len = key_states.shape[-2]
655
+ if past_key_value is not None:
656
+ if self.layer_idx is None:
657
+ raise ValueError(
658
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
659
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
660
+ "with a layer index."
661
+ )
662
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
663
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
664
+
665
+ # Partial rotary embedding
666
+ query_rot, query_pass = (
667
+ query_states[..., : self.rotary_emb.dim],
668
+ query_states[..., self.rotary_emb.dim :],
669
+ )
670
+ key_rot, key_pass = (
671
+ key_states[..., : self.rotary_emb.dim],
672
+ key_states[..., self.rotary_emb.dim :],
673
+ )
674
+ query_rot, key_rot = apply_rotary_pos_emb(
675
+ query_rot, key_rot, cos, sin, position_ids
676
+ )
677
+
678
+ # [batch_size, seq_length, num_heads, head_dim]
679
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
680
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
681
+
682
+ if past_key_value is not None:
683
+ cache_kwargs = {
684
+ "sin": sin,
685
+ "cos": cos,
686
+ "partial_rotation_size": self.rotary_emb.dim,
687
+ }
688
+ key_states, value_states = past_key_value.update(
689
+ key_states, value_states, self.layer_idx, cache_kwargs
690
+ )
691
+
692
+ # TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache
693
+ # to be able to avoid many of these transpose/reshape/view.
694
+ query_states = query_states.transpose(1, 2)
695
+ key_states = key_states.transpose(1, 2)
696
+ value_states = value_states.transpose(1, 2)
697
+
698
+ dropout_rate = self.attention_dropout.p if self.training else 0.0
699
+
700
+ attn_output = self._flash_attention_forward(
701
+ query_states,
702
+ key_states,
703
+ value_states,
704
+ attention_mask,
705
+ q_len,
706
+ dropout=dropout_rate,
707
+ )
708
+
709
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
710
+ attn_output = self.o_proj(attn_output)
711
+
712
+ if not output_attentions:
713
+ attn_weights = None
714
+
715
+ return attn_output, attn_weights, past_key_value
716
+
717
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._flash_attention_forward
718
+ def _flash_attention_forward(
719
+ self,
720
+ query_states,
721
+ key_states,
722
+ value_states,
723
+ attention_mask,
724
+ query_length,
725
+ dropout=0.0,
726
+ softmax_scale=None,
727
+ ):
728
+ """
729
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
730
+ first unpad the input, then computes the attention scores and pad the final attention scores.
731
+
732
+ Args:
733
+ query_states (`torch.Tensor`):
734
+ Input query states to be passed to Flash Attention API
735
+ key_states (`torch.Tensor`):
736
+ Input key states to be passed to Flash Attention API
737
+ value_states (`torch.Tensor`):
738
+ Input value states to be passed to Flash Attention API
739
+ attention_mask (`torch.Tensor`):
740
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
741
+ position of padding tokens and 1 for the position of non-padding tokens.
742
+ dropout (`float`):
743
+ Attention dropout
744
+ softmax_scale (`float`, *optional*):
745
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
746
+ """
747
+ if not self._flash_attn_uses_top_left_mask:
748
+ causal = self.is_causal
749
+ else:
750
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in LlamaFlashAttention2 __init__.
751
+ causal = self.is_causal and query_length != 1
752
+
753
+ # Contains at least one padding token in the sequence
754
+ if attention_mask is not None:
755
+ batch_size = query_states.shape[0]
756
+ (
757
+ query_states,
758
+ key_states,
759
+ value_states,
760
+ indices_q,
761
+ cu_seq_lens,
762
+ max_seq_lens,
763
+ ) = self._upad_input(
764
+ query_states, key_states, value_states, attention_mask, query_length
765
+ )
766
+
767
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
768
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
769
+
770
+ attn_output_unpad = flash_attn_varlen_func(
771
+ query_states,
772
+ key_states,
773
+ value_states,
774
+ cu_seqlens_q=cu_seqlens_q,
775
+ cu_seqlens_k=cu_seqlens_k,
776
+ max_seqlen_q=max_seqlen_in_batch_q,
777
+ max_seqlen_k=max_seqlen_in_batch_k,
778
+ dropout_p=dropout,
779
+ softmax_scale=softmax_scale,
780
+ causal=causal,
781
+ )
782
+
783
+ attn_output = pad_input(
784
+ attn_output_unpad, indices_q, batch_size, query_length
785
+ )
786
+ else:
787
+ attn_output = flash_attn_func(
788
+ query_states,
789
+ key_states,
790
+ value_states,
791
+ dropout,
792
+ softmax_scale=softmax_scale,
793
+ causal=causal,
794
+ )
795
+
796
+ return attn_output
797
+
798
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._upad_input
799
+ def _upad_input(
800
+ self, query_layer, key_layer, value_layer, attention_mask, query_length
801
+ ):
802
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
803
+ batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
804
+
805
+ key_layer = index_first_axis(
806
+ key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
807
+ indices_k,
808
+ )
809
+ value_layer = index_first_axis(
810
+ value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim),
811
+ indices_k,
812
+ )
813
+ if query_length == kv_seq_len:
814
+ query_layer = index_first_axis(
815
+ query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim),
816
+ indices_k,
817
+ )
818
+ cu_seqlens_q = cu_seqlens_k
819
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
820
+ indices_q = indices_k
821
+ elif query_length == 1:
822
+ max_seqlen_in_batch_q = 1
823
+ cu_seqlens_q = torch.arange(
824
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
825
+ ) # There is a memcpy here, that is very bad.
826
+ indices_q = cu_seqlens_q[:-1]
827
+ query_layer = query_layer.squeeze(1)
828
+ else:
829
+ # The -q_len: slice assumes left padding.
830
+ attention_mask = attention_mask[:, -query_length:]
831
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(
832
+ query_layer, attention_mask
833
+ )
834
+
835
+ return (
836
+ query_layer,
837
+ key_layer,
838
+ value_layer,
839
+ indices_q,
840
+ (cu_seqlens_q, cu_seqlens_k),
841
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
842
+ )
843
+
844
+
845
+ ATTENTION_CLASSES = {
846
+ "eager": StableLmAttention,
847
+ "sdpa": StableLmSdpaAttention,
848
+ "flash_attention_2": StableLmFlashAttention2,
849
+ }
850
+
851
+
852
+ class StableLmDecoderLayer(nn.Module):
853
+ def __init__(self, config: StableLmConfig, layer_idx: int):
854
+ super().__init__()
855
+ self.use_parallel_residual = config.use_parallel_residual
856
+ self.hidden_size = config.hidden_size
857
+ self.self_attn = ATTENTION_CLASSES[config._attn_implementation](
858
+ config, layer_idx=layer_idx
859
+ )
860
+ self.mlp = StableLmMLP(config)
861
+ self.input_layernorm = nn.LayerNorm(
862
+ config.hidden_size, eps=config.layer_norm_eps
863
+ )
864
+ self.post_attention_layernorm = None
865
+ if not self.use_parallel_residual:
866
+ self.post_attention_layernorm = nn.LayerNorm(
867
+ config.hidden_size, eps=config.layer_norm_eps
868
+ )
869
+ self.dropout = nn.Dropout(config.hidden_dropout)
870
+
871
+ def forward(
872
+ self,
873
+ hidden_states: torch.Tensor,
874
+ attention_mask: Optional[torch.Tensor] = None,
875
+ position_ids: Optional[torch.LongTensor] = None,
876
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
877
+ output_attentions: Optional[bool] = False,
878
+ use_cache: Optional[bool] = False,
879
+ ) -> Tuple[
880
+ torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]
881
+ ]:
882
+ """
883
+ Args:
884
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
885
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
886
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
887
+ position_ids (`torch.LongTensor` of shape `({0})`, *optional*):
888
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range
889
+ `[0, config.n_positions - 1]`.
890
+
891
+ [What are position IDs?](../glossary#position-ids)
892
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*):
893
+ cached past key and value projection states
894
+ output_attentions (`bool`, *optional*):
895
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
896
+ returned tensors for more detail.
897
+ use_cache (`bool`, *optional*):
898
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
899
+ (see `past_key_values`).
900
+ """
901
+
902
+ residual = hidden_states
903
+
904
+ hidden_states = self.input_layernorm(hidden_states)
905
+
906
+ # Self Attention
907
+ self_attn_output, self_attn_weights, present_key_value = self.self_attn(
908
+ hidden_states=hidden_states,
909
+ attention_mask=attention_mask,
910
+ position_ids=position_ids,
911
+ past_key_value=past_key_value,
912
+ output_attentions=output_attentions,
913
+ use_cache=use_cache,
914
+ )
915
+
916
+ # copied from transformers.models.gpt_neox.modeling_gpt_neox.GPTNeoXLayer.forward
917
+ if self.use_parallel_residual:
918
+ # x = x + attn(ln1(x)) + mlp(ln1(x))
919
+ # Fully Connected
920
+ mlp_output = self.mlp(hidden_states)
921
+ mlp_output = self.dropout(mlp_output)
922
+ hidden_states = residual + self_attn_output + mlp_output
923
+ else:
924
+ # x = x + attn(ln1(x))
925
+ # x = x + mlp(ln2(x))
926
+ residual = residual + self_attn_output
927
+ # Fully Connected
928
+ mlp_output = self.mlp(self.post_attention_layernorm(residual))
929
+ mlp_output = self.dropout(mlp_output)
930
+ hidden_states = residual + mlp_output
931
+
932
+ outputs = (hidden_states,)
933
+
934
+ if output_attentions:
935
+ outputs += (self_attn_weights,)
936
+
937
+ if use_cache:
938
+ outputs += (present_key_value,)
939
+
940
+ return outputs
941
+
942
+
943
+ STABLELM_START_DOCSTRING = r"""
944
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
945
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
946
+ etc.)
947
+
948
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
949
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
950
+ and behavior.
951
+
952
+ Parameters:
953
+ config ([`StableLmConfig`]):
954
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
955
+ load the weights associated with the model, only the configuration. Check out the
956
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
957
+ """
958
+
959
+
960
+ @add_start_docstrings(
961
+ "The bare StableLm Model outputting raw hidden-states without any specific head on top.",
962
+ STABLELM_START_DOCSTRING,
963
+ )
964
+ class StableLmPreTrainedModel(PreTrainedModel):
965
+ config_class = StableLmConfig
966
+ base_model_prefix = "model"
967
+ supports_gradient_checkpointing = True
968
+ _no_split_modules = ["StableLmDecoderLayer"]
969
+ _skip_keys_device_placement = "past_key_values"
970
+ _supports_flash_attn_2 = True
971
+ _supports_cache_class = True
972
+ _supports_sdpa = True
973
+
974
+ def _init_weights(self, module):
975
+ std = self.config.initializer_range
976
+ if isinstance(module, nn.Linear):
977
+ module.weight.data.normal_(mean=0.0, std=std)
978
+ if module.bias is not None:
979
+ module.bias.data.zero_()
980
+ elif isinstance(module, nn.Embedding):
981
+ module.weight.data.normal_(mean=0.0, std=std)
982
+ if module.padding_idx is not None:
983
+ module.weight.data[module.padding_idx].zero_()
984
+
985
+
986
+ STABLELM_INPUTS_DOCSTRING = r"""
987
+ Args:
988
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
989
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
990
+ it.
991
+
992
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
993
+ [`PreTrainedTokenizer.__call__`] for details.
994
+
995
+ [What are input IDs?](../glossary#input-ids)
996
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
997
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
998
+
999
+ - 1 for tokens that are **not masked**,
1000
+ - 0 for tokens that are **masked**.
1001
+
1002
+ [What are attention masks?](../glossary#attention-mask)
1003
+
1004
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1005
+ [`PreTrainedTokenizer.__call__`] for details.
1006
+
1007
+ If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
1008
+ `past_key_values`).
1009
+
1010
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
1011
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
1012
+ information on the default strategy.
1013
+
1014
+ - 1 indicates the head is **not masked**,
1015
+ - 0 indicates the head is **masked**.
1016
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1017
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
1018
+ config.n_positions - 1]`.
1019
+
1020
+ [What are position IDs?](../glossary#position-ids)
1021
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
1022
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
1023
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
1024
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
1025
+
1026
+ Two formats are allowed:
1027
+ - a [`~cache_utils.Cache`] instance;
1028
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
1029
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
1030
+ cache format.
1031
+
1032
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
1033
+ legacy cache format will be returned.
1034
+
1035
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
1036
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
1037
+ of shape `(batch_size, sequence_length)`.
1038
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
1039
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
1040
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
1041
+ model's internal embedding lookup matrix.
1042
+ use_cache (`bool`, *optional*):
1043
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
1044
+ `past_key_values`).
1045
+ output_attentions (`bool`, *optional*):
1046
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
1047
+ tensors for more detail.
1048
+ output_hidden_states (`bool`, *optional*):
1049
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
1050
+ more detail.
1051
+ return_dict (`bool`, *optional*):
1052
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
1053
+ """
1054
+
1055
+
1056
+ @add_start_docstrings(
1057
+ "The bare StableLm Model outputting raw hidden-states without any specific head on top.",
1058
+ STABLELM_START_DOCSTRING,
1059
+ )
1060
+ class StableLmModel(StableLmPreTrainedModel):
1061
+ """
1062
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`StableLmDecoderLayer`]
1063
+
1064
+ Args:
1065
+ config: StableLmConfig
1066
+ """
1067
+
1068
+ def __init__(self, config: StableLmConfig):
1069
+ super().__init__(config)
1070
+ self.padding_idx = config.pad_token_id
1071
+ self.vocab_size = config.vocab_size
1072
+
1073
+ self.embed_tokens = nn.Embedding(
1074
+ config.vocab_size, config.hidden_size, self.padding_idx
1075
+ )
1076
+ self.layers = nn.ModuleList(
1077
+ [
1078
+ StableLmDecoderLayer(config, layer_idx)
1079
+ for layer_idx in range(config.num_hidden_layers)
1080
+ ]
1081
+ )
1082
+ self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
1083
+
1084
+ self._attn_implementation = config._attn_implementation
1085
+ self.gradient_checkpointing = False
1086
+ # Initialize weights and apply final processing
1087
+ self.post_init()
1088
+
1089
+ def get_input_embeddings(self):
1090
+ return self.embed_tokens
1091
+
1092
+ def set_input_embeddings(self, value):
1093
+ self.embed_tokens = value
1094
+
1095
+ @add_start_docstrings_to_model_forward(STABLELM_INPUTS_DOCSTRING)
1096
+ def forward(
1097
+ self,
1098
+ input_ids: torch.LongTensor = None,
1099
+ attention_mask: Optional[torch.Tensor] = None,
1100
+ position_ids: Optional[torch.LongTensor] = None,
1101
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1102
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1103
+ use_cache: Optional[bool] = None,
1104
+ output_attentions: Optional[bool] = None,
1105
+ output_hidden_states: Optional[bool] = None,
1106
+ return_dict: Optional[bool] = None,
1107
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
1108
+ output_attentions = (
1109
+ output_attentions
1110
+ if output_attentions is not None
1111
+ else self.config.output_attentions
1112
+ )
1113
+ output_hidden_states = (
1114
+ output_hidden_states
1115
+ if output_hidden_states is not None
1116
+ else self.config.output_hidden_states
1117
+ )
1118
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
1119
+
1120
+ return_dict = (
1121
+ return_dict if return_dict is not None else self.config.use_return_dict
1122
+ )
1123
+
1124
+ # retrieve input_ids and inputs_embeds
1125
+ if input_ids is not None and inputs_embeds is not None:
1126
+ raise ValueError(
1127
+ "You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time"
1128
+ )
1129
+ elif input_ids is not None:
1130
+ batch_size, seq_length = input_ids.shape
1131
+ elif inputs_embeds is not None:
1132
+ batch_size, seq_length, _ = inputs_embeds.shape
1133
+ else:
1134
+ raise ValueError(
1135
+ "You have to specify either decoder_input_ids or decoder_inputs_embeds"
1136
+ )
1137
+
1138
+ seq_length_with_past = seq_length
1139
+ past_key_values_length = 0
1140
+
1141
+ if self.gradient_checkpointing and self.training:
1142
+ if use_cache:
1143
+ logger.warning_once(
1144
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
1145
+ )
1146
+ use_cache = False
1147
+
1148
+ if use_cache:
1149
+ use_legacy_cache = not isinstance(past_key_values, Cache)
1150
+ if use_legacy_cache:
1151
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
1152
+ past_key_values_length = past_key_values.get_usable_length(seq_length)
1153
+ seq_length_with_past = seq_length_with_past + past_key_values_length
1154
+
1155
+ if position_ids is None:
1156
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
1157
+ position_ids = torch.arange(
1158
+ past_key_values_length,
1159
+ seq_length + past_key_values_length,
1160
+ dtype=torch.long,
1161
+ device=device,
1162
+ )
1163
+ position_ids = position_ids.unsqueeze(0)
1164
+
1165
+ if inputs_embeds is None:
1166
+ inputs_embeds = self.embed_tokens(input_ids)
1167
+ # embed positions
1168
+ if self._attn_implementation == "flash_attention_2":
1169
+ # 2d mask is passed through the layers
1170
+ attention_mask = (
1171
+ attention_mask
1172
+ if (attention_mask is not None and 0 in attention_mask)
1173
+ else None
1174
+ )
1175
+ # for output_attentions case used fallback to eager attention realization
1176
+ elif self._attn_implementation == "sdpa" and not output_attentions:
1177
+ attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
1178
+ attention_mask,
1179
+ (batch_size, seq_length),
1180
+ inputs_embeds,
1181
+ past_key_values_length,
1182
+ )
1183
+ else:
1184
+ # 4d mask is passed through the layers
1185
+ attention_mask = _prepare_4d_causal_attention_mask(
1186
+ attention_mask,
1187
+ (batch_size, seq_length),
1188
+ inputs_embeds,
1189
+ past_key_values_length,
1190
+ )
1191
+
1192
+ hidden_states = inputs_embeds
1193
+
1194
+ # decoder layers
1195
+ all_hidden_states = () if output_hidden_states else None
1196
+ all_self_attns = () if output_attentions else None
1197
+ next_decoder_cache = None
1198
+
1199
+ for decoder_layer in self.layers:
1200
+ if output_hidden_states:
1201
+ all_hidden_states += (hidden_states,)
1202
+
1203
+ if self.gradient_checkpointing and self.training:
1204
+ layer_outputs = self._gradient_checkpointing_func(
1205
+ decoder_layer.__call__,
1206
+ hidden_states,
1207
+ attention_mask,
1208
+ position_ids,
1209
+ past_key_values,
1210
+ output_attentions,
1211
+ )
1212
+ else:
1213
+ layer_outputs = decoder_layer(
1214
+ hidden_states,
1215
+ attention_mask=attention_mask,
1216
+ position_ids=position_ids,
1217
+ past_key_value=past_key_values,
1218
+ output_attentions=output_attentions,
1219
+ use_cache=use_cache,
1220
+ )
1221
+
1222
+ hidden_states = layer_outputs[0]
1223
+
1224
+ if use_cache:
1225
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1226
+
1227
+ if output_attentions:
1228
+ all_self_attns += (layer_outputs[1],)
1229
+
1230
+ hidden_states = self.norm(hidden_states)
1231
+
1232
+ # add hidden states from the last decoder layer
1233
+ if output_hidden_states:
1234
+ all_hidden_states += (hidden_states,)
1235
+
1236
+ next_cache = None
1237
+ if use_cache:
1238
+ next_cache = (
1239
+ next_decoder_cache.to_legacy_cache()
1240
+ if use_legacy_cache
1241
+ else next_decoder_cache
1242
+ )
1243
+
1244
+ if not return_dict:
1245
+ return tuple(
1246
+ v
1247
+ for v in [hidden_states, next_cache, all_hidden_states, all_self_attns]
1248
+ if v is not None
1249
+ )
1250
+ return BaseModelOutputWithPast(
1251
+ last_hidden_state=hidden_states,
1252
+ past_key_values=next_cache,
1253
+ hidden_states=all_hidden_states,
1254
+ attentions=all_self_attns,
1255
+ )
1256
+
1257
+
1258
+ # Copied from transformers.models.persimmon.modeling_persimmon.PersimmonForCausalLM with PERSIMMON->STABLELM,Persimmon->StableLm
1259
+ class StableLmForCausalLM(StableLmPreTrainedModel):
1260
+ _tied_weights_keys = ["lm_head.weight"]
1261
+
1262
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.__init__ with LLAMA->STABLELM,Llama->StableLm
1263
+ def __init__(self, config):
1264
+ super().__init__(config)
1265
+ self.model = StableLmModel(config)
1266
+ self.vocab_size = config.vocab_size
1267
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1268
+
1269
+ # Initialize weights and apply final processing
1270
+ self.post_init()
1271
+
1272
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_input_embeddings
1273
+ def get_input_embeddings(self):
1274
+ return self.model.embed_tokens
1275
+
1276
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_input_embeddings
1277
+ def set_input_embeddings(self, value):
1278
+ self.model.embed_tokens = value
1279
+
1280
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_output_embeddings
1281
+ def get_output_embeddings(self):
1282
+ return self.lm_head
1283
+
1284
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_output_embeddings
1285
+ def set_output_embeddings(self, new_embeddings):
1286
+ self.lm_head = new_embeddings
1287
+
1288
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_decoder
1289
+ def set_decoder(self, decoder):
1290
+ self.model = decoder
1291
+
1292
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_decoder
1293
+ def get_decoder(self):
1294
+ return self.model
1295
+
1296
+ @add_start_docstrings_to_model_forward(STABLELM_INPUTS_DOCSTRING)
1297
+ @replace_return_docstrings(
1298
+ output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC
1299
+ )
1300
+ # Ignore copy
1301
+ def forward(
1302
+ self,
1303
+ input_ids: torch.LongTensor = None,
1304
+ attention_mask: Optional[torch.Tensor] = None,
1305
+ position_ids: Optional[torch.LongTensor] = None,
1306
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1307
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1308
+ labels: Optional[torch.LongTensor] = None,
1309
+ use_cache: Optional[bool] = None,
1310
+ output_attentions: Optional[bool] = None,
1311
+ output_hidden_states: Optional[bool] = None,
1312
+ return_dict: Optional[bool] = None,
1313
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1314
+ r"""
1315
+ Args:
1316
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1317
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1318
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1319
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1320
+
1321
+ Returns:
1322
+
1323
+ Example:
1324
+
1325
+ ```python
1326
+ >>> from transformers import AutoTokenizer, StableLmForCausalLM
1327
+
1328
+ >>> model = StableLmForCausalLM.from_pretrained("stabilityai/stablelm-3b-4e1t")
1329
+ >>> tokenizer = AutoTokenizer.from_pretrained("stabilityai/stablelm-3b-4e1t")
1330
+
1331
+ >>> prompt = "The weather is always wonderful in"
1332
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1333
+
1334
+ >>> # Generate
1335
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1336
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1337
+ 'The weather is always wonderful in the summer in the city of San Diego. The city is located on the coast of the Pacific Ocean and is surrounded by'
1338
+ ```"""
1339
+
1340
+ output_attentions = (
1341
+ output_attentions
1342
+ if output_attentions is not None
1343
+ else self.config.output_attentions
1344
+ )
1345
+ output_hidden_states = (
1346
+ output_hidden_states
1347
+ if output_hidden_states is not None
1348
+ else self.config.output_hidden_states
1349
+ )
1350
+ return_dict = (
1351
+ return_dict if return_dict is not None else self.config.use_return_dict
1352
+ )
1353
+
1354
+ outputs = self.model(
1355
+ input_ids=input_ids,
1356
+ attention_mask=attention_mask,
1357
+ position_ids=position_ids,
1358
+ past_key_values=past_key_values,
1359
+ inputs_embeds=inputs_embeds,
1360
+ use_cache=use_cache,
1361
+ output_attentions=output_attentions,
1362
+ output_hidden_states=output_hidden_states,
1363
+ return_dict=return_dict,
1364
+ )
1365
+
1366
+ hidden_states = outputs[0]
1367
+ logits = self.lm_head(hidden_states)
1368
+
1369
+ loss = None
1370
+ if labels is not None:
1371
+ # Shift so that tokens < n predict n
1372
+ shift_logits = logits[..., :-1, :].contiguous()
1373
+ shift_labels = labels[..., 1:].contiguous()
1374
+ # Flatten the tokens
1375
+ loss_fct = CrossEntropyLoss()
1376
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1377
+ shift_labels = shift_labels.view(-1)
1378
+ # Enable model parallelism
1379
+ shift_labels = shift_labels.to(shift_logits.device)
1380
+ loss = loss_fct(shift_logits, shift_labels)
1381
+
1382
+ if not return_dict:
1383
+ output = (logits,) + outputs[1:]
1384
+ return (loss,) + output if loss is not None else output
1385
+
1386
+ return CausalLMOutputWithPast(
1387
+ loss=loss,
1388
+ logits=logits,
1389
+ past_key_values=outputs.past_key_values,
1390
+ hidden_states=outputs.hidden_states,
1391
+ attentions=outputs.attentions,
1392
+ )
1393
+
1394
+ def prepare_inputs_for_generation(
1395
+ self,
1396
+ input_ids,
1397
+ past_key_values=None,
1398
+ attention_mask=None,
1399
+ inputs_embeds=None,
1400
+ **kwargs,
1401
+ ):
1402
+ if past_key_values is not None:
1403
+ if isinstance(past_key_values, Cache):
1404
+ cache_length = past_key_values.get_seq_length()
1405
+ past_length = past_key_values.seen_tokens
1406
+ max_cache_length = past_key_values.get_max_length()
1407
+ else:
1408
+ cache_length = past_length = past_key_values[0][0].shape[2]
1409
+ max_cache_length = None
1410
+
1411
+ # Keep only the unprocessed tokens:
1412
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1413
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
1414
+ # input)
1415
+ if (
1416
+ attention_mask is not None
1417
+ and attention_mask.shape[1] > input_ids.shape[1]
1418
+ ):
1419
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1420
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1421
+ # input_ids based on the past_length.
1422
+ elif past_length < input_ids.shape[1]:
1423
+ input_ids = input_ids[:, past_length:]
1424
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1425
+
1426
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1427
+ if (
1428
+ max_cache_length is not None
1429
+ and attention_mask is not None
1430
+ and cache_length + input_ids.shape[1] > max_cache_length
1431
+ ):
1432
+ attention_mask = attention_mask[:, -max_cache_length:]
1433
+
1434
+ position_ids = kwargs.get("position_ids", None)
1435
+ if attention_mask is not None and position_ids is None:
1436
+ # create position_ids on the fly for batch generation
1437
+ position_ids = attention_mask.long().cumsum(-1) - 1
1438
+ position_ids.masked_fill_(attention_mask == 0, 1)
1439
+ if past_key_values:
1440
+ position_ids = position_ids[:, -input_ids.shape[1] :]
1441
+
1442
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1443
+ if inputs_embeds is not None and past_key_values is None:
1444
+ model_inputs = {"inputs_embeds": inputs_embeds}
1445
+ else:
1446
+ model_inputs = {"input_ids": input_ids}
1447
+
1448
+ model_inputs.update(
1449
+ {
1450
+ "position_ids": position_ids,
1451
+ "past_key_values": past_key_values,
1452
+ "use_cache": kwargs.get("use_cache"),
1453
+ "attention_mask": attention_mask,
1454
+ }
1455
+ )
1456
+ return model_inputs
1457
+
1458
+ @staticmethod
1459
+ def _reorder_cache(past_key_values, beam_idx):
1460
+ reordered_past = ()
1461
+ for layer_past in past_key_values:
1462
+ reordered_past += (
1463
+ tuple(
1464
+ past_state.index_select(0, beam_idx.to(past_state.device))
1465
+ for past_state in layer_past
1466
+ ),
1467
+ )
1468
+ return reordered_past
1469
+
1470
+
1471
+ @add_start_docstrings(
1472
+ """
1473
+ The StableLm transformer with a sequence classification head on top (linear layer).
1474
+
1475
+ [`StableLmForSequenceClassification`] uses the last token in order to do the classification, as other causal
1476
+ models (e.g. GPT-2) do.
1477
+
1478
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1479
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1480
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1481
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1482
+ each row of the batch).
1483
+ """,
1484
+ STABLELM_START_DOCSTRING,
1485
+ )
1486
+ # Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with LLAMA->STABLELM,Llama->StableLm
1487
+ class StableLmForSequenceClassification(StableLmPreTrainedModel):
1488
+ def __init__(self, config):
1489
+ super().__init__(config)
1490
+ self.num_labels = config.num_labels
1491
+ self.model = StableLmModel(config)
1492
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1493
+
1494
+ # Initialize weights and apply final processing
1495
+ self.post_init()
1496
+
1497
+ def get_input_embeddings(self):
1498
+ return self.model.embed_tokens
1499
+
1500
+ def set_input_embeddings(self, value):
1501
+ self.model.embed_tokens = value
1502
+
1503
+ @add_start_docstrings_to_model_forward(STABLELM_INPUTS_DOCSTRING)
1504
+ def forward(
1505
+ self,
1506
+ input_ids: torch.LongTensor = None,
1507
+ attention_mask: Optional[torch.Tensor] = None,
1508
+ position_ids: Optional[torch.LongTensor] = None,
1509
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1510
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1511
+ labels: Optional[torch.LongTensor] = None,
1512
+ use_cache: Optional[bool] = None,
1513
+ output_attentions: Optional[bool] = None,
1514
+ output_hidden_states: Optional[bool] = None,
1515
+ return_dict: Optional[bool] = None,
1516
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1517
+ r"""
1518
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1519
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1520
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1521
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1522
+ """
1523
+ return_dict = (
1524
+ return_dict if return_dict is not None else self.config.use_return_dict
1525
+ )
1526
+
1527
+ transformer_outputs = self.model(
1528
+ input_ids,
1529
+ attention_mask=attention_mask,
1530
+ position_ids=position_ids,
1531
+ past_key_values=past_key_values,
1532
+ inputs_embeds=inputs_embeds,
1533
+ use_cache=use_cache,
1534
+ output_attentions=output_attentions,
1535
+ output_hidden_states=output_hidden_states,
1536
+ return_dict=return_dict,
1537
+ )
1538
+ hidden_states = transformer_outputs[0]
1539
+ logits = self.score(hidden_states)
1540
+
1541
+ if input_ids is not None:
1542
+ batch_size = input_ids.shape[0]
1543
+ else:
1544
+ batch_size = inputs_embeds.shape[0]
1545
+
1546
+ if self.config.pad_token_id is None and batch_size != 1:
1547
+ raise ValueError(
1548
+ "Cannot handle batch sizes > 1 if no padding token is defined."
1549
+ )
1550
+ if self.config.pad_token_id is None:
1551
+ sequence_lengths = -1
1552
+ else:
1553
+ if input_ids is not None:
1554
+ # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
1555
+ sequence_lengths = (
1556
+ torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
1557
+ )
1558
+ sequence_lengths = sequence_lengths % input_ids.shape[-1]
1559
+ sequence_lengths = sequence_lengths.to(logits.device)
1560
+ else:
1561
+ sequence_lengths = -1
1562
+
1563
+ pooled_logits = logits[
1564
+ torch.arange(batch_size, device=logits.device), sequence_lengths
1565
+ ]
1566
+
1567
+ loss = None
1568
+ if labels is not None:
1569
+ labels = labels.to(logits.device)
1570
+ if self.config.problem_type is None:
1571
+ if self.num_labels == 1:
1572
+ self.config.problem_type = "regression"
1573
+ elif self.num_labels > 1 and (
1574
+ labels.dtype == torch.long or labels.dtype == torch.int
1575
+ ):
1576
+ self.config.problem_type = "single_label_classification"
1577
+ else:
1578
+ self.config.problem_type = "multi_label_classification"
1579
+
1580
+ if self.config.problem_type == "regression":
1581
+ loss_fct = MSELoss()
1582
+ if self.num_labels == 1:
1583
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1584
+ else:
1585
+ loss = loss_fct(pooled_logits, labels)
1586
+ elif self.config.problem_type == "single_label_classification":
1587
+ loss_fct = CrossEntropyLoss()
1588
+ loss = loss_fct(
1589
+ pooled_logits.view(-1, self.num_labels), labels.view(-1)
1590
+ )
1591
+ elif self.config.problem_type == "multi_label_classification":
1592
+ loss_fct = BCEWithLogitsLoss()
1593
+ loss = loss_fct(pooled_logits, labels)
1594
+ if not return_dict:
1595
+ output = (pooled_logits,) + transformer_outputs[1:]
1596
+ return ((loss,) + output) if loss is not None else output
1597
+
1598
+ return SequenceClassifierOutputWithPast(
1599
+ loss=loss,
1600
+ logits=pooled_logits,
1601
+ past_key_values=transformer_outputs.past_key_values,
1602
+ hidden_states=transformer_outputs.hidden_states,
1603
+ attentions=transformer_outputs.attentions,
1604
+ )
1605
+
1606
+
1607
+ StableLmConfig.register_for_auto_class()
1608
+ StableLmForCausalLM.register_for_auto_class("AutoModelForCausalLM")
special_tokens_map.json ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|reg_extra|>",
4
+ "<|endoftext|>",
5
+ "<|fim_prefix|>",
6
+ "<|fim_middle|>",
7
+ "<|fim_suffix|>",
8
+ "<|fim_pad|>",
9
+ "<gh_stars>",
10
+ "<filename>",
11
+ "<issue_start>",
12
+ "<issue_comment>",
13
+ "<issue_closed>",
14
+ "<jupyter_start>",
15
+ "<jupyter_text>",
16
+ "<jupyter_code>",
17
+ "<jupyter_output>",
18
+ "<empty_output>",
19
+ "<commit_before>",
20
+ "<commit_msg>",
21
+ "<commit_after>",
22
+ "<reponame>",
23
+ "<|endofprompt|>",
24
+ "<|im_start|>",
25
+ "<|im_end|>",
26
+ "<|pause|>",
27
+ "<|reg0|>",
28
+ "<|reg1|>",
29
+ "<|reg2|>",
30
+ "<|reg3|>",
31
+ "<|reg4|>",
32
+ "<|reg5|>",
33
+ "<|reg6|>",
34
+ "<|reg7|>",
35
+ "<|extra0|>"
36
+ ],
37
+ "bos_token": {
38
+ "content": "<|endoftext|>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false
43
+ },
44
+ "eos_token": {
45
+ "content": "<|endoftext|>",
46
+ "lstrip": false,
47
+ "normalized": false,
48
+ "rstrip": false,
49
+ "single_word": false
50
+ },
51
+ "pad_token": {
52
+ "content": "<|endoftext|>",
53
+ "lstrip": false,
54
+ "normalized": false,
55
+ "rstrip": false,
56
+ "single_word": false
57
+ },
58
+ "unk_token": {
59
+ "content": "<|endoftext|>",
60
+ "lstrip": false,
61
+ "normalized": false,
62
+ "rstrip": false,
63
+ "single_word": false
64
+ }
65
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,316 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "added_tokens_decoder": {
4
+ "100256": {
5
+ "content": "<|reg_extra|>",
6
+ "lstrip": false,
7
+ "normalized": false,
8
+ "rstrip": false,
9
+ "single_word": false,
10
+ "special": true
11
+ },
12
+ "100257": {
13
+ "content": "<|endoftext|>",
14
+ "lstrip": false,
15
+ "normalized": false,
16
+ "rstrip": false,
17
+ "single_word": false,
18
+ "special": true
19
+ },
20
+ "100258": {
21
+ "content": "<|fim_prefix|>",
22
+ "lstrip": false,
23
+ "normalized": false,
24
+ "rstrip": false,
25
+ "single_word": false,
26
+ "special": true
27
+ },
28
+ "100259": {
29
+ "content": "<|fim_middle|>",
30
+ "lstrip": false,
31
+ "normalized": false,
32
+ "rstrip": false,
33
+ "single_word": false,
34
+ "special": true
35
+ },
36
+ "100260": {
37
+ "content": "<|fim_suffix|>",
38
+ "lstrip": false,
39
+ "normalized": false,
40
+ "rstrip": false,
41
+ "single_word": false,
42
+ "special": true
43
+ },
44
+ "100261": {
45
+ "content": "<|fim_pad|>",
46
+ "lstrip": false,
47
+ "normalized": false,
48
+ "rstrip": false,
49
+ "single_word": false,
50
+ "special": true
51
+ },
52
+ "100262": {
53
+ "content": "<gh_stars>",
54
+ "lstrip": false,
55
+ "normalized": false,
56
+ "rstrip": false,
57
+ "single_word": false,
58
+ "special": true
59
+ },
60
+ "100263": {
61
+ "content": "<filename>",
62
+ "lstrip": false,
63
+ "normalized": false,
64
+ "rstrip": false,
65
+ "single_word": false,
66
+ "special": true
67
+ },
68
+ "100264": {
69
+ "content": "<issue_start>",
70
+ "lstrip": false,
71
+ "normalized": false,
72
+ "rstrip": false,
73
+ "single_word": false,
74
+ "special": true
75
+ },
76
+ "100265": {
77
+ "content": "<issue_comment>",
78
+ "lstrip": false,
79
+ "normalized": false,
80
+ "rstrip": false,
81
+ "single_word": false,
82
+ "special": true
83
+ },
84
+ "100266": {
85
+ "content": "<issue_closed>",
86
+ "lstrip": false,
87
+ "normalized": false,
88
+ "rstrip": false,
89
+ "single_word": false,
90
+ "special": true
91
+ },
92
+ "100267": {
93
+ "content": "<jupyter_start>",
94
+ "lstrip": false,
95
+ "normalized": false,
96
+ "rstrip": false,
97
+ "single_word": false,
98
+ "special": true
99
+ },
100
+ "100268": {
101
+ "content": "<jupyter_text>",
102
+ "lstrip": false,
103
+ "normalized": false,
104
+ "rstrip": false,
105
+ "single_word": false,
106
+ "special": true
107
+ },
108
+ "100269": {
109
+ "content": "<jupyter_code>",
110
+ "lstrip": false,
111
+ "normalized": false,
112
+ "rstrip": false,
113
+ "single_word": false,
114
+ "special": true
115
+ },
116
+ "100270": {
117
+ "content": "<jupyter_output>",
118
+ "lstrip": false,
119
+ "normalized": false,
120
+ "rstrip": false,
121
+ "single_word": false,
122
+ "special": true
123
+ },
124
+ "100271": {
125
+ "content": "<empty_output>",
126
+ "lstrip": false,
127
+ "normalized": false,
128
+ "rstrip": false,
129
+ "single_word": false,
130
+ "special": true
131
+ },
132
+ "100272": {
133
+ "content": "<commit_before>",
134
+ "lstrip": false,
135
+ "normalized": false,
136
+ "rstrip": false,
137
+ "single_word": false,
138
+ "special": true
139
+ },
140
+ "100273": {
141
+ "content": "<commit_msg>",
142
+ "lstrip": false,
143
+ "normalized": false,
144
+ "rstrip": false,
145
+ "single_word": false,
146
+ "special": true
147
+ },
148
+ "100274": {
149
+ "content": "<commit_after>",
150
+ "lstrip": false,
151
+ "normalized": false,
152
+ "rstrip": false,
153
+ "single_word": false,
154
+ "special": true
155
+ },
156
+ "100275": {
157
+ "content": "<reponame>",
158
+ "lstrip": false,
159
+ "normalized": false,
160
+ "rstrip": false,
161
+ "single_word": false,
162
+ "special": true
163
+ },
164
+ "100276": {
165
+ "content": "<|endofprompt|>",
166
+ "lstrip": false,
167
+ "normalized": false,
168
+ "rstrip": false,
169
+ "single_word": false,
170
+ "special": true
171
+ },
172
+ "100277": {
173
+ "content": "<|im_start|>",
174
+ "lstrip": false,
175
+ "normalized": false,
176
+ "rstrip": false,
177
+ "single_word": false,
178
+ "special": true
179
+ },
180
+ "100278": {
181
+ "content": "<|im_end|>",
182
+ "lstrip": false,
183
+ "normalized": false,
184
+ "rstrip": false,
185
+ "single_word": false,
186
+ "special": true
187
+ },
188
+ "100279": {
189
+ "content": "<|pause|>",
190
+ "lstrip": false,
191
+ "normalized": false,
192
+ "rstrip": false,
193
+ "single_word": false,
194
+ "special": true
195
+ },
196
+ "100280": {
197
+ "content": "<|reg0|>",
198
+ "lstrip": false,
199
+ "normalized": false,
200
+ "rstrip": false,
201
+ "single_word": false,
202
+ "special": true
203
+ },
204
+ "100281": {
205
+ "content": "<|reg1|>",
206
+ "lstrip": false,
207
+ "normalized": false,
208
+ "rstrip": false,
209
+ "single_word": false,
210
+ "special": true
211
+ },
212
+ "100282": {
213
+ "content": "<|reg2|>",
214
+ "lstrip": false,
215
+ "normalized": false,
216
+ "rstrip": false,
217
+ "single_word": false,
218
+ "special": true
219
+ },
220
+ "100283": {
221
+ "content": "<|reg3|>",
222
+ "lstrip": false,
223
+ "normalized": false,
224
+ "rstrip": false,
225
+ "single_word": false,
226
+ "special": true
227
+ },
228
+ "100284": {
229
+ "content": "<|reg4|>",
230
+ "lstrip": false,
231
+ "normalized": false,
232
+ "rstrip": false,
233
+ "single_word": false,
234
+ "special": true
235
+ },
236
+ "100285": {
237
+ "content": "<|reg5|>",
238
+ "lstrip": false,
239
+ "normalized": false,
240
+ "rstrip": false,
241
+ "single_word": false,
242
+ "special": true
243
+ },
244
+ "100286": {
245
+ "content": "<|reg6|>",
246
+ "lstrip": false,
247
+ "normalized": false,
248
+ "rstrip": false,
249
+ "single_word": false,
250
+ "special": true
251
+ },
252
+ "100287": {
253
+ "content": "<|reg7|>",
254
+ "lstrip": false,
255
+ "normalized": false,
256
+ "rstrip": false,
257
+ "single_word": false,
258
+ "special": true
259
+ },
260
+ "100288": {
261
+ "content": "<|extra0|>",
262
+ "lstrip": false,
263
+ "normalized": false,
264
+ "rstrip": false,
265
+ "single_word": false,
266
+ "special": true
267
+ }
268
+ },
269
+ "additional_special_tokens": [
270
+ "<|reg_extra|>",
271
+ "<|endoftext|>",
272
+ "<|fim_prefix|>",
273
+ "<|fim_middle|>",
274
+ "<|fim_suffix|>",
275
+ "<|fim_pad|>",
276
+ "<gh_stars>",
277
+ "<filename>",
278
+ "<issue_start>",
279
+ "<issue_comment>",
280
+ "<issue_closed>",
281
+ "<jupyter_start>",
282
+ "<jupyter_text>",
283
+ "<jupyter_code>",
284
+ "<jupyter_output>",
285
+ "<empty_output>",
286
+ "<commit_before>",
287
+ "<commit_msg>",
288
+ "<commit_after>",
289
+ "<reponame>",
290
+ "<|endofprompt|>",
291
+ "<|im_start|>",
292
+ "<|im_end|>",
293
+ "<|pause|>",
294
+ "<|reg0|>",
295
+ "<|reg1|>",
296
+ "<|reg2|>",
297
+ "<|reg3|>",
298
+ "<|reg4|>",
299
+ "<|reg5|>",
300
+ "<|reg6|>",
301
+ "<|reg7|>",
302
+ "<|extra0|>"
303
+ ],
304
+ "bos_token": "<|endoftext|>",
305
+ "chat_template": "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% set system_message = 'You are a helpful assistant.' %}{% endif %}{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in loop_messages %}{% if loop.index0 == 0 %}{{'<|im_start|>system\n' + system_message + '<|im_end|>\n'}}{% endif %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
306
+ "clean_up_tokenization_spaces": true,
307
+ "eos_token": "<|endoftext|>",
308
+ "max_length": null,
309
+ "model_max_length": 4096,
310
+ "pad_to_multiple_of": null,
311
+ "pad_token": "<|endoftext|>",
312
+ "pad_token_type_id": 0,
313
+ "padding_side": "left",
314
+ "tokenizer_class": "GPT2Tokenizer",
315
+ "unk_token": "<|endoftext|>"
316
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff