-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgte_onnx_probe.py
More file actions
154 lines (129 loc) · 5.65 KB
/
gte_onnx_probe.py
File metadata and controls
154 lines (129 loc) · 5.65 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from __future__ import annotations
import argparse
import json
import time
from pathlib import Path
import numpy as np
import torch
from transformers import AutoModel, AutoTokenizer
from keyword_bench.output_paths import resolve_output_path
try:
import onnx # noqa: F401
except ModuleNotFoundError as exc:
raise SystemExit(
"Missing optional dependency 'onnx'. Install with: "
'pip install "keyatten[inference,zh,lightweight]"'
) from exc
try:
import onnxruntime as ort
except ModuleNotFoundError as exc:
raise SystemExit(
"Missing optional dependency 'onnxruntime'. Install with: "
'pip install "keyatten[inference,zh,lightweight]"'
) from exc
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Validate gte-small-zh attention export with ONNX Runtime.")
parser.add_argument("--model-path", required=True)
parser.add_argument("--output-path", required=True)
parser.add_argument("--words", nargs="+", required=True)
parser.add_argument("--layer-index", type=int, default=-1)
parser.add_argument("--opset", type=int, default=17)
return parser.parse_args()
class AttentionMeanExportWrapper(torch.nn.Module):
def __init__(self, model: torch.nn.Module, layer_index: int) -> None:
super().__init__()
self.model = model
self.layer_index = layer_index
def forward(self, input_ids: torch.Tensor, attention_mask: torch.Tensor) -> torch.Tensor:
outputs = self.model(
input_ids=input_ids,
attention_mask=attention_mask,
output_attentions=True,
return_dict=True,
)
return outputs.attentions[self.layer_index].float().mean(dim=1)
def aggregate_subwords_to_words(word_ids: list[int | None], token_scores: np.ndarray, word_count: int) -> np.ndarray:
sums = np.zeros(word_count, dtype=np.float32)
counts = np.zeros(word_count, dtype=np.float32)
for token_index, word_id in enumerate(word_ids):
if word_id is None or word_id < 0 or word_id >= word_count:
continue
sums[word_id] += float(token_scores[token_index])
counts[word_id] += 1.0
counts[counts == 0.0] = 1.0
return sums / counts
def word_received_scores(attention_map: np.ndarray, word_ids: list[int | None], words: list[str]) -> np.ndarray:
received_scores = attention_map.sum(axis=0)
return aggregate_subwords_to_words(word_ids, received_scores, len(words))
def main() -> None:
args = parse_args()
output_path = resolve_output_path(args.output_path)
tokenizer = AutoTokenizer.from_pretrained(args.model_path, use_fast=True)
try:
model = AutoModel.from_pretrained(args.model_path, output_attentions=True, attn_implementation="eager")
except TypeError:
model = AutoModel.from_pretrained(args.model_path, output_attentions=True)
model.eval()
encoded = tokenizer(
[args.words],
is_split_into_words=True,
padding=False,
truncation=True,
max_length=512,
return_tensors="pt",
)
word_ids = encoded.word_ids(batch_index=0)
valid_token_count = int(encoded["attention_mask"][0].sum().item())
valid_word_ids = word_ids[:valid_token_count]
wrapper = AttentionMeanExportWrapper(model, layer_index=args.layer_index)
with torch.no_grad():
torch_attention = wrapper(encoded["input_ids"], encoded["attention_mask"])[0, :valid_token_count, :valid_token_count]
torch_attention_np = torch_attention.detach().cpu().numpy().astype(np.float32, copy=False)
export_start = time.perf_counter()
torch.onnx.export(
wrapper,
(encoded["input_ids"], encoded["attention_mask"]),
str(output_path),
input_names=["input_ids", "attention_mask"],
output_names=["attention_mean"],
dynamic_axes={
"input_ids": {0: "batch", 1: "sequence"},
"attention_mask": {0: "batch", 1: "sequence"},
"attention_mean": {0: "batch", 1: "sequence_out", 2: "sequence_in"},
},
opset_version=args.opset,
dynamo=False,
)
export_seconds = time.perf_counter() - export_start
session = ort.InferenceSession(str(output_path), providers=["CPUExecutionProvider"])
ort_start = time.perf_counter()
ort_outputs = session.run(
["attention_mean"],
{
"input_ids": encoded["input_ids"].cpu().numpy(),
"attention_mask": encoded["attention_mask"].cpu().numpy(),
},
)
ort_seconds = time.perf_counter() - ort_start
ort_attention = ort_outputs[0][0, :valid_token_count, :valid_token_count].astype(np.float32, copy=False)
diff = np.abs(torch_attention_np - ort_attention)
torch_received = word_received_scores(torch_attention_np, valid_word_ids, args.words)
ort_received = word_received_scores(ort_attention, valid_word_ids, args.words)
received_diff = np.abs(torch_received - ort_received)
report = {
"model_path": args.model_path,
"output_path": str(output_path),
"word_count": len(args.words),
"valid_token_count": valid_token_count,
"attention_shape": list(torch_attention_np.shape),
"onnx_bytes": output_path.stat().st_size,
"export_seconds": export_seconds,
"ort_seconds": ort_seconds,
"attention_max_abs_diff": float(diff.max(initial=0.0)),
"attention_mean_abs_diff": float(diff.mean() if diff.size else 0.0),
"received_max_abs_diff": float(received_diff.max(initial=0.0)),
"received_mean_abs_diff": float(received_diff.mean() if received_diff.size else 0.0),
}
print(json.dumps(report, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()