File size: 2,305 Bytes
34206a8
 
12a6e49
 
 
 
34206a8
12a6e49
 
 
3755945
12a6e49
 
 
 
 
 
 
4b33c8a
 
 
12a6e49
 
 
 
 
 
 
 
 
 
 
 
4b33c8a
12a6e49
682fb09
12a6e49
4b33c8a
 
12a6e49
 
 
 
 
 
 
 
 
 
 
 
 
 
e291f68
4b33c8a
12a6e49
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
---
license: apache-2.0
language:
- en
pipeline_tag: fill-mask
inference: false
---

# Monarch Mixer-BERT

The 80M checkpoint for M2-BERT-32k from the paper [Benchmarking and Building Long-Context Retrieval Models with LoCo and M2-BERT](https://arxiv.org/abs/2402.07440).

Check out our [GitHub](https://github.com/HazyResearch/m2/tree/main) for instructions on how to download and fine-tune it!

## How to use

You can load this model using Hugging Face `AutoModel`:
```python
from transformers import AutoModelForMaskedLM, BertConfig
config = BertConfig.from_pretrained("hazyresearch/M2-BERT-32K-Retrieval-Encoder-V1")
model = AutoModelForMaskedLM.from_pretrained("hazyresearch/M2-BERT-32K-Retrieval-Encoder-V1", config=config, trust_remote_code=True)
```

This model uses the Hugging Face `bert-base-uncased tokenizer`:
```
from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
```

## How to use

This model generates embeddings for retrieval. The embeddings have a dimensionality of 768:
```
from transformers import AutoTokenizer, AutoModelForMaskedLM, BertConfig

max_seq_length = 32768
testing_string = "Every morning, I make a cup of coffee to start my day."
config = BertConfig.from_pretrained("hazyresearch/M2-BERT-32K-Retrieval-Encoder-V1")
model = AutoModelForMaskedLM.from_pretrained("hazyresearch/M2-BERT-32K-Retrieval-Encoder-V1", config=config, trust_remote_code=True)

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", model_max_length=max_seq_length)
input_ids = tokenizer([testing_string], return_tensors="pt", padding="max_length", return_token_type_ids=False, truncation=True, max_length=max_seq_length)

outputs = model(**input_ids)
embeddings = outputs['sentence_embedding']
```

### Remote Code

This model requires `trust_remote_code=True` to be passed to the `from_pretrained` method. This is because we use custom PyTorch code (see our GitHub). You should consider passing a `revision` argument that specifies the exact git commit of the code, for example:

```python
mlm = AutoModelForMaskedLM.from_pretrained(
   "hazyresearch/M2-BERT-32K-Retrieval-Encoder-V1",
   config=config,
   trust_remote_code=True,
)
```

### Configuration
Note `use_flash_mm` is false by default. Using FlashMM is currently not supported.