forked from milvus-io/bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.py
163 lines (142 loc) · 6.32 KB
/
load.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
import os
import numpy as np
import time
from sklearn.preprocessing import normalize
from logs import LOGGER
from config import FILE_TYPE, BASE_FILE_PATH, IS_UINT8, IF_NORMALIZE, TOTAL_VECTOR_COUNT, IMPORT_CHUNK_SIZE, \
METRIC_TYPE, NLIST, PQ_M, N_TREE, EFCONSTRUCTION, HNSW_M
import pandas as pd
def load_csv_data(filename):
# filename = BASE_FILE_PATH + "/" + filename
data = pd.read_csv(filename, header=None)
data = np.array(data)
if IS_UINT8:
data = (data + 0.5) / 255
if IF_NORMALIZE:
data = normalize(data)
data = data.tolist()
return data
def csv_to_milvus(collection_name, client):
filenames = os.listdir(BASE_FILE_PATH)
filenames.sort()
total_insert_time = 0
collection_rows = client.count(collection_name)
for filename in filenames:
fname = os.path.join(BASE_FILE_PATH, filename)
vectors = load_csv_data(fname)
vectors_ids = list(id for id in range(collection_rows, collection_rows + len(vectors)))
time_add_start = time.time()
ids = client.insert(collection_name, vectors, vectors_ids)
total_insert_time = total_insert_time + time.time() - time_add_start
collection_rows = collection_rows + len(ids)
print(filename, " insert time: ", time.time() - time_add_start)
client.count(collection_name)
print("total insert time: ", total_insert_time)
def load_fvecs_data(base_len, idx, fname):
begin_num = base_len * idx
x = np.memmap(fname, dtype='uint8', mode='r')
d = x[:4].view('int32')[0]
data = x.view('float32').reshape(-1, d + 1)[begin_num:(begin_num + base_len), 1:]
if IF_NORMALIZE:
data = normalize(data)
data = data.tolist()
return data
def fvecs_to_milvus(collection_name, client):
fname = BASE_FILE_PATH
count = 0
total_insert_time = 0
while count < (TOTAL_VECTOR_COUNT // IMPORT_CHUNK_SIZE):
vectors = load_fvecs_data(IMPORT_CHUNK_SIZE, count, fname)
vectors_ids = list(id for id in range(count * IMPORT_CHUNK_SIZE, (count + 1) * IMPORT_CHUNK_SIZE))
# vectors_ids = [id for id in range(count * IMPORT_CHUNK_SIZE, (count + 1) * IMPORT_CHUNK_SIZE)]
time_add_start = time.time()
_ = client.insert(collection_name, vectors, vectors_ids)
total_insert_time = total_insert_time + time.time() - time_add_start
print(count * IMPORT_CHUNK_SIZE, (count + 1) * IMPORT_CHUNK_SIZE, 'time:',
time.time() - time_add_start)
count = count + 1
client.count(collection_name)
print("total insert time: ", total_insert_time)
def load_npy_data(filename):
data = np.load(filename)
if IS_UINT8:
data = (data + 0.5) / 255
if IF_NORMALIZE:
data = normalize(data)
data = data.tolist()
return data
def npy_to_milvus(collection_name, client):
filenames = os.listdir(BASE_FILE_PATH)
filenames.sort()
total_insert_time = 0
collection_rows = client.count(collection_name)
for filename in filenames:
vectors = load_npy_data(os.path.join(BASE_FILE_PATH, filename))
vectors_ids =list(id for id in range(collection_rows, collection_rows + len(vectors)))
#vectors_ids = [id for id in range(collection_rows, collection_rows + len(vectors))]
time_add_start = time.time()
ids = client.insert(collection_name, vectors, vectors_ids)
total_insert_time = total_insert_time + time.time() - time_add_start
print(filename, "insert rows", len(ids), " insert milvus time: ", time.time() - time_add_start)
collection_rows = collection_rows + len(ids)
client.count(collection_name)
print("total insert time: ", total_insert_time)
def load_bvecs_data(base_len, idx, fname):
begin_num = base_len * idx
x = np.memmap(fname, dtype='uint8', mode='r')
d = x[:4].view('int32')[0]
data = x.reshape(-1, d + 4)[begin_num:(begin_num + base_len), 4:]
data = (data + 0.5) / 255
if IF_NORMALIZE:
data = normalize(data)
data = data.tolist()
return data
def bvecs_to_milvus(collection_name, client):
fname = BASE_FILE_PATH
count = 0
total_insert_time = 0
collection_rows = client.count(collection_name)
while count < (TOTAL_VECTOR_COUNT // IMPORT_CHUNK_SIZE):
vectors = load_bvecs_data(IMPORT_CHUNK_SIZE, count, fname)
vectors_ids = list(id for id in range(collection_rows, collection_rows + len(vectors)))
# vectors_ids = [id for id in range(collection_rows, collection_rows + len(vectors))]
time_add_start = time.time()
ids = client.insert(collection_name, vectors, vectors_ids)
print(count * IMPORT_CHUNK_SIZE, (count + 1) * IMPORT_CHUNK_SIZE, 'time:',
time.time() - time_add_start)
total_insert_time = total_insert_time + time.time() - time_add_start
count = count + 1
collection_rows = collection_rows + len(ids)
client.count(collection_name)
print(f"total insert time: {total_insert_time}")
def insert_data(client, collection_name):
if FILE_TYPE[0] == 'npy':
npy_to_milvus(collection_name, client)
if FILE_TYPE[0] == 'csv':
csv_to_milvus(collection_name, client)
if FILE_TYPE[0] == 'bvecs':
bvecs_to_milvus(collection_name, client)
if FILE_TYPE[0] == 'fvecs':
fvecs_to_milvus(collection_name, client)
def get_index_params(index_type):
if index_type == 'FLAT':
index_param = {"index_type": index_type}
elif index_type == 'HNSW':
params = {"M": HNSW_M, "efConstruction": EFCONSTRUCTION}
index_param = {"index_type": index_type, "metric_type": METRIC_TYPE, "params": params}
elif index_type == 'ANNOY':
params = {"n_trees": N_TREE}
index_param = {"index_type": index_type, "metric_type": METRIC_TYPE, "params": params}
elif index_type == 'IVF_PQ':
params = {"nlist": NLIST, "m": PQ_M}
index_param = {"index_type": index_type, "metric_type": METRIC_TYPE, "params": params}
else:
params = {"nlist": NLIST}
index_param = {"index_type": index_type, "metric_type": METRIC_TYPE, "params": params}
LOGGER.info(index_param)
return index_param
def create_index(client, collection_name, index_type):
index_param = get_index_params(index_type)
time1 = time.time()
client.create_index(collection_name, index_param)
LOGGER.info(f"create index total cost time: {time.time() - time1}")