-
Notifications
You must be signed in to change notification settings - Fork 12
/
kmeans_ec.py
192 lines (157 loc) · 7.56 KB
/
kmeans_ec.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import urllib.parse
import numpy as np
import time
from data_loader import libsvm_dataset
from utils.constants import Prefix, Synchronization
from storage import S3Storage, MemcachedStorage
from communicator import MemcachedCommunicator
from model import cluster_models
from model.cluster_models import KMeans, SparseKMeans
def sparse_centroid_to_numpy(centroid_sparse_tensor, nr_cluster):
cent_lst = [centroid_sparse_tensor[i].to_dense().numpy() for i in range(nr_cluster)]
centroid = np.array(cent_lst)
return centroid
def centroid_bytes2np(centroid_bytes, n_cluster, data_type, with_error=False):
centroid_np = np.frombuffer(centroid_bytes, dtype=data_type)
if with_error:
centroid_size = centroid_np.shape[0] - 1
return centroid_np[-1], centroid_np[0:-1].reshape(n_cluster, int(centroid_size / n_cluster))
else:
centroid_size = centroid_np.shape[0]
return centroid_np.reshape(n_cluster, int(centroid_size / n_cluster))
def new_centroids_with_error(dataset, dataset_type, old_centroids, epoch, n_features, n_clusters, data_type):
compute_start = time.time()
if dataset_type == "dense_libsvm":
model = KMeans(dataset, old_centroids)
elif dataset_type == "sparse_libsvm":
model = SparseKMeans(dataset, old_centroids, n_features, n_clusters)
model.find_nearest_cluster()
new_centroids = model.get_centroids("numpy").reshape(-1)
compute_end = time.time()
print("Epoch = {}, compute new centroids time: {}, error = {}"
.format(epoch, compute_end - compute_start, model.error))
res = np.append(new_centroids, model.error).astype(data_type)
return res
def compute_average_centroids(storage, avg_cent_bucket, worker_cent_bucket, n_workers, shape, epoch, data_type):
assert isinstance(storage, S3Storage)
n_files = 0
centroids_vec_list = []
error_list = []
while n_files < n_workers:
n_files = 0
centroids_vec_list = []
error_list = []
objects = storage.list(worker_cent_bucket)
if objects is not None:
for obj in objects:
file_key = urllib.parse.unquote_plus(obj["Key"], encoding='utf-8')
cent_bytes = storage.load(file_key, worker_cent_bucket).read()
cent_with_error = np.frombuffer(cent_bytes, dtype=data_type)
cent_np = cent_with_error[0:-1].reshape(shape)
error = cent_with_error[-1]
centroids_vec_list.append(cent_np)
error_list.append(error)
n_files = n_files + 1
else:
print('No objects in {}'.format(worker_cent_bucket))
avg_cent = np.average(np.array(centroids_vec_list), axis=0).reshape(-1)
avg_error = np.mean(np.array(error_list))
storage.clear(worker_cent_bucket)
print("Average error for {}-th epoch: {}".format(epoch, avg_error))
res = np.append(avg_cent, avg_error).astype(data_type)
storage.save(res.tobytes(), f"avg-{epoch}", avg_cent_bucket)
return True
def handler(event, context):
# dataset
data_bucket = event['data_bucket']
file = event['file']
dataset_type = event["dataset_type"]
assert dataset_type == "dense_libsvm"
n_features = event['n_features']
host = event['host']
port = event['port']
tmp_bucket = event["tmp_bucket"]
merged_bucket = event["merged_bucket"]
# hyper-parameter
n_clusters = event['n_clusters']
n_epochs = event["n_epochs"]
threshold = event["threshold"]
sync_mode = event["sync_mode"]
n_workers = event["n_workers"]
worker_index = event['worker_index']
assert sync_mode.lower() in [Synchronization.Reduce, Synchronization.Reduce_Scatter]
print('data bucket = {}'.format(data_bucket))
print("file = {}".format(file))
print('number of workers = {}'.format(n_workers))
print('worker index = {}'.format(worker_index))
print('num clusters = {}'.format(n_clusters))
print('sync mode = {}'.format(sync_mode))
s3_storage = S3Storage()
mem_storage = MemcachedStorage(host, port)
communicator = MemcachedCommunicator(mem_storage, tmp_bucket, merged_bucket, n_workers, worker_index)
if worker_index == 0:
mem_storage.clear()
# Reading data from S3
read_start = time.time()
lines = s3_storage.load(file, data_bucket).read().decode('utf-8').split("\n")
print("read data cost {} s".format(time.time() - read_start))
parse_start = time.time()
dataset = libsvm_dataset.from_lines(lines, n_features, dataset_type)
if dataset_type == "dense_libsvm":
dataset = dataset.ins_np
data_type = dataset.dtype
centroid_shape = (n_clusters, dataset.shape[1])
elif dataset_type == "sparse_libsvm":
dataset = dataset.ins_list
first_entry = dataset[0].to_dense().numpy()
data_type = first_entry.dtype
centroid_shape = (n_clusters, first_entry.shape[1])
print("parse data cost {} s".format(time.time() - parse_start))
print("dataset type: {}, dtype: {}, Centroids shape: {}, num_features: {}"
.format(dataset_type, data_type, centroid_shape, n_features))
init_centroids_start = time.time()
if worker_index == 0:
if dataset_type == "dense_libsvm":
centroids = dataset[0:n_clusters]
elif dataset_type == "sparse_libsvm":
centroids = sparse_centroid_to_numpy(dataset[0:n_clusters], n_clusters)
mem_storage.save_v2(centroids.tobytes(), Prefix.KMeans_Init_Cent + "-1", merged_bucket)
print("generate initial centroids takes {} s"
.format(time.time() - init_centroids_start))
else:
centroid_bytes = mem_storage.load_or_wait_v2(Prefix.KMeans_Init_Cent + "-1", merged_bucket)
centroids = centroid_bytes2np(centroid_bytes, n_clusters, data_type)
if centroid_shape != centroids.shape:
raise Exception("The shape of centroids does not match.")
print("Waiting for initial centroids takes {} s".format(time.time() - init_centroids_start))
model = cluster_models.get_model(dataset, centroids, dataset_type, n_features, n_clusters)
train_start = time.time()
for epoch in range(n_epochs):
epoch_start = time.time()
# rearrange data points
model.find_nearest_cluster()
local_cent = model.get_centroids("numpy").reshape(-1)
local_cent_error = np.concatenate((local_cent.flatten(), np.array([model.error])))
epoch_cal_time = time.time() - epoch_start
# sync local centroids and error
epoch_sync_start = time.time()
if sync_mode == "reduce":
cent_error_merge = communicator.reduce_epoch(local_cent_error, epoch)
elif sync_mode == "reduce_scatter":
cent_error_merge = communicator.reduce_scatter_epoch(local_cent_error, epoch)
cent_merge = cent_error_merge[:-1].reshape(centroid_shape) / float(n_workers)
error_merge = cent_error_merge[-1] / float(n_workers)
model.centroids = cent_merge
model.error = error_merge
print("one {} round cost {} s".format(sync_mode, time.time() - epoch_sync_start))
epoch_sync_time = time.time() - epoch_sync_start
print("Epoch[{}] Worker[{}], error = {}, cost {} s, cal cost {} s, sync cost {} s"
.format(epoch, worker_index, model.error,
time.time() - epoch_start, epoch_cal_time, epoch_sync_time))
if model.error < threshold:
break
#if worker_index == 0:
# mem_storage.clear()
print("Worker[{}] finishes training: Error = {}, cost {} s"
.format(worker_index, model.error, time.time() - train_start))
return