scaled rotary embeddings for LLaMA
Browse files- README.md +170 -0
- config.json +27 -0
- generation_config.json +7 -0
- modelling_llama.py +895 -0
- pytorch_model-00001-of-00002.bin +3 -0
- pytorch_model-00002-of-00002.bin +3 -0
- pytorch_model.bin.index.json +330 -0
- special_tokens_map.json +1 -0
- tokenizer.model +3 -0
- tokenizer_config.json +1 -0
README.md
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
datasets:
|
4 |
+
- togethercomputer/RedPajama-Data-1T
|
5 |
+
---
|
6 |
+
|
7 |
+
This is a modified version of the original LLaMA model that incorporates Scaled Rotary Embeddings first proposed by [kaiokendev](https://kaiokendev.github.io/). By default, the model is configured to be equivalent to the original OpenLLaMA model (2048 context length). To modify, instantiate the LLaMA configuration and set `max_position_embeddings` to the desired context length. The value should be a power of 2, e.g. 2048, 4096, 8192, etc.
|
8 |
+
|
9 |
+
```python
|
10 |
+
config = AutoConfig.from_pretrained("emozilla/open_llama_7b-scaled")
|
11 |
+
config.max_position_embeddings = 8192
|
12 |
+
model = AutoModelForCausalLM.from_pretrained("emozilla/open_llama_7b-scaled", config=config)
|
13 |
+
```
|
14 |
+
|
15 |
+
You should also set `max_model_length` on your tokenizer.
|
16 |
+
|
17 |
+
```python
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained("emozilla/open_llama_7b-scaled", max_model_length=8192)
|
19 |
+
```
|
20 |
+
|
21 |
+
# OpenLLaMA: An Open Reproduction of LLaMA
|
22 |
+
|
23 |
+
|
24 |
+
In this repo, we present a permissively licensed open source reproduction of Meta AI's [LLaMA](https://ai.facebook.com/blog/large-language-model-llama-meta-ai/) large language model. We are releasing a 7B and 3B model trained on 1T tokens, as well as the preview of a 13B model trained on 600B tokens. We provide PyTorch and JAX weights of pre-trained OpenLLaMA models, as well as evaluation results and comparison against the original LLaMA models. Please see the [project homepage of OpenLLaMA](https://github.com/openlm-research/open_llama) for more details.
|
25 |
+
|
26 |
+
|
27 |
+
## Weights Release, License and Usage
|
28 |
+
|
29 |
+
We release the weights in two formats: an EasyLM format to be use with our [EasyLM framework](https://github.com/young-geng/EasyLM), and a PyTorch format to be used with the [Hugging Face transformers](https://huggingface.co/docs/transformers/index) library. Both our training framework EasyLM and the checkpoint weights are licensed permissively under the Apache 2.0 license.
|
30 |
+
|
31 |
+
### Loading the Weights with Hugging Face Transformers
|
32 |
+
Preview checkpoints can be directly loaded from Hugging Face Hub. **Please note that it is advised to avoid using the Hugging Face fast tokenizer for now, as we’ve observed that the auto-converted fast tokenizer sometimes gives incorrect tokenizations.** This can be achieved by directly using the `LlamaTokenizer` class, or passing in the `use_fast=False` option for the `AutoTokenizer` class. See the following example for usage.
|
33 |
+
|
34 |
+
```python
|
35 |
+
import torch
|
36 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM
|
37 |
+
|
38 |
+
model_path = 'openlm-research/open_llama_3b'
|
39 |
+
# model_path = 'openlm-research/open_llama_7b'
|
40 |
+
|
41 |
+
tokenizer = LlamaTokenizer.from_pretrained(model_path)
|
42 |
+
model = LlamaForCausalLM.from_pretrained(
|
43 |
+
model_path, torch_dtype=torch.float16, device_map='auto',
|
44 |
+
)
|
45 |
+
|
46 |
+
prompt = 'Q: What is the largest animal?\nA:'
|
47 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
48 |
+
|
49 |
+
generation_output = model.generate(
|
50 |
+
input_ids=input_ids, max_new_tokens=32
|
51 |
+
)
|
52 |
+
print(tokenizer.decode(generation_output[0]))
|
53 |
+
```
|
54 |
+
|
55 |
+
For more advanced usage, please follow the [transformers LLaMA documentation](https://huggingface.co/docs/transformers/main/model_doc/llama).
|
56 |
+
|
57 |
+
### Evaluating with LM-Eval-Harness
|
58 |
+
The model can be evaluated with [lm-eval-harness](https://github.com/EleutherAI/lm-evaluation-harness). However, due to the aforementioned tokenizer issue, we need to avoid using the fast tokenizer to obtain the correct results. This can be achieved by passing in `use_fast=False` to [this part of lm-eval-harness](https://github.com/EleutherAI/lm-evaluation-harness/blob/4b701e228768052cfae9043dca13e82052ca5eea/lm_eval/models/huggingface.py#LL313C9-L316C10), as shown in the example below:
|
59 |
+
|
60 |
+
```python
|
61 |
+
tokenizer = self.AUTO_TOKENIZER_CLASS.from_pretrained(
|
62 |
+
pretrained if tokenizer is None else tokenizer,
|
63 |
+
revision=revision + ("/" + subfolder if subfolder is not None else ""),
|
64 |
+
use_fast=False
|
65 |
+
)
|
66 |
+
```
|
67 |
+
|
68 |
+
### Loading the Weights with EasyLM
|
69 |
+
|
70 |
+
For using the weights in our EasyLM framework, please refer to the [LLaMA documentation of EasyLM](https://github.com/young-geng/EasyLM/blob/main/docs/llama.md). Note that unlike the original LLaMA model, our OpenLLaMA tokenizer and weights are trained completely from scratch so it is no longer needed to obtain the original LLaMA tokenizer and weights. Note that we use BOS (beginning of sentence) token (id=1) during training, so it is best to prepend this token for best performance during few-shot evaluation.
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
## Dataset and Training
|
75 |
+
|
76 |
+
We train our models on the [RedPajama](https://www.together.xyz/blog/redpajama) dataset released by [Together](https://www.together.xyz/), which is a reproduction of the LLaMA training dataset containing over 1.2 trillion tokens. We follow the exactly same preprocessing steps and training hyperparameters as the original LLaMA paper, including model architecture, context length, training steps, learning rate schedule, and optimizer. The only difference between our setting and the original one is the dataset used: OpenLLaMA employs the RedPajama dataset rather than the one utilized by the original LLaMA.
|
77 |
+
|
78 |
+
We train the models on cloud TPU-v4s using [EasyLM](https://github.com/young-geng/EasyLM), a JAX based training pipeline we developed for training and fine-tuning large language models. We employ a combination of normal data parallelism and [fully sharded data parallelism (also know as ZeRO stage 3)](https://engineering.fb.com/2021/07/15/open-source/fsdp/) to balance the training throughput and memory usage. Overall we reach a throughput of over 2200 tokens / second / TPU-v4 chip for our 7B model.
|
79 |
+
|
80 |
+
|
81 |
+
## Evaluation
|
82 |
+
We evaluated OpenLLaMA on a wide range of tasks using [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness). The LLaMA results are generated by running the original LLaMA model on the same evaluation metrics. We note that our results for the LLaMA model differ slightly from the original LLaMA paper, which we believe is a result of different evaluation protocols. Similar differences have been reported in [this issue of lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness/issues/443). Additionally, we present the results of GPT-J, a 6B parameter model trained on the [Pile](https://pile.eleuther.ai/) dataset by [EleutherAI](https://www.eleuther.ai/).
|
83 |
+
|
84 |
+
The original LLaMA model was trained for 1 trillion tokens and GPT-J was trained for 500 billion tokens. We present the results in the table below. OpenLLaMA exhibits comparable performance to the original LLaMA and GPT-J across a majority of tasks, and outperforms them in some tasks.
|
85 |
+
|
86 |
+
|
87 |
+
| **Task/Metric** | GPT-J 6B | LLaMA 7B | OpenLLaMA 7B | OpenLLaMA 3B | OpenLLaMA 13B 600BT |
|
88 |
+
| ---------------------- | -------- | -------- | ------------ | ------------ | ------------------- |
|
89 |
+
| anli_r1/acc | 0.32 | 0.35 | 0.33 | 0.33 | 0.33 |
|
90 |
+
| anli_r2/acc | 0.34 | 0.34 | 0.36 | 0.32 | 0.35 |
|
91 |
+
| anli_r3/acc | 0.35 | 0.37 | 0.38 | 0.35 | 0.38 |
|
92 |
+
| arc_challenge/acc | 0.34 | 0.39 | 0.37 | 0.34 | 0.39 |
|
93 |
+
| arc_challenge/acc_norm | 0.37 | 0.41 | 0.38 | 0.37 | 0.42 |
|
94 |
+
| arc_easy/acc | 0.67 | 0.68 | 0.72 | 0.69 | 0.74 |
|
95 |
+
| arc_easy/acc_norm | 0.62 | 0.52 | 0.68 | 0.65 | 0.70 |
|
96 |
+
| ddboolq/acc | 0.50 | 0.56 | 0.53 | 0.49 | 0.71 |
|
97 |
+
| hellaswag/acc | 0.36 | 0.36 | 0.63 | 0.43 | 0.54 |
|
98 |
+
| hellaswag/acc_norm | 0.66 | 0.73 | 0.72 | 0.67 | 0.73 |
|
99 |
+
| openbookqa/acc | 0.29 | 0.29 | 0.30 | 0.27 | 0.30 |
|
100 |
+
| openbookqa/acc_norm | 0.38 | 0.41 | 0.40 | 0.40 | 0.41 |
|
101 |
+
| piqa/acc | 0.75 | 0.78 | 0.76 | 0.75 | 0.77 |
|
102 |
+
| piqa/acc_norm | 0.76 | 0.78 | 0.77 | 0.76 | 0.78 |
|
103 |
+
| record/em | 0.88 | 0.91 | 0.89 | 0.88 | 0.90 |
|
104 |
+
| record/f1 | 0.89 | 0.91 | 0.90 | 0.89 | 0.90 |
|
105 |
+
| rte/acc | 0.54 | 0.56 | 0.60 | 0.58 | 0.65 |
|
106 |
+
| truthfulqa_mc/mc1 | 0.20 | 0.21 | 0.23 | 0.22 | 0.22 |
|
107 |
+
| truthfulqa_mc/mc2 | 0.36 | 0.34 | 0.35 | 0.35 | 0.35 |
|
108 |
+
| wic/acc | 0.50 | 0.50 | 0.51 | 0.48 | 0.49 |
|
109 |
+
| winogrande/acc | 0.64 | 0.68 | 0.67 | 0.62 | 0.67 |
|
110 |
+
| Average | 0.51 | 0.53 | 0.55 | 0.52 | 0.56 |
|
111 |
+
|
112 |
+
|
113 |
+
We removed the task CB and WSC from our benchmark, as our model performs suspiciously well on these two tasks. We hypothesize that there could be a benchmark data contamination in the training set.
|
114 |
+
|
115 |
+
|
116 |
+
## Contact
|
117 |
+
|
118 |
+
We would love to get feedback from the community. If you have any questions, please open an issue or contact us.
|
119 |
+
|
120 |
+
OpenLLaMA is developed by:
|
121 |
+
[Xinyang Geng](https://young-geng.xyz/)* and [Hao Liu](https://www.haoliu.site/)* from Berkeley AI Research.
|
122 |
+
*Equal Contribution
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
## Acknowledgment
|
127 |
+
|
128 |
+
We thank the [Google TPU Research Cloud](https://sites.research.google/trc/about/) program for providing part of the computation resources. We’d like to specially thank Jonathan Caton from TPU Research Cloud for helping us organizing compute resources, Rafi Witten from the Google Cloud team and James Bradbury from the Google JAX team for helping us optimizing our training throughput. We’d also want to thank Charlie Snell, Gautier Izacard, Eric Wallace, Lianmin Zheng and our user community for the discussions and feedback.
|
129 |
+
|
130 |
+
The OpenLLaMA 13B model is trained in collaboration with [Stability AI](https://stability.ai/), and we thank Stability AI for providing the computation resources. We’d like to especially thank David Ha and Shivanshu Purohit for the coordinating the logistics and providing engineering support.
|
131 |
+
|
132 |
+
|
133 |
+
## Reference
|
134 |
+
|
135 |
+
If you found OpenLLaMA useful in your research or applications, please cite using the following BibTeX:
|
136 |
+
```
|
137 |
+
@software{openlm2023openllama,
|
138 |
+
author = {Geng, Xinyang and Liu, Hao},
|
139 |
+
title = {OpenLLaMA: An Open Reproduction of LLaMA},
|
140 |
+
month = May,
|
141 |
+
year = 2023,
|
142 |
+
url = {https://github.com/openlm-research/open_llama}
|
143 |
+
}
|
144 |
+
```
|
145 |
+
```
|
146 |
+
@software{together2023redpajama,
|
147 |
+
author = {Together Computer},
|
148 |
+
title = {RedPajama-Data: An Open Source Recipe to Reproduce LLaMA training dataset},
|
149 |
+
month = April,
|
150 |
+
year = 2023,
|
151 |
+
url = {https://github.com/togethercomputer/RedPajama-Data}
|
152 |
+
}
|
153 |
+
```
|
154 |
+
```
|
155 |
+
@article{touvron2023llama,
|
156 |
+
title={Llama: Open and efficient foundation language models},
|
157 |
+
author={Touvron, Hugo and Lavril, Thibaut and Izacard, Gautier and Martinet, Xavier and Lachaux, Marie-Anne and Lacroix, Timoth{\'e}e and Rozi{\`e}re, Baptiste and Goyal, Naman and Hambro, Eric and Azhar, Faisal and others},
|
158 |
+
journal={arXiv preprint arXiv:2302.13971},
|
159 |
+
year={2023}
|
160 |
+
}
|
161 |
+
```
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
config.json
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"LlamaForCausalLM"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoModel": "modelling_llama.LlamaModel",
|
7 |
+
"AutoModelForCausalLM": "modelling_llama.LlamaForCausalLM",
|
8 |
+
"AutoModelForSequenceClassification": "modelling_llama.LlamaForSequenceClassification"
|
9 |
+
},
|
10 |
+
"bos_token_id": 1,
|
11 |
+
"eos_token_id": 2,
|
12 |
+
"hidden_act": "silu",
|
13 |
+
"hidden_size": 4096,
|
14 |
+
"initializer_range": 0.02,
|
15 |
+
"intermediate_size": 11008,
|
16 |
+
"max_position_embeddings": 2048,
|
17 |
+
"model_type": "llama",
|
18 |
+
"num_attention_heads": 32,
|
19 |
+
"num_hidden_layers": 32,
|
20 |
+
"pad_token_id": 0,
|
21 |
+
"rms_norm_eps": 1e-06,
|
22 |
+
"tie_word_embeddings": false,
|
23 |
+
"torch_dtype": "float16",
|
24 |
+
"transformers_version": "4.30.0.dev0",
|
25 |
+
"use_cache": true,
|
26 |
+
"vocab_size": 32000
|
27 |
+
}
|
generation_config.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 1,
|
4 |
+
"eos_token_id": 2,
|
5 |
+
"pad_token_id": 0,
|
6 |
+
"transformers_version": "4.30.0.dev0"
|
7 |
+
}
|
modelling_llama.py
ADDED
@@ -0,0 +1,895 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
""" PyTorch LLaMA model."""
|
21 |
+
import math
|
22 |
+
from typing import List, Optional, Tuple, Union
|
23 |
+
|
24 |
+
import torch
|
25 |
+
import torch.utils.checkpoint
|
26 |
+
from torch import nn
|
27 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
28 |
+
|
29 |
+
from transformers.activations import ACT2FN
|
30 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
|
31 |
+
from transformers.modeling_utils import PreTrainedModel
|
32 |
+
from transformers.utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
|
33 |
+
from transformers.models.llama.modeling_llama import LlamaConfig
|
34 |
+
|
35 |
+
|
36 |
+
logger = logging.get_logger(__name__)
|
37 |
+
|
38 |
+
_CONFIG_FOR_DOC = "LlamaConfig"
|
39 |
+
|
40 |
+
|
41 |
+
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
|
42 |
+
def _make_causal_mask(
|
43 |
+
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
44 |
+
):
|
45 |
+
"""
|
46 |
+
Make causal mask used for bi-directional self-attention.
|
47 |
+
"""
|
48 |
+
bsz, tgt_len = input_ids_shape
|
49 |
+
mask = torch.full((tgt_len, tgt_len), torch.tensor(torch.finfo(dtype).min, device=device), device=device)
|
50 |
+
mask_cond = torch.arange(mask.size(-1), device=device)
|
51 |
+
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
|
52 |
+
mask = mask.to(dtype)
|
53 |
+
|
54 |
+
if past_key_values_length > 0:
|
55 |
+
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
|
56 |
+
return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
|
57 |
+
|
58 |
+
|
59 |
+
# Copied from transformers.models.bart.modeling_bart._expand_mask
|
60 |
+
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
|
61 |
+
"""
|
62 |
+
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
|
63 |
+
"""
|
64 |
+
bsz, src_len = mask.size()
|
65 |
+
tgt_len = tgt_len if tgt_len is not None else src_len
|
66 |
+
|
67 |
+
expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
|
68 |
+
|
69 |
+
inverted_mask = 1.0 - expanded_mask
|
70 |
+
|
71 |
+
return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
|
72 |
+
|
73 |
+
|
74 |
+
class LlamaRMSNorm(nn.Module):
|
75 |
+
def __init__(self, hidden_size, eps=1e-6):
|
76 |
+
"""
|
77 |
+
LlamaRMSNorm is equivalent to T5LayerNorm
|
78 |
+
"""
|
79 |
+
super().__init__()
|
80 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
81 |
+
self.variance_epsilon = eps
|
82 |
+
|
83 |
+
def forward(self, hidden_states):
|
84 |
+
input_dtype = hidden_states.dtype
|
85 |
+
variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
|
86 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
87 |
+
|
88 |
+
return (self.weight * hidden_states).to(input_dtype)
|
89 |
+
|
90 |
+
|
91 |
+
class LlamaRotaryEmbedding(torch.nn.Module):
|
92 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, scale=1, device=None):
|
93 |
+
super().__init__()
|
94 |
+
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
|
95 |
+
self.register_buffer("inv_freq", inv_freq)
|
96 |
+
|
97 |
+
# Build here to make `torch.jit.trace` work.
|
98 |
+
self.max_seq_len_cached = max_position_embeddings
|
99 |
+
t = torch.arange(self.max_seq_len_cached, device=self.inv_freq.device, dtype=self.inv_freq.dtype)
|
100 |
+
|
101 |
+
self.scale = scale
|
102 |
+
t *= self.scale
|
103 |
+
|
104 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
105 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
106 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
107 |
+
dtype = torch.get_default_dtype()
|
108 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
|
109 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
|
110 |
+
|
111 |
+
def forward(self, x, seq_len=None):
|
112 |
+
# x: [bs, num_attention_heads, seq_len, head_size]
|
113 |
+
# This `if` block is unlikely to be run after we build sin/cos in `__init__`. Keep the logic here just in case.
|
114 |
+
if seq_len > self.max_seq_len_cached:
|
115 |
+
self.max_seq_len_cached = seq_len
|
116 |
+
t = torch.arange(self.max_seq_len_cached, device=x.device, dtype=self.inv_freq.dtype)
|
117 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
118 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
119 |
+
emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
|
120 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(x.dtype), persistent=False)
|
121 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(x.dtype), persistent=False)
|
122 |
+
return (
|
123 |
+
self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
124 |
+
self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
125 |
+
)
|
126 |
+
|
127 |
+
|
128 |
+
def rotate_half(x):
|
129 |
+
"""Rotates half the hidden dims of the input."""
|
130 |
+
x1 = x[..., : x.shape[-1] // 2]
|
131 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
132 |
+
return torch.cat((-x2, x1), dim=-1)
|
133 |
+
|
134 |
+
|
135 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
|
136 |
+
# The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
|
137 |
+
cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
|
138 |
+
sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
|
139 |
+
cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
140 |
+
sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
141 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
142 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
143 |
+
return q_embed, k_embed
|
144 |
+
|
145 |
+
|
146 |
+
class LlamaMLP(nn.Module):
|
147 |
+
def __init__(
|
148 |
+
self,
|
149 |
+
hidden_size: int,
|
150 |
+
intermediate_size: int,
|
151 |
+
hidden_act: str,
|
152 |
+
):
|
153 |
+
super().__init__()
|
154 |
+
self.gate_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
|
155 |
+
self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False)
|
156 |
+
self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
|
157 |
+
self.act_fn = ACT2FN[hidden_act]
|
158 |
+
|
159 |
+
def forward(self, x):
|
160 |
+
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
161 |
+
|
162 |
+
|
163 |
+
class LlamaAttention(nn.Module):
|
164 |
+
"""Multi-headed attention from 'Attention Is All You Need' paper"""
|
165 |
+
|
166 |
+
def __init__(self, config: LlamaConfig):
|
167 |
+
super().__init__()
|
168 |
+
self.config = config
|
169 |
+
self.hidden_size = config.hidden_size
|
170 |
+
self.num_heads = config.num_attention_heads
|
171 |
+
self.head_dim = self.hidden_size // self.num_heads
|
172 |
+
self.max_position_embeddings = config.max_position_embeddings
|
173 |
+
self.position_embeddings_scale = 2048 / self.max_position_embeddings
|
174 |
+
|
175 |
+
if (self.head_dim * self.num_heads) != self.hidden_size:
|
176 |
+
raise ValueError(
|
177 |
+
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
178 |
+
f" and `num_heads`: {self.num_heads})."
|
179 |
+
)
|
180 |
+
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
181 |
+
self.k_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
182 |
+
self.v_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
183 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
184 |
+
self.rotary_emb = LlamaRotaryEmbedding(self.head_dim, max_position_embeddings=self.max_position_embeddings, scale=self.position_embeddings_scale)
|
185 |
+
|
186 |
+
def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
|
187 |
+
return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
|
188 |
+
|
189 |
+
def forward(
|
190 |
+
self,
|
191 |
+
hidden_states: torch.Tensor,
|
192 |
+
attention_mask: Optional[torch.Tensor] = None,
|
193 |
+
position_ids: Optional[torch.LongTensor] = None,
|
194 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
195 |
+
output_attentions: bool = False,
|
196 |
+
use_cache: bool = False,
|
197 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
198 |
+
bsz, q_len, _ = hidden_states.size()
|
199 |
+
|
200 |
+
query_states = self.q_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
201 |
+
key_states = self.k_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
202 |
+
value_states = self.v_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
203 |
+
|
204 |
+
kv_seq_len = key_states.shape[-2]
|
205 |
+
if past_key_value is not None:
|
206 |
+
kv_seq_len += past_key_value[0].shape[-2]
|
207 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
208 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
209 |
+
# [bsz, nh, t, hd]
|
210 |
+
|
211 |
+
if past_key_value is not None:
|
212 |
+
# reuse k, v, self_attention
|
213 |
+
key_states = torch.cat([past_key_value[0], key_states], dim=2)
|
214 |
+
value_states = torch.cat([past_key_value[1], value_states], dim=2)
|
215 |
+
|
216 |
+
past_key_value = (key_states, value_states) if use_cache else None
|
217 |
+
|
218 |
+
attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
|
219 |
+
|
220 |
+
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
221 |
+
raise ValueError(
|
222 |
+
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
|
223 |
+
f" {attn_weights.size()}"
|
224 |
+
)
|
225 |
+
|
226 |
+
if attention_mask is not None:
|
227 |
+
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
|
228 |
+
raise ValueError(
|
229 |
+
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
|
230 |
+
)
|
231 |
+
attn_weights = attn_weights + attention_mask
|
232 |
+
attn_weights = torch.max(
|
233 |
+
attn_weights, torch.tensor(torch.finfo(attn_weights.dtype).min, device=attn_weights.device)
|
234 |
+
)
|
235 |
+
|
236 |
+
# upcast attention to fp32
|
237 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
|
238 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
239 |
+
|
240 |
+
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
|
241 |
+
raise ValueError(
|
242 |
+
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
|
243 |
+
f" {attn_output.size()}"
|
244 |
+
)
|
245 |
+
|
246 |
+
attn_output = attn_output.transpose(1, 2)
|
247 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
248 |
+
|
249 |
+
attn_output = self.o_proj(attn_output)
|
250 |
+
|
251 |
+
if not output_attentions:
|
252 |
+
attn_weights = None
|
253 |
+
|
254 |
+
return attn_output, attn_weights, past_key_value
|
255 |
+
|
256 |
+
|
257 |
+
class LlamaDecoderLayer(nn.Module):
|
258 |
+
def __init__(self, config: LlamaConfig):
|
259 |
+
super().__init__()
|
260 |
+
self.hidden_size = config.hidden_size
|
261 |
+
self.self_attn = LlamaAttention(config=config)
|
262 |
+
self.mlp = LlamaMLP(
|
263 |
+
hidden_size=self.hidden_size,
|
264 |
+
intermediate_size=config.intermediate_size,
|
265 |
+
hidden_act=config.hidden_act,
|
266 |
+
)
|
267 |
+
self.input_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
268 |
+
self.post_attention_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
269 |
+
|
270 |
+
def forward(
|
271 |
+
self,
|
272 |
+
hidden_states: torch.Tensor,
|
273 |
+
attention_mask: Optional[torch.Tensor] = None,
|
274 |
+
position_ids: Optional[torch.LongTensor] = None,
|
275 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
276 |
+
output_attentions: Optional[bool] = False,
|
277 |
+
use_cache: Optional[bool] = False,
|
278 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
279 |
+
"""
|
280 |
+
Args:
|
281 |
+
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
282 |
+
attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
|
283 |
+
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
|
284 |
+
output_attentions (`bool`, *optional*):
|
285 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
286 |
+
returned tensors for more detail.
|
287 |
+
use_cache (`bool`, *optional*):
|
288 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
289 |
+
(see `past_key_values`).
|
290 |
+
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
|
291 |
+
"""
|
292 |
+
|
293 |
+
residual = hidden_states
|
294 |
+
|
295 |
+
hidden_states = self.input_layernorm(hidden_states)
|
296 |
+
|
297 |
+
# Self Attention
|
298 |
+
hidden_states, self_attn_weights, present_key_value = self.self_attn(
|
299 |
+
hidden_states=hidden_states,
|
300 |
+
attention_mask=attention_mask,
|
301 |
+
position_ids=position_ids,
|
302 |
+
past_key_value=past_key_value,
|
303 |
+
output_attentions=output_attentions,
|
304 |
+
use_cache=use_cache,
|
305 |
+
)
|
306 |
+
hidden_states = residual + hidden_states
|
307 |
+
|
308 |
+
# Fully Connected
|
309 |
+
residual = hidden_states
|
310 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
311 |
+
hidden_states = self.mlp(hidden_states)
|
312 |
+
hidden_states = residual + hidden_states
|
313 |
+
|
314 |
+
outputs = (hidden_states,)
|
315 |
+
|
316 |
+
if output_attentions:
|
317 |
+
outputs += (self_attn_weights,)
|
318 |
+
|
319 |
+
if use_cache:
|
320 |
+
outputs += (present_key_value,)
|
321 |
+
|
322 |
+
return outputs
|
323 |
+
|
324 |
+
|
325 |
+
LLAMA_START_DOCSTRING = r"""
|
326 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
327 |
+
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
328 |
+
etc.)
|
329 |
+
|
330 |
+
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
331 |
+
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
332 |
+
and behavior.
|
333 |
+
|
334 |
+
Parameters:
|
335 |
+
config ([`LlamaConfig`]):
|
336 |
+
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
337 |
+
load the weights associated with the model, only the configuration. Check out the
|
338 |
+
[`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
339 |
+
"""
|
340 |
+
|
341 |
+
|
342 |
+
@add_start_docstrings(
|
343 |
+
"The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
|
344 |
+
LLAMA_START_DOCSTRING,
|
345 |
+
)
|
346 |
+
class LlamaPreTrainedModel(PreTrainedModel):
|
347 |
+
config_class = LlamaConfig
|
348 |
+
base_model_prefix = "model"
|
349 |
+
supports_gradient_checkpointing = True
|
350 |
+
_no_split_modules = ["LlamaDecoderLayer"]
|
351 |
+
_skip_keys_device_placement = "past_key_values"
|
352 |
+
_keys_to_ignore_on_load_unexpected = [r"decoder\.version"]
|
353 |
+
|
354 |
+
def _init_weights(self, module):
|
355 |
+
std = self.config.initializer_range
|
356 |
+
if isinstance(module, nn.Linear):
|
357 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
358 |
+
if module.bias is not None:
|
359 |
+
module.bias.data.zero_()
|
360 |
+
elif isinstance(module, nn.Embedding):
|
361 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
362 |
+
if module.padding_idx is not None:
|
363 |
+
module.weight.data[module.padding_idx].zero_()
|
364 |
+
|
365 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
366 |
+
if isinstance(module, LlamaModel):
|
367 |
+
module.gradient_checkpointing = value
|
368 |
+
|
369 |
+
|
370 |
+
LLAMA_INPUTS_DOCSTRING = r"""
|
371 |
+
Args:
|
372 |
+
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
373 |
+
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
374 |
+
it.
|
375 |
+
|
376 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
377 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
378 |
+
|
379 |
+
[What are input IDs?](../glossary#input-ids)
|
380 |
+
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
381 |
+
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
382 |
+
|
383 |
+
- 1 for tokens that are **not masked**,
|
384 |
+
- 0 for tokens that are **masked**.
|
385 |
+
|
386 |
+
[What are attention masks?](../glossary#attention-mask)
|
387 |
+
|
388 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
389 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
390 |
+
|
391 |
+
If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
|
392 |
+
`past_key_values`).
|
393 |
+
|
394 |
+
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
395 |
+
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
396 |
+
information on the default strategy.
|
397 |
+
|
398 |
+
- 1 indicates the head is **not masked**,
|
399 |
+
- 0 indicates the head is **masked**.
|
400 |
+
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
401 |
+
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
402 |
+
config.n_positions - 1]`.
|
403 |
+
|
404 |
+
[What are position IDs?](../glossary#position-ids)
|
405 |
+
past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
|
406 |
+
Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
|
407 |
+
`(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
|
408 |
+
`(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
|
409 |
+
|
410 |
+
Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
411 |
+
blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
|
412 |
+
|
413 |
+
If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
|
414 |
+
don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
|
415 |
+
`decoder_input_ids` of shape `(batch_size, sequence_length)`.
|
416 |
+
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
417 |
+
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
|
418 |
+
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
|
419 |
+
model's internal embedding lookup matrix.
|
420 |
+
use_cache (`bool`, *optional*):
|
421 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
|
422 |
+
`past_key_values`).
|
423 |
+
output_attentions (`bool`, *optional*):
|
424 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
|
425 |
+
tensors for more detail.
|
426 |
+
output_hidden_states (`bool`, *optional*):
|
427 |
+
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
|
428 |
+
more detail.
|
429 |
+
return_dict (`bool`, *optional*):
|
430 |
+
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
|
431 |
+
"""
|
432 |
+
|
433 |
+
|
434 |
+
@add_start_docstrings(
|
435 |
+
"The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
|
436 |
+
LLAMA_START_DOCSTRING,
|
437 |
+
)
|
438 |
+
class LlamaModel(LlamaPreTrainedModel):
|
439 |
+
"""
|
440 |
+
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`LlamaDecoderLayer`]
|
441 |
+
|
442 |
+
Args:
|
443 |
+
config: LlamaConfig
|
444 |
+
"""
|
445 |
+
|
446 |
+
def __init__(self, config: LlamaConfig):
|
447 |
+
super().__init__(config)
|
448 |
+
self.padding_idx = config.pad_token_id
|
449 |
+
self.vocab_size = config.vocab_size
|
450 |
+
|
451 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
452 |
+
self.layers = nn.ModuleList([LlamaDecoderLayer(config) for _ in range(config.num_hidden_layers)])
|
453 |
+
self.norm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
454 |
+
|
455 |
+
self.gradient_checkpointing = False
|
456 |
+
# Initialize weights and apply final processing
|
457 |
+
self.post_init()
|
458 |
+
|
459 |
+
def get_input_embeddings(self):
|
460 |
+
return self.embed_tokens
|
461 |
+
|
462 |
+
def set_input_embeddings(self, value):
|
463 |
+
self.embed_tokens = value
|
464 |
+
|
465 |
+
# Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
|
466 |
+
def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
|
467 |
+
# create causal mask
|
468 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
469 |
+
combined_attention_mask = None
|
470 |
+
if input_shape[-1] > 1:
|
471 |
+
combined_attention_mask = _make_causal_mask(
|
472 |
+
input_shape,
|
473 |
+
inputs_embeds.dtype,
|
474 |
+
device=inputs_embeds.device,
|
475 |
+
past_key_values_length=past_key_values_length,
|
476 |
+
)
|
477 |
+
|
478 |
+
if attention_mask is not None:
|
479 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
480 |
+
expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
|
481 |
+
inputs_embeds.device
|
482 |
+
)
|
483 |
+
combined_attention_mask = (
|
484 |
+
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
|
485 |
+
)
|
486 |
+
|
487 |
+
return combined_attention_mask
|
488 |
+
|
489 |
+
@add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
|
490 |
+
def forward(
|
491 |
+
self,
|
492 |
+
input_ids: torch.LongTensor = None,
|
493 |
+
attention_mask: Optional[torch.Tensor] = None,
|
494 |
+
position_ids: Optional[torch.LongTensor] = None,
|
495 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
496 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
497 |
+
use_cache: Optional[bool] = None,
|
498 |
+
output_attentions: Optional[bool] = None,
|
499 |
+
output_hidden_states: Optional[bool] = None,
|
500 |
+
return_dict: Optional[bool] = None,
|
501 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
502 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
503 |
+
output_hidden_states = (
|
504 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
505 |
+
)
|
506 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
507 |
+
|
508 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
509 |
+
|
510 |
+
# retrieve input_ids and inputs_embeds
|
511 |
+
if input_ids is not None and inputs_embeds is not None:
|
512 |
+
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
|
513 |
+
elif input_ids is not None:
|
514 |
+
batch_size, seq_length = input_ids.shape
|
515 |
+
elif inputs_embeds is not None:
|
516 |
+
batch_size, seq_length, _ = inputs_embeds.shape
|
517 |
+
else:
|
518 |
+
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
|
519 |
+
|
520 |
+
seq_length_with_past = seq_length
|
521 |
+
past_key_values_length = 0
|
522 |
+
|
523 |
+
if past_key_values is not None:
|
524 |
+
past_key_values_length = past_key_values[0][0].shape[2]
|
525 |
+
seq_length_with_past = seq_length_with_past + past_key_values_length
|
526 |
+
|
527 |
+
if position_ids is None:
|
528 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
529 |
+
position_ids = torch.arange(
|
530 |
+
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
|
531 |
+
)
|
532 |
+
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
|
533 |
+
else:
|
534 |
+
position_ids = position_ids.view(-1, seq_length).long()
|
535 |
+
|
536 |
+
if inputs_embeds is None:
|
537 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
538 |
+
# embed positions
|
539 |
+
if attention_mask is None:
|
540 |
+
attention_mask = torch.ones(
|
541 |
+
(batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
|
542 |
+
)
|
543 |
+
attention_mask = self._prepare_decoder_attention_mask(
|
544 |
+
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
|
545 |
+
)
|
546 |
+
|
547 |
+
hidden_states = inputs_embeds
|
548 |
+
|
549 |
+
if self.gradient_checkpointing and self.training:
|
550 |
+
if use_cache:
|
551 |
+
logger.warning_once(
|
552 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
553 |
+
)
|
554 |
+
use_cache = False
|
555 |
+
|
556 |
+
# decoder layers
|
557 |
+
all_hidden_states = () if output_hidden_states else None
|
558 |
+
all_self_attns = () if output_attentions else None
|
559 |
+
next_decoder_cache = () if use_cache else None
|
560 |
+
|
561 |
+
for idx, decoder_layer in enumerate(self.layers):
|
562 |
+
if output_hidden_states:
|
563 |
+
all_hidden_states += (hidden_states,)
|
564 |
+
|
565 |
+
past_key_value = past_key_values[idx] if past_key_values is not None else None
|
566 |
+
|
567 |
+
if self.gradient_checkpointing and self.training:
|
568 |
+
|
569 |
+
def create_custom_forward(module):
|
570 |
+
def custom_forward(*inputs):
|
571 |
+
# None for past_key_value
|
572 |
+
return module(*inputs, output_attentions, None)
|
573 |
+
|
574 |
+
return custom_forward
|
575 |
+
|
576 |
+
layer_outputs = torch.utils.checkpoint.checkpoint(
|
577 |
+
create_custom_forward(decoder_layer),
|
578 |
+
hidden_states,
|
579 |
+
attention_mask,
|
580 |
+
position_ids,
|
581 |
+
None,
|
582 |
+
)
|
583 |
+
else:
|
584 |
+
layer_outputs = decoder_layer(
|
585 |
+
hidden_states,
|
586 |
+
attention_mask=attention_mask,
|
587 |
+
position_ids=position_ids,
|
588 |
+
past_key_value=past_key_value,
|
589 |
+
output_attentions=output_attentions,
|
590 |
+
use_cache=use_cache,
|
591 |
+
)
|
592 |
+
|
593 |
+
hidden_states = layer_outputs[0]
|
594 |
+
|
595 |
+
if use_cache:
|
596 |
+
next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
|
597 |
+
|
598 |
+
if output_attentions:
|
599 |
+
all_self_attns += (layer_outputs[1],)
|
600 |
+
|
601 |
+
hidden_states = self.norm(hidden_states)
|
602 |
+
|
603 |
+
# add hidden states from the last decoder layer
|
604 |
+
if output_hidden_states:
|
605 |
+
all_hidden_states += (hidden_states,)
|
606 |
+
|
607 |
+
next_cache = next_decoder_cache if use_cache else None
|
608 |
+
if not return_dict:
|
609 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
|
610 |
+
return BaseModelOutputWithPast(
|
611 |
+
last_hidden_state=hidden_states,
|
612 |
+
past_key_values=next_cache,
|
613 |
+
hidden_states=all_hidden_states,
|
614 |
+
attentions=all_self_attns,
|
615 |
+
)
|
616 |
+
|
617 |
+
|
618 |
+
class LlamaForCausalLM(LlamaPreTrainedModel):
|
619 |
+
_tied_weights_keys = ["lm_head.weight"]
|
620 |
+
|
621 |
+
def __init__(self, config):
|
622 |
+
super().__init__(config)
|
623 |
+
self.model = LlamaModel(config)
|
624 |
+
|
625 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
626 |
+
|
627 |
+
# Initialize weights and apply final processing
|
628 |
+
self.post_init()
|
629 |
+
|
630 |
+
def get_input_embeddings(self):
|
631 |
+
return self.model.embed_tokens
|
632 |
+
|
633 |
+
def set_input_embeddings(self, value):
|
634 |
+
self.model.embed_tokens = value
|
635 |
+
|
636 |
+
def get_output_embeddings(self):
|
637 |
+
return self.lm_head
|
638 |
+
|
639 |
+
def set_output_embeddings(self, new_embeddings):
|
640 |
+
self.lm_head = new_embeddings
|
641 |
+
|
642 |
+
def set_decoder(self, decoder):
|
643 |
+
self.model = decoder
|
644 |
+
|
645 |
+
def get_decoder(self):
|
646 |
+
return self.model
|
647 |
+
|
648 |
+
@add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
|
649 |
+
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
|
650 |
+
def forward(
|
651 |
+
self,
|
652 |
+
input_ids: torch.LongTensor = None,
|
653 |
+
attention_mask: Optional[torch.Tensor] = None,
|
654 |
+
position_ids: Optional[torch.LongTensor] = None,
|
655 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
656 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
657 |
+
labels: Optional[torch.LongTensor] = None,
|
658 |
+
use_cache: Optional[bool] = None,
|
659 |
+
output_attentions: Optional[bool] = None,
|
660 |
+
output_hidden_states: Optional[bool] = None,
|
661 |
+
return_dict: Optional[bool] = None,
|
662 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
663 |
+
r"""
|
664 |
+
Args:
|
665 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
666 |
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
667 |
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
668 |
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
669 |
+
|
670 |
+
Returns:
|
671 |
+
|
672 |
+
Example:
|
673 |
+
|
674 |
+
```python
|
675 |
+
>>> from transformers import AutoTokenizer, LlamaForCausalLM
|
676 |
+
|
677 |
+
>>> model = LlamaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
678 |
+
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
679 |
+
|
680 |
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
681 |
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
682 |
+
|
683 |
+
>>> # Generate
|
684 |
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
685 |
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
686 |
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
687 |
+
```"""
|
688 |
+
|
689 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
690 |
+
output_hidden_states = (
|
691 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
692 |
+
)
|
693 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
694 |
+
|
695 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
696 |
+
outputs = self.model(
|
697 |
+
input_ids=input_ids,
|
698 |
+
attention_mask=attention_mask,
|
699 |
+
position_ids=position_ids,
|
700 |
+
past_key_values=past_key_values,
|
701 |
+
inputs_embeds=inputs_embeds,
|
702 |
+
use_cache=use_cache,
|
703 |
+
output_attentions=output_attentions,
|
704 |
+
output_hidden_states=output_hidden_states,
|
705 |
+
return_dict=return_dict,
|
706 |
+
)
|
707 |
+
|
708 |
+
hidden_states = outputs[0]
|
709 |
+
logits = self.lm_head(hidden_states)
|
710 |
+
|
711 |
+
loss = None
|
712 |
+
if labels is not None:
|
713 |
+
# Shift so that tokens < n predict n
|
714 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
715 |
+
shift_labels = labels[..., 1:].contiguous()
|
716 |
+
# Flatten the tokens
|
717 |
+
loss_fct = CrossEntropyLoss()
|
718 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
719 |
+
shift_labels = shift_labels.view(-1)
|
720 |
+
# Enable model parallelism
|
721 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
722 |
+
loss = loss_fct(shift_logits, shift_labels)
|
723 |
+
|
724 |
+
if not return_dict:
|
725 |
+
output = (logits,) + outputs[1:]
|
726 |
+
return (loss,) + output if loss is not None else output
|
727 |
+
|
728 |
+
return CausalLMOutputWithPast(
|
729 |
+
loss=loss,
|
730 |
+
logits=logits,
|
731 |
+
past_key_values=outputs.past_key_values,
|
732 |
+
hidden_states=outputs.hidden_states,
|
733 |
+
attentions=outputs.attentions,
|
734 |
+
)
|
735 |
+
|
736 |
+
def prepare_inputs_for_generation(
|
737 |
+
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
|
738 |
+
):
|
739 |
+
if past_key_values:
|
740 |
+
input_ids = input_ids[:, -1:]
|
741 |
+
|
742 |
+
position_ids = kwargs.get("position_ids", None)
|
743 |
+
if attention_mask is not None and position_ids is None:
|
744 |
+
# create position_ids on the fly for batch generation
|
745 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
746 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
747 |
+
if past_key_values:
|
748 |
+
position_ids = position_ids[:, -1].unsqueeze(-1)
|
749 |
+
|
750 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
751 |
+
if inputs_embeds is not None and past_key_values is None:
|
752 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
753 |
+
else:
|
754 |
+
model_inputs = {"input_ids": input_ids}
|
755 |
+
|
756 |
+
model_inputs.update(
|
757 |
+
{
|
758 |
+
"position_ids": position_ids,
|
759 |
+
"past_key_values": past_key_values,
|
760 |
+
"use_cache": kwargs.get("use_cache"),
|
761 |
+
"attention_mask": attention_mask,
|
762 |
+
}
|
763 |
+
)
|
764 |
+
return model_inputs
|
765 |
+
|
766 |
+
@staticmethod
|
767 |
+
def _reorder_cache(past_key_values, beam_idx):
|
768 |
+
reordered_past = ()
|
769 |
+
for layer_past in past_key_values:
|
770 |
+
reordered_past += (
|
771 |
+
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
|
772 |
+
)
|
773 |
+
return reordered_past
|
774 |
+
|
775 |
+
|
776 |
+
@add_start_docstrings(
|
777 |
+
"""
|
778 |
+
The LLaMa Model transformer with a sequence classification head on top (linear layer).
|
779 |
+
|
780 |
+
[`LlamaForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
781 |
+
(e.g. GPT-2) do.
|
782 |
+
|
783 |
+
Since it does classification on the last token, it requires to know the position of the last token. If a
|
784 |
+
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
785 |
+
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
786 |
+
padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
|
787 |
+
each row of the batch).
|
788 |
+
""",
|
789 |
+
LLAMA_START_DOCSTRING,
|
790 |
+
)
|
791 |
+
class LlamaForSequenceClassification(LlamaPreTrainedModel):
|
792 |
+
_keys_to_ignore_on_load_missing = [r"lm_head.weight"]
|
793 |
+
|
794 |
+
def __init__(self, config):
|
795 |
+
super().__init__(config)
|
796 |
+
self.num_labels = config.num_labels
|
797 |
+
self.model = LlamaModel(config)
|
798 |
+
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
799 |
+
|
800 |
+
# Initialize weights and apply final processing
|
801 |
+
self.post_init()
|
802 |
+
|
803 |
+
def get_input_embeddings(self):
|
804 |
+
return self.model.embed_tokens
|
805 |
+
|
806 |
+
def set_input_embeddings(self, value):
|
807 |
+
self.model.embed_tokens = value
|
808 |
+
|
809 |
+
@add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
|
810 |
+
def forward(
|
811 |
+
self,
|
812 |
+
input_ids: torch.LongTensor = None,
|
813 |
+
attention_mask: Optional[torch.Tensor] = None,
|
814 |
+
position_ids: Optional[torch.LongTensor] = None,
|
815 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
816 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
817 |
+
labels: Optional[torch.LongTensor] = None,
|
818 |
+
use_cache: Optional[bool] = None,
|
819 |
+
output_attentions: Optional[bool] = None,
|
820 |
+
output_hidden_states: Optional[bool] = None,
|
821 |
+
return_dict: Optional[bool] = None,
|
822 |
+
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
823 |
+
r"""
|
824 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
825 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
826 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
827 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
828 |
+
"""
|
829 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
830 |
+
|
831 |
+
transformer_outputs = self.model(
|
832 |
+
input_ids,
|
833 |
+
attention_mask=attention_mask,
|
834 |
+
position_ids=position_ids,
|
835 |
+
past_key_values=past_key_values,
|
836 |
+
inputs_embeds=inputs_embeds,
|
837 |
+
use_cache=use_cache,
|
838 |
+
output_attentions=output_attentions,
|
839 |
+
output_hidden_states=output_hidden_states,
|
840 |
+
return_dict=return_dict,
|
841 |
+
)
|
842 |
+
hidden_states = transformer_outputs[0]
|
843 |
+
logits = self.score(hidden_states)
|
844 |
+
|
845 |
+
if input_ids is not None:
|
846 |
+
batch_size = input_ids.shape[0]
|
847 |
+
else:
|
848 |
+
batch_size = inputs_embeds.shape[0]
|
849 |
+
|
850 |
+
if self.config.pad_token_id is None and batch_size != 1:
|
851 |
+
raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
|
852 |
+
if self.config.pad_token_id is None:
|
853 |
+
sequence_lengths = -1
|
854 |
+
else:
|
855 |
+
if input_ids is not None:
|
856 |
+
sequence_lengths = (torch.ne(input_ids, self.config.pad_token_id).sum(-1) - 1).to(logits.device)
|
857 |
+
else:
|
858 |
+
sequence_lengths = -1
|
859 |
+
|
860 |
+
pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
|
861 |
+
|
862 |
+
loss = None
|
863 |
+
if labels is not None:
|
864 |
+
labels = labels.to(logits.device)
|
865 |
+
if self.config.problem_type is None:
|
866 |
+
if self.num_labels == 1:
|
867 |
+
self.config.problem_type = "regression"
|
868 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
869 |
+
self.config.problem_type = "single_label_classification"
|
870 |
+
else:
|
871 |
+
self.config.problem_type = "multi_label_classification"
|
872 |
+
|
873 |
+
if self.config.problem_type == "regression":
|
874 |
+
loss_fct = MSELoss()
|
875 |
+
if self.num_labels == 1:
|
876 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
877 |
+
else:
|
878 |
+
loss = loss_fct(pooled_logits, labels)
|
879 |
+
elif self.config.problem_type == "single_label_classification":
|
880 |
+
loss_fct = CrossEntropyLoss()
|
881 |
+
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
882 |
+
elif self.config.problem_type == "multi_label_classification":
|
883 |
+
loss_fct = BCEWithLogitsLoss()
|
884 |
+
loss = loss_fct(pooled_logits, labels)
|
885 |
+
if not return_dict:
|
886 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
887 |
+
return ((loss,) + output) if loss is not None else output
|
888 |
+
|
889 |
+
return SequenceClassifierOutputWithPast(
|
890 |
+
loss=loss,
|
891 |
+
logits=pooled_logits,
|
892 |
+
past_key_values=transformer_outputs.past_key_values,
|
893 |
+
hidden_states=transformer_outputs.hidden_states,
|
894 |
+
attentions=transformer_outputs.attentions,
|
895 |
+
)
|
pytorch_model-00001-of-00002.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:66347239b4c055cb28879975813f20fd57807a133e8c5ec10f2287af4ffe242c
|
3 |
+
size 9976634558
|
pytorch_model-00002-of-00002.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d8764ea9d807e0bdadd901218911aa0e7ca421f66134e08c3326846865c1c786
|
3 |
+
size 3500315539
|
pytorch_model.bin.index.json
ADDED
@@ -0,0 +1,330 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"metadata": {
|
3 |
+
"total_size": 13476839424
|
4 |
+
},
|
5 |
+
"weight_map": {
|
6 |
+
"lm_head.weight": "pytorch_model-00002-of-00002.bin",
|
7 |
+
"model.embed_tokens.weight": "pytorch_model-00001-of-00002.bin",
|
8 |
+
"model.layers.0.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
9 |
+
"model.layers.0.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
10 |
+
"model.layers.0.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
11 |
+
"model.layers.0.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
12 |
+
"model.layers.0.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
13 |
+
"model.layers.0.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
14 |
+
"model.layers.0.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
15 |
+
"model.layers.0.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
16 |
+
"model.layers.0.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
17 |
+
"model.layers.0.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
18 |
+
"model.layers.1.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
19 |
+
"model.layers.1.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
20 |
+
"model.layers.1.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
21 |
+
"model.layers.1.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
22 |
+
"model.layers.1.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
23 |
+
"model.layers.1.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
24 |
+
"model.layers.1.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
25 |
+
"model.layers.1.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
26 |
+
"model.layers.1.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
27 |
+
"model.layers.1.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
28 |
+
"model.layers.10.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
29 |
+
"model.layers.10.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
30 |
+
"model.layers.10.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
31 |
+
"model.layers.10.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
32 |
+
"model.layers.10.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
33 |
+
"model.layers.10.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
34 |
+
"model.layers.10.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
35 |
+
"model.layers.10.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
36 |
+
"model.layers.10.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
37 |
+
"model.layers.10.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
38 |
+
"model.layers.11.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
39 |
+
"model.layers.11.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
40 |
+
"model.layers.11.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
41 |
+
"model.layers.11.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
42 |
+
"model.layers.11.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
43 |
+
"model.layers.11.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
44 |
+
"model.layers.11.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
45 |
+
"model.layers.11.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
46 |
+
"model.layers.11.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
47 |
+
"model.layers.11.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
48 |
+
"model.layers.12.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
49 |
+
"model.layers.12.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
50 |
+
"model.layers.12.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
51 |
+
"model.layers.12.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
52 |
+
"model.layers.12.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
53 |
+
"model.layers.12.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
54 |
+
"model.layers.12.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
55 |
+
"model.layers.12.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
56 |
+
"model.layers.12.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
57 |
+
"model.layers.12.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
58 |
+
"model.layers.13.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
59 |
+
"model.layers.13.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
60 |
+
"model.layers.13.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
61 |
+
"model.layers.13.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
62 |
+
"model.layers.13.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
63 |
+
"model.layers.13.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
64 |
+
"model.layers.13.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
65 |
+
"model.layers.13.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
66 |
+
"model.layers.13.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
67 |
+
"model.layers.13.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
68 |
+
"model.layers.14.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
69 |
+
"model.layers.14.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
70 |
+
"model.layers.14.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
71 |
+
"model.layers.14.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
72 |
+
"model.layers.14.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
73 |
+
"model.layers.14.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
74 |
+
"model.layers.14.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
75 |
+
"model.layers.14.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
76 |
+
"model.layers.14.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
77 |
+
"model.layers.14.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
78 |
+
"model.layers.15.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
79 |
+
"model.layers.15.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
80 |
+
"model.layers.15.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
81 |
+
"model.layers.15.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
82 |
+
"model.layers.15.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
83 |
+
"model.layers.15.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
84 |
+
"model.layers.15.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
85 |
+
"model.layers.15.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
86 |
+
"model.layers.15.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
87 |
+
"model.layers.15.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
88 |
+
"model.layers.16.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
89 |
+
"model.layers.16.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
90 |
+
"model.layers.16.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
91 |
+
"model.layers.16.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
92 |
+
"model.layers.16.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
93 |
+
"model.layers.16.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
94 |
+
"model.layers.16.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
95 |
+
"model.layers.16.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
96 |
+
"model.layers.16.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
97 |
+
"model.layers.16.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
98 |
+
"model.layers.17.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
99 |
+
"model.layers.17.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
100 |
+
"model.layers.17.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
101 |
+
"model.layers.17.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
102 |
+
"model.layers.17.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
103 |
+
"model.layers.17.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
104 |
+
"model.layers.17.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
105 |
+
"model.layers.17.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
106 |
+
"model.layers.17.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
107 |
+
"model.layers.17.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
108 |
+
"model.layers.18.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
109 |
+
"model.layers.18.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
110 |
+
"model.layers.18.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
111 |
+
"model.layers.18.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
112 |
+
"model.layers.18.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
113 |
+
"model.layers.18.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
114 |
+
"model.layers.18.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
115 |
+
"model.layers.18.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
116 |
+
"model.layers.18.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
117 |
+
"model.layers.18.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
118 |
+
"model.layers.19.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
119 |
+
"model.layers.19.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
120 |
+
"model.layers.19.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
121 |
+
"model.layers.19.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
122 |
+
"model.layers.19.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
123 |
+
"model.layers.19.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
124 |
+
"model.layers.19.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
125 |
+
"model.layers.19.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
126 |
+
"model.layers.19.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
127 |
+
"model.layers.19.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
128 |
+
"model.layers.2.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
129 |
+
"model.layers.2.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
130 |
+
"model.layers.2.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
131 |
+
"model.layers.2.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
132 |
+
"model.layers.2.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
133 |
+
"model.layers.2.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
134 |
+
"model.layers.2.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
135 |
+
"model.layers.2.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
136 |
+
"model.layers.2.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
137 |
+
"model.layers.2.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
138 |
+
"model.layers.20.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
139 |
+
"model.layers.20.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
140 |
+
"model.layers.20.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
141 |
+
"model.layers.20.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
142 |
+
"model.layers.20.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
143 |
+
"model.layers.20.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
144 |
+
"model.layers.20.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
145 |
+
"model.layers.20.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
146 |
+
"model.layers.20.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
147 |
+
"model.layers.20.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
148 |
+
"model.layers.21.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
149 |
+
"model.layers.21.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
150 |
+
"model.layers.21.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
151 |
+
"model.layers.21.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
152 |
+
"model.layers.21.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
153 |
+
"model.layers.21.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
154 |
+
"model.layers.21.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
155 |
+
"model.layers.21.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
156 |
+
"model.layers.21.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
157 |
+
"model.layers.21.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
158 |
+
"model.layers.22.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
159 |
+
"model.layers.22.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
160 |
+
"model.layers.22.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
161 |
+
"model.layers.22.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
162 |
+
"model.layers.22.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
163 |
+
"model.layers.22.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
164 |
+
"model.layers.22.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
165 |
+
"model.layers.22.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
166 |
+
"model.layers.22.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
167 |
+
"model.layers.22.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
168 |
+
"model.layers.23.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
169 |
+
"model.layers.23.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
170 |
+
"model.layers.23.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
171 |
+
"model.layers.23.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
172 |
+
"model.layers.23.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
173 |
+
"model.layers.23.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
174 |
+
"model.layers.23.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
175 |
+
"model.layers.23.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
176 |
+
"model.layers.23.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
177 |
+
"model.layers.23.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
178 |
+
"model.layers.24.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
179 |
+
"model.layers.24.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
180 |
+
"model.layers.24.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
181 |
+
"model.layers.24.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
182 |
+
"model.layers.24.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
183 |
+
"model.layers.24.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
184 |
+
"model.layers.24.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
185 |
+
"model.layers.24.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
186 |
+
"model.layers.24.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
187 |
+
"model.layers.24.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
188 |
+
"model.layers.25.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
189 |
+
"model.layers.25.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
190 |
+
"model.layers.25.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
191 |
+
"model.layers.25.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
192 |
+
"model.layers.25.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
193 |
+
"model.layers.25.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
194 |
+
"model.layers.25.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
195 |
+
"model.layers.25.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
196 |
+
"model.layers.25.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
197 |
+
"model.layers.25.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
198 |
+
"model.layers.26.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
199 |
+
"model.layers.26.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
200 |
+
"model.layers.26.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
201 |
+
"model.layers.26.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
202 |
+
"model.layers.26.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
203 |
+
"model.layers.26.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
204 |
+
"model.layers.26.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
205 |
+
"model.layers.26.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
206 |
+
"model.layers.26.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
207 |
+
"model.layers.26.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
208 |
+
"model.layers.27.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
209 |
+
"model.layers.27.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
210 |
+
"model.layers.27.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
211 |
+
"model.layers.27.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
212 |
+
"model.layers.27.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
213 |
+
"model.layers.27.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
214 |
+
"model.layers.27.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
215 |
+
"model.layers.27.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
216 |
+
"model.layers.27.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
217 |
+
"model.layers.27.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
218 |
+
"model.layers.28.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
219 |
+
"model.layers.28.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
220 |
+
"model.layers.28.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
221 |
+
"model.layers.28.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
222 |
+
"model.layers.28.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
223 |
+
"model.layers.28.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
224 |
+
"model.layers.28.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
225 |
+
"model.layers.28.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
226 |
+
"model.layers.28.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
227 |
+
"model.layers.28.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
228 |
+
"model.layers.29.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
229 |
+
"model.layers.29.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
230 |
+
"model.layers.29.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
231 |
+
"model.layers.29.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
232 |
+
"model.layers.29.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
233 |
+
"model.layers.29.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
234 |
+
"model.layers.29.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
235 |
+
"model.layers.29.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
236 |
+
"model.layers.29.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
237 |
+
"model.layers.29.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
238 |
+
"model.layers.3.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
239 |
+
"model.layers.3.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
240 |
+
"model.layers.3.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
241 |
+
"model.layers.3.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
242 |
+
"model.layers.3.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
243 |
+
"model.layers.3.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
244 |
+
"model.layers.3.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
245 |
+
"model.layers.3.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
246 |
+
"model.layers.3.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
247 |
+
"model.layers.3.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
248 |
+
"model.layers.30.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
249 |
+
"model.layers.30.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
250 |
+
"model.layers.30.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
251 |
+
"model.layers.30.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
252 |
+
"model.layers.30.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
253 |
+
"model.layers.30.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
254 |
+
"model.layers.30.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
255 |
+
"model.layers.30.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
256 |
+
"model.layers.30.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
257 |
+
"model.layers.30.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
258 |
+
"model.layers.31.input_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
259 |
+
"model.layers.31.mlp.down_proj.weight": "pytorch_model-00002-of-00002.bin",
|
260 |
+
"model.layers.31.mlp.gate_proj.weight": "pytorch_model-00002-of-00002.bin",
|
261 |
+
"model.layers.31.mlp.up_proj.weight": "pytorch_model-00002-of-00002.bin",
|
262 |
+
"model.layers.31.post_attention_layernorm.weight": "pytorch_model-00002-of-00002.bin",
|
263 |
+
"model.layers.31.self_attn.k_proj.weight": "pytorch_model-00002-of-00002.bin",
|
264 |
+
"model.layers.31.self_attn.o_proj.weight": "pytorch_model-00002-of-00002.bin",
|
265 |
+
"model.layers.31.self_attn.q_proj.weight": "pytorch_model-00002-of-00002.bin",
|
266 |
+
"model.layers.31.self_attn.rotary_emb.inv_freq": "pytorch_model-00002-of-00002.bin",
|
267 |
+
"model.layers.31.self_attn.v_proj.weight": "pytorch_model-00002-of-00002.bin",
|
268 |
+
"model.layers.4.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
269 |
+
"model.layers.4.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
270 |
+
"model.layers.4.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
271 |
+
"model.layers.4.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
272 |
+
"model.layers.4.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
273 |
+
"model.layers.4.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
274 |
+
"model.layers.4.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
275 |
+
"model.layers.4.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
276 |
+
"model.layers.4.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
277 |
+
"model.layers.4.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
278 |
+
"model.layers.5.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
279 |
+
"model.layers.5.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
280 |
+
"model.layers.5.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
281 |
+
"model.layers.5.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
282 |
+
"model.layers.5.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
283 |
+
"model.layers.5.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
284 |
+
"model.layers.5.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
285 |
+
"model.layers.5.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
286 |
+
"model.layers.5.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
287 |
+
"model.layers.5.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
288 |
+
"model.layers.6.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
289 |
+
"model.layers.6.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
290 |
+
"model.layers.6.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
291 |
+
"model.layers.6.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
292 |
+
"model.layers.6.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
293 |
+
"model.layers.6.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
294 |
+
"model.layers.6.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
295 |
+
"model.layers.6.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
296 |
+
"model.layers.6.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
297 |
+
"model.layers.6.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
298 |
+
"model.layers.7.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
299 |
+
"model.layers.7.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
300 |
+
"model.layers.7.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
301 |
+
"model.layers.7.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
302 |
+
"model.layers.7.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
303 |
+
"model.layers.7.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
304 |
+
"model.layers.7.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
305 |
+
"model.layers.7.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
306 |
+
"model.layers.7.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
307 |
+
"model.layers.7.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
308 |
+
"model.layers.8.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
309 |
+
"model.layers.8.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
310 |
+
"model.layers.8.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
311 |
+
"model.layers.8.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
312 |
+
"model.layers.8.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
313 |
+
"model.layers.8.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
314 |
+
"model.layers.8.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
315 |
+
"model.layers.8.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
316 |
+
"model.layers.8.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
317 |
+
"model.layers.8.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
318 |
+
"model.layers.9.input_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
319 |
+
"model.layers.9.mlp.down_proj.weight": "pytorch_model-00001-of-00002.bin",
|
320 |
+
"model.layers.9.mlp.gate_proj.weight": "pytorch_model-00001-of-00002.bin",
|
321 |
+
"model.layers.9.mlp.up_proj.weight": "pytorch_model-00001-of-00002.bin",
|
322 |
+
"model.layers.9.post_attention_layernorm.weight": "pytorch_model-00001-of-00002.bin",
|
323 |
+
"model.layers.9.self_attn.k_proj.weight": "pytorch_model-00001-of-00002.bin",
|
324 |
+
"model.layers.9.self_attn.o_proj.weight": "pytorch_model-00001-of-00002.bin",
|
325 |
+
"model.layers.9.self_attn.q_proj.weight": "pytorch_model-00001-of-00002.bin",
|
326 |
+
"model.layers.9.self_attn.rotary_emb.inv_freq": "pytorch_model-00001-of-00002.bin",
|
327 |
+
"model.layers.9.self_attn.v_proj.weight": "pytorch_model-00001-of-00002.bin",
|
328 |
+
"model.norm.weight": "pytorch_model-00002-of-00002.bin"
|
329 |
+
}
|
330 |
+
}
|
special_tokens_map.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"bos_token": {"content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false}, "eos_token": {"content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false}, "unk_token": {"content": "<unk>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false}}
|
tokenizer.model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ab1b681ec7fc02fed5edd3026687d7a692a918c4dd8e150ca2e3994a6229843b
|
3 |
+
size 534194
|
tokenizer_config.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"add_bos_token": true, "add_eos_token": false, "model_max_length": 2048, "pad_token": null, "sp_model_kwargs": {}, "tokenizer_class": "LlamaTokenizer", "clean_up_tokenization_spaces": false, "bos_token": {"__type": "AddedToken", "content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false}, "eos_token": {"__type": "AddedToken", "content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false}, "unk_token": {"__type": "AddedToken", "content": "<unk>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false}}
|