-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathweight_virtualization.py
More file actions
1301 lines (1045 loc) · 42 KB
/
Copy pathweight_virtualization.py
File metadata and controls
1301 lines (1045 loc) · 42 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
import os
import copy
import pickle
import struct
import sys
import argparse
import importlib
import matplotlib.pyplot as plt
import time
tf.logging.set_verbosity(tf.logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#np.set_printoptions(threshold=sys.maxsize)
class VNN:
def __init__(self, network_path, id, state=-1, weight_page_list=None):
self.id = id
self.name = os.path.basename(os.path.normpath(network_path))
self.state = state
self.weight_page_list = weight_page_list
self.network_path = network_path
assert os.path.exists(self.network_path), 'No network path %s exists' % self.network_path
self.meta_filename = self.name + '.meta'
self.meta_filepath = os.path.join(self.network_path, self.meta_filename)
assert os.path.exists(self.meta_filepath), 'No filepath %s exists' % self.meta_filepath
self.num_of_weight = self.get_weight_num()
self.model_filepath = os.path.join(self.network_path, self.name)
self.network_weight_filename = self.name + '_network_weight.npy'
self.network_weight_filepath = os.path.join(self.network_path, self.network_weight_filename)
self.network_fisher_filename = self.name + '_network_fisher.npy'
self.network_fisher_filepath = os.path.join(self.network_path, self.network_fisher_filename)
self.weight_filename = self.name + '_weight.npy'
self.weight_filepath = os.path.join(self.network_path, self.weight_filename)
self.fisher_filename = self.name + '_fisher.npy'
self.fisher_filepath = os.path.join(self.network_path, self.fisher_filename)
self.pintle_filename = 'pintle.py'
self.pintle_filepath = os.path.join(self.network_path, self.pintle_filename)
self.filepath = self.name + '.vnn'
def get_weight_num(self):
meta_graph_def = tf.MetaGraphDef()
with open(self.meta_filepath, 'rb') as f:
meta_graph_def.MergeFromString(f.read())
num_of_weight = 0
with tf.Graph().as_default() as graph:
tf.train.import_meta_graph(meta_graph_def)
trainable_variables = tf.trainable_variables()
for trainable_variable in trainable_variables:
num_of_weight += np.prod(trainable_variable.get_shape().as_list())
return num_of_weight
class WeightVirtualization:
__instance = None
@staticmethod
def getInstance():
if WeightVirtualization.__instance == None:
WeightVirtualization()
return WeightVirtualization.__instance
def __init__(self, num_of_weight_page=665, weight_per_page=100,
weight_page_filename='virtual_weight_page.npy',
weight_page_occupation_filename='weight_page_occupation.npy',
weight_virtualization_op_filename='./tf_operation.so'):
if WeightVirtualization.__instance != None:
raise Exception("this class is a singleton")
else:
WeightVirtualization.__instance = self
self.num_of_weight_page = num_of_weight_page
self.weight_per_page = weight_per_page
self.weight_page = None
self.weight_page_filename = weight_page_filename
self.weight_page_occupation_filename = weight_page_occupation_filename
self.weight_virtualization_op_filename = weight_virtualization_op_filename
if self.load_weight_page() is False:
print('init new weight pages')
self.init_weight_page()
self.save_weight_page()
self.next_vnn_id = 0
self.vnns = {}
self.load_vnns()
def create_vnn(self, network_path):
# create a vnn
vnn = VNN(network_path, self.next_vnn_id)
if vnn.name in self.vnns:
raise Exception('vnn named %s is already there' % vnn.name)
# save the network weights
self.save_network_weight(vnn)
# get and save fisher of network
fisher_information = self.compute_fisher(vnn, 'network')
self.save_network_fisher(vnn, fisher_information)
return vnn
def add_vnn(self, network_path):
print('add_vnn')
# create a vnn
vnn = self.create_vnn(network_path)
# allocate weight pages
self.match_weight_page(vnn)
# increment next_vnn_id and add vnn to the dictionary
self.vnns[vnn.name] = vnn
self.next_vnn_id += 1
# save vnn
self.save_vnn(vnn)
total_vnn_list = []
for name, vnn in self.vnns.items():
total_vnn_list.append(vnn)
total_network_cost = self.calculate_network_cost(total_vnn_list)
print('total_network_cost:', total_network_cost)
def add_multi_vnns(self, network_path_list):
print('add_multi_vnns')
new_vnn_list = []
for network_path in network_path_list:
vnn = self.create_vnn(network_path)
new_vnn_list.append(vnn)
self.next_vnn_id += 1
self.match_weight_page_multi(new_vnn_list)
for vnn in new_vnn_list:
self.vnns[vnn.name] = vnn
self.save_vnn(vnn)
total_vnn_list = []
for name, vnn in self.vnns.items():
total_vnn_list.append(vnn)
total_network_cost = self.calculate_network_cost(total_vnn_list)
print('total_network_cost:', total_network_cost)
def remove_vnn(self, vnn):
self.dematch_weight_page(vnn)
if vnn.name in self.vnns:
del self.vnns[vnn.name]
if os.path.exists(vnn.network_weight_filepath):
os.remove(vnn.network_weight_filepath)
if os.path.exists(vnn.network_fisher_filepath):
os.remove(vnn.network_fisher_filepath)
if os.path.exists(vnn.weight_filepath):
os.remove(vnn.weight_filepath)
if os.path.exists(vnn.fisher_filepath):
os.remove(vnn.fisher_filepath)
if os.path.exists(vnn.filepath):
os.remove(vnn.filepath)
def train_vnn(self, vnn, iteration):
with tf.Graph().as_default() as graph:
with tf.Session(graph=graph) as sess:
self.restore_vnn(vnn, graph, sess)
matching_loss = self.get_matching_loss(vnn, sess, lamb=10.0)
pintle = self.import_pintle(vnn)
weight_vector = pintle.pintle.v_train(graph, sess, matching_loss,
100, iteration, self.get_weight_from_vnn)
#weight_vector = self.get_weight_from_vnn(sess)
if weight_vector is not None:
self.apply_weight_to_page(vnn, weight_vector)
self.save_weight_page()
def execute_vnn(self, vnn, input_variables, ground_truth=None):
pintle = self.import_pintle(vnn)
input_variable_names = pintle.pintle.v_input_variable_names()
input_tensors = []
with tf.Graph().as_default() as graph:
with tf.Session(graph=graph) as sess:
self.restore_vnn(vnn, graph, sess)
for variable_name in input_variable_names:
input_tensor_name = variable_name + ':0'
input_tensors.append(graph.get_tensor_by_name(input_tensor_name))
return pintle.pintle.v_execute(graph, sess,
input_tensors, input_variables, ground_truth)
def load_vnns(self):
for file in sorted(os.listdir("./")):
if file.endswith(".vnn"):
vnn = self.load_vnn(file)
self.vnns[vnn.name] = vnn
if vnn.id >= self.next_vnn_id:
self.next_vnn_id = vnn.id + 1
def load_vnn(self, filepath):
with open(filepath, 'rb') as f:
vnn = pickle.load(f)
return vnn
def save_vnn(self, vnn):
with open(vnn.filepath, 'wb') as f:
pickle.dump(vnn, f)
def load_weight(self, vnn):
weight = np.load(vnn.weight_filepath, allow_pickle=True)
return weight
def save_weight(self, vnn):
with tf.Graph().as_default() as graph:
with tf.Session(graph=graph) as sess:
self.restore_vnn(vnn, graph, sess)
tensor_weights = tf.trainable_variables()
weights = sess.run(tensor_weights)
np.save(vnn.weight_filepath, weights)
print(vnn.weight_filepath)
def load_network_weight(self, vnn):
network_weight = np.load(vnn.network_weight_filepath, allow_pickle=True)
return network_weight
def save_network_weight(self, vnn):
with tf.Graph().as_default() as graph:
with tf.Session(graph=graph) as sess:
self.restore_network(vnn, sess)
tensor_weights = tf.trainable_variables()
network_weights = sess.run(tensor_weights)
np.save(vnn.network_weight_filepath, network_weights)
print(vnn.network_weight_filepath)
def load_network_fisher(self, vnn):
fisher_information = np.load(vnn.network_fisher_filepath, allow_pickle=True)
return fisher_information
def save_network_fisher(self, vnn, fisher_information):
np.save(vnn.network_fisher_filepath, fisher_information)
print(vnn.network_fisher_filepath)
def load_vnn_fisher(self, vnn):
if os.path.exists(vnn.fisher_filepath):
fisher_information = np.load(vnn.fisher_filepath, allow_pickle=True)
return fisher_information
else:
return None
def save_vnn_fisher(self, vnn, fisher_information):
np.save(vnn.fisher_filepath, fisher_information)
print(vnn.fisher_filepath)
def load_weight_to_vnn(self, vnn, graph, sess, weight_vector):
assign_tensor_weight = []
tensor_weights = tf.trainable_variables()
start_idx = 0
end_idx = 0
for weight in tensor_weights:
end_idx = start_idx + np.prod(weight.get_shape().as_list())
assign_weight = tf.assign(weight, weight_vector[start_idx:end_idx].reshape(weight.shape))
assign_tensor_weight.append(assign_weight)
start_idx = end_idx
sess.run(assign_tensor_weight)
def get_weight_from_vnn(self, sess):
tensor_weights = tf.trainable_variables()
weights = sess.run(tensor_weights)
weight_vector_list = []
for weight in weights:
weight_vector_list.append(weight.reshape((weight.size)))
weight_vector = np.concatenate(weight_vector_list)
return weight_vector
def get_weight_from_page(self, vnn):
weight_vector_list = []
for page in vnn.weight_page_list:
weight_vector_list.append(self.weight_page[page])
weight_vector = np.concatenate(weight_vector_list)
return weight_vector[0:vnn.num_of_weight]
def apply_weight_to_page(self, vnn, weight_vector):
start_idx = 0
end_idx = 0
for page in vnn.weight_page_list:
end_idx = start_idx + self.weight_page[page].size
if end_idx <= len(weight_vector):
self.weight_page[page] = copy.deepcopy(weight_vector[start_idx:end_idx])
start_idx = end_idx
else:
end_idx = len(weight_vector)
for i in range(end_idx-start_idx):
self.weight_page[page][i] = copy.deepcopy(weight_vector[start_idx+i])
def init_weight_page(self, num_of_weight_page=None, weight_per_page=None):
if num_of_weight_page is not None:
self.num_of_weight_page = num_of_weight_page
if weight_per_page is not None:
self.weight_per_page = weight_per_page
self.weight_page = np.random.normal(scale=0.01,
size=(self.num_of_weight_page, self.weight_per_page)).astype(np.float32)
def load_weight_page(self, weight_page_filename=None):
if weight_page_filename is not None:
self.weight_page_filename = weight_page_filename
if os.path.exists(self.weight_page_filename):
self.weight_page = np.load(self.weight_page_filename, allow_pickle=True)
self.num_of_weight_page = len(self.weight_page)
self.weight_per_page = len(self.weight_page[0])
return True
else:
return False
def save_weight_page(self, weight_page_filename=None):
if weight_page_filename is not None:
self.weight_page_filename = weight_page_filename
np.save(self.weight_page_filename, self.weight_page)
def update_weight_page_occupation(self, vnn):
weight_page_occupation = self.load_weight_page_occupation()
for i in range(len(vnn.weight_page_list)):
page_no = vnn.weight_page_list[i]
weight_page_occupation[page_no].append([vnn.id, i])
self.save_weight_page_occupation(weight_page_occupation)
def load_weight_page_occupation(self):
if os.path.exists(self.weight_page_occupation_filename):
return np.load(self.weight_page_occupation_filename, allow_pickle=True)
else:
weight_page_occupation = [[] for _ in np.arange(self.num_of_weight_page, dtype=np.int32)]
return weight_page_occupation
def save_weight_page_occupation(self, weight_page_occupation):
np.save(self.weight_page_occupation_filename, weight_page_occupation)
def import_pintle(self, vnn):
pintle_name = os.path.splitext(vnn.pintle_filepath)[0]
pintle_import_name = pintle_name.replace('/', '.')
pintle = __import__(pintle_import_name)
return pintle
def restore_network(self, vnn, sess):
saver = tf.train.import_meta_graph(vnn.meta_filepath)
saver.restore(sess, vnn.model_filepath)
def restore_vnn(self, vnn, graph, sess):
tf.train.import_meta_graph(vnn.meta_filepath)
weight_vector = self.get_weight_from_page(vnn)
self.load_weight_to_vnn(vnn, graph, sess, weight_vector)
def do_compute_fisher(self, sess, fx_tensors, x_tensors, input_tensors,\
input_variables, num_samples=100):
print("do_compute_fisher")
# input_variable[0] is data
assert input_variables[0].shape[0] >= num_samples
fisher_information = []
for v in range(len(x_tensors)):
fisher_information.append(np.zeros(x_tensors[v].get_shape().as_list()).astype(np.float32))
for i in range(num_samples):
data_idx = np.random.randint(input_variables[0].shape[0])
sampled_data = input_variables[0][data_idx:data_idx+1]
sampled_input_variables = [ sampled_data ] + input_variables[1:]
print ('sample num: %4d, data_idx: %5d' % (i, data_idx))
derivatives, prob = sess.run([tf.gradients(tf.log(fx_tensors), x_tensors), fx_tensors],
feed_dict={t: v for t,v in zip(input_tensors, sampled_input_variables)})
for v in range(len(fisher_information)):
fisher_information[v] += np.square(derivatives[v]) * prob
for v in range(len(fisher_information)):
fisher_information[v] /= num_samples
return fisher_information
def compute_fisher(self, vnn, target):
print('compute_fisher')
pintle = self.import_pintle(vnn)
input_variable_names = pintle.pintle.v_input_variable_names()
with tf.Graph().as_default() as graph:
with tf.Session(graph=graph) as sess:
if target == 'network':
self.restore_network(vnn, sess)
elif target == 'vnn':
self.restore_vnn(vnn, graph, sess)
else:
raise Exception('Neither network nor vnn')
input_tensors = []
for variable_name in input_variable_names:
input_tensor_name = variable_name + ':0'
input_tensors.append(graph.get_tensor_by_name(input_tensor_name))
weight_tensors = tf.trainable_variables()
pintle = self.import_pintle(vnn)
raw_input_variables = pintle.pintle.v_train_input_variables()
input_variables = [ raw_input_variables[0][0] ]
for i in range(1, len(raw_input_variables)):
input_variables.append(raw_input_variables[i])
target_tensors = pintle.pintle.v_fx_tensors(graph)
fisher_information = self.do_compute_fisher(sess, target_tensors, \
weight_tensors, input_tensors, input_variables, num_samples=100)
return fisher_information
def get_fisher_sum_vector(self):
fisher_dic = {}
for name_, vnn_ in self.vnns.items():
fisher = self.load_network_fisher(vnn_)
#fisher = self.load_vnn_fisher(vnn_)
fisher_vector = self.vectorize_list(fisher)
fisher_dic[vnn_.id] = fisher_vector
fisher_sum_vector = np.zeros(self.num_of_weight_page*self.weight_per_page, dtype=np.float32)
weight_page_occupation = self.load_weight_page_occupation()
for i in range(len(weight_page_occupation)):
fisher_list = []
for occupation in weight_page_occupation[i]:
size = self.weight_per_page
src_start = occupation[1]*self.weight_per_page
if src_start + size > len(fisher_dic[occupation[0]]):
size = len(fisher_dic[occupation[0]]) % self.weight_per_page
src_end = src_start + size
fisher = fisher_dic[occupation[0]][src_start:src_end]
if len(fisher) != self.weight_per_page:
fisher = np.concatenate([fisher,
np.zeros(self.weight_per_page-len(fisher), dtype=np.float32)])
fisher_list.append(fisher)
if not fisher_list:
continue
fisher_page_sum = np.sum(fisher_list, axis=0)
dst_start = i*self.weight_per_page
dst_end = dst_start+self.weight_per_page
fisher_sum_vector[dst_start:dst_end] = fisher_page_sum
return fisher_sum_vector
def get_fisher_vector_page_order(self, vnn, target):
fisher = None
if target == 'network':
fisher = self.load_network_fisher(vnn)
elif target == 'vnn':
fisher = self.load_vnn_fisher(vnn)
else:
raise Exception('Neither network nor vnn')
fisher_vector = self.vectorize_list(fisher)
fisher_vector_page_order = np.zeros(self.num_of_weight_page*self.weight_per_page,
dtype=np.float32)
weight_page_occupation = self.load_weight_page_occupation()
for i in range(len(weight_page_occupation)):
for occupation in weight_page_occupation[i]:
if occupation[0] == vnn.id:
size = self.weight_per_page
src_start = occupation[1]*self.weight_per_page
if src_start + size > len(fisher_vector):
size = len(fisher_vector) % self.weight_per_page
src_end = src_start + size
fisher = fisher_vector[src_start:src_end]
if len(fisher) != self.weight_per_page:
fisher = np.concatenate([fisher,
np.zeros(self.weight_per_page-len(fisher),
dtype=np.float32)])
dst_start = i*self.weight_per_page
dst_end = dst_start+self.weight_per_page
fisher_vector_page_order[dst_start:dst_end] = fisher
break
return fisher_vector_page_order
def get_weight_vector_page_order(self, vnn, target):
weight = None
if target == 'network':
weight = self.load_network_weight(vnn)
elif target == 'vnn':
weight = self.load_vnn_weight(vnn)
else:
raise Exception('Neither network nor vnn')
weight_vector = self.vectorize_list(weight)
weight_vector_page_order = np.zeros(self.num_of_weight_page*self.weight_per_page,
dtype=np.float32)
weight_page_occupation = self.load_weight_page_occupation()
for i in range(len(weight_page_occupation)):
for occupation in weight_page_occupation[i]:
if occupation[0] == vnn.id:
size = self.weight_per_page
src_start = occupation[1]*self.weight_per_page
if src_start + size > len(weight_vector):
size = len(weight_vector) % self.weight_per_page
src_end = src_start + size
weight = weight_vector[src_start:src_end]
if len(weight) != self.weight_per_page:
weight = np.concatenate([weight,
np.zeros(self.weight_per_page-len(weight),
dtype=np.float32)])
dst_start = i*self.weight_per_page
dst_end = dst_start+self.weight_per_page
weight_vector_page_order[dst_start:dst_end] = weight
break
return weight_vector_page_order
def matching_cost_pair(self, fisher1, weight1, fisher2, weight2):
assert len(fisher1) == len(weight1)
assert len(fisher2) == len(weight2)
assert len(fisher1) == len(fisher2)
fisher_sum = np.add(fisher1, fisher2)
square_weight_diff = np.square(np.subtract(weight1, weight2))
cost = np.sum(np.multiply(fisher_sum, square_weight_diff))
return cost
def calculate_cost(self, vnn):
print('[calculate_cost]')
fisher_sum_vector = self.get_fisher_sum_vector()
weight_vector = self.weight_page.flatten()
assert len(fisher_sum_vector) == len(weight_vector)
network_fisher_vector = self.get_fisher_vector_pad(vnn, 'network')
network_weight_vector = self.get_weight_vector_pad(vnn, 'network')
assert len(network_fisher_vector) == len(network_weight_vector)
total_cost = 0.0
idx = 0
for page_no in vnn.weight_page_list:
size = self.weight_per_page
start_n = idx * self.weight_per_page
end_n = start_n + size
start_s = page_no * self.weight_per_page
end_s = start_s + size
fisher_network = network_fisher_vector[start_n:end_n]
weight_network = network_weight_vector[start_n:end_n]
fisher_star = fisher_sum_vector[start_s:end_s]
weight_star = weight_vector[start_s:end_s]
zero_fisher_network = np.where(fisher_network == 0)
fisher_star[zero_fisher_network] = 0
zero_fisher_star = np.where(fisher_star == 0)
fisher_network[zero_fisher_star] = 0
fisher_cost = np.add(fisher_network, fisher_star)
#fisher_cost = np.multiply(fisher_network, fisher_star)
weight_cost = np.square(weight_network - weight_star)
cost = np.sum(np.multiply(fisher_cost, weight_cost))
total_cost += cost
idx += 1
print('toal_cost:', total_cost)
return total_cost
def calculate_network_cost(self, vnn_list):
total_cost = 0
for i in range(len(vnn_list)):
for j in range(i+1, len(vnn_list)):
fisher1 = self.get_fisher_vector_page_order(vnn_list[i], 'network')
weight1 = self.get_weight_vector_page_order(vnn_list[i], 'network')
fisher2 = self.get_fisher_vector_page_order(vnn_list[j], 'network')
weight2 = self.get_weight_vector_page_order(vnn_list[j], 'network')
zero_fisher1 = np.where(fisher1 <= 0)
fisher2[zero_fisher1] = 0
zero_fisher2 = np.where(fisher2 <= 0)
fisher1[zero_fisher2] = 0
total_cost += self.matching_cost_pair(fisher1, weight1, fisher2, weight2)
return total_cost
def match_page_by_cost(self, vnn):
print('[match_page_by_cost]')
fisher_sum_vector = self.get_fisher_sum_vector()
weight_vector = self.weight_page.flatten()
assert len(fisher_sum_vector) == len(weight_vector)
network_fisher_vector = self.get_fisher_vector_pad(vnn, 'network')
network_weight_vector = self.get_weight_vector_pad(vnn, 'network')
assert len(network_fisher_vector) == len(network_weight_vector)
"""
page_to_alloc = len(network_weight_vector)/self.weight_per_page
page_list = np.arange(self.num_of_weight_page, dtype=np.int32)
network_page_list = np.arange(page_to_alloc, dtype=np.int32)
weight_virtualization_op = tf.load_op_library(self.weight_virtualization_op_filename)
with tf.Graph().as_default() as graph:
with tf.Session() as sess:
page_alloc_op = weight_virtualization_op.page_alloc(fisher_sum_vector,
weight_vector, page_list, network_fisher_vector,
network_weight_vector, network_page_list,
page_size=self.weight_per_page)
page_match, cost = sess.run(page_alloc_op)
print('cost:', cost)
print('')
weight_page_list = page_match[page_match[:,1].argsort()][:,0].astype(np.int32)
if len(weight_page_list) > len(set(weight_page_list)):
raise Exception('weight_page_list is not unique')
assert len(weight_page_list) == page_to_alloc
"""
#"""
weight_page_occupation = self.load_weight_page_occupation()
len_list_of_occupation = np.asarray([len(page_occupation) for page_occupation in weight_page_occupation])
max_occupation = np.max(len_list_of_occupation)
page_to_alloc = len(network_weight_vector)/self.weight_per_page
network_page_list = np.arange(page_to_alloc, dtype=np.int32)
page_match_list = []
total_cost = 0
weight_virtualization_op = tf.load_op_library(self.weight_virtualization_op_filename)
with tf.Graph().as_default() as graph:
with tf.Session() as sess:
for occupation in range(max_occupation+1):
page_list = np.where(len_list_of_occupation == occupation)[0]
print('occupation:', occupation)
print('len(page_list):', len(page_list))
print('len(network_page_list):', len(network_page_list))
if len(page_list) <= 0:
print('cost: 0\n')
continue
page_alloc_op = weight_virtualization_op.page_alloc(fisher_sum_vector,
weight_vector, page_list, network_fisher_vector,
network_weight_vector, network_page_list,
page_size=self.weight_per_page)
page_match, cost = sess.run(page_alloc_op)
total_cost += cost
print('cost:', cost)
print('')
page_match_list.append(page_match)
network_page_list = list(set(network_page_list) - set(page_match[:,1]))
if not network_page_list:
break
if network_page_list:
raise Exception('network_page_list is not empty')
page_match_array = np.concatenate(page_match_list)
weight_page_list = page_match_array[page_match_array[:,1].argsort()][:,0].astype(np.int32)
if len(weight_page_list) > len(set(weight_page_list)):
raise Exception('weight_page_list is not unique')
assert len(weight_page_list) == page_to_alloc
#print('total_cost:', total_cost)
#"""
vnn.weight_page_list = weight_page_list
def get_fisher_vector_pad(self, vnn, target):
if target == 'network':
fisher = self.load_network_fisher(vnn)
elif target == 'vnn':
fisher = self.load_vnn_fisher(vnn)
else:
raise Exception('Neither network nor vnn')
fisher_vector = self.vectorize_list(fisher)
pad_len = self.weight_per_page - (len(fisher_vector) % self.weight_per_page)
fisher_vector_pad = np.concatenate([fisher_vector,
np.zeros(pad_len, dtype=np.float32)])
assert len(fisher_vector_pad) % self.weight_per_page == 0
return fisher_vector_pad
def get_weight_vector_pad(self, vnn, target):
if target == 'network':
weight = self.load_network_weight(vnn)
elif target == 'vnn':
weight = self.load_vnn_weight(vnn)
else:
raise Exception('Neither network nor vnn')
weight_vector = self.vectorize_list(weight)
pad_len = self.weight_per_page - (len(weight_vector) % self.weight_per_page)
weight_vector_pad = np.concatenate([weight_vector,
np.zeros(pad_len, dtype=np.float32)])
assert len(weight_vector_pad) % self.weight_per_page == 0
return weight_vector_pad
def match_page_by_random_multi(self, vnn_list):
print('[match_page_by_random_multi]')
for vnn in vnn_list:
network_fisher_vector = self.get_fisher_vector_pad(vnn, 'network')
network_weight_vector = self.get_weight_vector_pad(vnn, 'network')
assert len(network_fisher_vector) == len(network_weight_vector)
page_to_alloc = len(network_weight_vector)/self.weight_per_page
vnn.weight_page_list = np.random.choice(self.num_of_weight_page, page_to_alloc, replace=False)
def match_page_by_cost_multi(self, vnn_list):
print('[match_page_by_cost_multi]')
fisher_sum_vector = self.get_fisher_sum_vector()
weight_vector = self.weight_page.flatten()
assert len(fisher_sum_vector) == len(weight_vector)
base_page_list = np.arange(self.num_of_weight_page, dtype=np.int32)
network_fisher_vector_list = []
network_weight_vector_list = []
network_page_list_list = []
page_to_alloc_list = []
for vnn in vnn_list:
network_fisher_vector = self.get_fisher_vector_pad(vnn, 'network')
network_weight_vector = self.get_weight_vector_pad(vnn, 'network')
assert len(network_fisher_vector) == len(network_weight_vector)
network_fisher_vector_list.append(network_fisher_vector)
network_weight_vector_list.append(network_weight_vector)
page_to_alloc = len(network_weight_vector)/self.weight_per_page
page_to_alloc_list.append(page_to_alloc)
network_page_list = np.arange(page_to_alloc, dtype=np.int32)
network_page_list_list.append(network_page_list)
weight_virtualization_op = tf.load_op_library(self.weight_virtualization_op_filename)
with tf.Graph().as_default() as graph:
with tf.Session() as sess:
page_alloc_multi_op = weight_virtualization_op.page_alloc_multi(fisher_sum_vector,
weight_vector, base_page_list, network_fisher_vector_list,
network_weight_vector_list, network_page_list_list,
page_size=self.weight_per_page)
page_match_list, cost = sess.run(page_alloc_multi_op)
print('total cost[d]:', cost)
for vnn, page_match, num_of_page in zip(vnn_list, page_match_list, page_to_alloc_list):
weight_page_list = page_match.astype(np.int32)
assert len(weight_page_list) == num_of_page
if len(weight_page_list) > len(set(weight_page_list)):
raise Exception('weight_page_list is not unique')
vnn.weight_page_list = weight_page_list
def match_page_by_random(self, vnn, num_of_page_to_select):
print('[match_page_by_random]')
weight_page_occupation = self.load_weight_page_occupation()
len_list_of_occupation = np.asarray([len(page_occupation) for page_occupation in weight_page_occupation])
max_occupation = np.max(len_list_of_occupation)
page_sorted_by_occupation = []
num_of_weight_page_not_allocated = num_of_page_to_select
weight_page_list = []
for occupation in range(max_occupation+1):
page_sorted_by_occupation.append(np.where(len_list_of_occupation == occupation))
pages = page_sorted_by_occupation[occupation][0]
if num_of_weight_page_not_allocated > len(pages):
weight_page_list = np.concatenate((weight_page_list, pages))
num_of_weight_page_not_allocated -= len(pages)
else:
if occupation != 0:
np.random.shuffle(pages) # By RANDOM
weight_page_list = np.concatenate((weight_page_list,
pages[0:num_of_weight_page_not_allocated]))
num_of_weight_page_not_allocated = 0
np.random.shuffle(weight_page_list)
if len(weight_page_list) > len(set(weight_page_list)):
raise Exception('weight_page_list is not unique')
vnn.weight_page_list = weight_page_list.astype(np.int32)
#vnn.weight_page_list = np.random.choice(self.num_of_weight_page, num_of_page_to_select, replace=False)
def match_weight_page(self, vnn):
num_of_weight_page = vnn.num_of_weight // self.weight_per_page
if vnn.num_of_weight % self.weight_per_page != 0:
num_of_weight_page += 1
assert num_of_weight_page <= self.num_of_weight_page,\
"%d vs. %d" % (num_of_weight_page, self.num_of_weight_page)
if not self.vnns:
vnn.weight_page_list = np.arange(num_of_weight_page, dtype=np.int32)
else:
time1 = time.time()
#self.match_page_by_random(vnn, num_of_weight_page)
self.match_page_by_cost(vnn)
time2 = time.time()
print('assing_page %0.3f ms' % ((time2-time1)*1000.0))
self.calculate_cost(vnn)
self.update_weight_page_occupation(vnn)
print("%d pages allocated for %d weights" %
(len(vnn.weight_page_list), vnn.num_of_weight))
def match_weight_page_multi(self, vnn_list):
for vnn in vnn_list:
num_of_weight_page = vnn.num_of_weight // self.weight_per_page
if vnn.num_of_weight % self.weight_per_page != 0:
num_of_weight_page += 1
assert num_of_weight_page <= self.num_of_weight_page,\
"%d vs. %d" % (num_of_weight_page, self.num_of_weight_page)
self.match_page_by_cost_multi(vnn_list)
for vnn in vnn_list:
self.update_weight_page_occupation(vnn)
print("%d pages allocated for %d weights" %
(len(vnn.weight_page_list), vnn.num_of_weight))
new_network_cost = self.calculate_network_cost(vnn_list)
print('new_network_cost:', new_network_cost)
"""
if os.path.exists(self.weight_page_occupation_filename):
os.remove(self.weight_page_occupation_filename)
self.match_page_by_random_multi(vnn_list)
for vnn in vnn_list:
self.update_weight_page_occupation(vnn)
total_cost = self.calculate_network_cost(vnn_list)
print('total cost:', total_cost)
if os.path.exists(self.weight_page_occupation_filename):
os.remove(self.weight_page_occupation_filename)
exit(1)
"""
def dematch_weight_page(self, vnn):
weight_page_occupation = self.load_weight_page_occupation()
for page in weight_page_occupation:
for occupation in page:
if occupation[0] == vnn.id:
page.remove(occupation)
break
self.save_weight_page_occupation(weight_page_occupation)
def quadratic_mean(self, cost_list):
if not cost_list:
return 0
stacked = tf.stack(cost_list)
non_zero = tf.cast(tf.count_nonzero(stacked, 0), tf.float32)
non_zero_pad = tf.where(tf.equal(non_zero, 0), tf.ones_like(non_zero), non_zero)
return tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(stacked), 0) / non_zero_pad))
def arithmetic_mean(self, cost_list):
if not cost_list:
return 0
stacked = tf.stack(cost_list)
non_zero = tf.cast(tf.count_nonzero(stacked, 0), tf.float32)
non_zero_pad = tf.where(tf.equal(non_zero, 0), tf.ones_like(non_zero), non_zero)
return tf.reduce_sum(tf.reduce_sum(stacked, 0) / non_zero_pad)
def harmonic_mean(self, cost_list):
@ops.RegisterGradient("HarmonicMean")
def harmonic_mean_grad(op, grad):
input_list = []
for i in range(len(op.inputs)):
input_list.append(op.inputs[i])
stacked = tf.stack(input_list)
non_zero = tf.count_nonzero(stacked, 0)
grad_list = []
for i in range(len(op.inputs)):
gradient = tf.where(op.inputs[i] <= 0, tf.zeros_like(non_zero), non_zero)
grad_list.append(tf.cast(gradient, tf.float32))
return grad_list
if not cost_list:
return 0
weight_virtualization_op = tf.load_op_library(self.weight_virtualization_op_filename)
return weight_virtualization_op.harmonic_mean(cost_list)
def get_matching_loss(self, vnn, sess, lamb=10.0):
print ("get_matching_loss")
matching_loss = tf.constant(0.0)
tensor_weights = tf.trainable_variables()
tensor_weights_concat = []
for weight in tensor_weights:
tensor_weights_concat.append(tf.reshape(weight, [tf.size(weight)]))
new_weight_vector = tf.concat(tensor_weights_concat, 0)
new_weight_vector_len = new_weight_vector.get_shape().as_list()[0]
weight_page_occupation = self.load_weight_page_occupation()
cost_list = []
for name_, vnn_ in self.vnns.items():
if vnn.id == vnn_.id:
continue;
fisher = self.load_network_fisher(vnn_)
#fisher = self.load_vnn_fisher(vnn_)
if fisher is None:
continue
fisher_vector = self.vectorize_list(fisher)
#weight = self.load_network_weight(vnn_)
weight = self.load_weight(vnn_)
#weight_vector = self.get_weight_from_page(vnn_)
weight_vector = self.vectorize_list(weight)
assert len(fisher_vector) == len(weight_vector)
fisher_vector_reordered = np.zeros(new_weight_vector_len, dtype=np.float32)
weight_vector_reordered = np.zeros(new_weight_vector_len, dtype=np.float32)
page_idx = 0
for page in vnn.weight_page_list:
size = self.weight_per_page
page_pos = -1
for occupation in weight_page_occupation[page]:
if occupation[0] == vnn_.id:
page_pos = occupation[1]
break
if page_pos == -1:
page_idx += 1
continue
if page_idx*self.weight_per_page+size > new_weight_vector_len:
size = new_weight_vector_len % self.weight_per_page
if page_pos*self.weight_per_page+size > len(fisher_vector):
size = len(fisher_vector) % self.weight_per_page
dst_start = page_idx*self.weight_per_page
dst_end = dst_start+size
src_start = page_pos*self.weight_per_page
src_end = src_start+size
fisher_vector_reordered[dst_start:dst_end] = fisher_vector[src_start:src_end]
weight_vector_reordered[dst_start:dst_end] = weight_vector[src_start:src_end]
page_idx += 1
weight_diff_square = tf.square(new_weight_vector - weight_vector_reordered)
cost_list.append(tf.multiply(weight_diff_square, fisher_vector_reordered))
if cost_list:
#matching_loss += self.quadratic_mean(cost_list)
matching_loss += self.arithmetic_mean(cost_list)
#matching_loss += self.harmonic_mean(cost_list)
return lamb*matching_loss
def vectorize_list(self, list_to_vectorize):
vector_list = []
for item in list_to_vectorize:
vector_list.append(item.flatten())
return np.concatenate(vector_list)
def plot_vnn_fisher(self, vnn_list=None):
if vnn_list is None:
vnn_list = []
for name, vnn in self.vnns.items():
vnn_list.append(vnn)
for vnn in vnn_list:
fisher = self.get_fisher_vector_page_order(vnn, 'vnn')
plt.plot(fisher, label=vnn.name)