PyTorch
Safetensors
English
nomic_bert
custom_code
hsila commited on
Commit
b0d0f1f
·
verified ·
1 Parent(s): f25bb14

Upload 10 files

Browse files
README.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ license: apache-2.0
5
+ datasets:
6
+ - wikimedia/wikipedia
7
+ - bookcorpus
8
+ - nomic-ai/nomic-bert-2048-pretraining-data
9
+ inference: false
10
+ ---
11
+
12
+ # nomic-bert-2048: A 2048 Sequence Length Pretrained BERT
13
+
14
+ `nomic-bert-2048` is a BERT model pretrained on `wikipedia` and `bookcorpus` with a max sequence length of 2048.
15
+
16
+ We make several modifications to our BERT training procedure similar to [MosaicBERT](https://www.databricks.com/blog/mosaicbert).
17
+ Namely, we add:
18
+ - Use [Rotary Position Embeddings](https://arxiv.org/pdf/2104.09864.pdf) to allow for context length extrapolation.
19
+ - Use SwiGLU activations as it has [been shown](https://arxiv.org/abs/2002.05202) to [improve model performance](https://www.databricks.com/blog/mosaicbert)
20
+ - Set dropout to 0
21
+
22
+ We evaluate the quality of nomic-bert-2048 on the standard [GLUE](https://gluebenchmark.com/) benchmark. We find
23
+ it performs comparably to other BERT models but with the advantage of a significantly longer context length.
24
+
25
+ | Model | Bsz | Steps | Seq | Avg | Cola | SST2 | MRPC | STSB | QQP | MNLI | QNLI | RTE |
26
+ |-------------|-----|-------|-------|----------|----------|----------|------|------|------|------|------|------|
27
+ | NomicBERT | 4k | 100k | 2048 | 0.84 | 0.50 | 0.93 | 0.88 | 0.90 | 0.92 | 0.86 | 0.92 | 0.82 |
28
+ | RobertaBase | 8k | 500k | 512 | 0.86 | 0.64 | 0.95 | 0.90 | 0.91 | 0.92 | 0.88 | 0.93 | 0.79 |
29
+ | JinaBERTBase| 4k | 100k | 512 | 0.83 | 0.51 | 0.95 | 0.88 | 0.90 | 0.81 | 0.86 | 0.92 | 0.79 |
30
+ | MosaicBERT | 4k | 178k | 128 | 0.85 | 0.59 | 0.94 | 0.89 | 0.90 | 0.92 | 0.86 | 0.91 | 0.83 |
31
+
32
+ ## Pretraining Data
33
+
34
+ We use [BookCorpus](https://huggingface.co/datasets/bookcorpus) and a 2023 dump of [wikipedia](https://huggingface.co/datasets/wikimedia/wikipedia).
35
+ We pack and tokenize the sequences to 2048 tokens. If a document is shorter than 2048 tokens, we append another document until it fits 2048 tokens.
36
+ If a document is greater than 2048 tokens, we split it across multiple documents. We release the dataset [here](https://huggingface.co/datasets/nomic-ai/nomic-bert-2048-pretraining-data/)
37
+
38
+
39
+ # Usage
40
+
41
+ ```python
42
+ from transformers import AutoModelForMaskedLM, AutoConfig, AutoTokenizer, pipeline
43
+
44
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased') # `nomic-bert-2048` uses the standard BERT tokenizer
45
+
46
+ config = AutoConfig.from_pretrained('nomic-ai/nomic-bert-2048', trust_remote_code=True) # the config needs to be passed in
47
+ model = AutoModelForMaskedLM.from_pretrained('nomic-ai/nomic-bert-2048',config=config, trust_remote_code=True)
48
+
49
+ # To use this model directly for masked language modeling
50
+ classifier = pipeline('fill-mask', model=model, tokenizer=tokenizer,device="cpu")
51
+
52
+ print(classifier("I [MASK] to the store yesterday."))
53
+ ```
54
+ To finetune the model for a Sequence Classification task, you can use the following snippet
55
+
56
+ ```python
57
+ from transformers import AutoConfig, AutoModelForSequenceClassification
58
+ model_path = "nomic-ai/nomic-bert-2048"
59
+ config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
60
+ # strict needs to be false here since we're initializing some new params
61
+ model = AutoModelForSequenceClassification.from_pretrained(model_path, config=config, trust_remote_code=True, strict=False)
62
+ ```
63
+
64
+ # Join the Nomic Community
65
+
66
+ - Nomic: [https://nomic.ai](https://nomic.ai)
67
+ - Discord: [https://discord.gg/myY5YDR8z8](https://discord.gg/myY5YDR8z8)
68
+ - Twitter: [https://twitter.com/nomic_ai](https://twitter.com/nomic_ai)
config.json ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation_function": "swiglu",
3
+ "architectures": [
4
+ "NomicBertForPreTraining"
5
+ ],
6
+ "attn_pdrop": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_hf_nomic_bert.NomicBertConfig",
9
+ "AutoModel": "modeling_hf_nomic_bert.NomicBertModel",
10
+ "AutoModelForMaskedLM": "modeling_hf_nomic_bert.NomicBertForPreTraining",
11
+ "AutoModelForSequenceClassification": "modeling_hf_nomic_bert.NomicBertForSequenceClassification",
12
+ "AutoModelForMultipleChoice": "modeling_hf_nomic_bert.NomicBertForMultipleChoice",
13
+ "AutoModelForQuestionAnswering": "modeling_hf_nomic_bert.NomicBertForQuestionAnswering",
14
+ "AutoModelForTokenClassification": "modeling_hf_nomic_bert.NomicBertForTokenClassification"
15
+ },
16
+ "bos_token_id": null,
17
+ "causal": false,
18
+ "dense_seq_output": true,
19
+ "embd_pdrop": 0.1,
20
+ "eos_token_id": null,
21
+ "fused_bias_fc": true,
22
+ "fused_dropout_add_ln": true,
23
+ "initializer_range": 0.02,
24
+ "layer_norm_epsilon": 1e-12,
25
+ "mlp_fc1_bias": false,
26
+ "mlp_fc2_bias": false,
27
+ "model_type": "nomic_bert",
28
+ "n_embd": 768,
29
+ "n_head": 12,
30
+ "n_inner": 3072,
31
+ "n_layer": 12,
32
+ "n_positions": 2048,
33
+ "pad_vocab_size_multiple": 64,
34
+ "parallel_block": false,
35
+ "parallel_block_tied_norm": false,
36
+ "prenorm": false,
37
+ "qkv_proj_bias": false,
38
+ "reorder_and_upcast_attn": false,
39
+ "resid_pdrop": 0.1,
40
+ "rotary_emb_base": 1000,
41
+ "rotary_emb_fraction": 1.0,
42
+ "rotary_emb_interleaved": false,
43
+ "rotary_emb_scale_base": null,
44
+ "scale_attn_by_inverse_layer_idx": false,
45
+ "scale_attn_weights": true,
46
+ "summary_activation": null,
47
+ "summary_first_dropout": 0.1,
48
+ "summary_proj_to_labels": true,
49
+ "summary_type": "cls_index",
50
+ "summary_use_proj": true,
51
+ "torch_dtype": "float32",
52
+ "transformers_version": "4.34.0",
53
+ "type_vocab_size": 2,
54
+ "use_cache": true,
55
+ "use_flash_attn": true,
56
+ "use_rms_norm": false,
57
+ "use_xentropy": true,
58
+ "vocab_size": 30528
59
+ }
configuration_hf_nomic_bert.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import GPT2Config
2
+
3
+
4
+ class NomicBertConfig(GPT2Config):
5
+ model_type = "nomic_bert"
6
+
7
+ def __init__(
8
+ self,
9
+ prenorm=False,
10
+ parallel_block=False,
11
+ parallel_block_tied_norm=False,
12
+ rotary_emb_fraction=0.0,
13
+ fused_dropout_add_ln=False,
14
+ fused_bias_fc=False,
15
+ use_flash_attn=False,
16
+ use_xentropy=False,
17
+ qkv_proj_bias=True,
18
+ rotary_emb_base=10_000,
19
+ rotary_emb_scale_base=None,
20
+ rotary_emb_interleaved=False,
21
+ mlp_fc1_bias=True,
22
+ mlp_fc2_bias=True,
23
+ use_rms_norm=False,
24
+ causal=False,
25
+ type_vocab_size=2,
26
+ dense_seq_output=True,
27
+ pad_vocab_size_multiple=1,
28
+ tie_word_embeddings=True,
29
+ rotary_scaling_factor=None,
30
+ max_trained_positions=2048,
31
+ **kwargs,
32
+ ):
33
+ self.prenorm = prenorm
34
+ self.parallel_block = parallel_block
35
+ self.parallel_block_tied_norm = parallel_block_tied_norm
36
+ self.rotary_emb_fraction = rotary_emb_fraction
37
+ self.tie_word_embeddings = tie_word_embeddings
38
+ self.fused_dropout_add_ln = fused_dropout_add_ln
39
+ self.fused_bias_fc = fused_bias_fc
40
+ self.use_flash_attn = use_flash_attn
41
+ self.use_xentropy = use_xentropy
42
+ self.qkv_proj_bias = qkv_proj_bias
43
+ self.rotary_emb_base = rotary_emb_base
44
+ self.rotary_emb_scale_base = rotary_emb_scale_base
45
+ self.rotary_emb_interleaved = rotary_emb_interleaved
46
+ self.mlp_fc1_bias = mlp_fc1_bias
47
+ self.mlp_fc2_bias = mlp_fc2_bias
48
+ self.use_rms_norm = use_rms_norm
49
+ self.causal = causal
50
+ self.type_vocab_size = type_vocab_size
51
+ self.dense_seq_output = dense_seq_output
52
+ self.pad_vocab_size_multiple = pad_vocab_size_multiple
53
+ self.rotary_scaling_factor = rotary_scaling_factor
54
+ self.max_trained_positions = max_trained_positions
55
+
56
+ super().__init__(**kwargs)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9acacdb7c24a8eac0fc7c11b00c4a2e7986a49aacbd40c9b30ae12b2763ead19
3
+ size 549304584
modeling_hf_nomic_bert.py ADDED
The diff for this file is too large to render. See raw diff
 
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e0b1c7db843aeae0c744716acf3c7b7da60d142e7ca31edd11853f5b163c8776
3
+ size 549328982
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": true,
45
+ "cls_token": "[CLS]",
46
+ "do_lower_case": true,
47
+ "mask_token": "[MASK]",
48
+ "model_max_length": 8192,
49
+ "pad_token": "[PAD]",
50
+ "sep_token": "[SEP]",
51
+ "strip_accents": null,
52
+ "tokenize_chinese_chars": true,
53
+ "tokenizer_class": "BertTokenizer",
54
+ "unk_token": "[UNK]"
55
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff