Views
No views yet
export_onnx_int8.py and re-run the script.export_onnx_int8.py with appropriate optimization argument.pip install onnxruntimepip install transformers1import time
2
3from optimum.onnxruntime import ORTModelForCustomTasks
4from transformers import AutoTokenizer
5
6tokenizer = AutoTokenizer.from_pretrained("BAAI/bge-m3")
7model = ORTModelForCustomTasks.from_pretrained("gpahal/bge-m3-onnx-int8")
8
9questions = ["What is your opening hour?", "Where are your offices?"]
10input_q = tokenizer(
11 questions,
12 padding=True,
13 truncation=True,
14 return_tensors="np"
15)
16print(f"Question input keys: {list(input_q.keys())}, shapes: {[v.shape for v in input_q.values()]}")
17
18t0 = time.perf_counter()
19output_q = model(**input_q)
20print(f"Time taken: {(time.perf_counter()-t0)*1e3:.1f} ms")1from collections import defaultdict
2
3
4def process_token_weights(token_weights: np.ndarray, input_ids: list):
5 # conver to dict
6 result = defaultdict(int)
7 unused_tokens = {
8 tokenizer.cls_token_id,
9 tokenizer.eos_token_id,
10 tokenizer.pad_token_id,
11 tokenizer.unk_token_id,
12 }
13 for w, idx in zip(token_weights, input_ids):
14 if idx not in unused_tokens and w > 0:
15 idx = str(idx)
16 if w > result[idx]:
17 result[idx] = w
18 return result
19
20
21token_weights = outputs[1].squeeze(-1)
22lexical_weights = list(
23 map(process_token_weights, token_weights, inputs["input_ids"].tolist())
24)export_onnx_int8.py ONNX weight export script which leverages HF Optimum.
If needed, you can modify the model configuration to for example remove embedding normalization or to not output all three embedding representations. If you modify the number of output representations, you need to also modify the ONNX output config BGEM3OnnxConfig in export_onnx_int8.py.pip install -r requirements.txtpython export_onnx.py --opset 17 --device cpu --optimize O2