langminer commited on
Commit
ef48f4c
·
verified ·
1 Parent(s): 48a65a1

Upload PhraseBERT ONNX model

Browse files
Files changed (7) hide show
  1. README.md +85 -0
  2. config.json +26 -0
  3. model.onnx +3 -0
  4. special_tokens_map.json +37 -0
  5. tokenizer.json +0 -0
  6. tokenizer_config.json +66 -0
  7. vocab.txt +0 -0
README.md ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ license: apache-2.0
4
+ library_name: onnxruntime
5
+ tags:
6
+ - onnx
7
+ - phrase-embeddings
8
+ - sentence-similarity
9
+ - feature-extraction
10
+ base_model: whaleloops/phrase-bert
11
+ pipeline_tag: feature-extraction
12
+ ---
13
+
14
+ # PhraseBERT ONNX
15
+
16
+ ONNX export of [whaleloops/phrase-bert](https://huggingface.co/whaleloops/phrase-bert) for lightweight inference using ONNX Runtime — **no PyTorch or Transformers required**.
17
+
18
+ ## Model Details
19
+
20
+ - **Base model**: whaleloops/phrase-bert (BERT-base, 12 layers, 768 hidden dim)
21
+ - **Pooling**: Mean pooling (attention-mask weighted)
22
+ - **Format**: ONNX
23
+ - **Size**: ~416 MB
24
+
25
+ ## Usage
26
+
27
+ ### Install dependencies (no torch/transformers needed)
28
+
29
+ ```bash
30
+ pip install onnxruntime tokenizers numpy
31
+ ```
32
+
33
+ ### Download and run
34
+
35
+ ```python
36
+ from huggingface_hub import snapshot_download
37
+
38
+ # Download the model
39
+ model_dir = snapshot_download("langminer/phrase-bert-onnx")
40
+ ```
41
+
42
+ ```python
43
+ import numpy as np
44
+ import onnxruntime as ort
45
+ from tokenizers import Tokenizer
46
+
47
+ # Load model and tokenizer
48
+ session = ort.InferenceSession(f"{model_dir}/model.onnx", providers=["CPUExecutionProvider"])
49
+ tokenizer = Tokenizer.from_file(f"{model_dir}/tokenizer.json")
50
+ tokenizer.enable_padding(pad_id=0, pad_token="[PAD]")
51
+ tokenizer.enable_truncation(max_length=512)
52
+
53
+ # Encode phrases
54
+ phrases = ["play an active role", "participate actively", "machine learning"]
55
+ encodings = tokenizer.encode_batch(phrases)
56
+
57
+ input_ids = np.array([e.ids for e in encodings], dtype=np.int64)
58
+ attention_mask = np.array([e.attention_mask for e in encodings], dtype=np.int64)
59
+ token_type_ids = np.array([e.type_ids for e in encodings], dtype=np.int64)
60
+
61
+ # Run inference
62
+ outputs = session.run(None, {
63
+ "input_ids": input_ids,
64
+ "attention_mask": attention_mask,
65
+ "token_type_ids": token_type_ids,
66
+ })
67
+ token_embeddings = outputs[0] # (batch, seq_len, 768)
68
+
69
+ # Mean pooling
70
+ mask = attention_mask[:, :, np.newaxis].astype(np.float32)
71
+ embeddings = np.sum(token_embeddings * mask, axis=1) / np.sum(mask, axis=1)
72
+
73
+ print(embeddings.shape) # (3, 768)
74
+ ```
75
+
76
+ ## Citation
77
+
78
+ ```bibtex
79
+ @inproceedings{wang2021phrase,
80
+ title={Phrase-BERT: Improved Phrase Embeddings from BERT with an Application to Corpus Exploration},
81
+ author={Wang, Shufan and Thompson, Laure and Iyyer, Mohit},
82
+ booktitle={EMNLP},
83
+ year={2021}
84
+ }
85
+ ```
config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertModel"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "classifier_dropout": null,
7
+ "dtype": "float32",
8
+ "export_model_type": "transformer",
9
+ "gradient_checkpointing": false,
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 768,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 3072,
15
+ "layer_norm_eps": 1e-12,
16
+ "max_position_embeddings": 512,
17
+ "model_type": "bert",
18
+ "num_attention_heads": 12,
19
+ "num_hidden_layers": 12,
20
+ "pad_token_id": 0,
21
+ "position_embedding_type": "absolute",
22
+ "transformers_version": "4.57.6",
23
+ "type_vocab_size": 2,
24
+ "use_cache": true,
25
+ "vocab_size": 30522
26
+ }
model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4635d62a5fd44f49076b287449aa8ad0a9af6b66306e9ce7edf6098ba2c95f9b
3
+ size 435812071
special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "mask_token": {
10
+ "content": "[MASK]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "[PAD]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "sep_token": {
24
+ "content": "[SEP]",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "[UNK]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": false,
45
+ "cls_token": "[CLS]",
46
+ "do_basic_tokenize": true,
47
+ "do_lower_case": true,
48
+ "extra_special_tokens": {},
49
+ "full_tokenizer_file": null,
50
+ "mask_token": "[MASK]",
51
+ "max_length": 128,
52
+ "model_max_length": 1000000000000000019884624838656,
53
+ "never_split": null,
54
+ "pad_to_multiple_of": null,
55
+ "pad_token": "[PAD]",
56
+ "pad_token_type_id": 0,
57
+ "padding_side": "right",
58
+ "sep_token": "[SEP]",
59
+ "stride": 0,
60
+ "strip_accents": null,
61
+ "tokenize_chinese_chars": true,
62
+ "tokenizer_class": "BertTokenizer",
63
+ "truncation_side": "right",
64
+ "truncation_strategy": "longest_first",
65
+ "unk_token": "[UNK]"
66
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff