TheBloke commited on
Commit
de52801
1 Parent(s): d192413

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +682 -0
README.md ADDED
@@ -0,0 +1,682 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: Yukang/LongAlpaca-70B
3
+ inference: false
4
+ license: llama2
5
+ model_creator: YukangChen
6
+ model_name: LongAlpaca 70B
7
+ model_type: llama
8
+ prompt_template: 'Below is an instruction that describes a task. Write a response
9
+ that appropriately completes the request.
10
+
11
+
12
+ ### Instruction:
13
+
14
+ {prompt}
15
+
16
+
17
+ ### Response:
18
+
19
+ '
20
+ quantized_by: TheBloke
21
+ ---
22
+
23
+ <!-- header start -->
24
+ <!-- 200823 -->
25
+ <div style="width: auto; margin-left: auto; margin-right: auto">
26
+ <img src="https://i.imgur.com/EBdldam.jpg" alt="TheBlokeAI" style="width: 100%; min-width: 400px; display: block; margin: auto;">
27
+ </div>
28
+ <div style="display: flex; justify-content: space-between; width: 100%;">
29
+ <div style="display: flex; flex-direction: column; align-items: flex-start;">
30
+ <p style="margin-top: 0.5em; margin-bottom: 0em;"><a href="https://discord.gg/theblokeai">Chat & support: TheBloke's Discord server</a></p>
31
+ </div>
32
+ <div style="display: flex; flex-direction: column; align-items: flex-end;">
33
+ <p style="margin-top: 0.5em; margin-bottom: 0em;"><a href="https://www.patreon.com/TheBlokeAI">Want to contribute? TheBloke's Patreon page</a></p>
34
+ </div>
35
+ </div>
36
+ <div style="text-align:center; margin-top: 0em; margin-bottom: 0em"><p style="margin-top: 0.25em; margin-bottom: 0em;">TheBloke's LLM work is generously supported by a grant from <a href="https://a16z.com">andreessen horowitz (a16z)</a></p></div>
37
+ <hr style="margin-top: 1.0em; margin-bottom: 1.0em;">
38
+ <!-- header end -->
39
+
40
+ # LongAlpaca 70B - AWQ
41
+ - Model creator: [YukangChen](https://huggingface.co/Yukang)
42
+ - Original model: [LongAlpaca 70B](https://huggingface.co/Yukang/LongAlpaca-70B)
43
+
44
+ <!-- description start -->
45
+ ## Description
46
+
47
+ This repo contains AWQ model files for [YukangChen's LongAlpaca 70B](https://huggingface.co/Yukang/LongAlpaca-70B).
48
+
49
+
50
+ ### About AWQ
51
+
52
+ AWQ is an efficient, accurate and blazing-fast low-bit weight quantization method, currently supporting 4-bit quantization. Compared to GPTQ, it offers faster Transformers-based inference.
53
+
54
+ It is also now supported by continuous batching server [vLLM](https://github.com/vllm-project/vllm), allowing use of Llama AWQ models for high-throughput concurrent inference in multi-user server scenarios.
55
+
56
+ As of September 25th 2023, preliminary Llama-only AWQ support has also been added to [Huggingface Text Generation Inference (TGI)](https://github.com/huggingface/text-generation-inference).
57
+
58
+ Note that, at the time of writing, overall throughput is still lower than running vLLM or TGI with unquantised models, however using AWQ enables using much smaller GPUs which can lead to easier deployment and overall cost savings. For example, a 70B model can be run on 1 x 48GB GPU instead of 2 x 80GB.
59
+ <!-- description end -->
60
+ <!-- repositories-available start -->
61
+ ## Repositories available
62
+
63
+ * [AWQ model(s) for GPU inference.](https://huggingface.co/TheBloke/LongAlpaca-70B-AWQ)
64
+ * [GPTQ models for GPU inference, with multiple quantisation parameter options.](https://huggingface.co/TheBloke/LongAlpaca-70B-GPTQ)
65
+ * [2, 3, 4, 5, 6 and 8-bit GGUF models for CPU+GPU inference](https://huggingface.co/TheBloke/LongAlpaca-70B-GGUF)
66
+ * [YukangChen's original unquantised fp16 model in pytorch format, for GPU inference and for further conversions](https://huggingface.co/Yukang/LongAlpaca-70B)
67
+ <!-- repositories-available end -->
68
+
69
+ <!-- prompt-template start -->
70
+ ## Prompt template: Alpaca
71
+
72
+ ```
73
+ Below is an instruction that describes a task. Write a response that appropriately completes the request.
74
+
75
+ ### Instruction:
76
+ {prompt}
77
+
78
+ ### Response:
79
+
80
+ ```
81
+
82
+ <!-- prompt-template end -->
83
+
84
+
85
+ <!-- README_AWQ.md-provided-files start -->
86
+ ## Provided files, and AWQ parameters
87
+
88
+ For my first release of AWQ models, I am releasing 128g models only. I will consider adding 32g as well if there is interest, and once I have done perplexity and evaluation comparisons, but at this time 32g models are still not fully tested with AutoAWQ and vLLM.
89
+
90
+ Models are released as sharded safetensors files.
91
+
92
+ | Branch | Bits | GS | AWQ Dataset | Seq Len | Size |
93
+ | ------ | ---- | -- | ----------- | ------- | ---- |
94
+ | [main](https://huggingface.co/TheBloke/LongAlpaca-70B-AWQ/tree/main) | 4 | 128 | [wikitext](https://huggingface.co/datasets/wikitext/viewer/wikitext-2-v1/test) | 4096 | 36.61 GB
95
+
96
+ <!-- README_AWQ.md-provided-files end -->
97
+
98
+ <!-- README_AWQ.md-use-from-vllm start -->
99
+ ## Serving this model from vLLM
100
+
101
+ Documentation on installing and using vLLM [can be found here](https://vllm.readthedocs.io/en/latest/).
102
+
103
+ Note: at the time of writing, vLLM has not yet done a new release with AWQ support.
104
+
105
+ If you try the vLLM examples below and get an error about `quantization` being unrecognised, or other AWQ-related issues, please install vLLM from Github source.
106
+
107
+ - When using vLLM as a server, pass the `--quantization awq` parameter, for example:
108
+
109
+ ```shell
110
+ python3 python -m vllm.entrypoints.api_server --model TheBloke/LongAlpaca-70B-AWQ --quantization awq --dtype half
111
+ ```
112
+
113
+ When using vLLM from Python code, pass the `quantization=awq` parameter, for example:
114
+
115
+ ```python
116
+ from vllm import LLM, SamplingParams
117
+
118
+ prompts = [
119
+ "Hello, my name is",
120
+ "The president of the United States is",
121
+ "The capital of France is",
122
+ "The future of AI is",
123
+ ]
124
+ sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
125
+
126
+ llm = LLM(model="TheBloke/LongAlpaca-70B-AWQ", quantization="awq", dtype="half")
127
+
128
+ outputs = llm.generate(prompts, sampling_params)
129
+
130
+ # Print the outputs.
131
+ for output in outputs:
132
+ prompt = output.prompt
133
+ generated_text = output.outputs[0].text
134
+ print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
135
+ ```
136
+ <!-- README_AWQ.md-use-from-vllm start -->
137
+
138
+ <!-- README_AWQ.md-use-from-tgi start -->
139
+ ## Serving this model from Text Generation Inference (TGI)
140
+
141
+ Use TGI version 1.1.0 or later. The official Docker container is: `ghcr.io/huggingface/text-generation-inference:1.1.0`
142
+
143
+ Example Docker parameters:
144
+
145
+ ```shell
146
+ --model-id TheBloke/LongAlpaca-70B-AWQ --port 3000 --quantize awq --max-input-length 3696 --max-total-tokens 4096 --max-batch-prefill-tokens 4096
147
+ ```
148
+
149
+ Example Python code for interfacing with TGI (requires huggingface-hub 0.17.0 or later):
150
+
151
+ ```shell
152
+ pip3 install huggingface-hub
153
+ ```
154
+
155
+ ```python
156
+ from huggingface_hub import InferenceClient
157
+
158
+ endpoint_url = "https://your-endpoint-url-here"
159
+
160
+ prompt = "Tell me about AI"
161
+ prompt_template=f'''Below is an instruction that describes a task. Write a response that appropriately completes the request.
162
+
163
+ ### Instruction:
164
+ {prompt}
165
+
166
+ ### Response:
167
+
168
+ '''
169
+
170
+ client = InferenceClient(endpoint_url)
171
+ response = client.text_generation(prompt,
172
+ max_new_tokens=128,
173
+ do_sample=True,
174
+ temperature=0.7,
175
+ top_p=0.95,
176
+ top_k=40,
177
+ repetition_penalty=1.1)
178
+
179
+ print(f"Model output: {response}")
180
+ ```
181
+ <!-- README_AWQ.md-use-from-tgi end -->
182
+
183
+ <!-- README_AWQ.md-use-from-python start -->
184
+ ## How to use this AWQ model from Python code
185
+
186
+ ### Install the necessary packages
187
+
188
+ Requires: [AutoAWQ](https://github.com/casper-hansen/AutoAWQ) 0.1.1 or later
189
+
190
+ ```shell
191
+ pip3 install autoawq
192
+ ```
193
+
194
+ If you have problems installing [AutoAWQ](https://github.com/casper-hansen/AutoAWQ) using the pre-built wheels, install it from source instead:
195
+
196
+ ```shell
197
+ pip3 uninstall -y autoawq
198
+ git clone https://github.com/casper-hansen/AutoAWQ
199
+ cd AutoAWQ
200
+ pip3 install .
201
+ ```
202
+
203
+ ### You can then try the following example code
204
+
205
+ ```python
206
+ from awq import AutoAWQForCausalLM
207
+ from transformers import AutoTokenizer
208
+
209
+ model_name_or_path = "TheBloke/LongAlpaca-70B-AWQ"
210
+
211
+ # Load model
212
+ model = AutoAWQForCausalLM.from_quantized(model_name_or_path, fuse_layers=True,
213
+ trust_remote_code=False, safetensors=True)
214
+ tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=False)
215
+
216
+ prompt = "Tell me about AI"
217
+ prompt_template=f'''Below is an instruction that describes a task. Write a response that appropriately completes the request.
218
+
219
+ ### Instruction:
220
+ {prompt}
221
+
222
+ ### Response:
223
+
224
+ '''
225
+
226
+ print("\n\n*** Generate:")
227
+
228
+ tokens = tokenizer(
229
+ prompt_template,
230
+ return_tensors='pt'
231
+ ).input_ids.cuda()
232
+
233
+ # Generate output
234
+ generation_output = model.generate(
235
+ tokens,
236
+ do_sample=True,
237
+ temperature=0.7,
238
+ top_p=0.95,
239
+ top_k=40,
240
+ max_new_tokens=512
241
+ )
242
+
243
+ print("Output: ", tokenizer.decode(generation_output[0]))
244
+
245
+ """
246
+ # Inference should be possible with transformers pipeline as well in future
247
+ # But currently this is not yet supported by AutoAWQ (correct as of September 25th 2023)
248
+ from transformers import pipeline
249
+
250
+ print("*** Pipeline:")
251
+ pipe = pipeline(
252
+ "text-generation",
253
+ model=model,
254
+ tokenizer=tokenizer,
255
+ max_new_tokens=512,
256
+ do_sample=True,
257
+ temperature=0.7,
258
+ top_p=0.95,
259
+ top_k=40,
260
+ repetition_penalty=1.1
261
+ )
262
+
263
+ print(pipe(prompt_template)[0]['generated_text'])
264
+ """
265
+ ```
266
+ <!-- README_AWQ.md-use-from-python end -->
267
+
268
+ <!-- README_AWQ.md-compatibility start -->
269
+ ## Compatibility
270
+
271
+ The files provided are tested to work with:
272
+
273
+ - [AutoAWQ](https://github.com/casper-hansen/AutoAWQ)
274
+ - [vLLM](https://github.com/vllm-project/vllm)
275
+ - [Huggingface Text Generation Inference (TGI)](https://github.com/huggingface/text-generation-inference)
276
+
277
+ TGI merged AWQ support on September 25th, 2023: [TGI PR #1054](https://github.com/huggingface/text-generation-inference/pull/1054). Use the `:latest` Docker container until the next TGI release is made.
278
+
279
+ <!-- README_AWQ.md-compatibility end -->
280
+
281
+ <!-- footer start -->
282
+ <!-- 200823 -->
283
+ ## Discord
284
+
285
+ For further support, and discussions on these models and AI in general, join us at:
286
+
287
+ [TheBloke AI's Discord server](https://discord.gg/theblokeai)
288
+
289
+ ## Thanks, and how to contribute
290
+
291
+ Thanks to the [chirper.ai](https://chirper.ai) team!
292
+
293
+ Thanks to Clay from [gpus.llm-utils.org](llm-utils)!
294
+
295
+ I've had a lot of people ask if they can contribute. I enjoy providing models and helping people, and would love to be able to spend even more time doing it, as well as expanding into new projects like fine tuning/training.
296
+
297
+ If you're able and willing to contribute it will be most gratefully received and will help me to keep providing more models, and to start work on new AI projects.
298
+
299
+ Donaters will get priority support on any and all AI/LLM/model questions and requests, access to a private Discord room, plus other benefits.
300
+
301
+ * Patreon: https://patreon.com/TheBlokeAI
302
+ * Ko-Fi: https://ko-fi.com/TheBlokeAI
303
+
304
+ **Special thanks to**: Aemon Algiz.
305
+
306
+ **Patreon special mentions**: Pierre Kircher, Stanislav Ovsiannikov, Michael Levine, Eugene Pentland, Andrey, 준교 김, Randy H, Fred von Graf, Artur Olbinski, Caitlyn Gatomon, terasurfer, Jeff Scroggin, James Bentley, Vadim, Gabriel Puliatti, Harry Royden McLaughlin, Sean Connelly, Dan Guido, Edmond Seymore, Alicia Loh, subjectnull, AzureBlack, Manuel Alberto Morcote, Thomas Belote, Lone Striker, Chris Smitley, Vitor Caleffi, Johann-Peter Hartmann, Clay Pascal, biorpg, Brandon Frisco, sidney chen, transmissions 11, Pedro Madruga, jinyuan sun, Ajan Kanaga, Emad Mostaque, Trenton Dambrowitz, Jonathan Leane, Iucharbius, usrbinkat, vamX, George Stoitzev, Luke Pendergrass, theTransient, Olakabola, Swaroop Kallakuri, Cap'n Zoog, Brandon Phillips, Michael Dempsey, Nikolai Manek, danny, Matthew Berman, Gabriel Tamborski, alfie_i, Raymond Fosdick, Tom X Nguyen, Raven Klaugh, LangChain4j, Magnesian, Illia Dulskyi, David Ziegler, Mano Prime, Luis Javier Navarrete Lozano, Erik Bjäreholt, 阿明, Nathan Dryer, Alex, Rainer Wilmers, zynix, TL, Joseph William Delisle, John Villwock, Nathan LeClaire, Willem Michiel, Joguhyik, GodLy, OG, Alps Aficionado, Jeffrey Morgan, ReadyPlayerEmma, Tiffany J. Kim, Sebastain Graf, Spencer Kim, Michael Davis, webtim, Talal Aujan, knownsqashed, John Detwiler, Imad Khwaja, Deo Leter, Jerry Meng, Elijah Stavena, Rooh Singh, Pieter, SuperWojo, Alexandros Triantafyllidis, Stephen Murray, Ai Maven, ya boyyy, Enrico Ros, Ken Nordquist, Deep Realms, Nicholas, Spiking Neurons AB, Elle, Will Dee, Jack West, RoA, Luke @flexchar, Viktor Bowallius, Derek Yates, Subspace Studios, jjj, Toran Billups, Asp the Wyvern, Fen Risland, Ilya, NimbleBox.ai, Chadd, Nitin Borwankar, Emre, Mandus, Leonard Tan, Kalila, K, Trailburnt, S_X, Cory Kujawski
307
+
308
+
309
+ Thank you to all my generous patrons and donaters!
310
+
311
+ And thank you again to a16z for their generous grant.
312
+
313
+ <!-- footer end -->
314
+
315
+ # Original model card: YukangChen's LongAlpaca 70B
316
+
317
+ # LongLoRA and LongAlpaca for Long-context LLMs
318
+
319
+
320
+ [![Huggingface Models](https://img.shields.io/badge/Models-Huggingface%20Models-bron)](https://huggingface.co/Yukang)
321
+ [![Github](https://img.shields.io/badge/Github-Repo-cyan)](https://github.com/dvlab-research/LongLoRA)
322
+ [![Data](https://img.shields.io/badge/Data-LongAlpaca%2012k-light)](https://huggingface.co/datasets/Yukang/LongAlpaca-12k)
323
+ [![Paper](https://img.shields.io/badge/Paper-Arvix-blue)](https://arxiv.org/abs/2309.12307)
324
+
325
+ [![Code License](https://img.shields.io/badge/Code%20License-Apache_2.0-yellow.svg)](https://github.com/dvlab-research/LongLoRA/blob/main/LICENSE)
326
+ [![Data License](https://img.shields.io/badge/Data%20License-CC%20By%20NC%204.0-orange.svg)](https://github.com/dvlab-research/LongLoRA/blob/main/DATA_LICENSE)
327
+ [![Weight License](https://img.shields.io/badge/Weight%20License-CC%20By%20NC%204.0-red)](https://github.com/dvlab-research/LongLoRA/blob/main/WEIGHT_LICENSE)
328
+
329
+ For detailed usage and codes, please visit the [Github project](https://github.com/dvlab-research/LongLoRA).
330
+ ## TABLE OF CONTENTS
331
+ 1. [News](#news)
332
+ 2. [Examples](#examples)
333
+ 3. [Highlights](#highlights)
334
+ 4. [How to contribute](#how-to-contribute)
335
+ 5. [Requirements](#usage-requirements)
336
+ 6. [Installation and quick guide](#installation-and-quick-guide)
337
+ 7. [LongAlpaca Data](#longalpaca-data)
338
+ 8. [Models](#models)
339
+ 9. [Training](#training)
340
+ 10. [Evaluation](#evaluation)
341
+ 11. [Demo](#demo)
342
+ 12. [Data Generation via Pdf2Text](#data-generation-via-pdf2text)
343
+ 13. [Citation](#citation)
344
+ 14. [Acknowledgement](#acknowledgement)
345
+ 15. [License](#license)
346
+
347
+ ## News
348
+ - [x] [2023.10.8] **We release the long instruction-following dataset**, [LongAlpaca-12k](https://huggingface.co/datasets/Yukang/LongAlpaca-12k) and **the corresponding models**, [LongAlpaca-7B](https://huggingface.co/Yukang/LongAlpaca-7B), [LongAlpaca-13B](https://huggingface.co/Yukang/LongAlpaca-13B), and [LongAlpaca-70B](https://huggingface.co/Yukang/LongAlpaca-70B).
349
+ - (*The previous sft models*, [Llama-2-13b-chat-longlora-32k-sft](https://huggingface.co/Yukang/Llama-2-13b-chat-longlora-32k-sft) and [Llama-2-70b-chat-longlora-32k-sft](https://huggingface.co/Yukang/Llama-2-70b-chat-longlora-32k-sft), *have been depreciated*.)
350
+ - [x] [2023.10.3] We add support GPTNeoX models. Please refer to this [PR](https://github.com/dvlab-research/LongLoRA/pull/32) for usage. Thanks for @naubull2 for this contribution.
351
+ - [x] [2023.9.22] We release all our fine-tuned [models](https://huggingface.co/Yukang), including **70B-32k models**, [LLaMA2-LongLoRA-70B-32k](https://huggingface.co/Yukang/Llama-2-70b-longlora-32k), [LLaMA2-LongLoRA-7B-100k](https://huggingface.co/Yukang/Llama-2-7b-longlora-100k-ft). Welcome to check them out!
352
+ - [x] [2023.9.22] We release [Paper](http://arxiv.org/abs/2309.12307) and this GitHub repo, including training and evaluation code.
353
+
354
+ **LongLoRA: Efficient Fine-tuning of Long-Context Large Language Models [[Paper](http://arxiv.org/abs/2309.12307)]** <br />
355
+ [Yukang Chen](https://scholar.google.com/citations?user=6p0ygKUAAAAJ&hl=en),
356
+ [Shengju Qian](https://scholar.google.com/citations?user=QNnWmasAAAAJ),
357
+ [Haotian Tang](https://scholar.google.com/citations?user=WxL13BAAAAAJ&hl),
358
+ [Xin Lai](https://scholar.google.com/citations?user=tqNDPA4AAAAJ&hl=zh-CN),
359
+ [Zhijian Liu](https://scholar.google.com/citations?user=3coYSTUAAAAJ&hl=en),
360
+ [Song Han](https://scholar.google.com/citations?user=E0iCaa4AAAAJ&hl=zh-CN),
361
+ [Jiaya Jia](https://scholar.google.com/citations?user=XPAkzTEAAAAJ&hl=en)<br />
362
+
363
+ ## Highlights
364
+ 1. In LongLoRA approach, The proposed shifted short attention is easy to implement, compatible with Flash-Attention, and is not required during inference.
365
+ 2. We released all our models, including models from 7B to 70B, context length from 8k to 100k, including [LLaMA2-LongLoRA-7B-100k](https://huggingface.co/Yukang/Llama-2-7b-longlora-100k-ft), [LLaMA2-LongLoRA-13B-64k](https://huggingface.co/Yukang/Llama-2-13b-longlora-64k), and [LLaMA2-LongLoRA-70B-32k](https://huggingface.co/Yukang/Llama-2-70b-longlora-32k).
366
+ 3. We built up a long-context instruction-following dataset, [LongAlpaca-12k](#longalpaca-data). We released the corresponding [LongAlpaca-7B](https://huggingface.co/Yukang/LongAlpaca-7B), [LongAlpaca-13B](https://huggingface.co/Yukang/LongAlpaca-13B) and [LongAlpaca-70B](https://huggingface.co/Yukang/LongAlpaca-70B) models. To our best knowledge, this is the first open-sourced long-context 70B model.
367
+
368
+ ## How to Contribute
369
+ - Make sure to have git installed.
370
+ - Create your own [fork](https://github.com/dvlab-research/LongLoRA/fork) of the project.
371
+ - Clone the repository on your local machine, using git clone and pasting the url of this project.
372
+ - Read both the `Requirements` and `Installation and Quick Guide` sections below.
373
+ - Commit and push your changes.
374
+ - Make a pull request when finished modifying the project.
375
+
376
+
377
+ ## Usage Requirements
378
+ To download and use the [pre-trained weights](#pre-trained-weights) you will need:
379
+ 1. Hugging Face (HF) account with valid email. Note, the email used for HF must alse be used for the license agreement.
380
+ 2. Accept the Meta [license and acceptable use policy](https://ai.meta.com/resources/models-and-libraries/llama-downloads/)
381
+
382
+
383
+ ## Installation and Quick Guide
384
+ To install and run the application:
385
+ 1. [Fork this repo](https://github.com/dvlab-research/LongLoRA/fork) on github
386
+ 2. Clone the repository on your local machine, using git clone and pasting the url of this project.
387
+ 3. Run the following code:
388
+ ```
389
+ pip install -r requirements.txt
390
+ pip install flash-attn --no-build-isolation
391
+ ```
392
+ 4. Use either a [Released model](#released-models) or [Fine tune](#fine-tuning) a model to fit your preferences.
393
+ 5. Test your model by chat.
394
+ 6. Deploy your own demo.
395
+
396
+ ## LongAlpaca Data
397
+
398
+ LongAlpaca-12k contains 9k long QA data that we collected and 3k short QA sampled from the original [Alpaca data](https://github.com/tatsu-lab/stanford_alpaca/blob/main/alpaca_data.json). This is to avoid the case that the model might degrade at short instruction following. The data we collect contains various types and amounts as the following figure.
399
+
400
+ | Data | Short QA | Long QA | Total | Download |
401
+ |:---------------|----------|----------|----------|----------|
402
+ | LongAlpaca-12k | 3k | 9k | 12k | [Link](https://huggingface.co/datasets/Yukang/LongAlpaca-12k) |
403
+
404
+ Following the original Alpaca format, our Long QA data uses the following prompts for fine-tuning:
405
+ - `instruction`: `str`, describes the task the model should perform. For example, to answer a question after reading a book section or paper. We vary the contents and questions to make instructions diverse.
406
+ - `output`: `str`, the answer to the instruction.
407
+
408
+ We did not use the `input` format in the Alpaca format for simplicity.
409
+
410
+ ## Models
411
+
412
+ ### Models with supervised fine-tuning
413
+ | Model | Size | Context | Train | Link |
414
+ |:---------------|------|---------|---------|-----------------------------------------------------------------------------------------------------------------------|
415
+ | LongAlpaca-7B | 7B | 32768 | Full FT | [Model](https://huggingface.co/Yukang/LongAlpaca-7B) |
416
+ | LongAlpaca-13B | 13B | 32768 | Full FT | [Model](https://huggingface.co/Yukang/LongAlpaca-13B) |
417
+ | LongAlpaca-70B | 70B | 32768 | LoRA+ | [Model](https://huggingface.co/Yukang/LongAlpaca-70B) [(LoRA-weight)](https://huggingface.co/Yukang/LongAlpaca-70B-lora) |
418
+
419
+
420
+ ### Models with context extension via fully fine-tuning
421
+ | Model | Size | Context | Train | Link |
422
+ |:----------------------------|------|---------|-------|-------------------------------------------------------------------|
423
+ | Llama-2-7b-longlora-8k-ft | 7B | 8192 | Full FT | [Model](https://huggingface.co/Yukang/Llama-2-7b-longlora-8k-ft) |
424
+ | Llama-2-7b-longlora-16k-ft | 7B | 16384 | Full FT | [Model](https://huggingface.co/Yukang/Llama-2-7b-longlora-16k-ft) |
425
+ | Llama-2-7b-longlora-32k-ft | 7B | 32768 | Full FT | [Model](https://huggingface.co/Yukang/Llama-2-7b-longlora-32k-ft) |
426
+ | Llama-2-7b-longlora-100k-ft | 7B | 100000 | Full FT | [Model](https://huggingface.co/Yukang/Llama-2-7b-longlora-100k-ft) |
427
+ | Llama-2-13b-longlora-8k-ft | 13B | 8192 | Full FT | [Model](https://huggingface.co/Yukang/Llama-2-13b-longlora-8k-ft) |
428
+ | Llama-2-13b-longlora-16k-ft | 13B | 16384 | Full FT | [Model](https://huggingface.co/Yukang/Llama-2-13b-longlora-16k-ft) |
429
+ | Llama-2-13b-longlora-32k-ft | 13B | 32768 | Full FT | [Model](https://huggingface.co/Yukang/Llama-2-13b-longlora-32k-ft) |
430
+
431
+ ### Models with context extension via improved LoRA fine-tuning
432
+ | Model | Size | Context | Train | Link |
433
+ |:----------------------------|------|---------|-------|---------------------------------------------------------------------|
434
+ | Llama-2-7b-longlora-8k | 7B | 8192 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-7b-longlora-8k) |
435
+ | Llama-2-7b-longlora-16k | 7B | 16384 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-7b-longlora-16k) |
436
+ | Llama-2-7b-longlora-32k | 7B | 32768 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-7b-longlora-32k) |
437
+ | Llama-2-13b-longlora-8k | 13B | 8192 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-13b-longlora-8k) |
438
+ | Llama-2-13b-longlora-16k | 13B | 16384 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-13b-longlora-16k) |
439
+ | Llama-2-13b-longlora-32k | 13B | 32768 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-13b-longlora-32k) |
440
+ | Llama-2-13b-longlora-64k | 13B | 65536 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-13b-longlora-64k) |
441
+ | Llama-2-70b-longlora-32k | 70B | 32768 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-70b-longlora-32k) |
442
+ | Llama-2-70b-chat-longlora-32k | 70B | 32768 | LoRA+ | [LoRA-weight](https://huggingface.co/Yukang/Llama-2-70b-chat-longlora-32k) |
443
+
444
+ ## Training
445
+ ### Pre-trained weights
446
+ We use LLaMA2 models as the pre-trained weights and fine-tune them to long context window sizes. Download based on your choices.
447
+
448
+ | Pre-trained weights |
449
+ |:-------------------------------------------------------------------------------------|
450
+ | [Llama-2-7b-hf](https://huggingface.co/meta-llama/Llama-2-7b-hf) |
451
+ |[Llama-2-13b-hf](https://huggingface.co/meta-llama/Llama-2-13b-hf) |
452
+ | [Llama-2-70b-hf](https://huggingface.co/meta-llama/Llama-2-70b-hf) |
453
+ | [Llama-2-7b-chat-hf](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf) |
454
+ | [Llama-2-13b-chat-hf](https://huggingface.co/meta-llama/Llama-2-13b-chat-hf) |
455
+ | [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) |
456
+
457
+ This project also supports GPTNeoX models as the base model architecture. Some candidate pre-trained weights may include [GPT-NeoX-20B](https://huggingface.co/EleutherAI/gpt-neox-20b), [Polyglot-ko-12.8B](https://huggingface.co/EleutherAI/polyglot-ko-12.8b) and other variants.
458
+
459
+ ### Fine-tuning
460
+ ```
461
+ torchrun --nproc_per_node=8 fine-tune.py \
462
+ --model_name_or_path path_to/Llama-2-7b-hf \
463
+ --bf16 True \
464
+ --output_dir path_to_saving_checkpoints \
465
+ --cache_dir path_to_cache \
466
+ --model_max_length 8192 \
467
+ --use_flash_attn True \
468
+ --low_rank_training False \
469
+ --num_train_epochs 1 \
470
+ --per_device_train_batch_size 1 \
471
+ --per_device_eval_batch_size 2 \
472
+ --gradient_accumulation_steps 8 \
473
+ --evaluation_strategy "no" \
474
+ --save_strategy "steps" \
475
+ --save_steps 1000 \
476
+ --save_total_limit 2 \
477
+ --learning_rate 2e-5 \
478
+ --weight_decay 0.0 \
479
+ --warmup_steps 20 \
480
+ --lr_scheduler_type "constant_with_warmup" \
481
+ --logging_steps 1 \
482
+ --deepspeed "ds_configs/stage2.json" \
483
+ --tf32 True \
484
+ --max_steps 1000
485
+ ```
486
+
487
+ - Please remember to change `path_to/Llama-2-7b-hf`, `path_to_saving_checkpoints`, `path_to_cache` to your own directory.
488
+ - Note that you can change `model_max_length` to other values.
489
+ - You could change `ds_configs/stage2.json` to `ds_configs/stage3.json` if you want.
490
+ - Please set `use_flash_attn` as `False` if you use V100 machines or do not install flash attention.
491
+ - You can set `low_rank_training` as `False` if you want to use fully fine-tuning. It will cost more GPU memory and slower, but the performance will be a bit better.
492
+ - When training is finished, to get the full model weight:
493
+ ```
494
+ cd path_to_saving_checkpoints && python zero_to_fp32.py . pytorch_model.bin
495
+ ```
496
+
497
+ ### Supervised Fine-tuning
498
+ ```
499
+ torchrun --nproc_per_node=8 supervised-fine-tune.py \
500
+ --model_name_or_path path_to_Llama2_chat_models \
501
+ --bf16 True \
502
+ --output_dir path_to_saving_checkpoints \
503
+ --model_max_length 32768 \
504
+ --use_flash_attn True \
505
+ --data_path LongAlpaca-12k.json \
506
+ --low_rank_training True \
507
+ --num_train_epochs 3 \
508
+ --per_device_train_batch_size 1 \
509
+ --per_device_eval_batch_size 2 \
510
+ --gradient_accumulation_steps 1 \
511
+ --evaluation_strategy "no" \
512
+ --save_strategy "steps" \
513
+ --save_steps 1000 \
514
+ --save_total_limit 2 \
515
+ --learning_rate 2e-5 \
516
+ --weight_decay 0.0 \
517
+ --warmup_steps 20 \
518
+ --lr_scheduler_type "constant_with_warmup" \
519
+ --logging_steps 1 \
520
+ --deepspeed "ds_configs/stage2.json" \
521
+ --tf32 True
522
+ ```
523
+ - There is no need to make supervised fine-tuning upon the fine-tuned context extended models. It is all right to directly use base model as Llama2-chat models, as the amount of long instruction following data is enough for SFT.
524
+ - Our long instruction following data can be found in [LongAlpaca-12k.json](https://huggingface.co/datasets/Yukang/LongAlpaca-12k).
525
+
526
+
527
+ ### Get trainable weights in low-rank training
528
+ In low-rank training, we set embedding and normalization layers as trainable. Please use the following line to extract the trainable weights `trainable_params.bin` from `pytorch_model.bin`
529
+ ```
530
+ python3 get_trainable_weights.py --checkpoint_path path_to_saving_checkpoints --trainable_params "embed,norm"
531
+ ```
532
+
533
+ ### Merge LoRA Weight
534
+ Merge the LoRA weights of `pytorch_model.bin` and trainable parameters `trainable_params.bin`, save the resulting model into your desired path in the Hugging Face format:
535
+ ```
536
+ python3 merge_lora_weights_and_save_hf_model.py \
537
+ --base_model path_to/Llama-2-7b-hf \
538
+ --peft_model path_to_saving_checkpoints \
539
+ --context_size 8192 \
540
+ --save_path path_to_saving_merged_model
541
+ ```
542
+ For example,
543
+ ```
544
+ python3 merge_lora_weights_and_save_hf_model.py \
545
+ --base_model /dataset/pretrained-models/Llama-2-7b-hf \
546
+ --peft_model /dataset/yukangchen/hf_models/lora-models/Llama-2-7b-longlora-8k \
547
+ --context_size 8192 \
548
+ --save_path /dataset/yukangchen/models/Llama-2-7b-longlora-8k-merged
549
+ ```
550
+
551
+
552
+ ## Evaluation
553
+ ### Perplexity Validation
554
+ To evaluate a model that is trained in the low-rank setting, please set both `base_model` and `peft_model`. `base_model` is the pre-trained weight. `peft_model` is the path to the saved checkpoint, which should contain `trainable_params.bin`, `adapter_model.bin` and `adapter_config.json`. For example,
555
+ ```
556
+ python3 eval.py --seq_len 8192 --context_size 8192 --batch_size 1 --base_model path_to/Llama-2-7b-hf --peft_model path_to_saving_checkpoints --data_path pg19/test.bin
557
+ ```
558
+
559
+ To evaluate a model that is fully fine-tuned, you only need to set `base_model` as the path to the saved checkpoint, which should contain `pytorch_model.bin` and `config.json`. `peft_model` should be ignored.
560
+ ```
561
+ python3 eval.py --seq_len 8192 --context_size 8192 --batch_size 1 --base_model path_to_saving_checkpoints --data_path pg19/test.bin
562
+ ```
563
+
564
+ - Note that `--seq_len` is to set the sequence length for evaluation. `--context_size` is to set the context length of the model during fine-tuning. `--seq_len` should not be larger than `--context_size`.
565
+
566
+ - We have already tokenized the validation and test splits of PG19 and proof-pile dataset into `pg19/validation.bin`, `pg19/test.bin`, and `proof-pile/test_sampled_data.bin`, with the tokenizer of LLaMA. `proof-pile/test_sampled_data.bin` contains 128 documents that are randomly sampled from the total proof-pile test split. For each document, it has at least 32768 tokens. We also release the sampled ids in [proof-pile/test_sampled_ids.bin](https://drive.google.com/file/d/1cnzWODLRQYAd7HeugzLCIhaqzaLZv7J5/view?usp=share_link). You can download them from the links below.
567
+
568
+ | Dataset | Split | Link |
569
+ |:-----------|------------|--------------------------------------------------------------------------------------------------------------|
570
+ | PG19 | validation | [pg19/validation.bin](https://drive.google.com/file/d/1rbJvb0qRIf2mQoN2ON7S93TbTzMnlrN6/view?usp=share_link) |
571
+ | PG19 | test | [pg19/test.bin](https://drive.google.com/file/d/1QANDMdctpacPAYgS04adDXqByGEq-Ret/view?usp=share_link) |
572
+ | Proof-pile | test | [proof-pile/test_sampled_data.bin](https://drive.google.com/file/d/1bUI5lPDvrqzY_XXJJ2sSuvZx0Y9AZClE/view?usp=share_link) |
573
+
574
+
575
+ ### Passkey Retrieval
576
+ We provide a manner to test the passkey retrieval accuracy. For example,
577
+ ```
578
+ python3 passkey_retrivial.py \
579
+ --context_size 32768 \
580
+ --base_model path_to/Llama-2-7b-longlora-32k \
581
+ --max_tokens 32768 \
582
+ --interval 1000
583
+ ```
584
+ - Note that the `context_size` is the context length during fine-tuning.
585
+ - `max_tokens` is maximum length for the document in passkey retrieval evaluation.
586
+ - `interval` is the interval during the document length increasing. It is a rough number because the document increases by sentences.
587
+
588
+ ## Demo
589
+ ### Local Inference
590
+ To chat with [Llama-2-13b-chat-longlora-32k-sft](https://huggingface.co/Yukang/Llama-2-13b-chat-longlora-32k-sft) or [Llama-2-70b-chat-longlora-32k-sft](https://huggingface.co/Yukang/Llama-2-70b-chat-longlora-32k-sft), you need to run `merge_lora_weights_and_save_hf_model.py` first, and then:
591
+ ```
592
+ python3 inference.py \
593
+ --base_model path_to_model \
594
+ --question $question \
595
+ --context_size $context_length \
596
+ --max_gen_len $max_gen_len \
597
+ --flash_attn True \
598
+ --material $material_content \
599
+ --material_type $material_type \
600
+ --material_title $material_title
601
+ ```
602
+ To ask a question related to a book:
603
+ ```
604
+ python3 inference.py \
605
+ --base_model /data/models/Llama-2-13b-chat-longlora-32k-sft \
606
+ --question "Why doesn't Professor Snape seem to like Harry?" \
607
+ --context_size 32768 \
608
+ --max_gen_len 512 \
609
+ --flash_attn True \
610
+ --material "materials/Harry Potter and the Philosophers Stone_section2.txt" \
611
+ --material_type "book" \
612
+ --material_title "Harry Potter and the Philosophers Stone"
613
+ ```
614
+ Note that you can ignore `material_type` or `material_title`.
615
+
616
+ To ask a question related to a paper:
617
+ ```
618
+ python3 inference.py \
619
+ --base_model /data/models/Llama-2-13b-chat-longlora-32k-sft \
620
+ --question "What are the main contributions and novelties of this work?" \
621
+ --context_size 32768 \
622
+ --max_gen_len 512 \
623
+ --flash_attn True \
624
+ --material "materials/paper1.txt" \
625
+ --material_type "paper"
626
+ ```
627
+
628
+ ### Online Demo
629
+ To deploy your own demo run
630
+ ```
631
+ python3 demo.py \
632
+ --base_model path_to_model \
633
+ --context_size $context_size \
634
+ --max_gen_len $max_gen_len \
635
+ --flash_attn True
636
+ ```
637
+ Example
638
+ ```
639
+ python3 demo.py \
640
+ --base_model /data/models/Llama-2-13b-chat-longlora-32k-sft \
641
+ --context_size 32768 \
642
+ --max_gen_len 512 \
643
+ --flash_attn True
644
+ ```
645
+ - Note that `flash_attn=True` will make the generation slow but save much GPU memory.
646
+
647
+ ## Data Generation via Pdf2text
648
+ During our dataset collection, we convert paper and books from pdf to text. The conversion quality has a large influence on the final model quality. We think that this step is non-trivial. We release the tool for the pdf2txt conversion, in the folder `pdf2txt`. It is built upon `pdf2image`, `easyocr`, `ditod` and `detectron2`. Please refer to the [README.md](pdf2txt/README.md) in `pdf2txt` for more details.
649
+
650
+ ## Citation
651
+ If you find this project useful in your research, please consider citing:
652
+
653
+ ```
654
+ @article{longlora,
655
+ title={LongLoRA: Efficient Fine-tuning of Long-Context Large Language Models},
656
+ author={Yukang Chen and Shengju Qian and Haotian Tang and Xin Lai and Zhijian Liu and Song Han and Jiaya Jia},
657
+ journal={arXiv:2309.12307},
658
+ year={2023}
659
+ }
660
+ ```
661
+
662
+
663
+ ```
664
+ @misc{long-alpaca,
665
+ author = {Yukang Chen and Shaozuo Yu and Shengju Qian and Haotian Tang and Xin Lai and Zhijian Liu and Song Han and Jiaya Jia},
666
+ title = {Long Alpaca: Long-context Instruction-following models},
667
+ year = {2023},
668
+ publisher = {GitHub},
669
+ journal = {GitHub repository},
670
+ howpublished = {\url{https://github.com/dvlab-research/LongLoRA}},
671
+ }
672
+ ```
673
+ ## Acknowledgement
674
+ - This work is built upon the [LLaMA2](https://ai.meta.com/llama) as the pre-trained models.
675
+ - This work can also be built upon the [GPTNeoX-HF](https://huggingface.co/docs/transformers/model_doc/gpt_neox) which is based upon [EleutherAI/GPTNeoX](https://github.com/EleutherAI/gpt-neox) as the pre-trained model architecture.
676
+ - This work is based on [DeepSpeed](https://github.com/microsoft/DeepSpeed), [peft](https://github.com/huggingface/peft), and [Flash-Attention2](https://github.com/Dao-AILab/flash-attention) for acceleration.
677
+ - Some evaluation code is modified upon [Landmark Attention](https://github.com/epfml/landmark-attention).
678
+ - We use [LongChat](https://github.com/DachengLi1/LongChat) for the retrieval evaluation.
679
+
680
+ ## License
681
+ - LongLoRA is licensed under the Apache License 2.0. This means that it requires the preservation of copyright and license notices.
682
+ - Data and weights are under CC-BY-NC 4.0 License. They are licensed for research use only, and allowed only non-commercial. Models trained using the dataset should not be used outside of research purposes.