forked from shibing624/text2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_qps.py
176 lines (155 loc) · 7.34 KB
/
test_qps.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
"""
@author:XuMing([email protected])
@description:
"""
import os
import sys
import unittest
from loguru import logger
import time
import os
import torch
from transformers import AutoTokenizer, AutoModel
sys.path.append('..')
from text2vec import Word2Vec, SentenceModel
from sentence_transformers import SentenceTransformer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
pwd_path = os.path.abspath(os.path.dirname(__file__))
logger.add('test.log')
data = ['如何更换花呗绑定银行卡',
'花呗更改绑定银行卡']
print("data:", data)
num_tokens = sum([len(i) for i in data])
use_cuda = torch.cuda.is_available()
repeat = 10 if use_cuda else 1
class TransformersEncoder:
def __init__(self, model_name='shibing624/text2vec-base-chinese'):
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModel.from_pretrained(model_name).to(device)
def encode(self, sentences):
# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] # First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1),
min=1e-9)
# Tokenize sentences
encoded_input = self.tokenizer(sentences, padding=True, truncation=True, return_tensors='pt').to(device)
# Compute token embeddings
with torch.no_grad():
model_output = self.model(**encoded_input)
# Perform pooling. In this case, max pooling.
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
return sentence_embeddings
class SentenceTransformersEncoder:
def __init__(self, model_name="shibing624/text2vec-base-chinese"):
self.model = SentenceTransformer(model_name)
def encode(self, sentences, convert_to_numpy=True):
sentence_embeddings = self.model.encode(sentences, convert_to_numpy=convert_to_numpy)
return sentence_embeddings
class QPSEncoderTestCase(unittest.TestCase):
def test_cosent_speed(self):
"""测试cosent_speed"""
logger.info("\n---- cosent:")
model = SentenceModel('shibing624/text2vec-base-chinese')
logger.info(' convert_to_numpy=True:')
for j in range(repeat):
tmp = data * (2 ** j)
c_num_tokens = num_tokens * (2 ** j)
start_t = time.time()
r = model.encode(tmp, convert_to_numpy=True)
assert r is not None
if j == 0:
logger.info(f"result shape: {r.shape}, emb: {r[0][:10]}")
time_t = time.time() - start_t
logger.info('encoding %d sentences, spend %.2fs, %4d samples/s, %6d tokens/s' %
(len(tmp), time_t, int(len(tmp) / time_t), int(c_num_tokens / time_t)))
logger.info(' convert_to_numpy=False:')
for j in range(repeat):
tmp = data * (2 ** j)
c_num_tokens = num_tokens * (2 ** j)
start_t = time.time()
r = model.encode(tmp, convert_to_numpy=False)
assert r is not None
if j == 0:
logger.info(f"result shape: {len(r)}, emb: {r[0][:10]}")
time_t = time.time() - start_t
logger.info('encoding %d sentences, spend %.2fs, %4d samples/s, %6d tokens/s' %
(len(tmp), time_t, int(len(tmp) / time_t), int(c_num_tokens / time_t)))
def test_origin_transformers_speed(self):
"""测试origin_transformers_speed"""
logger.info("\n---- origin transformers:")
model = TransformersEncoder('shibing624/text2vec-base-chinese')
for j in range(repeat):
tmp = data * (2 ** j)
c_num_tokens = num_tokens * (2 ** j)
start_t = time.time()
r = model.encode(tmp)
assert r is not None
if j == 0:
logger.info(f"result shape: {r.shape}, emb: {r[0][:10]}")
time_t = time.time() - start_t
logger.info('encoding %d sentences, spend %.2fs, %4d samples/s, %6d tokens/s' %
(len(tmp), time_t, int(len(tmp) / time_t), int(c_num_tokens / time_t)))
def test_origin_sentence_transformers_speed(self):
"""测试origin_sentence_transformers_speed"""
logger.info("\n---- origin sentence_transformers:")
model = SentenceTransformersEncoder('shibing624/text2vec-base-chinese')
logger.info(' convert_to_numpy=True:')
for j in range(repeat):
tmp = data * (2 ** j)
c_num_tokens = num_tokens * (2 ** j)
start_t = time.time()
r = model.encode(tmp, convert_to_numpy=True)
assert r is not None
if j == 0:
logger.info(f"result shape: {r.shape}, emb: {r[0][:10]}")
time_t = time.time() - start_t
logger.info('encoding %d sentences, spend %.2fs, %4d samples/s, %6d tokens/s' %
(len(tmp), time_t, int(len(tmp) / time_t), int(c_num_tokens / time_t)))
logger.info(' convert_to_numpy=False:')
for j in range(repeat):
tmp = data * (2 ** j)
c_num_tokens = num_tokens * (2 ** j)
start_t = time.time()
r = model.encode(tmp, convert_to_numpy=False)
assert r is not None
if j == 0:
logger.info(f"result shape: {len(r)}, emb: {r[0][:10]}")
time_t = time.time() - start_t
logger.info('encoding %d sentences, spend %.2fs, %4d samples/s, %6d tokens/s' %
(len(tmp), time_t, int(len(tmp) / time_t), int(c_num_tokens / time_t)))
def test_sbert_speed(self):
"""测试sbert_speed"""
logger.info("\n---- sbert:")
model = SentenceModel('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2')
for j in range(repeat):
tmp = data * (2 ** j)
c_num_tokens = num_tokens * (2 ** j)
start_t = time.time()
r = model.encode(tmp)
assert r is not None
if j == 0:
logger.info(f"result shape: {r.shape}, emb: {r[0][:10]}")
time_t = time.time() - start_t
logger.info('encoding %d sentences, spend %.2fs, %4d samples/s, %6d tokens/s' %
(len(tmp), time_t, int(len(tmp) / time_t), int(c_num_tokens / time_t)))
def test_w2v_speed(self):
"""测试w2v_speed"""
logger.info("\n---- w2v:")
model = Word2Vec()
for j in range(repeat):
tmp = data * (2 ** j)
c_num_tokens = num_tokens * (2 ** j)
start_t = time.time()
r = model.encode(tmp)
assert r is not None
if j == 0:
logger.info(f"result shape: {r.shape}, emb: {r[0][:10]}")
time_t = time.time() - start_t
logger.info('encoding %d sentences, spend %.2fs, %4d samples/s, %6d tokens/s' %
(len(tmp), time_t, int(len(tmp) / time_t), int(c_num_tokens / time_t)))
if __name__ == '__main__':
unittest.main()