Update README.md
Browse files
README.md
CHANGED
|
@@ -1,199 +1,120 @@
|
|
| 1 |
---
|
| 2 |
library_name: transformers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
tags: []
|
| 4 |
---
|
| 5 |
|
| 6 |
-
# Model Card for
|
| 7 |
|
| 8 |
-
|
| 9 |
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
##
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
-
|
| 21 |
-
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
-
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
-
- **Model type:** [More Information Needed]
|
| 24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
- **Paper [optional]:** [More Information Needed]
|
| 34 |
-
- **Demo [optional]:** [More Information Needed]
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
###
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
[
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
###
|
| 65 |
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
### Training Data
|
| 79 |
-
|
| 80 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
-
|
| 82 |
-
[More Information Needed]
|
| 83 |
-
|
| 84 |
-
### Training Procedure
|
| 85 |
-
|
| 86 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
-
|
| 88 |
-
#### Preprocessing [optional]
|
| 89 |
-
|
| 90 |
-
[More Information Needed]
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#### Training Hyperparameters
|
| 94 |
-
|
| 95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
-
|
| 97 |
-
#### Speeds, Sizes, Times [optional]
|
| 98 |
-
|
| 99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
-
|
| 101 |
-
[More Information Needed]
|
| 102 |
-
|
| 103 |
-
## Evaluation
|
| 104 |
-
|
| 105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
-
|
| 107 |
-
### Testing Data, Factors & Metrics
|
| 108 |
-
|
| 109 |
-
#### Testing Data
|
| 110 |
-
|
| 111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
-
|
| 113 |
-
[More Information Needed]
|
| 114 |
-
|
| 115 |
-
#### Factors
|
| 116 |
-
|
| 117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
-
|
| 119 |
-
[More Information Needed]
|
| 120 |
-
|
| 121 |
-
#### Metrics
|
| 122 |
-
|
| 123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
-
|
| 125 |
-
[More Information Needed]
|
| 126 |
-
|
| 127 |
-
### Results
|
| 128 |
-
|
| 129 |
-
[More Information Needed]
|
| 130 |
-
|
| 131 |
-
#### Summary
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
## Model Examination [optional]
|
| 136 |
-
|
| 137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
-
|
| 139 |
-
[More Information Needed]
|
| 140 |
-
|
| 141 |
-
## Environmental Impact
|
| 142 |
-
|
| 143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
-
|
| 145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
-
|
| 147 |
-
- **Hardware Type:** [More Information Needed]
|
| 148 |
-
- **Hours used:** [More Information Needed]
|
| 149 |
-
- **Cloud Provider:** [More Information Needed]
|
| 150 |
-
- **Compute Region:** [More Information Needed]
|
| 151 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
-
|
| 153 |
-
## Technical Specifications [optional]
|
| 154 |
-
|
| 155 |
-
### Model Architecture and Objective
|
| 156 |
-
|
| 157 |
-
[More Information Needed]
|
| 158 |
-
|
| 159 |
-
### Compute Infrastructure
|
| 160 |
-
|
| 161 |
-
[More Information Needed]
|
| 162 |
-
|
| 163 |
-
#### Hardware
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Software
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
## Citation [optional]
|
| 172 |
-
|
| 173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
|
| 175 |
**BibTeX:**
|
| 176 |
|
| 177 |
-
[
|
| 178 |
-
|
| 179 |
-
**APA:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
-
|
| 185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
-
|
| 187 |
-
[More Information Needed]
|
| 188 |
-
|
| 189 |
-
## More Information [optional]
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## Model Card Authors [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Contact
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
|
|
|
| 1 |
---
|
| 2 |
library_name: transformers
|
| 3 |
+
license: other
|
| 4 |
+
license_name: "openai-gpt2-license"
|
| 5 |
+
license_link: "https://github.com/openai/gpt-2/blob/master/LICENSE"
|
| 6 |
+
base_model:
|
| 7 |
+
- openai-community/gpt2-large
|
| 8 |
tags: []
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Model Card for `schaeff/gpt2-large_LNFree600`
|
| 12 |
|
| 13 |
+
Associated publication: *Transformers Don’t Need LayerNorm at Inference Time: Scaling LayerNorm Removal to GPT-2 XL and the Implications for Mechanistic Interpretability* (arXiv TBD)
|
| 14 |
|
| 15 |
+
Associated GitHub: [removing-layer-norm](https://github.com/submarat/removing-layer-norm)
|
| 16 |
|
| 17 |
+
This model is based on *openai-community/gpt2-large* and was finetuned on OpenWebText for 600 iterations with 0.5M tokens per iteration. During the finetuning, LayerNorm modules were sequentially disabled. More details on the disabling procedure can be found in the associated publication.
|
| 18 |
|
| 19 |
+
## Usage
|
| 20 |
|
| 21 |
+
This model uses the standard `GPT2LMHeadModel` architecture to avoid requiring `trust_remote_code=True`. While LayerNorm blocks are technically present, they have been effectively disabled through parameter manipulation.
|
| 22 |
|
| 23 |
+
**How LayerNorm is disabled:**
|
| 24 |
|
| 25 |
+
- **Epsilon values**: Set to 1e12 (extremely large), so LayerNorm has no normalizing effect
|
| 26 |
+
- **Scale parameters**: Set to 1e6 to counteract the large epsilon value
|
| 27 |
|
| 28 |
+
This approach maintains compatibility with the standard GPT-2 architecture while effectively creating a LayerNorm-free model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
**Complete LayerNorm removal:**
|
| 31 |
+
If you want to fully remove LayerNorm operations, you can replace `ln_1`, `ln_2` and `ln_f` modules with identity functions.
|
| 32 |
|
| 33 |
+
Loading instructions:
|
| 34 |
|
| 35 |
+
You can load the model with `transformers`:
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
```python
|
| 38 |
+
model = GPT2LMHeadModel.from_pretrained("schaeff/gpt2-large_LNFree600")
|
| 39 |
+
```
|
| 40 |
|
| 41 |
+
The LayerNorm module inside transformers will not affect the model due to the parameter manipulation. Howevr, this is a bit hacky and we recommend properly the replacing LayerNorm modules with the identity in either TransformerLens or NNSight.
|
| 42 |
|
| 43 |
+
### TransformerLens and NNSight loading code
|
| 44 |
|
| 45 |
+
```python
|
| 46 |
+
import torch
|
| 47 |
+
from transformers import GPT2LMHeadModel
|
| 48 |
+
from transformer_lens import HookedTransformer
|
| 49 |
|
| 50 |
+
model = GPT2LMHeadModel.from_pretrained("schaeff/gpt2-large_LNFree600").to("cpu")
|
| 51 |
|
| 52 |
+
# Undo hacky LayerNorm removal
|
| 53 |
+
for block in model.transformer.h:
|
| 54 |
+
block.ln_1.weight.data = block.ln_1.weight.data / 1e6
|
| 55 |
+
block.ln_1.eps = 1e-5
|
| 56 |
+
block.ln_2.weight.data = block.ln_2.weight.data / 1e6
|
| 57 |
+
block.ln_2.eps = 1e-5
|
| 58 |
+
model.transformer.ln_f.weight.data = model.transformer.ln_f.weight.data / 1e6
|
| 59 |
+
model.transformer.ln_f.eps = 1e-5
|
| 60 |
|
| 61 |
+
# Properly replace LayerNorms by Identities
|
| 62 |
+
def removeLN(transformer_lens_model):
|
| 63 |
+
for i in range(len(transformer_lens_model.blocks)):
|
| 64 |
+
transformer_lens_model.blocks[i].ln1 = torch.nn.Identity()
|
| 65 |
+
transformer_lens_model.blocks[i].ln2 = torch.nn.Identity()
|
| 66 |
+
transformer_lens_model.ln_final = torch.nn.Identity()
|
| 67 |
|
| 68 |
+
# transformer_lens
|
| 69 |
+
hooked_model = HookedTransformer.from_pretrained("gpt2", hf_model=model, fold_ln=True, center_unembed=False).to("cpu")
|
| 70 |
+
removeLN(hooked_model)
|
| 71 |
|
| 72 |
+
# NNSight:
|
| 73 |
+
from nnsight.models.UnifiedTransformer import UnifiedTransformer
|
| 74 |
|
| 75 |
+
model_nnsight = UnifiedTransformer(model="gpt2", hf_model=model, fold_ln=True, center_unembed=False).to("cpu")
|
| 76 |
+
removeLN(model_nnsight)
|
| 77 |
+
```
|
| 78 |
|
| 79 |
+
This example code is based on [Logan Riggs' comment](https://www.lesswrong.com/posts/THzcKKQd4oWkg4dSP/you-can-remove-gpt2-s-layernorm-by-fine-tuning-for-an-hour?commentId=Gcq8wic9WmdnqM2Fm).
|
| 80 |
|
| 81 |
+
We recommend to look at [removing-layer-norm](https://github.com/submarat/removing-layer-norm) for seeing the entire workflow of removal, upload, and loading LN free models. In particular, the function `remove_layernorm` in `utils.py` for details on the parameter hack and `eval.py` for loading.
|
| 82 |
|
| 83 |
+
## Model Collection
|
| 84 |
|
| 85 |
+
This model is part of a collection of LayerNorm-free models. The table below provides links and details.
|
| 86 |
|
| 87 |
+
### Evaluation results of LN-free, vanilla fine-tuned, and original GPT-2 models
|
| 88 |
|
| 89 |
+
*Reported values are mean cross-entropy losses for 10.2M tokens for The Pile and The Pile filtered and 4.5M tokens for the OpenWebText (WT) validation set. For each model size and dataset, the lowest loss is highlighted in **bold**, and the loss difference between the LN-free model and the best-performing model is shown in brackets.*
|
| 90 |
|
| 91 |
+
| Model | FT steps | [OWT (val)](https://huggingface.co/datasets/Skylion007/openwebtext) | [The Pile](https://huggingface.co/datasets/apollo-research/monology-pile-uncopyrighted-tokenizer-gpt2) | [The Pile-filtered](https://huggingface.co/datasets/lucabaroni/apollo-pile-filtered-10k) |
|
| 92 |
+
|-------|----------|-----------|----------|-------------------|
|
| 93 |
+
| OpenAI [GPT-2 Small original](https://huggingface.co/openai-community/gpt2) | 0 | 3.1006 | **2.8450** | **2.7899** |
|
| 94 |
+
| schaeff [GPT-2 Small vanilla](https://huggingface.co/schaeff/gpt2-small_vanilla300) | 300 | **3.0126** | 2.8511 | 2.8112 |
|
| 95 |
+
| schaeff [GPT-2 Small LN-free](https://huggingface.co/schaeff/gpt2-small_LNFree300) | 300 | 3.0797 [+0.0671] | 2.8852 [+0.0402] | 2.8757 [+0.0858] |
|
| 96 |
+
||||||
|
| 97 |
+
| OpenAI [GPT-2 Medium original](https://huggingface.co/openai-community/gpt2-medium) | 0 | 2.8145 | **2.5163** | **2.5390** |
|
| 98 |
+
| schaeff [GPT-2 Medium vanilla](https://huggingface.co/schaeff/gpt2-medium_vanilla500) | 500 | **2.7390** | 2.5752 | 2.5724 |
|
| 99 |
+
| schaeff [GPT-2 Medium LN-free](https://huggingface.co/schaeff/gpt2-medium_LNFree500) | 500 | 2.7642 [+0.0252] | 2.6579 [+0.1416] | 2.6352 [+0.0962] |
|
| 100 |
+
||||||
|
| 101 |
+
| OpenAI [GPT-2 Large original](https://huggingface.co/openai-community/gpt2-large) | 0 | 2.6623 | **2.5320** | **2.4347** |
|
| 102 |
+
| schaeff [GPT-2 Large vanilla](https://huggingface.co/schaeff/gpt2-large_vanilla600) | 600 | **2.6240** | 2.6233 | 2.5074 |
|
| 103 |
+
| schaeff [GPT-2 Large LN-free](https://huggingface.co/schaeff/gpt2-large_LNFree600) | 600 | 2.6384 [+0.0144] | 2.7504 [+0.2184] | 2.5159 [+0.0812] |
|
| 104 |
+
||||||
|
| 105 |
+
| OpenAI [GPT-2 XL original](https://huggingface.co/openai-community/gpt2-xl) | 0 | 2.5567 | **2.4436**¹ | **2.3739** |
|
| 106 |
+
| schaeff [GPT-2 XL vanilla](https://huggingface.co/schaeff/gpt2-xl_vanilla800) | 800 | **2.4799** | 2.4673 | 2.3821 |
|
| 107 |
+
| schaeff [GPT-2 XL LN-free](https://huggingface.co/schaeff/gpt2-xl_LNFree800) | 800 | 2.5052 [+0.0253] | 130.2197² | 2.3992 [+0.0253] |
|
| 108 |
|
| 109 |
+
#### **Footnotes:**
|
| 110 |
|
| 111 |
+
1. GPT-2 XL original: Median: 1.0103, 95 Percentile Range: [0.0005, 10.6193], 99.9% Percentile Range [≈0.0000, 43.0064]
|
| 112 |
+
2. GPT-2 XL LN-free: Median: 1.0937, 95 Percentile Range: [0.0004, 10.7548], 99.9% Percentile Range [≈0.0000, 48.6459]
|
| 113 |
|
| 114 |
+
## Citation
|
| 115 |
|
| 116 |
+
Title: *Transformers Don’t Need LayerNorm at Inference Time: Scaling LayerNorm Removal to GPT-2 XL and the Implications for Mechanistic Interpretability*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
**BibTeX:**
|
| 119 |
|
| 120 |
+
[TBD]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|