jacobfulano commited on
Commit
40ad1f5
1 Parent(s): 27a6960

Clarify how to load model and use ALiBi in README

Browse files
Files changed (1) hide show
  1. README.md +35 -16
README.md CHANGED
@@ -33,42 +33,61 @@ The primary use case of these models is for research on efficient pretraining an
33
 
34
  April 2023
35
 
 
 
 
 
36
  ## Documentation
37
 
38
- * [Blog post](https://www.mosaicml.com/blog/mosaicbert)
39
- * [Github (mosaicml/examples/bert repo)](https://github.com/mosaicml/examples/tree/main/examples/bert)
 
 
 
 
40
 
41
  ## How to use
42
 
43
  ```python
44
- from transformers import AutoModelForMaskedLM
45
- mlm = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512', trust_remote_code=True)
46
- ```
 
47
 
48
- The tokenizer for this model is simply the Hugging Face `bert-base-uncased` tokenizer.
49
 
50
- ```python
51
- from transformers import BertTokenizer
52
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
 
 
 
53
  ```
54
 
55
- To use this model directly for masked language modeling, use `pipeline`:
 
 
 
56
 
57
  ```python
58
- from transformers import AutoModelForMaskedLM, BertTokenizer, pipeline
 
59
 
60
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
61
- mlm = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512', trust_remote_code=True)
62
 
63
- classifier = pipeline('fill-mask', model=mlm, tokenizer=tokenizer)
64
 
65
- classifier("I [MASK] to the store yesterday.")
66
- ```
67
 
68
  **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).
69
 
70
  **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).
71
 
 
 
 
 
 
72
  ### Remote Code
73
 
74
  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:
 
33
 
34
  April 2023
35
 
36
+ ## Model Date
37
+
38
+ April 2023
39
+
40
  ## Documentation
41
 
42
+ * [Project Page (mosaicbert.github.io)](mosaicbert.github.io)
43
+ * [Github (mosaicml/examples/tree/main/examples/benchmarks/bert)](https://github.com/mosaicml/examples/tree/main/examples/benchmarks/bert)
44
+ * [Paper (NeurIPS 2023)](https://openreview.net/forum?id=5zipcfLC2Z)
45
+ * Colab Tutorials:
46
+ * [MosaicBERT Tutorial Part 1: Load Pretrained Weights and Experiment with Sequence Length Extrapolation Using ALiBi](https://colab.research.google.com/drive/1r0A3QEbu4Nzs2Jl6LaiNoW5EumIVqrGc?usp=sharing)
47
+ * [Blog Post (March 2023)](https://www.mosaicml.com/blog/mosaicbert)
48
 
49
  ## How to use
50
 
51
  ```python
52
+ import torch
53
+ import transformers
54
+ from transformers import AutoModelForMaskedLM, BertTokenizer, pipeline
55
+ from transformers import BertTokenizer, BertConfig
56
 
57
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # MosaicBERT uses the standard BERT tokenizer
58
 
59
+ config = transformers.BertConfig.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512') # the config needs to be passed in
60
+ mosaicbert = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512',config=config,trust_remote_code=True)
61
+
62
+ # To use this model directly for masked language modeling
63
+ mosaicbert_classifier = pipeline('fill-mask', model=mosaicbert, tokenizer=tokenizer,device="cpu")
64
+ mosaicbert_classifier("I [MASK] to the store yesterday.")
65
  ```
66
 
67
+ Note that the tokenizer for this model is simply the Hugging Face `bert-base-uncased` tokenizer.
68
+
69
+ In order to take advantage of ALiBi by extrapolating to longer sequence lengths, simply change the `alibi_starting_size` flag in the
70
+ config file and reload the model.
71
 
72
  ```python
73
+ config = transformers.BertConfig.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512')
74
+ config.alibi_starting_size = 1024 # maximum sequence length updated to 1024 from config default of 512
75
 
76
+ mosaicbert = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512',config=config,trust_remote_code=True)
77
+ ```
78
 
79
+ This simply presets the non-learned linear bias matrix in every attention block to 1024 tokens (note that this particular model was trained with a sequence length of 512 tokens).
80
 
 
 
81
 
82
  **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).
83
 
84
  **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).
85
 
86
+ ### [Update 1/2/2024] Triton Flash Attention with ALiBi
87
+
88
+ 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,
89
+ 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)).
90
+
91
  ### Remote Code
92
 
93
  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: