jacobfulano commited on
Commit
f5a307f
1 Parent(s): 2e90941

Update Readme to clarify how to load model

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