Text Generation
Transformers
PyTorch
Safetensors
longllama
text-generation-inference
custom_code
Szymon Tworkowski commited on
Commit
65ce2de
1 Parent(s): f493e88

Add README.md

Browse files
Files changed (1) hide show
  1. README.md +194 -1
README.md CHANGED
@@ -5,4 +5,197 @@ datasets:
5
  pipeline_tag: text-generation
6
  tags:
7
  - text-generation-inference
8
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  pipeline_tag: text-generation
6
  tags:
7
  - text-generation-inference
8
+ ---
9
+ <p align="center" width="100%"><img src="https://raw.githubusercontent.com/CStanKonrad/long_llama/main/assets/longllama.png" alt="LongLLaMA" style="width: 50%; display: block; margin: auto;"></p>
10
+
11
+ # LongLLaMA: Focused Transformer Training for Context Scaling
12
+
13
+
14
+ [Colab](https://colab.research.google.com/github/CStanKonrad/long_llama/blob/main/long_llama_colab.ipynb) | [TLDR](#TLDR) | [Overview](#Overview) | [Usage](#Usage) | [LongLLaMA performance](#LongLLaMA-performance) | [Authors](#Authors) | [Citation](#Citation) | [License](License) | [Acknowledgments](#Acknowledgments)
15
+
16
+ ## TLDR
17
+ This repository contains the research preview of **LongLLaMA, a large language model capable of handling long contexts of 256k tokens or even more**.
18
+
19
+ LongLLaMA is built upon the foundation of [OpenLLaMA](https://github.com/openlm-research/open_llama) and fine-tuned using the Focused Transformer (FoT) method. We release a smaller 3B variant of the LongLLaMA model on a permissive license (Apache 2.0) and inference code supporting longer contexts on [Hugging Face](https://huggingface.co/syzymon/long_llama_3b). Our model weights can serve as the drop-in replacement of LLaMA in existing implementations (for short context up to 2048 tokens). Additionally, we provide evaluation results and comparisons against the original OpenLLaMA models. Stay tuned for further updates.
20
+
21
+
22
+ ## Overview
23
+ [Focused Transformer: Contrastive Training for Context Scaling](TODO) (FoT) presents a simple method for endowing language models with the ability to handle context consisting possibly of millions of tokens while training on significantly shorter input. FoT permits a subset of attention layers to access a memory cache of (key, value) pairs to extend the context length. The distinctive aspect of FoT is its training procedure, drawing from contrastive learning. Specifically, we deliberately expose the memory attention layers to both relevant and irrelevant keys (like negative samples from unrelated documents). This strategy incentivizes the model to differentiate keys connected with semantically diverse values, thereby enhancing their structure. This, in turn, makes it possible to extrapolate the effective context length much beyond what is seen in training.
24
+
25
+
26
+ **LongLLaMA** is an [OpenLLaMA](https://github.com/openlm-research/open_llama) model finetuned with the FoT method,
27
+ with three layers used for context extension. Crucially, LongLLama is able to extrapolate much beyond the context length seen in training: $8k$. E.g., in the key retrieval task, it can handle inputs of length $256k$.
28
+
29
+ <center>
30
+
31
+ | | [LongLLaMA-3B](https://huggingface.co/syzymon/long_llama_3b) | LongLLaMA-7B<br />*(coming soon)*| LongLLaMA-13B<br />*(coming soon)*|
32
+ |----------------|----------|-----------|-----------|
33
+ | Source model | [OpenLLaMA-3B](https://huggingface.co/openlm-research/open_llama_3b_easylm) | - | - |
34
+ | Source model tokens | 1T | - | - |
35
+ | Fine-tuning tokens | 10B | - | -|
36
+ | Memory layers | 6, 12, 18 | - | -|
37
+
38
+ </center>
39
+
40
+
41
+ ## Usage
42
+
43
+ See also: [colab with an example usage of LongLLaMA](https://colab.research.google.com/github/CStanKonrad/long_llama/blob/main/long_llama_colab.ipynb).
44
+ ### Requirements
45
+ ```
46
+ pip install --upgrade pip
47
+ pip install transformers==4.30 sentencepiece accelerate
48
+ ```
49
+
50
+ ### Loading model
51
+ ```python
52
+ import torch
53
+ from transformers import LlamaTokenizer, AutoModelForCausalLM
54
+
55
+ tokenizer = LlamaTokenizer.from_pretrained("syzymon/long_llama_3b")
56
+ model = AutoModelForCausalLM.from_pretrained("syzymon/long_llama_3b",
57
+ torch_dtype=torch.float32,
58
+ trust_remote_code=True)
59
+ ```
60
+
61
+ ### Input handling and generation
62
+ LongLLaMA uses the Hugging Face interface, the long input given to the model will be
63
+ split into context windows and loaded into the memory cache.
64
+ ```python
65
+ prompt = "My name is Julien and I like to"
66
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids
67
+ outputs = model(input_ids=input_ids)
68
+ ```
69
+ During the model call, one can provide the parameter `last_context_length` (default $1024$), which specifies the number of tokens left in the last context window. Tuning this parameter can improve generation as the first layers do not have access to memory. See details in [How LongLLaMA handles long inputs](#How-LongLLaMA-handles-long-inputs).
70
+
71
+ ```python
72
+ generation_output = model.generate(
73
+ input_ids=input_ids,
74
+ max_new_tokens=256,
75
+ num_beams=1,
76
+ last_context_length=1792,
77
+ do_sample=True,
78
+ temperature=1.0,
79
+ )
80
+ print(tokenizer.decode(generation_output[0]))
81
+ ```
82
+
83
+ ### Additional configuration
84
+ LongLLaMA has several other parameters:
85
+ * `mem_layers` specifies layers endowed with memory (should be either an empty list or a list of all memory layers specified in the description of the checkpoint).
86
+ * `mem_dtype` allows changing the type of memory cache
87
+ * `mem_attention_grouping` can trade off speed for reduced memory usage.
88
+ When equal to `(4, 2048)`, the memory layers will process at most $4*2048$ queries at once ($4$ heads and $2048$ queries for each head).
89
+
90
+ ```python
91
+ import torch
92
+ from transformers import LlamaTokenizer, AutoModelForCausalLM
93
+
94
+ tokenizer = LlamaTokenizer.from_pretrained("syzymon/long_llama_3b")
95
+ model = AutoModelForCausalLM.from_pretrained(
96
+ "syzymon/long_llama_3b", torch_dtype=torch.float32,
97
+ mem_layers=[],
98
+ mem_dtype='bfloat16',
99
+ trust_remote_code=True,
100
+ mem_attention_grouping=(4, 2048),
101
+ )
102
+ ```
103
+
104
+
105
+ ### Drop-in use with LLaMA code
106
+ LongLLaMA checkpoints can also be used as a drop-in replacement for LLaMA checkpoints in [Hugging Face implementation of LLaMA](https://huggingface.co/docs/transformers/main/model_doc/llama), but in this case, they will be limited to the original context length of $2048$.
107
+
108
+ ```python
109
+ from transformers import LlamaTokenizer, LlamaForCausalLM
110
+ import torch
111
+
112
+ tokenizer = LlamaTokenizer.from_pretrained("syzymon/long_llama_3b")
113
+ model = LlamaForCausalLM.from_pretrained("syzymon/long_llama_3b", torch_dtype=torch.float32)
114
+ ```
115
+
116
+
117
+ ### How LongLLaMA handles long inputs
118
+ Inputs over $2048$ tokens are automatically split into windows $w_1, \ldots, w_m$. The first $m-2$ windows contain $2048$ tokens each, $w_{m-1}$ has no more than $2048$ tokens, and $w_m$ contains the number of tokens specified by `last_context_length`. The model processes the windows one by one extending the memory cache after each. If `use_cache` is `True`, the last window will not be loaded to the memory cache but to the local (generation) cache.
119
+
120
+ The memory cache stores $(key, value)$ pairs for each head of the specified memory layers `mem_layers`. In addition to this, it stores attention masks.
121
+
122
+ If `use_cache=True` (which is the case in generation), LongLLaMA will use two caches: the memory cache for the specified layers and the local (generation) cache for all layers. When the local cache exceeds $2048$ elements, its content is moved to the memory cache for the memory layers.
123
+
124
+ For simplicity, context extension is realized with a memory cache and full attention in this repo. Replacing this simple mechanism with a KNN search over an external database is possible with systems like [Faiss](https://github.com/facebookresearch/faiss). This potentially would enable further context length scaling. We leave this as a future work.
125
+
126
+
127
+ ## LongLLaMA performance
128
+ We present some illustrative examples of LongLLaMA results and refer to our paper [Focused Transformer: Contrastive Training for Context Scaling](TODO) for more details.
129
+
130
+ We manage to achieve good performance on the passkey retrieval task from [Landmark Attention: Random-Access Infinite Context Length for Transformers](https://arxiv.org/abs/2305.16300). The code for generating the prompt and running the model is located in `examples/passkey.py`.
131
+
132
+ <p align="center" width="100%">
133
+ <img src="https://raw.githubusercontent.com/CStanKonrad/long_llama/main/assets/plot_passkey.png" alt="LongLLaMA" style="width: 70%; min-width: 300px; display: block; margin: auto;">
134
+ </p>
135
+
136
+ Our LongLLaMA 3B model also shows improvements when using long context on two downstream tasks, TREC question classification and WebQS question answering.
137
+ <center>
138
+
139
+
140
+ | Context/Dataset | TREC | WebQS |
141
+ | --- | --- | --- |
142
+ | $2K$ | 67.0 | 21.2 |
143
+ | $4K$ | 71.6 | 21.4 |
144
+ | $6K$ | 72.9 | 22.2 |
145
+ | $8K$ | **73.3** | **22.4** |
146
+
147
+ </center>
148
+
149
+ LongLLama retains performance on tasks that do not require long context. We provide a comparison with OpenLLaMA
150
+ on [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) in a zero-shot setting.
151
+ <center>
152
+
153
+ | Task/Metric | OpenLLaMA-3B | LongLLaMA-3B |
154
+ |----------------|----------|-----------|
155
+ | anli_r1/acc | 0.33 | 0.32 |
156
+ | anli_r2/acc | 0.32 | 0.33 |
157
+ | anli_r3/acc | 0.35 | 0.35 |
158
+ | arc_challenge/acc | 0.34 | 0.34 |
159
+ | arc_challenge/acc_norm | 0.37 | 0.37 |
160
+ | arc_easy/acc | 0.69 | 0.68 |
161
+ | arc_easy/acc_norm | 0.65 | 0.63 |
162
+ | boolq/acc | 0.68 | 0.68 |
163
+ | hellaswag/acc | 0.49 | 0.48 |
164
+ | hellaswag/acc_norm | 0.67 | 0.65 |
165
+ | openbookqa/acc | 0.27 | 0.28 |
166
+ | openbookqa/acc_norm | 0.40 | 0.38 |
167
+ | piqa/acc | 0.75 | 0.73 |
168
+ | piqa/acc_norm | 0.76 | 0.75 |
169
+ | record/em | 0.88 | 0.87 |
170
+ | record/f1 | 0.89 | 0.87 |
171
+ | rte/acc | 0.58 | 0.60 |
172
+ | truthfulqa_mc/mc1 | 0.22 | 0.24 |
173
+ | truthfulqa_mc/mc2 | 0.35 | 0.38 |
174
+ | wic/acc | 0.48 | 0.50 |
175
+ | winogrande/acc | 0.62 | 0.60 |
176
+ | Avg score | 0.53 | 0.53 |
177
+
178
+ </center>
179
+
180
+ ## Authors
181
+ - [Szymon Tworkowski](https://scholar.google.com/citations?user=1V8AeXYAAAAJ&hl=en)
182
+ - [Konrad Staniszewski](https://scholar.google.com/citations?user=CM6PCBYAAAAJ)
183
+ - [Mikołaj Pacek](https://scholar.google.com/citations?user=eh6iEbQAAAAJ&hl=en&oi=ao)
184
+ - [Henryk Michalewski](https://scholar.google.com/citations?user=YdHW1ycAAAAJ&hl=en)
185
+ - [Yuhuai Wu](https://scholar.google.com/citations?user=bOQGfFIAAAAJ&hl=en)
186
+ - [Piotr Miłoś](https://scholar.google.pl/citations?user=Se68XecAAAAJ&hl=pl&oi=ao)
187
+
188
+
189
+ ## Citation
190
+ To cite this work please use
191
+ ```bibtex
192
+ TODO
193
+ ```
194
+
195
+
196
+ ## License
197
+ The code and checkpoints are licensed under [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
198
+ Some of the examples use external code (see headers of files for copyright notices and licenses).
199
+
200
+ ## Acknowledgments
201
+ We gratefully acknowledge the TPU Research Cloud program, which was instrumental to our research by providing significant computational resources. We are also grateful to Xinyang Geng and Hao Liu for releasing [OpenLLaMA](https://github.com/openlm-research/open_llama) checkpoints and the [EasyLM](https://github.com/young-geng/EasyLM) library.