jacobfulano commited on
Commit
4f88b6e
1 Parent(s): 885cc97

Clarify how to load model and use ALiBi

Browse files
Files changed (1) hide show
  1. README.md +31 -16
README.md CHANGED
@@ -36,40 +36,55 @@ April 2023
36
 
37
  ## Documentation
38
 
39
- * [Blog post](https://www.mosaicml.com/blog/mosaicbert)
40
- * [Github (mosaicml/examples/bert repo)](https://github.com/mosaicml/examples/tree/main/examples/bert)
 
 
 
 
41
 
42
  ## How to use
43
 
44
  ```python
45
- from transformers import AutoModelForMaskedLM
46
- mlm = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256', trust_remote_code=True)
47
- ```
 
48
 
49
- The tokenizer for this model is simply the Hugging Face `bert-base-uncased` tokenizer.
50
 
51
- ```python
52
- from transformers import BertTokenizer
53
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
 
 
 
54
  ```
55
 
56
- To use this model directly for masked language modeling, use `pipeline`:
 
 
 
57
 
58
  ```python
59
- from transformers import AutoModelForMaskedLM, BertTokenizer, pipeline
 
60
 
61
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
62
- mlm = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256', trust_remote_code=True)
63
 
64
- classifier = pipeline('fill-mask', model=mlm, tokenizer=tokenizer)
65
 
66
- classifier("I [MASK] to the store yesterday.")
67
- ```
68
 
69
  **To continue MLM pretraining**, follow the [MLM pre-training section of the mosaicml/examples/bert repo](https://github.com/mosaicml/examples/tree/main/examples/bert#mlm-pre-training).
70
 
71
  **To fine-tune this model for classification**, follow the [Single-task fine-tuning section of the mosaicml/examples/bert repo](https://github.com/mosaicml/examples/tree/main/examples/bert#single-task-fine-tuning).
72
 
 
 
 
 
 
73
  ### Remote Code
74
 
75
  This model requires that `trust_remote_code=True` be passed to the `from_pretrained` method. This is because we train using [FlashAttention (Dao et al. 2022)](https://arxiv.org/pdf/2205.14135.pdf), which is not part of the `transformers` library and depends on [Triton](https://github.com/openai/triton) and some custom PyTorch code. Since this involves executing arbitrary code, you should consider passing a git `revision` argument that specifies the exact commit of the code, for example:
36
 
37
  ## Documentation
38
 
39
+ * [Project Page (mosaicbert.github.io)](mosaicbert.github.io)
40
+ * [Github (mosaicml/examples/tree/main/examples/benchmarks/bert)](https://github.com/mosaicml/examples/tree/main/examples/benchmarks/bert)
41
+ * [Paper (NeurIPS 2023)](https://openreview.net/forum?id=5zipcfLC2Z)
42
+ * Colab Tutorials:
43
+ * [MosaicBERT Tutorial Part 1: Load Pretrained Weights and Experiment with Sequence Length Extrapolation Using ALiBi](https://colab.research.google.com/drive/1r0A3QEbu4Nzs2Jl6LaiNoW5EumIVqrGc?usp=sharing)
44
+ * [Blog Post (March 2023)](https://www.mosaicml.com/blog/mosaicbert)
45
 
46
  ## How to use
47
 
48
  ```python
49
+ import torch
50
+ import transformers
51
+ from transformers import AutoModelForMaskedLM, BertTokenizer, pipeline
52
+ from transformers import BertTokenizer, BertConfig
53
 
54
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # MosaicBERT uses the standard BERT tokenizer
55
 
56
+ config = transformers.BertConfig.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256') # the config needs to be passed in
57
+ mosaicbert = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256',config=config,trust_remote_code=True)
58
+
59
+ # To use this model directly for masked language modeling
60
+ mosaicbert_classifier = pipeline('fill-mask', model=mosaicbert, tokenizer=tokenizer,device="cpu")
61
+ mosaicbert_classifier("I [MASK] to the store yesterday.")
62
  ```
63
 
64
+ Note that the tokenizer for this model is simply the Hugging Face `bert-base-uncased` tokenizer.
65
+
66
+ In order to take advantage of ALiBi by extrapolating to longer sequence lengths, simply change the `alibi_starting_size` flag in the
67
+ config file and reload the model.
68
 
69
  ```python
70
+ config = transformers.BertConfig.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256')
71
+ config.alibi_starting_size = 512 # maximum sequence length updated to 512 from config default of 256
72
 
73
+ mosaicbert = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512',config=config,trust_remote_code=True)
74
+ ```
75
 
76
+ This simply presets the non-learned linear bias matrix in every attention block to 512 tokens (note that this particular model was trained with a sequence length of 256 tokens).
77
 
 
 
78
 
79
  **To continue MLM pretraining**, follow the [MLM pre-training section of the mosaicml/examples/bert repo](https://github.com/mosaicml/examples/tree/main/examples/bert#mlm-pre-training).
80
 
81
  **To fine-tune this model for classification**, follow the [Single-task fine-tuning section of the mosaicml/examples/bert repo](https://github.com/mosaicml/examples/tree/main/examples/bert#single-task-fine-tuning).
82
 
83
+ ### [Update 1/2/2024] Triton Flash Attention with ALiBi
84
+
85
+ Note that by default, triton Flash Attention is **not** enabled or required. In order to enable our custom implementation of triton Flash Attention with ALiBi from March 2023,
86
+ set `attention_probs_dropout_prob: 0.0`. We are currently working on supporting Flash Attention 2 (see [PR here](https://github.com/mosaicml/examples/pull/440)).
87
+
88
  ### Remote Code
89
 
90
  This model requires that `trust_remote_code=True` be passed to the `from_pretrained` method. This is because we train using [FlashAttention (Dao et al. 2022)](https://arxiv.org/pdf/2205.14135.pdf), which is not part of the `transformers` library and depends on [Triton](https://github.com/openai/triton) and some custom PyTorch code. Since this involves executing arbitrary code, you should consider passing a git `revision` argument that specifies the exact commit of the code, for example: