bluesky333 commited on
Commit
2ea2611
1 Parent(s): a67d897

Upload folder using huggingface_hub

Browse files
CODE_OF_CONDUCT.md ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # Microsoft Open Source Code of Conduct
2
+
3
+ This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
4
+
5
+ Resources:
6
+
7
+ - [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
8
+ - [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
9
+ - Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns
LICENSE ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ PhyAGI.
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
NOTICE.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ NOTICES AND INFORMATION
2
+ Do Not Translate or Localize
3
+
4
+ This software incorporates material from third parties.
5
+
6
+ **Component.** https://github.com/Dao-AILab/flash-attention
7
+
8
+ **Open Source License/Copyright Notice.**
9
+
10
+ BSD 3-Clause License
11
+
12
+ Copyright (c) 2022, the respective contributors, as shown by the AUTHORS file.
13
+ All rights reserved.
14
+
15
+ Redistribution and use in source and binary forms, with or without
16
+ modification, are permitted provided that the following conditions are met:
17
+
18
+ * Redistributions of source code must retain the above copyright notice, this
19
+ list of conditions and the following disclaimer.
20
+
21
+ * Redistributions in binary form must reproduce the above copyright notice,
22
+ this list of conditions and the following disclaimer in the documentation
23
+ and/or other materials provided with the distribution.
24
+
25
+ * Neither the name of the copyright holder nor the names of its
26
+ contributors may be used to endorse or promote products derived from
27
+ this software without specific prior written permission.
28
+
29
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
33
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.md ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ inference: false
3
+ license: mit
4
+ license_link: https://huggingface.co/microsoft/phi-2/resolve/main/LICENSE
5
+ language:
6
+ - en
7
+ pipeline_tag: text-generation
8
+ tags:
9
+ - nlp
10
+ - code
11
+ ---
12
+
13
+ ## Model Summary
14
+
15
+ Phi-2 is a Transformer with **2.7 billion** parameters. It was trained using the same data sources as [Phi-1.5](https://huggingface.co/microsoft/phi-1.5), augmented with a new data source that consists of various NLP synthetic texts and filtered websites (for safety and educational value). When assessed against benchmarks testing common sense, language understanding, and logical reasoning, Phi-2 showcased a nearly state-of-the-art performance among models with less than 13 billion parameters.
16
+
17
+ Our model hasn't been fine-tuned through reinforcement learning from human feedback. The intention behind crafting this open-source model is to provide the research community with a non-restricted small model to explore vital safety challenges, such as reducing toxicity, understanding societal biases, enhancing controllability, and more.
18
+
19
+ ## How to Use
20
+
21
+ Phi-2 has been integrated in the development version (4.37.0.dev) of `transformers`. Until the official version is released through `pip`, ensure that you are doing one of the following:
22
+
23
+ * When loading the model, ensure that `trust_remote_code=True` is passed as an argument of the `from_pretrained()` function.
24
+
25
+ * Update your local `transformers` to the development version: `pip uninstall -y transformers && pip install git+https://github.com/huggingface/transformers`. The previous command is an alternative to cloning and installing from the source.
26
+
27
+ The current `transformers` version can be verified with: `pip list | grep transformers`.
28
+
29
+ ## Intended Uses
30
+
31
+ Given the nature of the training data, the Phi-2 model is best suited for prompts using the QA format, the chat format, and the code format.
32
+
33
+ ### QA Format:
34
+
35
+ You can provide the prompt as a standalone question as follows:
36
+
37
+ ```markdown
38
+ Write a detailed analogy between mathematics and a lighthouse.
39
+ ```
40
+ where the model generates the text after "." .
41
+ To encourage the model to write more concise answers, you can also try the following QA format using "Instruct: \<prompt\>\nOutput:"
42
+ ```markdown
43
+ Instruct: Write a detailed analogy between mathematics and a lighthouse.
44
+ Output: Mathematics is like a lighthouse. Just as a lighthouse guides ships safely to shore, mathematics provides a guiding light in the world of numbers and logic. It helps us navigate through complex problems and find solutions. Just as a lighthouse emits a steady beam of light, mathematics provides a consistent framework for reasoning and problem-solving. It illuminates the path to understanding and helps us make sense of the world around us.
45
+ ```
46
+
47
+ where the model generates the text after "Output:".
48
+
49
+ ### Chat Format:
50
+
51
+ ```markdown
52
+ Alice: I don't know why, I'm struggling to maintain focus while studying. Any suggestions?
53
+ Bob: Well, have you tried creating a study schedule and sticking to it?
54
+ Alice: Yes, I have, but it doesn't seem to help much.
55
+ Bob: Hmm, maybe you should try studying in a quiet environment, like the library.
56
+ Alice: ...
57
+ ```
58
+
59
+ where the model generates the text after the first "Bob:".
60
+
61
+ ### Code Format:
62
+
63
+ ```python
64
+ def print_prime(n):
65
+ """
66
+ Print all primes between 1 and n
67
+ """
68
+ primes = []
69
+ for num in range(2, n+1):
70
+ is_prime = True
71
+ for i in range(2, int(math.sqrt(num))+1):
72
+ if num % i == 0:
73
+ is_prime = False
74
+ break
75
+ if is_prime:
76
+ primes.append(num)
77
+ print(primes)
78
+ ```
79
+
80
+ where the model generates the text after the comments.
81
+
82
+ **Notes:**
83
+
84
+ * Phi-2 is intended for QA, chat, and code purposes. The model-generated text/code should be treated as a starting point rather than a definitive solution for potential use cases. Users should be cautious when employing these models in their applications.
85
+
86
+ * Direct adoption for production tasks without evaluation is out of scope of this project. As a result, the Phi-2 model has not been tested to ensure that it performs adequately for any production-level application. Please refer to the limitation sections of this document for more details.
87
+
88
+ * If you are using `transformers<4.37.0`, always load the model with `trust_remote_code=True` to prevent side-effects.
89
+
90
+ ## Sample Code
91
+
92
+ ```python
93
+ import torch
94
+ from transformers import AutoModelForCausalLM, AutoTokenizer
95
+
96
+ torch.set_default_device("cuda")
97
+
98
+ model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype="auto", trust_remote_code=True)
99
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
100
+
101
+ inputs = tokenizer('''def print_prime(n):
102
+ """
103
+ Print all primes between 1 and n
104
+ """''', return_tensors="pt", return_attention_mask=False)
105
+
106
+ outputs = model.generate(**inputs, max_length=200)
107
+ text = tokenizer.batch_decode(outputs)[0]
108
+ print(text)
109
+ ```
110
+
111
+ ## Limitations of Phi-2
112
+
113
+ * Generate Inaccurate Code and Facts: The model may produce incorrect code snippets and statements. Users should treat these outputs as suggestions or starting points, not as definitive or accurate solutions.
114
+
115
+ * Limited Scope for code: Majority of Phi-2 training data is based in Python and use common packages such as "typing, math, random, collections, datetime, itertools". If the model generates Python scripts that utilize other packages or scripts in other languages, we strongly recommend users manually verify all API uses.
116
+
117
+ * Unreliable Responses to Instruction: The model has not undergone instruction fine-tuning. As a result, it may struggle or fail to adhere to intricate or nuanced instructions provided by users.
118
+
119
+ * Language Limitations: The model is primarily designed to understand standard English. Informal English, slang, or any other languages might pose challenges to its comprehension, leading to potential misinterpretations or errors in response.
120
+
121
+ * Potential Societal Biases: Phi-2 is not entirely free from societal biases despite efforts in assuring training data safety. There's a possibility it may generate content that mirrors these societal biases, particularly if prompted or instructed to do so. We urge users to be aware of this and to exercise caution and critical thinking when interpreting model outputs.
122
+
123
+ * Toxicity: Despite being trained with carefully selected data, the model can still produce harmful content if explicitly prompted or instructed to do so. We chose to release the model to help the open-source community develop the most effective ways to reduce the toxicity of a model directly after pretraining.
124
+
125
+ * Verbosity: Phi-2 being a base model often produces irrelevant or extra text and responses following its first answer to user prompts within a single turn. This is due to its training dataset being primarily textbooks, which results in textbook-like responses.
126
+
127
+ ## Training
128
+
129
+ ### Model
130
+
131
+ * Architecture: a Transformer-based model with next-word prediction objective
132
+
133
+ * Context length: 2048 tokens
134
+
135
+ * Dataset size: 250B tokens, combination of NLP synthetic data created by AOAI GPT-3.5 and filtered web data from Falcon RefinedWeb and SlimPajama, which was assessed by AOAI GPT-4.
136
+
137
+ * Training tokens: 1.4T tokens
138
+
139
+ * GPUs: 96xA100-80G
140
+
141
+ * Training time: 14 days
142
+
143
+ ### Software
144
+
145
+ * [PyTorch](https://github.com/pytorch/pytorch)
146
+
147
+ * [DeepSpeed](https://github.com/microsoft/DeepSpeed)
148
+
149
+ * [Flash-Attention](https://github.com/HazyResearch/flash-attention)
150
+
151
+ ### License
152
+
153
+ The model is licensed under the [MIT license](https://huggingface.co/microsoft/phi-2/resolve/main/LICENSE).
154
+
155
+ ## Trademarks
156
+
157
+ This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow [Microsoft’s Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies.
SECURITY.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!-- BEGIN MICROSOFT SECURITY.MD V0.0.9 BLOCK -->
2
+
3
+ ## Security
4
+
5
+ Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet) and [Xamarin](https://github.com/xamarin).
6
+
7
+ If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/security.md/definition), please report it to us as described below.
8
+
9
+ ## Reporting Security Issues
10
+
11
+ **Please do not report security vulnerabilities through public GitHub issues.**
12
+
13
+ Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/security.md/msrc/create-report).
14
+
15
+ If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/security.md/msrc/pgp).
16
+
17
+ You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
18
+
19
+ Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
20
+
21
+ * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
22
+ * Full paths of source file(s) related to the manifestation of the issue
23
+ * The location of the affected source code (tag/branch/commit or direct URL)
24
+ * Any special configuration required to reproduce the issue
25
+ * Step-by-step instructions to reproduce the issue
26
+ * Proof-of-concept or exploit code (if possible)
27
+ * Impact of the issue, including how an attacker might exploit the issue
28
+
29
+ This information will help us triage your report more quickly.
30
+
31
+ If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/security.md/msrc/bounty) page for more details about our active programs.
32
+
33
+ ## Preferred Languages
34
+
35
+ We prefer all communications to be in English.
36
+
37
+ ## Policy
38
+
39
+ Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/security.md/cvd).
40
+
41
+ <!-- END MICROSOFT SECURITY.MD BLOCK -->
added_tokens.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "\t\t": 50294,
3
+ "\t\t\t": 50293,
4
+ "\t\t\t\t": 50292,
5
+ "\t\t\t\t\t": 50291,
6
+ "\t\t\t\t\t\t": 50290,
7
+ "\t\t\t\t\t\t\t": 50289,
8
+ "\t\t\t\t\t\t\t\t": 50288,
9
+ "\t\t\t\t\t\t\t\t\t": 50287,
10
+ " ": 50286,
11
+ " ": 50285,
12
+ " ": 50284,
13
+ " ": 50283,
14
+ " ": 50282,
15
+ " ": 50281,
16
+ " ": 50280,
17
+ " ": 50279,
18
+ " ": 50278,
19
+ " ": 50277,
20
+ " ": 50276,
21
+ " ": 50275,
22
+ " ": 50274,
23
+ " ": 50273,
24
+ " ": 50272,
25
+ " ": 50271,
26
+ " ": 50270,
27
+ " ": 50269,
28
+ " ": 50268,
29
+ " ": 50267,
30
+ " ": 50266,
31
+ " ": 50265,
32
+ " ": 50264,
33
+ " ": 50263,
34
+ " ": 50262,
35
+ " ": 50261,
36
+ " ": 50260,
37
+ " ": 50259,
38
+ " ": 50258,
39
+ " ": 50257
40
+ }
all_results.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "epoch": 3.0,
3
+ "train_loss": 0.5114809172482763,
4
+ "train_runtime": 44939.8765,
5
+ "train_samples_per_second": 16.083,
6
+ "train_steps_per_second": 0.251
7
+ }
config.json ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "./model/phi2-knowmed-pretrain",
3
+ "architectures": [
4
+ "PhiForCausalLM"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_phi.PhiConfig",
9
+ "AutoModel": "modeling_phi.PhiForCausalLM",
10
+ "AutoModelForCausalLM": "modeling_phi.PhiForCausalLM"
11
+ },
12
+ "bos_token_id": null,
13
+ "embd_pdrop": 0.0,
14
+ "eos_token_id": null,
15
+ "hidden_act": "gelu_new",
16
+ "hidden_size": 2560,
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 10240,
19
+ "layer_norm_eps": 1e-05,
20
+ "max_position_embeddings": 2048,
21
+ "model_type": "phi",
22
+ "num_attention_heads": 32,
23
+ "num_hidden_layers": 32,
24
+ "num_key_value_heads": 32,
25
+ "partial_rotary_factor": 0.4,
26
+ "qk_layernorm": false,
27
+ "resid_pdrop": 0.1,
28
+ "rope_scaling": null,
29
+ "rope_theta": 10000.0,
30
+ "tie_word_embeddings": false,
31
+ "torch_dtype": "bfloat16",
32
+ "transformers_version": "4.37.1",
33
+ "use_cache": false,
34
+ "vocab_size": 51200
35
+ }
configuration_phi.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 Microsoft and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """ Phi model configuration"""
17
+
18
+
19
+ from transformers.configuration_utils import PretrainedConfig
20
+ from transformers.utils import logging
21
+
22
+
23
+ logger = logging.get_logger(__name__)
24
+
25
+ PHI_PRETRAINED_CONFIG_ARCHIVE_MAP = {
26
+ "microsoft/phi-2": "https://huggingface.co/microsoft/phi-2/resolve/main/config.json",
27
+ }
28
+
29
+
30
+ class PhiConfig(PretrainedConfig):
31
+ r"""
32
+ This is the configuration class to store the configuration of a [`PhiModel`]. It is used to instantiate an Phi
33
+ model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
34
+ defaults will yield a similar configuration to that of the Phi
35
+ [microsoft/phi-1](https://huggingface.co/microsoft/phi-1).
36
+
37
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
38
+ documentation from [`PretrainedConfig`] for more information.
39
+
40
+ Args:
41
+ vocab_size (`int`, *optional*, defaults to 51200):
42
+ Vocabulary size of the Phi model. Defines the number of different tokens that can be represented by the
43
+ `inputs_ids` passed when calling [`PhiModel`].
44
+ hidden_size (`int`, *optional*, defaults to 2048):
45
+ Dimension of the hidden representations.
46
+ intermediate_size (`int`, *optional*, defaults to 8192):
47
+ Dimension of the MLP representations.
48
+ num_hidden_layers (`int`, *optional*, defaults to 24):
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 decoder.
52
+ num_key_value_heads (`int`, *optional*):
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
+ resid_pdrop (`float`, *optional*, defaults to 0.0):
61
+ Dropout probability for mlp outputs.
62
+ embd_pdrop (`int`, *optional*, defaults to 0.0):
63
+ The dropout ratio for the embeddings.
64
+ attention_dropout (`float`, *optional*, defaults to 0.0):
65
+ The dropout ratio after computing the attention scores.
66
+ hidden_act (`str` or `function`, *optional*, defaults to `"gelu_new"`):
67
+ The non-linear activation function (function or string) in the decoder.
68
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
69
+ The maximum sequence length that this model might ever be used with. Phi-1 and Phi-1.5 supports up to 2048
70
+ tokens.
71
+ initializer_range (`float`, *optional*, defaults to 0.02):
72
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
73
+ layer_norm_eps (`float`, *optional*, defaults to 1e-05):
74
+ The epsilon used by the rms normalization layers.
75
+ use_cache (`bool`, *optional*, defaults to `True`):
76
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
77
+ relevant if `config.is_decoder=True`. Whether to tie weight embeddings or not.
78
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
79
+ Whether to tie weight embeddings
80
+ rope_theta (`float`, *optional*, defaults to 10000.0):
81
+ The base period of the RoPE embeddings.
82
+ rope_scaling (`Dict`, *optional*):
83
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
84
+ strategies: linear and dynamic. Their scaling factor must be an float greater than 1. The expected format
85
+ is `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
86
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
87
+ these scaling strategies behave:
88
+ https://www.reddit.com/r/LocalPersimmon/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This
89
+ is an experimental feature, subject to breaking API changes in future versions.
90
+ partial_rotary_factor (`float`, *optional*, defaults to 0.5):
91
+ Percentage of the query and keys which will have rotary embedding.
92
+ qk_layernorm (`bool`, *optional*, defaults to `False`):
93
+ Whether or not to normalize the Queries and Keys after projecting the hidden states.
94
+ bos_token_id (`int`, *optional*, defaults to 1):
95
+ Denotes beginning of sequences token id.
96
+ eos_token_id (`int`, *optional*, defaults to 2):
97
+ Denotes end of sequences token id.
98
+
99
+ Example:
100
+
101
+ ```python
102
+ >>> from transformers import PhiModel, PhiConfig
103
+
104
+ >>> # Initializing a Phi-1 style configuration
105
+ >>> configuration = PhiConfig.from_pretrained("microsoft/phi-1")
106
+
107
+ >>> # Initializing a model from the configuration
108
+ >>> model = PhiModel(configuration)
109
+
110
+ >>> # Accessing the model configuration
111
+ >>> configuration = model.config
112
+ ```"""
113
+
114
+ model_type = "phi"
115
+ keys_to_ignore_at_inference = ["past_key_values"]
116
+
117
+ def __init__(
118
+ self,
119
+ vocab_size=51200,
120
+ hidden_size=2048,
121
+ intermediate_size=8192,
122
+ num_hidden_layers=24,
123
+ num_attention_heads=32,
124
+ num_key_value_heads=None,
125
+ resid_pdrop=0.0,
126
+ embd_pdrop=0.0,
127
+ attention_dropout=0.0,
128
+ hidden_act="gelu_new",
129
+ max_position_embeddings=2048,
130
+ initializer_range=0.02,
131
+ layer_norm_eps=1e-5,
132
+ use_cache=True,
133
+ tie_word_embeddings=False,
134
+ rope_theta=10000.0,
135
+ rope_scaling=None,
136
+ partial_rotary_factor=0.5,
137
+ qk_layernorm=False,
138
+ bos_token_id=1,
139
+ eos_token_id=2,
140
+ **kwargs,
141
+ ):
142
+ self.vocab_size = vocab_size
143
+ self.hidden_size = hidden_size
144
+ self.intermediate_size = intermediate_size
145
+ self.num_hidden_layers = num_hidden_layers
146
+ self.num_attention_heads = num_attention_heads
147
+
148
+ if num_key_value_heads is None:
149
+ num_key_value_heads = num_attention_heads
150
+
151
+ self.num_key_value_heads = num_key_value_heads
152
+ self.resid_pdrop = resid_pdrop
153
+ self.embd_pdrop = embd_pdrop
154
+ self.attention_dropout = attention_dropout
155
+ self.hidden_act = hidden_act
156
+ self.max_position_embeddings = max_position_embeddings
157
+ self.initializer_range = initializer_range
158
+ self.layer_norm_eps = layer_norm_eps
159
+ self.use_cache = use_cache
160
+ self.rope_theta = rope_theta
161
+ self.rope_scaling = rope_scaling
162
+ self.partial_rotary_factor = partial_rotary_factor
163
+ self.qk_layernorm = qk_layernorm
164
+ self._rope_scaling_validation()
165
+
166
+ super().__init__(
167
+ bos_token_id=bos_token_id,
168
+ eos_token_id=eos_token_id,
169
+ tie_word_embeddings=tie_word_embeddings,
170
+ **kwargs,
171
+ )
172
+
173
+ # Copied from transformers.models.llama.configuration_llama.LlamaConfig._rope_scaling_validation
174
+ def _rope_scaling_validation(self):
175
+ """
176
+ Validate the `rope_scaling` configuration.
177
+ """
178
+ if self.rope_scaling is None:
179
+ return
180
+
181
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
182
+ raise ValueError(
183
+ "`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, "
184
+ f"got {self.rope_scaling}"
185
+ )
186
+ rope_scaling_type = self.rope_scaling.get("type", None)
187
+ rope_scaling_factor = self.rope_scaling.get("factor", None)
188
+ if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
189
+ raise ValueError(
190
+ f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
191
+ )
192
+ if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
193
+ raise ValueError(f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}")
generation_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "transformers_version": "4.37.1"
4
+ }
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:0d174cb2fdb88f215c3715f29e005e25f10b274e5d56532d3960fcb0ac1ffe30
3
+ size 4995584848
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a194dcf9eb2d878921ba43078290483014cacefd734e34084417db9334c0afa
3
+ size 563833008
model.safetensors.index.json ADDED
@@ -0,0 +1,460 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 5559367680
4
+ },
5
+ "weight_map": {
6
+ "lm_head.bias": "model-00002-of-00002.safetensors",
7
+ "lm_head.weight": "model-00002-of-00002.safetensors",
8
+ "model.embed_tokens.weight": "model-00001-of-00002.safetensors",
9
+ "model.final_layernorm.bias": "model-00002-of-00002.safetensors",
10
+ "model.final_layernorm.weight": "model-00002-of-00002.safetensors",
11
+ "model.layers.0.input_layernorm.bias": "model-00001-of-00002.safetensors",
12
+ "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors",
13
+ "model.layers.0.mlp.fc1.bias": "model-00001-of-00002.safetensors",
14
+ "model.layers.0.mlp.fc1.weight": "model-00001-of-00002.safetensors",
15
+ "model.layers.0.mlp.fc2.bias": "model-00001-of-00002.safetensors",
16
+ "model.layers.0.mlp.fc2.weight": "model-00001-of-00002.safetensors",
17
+ "model.layers.0.self_attn.dense.bias": "model-00001-of-00002.safetensors",
18
+ "model.layers.0.self_attn.dense.weight": "model-00001-of-00002.safetensors",
19
+ "model.layers.0.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
20
+ "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
21
+ "model.layers.0.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
22
+ "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
23
+ "model.layers.0.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
24
+ "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
25
+ "model.layers.1.input_layernorm.bias": "model-00001-of-00002.safetensors",
26
+ "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors",
27
+ "model.layers.1.mlp.fc1.bias": "model-00001-of-00002.safetensors",
28
+ "model.layers.1.mlp.fc1.weight": "model-00001-of-00002.safetensors",
29
+ "model.layers.1.mlp.fc2.bias": "model-00001-of-00002.safetensors",
30
+ "model.layers.1.mlp.fc2.weight": "model-00001-of-00002.safetensors",
31
+ "model.layers.1.self_attn.dense.bias": "model-00001-of-00002.safetensors",
32
+ "model.layers.1.self_attn.dense.weight": "model-00001-of-00002.safetensors",
33
+ "model.layers.1.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
34
+ "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
35
+ "model.layers.1.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
36
+ "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
37
+ "model.layers.1.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
38
+ "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
39
+ "model.layers.10.input_layernorm.bias": "model-00001-of-00002.safetensors",
40
+ "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors",
41
+ "model.layers.10.mlp.fc1.bias": "model-00001-of-00002.safetensors",
42
+ "model.layers.10.mlp.fc1.weight": "model-00001-of-00002.safetensors",
43
+ "model.layers.10.mlp.fc2.bias": "model-00001-of-00002.safetensors",
44
+ "model.layers.10.mlp.fc2.weight": "model-00001-of-00002.safetensors",
45
+ "model.layers.10.self_attn.dense.bias": "model-00001-of-00002.safetensors",
46
+ "model.layers.10.self_attn.dense.weight": "model-00001-of-00002.safetensors",
47
+ "model.layers.10.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
48
+ "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
49
+ "model.layers.10.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
50
+ "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
51
+ "model.layers.10.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
52
+ "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
53
+ "model.layers.11.input_layernorm.bias": "model-00001-of-00002.safetensors",
54
+ "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors",
55
+ "model.layers.11.mlp.fc1.bias": "model-00001-of-00002.safetensors",
56
+ "model.layers.11.mlp.fc1.weight": "model-00001-of-00002.safetensors",
57
+ "model.layers.11.mlp.fc2.bias": "model-00001-of-00002.safetensors",
58
+ "model.layers.11.mlp.fc2.weight": "model-00001-of-00002.safetensors",
59
+ "model.layers.11.self_attn.dense.bias": "model-00001-of-00002.safetensors",
60
+ "model.layers.11.self_attn.dense.weight": "model-00001-of-00002.safetensors",
61
+ "model.layers.11.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
62
+ "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
63
+ "model.layers.11.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
64
+ "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
65
+ "model.layers.11.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
66
+ "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
67
+ "model.layers.12.input_layernorm.bias": "model-00001-of-00002.safetensors",
68
+ "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors",
69
+ "model.layers.12.mlp.fc1.bias": "model-00001-of-00002.safetensors",
70
+ "model.layers.12.mlp.fc1.weight": "model-00001-of-00002.safetensors",
71
+ "model.layers.12.mlp.fc2.bias": "model-00001-of-00002.safetensors",
72
+ "model.layers.12.mlp.fc2.weight": "model-00001-of-00002.safetensors",
73
+ "model.layers.12.self_attn.dense.bias": "model-00001-of-00002.safetensors",
74
+ "model.layers.12.self_attn.dense.weight": "model-00001-of-00002.safetensors",
75
+ "model.layers.12.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
76
+ "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
77
+ "model.layers.12.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
78
+ "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
79
+ "model.layers.12.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
80
+ "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
81
+ "model.layers.13.input_layernorm.bias": "model-00001-of-00002.safetensors",
82
+ "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors",
83
+ "model.layers.13.mlp.fc1.bias": "model-00001-of-00002.safetensors",
84
+ "model.layers.13.mlp.fc1.weight": "model-00001-of-00002.safetensors",
85
+ "model.layers.13.mlp.fc2.bias": "model-00001-of-00002.safetensors",
86
+ "model.layers.13.mlp.fc2.weight": "model-00001-of-00002.safetensors",
87
+ "model.layers.13.self_attn.dense.bias": "model-00001-of-00002.safetensors",
88
+ "model.layers.13.self_attn.dense.weight": "model-00001-of-00002.safetensors",
89
+ "model.layers.13.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
90
+ "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
91
+ "model.layers.13.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
92
+ "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
93
+ "model.layers.13.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
94
+ "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
95
+ "model.layers.14.input_layernorm.bias": "model-00001-of-00002.safetensors",
96
+ "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors",
97
+ "model.layers.14.mlp.fc1.bias": "model-00001-of-00002.safetensors",
98
+ "model.layers.14.mlp.fc1.weight": "model-00001-of-00002.safetensors",
99
+ "model.layers.14.mlp.fc2.bias": "model-00001-of-00002.safetensors",
100
+ "model.layers.14.mlp.fc2.weight": "model-00001-of-00002.safetensors",
101
+ "model.layers.14.self_attn.dense.bias": "model-00001-of-00002.safetensors",
102
+ "model.layers.14.self_attn.dense.weight": "model-00001-of-00002.safetensors",
103
+ "model.layers.14.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
104
+ "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
105
+ "model.layers.14.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
106
+ "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
107
+ "model.layers.14.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
108
+ "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
109
+ "model.layers.15.input_layernorm.bias": "model-00001-of-00002.safetensors",
110
+ "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors",
111
+ "model.layers.15.mlp.fc1.bias": "model-00001-of-00002.safetensors",
112
+ "model.layers.15.mlp.fc1.weight": "model-00001-of-00002.safetensors",
113
+ "model.layers.15.mlp.fc2.bias": "model-00001-of-00002.safetensors",
114
+ "model.layers.15.mlp.fc2.weight": "model-00001-of-00002.safetensors",
115
+ "model.layers.15.self_attn.dense.bias": "model-00001-of-00002.safetensors",
116
+ "model.layers.15.self_attn.dense.weight": "model-00001-of-00002.safetensors",
117
+ "model.layers.15.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
118
+ "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
119
+ "model.layers.15.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
120
+ "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
121
+ "model.layers.15.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
122
+ "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
123
+ "model.layers.16.input_layernorm.bias": "model-00001-of-00002.safetensors",
124
+ "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors",
125
+ "model.layers.16.mlp.fc1.bias": "model-00001-of-00002.safetensors",
126
+ "model.layers.16.mlp.fc1.weight": "model-00001-of-00002.safetensors",
127
+ "model.layers.16.mlp.fc2.bias": "model-00001-of-00002.safetensors",
128
+ "model.layers.16.mlp.fc2.weight": "model-00001-of-00002.safetensors",
129
+ "model.layers.16.self_attn.dense.bias": "model-00001-of-00002.safetensors",
130
+ "model.layers.16.self_attn.dense.weight": "model-00001-of-00002.safetensors",
131
+ "model.layers.16.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
132
+ "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
133
+ "model.layers.16.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
134
+ "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
135
+ "model.layers.16.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
136
+ "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
137
+ "model.layers.17.input_layernorm.bias": "model-00001-of-00002.safetensors",
138
+ "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors",
139
+ "model.layers.17.mlp.fc1.bias": "model-00001-of-00002.safetensors",
140
+ "model.layers.17.mlp.fc1.weight": "model-00001-of-00002.safetensors",
141
+ "model.layers.17.mlp.fc2.bias": "model-00001-of-00002.safetensors",
142
+ "model.layers.17.mlp.fc2.weight": "model-00001-of-00002.safetensors",
143
+ "model.layers.17.self_attn.dense.bias": "model-00001-of-00002.safetensors",
144
+ "model.layers.17.self_attn.dense.weight": "model-00001-of-00002.safetensors",
145
+ "model.layers.17.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
146
+ "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
147
+ "model.layers.17.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
148
+ "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
149
+ "model.layers.17.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
150
+ "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
151
+ "model.layers.18.input_layernorm.bias": "model-00001-of-00002.safetensors",
152
+ "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors",
153
+ "model.layers.18.mlp.fc1.bias": "model-00001-of-00002.safetensors",
154
+ "model.layers.18.mlp.fc1.weight": "model-00001-of-00002.safetensors",
155
+ "model.layers.18.mlp.fc2.bias": "model-00001-of-00002.safetensors",
156
+ "model.layers.18.mlp.fc2.weight": "model-00001-of-00002.safetensors",
157
+ "model.layers.18.self_attn.dense.bias": "model-00001-of-00002.safetensors",
158
+ "model.layers.18.self_attn.dense.weight": "model-00001-of-00002.safetensors",
159
+ "model.layers.18.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
160
+ "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
161
+ "model.layers.18.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
162
+ "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
163
+ "model.layers.18.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
164
+ "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
165
+ "model.layers.19.input_layernorm.bias": "model-00001-of-00002.safetensors",
166
+ "model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors",
167
+ "model.layers.19.mlp.fc1.bias": "model-00001-of-00002.safetensors",
168
+ "model.layers.19.mlp.fc1.weight": "model-00001-of-00002.safetensors",
169
+ "model.layers.19.mlp.fc2.bias": "model-00001-of-00002.safetensors",
170
+ "model.layers.19.mlp.fc2.weight": "model-00001-of-00002.safetensors",
171
+ "model.layers.19.self_attn.dense.bias": "model-00001-of-00002.safetensors",
172
+ "model.layers.19.self_attn.dense.weight": "model-00001-of-00002.safetensors",
173
+ "model.layers.19.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
174
+ "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
175
+ "model.layers.19.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
176
+ "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
177
+ "model.layers.19.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
178
+ "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
179
+ "model.layers.2.input_layernorm.bias": "model-00001-of-00002.safetensors",
180
+ "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors",
181
+ "model.layers.2.mlp.fc1.bias": "model-00001-of-00002.safetensors",
182
+ "model.layers.2.mlp.fc1.weight": "model-00001-of-00002.safetensors",
183
+ "model.layers.2.mlp.fc2.bias": "model-00001-of-00002.safetensors",
184
+ "model.layers.2.mlp.fc2.weight": "model-00001-of-00002.safetensors",
185
+ "model.layers.2.self_attn.dense.bias": "model-00001-of-00002.safetensors",
186
+ "model.layers.2.self_attn.dense.weight": "model-00001-of-00002.safetensors",
187
+ "model.layers.2.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
188
+ "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
189
+ "model.layers.2.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
190
+ "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
191
+ "model.layers.2.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
192
+ "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
193
+ "model.layers.20.input_layernorm.bias": "model-00001-of-00002.safetensors",
194
+ "model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors",
195
+ "model.layers.20.mlp.fc1.bias": "model-00001-of-00002.safetensors",
196
+ "model.layers.20.mlp.fc1.weight": "model-00001-of-00002.safetensors",
197
+ "model.layers.20.mlp.fc2.bias": "model-00001-of-00002.safetensors",
198
+ "model.layers.20.mlp.fc2.weight": "model-00001-of-00002.safetensors",
199
+ "model.layers.20.self_attn.dense.bias": "model-00001-of-00002.safetensors",
200
+ "model.layers.20.self_attn.dense.weight": "model-00001-of-00002.safetensors",
201
+ "model.layers.20.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
202
+ "model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
203
+ "model.layers.20.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
204
+ "model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
205
+ "model.layers.20.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
206
+ "model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
207
+ "model.layers.21.input_layernorm.bias": "model-00001-of-00002.safetensors",
208
+ "model.layers.21.input_layernorm.weight": "model-00001-of-00002.safetensors",
209
+ "model.layers.21.mlp.fc1.bias": "model-00001-of-00002.safetensors",
210
+ "model.layers.21.mlp.fc1.weight": "model-00001-of-00002.safetensors",
211
+ "model.layers.21.mlp.fc2.bias": "model-00001-of-00002.safetensors",
212
+ "model.layers.21.mlp.fc2.weight": "model-00001-of-00002.safetensors",
213
+ "model.layers.21.self_attn.dense.bias": "model-00001-of-00002.safetensors",
214
+ "model.layers.21.self_attn.dense.weight": "model-00001-of-00002.safetensors",
215
+ "model.layers.21.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
216
+ "model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
217
+ "model.layers.21.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
218
+ "model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
219
+ "model.layers.21.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
220
+ "model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
221
+ "model.layers.22.input_layernorm.bias": "model-00001-of-00002.safetensors",
222
+ "model.layers.22.input_layernorm.weight": "model-00001-of-00002.safetensors",
223
+ "model.layers.22.mlp.fc1.bias": "model-00001-of-00002.safetensors",
224
+ "model.layers.22.mlp.fc1.weight": "model-00001-of-00002.safetensors",
225
+ "model.layers.22.mlp.fc2.bias": "model-00001-of-00002.safetensors",
226
+ "model.layers.22.mlp.fc2.weight": "model-00001-of-00002.safetensors",
227
+ "model.layers.22.self_attn.dense.bias": "model-00001-of-00002.safetensors",
228
+ "model.layers.22.self_attn.dense.weight": "model-00001-of-00002.safetensors",
229
+ "model.layers.22.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
230
+ "model.layers.22.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
231
+ "model.layers.22.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
232
+ "model.layers.22.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
233
+ "model.layers.22.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
234
+ "model.layers.22.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
235
+ "model.layers.23.input_layernorm.bias": "model-00001-of-00002.safetensors",
236
+ "model.layers.23.input_layernorm.weight": "model-00001-of-00002.safetensors",
237
+ "model.layers.23.mlp.fc1.bias": "model-00001-of-00002.safetensors",
238
+ "model.layers.23.mlp.fc1.weight": "model-00001-of-00002.safetensors",
239
+ "model.layers.23.mlp.fc2.bias": "model-00001-of-00002.safetensors",
240
+ "model.layers.23.mlp.fc2.weight": "model-00001-of-00002.safetensors",
241
+ "model.layers.23.self_attn.dense.bias": "model-00001-of-00002.safetensors",
242
+ "model.layers.23.self_attn.dense.weight": "model-00001-of-00002.safetensors",
243
+ "model.layers.23.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
244
+ "model.layers.23.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
245
+ "model.layers.23.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
246
+ "model.layers.23.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
247
+ "model.layers.23.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
248
+ "model.layers.23.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
249
+ "model.layers.24.input_layernorm.bias": "model-00001-of-00002.safetensors",
250
+ "model.layers.24.input_layernorm.weight": "model-00001-of-00002.safetensors",
251
+ "model.layers.24.mlp.fc1.bias": "model-00001-of-00002.safetensors",
252
+ "model.layers.24.mlp.fc1.weight": "model-00001-of-00002.safetensors",
253
+ "model.layers.24.mlp.fc2.bias": "model-00001-of-00002.safetensors",
254
+ "model.layers.24.mlp.fc2.weight": "model-00001-of-00002.safetensors",
255
+ "model.layers.24.self_attn.dense.bias": "model-00001-of-00002.safetensors",
256
+ "model.layers.24.self_attn.dense.weight": "model-00001-of-00002.safetensors",
257
+ "model.layers.24.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
258
+ "model.layers.24.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
259
+ "model.layers.24.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
260
+ "model.layers.24.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
261
+ "model.layers.24.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
262
+ "model.layers.24.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
263
+ "model.layers.25.input_layernorm.bias": "model-00001-of-00002.safetensors",
264
+ "model.layers.25.input_layernorm.weight": "model-00001-of-00002.safetensors",
265
+ "model.layers.25.mlp.fc1.bias": "model-00001-of-00002.safetensors",
266
+ "model.layers.25.mlp.fc1.weight": "model-00001-of-00002.safetensors",
267
+ "model.layers.25.mlp.fc2.bias": "model-00001-of-00002.safetensors",
268
+ "model.layers.25.mlp.fc2.weight": "model-00001-of-00002.safetensors",
269
+ "model.layers.25.self_attn.dense.bias": "model-00001-of-00002.safetensors",
270
+ "model.layers.25.self_attn.dense.weight": "model-00001-of-00002.safetensors",
271
+ "model.layers.25.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
272
+ "model.layers.25.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
273
+ "model.layers.25.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
274
+ "model.layers.25.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
275
+ "model.layers.25.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
276
+ "model.layers.25.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
277
+ "model.layers.26.input_layernorm.bias": "model-00001-of-00002.safetensors",
278
+ "model.layers.26.input_layernorm.weight": "model-00001-of-00002.safetensors",
279
+ "model.layers.26.mlp.fc1.bias": "model-00001-of-00002.safetensors",
280
+ "model.layers.26.mlp.fc1.weight": "model-00001-of-00002.safetensors",
281
+ "model.layers.26.mlp.fc2.bias": "model-00001-of-00002.safetensors",
282
+ "model.layers.26.mlp.fc2.weight": "model-00001-of-00002.safetensors",
283
+ "model.layers.26.self_attn.dense.bias": "model-00001-of-00002.safetensors",
284
+ "model.layers.26.self_attn.dense.weight": "model-00001-of-00002.safetensors",
285
+ "model.layers.26.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
286
+ "model.layers.26.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
287
+ "model.layers.26.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
288
+ "model.layers.26.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
289
+ "model.layers.26.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
290
+ "model.layers.26.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
291
+ "model.layers.27.input_layernorm.bias": "model-00001-of-00002.safetensors",
292
+ "model.layers.27.input_layernorm.weight": "model-00001-of-00002.safetensors",
293
+ "model.layers.27.mlp.fc1.bias": "model-00001-of-00002.safetensors",
294
+ "model.layers.27.mlp.fc1.weight": "model-00001-of-00002.safetensors",
295
+ "model.layers.27.mlp.fc2.bias": "model-00001-of-00002.safetensors",
296
+ "model.layers.27.mlp.fc2.weight": "model-00001-of-00002.safetensors",
297
+ "model.layers.27.self_attn.dense.bias": "model-00001-of-00002.safetensors",
298
+ "model.layers.27.self_attn.dense.weight": "model-00001-of-00002.safetensors",
299
+ "model.layers.27.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
300
+ "model.layers.27.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
301
+ "model.layers.27.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
302
+ "model.layers.27.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
303
+ "model.layers.27.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
304
+ "model.layers.27.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
305
+ "model.layers.28.input_layernorm.bias": "model-00001-of-00002.safetensors",
306
+ "model.layers.28.input_layernorm.weight": "model-00001-of-00002.safetensors",
307
+ "model.layers.28.mlp.fc1.bias": "model-00001-of-00002.safetensors",
308
+ "model.layers.28.mlp.fc1.weight": "model-00001-of-00002.safetensors",
309
+ "model.layers.28.mlp.fc2.bias": "model-00001-of-00002.safetensors",
310
+ "model.layers.28.mlp.fc2.weight": "model-00001-of-00002.safetensors",
311
+ "model.layers.28.self_attn.dense.bias": "model-00001-of-00002.safetensors",
312
+ "model.layers.28.self_attn.dense.weight": "model-00001-of-00002.safetensors",
313
+ "model.layers.28.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
314
+ "model.layers.28.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
315
+ "model.layers.28.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
316
+ "model.layers.28.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
317
+ "model.layers.28.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
318
+ "model.layers.28.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
319
+ "model.layers.29.input_layernorm.bias": "model-00001-of-00002.safetensors",
320
+ "model.layers.29.input_layernorm.weight": "model-00001-of-00002.safetensors",
321
+ "model.layers.29.mlp.fc1.bias": "model-00001-of-00002.safetensors",
322
+ "model.layers.29.mlp.fc1.weight": "model-00001-of-00002.safetensors",
323
+ "model.layers.29.mlp.fc2.bias": "model-00001-of-00002.safetensors",
324
+ "model.layers.29.mlp.fc2.weight": "model-00001-of-00002.safetensors",
325
+ "model.layers.29.self_attn.dense.bias": "model-00001-of-00002.safetensors",
326
+ "model.layers.29.self_attn.dense.weight": "model-00001-of-00002.safetensors",
327
+ "model.layers.29.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
328
+ "model.layers.29.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
329
+ "model.layers.29.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
330
+ "model.layers.29.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
331
+ "model.layers.29.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
332
+ "model.layers.29.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
333
+ "model.layers.3.input_layernorm.bias": "model-00001-of-00002.safetensors",
334
+ "model.layers.3.input_layernorm.weight": "model-00001-of-00002.safetensors",
335
+ "model.layers.3.mlp.fc1.bias": "model-00001-of-00002.safetensors",
336
+ "model.layers.3.mlp.fc1.weight": "model-00001-of-00002.safetensors",
337
+ "model.layers.3.mlp.fc2.bias": "model-00001-of-00002.safetensors",
338
+ "model.layers.3.mlp.fc2.weight": "model-00001-of-00002.safetensors",
339
+ "model.layers.3.self_attn.dense.bias": "model-00001-of-00002.safetensors",
340
+ "model.layers.3.self_attn.dense.weight": "model-00001-of-00002.safetensors",
341
+ "model.layers.3.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
342
+ "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
343
+ "model.layers.3.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
344
+ "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
345
+ "model.layers.3.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
346
+ "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
347
+ "model.layers.30.input_layernorm.bias": "model-00002-of-00002.safetensors",
348
+ "model.layers.30.input_layernorm.weight": "model-00002-of-00002.safetensors",
349
+ "model.layers.30.mlp.fc1.bias": "model-00002-of-00002.safetensors",
350
+ "model.layers.30.mlp.fc1.weight": "model-00002-of-00002.safetensors",
351
+ "model.layers.30.mlp.fc2.bias": "model-00002-of-00002.safetensors",
352
+ "model.layers.30.mlp.fc2.weight": "model-00002-of-00002.safetensors",
353
+ "model.layers.30.self_attn.dense.bias": "model-00002-of-00002.safetensors",
354
+ "model.layers.30.self_attn.dense.weight": "model-00002-of-00002.safetensors",
355
+ "model.layers.30.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
356
+ "model.layers.30.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
357
+ "model.layers.30.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
358
+ "model.layers.30.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
359
+ "model.layers.30.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
360
+ "model.layers.30.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
361
+ "model.layers.31.input_layernorm.bias": "model-00002-of-00002.safetensors",
362
+ "model.layers.31.input_layernorm.weight": "model-00002-of-00002.safetensors",
363
+ "model.layers.31.mlp.fc1.bias": "model-00002-of-00002.safetensors",
364
+ "model.layers.31.mlp.fc1.weight": "model-00002-of-00002.safetensors",
365
+ "model.layers.31.mlp.fc2.bias": "model-00002-of-00002.safetensors",
366
+ "model.layers.31.mlp.fc2.weight": "model-00002-of-00002.safetensors",
367
+ "model.layers.31.self_attn.dense.bias": "model-00002-of-00002.safetensors",
368
+ "model.layers.31.self_attn.dense.weight": "model-00002-of-00002.safetensors",
369
+ "model.layers.31.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
370
+ "model.layers.31.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
371
+ "model.layers.31.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
372
+ "model.layers.31.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
373
+ "model.layers.31.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
374
+ "model.layers.31.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
375
+ "model.layers.4.input_layernorm.bias": "model-00001-of-00002.safetensors",
376
+ "model.layers.4.input_layernorm.weight": "model-00001-of-00002.safetensors",
377
+ "model.layers.4.mlp.fc1.bias": "model-00001-of-00002.safetensors",
378
+ "model.layers.4.mlp.fc1.weight": "model-00001-of-00002.safetensors",
379
+ "model.layers.4.mlp.fc2.bias": "model-00001-of-00002.safetensors",
380
+ "model.layers.4.mlp.fc2.weight": "model-00001-of-00002.safetensors",
381
+ "model.layers.4.self_attn.dense.bias": "model-00001-of-00002.safetensors",
382
+ "model.layers.4.self_attn.dense.weight": "model-00001-of-00002.safetensors",
383
+ "model.layers.4.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
384
+ "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
385
+ "model.layers.4.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
386
+ "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
387
+ "model.layers.4.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
388
+ "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
389
+ "model.layers.5.input_layernorm.bias": "model-00001-of-00002.safetensors",
390
+ "model.layers.5.input_layernorm.weight": "model-00001-of-00002.safetensors",
391
+ "model.layers.5.mlp.fc1.bias": "model-00001-of-00002.safetensors",
392
+ "model.layers.5.mlp.fc1.weight": "model-00001-of-00002.safetensors",
393
+ "model.layers.5.mlp.fc2.bias": "model-00001-of-00002.safetensors",
394
+ "model.layers.5.mlp.fc2.weight": "model-00001-of-00002.safetensors",
395
+ "model.layers.5.self_attn.dense.bias": "model-00001-of-00002.safetensors",
396
+ "model.layers.5.self_attn.dense.weight": "model-00001-of-00002.safetensors",
397
+ "model.layers.5.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
398
+ "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
399
+ "model.layers.5.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
400
+ "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
401
+ "model.layers.5.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
402
+ "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
403
+ "model.layers.6.input_layernorm.bias": "model-00001-of-00002.safetensors",
404
+ "model.layers.6.input_layernorm.weight": "model-00001-of-00002.safetensors",
405
+ "model.layers.6.mlp.fc1.bias": "model-00001-of-00002.safetensors",
406
+ "model.layers.6.mlp.fc1.weight": "model-00001-of-00002.safetensors",
407
+ "model.layers.6.mlp.fc2.bias": "model-00001-of-00002.safetensors",
408
+ "model.layers.6.mlp.fc2.weight": "model-00001-of-00002.safetensors",
409
+ "model.layers.6.self_attn.dense.bias": "model-00001-of-00002.safetensors",
410
+ "model.layers.6.self_attn.dense.weight": "model-00001-of-00002.safetensors",
411
+ "model.layers.6.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
412
+ "model.layers.6.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
413
+ "model.layers.6.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
414
+ "model.layers.6.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
415
+ "model.layers.6.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
416
+ "model.layers.6.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
417
+ "model.layers.7.input_layernorm.bias": "model-00001-of-00002.safetensors",
418
+ "model.layers.7.input_layernorm.weight": "model-00001-of-00002.safetensors",
419
+ "model.layers.7.mlp.fc1.bias": "model-00001-of-00002.safetensors",
420
+ "model.layers.7.mlp.fc1.weight": "model-00001-of-00002.safetensors",
421
+ "model.layers.7.mlp.fc2.bias": "model-00001-of-00002.safetensors",
422
+ "model.layers.7.mlp.fc2.weight": "model-00001-of-00002.safetensors",
423
+ "model.layers.7.self_attn.dense.bias": "model-00001-of-00002.safetensors",
424
+ "model.layers.7.self_attn.dense.weight": "model-00001-of-00002.safetensors",
425
+ "model.layers.7.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
426
+ "model.layers.7.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
427
+ "model.layers.7.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
428
+ "model.layers.7.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
429
+ "model.layers.7.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
430
+ "model.layers.7.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
431
+ "model.layers.8.input_layernorm.bias": "model-00001-of-00002.safetensors",
432
+ "model.layers.8.input_layernorm.weight": "model-00001-of-00002.safetensors",
433
+ "model.layers.8.mlp.fc1.bias": "model-00001-of-00002.safetensors",
434
+ "model.layers.8.mlp.fc1.weight": "model-00001-of-00002.safetensors",
435
+ "model.layers.8.mlp.fc2.bias": "model-00001-of-00002.safetensors",
436
+ "model.layers.8.mlp.fc2.weight": "model-00001-of-00002.safetensors",
437
+ "model.layers.8.self_attn.dense.bias": "model-00001-of-00002.safetensors",
438
+ "model.layers.8.self_attn.dense.weight": "model-00001-of-00002.safetensors",
439
+ "model.layers.8.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
440
+ "model.layers.8.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
441
+ "model.layers.8.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
442
+ "model.layers.8.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
443
+ "model.layers.8.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
444
+ "model.layers.8.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
445
+ "model.layers.9.input_layernorm.bias": "model-00001-of-00002.safetensors",
446
+ "model.layers.9.input_layernorm.weight": "model-00001-of-00002.safetensors",
447
+ "model.layers.9.mlp.fc1.bias": "model-00001-of-00002.safetensors",
448
+ "model.layers.9.mlp.fc1.weight": "model-00001-of-00002.safetensors",
449
+ "model.layers.9.mlp.fc2.bias": "model-00001-of-00002.safetensors",
450
+ "model.layers.9.mlp.fc2.weight": "model-00001-of-00002.safetensors",
451
+ "model.layers.9.self_attn.dense.bias": "model-00001-of-00002.safetensors",
452
+ "model.layers.9.self_attn.dense.weight": "model-00001-of-00002.safetensors",
453
+ "model.layers.9.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
454
+ "model.layers.9.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
455
+ "model.layers.9.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
456
+ "model.layers.9.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
457
+ "model.layers.9.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
458
+ "model.layers.9.self_attn.v_proj.weight": "model-00001-of-00002.safetensors"
459
+ }
460
+ }
modeling_phi.py ADDED
@@ -0,0 +1,1369 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 Microsoft and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """ PyTorch Phi model."""
17
+
18
+
19
+ import math
20
+ from typing import List, Optional, Tuple, Union
21
+
22
+ import torch
23
+ import torch.nn.functional as F
24
+ import torch.utils.checkpoint
25
+ from torch import nn
26
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
27
+
28
+ from transformers.activations import ACT2FN
29
+ from transformers.cache_utils import Cache, DynamicCache
30
+ from transformers.modeling_attn_mask_utils import _prepare_4d_causal_attention_mask
31
+ from transformers.modeling_outputs import (
32
+ BaseModelOutputWithPast,
33
+ CausalLMOutputWithPast,
34
+ SequenceClassifierOutputWithPast,
35
+ TokenClassifierOutput,
36
+ )
37
+ from transformers.modeling_utils import PreTrainedModel
38
+ from transformers.utils import (
39
+ add_code_sample_docstrings,
40
+ add_start_docstrings,
41
+ add_start_docstrings_to_model_forward,
42
+ is_flash_attn_2_available,
43
+ is_flash_attn_greater_or_equal_2_10,
44
+ logging,
45
+ replace_return_docstrings,
46
+ )
47
+ from .configuration_phi import PhiConfig
48
+
49
+
50
+ try:
51
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
52
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
53
+ except:
54
+ pass
55
+
56
+
57
+ logger = logging.get_logger(__name__)
58
+
59
+ _CHECKPOINT_FOR_DOC = "microsoft/phi-2"
60
+ _CONFIG_FOR_DOC = "PhiConfig"
61
+
62
+ PHI_PRETRAINED_MODEL_ARCHIVE_LIST = [
63
+ "microsoft/phi-2",
64
+ # See all Phi models at https://huggingface.co/models?filter=phi
65
+ ]
66
+
67
+
68
+ # Copied from transformers.models.llama.modeling_llama._get_unpad_data
69
+ def _get_unpad_data(attention_mask):
70
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
71
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
72
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
73
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
74
+ return (
75
+ indices,
76
+ cu_seqlens,
77
+ max_seqlen_in_batch,
78
+ )
79
+
80
+
81
+ # Copied from transformers.models.llama.modeling_llama.LlamaRotaryEmbedding with Llama->Phi
82
+ class PhiRotaryEmbedding(nn.Module):
83
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
84
+ super().__init__()
85
+
86
+ self.dim = dim
87
+ self.max_position_embeddings = max_position_embeddings
88
+ self.base = base
89
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
90
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
91
+
92
+ # Build here to make `torch.jit.trace` work.
93
+ self._set_cos_sin_cache(
94
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
95
+ )
96
+
97
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
98
+ self.max_seq_len_cached = seq_len
99
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
100
+
101
+ freqs = torch.outer(t, self.inv_freq)
102
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
103
+ emb = torch.cat((freqs, freqs), dim=-1)
104
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
105
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
106
+
107
+ def forward(self, x, seq_len=None):
108
+ # x: [bs, num_attention_heads, seq_len, head_size]
109
+ if seq_len > self.max_seq_len_cached:
110
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
111
+
112
+ return (
113
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
114
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
115
+ )
116
+
117
+
118
+ # Copied from transformers.models.llama.modeling_llama.LlamaLinearScalingRotaryEmbedding with Llama->Phi
119
+ class PhiLinearScalingRotaryEmbedding(PhiRotaryEmbedding):
120
+ """PhiRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
121
+
122
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
123
+ self.scaling_factor = scaling_factor
124
+ super().__init__(dim, max_position_embeddings, base, device)
125
+
126
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
127
+ self.max_seq_len_cached = seq_len
128
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
129
+ t = t / self.scaling_factor
130
+
131
+ freqs = torch.outer(t, self.inv_freq)
132
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
133
+ emb = torch.cat((freqs, freqs), dim=-1)
134
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
135
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
136
+
137
+
138
+ # Copied from transformers.models.llama.modeling_llama.LlamaDynamicNTKScalingRotaryEmbedding with Llama->Phi
139
+ class PhiDynamicNTKScalingRotaryEmbedding(PhiRotaryEmbedding):
140
+ """PhiRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
141
+
142
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
143
+ self.scaling_factor = scaling_factor
144
+ super().__init__(dim, max_position_embeddings, base, device)
145
+
146
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
147
+ self.max_seq_len_cached = seq_len
148
+
149
+ if seq_len > self.max_position_embeddings:
150
+ base = self.base * (
151
+ (self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
152
+ ) ** (self.dim / (self.dim - 2))
153
+ inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
154
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
155
+
156
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
157
+
158
+ freqs = torch.outer(t, self.inv_freq)
159
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
160
+ emb = torch.cat((freqs, freqs), dim=-1)
161
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
162
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
163
+
164
+
165
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
166
+ def rotate_half(x):
167
+ """Rotates half the hidden dims of the input."""
168
+ x1 = x[..., : x.shape[-1] // 2]
169
+ x2 = x[..., x.shape[-1] // 2 :]
170
+ return torch.cat((-x2, x1), dim=-1)
171
+
172
+
173
+ # Copied from transformers.models.llama.modeling_llama.apply_rotary_pos_emb
174
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
175
+ """Applies Rotary Position Embedding to the query and key tensors.
176
+
177
+ Args:
178
+ q (`torch.Tensor`): The query tensor.
179
+ k (`torch.Tensor`): The key tensor.
180
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
181
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
182
+ position_ids (`torch.Tensor`):
183
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
184
+ used to pass offsetted position ids when working with a KV-cache.
185
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
186
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
187
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
188
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
189
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
190
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
191
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
192
+ Returns:
193
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
194
+ """
195
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
196
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
197
+ q_embed = (q * cos) + (rotate_half(q) * sin)
198
+ k_embed = (k * cos) + (rotate_half(k) * sin)
199
+ return q_embed, k_embed
200
+
201
+
202
+ # Copied from transformers.models.clip.modeling_clip.CLIPMLP with CLIP->Phi
203
+ class PhiMLP(nn.Module):
204
+ def __init__(self, config):
205
+ super().__init__()
206
+ self.config = config
207
+ self.activation_fn = ACT2FN[config.hidden_act]
208
+ self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size)
209
+ self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size)
210
+
211
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
212
+ hidden_states = self.fc1(hidden_states)
213
+ hidden_states = self.activation_fn(hidden_states)
214
+ hidden_states = self.fc2(hidden_states)
215
+ return hidden_states
216
+
217
+
218
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv with llama->phi
219
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
220
+ """
221
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
222
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
223
+ """
224
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
225
+ if n_rep == 1:
226
+ return hidden_states
227
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
228
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
229
+
230
+
231
+ class PhiAttention(nn.Module):
232
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
233
+
234
+ def __init__(self, config: PhiConfig, layer_idx: Optional[int] = None):
235
+ super().__init__()
236
+ self.config = config
237
+ self.layer_idx = layer_idx
238
+ if layer_idx is None:
239
+ logger.warning_once(
240
+ f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
241
+ "to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
242
+ "when creating this class."
243
+ )
244
+
245
+ self.attention_dropout = config.attention_dropout
246
+ self.hidden_size = config.hidden_size
247
+ self.num_heads = config.num_attention_heads
248
+ self.head_dim = self.hidden_size // self.num_heads
249
+ self.num_key_value_heads = config.num_key_value_heads
250
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
251
+ self.max_position_embeddings = config.max_position_embeddings
252
+ self.rope_theta = config.rope_theta
253
+ self.partial_rotary_factor = config.partial_rotary_factor
254
+ self.is_causal = True
255
+
256
+ if (self.head_dim * self.num_heads) != self.hidden_size:
257
+ raise ValueError(
258
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
259
+ f" and `num_heads`: {self.num_heads})."
260
+ )
261
+
262
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=True)
263
+ self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
264
+ self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
265
+ self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=True)
266
+
267
+ self.qk_layernorm = config.qk_layernorm
268
+ if self.qk_layernorm:
269
+ self.q_layernorm = nn.LayerNorm(
270
+ config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True
271
+ )
272
+ self.k_layernorm = nn.LayerNorm(
273
+ config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True
274
+ )
275
+
276
+ self._init_rope()
277
+
278
+ def _init_rope(self):
279
+ if self.config.rope_scaling is None:
280
+ self.rotary_emb = PhiRotaryEmbedding(
281
+ int(self.partial_rotary_factor * self.head_dim),
282
+ max_position_embeddings=self.max_position_embeddings,
283
+ base=self.rope_theta,
284
+ )
285
+ else:
286
+ scaling_type = self.config.rope_scaling["type"]
287
+ scaling_factor = self.config.rope_scaling["factor"]
288
+ if scaling_type == "linear":
289
+ self.rotary_emb = PhiLinearScalingRotaryEmbedding(
290
+ int(self.partial_rotary_factor * self.head_dim),
291
+ max_position_embeddings=self.max_position_embeddings,
292
+ scaling_factor=scaling_factor,
293
+ base=self.rope_theta,
294
+ )
295
+ elif scaling_type == "dynamic":
296
+ self.rotary_emb = PhiDynamicNTKScalingRotaryEmbedding(
297
+ int(self.partial_rotary_factor * self.head_dim),
298
+ max_position_embeddings=self.max_position_embeddings,
299
+ scaling_factor=scaling_factor,
300
+ base=self.rope_theta,
301
+ )
302
+ else:
303
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
304
+
305
+ # Phi-2 has an attention overflow issue (with FP16) and requires autocast to be disabled
306
+ @torch.autocast("cpu", enabled=False)
307
+ @torch.autocast("cuda", enabled=False)
308
+ def forward(
309
+ self,
310
+ hidden_states: torch.Tensor,
311
+ attention_mask: Optional[torch.Tensor] = None,
312
+ position_ids: Optional[torch.LongTensor] = None,
313
+ past_key_value: Optional[Cache] = None,
314
+ output_attentions: bool = False,
315
+ use_cache: bool = False,
316
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
317
+ bsz, q_len, _ = hidden_states.size()
318
+
319
+ query_states = self.q_proj(hidden_states)
320
+ key_states = self.k_proj(hidden_states)
321
+ value_states = self.v_proj(hidden_states)
322
+
323
+ if self.qk_layernorm:
324
+ query_states = self.q_layernorm(query_states)
325
+ key_states = self.k_layernorm(key_states)
326
+
327
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
328
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
329
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
330
+
331
+ kv_seq_len = key_states.shape[-2]
332
+ if past_key_value is not None:
333
+ if self.layer_idx is None:
334
+ raise ValueError(
335
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
336
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
337
+ "with a layer index."
338
+ )
339
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
340
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
341
+
342
+ # Partial rotary embedding
343
+ query_rot, query_pass = (
344
+ query_states[..., : self.rotary_emb.dim],
345
+ query_states[..., self.rotary_emb.dim :],
346
+ )
347
+ key_rot, key_pass = (
348
+ key_states[..., : self.rotary_emb.dim],
349
+ key_states[..., self.rotary_emb.dim :],
350
+ )
351
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
352
+ query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
353
+
354
+ # [batch_size, seq_length, num_heads, head_dim]
355
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
356
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
357
+
358
+ if past_key_value is not None:
359
+ cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim}
360
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
361
+
362
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
363
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
364
+
365
+ # Queries and keys upcast to fp32 is required by Phi-2 to avoid overflow
366
+ attn_weights = torch.matmul(
367
+ query_states.to(torch.float32), key_states.to(torch.float32).transpose(2, 3)
368
+ ) / math.sqrt(self.head_dim)
369
+
370
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
371
+ raise ValueError(
372
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
373
+ f" {attn_weights.size()}"
374
+ )
375
+
376
+ if attention_mask is not None:
377
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
378
+ raise ValueError(
379
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
380
+ )
381
+ attn_weights = attn_weights + attention_mask
382
+
383
+ # upcast attention to fp32
384
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(value_states.dtype)
385
+ attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
386
+
387
+ attn_output = torch.matmul(attn_weights, value_states)
388
+
389
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
390
+ raise ValueError(
391
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
392
+ f" {attn_output.size()}"
393
+ )
394
+
395
+ attn_output = attn_output.transpose(1, 2).contiguous()
396
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
397
+
398
+ attn_output = self.dense(attn_output)
399
+
400
+ if not output_attentions:
401
+ attn_weights = None
402
+
403
+ return attn_output, attn_weights, past_key_value
404
+
405
+
406
+ class PhiFlashAttention2(PhiAttention):
407
+ """
408
+ Phi flash attention module. This module inherits from `PhiAttention` as the weights of the module stays
409
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
410
+ flash attention and deal with padding tokens in case the input contains any of them.
411
+ """
412
+
413
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
414
+ def __init__(self, *args, **kwargs):
415
+ super().__init__(*args, **kwargs)
416
+
417
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
418
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, 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.
419
+ # 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).
420
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
421
+
422
+ def forward(
423
+ self,
424
+ hidden_states: torch.Tensor,
425
+ attention_mask: Optional[torch.LongTensor] = None,
426
+ position_ids: Optional[torch.LongTensor] = None,
427
+ past_key_value: Optional[Cache] = None,
428
+ output_attentions: bool = False,
429
+ use_cache: bool = False,
430
+ **kwargs,
431
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
432
+ # PhiFlashAttention2 attention does not support output_attentions
433
+
434
+ output_attentions = False
435
+
436
+ bsz, q_len, _ = hidden_states.size()
437
+
438
+ query_states = self.q_proj(hidden_states)
439
+ key_states = self.k_proj(hidden_states)
440
+ value_states = self.v_proj(hidden_states)
441
+
442
+ if self.qk_layernorm:
443
+ query_states = self.q_layernorm(query_states)
444
+ key_states = self.k_layernorm(key_states)
445
+
446
+ # Flash attention requires the input to have the shape
447
+ # batch_size x seq_length x head_dim x hidden_dim
448
+ # therefore we just need to keep the original shape
449
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
450
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
451
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
452
+
453
+ kv_seq_len = key_states.shape[-2]
454
+ if past_key_value is not None:
455
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
456
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
457
+
458
+ # Partial rotary embedding
459
+ query_rot, query_pass = (
460
+ query_states[..., : self.rotary_emb.dim],
461
+ query_states[..., self.rotary_emb.dim :],
462
+ )
463
+ key_rot, key_pass = (
464
+ key_states[..., : self.rotary_emb.dim],
465
+ key_states[..., self.rotary_emb.dim :],
466
+ )
467
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
468
+ query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
469
+
470
+ # [batch_size, seq_length, num_heads, head_dim]
471
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
472
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
473
+
474
+ if past_key_value is not None:
475
+ cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim}
476
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
477
+
478
+ # 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
479
+ # to be able to avoid many of these transpose/reshape/view.
480
+ query_states = query_states.transpose(1, 2)
481
+ key_states = key_states.transpose(1, 2)
482
+ value_states = value_states.transpose(1, 2)
483
+
484
+ attn_dropout = self.attention_dropout if self.training else 0.0
485
+
486
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
487
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
488
+ # cast them back in the correct dtype just to be sure everything works as expected.
489
+ # This might slowdown training & inference so it is recommended to not cast the LayerNorms
490
+ # in fp32.
491
+
492
+ if query_states.dtype == torch.float32:
493
+ if torch.is_autocast_enabled():
494
+ target_dtype = torch.get_autocast_gpu_dtype()
495
+ # Handle the case where the model is quantized
496
+ elif hasattr(self.config, "_pre_quantization_dtype"):
497
+ target_dtype = self.config._pre_quantization_dtype
498
+ else:
499
+ target_dtype = self.q_proj.weight.dtype
500
+
501
+ logger.warning_once(
502
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
503
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
504
+ f" {target_dtype}."
505
+ )
506
+
507
+ query_states = query_states.to(target_dtype)
508
+ key_states = key_states.to(target_dtype)
509
+ value_states = value_states.to(target_dtype)
510
+
511
+ attn_output = self._flash_attention_forward(
512
+ query_states, key_states, value_states, attention_mask, q_len, dropout=attn_dropout, softmax_scale=None
513
+ )
514
+
515
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
516
+ attn_output = self.dense(attn_output)
517
+
518
+ if not output_attentions:
519
+ attn_weights = None
520
+
521
+ return attn_output, attn_weights, past_key_value
522
+
523
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._flash_attention_forward
524
+ def _flash_attention_forward(
525
+ self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
526
+ ):
527
+ """
528
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
529
+ first unpad the input, then computes the attention scores and pad the final attention scores.
530
+
531
+ Args:
532
+ query_states (`torch.Tensor`):
533
+ Input query states to be passed to Flash Attention API
534
+ key_states (`torch.Tensor`):
535
+ Input key states to be passed to Flash Attention API
536
+ value_states (`torch.Tensor`):
537
+ Input value states to be passed to Flash Attention API
538
+ attention_mask (`torch.Tensor`):
539
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
540
+ position of padding tokens and 1 for the position of non-padding tokens.
541
+ dropout (`int`, *optional*):
542
+ Attention dropout
543
+ softmax_scale (`float`, *optional*):
544
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
545
+ """
546
+ if not self._flash_attn_uses_top_left_mask:
547
+ causal = self.is_causal
548
+ else:
549
+ # 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__.
550
+ causal = self.is_causal and query_length != 1
551
+
552
+ # Contains at least one padding token in the sequence
553
+ if attention_mask is not None:
554
+ batch_size = query_states.shape[0]
555
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
556
+ query_states, key_states, value_states, attention_mask, query_length
557
+ )
558
+
559
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
560
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
561
+
562
+ attn_output_unpad = flash_attn_varlen_func(
563
+ query_states,
564
+ key_states,
565
+ value_states,
566
+ cu_seqlens_q=cu_seqlens_q,
567
+ cu_seqlens_k=cu_seqlens_k,
568
+ max_seqlen_q=max_seqlen_in_batch_q,
569
+ max_seqlen_k=max_seqlen_in_batch_k,
570
+ dropout_p=dropout,
571
+ softmax_scale=softmax_scale,
572
+ causal=causal,
573
+ )
574
+
575
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
576
+ else:
577
+ attn_output = flash_attn_func(
578
+ query_states, key_states, value_states, dropout, softmax_scale=softmax_scale, causal=causal
579
+ )
580
+
581
+ return attn_output
582
+
583
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._upad_input
584
+ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
585
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
586
+ batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
587
+
588
+ key_layer = index_first_axis(
589
+ key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
590
+ )
591
+ value_layer = index_first_axis(
592
+ value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
593
+ )
594
+ if query_length == kv_seq_len:
595
+ query_layer = index_first_axis(
596
+ query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim), indices_k
597
+ )
598
+ cu_seqlens_q = cu_seqlens_k
599
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
600
+ indices_q = indices_k
601
+ elif query_length == 1:
602
+ max_seqlen_in_batch_q = 1
603
+ cu_seqlens_q = torch.arange(
604
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
605
+ ) # There is a memcpy here, that is very bad.
606
+ indices_q = cu_seqlens_q[:-1]
607
+ query_layer = query_layer.squeeze(1)
608
+ else:
609
+ # The -q_len: slice assumes left padding.
610
+ attention_mask = attention_mask[:, -query_length:]
611
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
612
+
613
+ return (
614
+ query_layer,
615
+ key_layer,
616
+ value_layer,
617
+ indices_q,
618
+ (cu_seqlens_q, cu_seqlens_k),
619
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
620
+ )
621
+
622
+
623
+ PHI_ATTENTION_CLASSES = {
624
+ "eager": PhiAttention,
625
+ "flash_attention_2": PhiFlashAttention2,
626
+ }
627
+
628
+
629
+ class PhiDecoderLayer(nn.Module):
630
+ def __init__(self, config: PhiConfig, layer_idx: int):
631
+ super().__init__()
632
+ self.self_attn = PHI_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx=layer_idx)
633
+ self.mlp = PhiMLP(config)
634
+ self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
635
+ self.resid_dropout = nn.Dropout(config.resid_pdrop)
636
+
637
+ def forward(
638
+ self,
639
+ hidden_states: torch.Tensor,
640
+ attention_mask: Optional[torch.Tensor] = None,
641
+ position_ids: Optional[torch.LongTensor] = None,
642
+ output_attentions: Optional[bool] = False,
643
+ use_cache: Optional[bool] = False,
644
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
645
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
646
+ """
647
+ Args:
648
+ hidden_states (`torch.FloatTensor`):
649
+ input to the layer of shape `(batch, seq_len, embed_dim)`
650
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
651
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
652
+ position_ids (`torch.LongTensor` of shape `({0})`, *optional*):
653
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range
654
+ `[0, config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids)
655
+ output_attentions (`bool`, *optional*):
656
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
657
+ returned tensors for more detail.
658
+ use_cache (`bool`, *optional*):
659
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
660
+ (see `past_key_values`).
661
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
662
+ """
663
+
664
+ residual = hidden_states
665
+
666
+ hidden_states = self.input_layernorm(hidden_states)
667
+
668
+ # Self Attention
669
+ attn_outputs, self_attn_weights, present_key_value = self.self_attn(
670
+ hidden_states=hidden_states,
671
+ attention_mask=attention_mask,
672
+ position_ids=position_ids,
673
+ past_key_value=past_key_value,
674
+ output_attentions=output_attentions,
675
+ use_cache=use_cache,
676
+ )
677
+ attn_outputs = self.resid_dropout(attn_outputs)
678
+
679
+ feed_forward_hidden_states = self.resid_dropout(self.mlp(hidden_states))
680
+ hidden_states = attn_outputs + feed_forward_hidden_states + residual
681
+ outputs = (hidden_states,)
682
+
683
+ if output_attentions:
684
+ outputs += (self_attn_weights,)
685
+
686
+ if use_cache:
687
+ outputs += (present_key_value,)
688
+
689
+ return outputs
690
+
691
+
692
+ PHI_START_DOCSTRING = r"""
693
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
694
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
695
+ etc.)
696
+
697
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
698
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
699
+ and behavior.
700
+
701
+ Parameters:
702
+ config ([`PhiConfig`]):
703
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
704
+ load the weights associated with the model, only the configuration. Check out the
705
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
706
+ """
707
+
708
+
709
+ @add_start_docstrings(
710
+ "The bare Phi Model outputting raw hidden-states without any specific head on top.",
711
+ PHI_START_DOCSTRING,
712
+ )
713
+ class PhiPreTrainedModel(PreTrainedModel):
714
+ config_class = PhiConfig
715
+ base_model_prefix = "model"
716
+ supports_gradient_checkpointing = True
717
+ _no_split_modules = ["PhiDecoderLayer"]
718
+ _skip_keys_device_placement = "past_key_values"
719
+ _supports_flash_attn_2 = True
720
+ _supports_cache_class = True
721
+
722
+ def _init_weights(self, module):
723
+ std = self.config.initializer_range
724
+ if isinstance(module, nn.Linear):
725
+ module.weight.data.normal_(mean=0.0, std=std)
726
+ if module.bias is not None:
727
+ module.bias.data.zero_()
728
+ elif isinstance(module, nn.Embedding):
729
+ module.weight.data.normal_(mean=0.0, std=std)
730
+ if module.padding_idx is not None:
731
+ module.weight.data[module.padding_idx].zero_()
732
+
733
+
734
+ PHI_INPUTS_DOCSTRING = r"""
735
+ Args:
736
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
737
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
738
+ it.
739
+
740
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
741
+ [`PreTrainedTokenizer.__call__`] for details.
742
+
743
+ [What are input IDs?](../glossary#input-ids)
744
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
745
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
746
+
747
+ - 1 for tokens that are **not masked**,
748
+ - 0 for tokens that are **masked**.
749
+
750
+ [What are attention masks?](../glossary#attention-mask)
751
+
752
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
753
+ [`PreTrainedTokenizer.__call__`] for details.
754
+
755
+ If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
756
+ `past_key_values`).
757
+
758
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
759
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
760
+ information on the default strategy.
761
+
762
+ - 1 indicates the head is **not masked**,
763
+ - 0 indicates the head is **masked**.
764
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
765
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
766
+ config.n_positions - 1]`.
767
+
768
+ [What are position IDs?](../glossary#position-ids)
769
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
770
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
771
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
772
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
773
+
774
+ Two formats are allowed:
775
+ - a [`~cache_utils.Cache`] instance;
776
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
777
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
778
+ cache format.
779
+
780
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
781
+ legacy cache format will be returned.
782
+
783
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
784
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
785
+ of shape `(batch_size, sequence_length)`.
786
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
787
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
788
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
789
+ model's internal embedding lookup matrix.
790
+ use_cache (`bool`, *optional*):
791
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
792
+ `past_key_values`).
793
+ output_attentions (`bool`, *optional*):
794
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
795
+ tensors for more detail.
796
+ output_hidden_states (`bool`, *optional*):
797
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
798
+ more detail.
799
+ return_dict (`bool`, *optional*):
800
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
801
+ """
802
+
803
+
804
+ @add_start_docstrings(
805
+ "The bare Phi Model outputting raw hidden-states without any specific head on top.",
806
+ PHI_START_DOCSTRING,
807
+ )
808
+ class PhiModel(PhiPreTrainedModel):
809
+ """
810
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`PhiDecoderLayer`]
811
+
812
+ Args:
813
+ config: PhiConfig
814
+ """
815
+
816
+ def __init__(self, config: PhiConfig):
817
+ super().__init__(config)
818
+ self.padding_idx = config.pad_token_id
819
+ self.vocab_size = config.vocab_size
820
+
821
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
822
+ self.embed_dropout = nn.Dropout(config.embd_pdrop)
823
+ self.layers = nn.ModuleList(
824
+ [PhiDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
825
+ )
826
+ self.final_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
827
+ self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
828
+
829
+ self.gradient_checkpointing = False
830
+ # Initialize weights and apply final processing
831
+ self.post_init()
832
+
833
+ def get_input_embeddings(self):
834
+ return self.embed_tokens
835
+
836
+ def set_input_embeddings(self, value):
837
+ self.embed_tokens = value
838
+
839
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
840
+ def forward(
841
+ self,
842
+ input_ids: torch.LongTensor = None,
843
+ attention_mask: Optional[torch.Tensor] = None,
844
+ position_ids: Optional[torch.LongTensor] = None,
845
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
846
+ inputs_embeds: Optional[torch.FloatTensor] = None,
847
+ use_cache: Optional[bool] = None,
848
+ output_attentions: Optional[bool] = None,
849
+ output_hidden_states: Optional[bool] = None,
850
+ return_dict: Optional[bool] = None,
851
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
852
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
853
+ output_hidden_states = (
854
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
855
+ )
856
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
857
+
858
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
859
+
860
+ # retrieve input_ids and inputs_embeds
861
+ if input_ids is not None and inputs_embeds is not None:
862
+ raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
863
+ elif input_ids is not None:
864
+ batch_size, seq_length = input_ids.shape[:2]
865
+ elif inputs_embeds is not None:
866
+ batch_size, seq_length = inputs_embeds.shape[:2]
867
+ else:
868
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
869
+
870
+ past_key_values_length = 0
871
+
872
+ if self.gradient_checkpointing and self.training:
873
+ if use_cache:
874
+ logger.warning_once(
875
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
876
+ )
877
+ use_cache = False
878
+
879
+ if use_cache:
880
+ use_legacy_cache = not isinstance(past_key_values, Cache)
881
+ if use_legacy_cache:
882
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
883
+ past_key_values_length = past_key_values.get_usable_length(seq_length)
884
+
885
+ if position_ids is None:
886
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
887
+ position_ids = torch.arange(
888
+ past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
889
+ )
890
+ position_ids = position_ids.unsqueeze(0)
891
+
892
+ if inputs_embeds is None:
893
+ inputs_embeds = self.embed_tokens(input_ids)
894
+
895
+ inputs_embeds = self.embed_dropout(inputs_embeds)
896
+
897
+ # Attention mask.
898
+ if self._use_flash_attention_2:
899
+ # 2d mask is passed through the layers
900
+ attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
901
+ else:
902
+ # 4d mask is passed through the layers
903
+ attention_mask = _prepare_4d_causal_attention_mask(
904
+ attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
905
+ )
906
+
907
+ hidden_states = inputs_embeds
908
+
909
+ # decoder layers
910
+ all_hidden_states = () if output_hidden_states else None
911
+ all_self_attns = () if output_attentions else None
912
+ next_decoder_cache = None
913
+
914
+ for decoder_layer in self.layers:
915
+ if output_hidden_states:
916
+ all_hidden_states += (hidden_states,)
917
+
918
+ if self.gradient_checkpointing and self.training:
919
+ layer_outputs = self._gradient_checkpointing_func(
920
+ decoder_layer.__call__,
921
+ hidden_states,
922
+ attention_mask,
923
+ position_ids,
924
+ past_key_values,
925
+ output_attentions,
926
+ )
927
+ else:
928
+ layer_outputs = decoder_layer(
929
+ hidden_states,
930
+ attention_mask=attention_mask,
931
+ position_ids=position_ids,
932
+ past_key_value=past_key_values,
933
+ output_attentions=output_attentions,
934
+ use_cache=use_cache,
935
+ )
936
+
937
+ hidden_states = layer_outputs[0]
938
+
939
+ if use_cache:
940
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
941
+
942
+ if output_attentions:
943
+ all_self_attns += (layer_outputs[1],)
944
+
945
+ hidden_states = self.final_layernorm(hidden_states)
946
+
947
+ # add hidden states from the last decoder layer
948
+ if output_hidden_states:
949
+ all_hidden_states += (hidden_states,)
950
+
951
+ next_cache = None
952
+ if use_cache:
953
+ next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
954
+ if not return_dict:
955
+ return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
956
+ return BaseModelOutputWithPast(
957
+ last_hidden_state=hidden_states,
958
+ past_key_values=next_cache,
959
+ hidden_states=all_hidden_states,
960
+ attentions=all_self_attns,
961
+ )
962
+
963
+
964
+ class PhiForCausalLM(PhiPreTrainedModel):
965
+ _tied_weights_keys = ["lm_head.weight"]
966
+
967
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.__init__ with Llama->Phi,bias=False->bias=True
968
+ def __init__(self, config):
969
+ super().__init__(config)
970
+ self.model = PhiModel(config)
971
+ self.vocab_size = config.vocab_size
972
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=True)
973
+
974
+ # Initialize weights and apply final processing
975
+ self.post_init()
976
+
977
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_input_embeddings
978
+ def get_input_embeddings(self):
979
+ return self.model.embed_tokens
980
+
981
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_input_embeddings
982
+ def set_input_embeddings(self, value):
983
+ self.model.embed_tokens = value
984
+
985
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_output_embeddings
986
+ def get_output_embeddings(self):
987
+ return self.lm_head
988
+
989
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_output_embeddings
990
+ def set_output_embeddings(self, new_embeddings):
991
+ self.lm_head = new_embeddings
992
+
993
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_decoder
994
+ def set_decoder(self, decoder):
995
+ self.model = decoder
996
+
997
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_decoder
998
+ def get_decoder(self):
999
+ return self.model
1000
+
1001
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
1002
+ @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
1003
+ def forward(
1004
+ self,
1005
+ input_ids: torch.LongTensor = None,
1006
+ attention_mask: Optional[torch.Tensor] = None,
1007
+ position_ids: Optional[torch.LongTensor] = None,
1008
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1009
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1010
+ labels: Optional[torch.LongTensor] = None,
1011
+ use_cache: Optional[bool] = None,
1012
+ output_attentions: Optional[bool] = None,
1013
+ output_hidden_states: Optional[bool] = None,
1014
+ return_dict: Optional[bool] = None,
1015
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1016
+ r"""
1017
+ Args:
1018
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1019
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1020
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1021
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1022
+
1023
+ Returns:
1024
+
1025
+ Example:
1026
+
1027
+ ```python
1028
+ >>> from transformers import AutoTokenizer, PhiForCausalLM
1029
+
1030
+ >>> model = PhiForCausalLM.from_pretrained("microsoft/phi-1")
1031
+ >>> tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1")
1032
+
1033
+ >>> prompt = "This is an example script ."
1034
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1035
+
1036
+ >>> # Generate
1037
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1038
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1039
+ 'This is an example script .\n\n\n\nfrom typing import List\n\ndef find_most_common_letter(words: List[str'
1040
+ ```"""
1041
+
1042
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1043
+ output_hidden_states = (
1044
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1045
+ )
1046
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1047
+
1048
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1049
+ outputs = self.model(
1050
+ input_ids=input_ids,
1051
+ attention_mask=attention_mask,
1052
+ position_ids=position_ids,
1053
+ past_key_values=past_key_values,
1054
+ inputs_embeds=inputs_embeds,
1055
+ use_cache=use_cache,
1056
+ output_attentions=output_attentions,
1057
+ output_hidden_states=output_hidden_states,
1058
+ return_dict=return_dict,
1059
+ )
1060
+
1061
+ hidden_states = outputs[0]
1062
+ logits = self.lm_head(hidden_states)
1063
+ logits = logits.float()
1064
+
1065
+ loss = None
1066
+ if labels is not None:
1067
+ # Shift so that tokens < n predict n
1068
+ shift_logits = logits[..., :-1, :].contiguous()
1069
+ shift_labels = labels[..., 1:].contiguous()
1070
+ # Flatten the tokens
1071
+ loss_fct = CrossEntropyLoss()
1072
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1073
+ shift_labels = shift_labels.view(-1)
1074
+ # Enable model parallelism
1075
+ shift_labels = shift_labels.to(shift_logits.device)
1076
+ loss = loss_fct(shift_logits, shift_labels)
1077
+
1078
+ if not return_dict:
1079
+ output = (logits,) + outputs[1:]
1080
+ return (loss,) + output if loss is not None else output
1081
+
1082
+ return CausalLMOutputWithPast(
1083
+ loss=loss,
1084
+ logits=logits,
1085
+ past_key_values=outputs.past_key_values,
1086
+ hidden_states=outputs.hidden_states,
1087
+ attentions=outputs.attentions,
1088
+ )
1089
+
1090
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.prepare_inputs_for_generation
1091
+ def prepare_inputs_for_generation(
1092
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
1093
+ ):
1094
+ if past_key_values is not None:
1095
+ if isinstance(past_key_values, Cache):
1096
+ cache_length = past_key_values.get_seq_length()
1097
+ past_length = past_key_values.seen_tokens
1098
+ max_cache_length = past_key_values.get_max_length()
1099
+ else:
1100
+ cache_length = past_length = past_key_values[0][0].shape[2]
1101
+ max_cache_length = None
1102
+
1103
+ # Keep only the unprocessed tokens:
1104
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1105
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
1106
+ # input)
1107
+ if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
1108
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1109
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1110
+ # input_ids based on the past_length.
1111
+ elif past_length < input_ids.shape[1]:
1112
+ input_ids = input_ids[:, past_length:]
1113
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1114
+
1115
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1116
+ if (
1117
+ max_cache_length is not None
1118
+ and attention_mask is not None
1119
+ and cache_length + input_ids.shape[1] > max_cache_length
1120
+ ):
1121
+ attention_mask = attention_mask[:, -max_cache_length:]
1122
+
1123
+ position_ids = kwargs.get("position_ids", None)
1124
+ if attention_mask is not None and position_ids is None:
1125
+ # create position_ids on the fly for batch generation
1126
+ position_ids = attention_mask.long().cumsum(-1) - 1
1127
+ position_ids.masked_fill_(attention_mask == 0, 1)
1128
+ if past_key_values:
1129
+ position_ids = position_ids[:, -input_ids.shape[1] :]
1130
+
1131
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1132
+ if inputs_embeds is not None and past_key_values is None:
1133
+ model_inputs = {"inputs_embeds": inputs_embeds}
1134
+ else:
1135
+ model_inputs = {"input_ids": input_ids}
1136
+
1137
+ model_inputs.update(
1138
+ {
1139
+ "position_ids": position_ids,
1140
+ "past_key_values": past_key_values,
1141
+ "use_cache": kwargs.get("use_cache"),
1142
+ "attention_mask": attention_mask,
1143
+ }
1144
+ )
1145
+ return model_inputs
1146
+
1147
+ @staticmethod
1148
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM._reorder_cache
1149
+ def _reorder_cache(past_key_values, beam_idx):
1150
+ reordered_past = ()
1151
+ for layer_past in past_key_values:
1152
+ reordered_past += (
1153
+ tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
1154
+ )
1155
+ return reordered_past
1156
+
1157
+
1158
+ @add_start_docstrings(
1159
+ """
1160
+ The PhiModel with a sequence classification head on top (linear layer).
1161
+
1162
+ [`PhiForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1163
+ (e.g. GPT-2) do.
1164
+
1165
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1166
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1167
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1168
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1169
+ each row of the batch).
1170
+ """,
1171
+ PHI_START_DOCSTRING,
1172
+ )
1173
+ # Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with LLAMA->PHI,Llama->Phi with self.transformer->self.model, transformer_outputs->model_outputs
1174
+ class PhiForSequenceClassification(PhiPreTrainedModel):
1175
+ def __init__(self, config):
1176
+ super().__init__(config)
1177
+ self.num_labels = config.num_labels
1178
+ self.model = PhiModel(config)
1179
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1180
+
1181
+ # Initialize weights and apply final processing
1182
+ self.post_init()
1183
+
1184
+ def get_input_embeddings(self):
1185
+ return self.model.embed_tokens
1186
+
1187
+ def set_input_embeddings(self, value):
1188
+ self.model.embed_tokens = value
1189
+
1190
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
1191
+ def forward(
1192
+ self,
1193
+ input_ids: torch.LongTensor = None,
1194
+ attention_mask: Optional[torch.Tensor] = None,
1195
+ position_ids: Optional[torch.LongTensor] = None,
1196
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1197
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1198
+ labels: Optional[torch.LongTensor] = None,
1199
+ use_cache: Optional[bool] = None,
1200
+ output_attentions: Optional[bool] = None,
1201
+ output_hidden_states: Optional[bool] = None,
1202
+ return_dict: Optional[bool] = None,
1203
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1204
+ r"""
1205
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1206
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1207
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1208
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1209
+ """
1210
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1211
+
1212
+ model_outputs = self.model(
1213
+ input_ids,
1214
+ attention_mask=attention_mask,
1215
+ position_ids=position_ids,
1216
+ past_key_values=past_key_values,
1217
+ inputs_embeds=inputs_embeds,
1218
+ use_cache=use_cache,
1219
+ output_attentions=output_attentions,
1220
+ output_hidden_states=output_hidden_states,
1221
+ return_dict=return_dict,
1222
+ )
1223
+ hidden_states = model_outputs[0]
1224
+ logits = self.score(hidden_states)
1225
+
1226
+ if input_ids is not None:
1227
+ batch_size = input_ids.shape[0]
1228
+ else:
1229
+ batch_size = inputs_embeds.shape[0]
1230
+
1231
+ if self.config.pad_token_id is None and batch_size != 1:
1232
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
1233
+ if self.config.pad_token_id is None:
1234
+ sequence_lengths = -1
1235
+ else:
1236
+ if input_ids is not None:
1237
+ # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
1238
+ sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
1239
+ sequence_lengths = sequence_lengths % input_ids.shape[-1]
1240
+ sequence_lengths = sequence_lengths.to(logits.device)
1241
+ else:
1242
+ sequence_lengths = -1
1243
+
1244
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
1245
+
1246
+ loss = None
1247
+ if labels is not None:
1248
+ labels = labels.to(logits.device)
1249
+ if self.config.problem_type is None:
1250
+ if self.num_labels == 1:
1251
+ self.config.problem_type = "regression"
1252
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
1253
+ self.config.problem_type = "single_label_classification"
1254
+ else:
1255
+ self.config.problem_type = "multi_label_classification"
1256
+
1257
+ if self.config.problem_type == "regression":
1258
+ loss_fct = MSELoss()
1259
+ if self.num_labels == 1:
1260
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1261
+ else:
1262
+ loss = loss_fct(pooled_logits, labels)
1263
+ elif self.config.problem_type == "single_label_classification":
1264
+ loss_fct = CrossEntropyLoss()
1265
+ loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
1266
+ elif self.config.problem_type == "multi_label_classification":
1267
+ loss_fct = BCEWithLogitsLoss()
1268
+ loss = loss_fct(pooled_logits, labels)
1269
+ if not return_dict:
1270
+ output = (pooled_logits,) + model_outputs[1:]
1271
+ return ((loss,) + output) if loss is not None else output
1272
+
1273
+ return SequenceClassifierOutputWithPast(
1274
+ loss=loss,
1275
+ logits=pooled_logits,
1276
+ past_key_values=model_outputs.past_key_values,
1277
+ hidden_states=model_outputs.hidden_states,
1278
+ attentions=model_outputs.attentions,
1279
+ )
1280
+
1281
+
1282
+ @add_start_docstrings(
1283
+ """
1284
+ PhiModel with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for
1285
+ Named-Entity-Recognition (NER) tasks.
1286
+ """,
1287
+ PHI_START_DOCSTRING,
1288
+ )
1289
+ # Copied from transformers.models.mpt.modeling_mpt.MptForTokenClassification with MPT->PHI,Mpt->Phi,self.transformer->self.model,transformer_outputs->model_outputs
1290
+ class PhiForTokenClassification(PhiPreTrainedModel):
1291
+ def __init__(self, config: PhiConfig):
1292
+ super().__init__(config)
1293
+ self.num_labels = config.num_labels
1294
+
1295
+ self.model = PhiModel(config)
1296
+ if hasattr(config, "classifier_dropout") and config.classifier_dropout is not None:
1297
+ classifier_dropout = config.classifier_dropout
1298
+ elif hasattr(config, "hidden_dropout") and config.hidden_dropout is not None:
1299
+ classifier_dropout = config.hidden_dropout
1300
+ else:
1301
+ classifier_dropout = 0.1
1302
+ self.dropout = nn.Dropout(classifier_dropout)
1303
+ self.classifier = nn.Linear(config.hidden_size, config.num_labels)
1304
+
1305
+ # Initialize weights and apply final processing
1306
+ self.post_init()
1307
+
1308
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
1309
+ @add_code_sample_docstrings(
1310
+ checkpoint=_CHECKPOINT_FOR_DOC,
1311
+ output_type=TokenClassifierOutput,
1312
+ config_class=_CONFIG_FOR_DOC,
1313
+ )
1314
+ def forward(
1315
+ self,
1316
+ input_ids: Optional[torch.LongTensor] = None,
1317
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
1318
+ attention_mask: Optional[torch.Tensor] = None,
1319
+ inputs_embeds: Optional[torch.Tensor] = None,
1320
+ labels: Optional[torch.Tensor] = None,
1321
+ use_cache: Optional[bool] = None,
1322
+ output_attentions: Optional[bool] = None,
1323
+ output_hidden_states: Optional[bool] = None,
1324
+ return_dict: Optional[bool] = None,
1325
+ **deprecated_arguments,
1326
+ ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]:
1327
+ r"""
1328
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1329
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1330
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1331
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1332
+ """
1333
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1334
+
1335
+ model_outputs = self.model(
1336
+ input_ids,
1337
+ past_key_values=past_key_values,
1338
+ attention_mask=attention_mask,
1339
+ inputs_embeds=inputs_embeds,
1340
+ use_cache=use_cache,
1341
+ output_attentions=output_attentions,
1342
+ output_hidden_states=output_hidden_states,
1343
+ return_dict=return_dict,
1344
+ )
1345
+
1346
+ hidden_states = model_outputs[0]
1347
+ hidden_states = self.dropout(hidden_states)
1348
+ logits = self.classifier(hidden_states)
1349
+
1350
+ loss = None
1351
+ if labels is not None:
1352
+ # move labels to correct device to enable model parallelism
1353
+ labels = labels.to(logits.device)
1354
+ batch_size, seq_length = labels.shape
1355
+ loss_fct = CrossEntropyLoss()
1356
+ loss = loss_fct(
1357
+ logits.view(batch_size * seq_length, self.num_labels), labels.view(batch_size * seq_length)
1358
+ )
1359
+
1360
+ if not return_dict:
1361
+ output = (logits,) + model_outputs[2:]
1362
+ return ((loss,) + output) if loss is not None else output
1363
+
1364
+ return TokenClassifierOutput(
1365
+ loss=loss,
1366
+ logits=logits,
1367
+ hidden_states=model_outputs.hidden_states,
1368
+ attentions=model_outputs.attentions,
1369
+ )
special_tokens_map.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<|endoftext|>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "<|endoftext|>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "<|endoftext|>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "unk_token": {
24
+ "content": "<|endoftext|>",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ }
30
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "50256": {
6
+ "content": "<|endoftext|>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "50257": {
14
+ "content": " ",
15
+ "lstrip": false,
16
+ "normalized": true,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": false
20
+ },
21
+ "50258": {
22
+ "content": " ",
23
+ "lstrip": false,
24
+ "normalized": true,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": false
28
+ },
29
+ "50259": {
30
+ "content": " ",
31
+ "lstrip": false,
32
+ "normalized": true,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": false
36
+ },
37
+ "50260": {
38
+ "content": " ",
39
+ "lstrip": false,
40
+ "normalized": true,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": false
44
+ },
45
+ "50261": {
46
+ "content": " ",
47
+ "lstrip": false,
48
+ "normalized": true,
49
+ "rstrip": false,
50
+ "single_word": false,
51
+ "special": false
52
+ },
53
+ "50262": {
54
+ "content": " ",
55
+ "lstrip": false,
56
+ "normalized": true,
57
+ "rstrip": false,
58
+ "single_word": false,
59
+ "special": false
60
+ },
61
+ "50263": {
62
+ "content": " ",
63
+ "lstrip": false,
64
+ "normalized": true,
65
+ "rstrip": false,
66
+ "single_word": false,
67
+ "special": false
68
+ },
69
+ "50264": {
70
+ "content": " ",
71
+ "lstrip": false,
72
+ "normalized": true,
73
+ "rstrip": false,
74
+ "single_word": false,
75
+ "special": false
76
+ },
77
+ "50265": {
78
+ "content": " ",
79
+ "lstrip": false,
80
+ "normalized": true,
81
+ "rstrip": false,
82
+ "single_word": false,
83
+ "special": false
84
+ },
85
+ "50266": {
86
+ "content": " ",
87
+ "lstrip": false,
88
+ "normalized": true,
89
+ "rstrip": false,
90
+ "single_word": false,
91
+ "special": false
92
+ },
93
+ "50267": {
94
+ "content": " ",
95
+ "lstrip": false,
96
+ "normalized": true,
97
+ "rstrip": false,
98
+ "single_word": false,
99
+ "special": false
100
+ },
101
+ "50268": {
102
+ "content": " ",
103
+ "lstrip": false,
104
+ "normalized": true,
105
+ "rstrip": false,
106
+ "single_word": false,
107
+ "special": false
108
+ },
109
+ "50269": {
110
+ "content": " ",
111
+ "lstrip": false,
112
+ "normalized": true,
113
+ "rstrip": false,
114
+ "single_word": false,
115
+ "special": false
116
+ },
117
+ "50270": {
118
+ "content": " ",
119
+ "lstrip": false,
120
+ "normalized": true,
121
+ "rstrip": false,
122
+ "single_word": false,
123
+ "special": false
124
+ },
125
+ "50271": {
126
+ "content": " ",
127
+ "lstrip": false,
128
+ "normalized": true,
129
+ "rstrip": false,
130
+ "single_word": false,
131
+ "special": false
132
+ },
133
+ "50272": {
134
+ "content": " ",
135
+ "lstrip": false,
136
+ "normalized": true,
137
+ "rstrip": false,
138
+ "single_word": false,
139
+ "special": false
140
+ },
141
+ "50273": {
142
+ "content": " ",
143
+ "lstrip": false,
144
+ "normalized": true,
145
+ "rstrip": false,
146
+ "single_word": false,
147
+ "special": false
148
+ },
149
+ "50274": {
150
+ "content": " ",
151
+ "lstrip": false,
152
+ "normalized": true,
153
+ "rstrip": false,
154
+ "single_word": false,
155
+ "special": false
156
+ },
157
+ "50275": {
158
+ "content": " ",
159
+ "lstrip": false,
160
+ "normalized": true,
161
+ "rstrip": false,
162
+ "single_word": false,
163
+ "special": false
164
+ },
165
+ "50276": {
166
+ "content": " ",
167
+ "lstrip": false,
168
+ "normalized": true,
169
+ "rstrip": false,
170
+ "single_word": false,
171
+ "special": false
172
+ },
173
+ "50277": {
174
+ "content": " ",
175
+ "lstrip": false,
176
+ "normalized": true,
177
+ "rstrip": false,
178
+ "single_word": false,
179
+ "special": false
180
+ },
181
+ "50278": {
182
+ "content": " ",
183
+ "lstrip": false,
184
+ "normalized": true,
185
+ "rstrip": false,
186
+ "single_word": false,
187
+ "special": false
188
+ },
189
+ "50279": {
190
+ "content": " ",
191
+ "lstrip": false,
192
+ "normalized": true,
193
+ "rstrip": false,
194
+ "single_word": false,
195
+ "special": false
196
+ },
197
+ "50280": {
198
+ "content": " ",
199
+ "lstrip": false,
200
+ "normalized": true,
201
+ "rstrip": false,
202
+ "single_word": false,
203
+ "special": false
204
+ },
205
+ "50281": {
206
+ "content": " ",
207
+ "lstrip": false,
208
+ "normalized": true,
209
+ "rstrip": false,
210
+ "single_word": false,
211
+ "special": false
212
+ },
213
+ "50282": {
214
+ "content": " ",
215
+ "lstrip": false,
216
+ "normalized": true,
217
+ "rstrip": false,
218
+ "single_word": false,
219
+ "special": false
220
+ },
221
+ "50283": {
222
+ "content": " ",
223
+ "lstrip": false,
224
+ "normalized": true,
225
+ "rstrip": false,
226
+ "single_word": false,
227
+ "special": false
228
+ },
229
+ "50284": {
230
+ "content": " ",
231
+ "lstrip": false,
232
+ "normalized": true,
233
+ "rstrip": false,
234
+ "single_word": false,
235
+ "special": false
236
+ },
237
+ "50285": {
238
+ "content": " ",
239
+ "lstrip": false,
240
+ "normalized": true,
241
+ "rstrip": false,
242
+ "single_word": false,
243
+ "special": false
244
+ },
245
+ "50286": {
246
+ "content": " ",
247
+ "lstrip": false,
248
+ "normalized": true,
249
+ "rstrip": false,
250
+ "single_word": false,
251
+ "special": false
252
+ },
253
+ "50287": {
254
+ "content": "\t\t\t\t\t\t\t\t\t",
255
+ "lstrip": false,
256
+ "normalized": true,
257
+ "rstrip": false,
258
+ "single_word": false,
259
+ "special": false
260
+ },
261
+ "50288": {
262
+ "content": "\t\t\t\t\t\t\t\t",
263
+ "lstrip": false,
264
+ "normalized": true,
265
+ "rstrip": false,
266
+ "single_word": false,
267
+ "special": false
268
+ },
269
+ "50289": {
270
+ "content": "\t\t\t\t\t\t\t",
271
+ "lstrip": false,
272
+ "normalized": true,
273
+ "rstrip": false,
274
+ "single_word": false,
275
+ "special": false
276
+ },
277
+ "50290": {
278
+ "content": "\t\t\t\t\t\t",
279
+ "lstrip": false,
280
+ "normalized": true,
281
+ "rstrip": false,
282
+ "single_word": false,
283
+ "special": false
284
+ },
285
+ "50291": {
286
+ "content": "\t\t\t\t\t",
287
+ "lstrip": false,
288
+ "normalized": true,
289
+ "rstrip": false,
290
+ "single_word": false,
291
+ "special": false
292
+ },
293
+ "50292": {
294
+ "content": "\t\t\t\t",
295
+ "lstrip": false,
296
+ "normalized": true,
297
+ "rstrip": false,
298
+ "single_word": false,
299
+ "special": false
300
+ },
301
+ "50293": {
302
+ "content": "\t\t\t",
303
+ "lstrip": false,
304
+ "normalized": true,
305
+ "rstrip": false,
306
+ "single_word": false,
307
+ "special": false
308
+ },
309
+ "50294": {
310
+ "content": "\t\t",
311
+ "lstrip": false,
312
+ "normalized": true,
313
+ "rstrip": false,
314
+ "single_word": false,
315
+ "special": false
316
+ }
317
+ },
318
+ "bos_token": "<|endoftext|>",
319
+ "clean_up_tokenization_spaces": true,
320
+ "eos_token": "<|endoftext|>",
321
+ "errors": "replace",
322
+ "model_max_length": 2048,
323
+ "pad_token": "<|endoftext|>",
324
+ "padding_side": "right",
325
+ "split_special_tokens": false,
326
+ "tokenizer_class": "CodeGenTokenizer",
327
+ "unk_token": "<|endoftext|>"
328
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff