Jamie@TitanML commited on
Commit
8abd6f0
1 Parent(s): 01ab111

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -25,7 +25,6 @@
25
  *.safetensors filter=lfs diff=lfs merge=lfs -text
26
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
  *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
  *.tflite filter=lfs diff=lfs merge=lfs -text
30
  *.tgz filter=lfs diff=lfs merge=lfs -text
31
  *.wasm filter=lfs diff=lfs merge=lfs -text
 
25
  *.safetensors filter=lfs diff=lfs merge=lfs -text
26
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
  *.tar.* filter=lfs diff=lfs merge=lfs -text
 
28
  *.tflite filter=lfs diff=lfs merge=lfs -text
29
  *.tgz filter=lfs diff=lfs merge=lfs -text
30
  *.wasm filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ datasets:
3
+ - tiiuae/falcon-refinedweb
4
+ language:
5
+ - en
6
+ inference: true
7
+ widget:
8
+ - text: "Hey Falcon! Any recommendations for my holidays in Abu Dhabi?"
9
+ example_title: "Abu Dhabi Trip"
10
+ - text: "What's the Everett interpretation of quantum mechanics?"
11
+ example_title: "Q/A: Quantum & Answers"
12
+ - text: "Give me a list of the top 10 dive sites you would recommend around the world."
13
+ example_title: "Diving Top 10"
14
+ - text: "Can you tell me more about deep-water soloing?"
15
+ example_title: "Extreme sports"
16
+ - text: "Can you write a short tweet about the Apache 2.0 release of our latest AI model, Falcon LLM?"
17
+ example_title: "Twitter Helper"
18
+ - text: "What are the responsabilities of a Chief Llama Officer?"
19
+ example_title: "Trendy Jobs"
20
+ license: apache-2.0
21
+ ---
22
+
23
+ # ✨ Falcon-7B-Instruct
24
+
25
+ **Falcon-7B-Instruct is a 7B parameters causal decoder-only model built by [TII](https://www.tii.ae) based on [Falcon-7B](https://huggingface.co/tiiuae/falcon-7b) and finetuned on a mixture of chat/instruct datasets. It is made available under the Apache 2.0 license.**
26
+
27
+ *Paper coming soon 😊.*
28
+
29
+ 🤗 To get started with Falcon (inference, finetuning, quantization, etc.), we recommend reading [this great blogpost fron HF](https://huggingface.co/blog/falcon)!
30
+
31
+ ## Why use Falcon-7B-Instruct?
32
+
33
+ * **You are looking for a ready-to-use chat/instruct model based on [Falcon-7B](https://huggingface.co/tiiuae/falcon-7b).**
34
+ * **Falcon-7B is a strong base model, outperforming comparable open-source models** (e.g., [MPT-7B](https://huggingface.co/mosaicml/mpt-7b), [StableLM](https://github.com/Stability-AI/StableLM), [RedPajama](https://huggingface.co/togethercomputer/RedPajama-INCITE-Base-7B-v0.1) etc.), thanks to being trained on 1,500B tokens of [RefinedWeb](https://huggingface.co/datasets/tiiuae/falcon-refinedweb) enhanced with curated corpora. See the [OpenLLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard).
35
+ * **It features an architecture optimized for inference**, with FlashAttention ([Dao et al., 2022](https://arxiv.org/abs/2205.14135)) and multiquery ([Shazeer et al., 2019](https://arxiv.org/abs/1911.02150)).
36
+
37
+ 💬 **This is an instruct model, which may not be ideal for further finetuning.** If you are interested in building your own instruct/chat model, we recommend starting from [Falcon-7B](https://huggingface.co/tiiuae/falcon-7b).
38
+
39
+ 🔥 **Looking for an even more powerful model?** [Falcon-40B-Instruct](https://huggingface.co/tiiuae/falcon-40b-instruct) is Falcon-7B-Instruct's big brother!
40
+
41
+ ```python
42
+ from transformers import AutoTokenizer, AutoModelForCausalLM
43
+ import transformers
44
+ import torch
45
+
46
+ model = "tiiuae/falcon-7b-instruct"
47
+
48
+ tokenizer = AutoTokenizer.from_pretrained(model)
49
+ pipeline = transformers.pipeline(
50
+ "text-generation",
51
+ model=model,
52
+ tokenizer=tokenizer,
53
+ torch_dtype=torch.bfloat16,
54
+ trust_remote_code=True,
55
+ device_map="auto",
56
+ )
57
+ sequences = pipeline(
58
+ "Girafatron is obsessed with giraffes, the most glorious animal on the face of this Earth. Giraftron believes all other animals are irrelevant when compared to the glorious majesty of the giraffe.\nDaniel: Hello, Girafatron!\nGirafatron:",
59
+ max_length=200,
60
+ do_sample=True,
61
+ top_k=10,
62
+ num_return_sequences=1,
63
+ eos_token_id=tokenizer.eos_token_id,
64
+ )
65
+ for seq in sequences:
66
+ print(f"Result: {seq['generated_text']}")
67
+
68
+ ```
69
+
70
+ 💥 **Falcon LLMs require PyTorch 2.0 for use with `transformers`!**
71
+
72
+ For fast inference with Falcon, check-out [Text Generation Inference](https://github.com/huggingface/text-generation-inference)! Read more in this [blogpost]((https://huggingface.co/blog/falcon).
73
+
74
+ You will need **at least 16GB of memory** to swiftly run inference with Falcon-7B-Instruct.
75
+
76
+
77
+ # Model Card for Falcon-7B-Instruct
78
+
79
+ ## Model Details
80
+
81
+ ### Model Description
82
+
83
+ - **Developed by:** [https://www.tii.ae](https://www.tii.ae);
84
+ - **Model type:** Causal decoder-only;
85
+ - **Language(s) (NLP):** English and French;
86
+ - **License:** Apache 2.0;
87
+ - **Finetuned from model:** [Falcon-7B](https://huggingface.co/tiiuae/falcon-7b).
88
+
89
+ ### Model Source
90
+
91
+ - **Paper:** *coming soon*.
92
+
93
+ ## Uses
94
+
95
+ ### Direct Use
96
+
97
+ Falcon-7B-Instruct has been finetuned on a mixture of instruct and chat datasets.
98
+
99
+ ### Out-of-Scope Use
100
+
101
+ Production use without adequate assessment of risks and mitigation; any use cases which may be considered irresponsible or harmful.
102
+
103
+ ## Bias, Risks, and Limitations
104
+
105
+ Falcon-7B-Instruct is mostly trained on English data, and will not generalize appropriately to other languages. Furthermore, as it is trained on a large-scale corpora representative of the web, it will carry the stereotypes and biases commonly encountered online.
106
+
107
+ ### Recommendations
108
+
109
+ We recommend users of Falcon-7B-Instruct to develop guardrails and to take appropriate precautions for any production use.
110
+
111
+ ## How to Get Started with the Model
112
+
113
+
114
+ ```python
115
+ from transformers import AutoTokenizer, AutoModelForCausalLM
116
+ import transformers
117
+ import torch
118
+
119
+ model = "tiiuae/falcon-7b-instruct"
120
+
121
+ tokenizer = AutoTokenizer.from_pretrained(model)
122
+ pipeline = transformers.pipeline(
123
+ "text-generation",
124
+ model=model,
125
+ tokenizer=tokenizer,
126
+ torch_dtype=torch.bfloat16,
127
+ trust_remote_code=True,
128
+ device_map="auto",
129
+ )
130
+ sequences = pipeline(
131
+ "Girafatron is obsessed with giraffes, the most glorious animal on the face of this Earth. Giraftron believes all other animals are irrelevant when compared to the glorious majesty of the giraffe.\nDaniel: Hello, Girafatron!\nGirafatron:",
132
+ max_length=200,
133
+ do_sample=True,
134
+ top_k=10,
135
+ num_return_sequences=1,
136
+ eos_token_id=tokenizer.eos_token_id,
137
+ )
138
+ for seq in sequences:
139
+ print(f"Result: {seq['generated_text']}")
140
+
141
+ ```
142
+
143
+ ## Training Details
144
+
145
+ ### Training Data
146
+
147
+ Falcon-7B-Instruct was finetuned on a 250M tokens mixture of instruct/chat datasets.
148
+
149
+ | **Data source** | **Fraction** | **Tokens** | **Description** |
150
+ |--------------------|--------------|------------|-----------------------------------|
151
+ | [Bai ze](https://github.com/project-baize/baize-chatbot) | 65% | 164M | chat |
152
+ | [GPT4All](https://github.com/nomic-ai/gpt4all) | 25% | 62M | instruct |
153
+ | [GPTeacher](https://github.com/teknium1/GPTeacher) | 5% | 11M | instruct |
154
+ | [RefinedWeb-English](https://huggingface.co/datasets/tiiuae/falcon-refinedweb) | 5% | 13M | massive web crawl |
155
+
156
+
157
+ The data was tokenized with the Falcon-[7B](https://huggingface.co/tiiuae/falcon-7b)/[40B](https://huggingface.co/tiiuae/falcon-40b) tokenizer.
158
+
159
+
160
+ ## Evaluation
161
+
162
+ *Paper coming soon.*
163
+
164
+ See the [OpenLLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard) for early results.
165
+
166
+ Note that this model variant is not optimized for NLP benchmarks.
167
+
168
+
169
+ ## Technical Specifications
170
+
171
+ For more information about pretraining, see [Falcon-7B](https://huggingface.co/tiiuae/falcon-7b).
172
+
173
+ ### Model Architecture and Objective
174
+
175
+ Falcon-7B is a causal decoder-only model trained on a causal language modeling task (i.e., predict the next token).
176
+
177
+ The architecture is broadly adapted from the GPT-3 paper ([Brown et al., 2020](https://arxiv.org/abs/2005.14165)), with the following differences:
178
+
179
+ * **Positionnal embeddings:** rotary ([Su et al., 2021](https://arxiv.org/abs/2104.09864));
180
+ * **Attention:** multiquery ([Shazeer et al., 2019](https://arxiv.org/abs/1911.02150)) and FlashAttention ([Dao et al., 2022](https://arxiv.org/abs/2205.14135));
181
+ * **Decoder-block:** parallel attention/MLP with a single layer norm.
182
+
183
+ | **Hyperparameter** | **Value** | **Comment** |
184
+ |--------------------|-----------|----------------------------------------|
185
+ | Layers | 32 | |
186
+ | `d_model` | 4544 | Increased to compensate for multiquery |
187
+ | `head_dim` | 64 | Reduced to optimise for FlashAttention |
188
+ | Vocabulary | 65024 | |
189
+ | Sequence length | 2048 | |
190
+
191
+ ### Compute Infrastructure
192
+
193
+ #### Hardware
194
+
195
+ Falcon-7B-Instruct was trained on AWS SageMaker, on 32 A100 40GB GPUs in P4d instances.
196
+
197
+ #### Software
198
+
199
+ Falcon-7B-Instruct was trained a custom distributed training codebase, Gigatron. It uses a 3D parallelism approach combined with ZeRO and high-performance Triton kernels (FlashAttention, etc.)
200
+
201
+
202
+ ## Citation
203
+
204
+ *Paper coming soon* 😊. In the meanwhile, you can use the following information to cite:
205
+ ```
206
+ @article{falcon40b,
207
+ title={{Falcon-40B}: an open large language model with state-of-the-art performance},
208
+ author={Almazrouei, Ebtesam and Alobeidli, Hamza and Alshamsi, Abdulaziz and Cappelli, Alessandro and Cojocaru, Ruxandra and Debbah, Merouane and Goffinet, Etienne and Heslow, Daniel and Launay, Julien and Malartic, Quentin and Noune, Badreddine and Pannier, Baptiste and Penedo, Guilherme},
209
+ year={2023}
210
+ }
211
+ ```
212
+
213
+ To learn more about the pretraining dataset, see the 📓 [RefinedWeb paper](https://arxiv.org/abs/2306.01116).
214
+
215
+ ```
216
+ @article{refinedweb,
217
+ title={The {R}efined{W}eb dataset for {F}alcon {LLM}: outperforming curated corpora with web data, and web data only},
218
+ author={Guilherme Penedo and Quentin Malartic and Daniel Hesslow and Ruxandra Cojocaru and Alessandro Cappelli and Hamza Alobeidli and Baptiste Pannier and Ebtesam Almazrouei and Julien Launay},
219
+ journal={arXiv preprint arXiv:2306.01116},
220
+ eprint={2306.01116},
221
+ eprinttype = {arXiv},
222
+ url={https://arxiv.org/abs/2306.01116},
223
+ year={2023}
224
+ }
225
+ ```
226
+
227
+
228
+ ## License
229
+
230
+ Falcon-7B-Instruct is made available under the Apache 2.0 license.
231
+
232
+ ## Contact
233
+ falconllm@tii.ae
config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alibi": false,
3
+ "apply_residual_connection_post_layernorm": false,
4
+ "architectures": [
5
+ "RWForCausalLM"
6
+ ],
7
+ "attention_dropout": 0.0,
8
+ "auto_map": {
9
+ "AutoConfig": "configuration_RW.RWConfig",
10
+ "AutoModelForCausalLM": "modelling_RW.RWForCausalLM"
11
+ },
12
+ "bias": false,
13
+ "bos_token_id": 11,
14
+ "eos_token_id": 11,
15
+ "hidden_dropout": 0.0,
16
+ "hidden_size": 4544,
17
+ "initializer_range": 0.02,
18
+ "layer_norm_epsilon": 1e-05,
19
+ "model_type": "RefinedWebModel",
20
+ "multi_query": true,
21
+ "n_head": 71,
22
+ "n_layer": 32,
23
+ "parallel_attn": true,
24
+ "torch_dtype": "bfloat16",
25
+ "transformers_version": "4.27.4",
26
+ "use_cache": true,
27
+ "vocab_size": 65024
28
+ }
configuration_RW.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 the Big Science Workshop and 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
+ """ Bloom configuration"""
16
+ from transformers.configuration_utils import PretrainedConfig
17
+ from transformers.utils import logging
18
+
19
+
20
+ logger = logging.get_logger(__name__)
21
+
22
+
23
+ class RWConfig(PretrainedConfig):
24
+ model_type = "RefinedWebModel"
25
+ keys_to_ignore_at_inference = ["past_key_values"]
26
+ attribute_map = {
27
+ "num_hidden_layers": "n_layer",
28
+ "num_attention_heads": "n_head",
29
+ }
30
+
31
+ def __init__(
32
+ self,
33
+ vocab_size=250880,
34
+ hidden_size=64,
35
+ n_layer=2,
36
+ n_head=8,
37
+ layer_norm_epsilon=1e-5,
38
+ initializer_range=0.02,
39
+ use_cache=True,
40
+ bos_token_id=1,
41
+ eos_token_id=2,
42
+ apply_residual_connection_post_layernorm=False,
43
+ hidden_dropout=0.0,
44
+ attention_dropout=0.0,
45
+ multi_query=False,
46
+ alibi=False,
47
+ bias=False,
48
+ parallel_attn=False,
49
+ **kwargs,
50
+ ):
51
+ self.vocab_size = vocab_size
52
+ # Backward compatibility with n_embed kwarg
53
+ n_embed = kwargs.pop("n_embed", None)
54
+ self.hidden_size = hidden_size if n_embed is None else n_embed
55
+ self.n_layer = n_layer
56
+ self.n_head = n_head
57
+ self.layer_norm_epsilon = layer_norm_epsilon
58
+ self.initializer_range = initializer_range
59
+ self.use_cache = use_cache
60
+ self.apply_residual_connection_post_layernorm = apply_residual_connection_post_layernorm
61
+ self.hidden_dropout = hidden_dropout
62
+ self.attention_dropout = attention_dropout
63
+
64
+ self.bos_token_id = bos_token_id
65
+ self.eos_token_id = eos_token_id
66
+ self.multi_query = multi_query
67
+ self.alibi = alibi
68
+ self.bias = bias
69
+ self.parallel_attn = parallel_attn
70
+
71
+ super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
72
+
73
+ @property
74
+ def head_dim(self):
75
+ return self.hidden_size // self.n_head
76
+
77
+ @property
78
+ def rotary(self):
79
+ return not self.alibi
ct_output_models_bfloat16/config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<|endoftext|>",
3
+ "eos_token": "<|endoftext|>",
4
+ "layer_norm_epsilon": null,
5
+ "unk_token": "<|endoftext|>"
6
+ }
ct_output_models_bfloat16/model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:559e9b1ed0e82373ee20bbe620df20bdf4572b162124ee149e5da35a3a6236fa
3
+ size 13843458146
ct_output_models_bfloat16/vocabulary.json ADDED
The diff for this file is too large to render. See raw diff
 
generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "transformers_version": "4.27.4"
6
+ }
handler.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from typing import Any, Dict
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+
7
+ class EndpointHandler:
8
+ def __init__(self, path=""):
9
+ # load model and tokenizer from path
10
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
11
+ self.model = AutoModelForCausalLM.from_pretrained(
12
+ path, device_map="auto", torch_dtype=torch.float16, trust_remote_code=True
13
+ )
14
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
15
+
16
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
17
+ # process input
18
+ inputs = data.pop("inputs", data)
19
+ parameters = data.pop("parameters", None)
20
+
21
+ # preprocess
22
+ inputs = self.tokenizer(inputs, return_tensors="pt").to(self.device)
23
+
24
+ # pass inputs with all kwargs in data
25
+ if parameters is not None:
26
+ outputs = self.model.generate(**inputs, **parameters)
27
+ else:
28
+ outputs = self.model.generate(**inputs)
29
+
30
+ # postprocess the prediction
31
+ prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+
33
+ return [{"generated_text": prediction}]
modelling_RW.py ADDED
@@ -0,0 +1,1100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # port of models described in RW
2
+ # We use the bloom model as a starting point for these model.
3
+ # Please refer to the bloom models for usage instructions.
4
+
5
+ import math
6
+ import warnings
7
+ from typing import Optional, Tuple, Union
8
+
9
+ import torch
10
+ import torch.utils.checkpoint
11
+ from torch import nn
12
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, LayerNorm, MSELoss
13
+ from torch.nn import functional as F
14
+
15
+ from transformers.modeling_outputs import (
16
+ BaseModelOutputWithPastAndCrossAttentions,
17
+ CausalLMOutputWithCrossAttentions,
18
+ QuestionAnsweringModelOutput,
19
+ SequenceClassifierOutputWithPast,
20
+ TokenClassifierOutput,
21
+ )
22
+ from transformers.modeling_utils import PreTrainedModel
23
+ from transformers.utils import logging
24
+ from .configuration_RW import RWConfig
25
+
26
+ logger = logging.get_logger(__name__)
27
+
28
+ # NOTE(Hesslow): Unfortunately we did not fuse matmul and bias during training, this means that there's one additional quantization to bfloat16 between the operations.
29
+ # In order not to degrade the quality of our HF-port, we keep these characteristics in the final model.
30
+ class Linear(nn.Linear):
31
+ def forward(self, input: torch.Tensor) -> torch.Tensor:
32
+ ret = input @ self.weight.T
33
+ if self.bias is None:
34
+ return ret
35
+ else:
36
+ return ret + self.bias
37
+
38
+
39
+ from einops import rearrange
40
+
41
+ # rotary pos emb helpers (torch.jit.script does not seem to support staticmethod...)
42
+ def rotate_half(x):
43
+ x1, x2 = x[..., : x.shape[-1] // 2], x[..., x.shape[-1] // 2 :]
44
+ return torch.cat((-x2, x1), dim=x1.ndim - 1) # dim=-1 triggers a bug in torch < 1.8.0
45
+
46
+
47
+ class RotaryEmbedding(torch.nn.Module):
48
+ """Implementation of RotaryEmbedding from GPT-NeoX.
49
+ This implementation is design to operate on queries and keys that are compatible with
50
+ [batch_size, n_heads_per_partition, seq_len, head_dim] (e.g. MinGPTAttention format).
51
+ """
52
+
53
+ def __init__(
54
+ self,
55
+ head_dim: int,
56
+ base=10000,
57
+ ):
58
+ super().__init__()
59
+ inv_freq = 1.0 / (base ** (torch.arange(0, head_dim, 2).float() / head_dim))
60
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
61
+ self.head_dim = head_dim
62
+ self.seq_len_cached = None
63
+ self.batch_size_cached = None
64
+ self.cos_cached: torch.Tensor | None = None
65
+ self.sin_cached: torch.Tensor | None = None
66
+
67
+ def cos_sin(
68
+ self,
69
+ seq_len: int,
70
+ device="cuda",
71
+ dtype=torch.bfloat16,
72
+ ) -> torch.Tensor:
73
+ if seq_len != self.seq_len_cached:
74
+ self.seq_len_cached = seq_len
75
+ t = torch.arange(seq_len, device=device).type_as(self.inv_freq)
76
+ freqs = torch.einsum("i,j->ij", t, self.inv_freq)
77
+ emb = torch.cat((freqs, freqs), dim=-1).to(device)
78
+
79
+ if dtype in [torch.float16, torch.bfloat16]:
80
+ emb = emb.float()
81
+
82
+ self.cos_cached = emb.cos()[None, :, :]
83
+ self.sin_cached = emb.sin()[None, :, :]
84
+
85
+ self.cos_cached = self.cos_cached.type(dtype)
86
+ self.sin_cached = self.sin_cached.type(dtype)
87
+
88
+ return self.cos_cached, self.sin_cached
89
+
90
+ def forward(self, q, k):
91
+ batch, seq_len, head_dim = q.shape
92
+ cos, sin = self.cos_sin(seq_len, q.device, q.dtype)
93
+ return (q * cos) + (rotate_half(q) * sin), (k * cos) + (rotate_half(k) * sin)
94
+
95
+
96
+ def _make_causal_mask(
97
+ input_ids_shape: torch.Size, device: torch.device, past_key_values_length: int
98
+ ) -> torch.BoolTensor:
99
+ batch_size, target_length = input_ids_shape
100
+ mask = torch.empty((target_length, target_length + past_key_values_length), dtype=torch.bool, device=device)
101
+ # ONNX doesn't support `torch.Tensor.triu` properly, thus we use this workaround
102
+ seq_ids = torch.arange(target_length, device=device)
103
+ mask[:, past_key_values_length:] = seq_ids[:, None] < seq_ids[None, :]
104
+
105
+ if past_key_values_length > 0:
106
+ mask[:, :past_key_values_length] = False
107
+
108
+ expanded_mask = mask[None, None, :, :].expand(batch_size, 1, target_length, target_length + past_key_values_length)
109
+ return expanded_mask
110
+
111
+
112
+ def _expand_mask(mask: torch.Tensor, tgt_length: int) -> torch.BoolTensor:
113
+ batch_size, src_length = mask.shape
114
+ tgt_length = tgt_length if tgt_length is not None else src_length
115
+
116
+ expanded_mask = ~(mask[:, None, None, :].to(torch.bool))
117
+ return expanded_mask.expand(batch_size, 1, tgt_length, src_length)
118
+
119
+
120
+ def build_alibi_tensor(attention_mask: torch.Tensor, num_heads: int, dtype: torch.dtype) -> torch.Tensor:
121
+ batch_size, seq_length = attention_mask.shape
122
+ closest_power_of_2 = 2 ** math.floor(math.log2(num_heads))
123
+ base = torch.tensor(
124
+ 2 ** (-(2 ** -(math.log2(closest_power_of_2) - 3))), device=attention_mask.device, dtype=torch.float32
125
+ )
126
+ powers = torch.arange(1, 1 + closest_power_of_2, device=attention_mask.device, dtype=torch.int32)
127
+ slopes = torch.pow(base, powers)
128
+
129
+ if closest_power_of_2 != num_heads:
130
+ extra_base = torch.tensor(
131
+ 2 ** (-(2 ** -(math.log2(2 * closest_power_of_2) - 3))), device=attention_mask.device, dtype=torch.float32
132
+ )
133
+ num_remaining_heads = min(closest_power_of_2, num_heads - closest_power_of_2)
134
+ extra_powers = torch.arange(1, 1 + 2 * num_remaining_heads, 2, device=attention_mask.device, dtype=torch.int32)
135
+ slopes = torch.cat([slopes, torch.pow(extra_base, extra_powers)], dim=0)
136
+
137
+ # Note: alibi will added to the attention bias that will be applied to the query, key product of attention
138
+ # => therefore alibi will have to be of shape (batch_size, num_heads, query_length, key_length)
139
+ # => here we set (batch_size=1, num_heads=num_heads, query_length=1, key_length=max_length)
140
+ # => the query_length dimension will then be broadcasted correctly
141
+ # This is more or less identical to T5's relative position bias:
142
+ # https://github.com/huggingface/transformers/blob/f681437203baa7671de3174b0fa583c349d9d5e1/src/transformers/models/t5/modeling_t5.py#L527
143
+ arange_tensor = ((attention_mask.cumsum(dim=-1) - 1) * attention_mask)[:, None, :]
144
+ alibi = slopes[..., None].bfloat16() * arange_tensor
145
+ return alibi.reshape(batch_size * num_heads, 1, seq_length).to(dtype)
146
+
147
+
148
+ def dropout_add(x: torch.Tensor, residual: torch.Tensor, prob: float, training: bool) -> torch.Tensor:
149
+ out = F.dropout(x, p=prob, training=training)
150
+ out = residual + out
151
+ return out
152
+
153
+
154
+ class Attention(nn.Module):
155
+ def __init__(self, config: RWConfig):
156
+ super().__init__()
157
+
158
+ self.hidden_size = config.hidden_size
159
+ self.num_heads = config.n_head
160
+ self.head_dim = self.hidden_size // self.num_heads
161
+ self.split_size = self.hidden_size
162
+ self.hidden_dropout = config.hidden_dropout
163
+
164
+ if self.head_dim * self.num_heads != self.hidden_size:
165
+ raise ValueError(
166
+ f"`hidden_size` must be divisible by num_heads (got `hidden_size`: {self.hidden_size} and `num_heads`:"
167
+ f" {self.num_heads})."
168
+ )
169
+
170
+ self.maybe_rotary = RotaryEmbedding(config.head_dim) if config.rotary else lambda q, k: (q, k)
171
+
172
+ # Layer-wise attention scaling
173
+ self.inv_norm_factor = 1.0 / math.sqrt(self.head_dim)
174
+ self.beta = self.inv_norm_factor
175
+
176
+ self.query_key_value = Linear(
177
+ self.hidden_size,
178
+ 3 * self.hidden_size if not config.multi_query else (self.hidden_size + 2 * self.head_dim),
179
+ bias=config.bias,
180
+ )
181
+ self.multi_query = config.multi_query
182
+ self.dense = Linear(self.hidden_size, self.hidden_size, bias=config.bias)
183
+ self.attention_dropout = nn.Dropout(config.attention_dropout)
184
+ self.num_kv = config.n_head if not self.multi_query else 1
185
+
186
+ def _split_heads(self, fused_qkv: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
187
+ """
188
+ Split the last dimension into (num_heads, head_dim) without making any copies, results share same memory
189
+ storage as `fused_qkv`
190
+
191
+ Args:
192
+ fused_qkv (`torch.tensor`, *required*): [batch_size, seq_length, num_heads * 3 * head_dim]
193
+
194
+ Returns:
195
+ query: [batch_size, seq_length, num_heads, head_dim] key: [batch_size, seq_length, num_heads, head_dim]
196
+ value: [batch_size, seq_length, num_heads, head_dim]
197
+ """
198
+ if not self.multi_query:
199
+ batch_size, seq_length, three_times_hidden_size = fused_qkv.shape
200
+ fused_qkv = fused_qkv.view(batch_size, seq_length, self.num_heads, 3, self.head_dim)
201
+ return fused_qkv[..., 0, :], fused_qkv[..., 1, :], fused_qkv[..., 2, :]
202
+ else:
203
+ batch_size, seq_length, three_times_hidden_size = fused_qkv.shape
204
+ fused_qkv = fused_qkv.view(batch_size, seq_length, self.num_heads + 2, self.head_dim)
205
+ return fused_qkv[..., :-2, :], fused_qkv[..., [-2], :], fused_qkv[..., [-1], :]
206
+
207
+ def _merge_heads(self, x: torch.Tensor) -> torch.Tensor:
208
+ """
209
+ Merge heads together over the last dimenstion
210
+
211
+ Args:
212
+ x: (`torch.tensor`, *required*): [batch_size * num_heads, seq_length, head_dim]
213
+
214
+ Returns:
215
+ torch.tensor: [batch_size, seq_length, num_heads * head_dim]
216
+ """
217
+ # What we want to achieve is:
218
+ # batch_size * num_heads, seq_length, head_dim -> batch_size, seq_length, num_heads * head_dim
219
+ batch_size_and_num_heads, seq_length, _ = x.shape
220
+ batch_size = batch_size_and_num_heads // self.num_heads
221
+
222
+ # First view to decompose the batch size
223
+ # batch_size * num_heads, seq_length, head_dim -> batch_size, num_heads, seq_length, head_dim
224
+ x = x.view(batch_size, self.num_heads, seq_length, self.head_dim)
225
+
226
+ # batch_size, num_heads, seq_length, head_dim -> batch_size, seq_length, num_heads, head_dim
227
+ x = x.permute(0, 2, 1, 3)
228
+
229
+ # batch_size, seq_length, num_heads, head_dim -> batch_size, seq_length, num_heads * head_dim
230
+ return x.reshape(batch_size, seq_length, self.num_heads * self.head_dim)
231
+
232
+ def forward(
233
+ self,
234
+ hidden_states: torch.Tensor,
235
+ alibi: torch.Tensor,
236
+ attention_mask: torch.Tensor,
237
+ layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
238
+ head_mask: Optional[torch.Tensor] = None,
239
+ use_cache: bool = False,
240
+ output_attentions: bool = False,
241
+ ):
242
+ fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size]
243
+
244
+ # 3 x [batch_size, seq_length, num_heads, head_dim]
245
+ (query_layer, key_layer, value_layer) = self._split_heads(fused_qkv)
246
+
247
+ batch_size, q_length, _, _ = query_layer.shape
248
+
249
+ query_layer = query_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim)
250
+ key_layer = key_layer.transpose(1, 2).reshape(
251
+ batch_size * self.num_kv,
252
+ q_length,
253
+ self.head_dim,
254
+ )
255
+ value_layer = value_layer.transpose(1, 2).reshape(batch_size * self.num_kv, q_length, self.head_dim)
256
+
257
+ query_layer, key_layer = self.maybe_rotary(query_layer, key_layer)
258
+
259
+ if layer_past is not None:
260
+ past_key, past_value = layer_past
261
+ # concatenate along seq_length dimension:
262
+ # - key: [batch_size * self.num_heads, head_dim, kv_length]
263
+ # - value: [batch_size * self.num_heads, kv_length, head_dim]
264
+ key_layer = torch.cat((past_key, key_layer), dim=1)
265
+ value_layer = torch.cat((past_value, value_layer), dim=1)
266
+
267
+ _, kv_length, _ = key_layer.shape
268
+
269
+ if use_cache is True:
270
+ present = (key_layer, value_layer)
271
+ else:
272
+ present = None
273
+
274
+ if alibi is None:
275
+ query_layer_ = query_layer.reshape(batch_size, self.num_heads, -1, self.head_dim)
276
+ key_layer_ = key_layer.reshape(batch_size, self.num_kv, -1, self.head_dim)
277
+ value_layer_ = value_layer.reshape(batch_size, self.num_kv, -1, self.head_dim)
278
+
279
+ attn_output = F.scaled_dot_product_attention(
280
+ query_layer_, key_layer_, value_layer_, None, 0.0, is_causal=True
281
+ )
282
+
283
+ x = attn_output.view(batch_size, self.num_heads, q_length, self.head_dim)
284
+ x = x.permute(0, 2, 1, 3)
285
+ attn_output = x.reshape(batch_size, q_length, self.num_heads * self.head_dim)
286
+
287
+ output_tensor = self.dense(attn_output)
288
+
289
+ outputs = (output_tensor, present)
290
+ assert not output_attentions # not supported.
291
+ return outputs
292
+ else:
293
+ attention_mask_float = (attention_mask * 1.0).masked_fill(attention_mask, -1e9).to(torch.bfloat16)
294
+ matmul_result = query_layer @ key_layer.transpose(-1, -2)
295
+
296
+ # change view to [batch_size, num_heads, q_length, kv_length]
297
+ attention_scores = matmul_result.view(batch_size, self.num_heads, q_length, kv_length)
298
+
299
+ # cast attention scores to fp32, compute scaled softmax and cast back to initial dtype - [batch_size, num_heads, q_length, kv_length]
300
+ input_dtype = attention_scores.dtype
301
+ # `float16` has a minimum value of -65504.0, whereas `bfloat16` and `float32` have a minimum value of `-3.4e+38`
302
+ if input_dtype == torch.float16 or input_dtype == torch.bfloat16:
303
+ attention_scores = attention_scores.to(torch.float32)
304
+ # attn_weights = torch.masked_fill(attention_scores, attention_mask, torch.finfo(attention_scores.dtype).min)
305
+ attention_probs = F.softmax(
306
+ (attention_scores + alibi) * self.inv_norm_factor + attention_mask_float,
307
+ dim=-1,
308
+ dtype=hidden_states.dtype,
309
+ )
310
+ # [batch_size, num_heads, q_length, kv_length]
311
+ attention_probs = self.attention_dropout(attention_probs)
312
+
313
+ if head_mask is not None:
314
+ attention_probs = attention_probs * head_mask
315
+
316
+ # change view [batch_size x num_heads, q_length, kv_length]
317
+ attention_probs_reshaped = attention_probs.view(batch_size * self.num_heads, q_length, kv_length)
318
+
319
+ # matmul: [batch_size * num_heads, q_length, head_dim]
320
+ context_layer = attention_probs_reshaped @ value_layer
321
+
322
+ # change view [batch_size, num_heads, q_length, head_dim]
323
+ context_layer = self._merge_heads(context_layer)
324
+
325
+ output_tensor = self.dense(context_layer)
326
+
327
+ outputs = (output_tensor, present)
328
+ if output_attentions:
329
+ outputs += (attention_probs,)
330
+
331
+ return outputs
332
+
333
+
334
+ class MLP(nn.Module):
335
+ def __init__(self, config: RWConfig):
336
+ super().__init__()
337
+ hidden_size = config.hidden_size
338
+
339
+ self.dense_h_to_4h = Linear(hidden_size, 4 * hidden_size, bias=config.bias)
340
+ self.act = nn.GELU()
341
+ self.dense_4h_to_h = Linear(4 * hidden_size, hidden_size, bias=config.bias)
342
+ self.hidden_dropout = config.hidden_dropout
343
+
344
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
345
+ x = self.act(self.dense_h_to_4h(x))
346
+ x = self.dense_4h_to_h(x)
347
+ return x
348
+
349
+
350
+ class DecoderLayer(nn.Module):
351
+ def __init__(self, config: RWConfig):
352
+ super().__init__()
353
+ hidden_size = config.hidden_size
354
+
355
+ self.input_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
356
+ self.num_heads = config.n_head
357
+ self.self_attention = Attention(config)
358
+
359
+ if not config.parallel_attn:
360
+ # unused if parallel attn
361
+ self.post_attention_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon)
362
+
363
+ self.mlp = MLP(config)
364
+
365
+ self.apply_residual_connection_post_layernorm = config.apply_residual_connection_post_layernorm
366
+ self.hidden_dropout = config.hidden_dropout
367
+
368
+ self.config = config
369
+
370
+ def forward(
371
+ self,
372
+ hidden_states: torch.Tensor,
373
+ alibi: torch.Tensor,
374
+ attention_mask: torch.Tensor,
375
+ layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
376
+ head_mask: Optional[torch.Tensor] = None,
377
+ use_cache: bool = False,
378
+ output_attentions: bool = False,
379
+ ):
380
+
381
+ layernorm_output = self.input_layernorm(hidden_states)
382
+ residual = hidden_states
383
+
384
+ # Self attention.
385
+ attn_outputs = self.self_attention(
386
+ layernorm_output,
387
+ layer_past=layer_past,
388
+ attention_mask=attention_mask,
389
+ alibi=alibi,
390
+ head_mask=head_mask,
391
+ use_cache=use_cache,
392
+ output_attentions=output_attentions,
393
+ )
394
+
395
+ attention_output = attn_outputs[0]
396
+
397
+ if not self.config.parallel_attn:
398
+ residual = dropout_add(attention_output, residual, self.config.attention_dropout, training=self.training)
399
+ layernorm_output = self.post_attention_layernorm(residual)
400
+
401
+ outputs = attn_outputs[1:]
402
+
403
+ # MLP.
404
+ mlp_output = self.mlp(layernorm_output)
405
+
406
+ if self.config.parallel_attn:
407
+ mlp_output += attention_output
408
+
409
+ output = dropout_add(mlp_output, residual, self.config.hidden_dropout, training=self.training)
410
+
411
+ if use_cache:
412
+ outputs = (output,) + outputs
413
+ else:
414
+ outputs = (output,) + outputs[1:]
415
+
416
+ return outputs # hidden_states, present, attentions
417
+
418
+
419
+ class RWPreTrainedModel(PreTrainedModel):
420
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
421
+ """
422
+ An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
423
+ models.
424
+ """
425
+
426
+ config_class = RWConfig
427
+ base_model_prefix = "transformer"
428
+ supports_gradient_checkpointing = True
429
+ _no_split_modules = ["DecoderLayer"]
430
+
431
+ def __init__(self, *inputs, **kwargs):
432
+ super().__init__(*inputs, **kwargs)
433
+
434
+ def _init_weights(self, module: nn.Module):
435
+ """Initialize the weights."""
436
+ if isinstance(module, nn.Linear) or isinstance(module, Linear):
437
+ # Slightly different from the TF version which uses truncated_normal for initialization
438
+ # cf https://github.com/pytorch/pytorch/pull/5617
439
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
440
+ if module.bias is not None:
441
+ module.bias.data.zero_()
442
+ elif isinstance(module, nn.Embedding):
443
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
444
+ if module.padding_idx is not None:
445
+ module.weight.data[module.padding_idx].zero_()
446
+ elif isinstance(module, LayerNorm):
447
+ module.bias.data.zero_()
448
+ module.weight.data.fill_(1.0)
449
+
450
+ def _set_gradient_checkpointing(self, module: nn.Module, value: bool = False):
451
+ if isinstance(module, RWModel):
452
+ module.gradient_checkpointing = value
453
+
454
+ @staticmethod
455
+ def _convert_to_standard_cache(
456
+ past_key_value: Tuple[Tuple[torch.Tensor, torch.Tensor]], batch_size: int
457
+ ) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]:
458
+ """
459
+ Standardizes the format of the cache so as to match most implementations, i.e. to tuple(tuple([batch_size,
460
+ num_heads, ...]))
461
+ """
462
+ batch_size_times_num_heads, head_dim, seq_length = past_key_value[0][0].shape
463
+ num_heads = batch_size_times_num_heads // batch_size
464
+ # key: [batch_size * num_heads, head_dim, seq_length] -> [batch_size, num_heads, head_dim, seq_length]
465
+ # value: [batch_size * num_heads, seq_length, head_dim] -> [batch_size, num_heads, seq_length, head_dim]
466
+ return tuple(
467
+ (
468
+ layer_past[0].view(batch_size, num_heads, head_dim, seq_length),
469
+ layer_past[1].view(batch_size, num_heads, seq_length, head_dim),
470
+ )
471
+ for layer_past in past_key_value
472
+ )
473
+
474
+ @staticmethod
475
+ def _convert_to_rw_cache(
476
+ past_key_value: Tuple[Tuple[torch.Tensor, torch.Tensor]]
477
+ ) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]:
478
+ batch_size, num_heads, head_dim, seq_length = past_key_value[0][0].shape
479
+ batch_size_times_num_heads = batch_size * num_heads
480
+ # key: [batch_size, num_heads, head_dim, seq_length] -> [batch_size * num_heads, head_dim, seq_length]
481
+ # value: [batch_size, num_heads, seq_length, head_dim] -> [batch_size * num_heads, seq_length, head_dim]
482
+ return tuple(
483
+ (
484
+ layer_past[0].view(batch_size_times_num_heads, head_dim, seq_length),
485
+ layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim),
486
+ )
487
+ for layer_past in past_key_value
488
+ )
489
+
490
+
491
+ class RWModel(RWPreTrainedModel):
492
+ def __init__(self, config: RWConfig):
493
+ super().__init__(config)
494
+
495
+ self.embed_dim = config.hidden_size
496
+ self.num_heads = config.n_head
497
+ self.alibi = config.alibi
498
+
499
+ # Embedding + LN Embedding
500
+ self.word_embeddings = nn.Embedding(config.vocab_size, self.embed_dim)
501
+
502
+ # Transformer blocks
503
+ self.h = nn.ModuleList([DecoderLayer(config) for _ in range(config.num_hidden_layers)])
504
+
505
+ # Final Layer Norm
506
+ self.ln_f = LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
507
+
508
+ self.gradient_checkpointing = False
509
+
510
+ # Initialize weights and apply final processing
511
+ self.post_init()
512
+
513
+ def get_input_embeddings(self):
514
+ return self.word_embeddings
515
+
516
+ def _prepare_attn_mask(
517
+ self, attention_mask: torch.Tensor, input_shape: Tuple[int, int], past_key_values_length: int
518
+ ) -> torch.BoolTensor:
519
+ # create causal mask
520
+ # [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]
521
+ combined_attention_mask = None
522
+ device = attention_mask.device
523
+ _, src_length = input_shape
524
+
525
+ if src_length > 1:
526
+ combined_attention_mask = _make_causal_mask(
527
+ input_shape, device=device, past_key_values_length=past_key_values_length
528
+ )
529
+
530
+ # [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]
531
+ expanded_attn_mask = _expand_mask(attention_mask, tgt_length=src_length)
532
+ combined_attention_mask = (
533
+ expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask | combined_attention_mask
534
+ )
535
+
536
+ return combined_attention_mask
537
+
538
+ def set_input_embeddings(self, new_embeddings: torch.Tensor):
539
+ self.word_embeddings = new_embeddings
540
+
541
+ def forward(
542
+ self,
543
+ input_ids: Optional[torch.LongTensor] = None,
544
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
545
+ attention_mask: Optional[torch.Tensor] = None,
546
+ head_mask: Optional[torch.LongTensor] = None,
547
+ inputs_embeds: Optional[torch.LongTensor] = None,
548
+ use_cache: Optional[bool] = None,
549
+ output_attentions: Optional[bool] = None,
550
+ output_hidden_states: Optional[bool] = None,
551
+ return_dict: Optional[bool] = None,
552
+ **deprecated_arguments,
553
+ ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]:
554
+ if deprecated_arguments.pop("position_ids", False) is not False:
555
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
556
+ warnings.warn(
557
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
558
+ " passing `position_ids`.",
559
+ FutureWarning,
560
+ )
561
+ if len(deprecated_arguments) > 0:
562
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
563
+
564
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
565
+ output_hidden_states = (
566
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
567
+ )
568
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
569
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
570
+
571
+ if input_ids is not None and inputs_embeds is not None:
572
+ raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
573
+ elif input_ids is not None:
574
+ batch_size, seq_length = input_ids.shape
575
+ elif inputs_embeds is not None:
576
+ batch_size, seq_length, _ = inputs_embeds.shape
577
+ else:
578
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
579
+
580
+ if past_key_values is None:
581
+ past_key_values = tuple([None] * len(self.h))
582
+
583
+ # Prepare head mask if needed
584
+ # 1.0 in head_mask indicate we keep the head
585
+ # attention_probs has shape batch_size x num_heads x N x N
586
+ # head_mask has shape n_layer x batch x num_heads x N x N
587
+ head_mask = self.get_head_mask(head_mask, self.config.n_layer)
588
+
589
+ if inputs_embeds is None:
590
+ inputs_embeds = self.word_embeddings(input_ids)
591
+
592
+ hidden_states = inputs_embeds
593
+
594
+ presents = () if use_cache else None
595
+ all_self_attentions = () if output_attentions else None
596
+ all_hidden_states = () if output_hidden_states else None
597
+
598
+ # Compute alibi tensor: check build_alibi_tensor documentation
599
+ seq_length_with_past = seq_length
600
+ past_key_values_length = 0
601
+ if past_key_values[0] is not None:
602
+ past_key_values_length = past_key_values[0][0].shape[2]
603
+ seq_length_with_past = seq_length_with_past + past_key_values_length
604
+ if attention_mask is None:
605
+ attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device)
606
+ else:
607
+ attention_mask = attention_mask.to(hidden_states.device)
608
+
609
+ if self.alibi:
610
+ alibi = build_alibi_tensor(attention_mask, self.num_heads, dtype=hidden_states.dtype)
611
+ else:
612
+ alibi = None
613
+
614
+ causal_mask = self._prepare_attn_mask(
615
+ attention_mask,
616
+ input_shape=(batch_size, seq_length),
617
+ past_key_values_length=past_key_values_length,
618
+ )
619
+
620
+ for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)):
621
+
622
+ if output_hidden_states:
623
+ all_hidden_states = all_hidden_states + (hidden_states,)
624
+
625
+ if self.gradient_checkpointing and self.training:
626
+
627
+ if use_cache:
628
+ logger.warning(
629
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
630
+ )
631
+ use_cache = False
632
+
633
+ def create_custom_forward(module):
634
+ def custom_forward(*inputs):
635
+ # None for past_key_value
636
+ return module(*inputs, use_cache=use_cache, output_attentions=output_attentions)
637
+
638
+ return custom_forward
639
+
640
+ outputs = torch.utils.checkpoint.checkpoint(
641
+ create_custom_forward(block),
642
+ hidden_states,
643
+ alibi,
644
+ causal_mask,
645
+ head_mask[i],
646
+ )
647
+ else:
648
+ outputs = block(
649
+ hidden_states,
650
+ layer_past=layer_past,
651
+ attention_mask=causal_mask,
652
+ head_mask=head_mask[i],
653
+ use_cache=use_cache,
654
+ output_attentions=output_attentions,
655
+ alibi=alibi,
656
+ )
657
+
658
+ hidden_states = outputs[0]
659
+ if use_cache is True:
660
+ presents = presents + (outputs[1],)
661
+
662
+ if output_attentions:
663
+ all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
664
+
665
+ # Add last hidden state
666
+ hidden_states = self.ln_f(hidden_states)
667
+
668
+ if output_hidden_states:
669
+ all_hidden_states = all_hidden_states + (hidden_states,)
670
+
671
+ if not return_dict:
672
+ return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None)
673
+
674
+ return BaseModelOutputWithPastAndCrossAttentions(
675
+ last_hidden_state=hidden_states,
676
+ past_key_values=presents,
677
+ hidden_states=all_hidden_states,
678
+ attentions=all_self_attentions,
679
+ )
680
+
681
+
682
+ class RWForCausalLM(RWPreTrainedModel):
683
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
684
+
685
+ def __init__(self, config: RWConfig):
686
+ super().__init__(config)
687
+ self.transformer = RWModel(config)
688
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
689
+
690
+ # Initialize weights and apply final processing
691
+ self.post_init()
692
+
693
+ def get_output_embeddings(self):
694
+ return self.lm_head
695
+
696
+ def set_output_embeddings(self, new_embeddings: torch.Tensor):
697
+ self.lm_head = new_embeddings
698
+
699
+ def prepare_inputs_for_generation(
700
+ self,
701
+ input_ids: torch.LongTensor,
702
+ past: Optional[torch.Tensor] = None,
703
+ attention_mask: Optional[torch.Tensor] = None,
704
+ **kwargs,
705
+ ) -> dict:
706
+ # only last token for input_ids if past is not None
707
+ if past:
708
+ input_ids = input_ids[:, -1].unsqueeze(-1)
709
+
710
+ # the cache may be in the stardard format (e.g. in contrastive search), convert to our's format if needed
711
+ if past[0][0].shape[0] == input_ids.shape[0]:
712
+ past = self._convert_to_rw_cache(past)
713
+
714
+ return {
715
+ "input_ids": input_ids,
716
+ "past_key_values": past,
717
+ "use_cache": kwargs.get("use_cache"),
718
+ "attention_mask": attention_mask,
719
+ }
720
+
721
+ def forward(
722
+ self,
723
+ input_ids: Optional[torch.LongTensor] = None,
724
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
725
+ attention_mask: Optional[torch.Tensor] = None,
726
+ head_mask: Optional[torch.Tensor] = None,
727
+ inputs_embeds: Optional[torch.Tensor] = None,
728
+ labels: Optional[torch.Tensor] = None,
729
+ use_cache: Optional[bool] = None,
730
+ output_attentions: Optional[bool] = None,
731
+ output_hidden_states: Optional[bool] = None,
732
+ return_dict: Optional[bool] = None,
733
+ **deprecated_arguments,
734
+ ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]:
735
+ r"""
736
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
737
+ Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
738
+ `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
739
+ are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
740
+ """
741
+ if deprecated_arguments.pop("position_ids", False) is not False:
742
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
743
+ warnings.warn(
744
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
745
+ " passing `position_ids`.",
746
+ FutureWarning,
747
+ )
748
+ if len(deprecated_arguments) > 0:
749
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
750
+
751
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
752
+
753
+ transformer_outputs = self.transformer(
754
+ input_ids,
755
+ past_key_values=past_key_values,
756
+ attention_mask=attention_mask,
757
+ head_mask=head_mask,
758
+ inputs_embeds=inputs_embeds,
759
+ use_cache=use_cache,
760
+ output_attentions=output_attentions,
761
+ output_hidden_states=output_hidden_states,
762
+ return_dict=return_dict,
763
+ )
764
+ hidden_states = transformer_outputs[0]
765
+
766
+ lm_logits = self.lm_head(hidden_states)
767
+
768
+ loss = None
769
+ if labels is not None:
770
+ # Shift so that tokens < n predict n
771
+ shift_logits = lm_logits[..., :-1, :].contiguous()
772
+ shift_labels = labels[..., 1:].contiguous()
773
+ batch_size, seq_length, vocab_size = shift_logits.shape
774
+ # Flatten the tokens
775
+ loss_fct = CrossEntropyLoss()
776
+ loss = loss_fct(
777
+ shift_logits.view(batch_size * seq_length, vocab_size), shift_labels.view(batch_size * seq_length)
778
+ )
779
+
780
+ if not return_dict:
781
+ output = (lm_logits,) + transformer_outputs[1:]
782
+ return ((loss,) + output) if loss is not None else output
783
+
784
+ return CausalLMOutputWithCrossAttentions(
785
+ loss=loss,
786
+ logits=lm_logits,
787
+ past_key_values=transformer_outputs.past_key_values,
788
+ hidden_states=transformer_outputs.hidden_states,
789
+ attentions=transformer_outputs.attentions,
790
+ )
791
+
792
+ def _reorder_cache(
793
+ self, past: Tuple[Tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor
794
+ ) -> Tuple[Tuple[torch.Tensor, torch.Tensor], ...]:
795
+ """
796
+ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or
797
+ [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct
798
+ beam_idx at every generation step.
799
+
800
+ Output shares the same memory storage as `past`.
801
+ """
802
+ standardized_past = self._convert_to_standard_cache(past, batch_size=len(beam_idx))
803
+
804
+ # Get a copy of `beam_idx` on all the devices where we need those indices.
805
+ device_to_beam_idx = {
806
+ past_state.device: beam_idx.to(past_state.device) for layer_past in past for past_state in layer_past
807
+ }
808
+ reordered_past = tuple(
809
+ (
810
+ layer_past[0].index_select(0, device_to_beam_idx[layer_past[0].device]),
811
+ layer_past[1].index_select(0, device_to_beam_idx[layer_past[0].device]),
812
+ )
813
+ for layer_past in standardized_past
814
+ )
815
+ return self._convert_to_rw_cache(reordered_past)
816
+
817
+
818
+ class RWForSequenceClassification(RWPreTrainedModel):
819
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
820
+
821
+ def __init__(self, config: RWConfig):
822
+ super().__init__(config)
823
+ self.num_labels = config.num_labels
824
+ self.transformer = RWModel(config)
825
+ self.score = nn.Linear(config.hidden_size, config.num_labels, bias=False)
826
+
827
+ # Initialize weights and apply final processing
828
+ self.post_init()
829
+
830
+ def forward(
831
+ self,
832
+ input_ids: Optional[torch.LongTensor] = None,
833
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
834
+ attention_mask: Optional[torch.Tensor] = None,
835
+ head_mask: Optional[torch.Tensor] = None,
836
+ inputs_embeds: Optional[torch.Tensor] = None,
837
+ labels: Optional[torch.Tensor] = None,
838
+ use_cache: Optional[bool] = None,
839
+ output_attentions: Optional[bool] = None,
840
+ output_hidden_states: Optional[bool] = None,
841
+ return_dict: Optional[bool] = None,
842
+ **deprecated_arguments,
843
+ ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutputWithPast]:
844
+ r"""
845
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
846
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
847
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
848
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
849
+ """
850
+ if deprecated_arguments.pop("position_ids", False) is not False:
851
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
852
+ warnings.warn(
853
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
854
+ " passing `position_ids`.",
855
+ FutureWarning,
856
+ )
857
+ if len(deprecated_arguments) > 0:
858
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
859
+
860
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
861
+
862
+ transformer_outputs = self.transformer(
863
+ input_ids,
864
+ past_key_values=past_key_values,
865
+ attention_mask=attention_mask,
866
+ head_mask=head_mask,
867
+ inputs_embeds=inputs_embeds,
868
+ use_cache=use_cache,
869
+ output_attentions=output_attentions,
870
+ output_hidden_states=output_hidden_states,
871
+ return_dict=return_dict,
872
+ )
873
+
874
+ hidden_states = transformer_outputs[0]
875
+ logits = self.score(hidden_states)
876
+
877
+ if input_ids is not None:
878
+ batch_size = input_ids.shape[0]
879
+ else:
880
+ batch_size = inputs_embeds.shape[0]
881
+
882
+ if self.config.pad_token_id is None and batch_size != 1:
883
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
884
+ if self.config.pad_token_id is None:
885
+ sequence_lengths = -1
886
+ else:
887
+ if input_ids is not None:
888
+ sequence_lengths = torch.ne(input_ids, self.config.pad_token_id).sum(dim=-1) - 1
889
+ else:
890
+ sequence_lengths = -1
891
+ logger.warning(
892
+ f"{self.__class__.__name__} will not detect padding tokens in `inputs_embeds`. Results may be "
893
+ "unexpected if using padding tokens in conjunction with `inputs_embeds.`"
894
+ )
895
+
896
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
897
+
898
+ loss = None
899
+ if labels is not None:
900
+ if self.config.problem_type is None:
901
+ if self.num_labels == 1:
902
+ self.config.problem_type = "regression"
903
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
904
+ self.config.problem_type = "single_label_classification"
905
+ else:
906
+ self.config.problem_type = "multi_label_classification"
907
+
908
+ if self.config.problem_type == "regression":
909
+ loss_fct = MSELoss()
910
+ if self.num_labels == 1:
911
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
912
+ else:
913
+ loss = loss_fct(pooled_logits, labels)
914
+ elif self.config.problem_type == "single_label_classification":
915
+ loss_fct = CrossEntropyLoss()
916
+ loss = loss_fct(pooled_logits, labels)
917
+ elif self.config.problem_type == "multi_label_classification":
918
+ loss_fct = BCEWithLogitsLoss()
919
+ loss = loss_fct(pooled_logits, labels)
920
+ if not return_dict:
921
+ output = (pooled_logits,) + transformer_outputs[1:]
922
+ return ((loss,) + output) if loss is not None else output
923
+
924
+ return SequenceClassifierOutputWithPast(
925
+ loss=loss,
926
+ logits=pooled_logits,
927
+ past_key_values=transformer_outputs.past_key_values,
928
+ hidden_states=transformer_outputs.hidden_states,
929
+ attentions=transformer_outputs.attentions,
930
+ )
931
+
932
+
933
+ class RWForTokenClassification(RWPreTrainedModel):
934
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
935
+
936
+ def __init__(self, config: RWConfig):
937
+ super().__init__(config)
938
+ self.num_labels = config.num_labels
939
+
940
+ self.transformer = RWModel(config)
941
+ if hasattr(config, "classifier_dropout") and config.classifier_dropout is not None:
942
+ classifier_dropout = config.classifier_dropout
943
+ elif hasattr(config, "hidden_dropout") and config.hidden_dropout is not None:
944
+ classifier_dropout = config.hidden_dropout
945
+ else:
946
+ classifier_dropout = 0.1
947
+ self.dropout = nn.Dropout(classifier_dropout)
948
+ self.classifier = nn.Linear(config.hidden_size, config.num_labels)
949
+
950
+ # Initialize weights and apply final processing
951
+ self.post_init()
952
+
953
+ def forward(
954
+ self,
955
+ input_ids: Optional[torch.LongTensor] = None,
956
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
957
+ attention_mask: Optional[torch.Tensor] = None,
958
+ head_mask: Optional[torch.Tensor] = None,
959
+ inputs_embeds: Optional[torch.Tensor] = None,
960
+ labels: Optional[torch.Tensor] = None,
961
+ use_cache: Optional[bool] = None,
962
+ output_attentions: Optional[bool] = None,
963
+ output_hidden_states: Optional[bool] = None,
964
+ return_dict: Optional[bool] = None,
965
+ **deprecated_arguments,
966
+ ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]:
967
+ r"""
968
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
969
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
970
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
971
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
972
+ """
973
+ if deprecated_arguments.pop("position_ids", False) is not False:
974
+ # `position_ids` could have been `torch.Tensor` or `None` so defaulting pop to `False` allows to detect if users were passing explicitly `None`
975
+ warnings.warn(
976
+ "`position_ids` have no functionality in BLOOM and will be removed in v5.0.0. You can safely ignore"
977
+ " passing `position_ids`.",
978
+ FutureWarning,
979
+ )
980
+ if len(deprecated_arguments) > 0:
981
+ raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
982
+
983
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
984
+
985
+ transformer_outputs = self.transformer(
986
+ input_ids,
987
+ past_key_values=past_key_values,
988
+ attention_mask=attention_mask,
989
+ head_mask=head_mask,
990
+ inputs_embeds=inputs_embeds,
991
+ use_cache=use_cache,
992
+ output_attentions=output_attentions,
993
+ output_hidden_states=output_hidden_states,
994
+ return_dict=return_dict,
995
+ )
996
+
997
+ hidden_states = transformer_outputs[0]
998
+ hidden_states = self.dropout(hidden_states)
999
+ logits = self.classifier(hidden_states)
1000
+
1001
+ loss = None
1002
+ if labels is not None:
1003
+ batch_size, seq_length = labels.shape
1004
+ loss_fct = CrossEntropyLoss()
1005
+ loss = loss_fct(logits.view(batch_size * seq_length, self.num_labels), labels.view(batch_size * seq_length))
1006
+
1007
+ if not return_dict:
1008
+ output = (logits,) + transformer_outputs[2:]
1009
+ return ((loss,) + output) if loss is not None else output
1010
+
1011
+ return TokenClassifierOutput(
1012
+ loss=loss,
1013
+ logits=logits,
1014
+ hidden_states=transformer_outputs.hidden_states,
1015
+ attentions=transformer_outputs.attentions,
1016
+ )
1017
+
1018
+
1019
+ class RWForQuestionAnswering(RWPreTrainedModel):
1020
+ _keys_to_ignore_on_load_missing = [r"h.*.self_attention.scale_mask_softmax.causal_mask", r"lm_head.weight"]
1021
+
1022
+ def __init__(self, config):
1023
+ super().__init__(config)
1024
+ self.transformer = RWModel(config)
1025
+ self.qa_outputs = nn.Linear(config.hidden_size, 2)
1026
+
1027
+ # Initialize weights and apply final processing
1028
+ self.post_init()
1029
+
1030
+ def forward(
1031
+ self,
1032
+ input_ids: Optional[torch.LongTensor] = None,
1033
+ attention_mask: Optional[torch.FloatTensor] = None,
1034
+ position_ids: Optional[torch.LongTensor] = None,
1035
+ head_mask: Optional[torch.FloatTensor] = None,
1036
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1037
+ start_positions: Optional[torch.LongTensor] = None,
1038
+ end_positions: Optional[torch.LongTensor] = None,
1039
+ output_attentions: Optional[bool] = None,
1040
+ output_hidden_states: Optional[bool] = None,
1041
+ return_dict: Optional[bool] = None,
1042
+ ) -> Union[Tuple, QuestionAnsweringModelOutput]:
1043
+ r"""
1044
+ start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1045
+ Labels for position (index) of the start of the labelled span for computing the token classification loss.
1046
+ Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
1047
+ are not taken into account for computing the loss.
1048
+ end_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1049
+ Labels for position (index) of the end of the labelled span for computing the token classification loss.
1050
+ Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
1051
+ are not taken into account for computing the loss.
1052
+ """
1053
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1054
+
1055
+ outputs = self.transformer(
1056
+ input_ids,
1057
+ attention_mask=attention_mask,
1058
+ position_ids=position_ids,
1059
+ head_mask=head_mask,
1060
+ inputs_embeds=inputs_embeds,
1061
+ output_attentions=output_attentions,
1062
+ output_hidden_states=output_hidden_states,
1063
+ return_dict=return_dict,
1064
+ )
1065
+
1066
+ sequence_output = outputs[0]
1067
+
1068
+ logits = self.qa_outputs(sequence_output)
1069
+ start_logits, end_logits = logits.split(1, dim=-1)
1070
+ start_logits = start_logits.squeeze(-1).contiguous()
1071
+ end_logits = end_logits.squeeze(-1).contiguous()
1072
+
1073
+ total_loss = None
1074
+ if start_positions is not None and end_positions is not None:
1075
+ # If we are on multi-GPU, split add a dimension
1076
+ if len(start_positions.size()) > 1:
1077
+ start_positions = start_positions.squeeze(-1)
1078
+ if len(end_positions.size()) > 1:
1079
+ end_positions = end_positions.squeeze(-1)
1080
+ # sometimes the start/end positions are outside our model inputs, we ignore these terms
1081
+ ignored_index = start_logits.size(1)
1082
+ start_positions = start_positions.clamp(0, ignored_index)
1083
+ end_positions = end_positions.clamp(0, ignored_index)
1084
+
1085
+ loss_fct = CrossEntropyLoss(ignore_index=ignored_index)
1086
+ start_loss = loss_fct(start_logits, start_positions)
1087
+ end_loss = loss_fct(end_logits, end_positions)
1088
+ total_loss = (start_loss + end_loss) / 2
1089
+
1090
+ if not return_dict:
1091
+ output = (start_logits, end_logits) + outputs[2:]
1092
+ return ((total_loss,) + output) if total_loss is not None else output
1093
+
1094
+ return QuestionAnsweringModelOutput(
1095
+ loss=total_loss,
1096
+ start_logits=start_logits,
1097
+ end_logits=end_logits,
1098
+ hidden_states=outputs.hidden_states,
1099
+ attentions=outputs.attentions,
1100
+ )
special_tokens_map.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ ">>TITLE<<",
4
+ ">>ABSTRACT<<",
5
+ ">>INTRODUCTION<<",
6
+ ">>SUMMARY<<",
7
+ ">>COMMENT<<",
8
+ ">>ANSWER<<",
9
+ ">>QUESTION<<",
10
+ ">>DOMAIN<<",
11
+ ">>PREFIX<<",
12
+ ">>SUFFIX<<",
13
+ ">>MIDDLE<<"
14
+ ],
15
+ "eos_token": "<|endoftext|>"
16
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "eos_token": "<|endoftext|>",
4
+ "model_max_length": 2048,
5
+ "name_or_path": "tiiuae/falcon_tokenizer",
6
+ "special_tokens_map_file": null,
7
+ "tokenizer_class": "PreTrainedTokenizerFast"
8
+ }