-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.cpp
2341 lines (1693 loc) · 67.9 KB
/
demo.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <vector>
#include <fstream>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <chrono>
#include <seal/seal.h>
#include <zlib.h>
#include "examples.h"
#include <sstream>
#include <cstdlib>
#include <cstring>
using namespace std;
using namespace seal;
class Stopwatch
{
public:
Stopwatch(string timer_name) :
name_(timer_name),
start_time_(chrono::high_resolution_clock::now())
{
}
~Stopwatch()
{
auto end_time = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end_time - start_time_);
cout << name_ << ": " << double(duration.count()) << " milliseconds" << endl;
}
private:
string name_;
chrono::high_resolution_clock::time_point start_time_;
};
vector<vector<double>> read_params_2dim(string path)
{
ifstream is(path);
int line_size = 100000 * 3;
char line[line_size];
is.seekg(0, is.end);
int file_size = is.tellg();
is.seekg(0, is.beg);
int line_number = 1;
vector<vector<double>> result;
while(is.good()) {
is.getline(line, file_size);
if(strlen(line) == 0) {
continue;
}
stringstream ss;
ss << line;
char number_str[100];
vector<double> line_doubles;
while(ss.good()) {
ss.getline(number_str, line_size, ' ');
if(strlen(number_str) == 0) {
continue;
}
line_doubles.push_back(atof(number_str));
}
result.push_back(line_doubles);
}
is.close();
return result;
}
vector<double> read_params_1dim(string path)
{
ifstream is(path);
int line_size = 500 * 3;
char line[line_size];
is.seekg(0, is.end);
int file_size = is.tellg();
is.seekg(0, is.beg);
int line_number = 1;
vector<double> result;
while(is.good()) {
is.getline(line, file_size);
if(strlen(line) == 0) {
continue;
}
result.push_back(atof(line));
}
is.close();
return result;
}
vector<vector<vector<double>>> reshape_2to3(int dim1, vector<vector<double>> X)
{
int dim2=X.size()/dim1;
int dim3=X[0].size();
vector<vector<vector<double>>> reshape(dim1, vector<vector<double>>(dim2 , vector<double>(dim3, 0)));
for (int i=0;i<reshape.size();i++){
for(int j=0;j<reshape[0].size();j++){
for(int k=0;k<reshape[0][0].size();k++){
reshape[i][j][k] = X[j+i*(reshape[0].size())][k];
}
}
}
return reshape;
}
vector<vector<double>> matrix_encode(vector<vector<double>> pA, vector<vector<double>> A, vector<int> xindex, vector<int> yindex, int dimension, int slot_count)
{
pA.resize(dimension);
for (int i = 0; i < dimension; i++){
pA[i].resize(slot_count);
for (int j = 0; j < slot_count; j++){
//pA[i][j]= A[xindex[i]][yindex[i]];
if (xindex[i]==16){
pA[i][j]=0;
}
else{
pA[i][j]= A[xindex[i]][yindex[i]];
}
//print_vector(pA[i],3,7);
}
}
return pA;
}
// vector<double> parms_1d_encode(vector<double> vec_encode, int slot_count, int output_channel)
// {
// vector<double> vec_plain(slot_count);
// for( int i=0; i< vec_plain.size() ;i++){
// vec_plain[i]= vec_encode[ i / output_channel ];
// }
// return vec_plain;
// }
void test_demo()
{
// CLIENT'S VIEW
// Vector of inputs
size_t dimension =16;
// 2 by 2 by 32 by 16 dimension feature map
vector<double> inputs { 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.009, 0.003, 0.007, 0.009, 0.003, 0.002, 0.001, 0.004, 0.007, 0.008 };
//feature map size
vector<int> featuredim{2,2,32,16};
// init the feature map
vector<vector<vector<vector<double>>>> input_featuremap;
input_featuremap.resize(featuredim[0]);
for (size_t i = 0; i < featuredim[0]; i++){
input_featuremap[i].resize(featuredim[1]);
for (size_t j = 0; j < featuredim[1]; j++){
input_featuremap[i][j].resize(featuredim[2]);
for (size_t k = 0; k < featuredim[2]; k++){
input_featuremap[i][j][k].resize(featuredim[3]);
for (size_t v = 0; v < featuredim[3]; v++) {
input_featuremap[i][j][k][v]=inputs[v] + 0.0001*(j+k+v)+0.00005*i;
};
};
};
};
cout<<"feature map first row--"<<endl;
print_vector(input_featuremap[0][0][1],16,3);
//done with the feature map 2*2*32*16 init
/*Setting up encryption parameters */
EncryptionParameters parms(scheme_type::ckks);
//size_t poly_modulus_degree = 128;
size_t poly_modulus_degree = 8192;
parms.set_poly_modulus_degree(poly_modulus_degree);
int my_scale = 32;
//parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, { 60, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 60 })); // this works with 8192
parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, { 60, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, my_scale, 60 })); // this works with 8192
//parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, { 60, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 60 })); // this works with 8192
//parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, { 60, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 60 })); // this works with 8192
//parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, { 60, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 60 })); // this works with 8192
/*random seed fixed*/
prng_seed_type seed = { uint64_t(1), uint64_t(2), uint64_t(3), uint64_t(4),
uint64_t(5), uint64_t(6), uint64_t(7), uint64_t(8) };
auto rng = make_shared<Blake2xbPRNGFactory>(Blake2xbPRNGFactory(seed));
parms.set_random_generator(rng);
// Set up the SEALContext
//auto context = SEALContext::Create(parms);
SEALContext context(parms, true, sec_level_type::none);
print_parameters(context);
cout << endl;
// Use a scale to encode
double scale = pow(2.0, my_scale);
// Create a vector of plaintext for the input data
cout << "Input vector first row: " << endl;
print_vector(input_featuremap[0][0][1], 16, 3);
// Set up keys
KeyGenerator keygen(context);
auto sk = keygen.secret_key();
PublicKey pk;
keygen.create_public_key(pk);
RelinKeys relin_keys;
keygen.create_relin_keys(relin_keys);
GaloisKeys galk;
{
Stopwatch sw("GaloisKeys creation time");
keygen.create_galois_keys(galk);
// ofstream fs("test.galk", ios::binary);
// keygen.galois_keys_save(rots, fs);
}
Encryptor encryptor(context, pk);
Evaluator evaluator(context);
Decryptor decryptor(context, sk);
CKKSEncoder encoder(context);
size_t slot_count = encoder.slot_count();
cout << "Number of slots: " << slot_count << endl;
// begin encode the plaintext for the featuremap
// define the modulus for later encoding
int mod0=featuredim[0]*featuredim[1]*featuredim[2]; // 2*2*32=128
int mod1=featuredim[1]*featuredim[2]; // 2*32=64
int mod2=featuredim[2]; // 32
// define the index for encoding
int index=0;
int index0=0;
int index1=0;
int index2=0;
vector<vector<double>> vrep;
vrep.resize(dimension);
for (int i = 0; i < vrep.size(); i++){
vrep[i].resize(slot_count);
for (int j = 0; j < slot_count; j++){
index = j % mod0;
index2= j % mod2;
//cout<<"index print: "<<index<<endl;
if ( index <=31){
index0=0;
index1=0;
//cout<<"index print: "<<index0<<" "<<index1<<endl;
vrep[i][j] = input_featuremap[index0][index1][index2][i];
}else if( (32<= index) & (index <=63)){
index0=1;
index1=0;
//cout<<"index print: "<<index0<<" "<<index1<<endl;
vrep[i][j] = input_featuremap[index0][index1][index2][i];
}else if( (64<= index) & (index <=95)){
index0=0;
index1=1;
//cout<<"index print: "<<index0<<" "<<index1<<endl;
vrep[i][j] = input_featuremap[index0][index1][index2][i];
}else if(index >=96){
index0=1;
index1=1;
//cout<<"index print: "<<index0<<" "<<index1<<endl;
vrep[i][j] = input_featuremap[index0][index1][index2][i];
}
};
};
cout<< "Encode vector size: " << vrep[0].size()<<endl;
cout<< "Encode vector print: " <<endl;
print_vector(vrep[0],128,4);
// create a vector of plaintext
vector<Plaintext> pts;
for (int i=0; i<dimension; i++){
Plaintext p;
encoder.encode(vrep[i],scale,p);
pts.emplace_back(move(p));
}
//finished the encode for the input data
// Create a vector ciphertext
vector<Ciphertext> cts;
{
Stopwatch sw("Encryption time");
for (const auto &p :pts){
Ciphertext c;
encryptor.encrypt(p,c);
cts.emplace_back(move(c));
}
cout<<"Encryption finished"<<endl;
}
//Save to see size
// {
// ofstream fs("test.ct", ios::binary);
// ct.save(fs);
// }
// Now send this vector to the server!
// Also save and send the EncryptionParameters.
// SERVER'S VIEW
/* column-wise sparse matrix multiplication algorithm*/
/* first read the sparse matrices and other parameters*/
vector<vector<double>> raw_A;
vector<vector<double>> edge_importance0;
vector<vector<double>> conv1;
vector<vector<double>> bias1;
vector<double> gamma1;
vector<double> beta1;
vector<double> mean1;
vector<double> var1;
vector<vector<double>> conv2;
vector<double> bias2;
vector<double> gamma2;
vector<double> beta2;
vector<double> mean2;
vector<double> var2;
conv1=read_params_2dim("./params/st_gcn_networks.0.gcn.conv.weight.txt");
bias1=read_params_2dim("./params/st_gcn_networks.0.gcn.conv.bias.txt");
raw_A=read_params_2dim("./params/A.txt");
edge_importance0=read_params_2dim("./params/edge_importance.0.txt");
gamma1=read_params_1dim("./params/st_gcn_networks.0.tcn.0.weight.txt");
beta1=read_params_1dim("./params/st_gcn_networks.0.tcn.0.bias.txt");
mean1=read_params_1dim("./params/st_gcn_networks.0.tcn.0.running_mean.txt");
var1=read_params_1dim("./params/st_gcn_networks.0.tcn.0.running_var.txt");
conv2=read_params_2dim("./params/st_gcn_networks.0.tcn.2.weight.txt");
bias2=read_params_1dim("./params/st_gcn_networks.0.tcn.2.bias.txt");
gamma2=read_params_1dim("./params/st_gcn_networks.0.tcn.3.weight.txt");
beta2=read_params_1dim("./params/st_gcn_networks.0.tcn.3.bias.txt");
mean2=read_params_1dim("./params/st_gcn_networks.0.tcn.3.running_mean.txt");
var2=read_params_1dim("./params/st_gcn_networks.0.tcn.3.running_var.txt");
vector<vector<vector<double>>> reshape_conv1;
vector<vector<vector<double>>> reshape_conv2;
reshape_conv1=reshape_2to3(3,conv1);
reshape_conv2=reshape_2to3(64,conv2);
int output_channel=reshape_conv1[0].size();
for (int i=0; i<var1.size() ;i++){
var1[i]=sqrt(var1[i]);
var2[i]=sqrt(var2[i]);
}
cout<<"current outputchannel size :"<<output_channel<<endl;
/*resize the conv1 layer to 3*64*2*/
for(int i=0; i<reshape_conv1.size(); i++){
for(int j=0; j<reshape_conv1[0].size(); j++){
reshape_conv1[i][j].resize(2);
//print_vector(reshape_conv1[i][j]);
}
}
//print_vector(reshape_conv1[0][1]);
// init the square Matrxi A0, A1, A2
vector<vector<double> > A0(dimension);
vector<vector<double> > A1(dimension);
vector<vector<double> > A2(dimension);
for (int i = 0; i < dimension; i++){
A0[i].resize(dimension);
A1[i].resize(dimension);
A2[i].resize(dimension);
for (int j = 0; j < dimension; j++ ){
A0[i][j] = raw_A[i][j]*edge_importance0[i][j];
A1[i][j] = raw_A[i+25][j]*edge_importance0[i+25][j];
A2[i][j] = raw_A[i+50][j]*edge_importance0[i+50][j];
}
//print_vector(A0[i],3,7);
}
/*index set for the sparse matrix*/
vector<int>xindex0 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
vector<int>yindex0 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
vector<int>xindex1_0{12, 0, 3, 16, 5, 6, 7, 16, 9, 10, 11, 16, 13, 14, 15, 16};
vector<int>yindex1_0{0, 1, 2, 16, 4, 5, 6, 16, 8, 9, 10, 16, 12, 13, 14, 16};
vector<int>xindex1_1{16, 16, 16, 16, 16, 16, 5, 6, 16, 16, 9, 10, 0, 12, 13, 14};
vector<int>yindex1_1{16, 16, 16, 16, 16, 16, 6, 7, 16, 16, 10, 11, 12, 13, 14, 15};
vector<int>xindex2 {1, 16, 16, 2, 16, 4, 16, 16, 16, 8, 16, 16, 16, 16, 16, 16};
vector<int>yindex2 {0, 16, 16, 3, 16, 5, 16, 16, 16, 9, 16, 16, 16, 16, 16, 16};
//encoding the matrix plaintexts for sparse matrix multiplication
vector<vector<double>> pA0,pA1_0,pA1_1,pA2;
pA0=matrix_encode(pA0, A0, xindex0, yindex0, dimension, slot_count);
pA1_0=matrix_encode(pA1_0, A1, xindex1_0, yindex1_0, dimension, slot_count);
pA1_1=matrix_encode(pA1_0, A1, xindex1_1, yindex1_1, dimension, slot_count);
pA2=matrix_encode(pA2, A2, xindex2, yindex2, dimension, slot_count);
cout<<"print the encode matrix "<<endl;
print_vector(pA0[0]);
print_vector(pA1_0[0]);
print_vector(pA1_1[0]);
print_vector(pA2[0]);
/*absorb the conv1 layer with matrix A and bn1*/
for (int i=0; i<pA0.size() ;i++){
for (int j=0; j < pA0[0].size(); j++){
pA0[i][j] = pA0[i][j] * (gamma1[j / 64]/var1[j / 64]);
pA1_0[i][j]= pA1_0[i][j] * (gamma1[j / 64]/var1[j / 64]);
pA1_1[i][j]= pA1_1[i][j] * (gamma1[j / 64]/var1[j / 64]);
pA2[i][j] = pA2[i][j] * (gamma1[j / 64]/var1[j / 64]);
}
}
vector<double> b1(slot_count);
for (int i=0; i< slot_count ;i++){
b1[i]=beta1[i / 64]-mean1[i / 64]*(gamma1[i / 64]/var1[i / 64]);
}
//print_vector(b1);
Plaintext b1_plain;
encoder.encode(b1,scale,b1_plain);
cout<<"matrix A: "<<endl;
print_vector(A0[0],16,3);
cout<<"encode matrix A: "<<endl;
print_vector(pA0[0],16,3);
/* encode the sparse matrix into plaintexts*/
vector<vector<vector<double>>> A_sparse{pA0, pA1_0, pA1_1, pA2};
vector<vector<Plaintext>> A_plain(4);
// 4 * 16 (16 columns)
for (int k =0; k<4; k++){
A_plain[k].resize(dimension);
for(int i=0; i<dimension; i++){
//Plaintext p;
encoder.encode(A_sparse[k][i], scale, A_plain[k][i]);
//A_plain[k].emplace_back(move(p));
}
}
/*absorb the bn2 layer with conv2 layer*/
for (int i=0; i<bias2.size(); i++){
//bias2[i]= beta2[i]-(gamma2[i]/var2[i])*mean2[i] + (gamma2[i]/var2[i]) * bias2[i];
bias2[i]= beta2[i] + (gamma2[i]/var2[i]) * (bias2[i] - mean2[i]);
}
for (int k=0; k< reshape_conv2.size(); k++){
for (int i=0; i<reshape_conv2[0].size(); i++){
for ( int j=0; j< reshape_conv2[0][0].size(); j++){
reshape_conv2[k][i][j]= reshape_conv2[k][i][j] * (gamma2[k]/var2[k]);
}
}
}
/* the st-gcn layer implementation*/
cout<<"conv1 weight layer size "<<reshape_conv1.size()<<" , "<<reshape_conv1[0].size()<<" , "<<reshape_conv1[0][0].size()<<endl;
cout<<"conv1 bias layer size "<<bias1.size()<<" , "<<bias1[0].size()<<endl;
cout<<"bn1 gamma1 layer size "<<gamma1.size()<<endl;
cout<<"bn1 beta1 layer size "<<beta1.size()<<endl;
cout<<"bn1 mean1 layer size "<<mean1.size()<<endl;
cout<<"bn1 var1 layer size "<<var1.size()<<endl;
cout<<"conv2 layer size "<<reshape_conv2.size()<<" , "<<reshape_conv2[0].size()<<" , "<<reshape_conv2[0][0].size()<<endl;
cout<<"conv2 bias layer size "<<bias2.size()<<endl;
cout<<"bn2 gamma1 layer size "<<gamma1.size()<<endl;
cout<<"bn2 beta1 layer size "<<beta1.size()<<endl;
cout<<"bn2 mean1 layer size "<<mean1.size()<<endl;
cout<<"bn2 var1 layer size "<<var1.size()<<endl;
/* first conv1*1 layer and the sparse matrix multiplication*/
vector<vector<vector<double>>> conv1_encode(3);
//2(input channels) * 3 * 4096 (64 output channels * 64 (2 person * 32 frames))
/*encode the conv1 layer weights*/
for (int r=0; r<3 ;r++){
conv1_encode[r].resize(2);
for (int k=0; k < 2; k++){
conv1_encode[r][k].resize(slot_count);
for(int j=0; j<slot_count; j++){
conv1_encode[r][k][j]=reshape_conv1[r][j / 64][ ( (j / 64) +k ) % 2];
}
}
}
cout<<"print conv1 encode: "<<endl;
print_vector(conv1_encode[0][0],128);
print_vector(conv1_encode[0][1],128);
//print_vector(conv1_encode[0][0],128);
/*encode the conv1 weight into plaintexts*/
vector<vector<Plaintext>> conv1_plain(3); // 3*2
for (int r=0; r<3 ;r++){
conv1_plain[r].resize(2);
for (int k =0; k<2; k++){
encoder.encode(conv1_encode[r][k], scale, conv1_plain[r][k]);
}
}
/* encode the 9*1 conv kernel weights*/
vector<vector<vector<double>>> conv2_encode(64); // 64*9*4096
//reshape_conv2 : 64(output channel) * 64(input channel) * 90
for (int i=0; i< output_channel; i++){
conv2_encode[i].resize(9);
for (int r=0; r<9; r++){
conv2_encode[i][r].resize(slot_count);
for (int j=0; j<slot_count; j++){
if( (r < 4) & ((j % 32) < (4-r)) ){
conv2_encode[i][r][j]=0;
}else if((r > 4) & ((j % 32) >= (32-(r-4))) ){
conv2_encode[i][r][j]=0;
}else{
// full-step
//conv2_encode[i][r][j]= reshape_conv2[ j / 64 ][ (j / 64 + i) % 64 ][r];
// baby-step
conv2_encode[i][r][j]= reshape_conv2[ ( (j / 64) + (64-i) ) % 64 ][ j / 64 ][r];
}
}
}
}
// cout<<"print conv2 weights"<<endl;
// print_vector(reshape_conv2[63][0]);
cout<<"print conv2 encode: "<<endl;
print_vector(conv2_encode[0][0],128);
/*encode the conv2 weight into plaintexts*/
vector<vector<Plaintext>> conv2_plain(64);
for (int i=0; i< 64; i++){
conv2_plain[i].resize(9);
for (int j=0; j<9; j++){
encoder.encode(conv2_encode[i][j],scale,conv2_plain[i][j]);
}
}
/*encode the conv bias 1 & 2 into plaintexts*/
vector<vector<double>> bias1_encode(3);
vector<Plaintext> bias1_plain(3);
for (int i=0; i<3; i++){
bias1_encode[i].resize(slot_count);
for (int j=0; j<slot_count; j++){
bias1_encode[i][j]=bias1[i][ j / 64];
}
encoder.encode(bias1_encode[i], scale, bias1_plain[i]);
}
vector<double> bias2_encode(slot_count);
Plaintext bias2_plain;
for (int i=0; i<slot_count; i++){
bias2_encode[i]=bias2[ i / 64];
}
encoder.encode(bias2_encode, scale, bias2_plain);
vector<vector<int>> index_set{xindex0,xindex1_0,xindex1_1,xindex2};
vector<double> mask(slot_count,0);
Plaintext mask_plain;
for (int i=0; i< slot_count ;i++){
if ( (i % 2) == 0 ){
mask[i]=1;
}
}
cout<<"print mask vector"<<endl;
print_vector(mask);
encoder.encode(mask,scale,mask_plain);
vector<Ciphertext> st_gcn0_output;
{
Stopwatch sw("Inference time for st-gcn layer 0");
/*start the inference for st-gcn layer 0*/
vector<vector<Ciphertext>> cts_conv1{cts,cts,cts};
vector<vector<Ciphertext>> cts_rotated(3);
vector<vector<Ciphertext>> cts_temp(3);
for (int k=0; k<3; k++){
cts_rotated[k].resize(dimension);
cts_temp[k].resize(dimension);
for(int i=0; i<dimension; i++){
evaluator.rotate_vector(cts_conv1[k][i], 64, galk, cts_rotated[k][i]);
parms_id_type last_parms_id = cts_conv1[k][i].parms_id();
evaluator.mod_switch_to_inplace(conv1_plain[k][0], last_parms_id);
evaluator.multiply_plain_inplace(cts_conv1[k][i],conv1_plain[k][0]);
//evaluator.rescale_to_next_inplace(cts_conv1[k][i]);
evaluator.mod_switch_to_inplace(conv1_plain[k][1], last_parms_id);
evaluator.multiply_plain_inplace(cts_rotated[k][i],conv1_plain[k][1]);
//evaluator.rescale_to_next_inplace(cts_rotated[k][i]);
evaluator.add(cts_conv1[k][i],cts_rotated[k][i],cts_temp[k][i]);
evaluator.rescale_to_next_inplace(cts_temp[k][i]);
// add bias1
cts_temp[k][i].scale() = scale;
parms_id_type temp_parms_id = cts_temp[k][i].parms_id();
evaluator.mod_switch_to_inplace(bias1_plain[k], temp_parms_id);
evaluator.add_plain_inplace(cts_temp[k][i], bias1_plain[k]);
}
}
cout<< "Decrypt the result after the conv1"<<endl;
Plaintext output_plain;
vector<double> output_featuremap;
decryptor.decrypt(cts_temp[0][0],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
decryptor.decrypt(cts_temp[0][1],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
decryptor.decrypt(cts_temp[0][2],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
/*times with sparse matrix */
vector<Ciphertext> cts_A(dimension);
vector<vector<Ciphertext>> cts_preA{cts_temp[0], cts_temp[1], cts_temp[1], cts_temp[2]};
//vector<vector<int>> index_set{xindex0,xindex1_0,xindex1_1,xindex2};
for(int i=0; i<dimension; i++){
vector<Ciphertext> matrix_temp;
for (int v=0; v<4; v++){
if( index_set[v][i]<16 ){
parms_id_type last_parms_id = cts_preA[v][index_set[v][i]].parms_id();
evaluator.mod_switch_to_inplace(A_plain[v][i], last_parms_id);
// {
// Stopwatch sw("plain multiply time test");
evaluator.multiply_plain_inplace(cts_preA[v][index_set[v][i]], A_plain[v][i]);
// }
matrix_temp.emplace_back(move(cts_preA[v][index_set[v][i]]));
}
}
//cout<<"matrix_temp size "<<matrix_temp.size()<<endl;
evaluator.add_many(matrix_temp, cts_A[i]);
evaluator.rescale_to_next_inplace(cts_A[i]);
//cout<<"scale after conv1 and mult A: "<<cts_A[i].scale()<<endl;
cts_A[i].scale()=scale;
parms_id_type parms_id_b1 = cts_A[i].parms_id();
evaluator.mod_switch_to_inplace(b1_plain, parms_id_b1);
evaluator.add_plain_inplace(cts_A[i],b1_plain);
// {
// Stopwatch sw("square time test");
evaluator.square_inplace(cts_A[i]);
evaluator.relinearize_inplace(cts_A[i],relin_keys);
evaluator.rescale_to_next_inplace(cts_A[i]);
//}
//cout<<"scale after bn 1: "<<cts_A[i].scale()<<endl;
}
//cout << "Noise budget = " << decryptor.invariant_noise_budget(cts_A[0]) << endl;
cout<< "Decrypt the result after the conv1, A , bn1, x^2"<<endl;
//Plaintext output_plain;
//vector<double> output_featuremap;
decryptor.decrypt(cts_A[0],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
decryptor.decrypt(cts_A[1],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
decryptor.decrypt(cts_A[2],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
/* second conv 9*1 layer & bn1 layer & bn2 layer & x^2 activation*/
vector<Ciphertext> cts_conv2(dimension);
vector<vector<Ciphertext>> conv2_rotated_intra(dimension);
vector<vector<vector<Ciphertext>>> conv2_temp(dimension);
vector<vector<Ciphertext>> conv2_rotated_extra(dimension);
for (int i=0; i< dimension; i++){
conv2_rotated_intra[i].resize(9);
for (int j=0; j<9; j++){
// {
// Stopwatch sw("rotation time for the convolutional");
// evaluator.rotate_vector(cts_A[i],(j-4),galk, conv2_rotated_intra[i][j]);
// }
evaluator.rotate_vector(cts_A[i], (j-4) , galk, conv2_rotated_intra[i][j]);
}
}
//full step algorithm
// for (int i=0; i< dimension; i++){
// conv2_rotated_intra[i].resize(9);
// for (int j=0; j<9; j++){
// // {
// // Stopwatch sw("rotation time for the convolutional");
// // evaluator.rotate_vector(cts_A[i],(j-4),galk, conv2_rotated_intra[i][j]);
// // }
// evaluator.rotate_vector(cts_A[i], (j-4) , galk, conv2_rotated_intra[i][j]);
// }
// }
cout<<"baby-step ciphertext rotation in advance end"<<endl;
for (int i=0; i<dimension; i++){
conv2_temp[i].resize(64);
conv2_rotated_extra[i].resize(64);
{
Stopwatch sw("computation time for one ciphertext");
for (int r=0; r < 64; r++){
conv2_temp[i][r].resize(9);
for (int j=0; j < 9; j++){
conv2_temp[i][r][j]=conv2_rotated_intra[i][j];
parms_id_type last_parms_id = conv2_temp[i][r][j].parms_id();
evaluator.mod_switch_to_inplace(conv2_plain[r][j], last_parms_id);
evaluator.multiply_plain_inplace(conv2_temp[i][r][j], conv2_plain[r][j]);
// {
// Stopwatch sw("multiplication time");
// evaluator.multiply_plain_inplace(conv2_temp[i][r][j], conv2_plain[r][j]);
// }
//evaluator.rescale_to_next_inplace(conv2_temp[i][r][j]);
}
// {
// Stopwatch sw("sum time");
// evaluator.add_many(conv2_temp[i][r], conv2_rotated_extra[i][r]);
// }
evaluator.add_many(conv2_temp[i][r], conv2_rotated_extra[i][r]);
evaluator.rotate_vector(conv2_rotated_extra[i][r], 64*r, galk, conv2_rotated_extra[i][r]);
// {
// Stopwatch sw("rotation time");
// evaluator.rotate_vector(conv2_rotated_extra[i][r], 64*r, galk, conv2_rotated_extra[i][r]);
// }
}
}
evaluator.add_many(conv2_rotated_extra[i], cts_conv2[i]);
evaluator.rescale_to_next_inplace(cts_conv2[i]);
cts_conv2[i].scale() = scale;
/*add bias2*/
parms_id_type temp_parms_id = cts_conv2[i].parms_id();
evaluator.mod_switch_to_inplace(bias2_plain, temp_parms_id);
evaluator.add_plain_inplace(cts_conv2[i], bias2_plain);
/*Relu Approximation*/
evaluator.square_inplace(cts_conv2[i]);
evaluator.relinearize_inplace(cts_conv2[i],relin_keys);
evaluator.rescale_to_next_inplace(cts_conv2[i]);
cts_conv2[i].scale()=scale;
//cout<<"scale after bn 2: "<<cts_conv2[i].scale()<<endl;
}
cout<< "Decrypt the result after the conv2, bn2, x^2"<<endl;
decryptor.decrypt(cts_conv2[0],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
decryptor.decrypt(cts_conv2[1],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
decryptor.decrypt(cts_conv2[2],output_plain);
encoder.decode(output_plain,output_featuremap);
print_vector(output_featuremap,128);
// decryptor.decrypt(cts_conv2[14],output_plain);
// encoder.decode(output_plain,output_featuremap);
// print_vector(output_featuremap,32);
// decryptor.decrypt(cts_conv2[15],output_plain);
// encoder.decode(output_plain,output_featuremap);
// print_vector(output_featuremap,32);
cout<<"st-gcn layer 0 computation finished"<<endl;
st_gcn0_output=cts_conv2;
//Save to see size
{
ofstream f("cts_stgcn0_0.ct", ios::binary);
cts_conv2[0].save(f);
}
{
ofstream f("cts_stgcn0_1.ct", ios::binary);
cts_conv2[1].save(f);
}
{
ofstream f("cts_stgcn0_2.ct", ios::binary);
cts_conv2[2].save(f);
}
{
ofstream f("cts_stgcn0_3.ct", ios::binary);
cts_conv2[3].save(f);
}
{
ofstream f("cts_stgcn0_4.ct", ios::binary);
cts_conv2[4].save(f);
}
{
ofstream f("cts_stgcn0_5.ct", ios::binary);
cts_conv2[5].save(f);
}
{
ofstream f("cts_stgcn0_6.ct", ios::binary);
cts_conv2[6].save(f);
}
{
ofstream f("cts_stgcn0_7.ct", ios::binary);
cts_conv2[7].save(f);
}
{
ofstream f("cts_stgcn0_8.ct", ios::binary);
cts_conv2[8].save(f);
}
{
ofstream f("cts_stgcn0_9.ct", ios::binary);
cts_conv2[9].save(f);
}
}
/*second st-gcn layer*/
vector<vector<double>> edge_importance1;
vector<vector<double>> conv1_1;
vector<vector<double>> bias1_1;
vector<double> gamma1_1;
vector<double> beta1_1;
vector<double> mean1_1;
vector<double> var1_1;
vector<vector<double>> conv2_1;
vector<double> bias2_1;
vector<double> gamma2_1;
vector<double> beta2_1;
vector<double> mean2_1;
vector<double> var2_1;
vector<vector<double>> conv_res1;
vector<double> bias_res1;
vector<double> gamma_res1;
vector<double> beta_res1;
vector<double> mean_res1;
vector<double> var_res1;
edge_importance1=read_params_2dim("./params/edge_importance.2.txt");
conv1_1=read_params_2dim("./params/st_gcn_networks.2.gcn.conv.weight.txt");
bias1_1=read_params_2dim("./params/st_gcn_networks.2.gcn.conv.bias.txt");
gamma1_1=read_params_1dim("./params/st_gcn_networks.2.tcn.0.weight.txt");
beta1_1=read_params_1dim("./params/st_gcn_networks.2.tcn.0.bias.txt");
mean1_1=read_params_1dim("./params/st_gcn_networks.2.tcn.0.running_mean.txt");
var1_1=read_params_1dim("./params/st_gcn_networks.2.tcn.0.running_var.txt");
conv2_1=read_params_2dim("./params/st_gcn_networks.2.tcn.2.weight.txt");
bias2_1=read_params_1dim("./params/st_gcn_networks.2.tcn.2.bias.txt");
gamma2_1=read_params_1dim("./params/st_gcn_networks.2.tcn.3.weight.txt");
beta2_1=read_params_1dim("./params/st_gcn_networks.2.tcn.3.bias.txt");
mean2_1=read_params_1dim("./params/st_gcn_networks.2.tcn.3.running_mean.txt");
var2_1=read_params_1dim("./params/st_gcn_networks.2.tcn.3.running_var.txt");
conv_res1=read_params_2dim("./params/st_gcn_networks.2.residual.0.weight.txt");
bias_res1=read_params_1dim("./params/st_gcn_networks.2.residual.0.bias.txt");
gamma_res1=read_params_1dim("./params/st_gcn_networks.2.residual.1.weight.txt");
beta_res1=read_params_1dim("./params/st_gcn_networks.2.residual.1.bias.txt");
mean_res1=read_params_1dim("./params/st_gcn_networks.2.residual.1.running_mean.txt");
var_res1=read_params_1dim("./params/st_gcn_networks.2.residual.1.running_var.txt");
vector<vector<vector<double>>> reshape_conv1_1;
vector<vector<vector<double>>> reshape_conv2_1;
reshape_conv1_1=reshape_2to3(3,conv1_1);
int output_channel_1=reshape_conv1_1[0].size();
reshape_conv2_1=reshape_2to3(output_channel_1,conv2_1);
for (int i=0; i<var1_1.size() ;i++){
var1_1[i]=sqrt(var1_1[i]);
var2_1[i]=sqrt(var2_1[i]);
var_res1[i]=sqrt(var_res1[i]);
}
cout<<"current outputchannel size :"<<output_channel_1<<endl;
/* the st-gcn layer 1 implementation*/
cout<<"conv1_1 weight layer size "<<reshape_conv1_1.size()<<" , "<<reshape_conv1_1[0].size()<<" , "<<reshape_conv1_1[0][0].size()<<endl;
cout<<"conv1_1 bias layer size "<<bias1_1.size()<<" , "<<bias1_1[0].size()<<endl;
cout<<"bn1_1 gamma1 layer size "<<gamma1_1.size()<<endl;
cout<<"bn1_1 beta1 layer size "<<beta1_1.size()<<endl;
cout<<"bn1_1 mean1 layer size "<<mean1_1.size()<<endl;
cout<<"bn1_1 var1 layer size "<<var1_1.size()<<endl;
cout<<"conv2_1 layer size "<<reshape_conv2_1.size()<<" , "<<reshape_conv2_1[0].size()<<" , "<<reshape_conv2_1[0][0].size()<<endl;
cout<<"conv2_1 bias layer size "<<bias2_1.size()<<endl;
cout<<"bn2_1 gamma1 layer size "<<gamma2_1.size()<<endl;
cout<<"bn2_1 beta1 layer size "<<beta2_1.size()<<endl;
cout<<"bn2_1 mean1 layer size "<<mean2_1.size()<<endl;
cout<<"bn2_1 var1 layer size "<<var2_1.size()<<endl;
/**/
cout<<"conv_res1 layer size "<<conv_res1.size()<<" , "<<conv_res1[0].size()<<endl;
cout<<"bias_res1 layer size "<<bias_res1.size()<<endl;
cout<<"gamma_res1 layer size "<<gamma_res1.size()<<endl;
cout<<"beta_res1 layer size "<<beta_res1.size()<<endl;
cout<<"mean_res1 layer size "<<mean_res1.size()<<endl;
cout<<"var_res1 layer size "<<var_res1.size()<<endl;
// init the square Matrxi A0, A1, A2
vector<vector<double>> A0_0(dimension);
vector<vector<double>> A1_0(dimension);
vector<vector<double>> A2_0(dimension);
for (int i = 0; i < dimension; i++){
A0_0[i].resize(dimension);
A1_0[i].resize(dimension);
A2_0[i].resize(dimension);
for (int j = 0; j < dimension; j++ ){
A0_0[i][j] = raw_A[i][j]*edge_importance1[i][j];
A1_0[i][j] = raw_A[i+25][j]*edge_importance1[i+25][j];
A2_0[i][j] = raw_A[i+50][j]*edge_importance1[i+50][j];
}
//print_vector(A0[i],3,7);
}
//encoding the matrix plaintexts for sparse matrix multiplication
vector<vector<double>> fpA0,fpA1_0,fpA1_1,fpA2; //16*4096
fpA0=matrix_encode(fpA0, A0_0, xindex0, yindex0, dimension, slot_count);
fpA1_0=matrix_encode(fpA1_0, A1_0, xindex1_0, yindex1_0, dimension, slot_count);
fpA1_1=matrix_encode(fpA1_0, A1_0, xindex1_1, yindex1_1, dimension, slot_count);
fpA2=matrix_encode(fpA2, A2_0, xindex2, yindex2, dimension, slot_count);
cout<<"The original matrix size "<<fpA0.size()<<" "<<fpA0[0].size()<<endl;
vector<vector<vector<double>>> fpA0m(16); //{fpA0, fpA0};
vector<vector<vector<double>>> fpA1_0m(16); //{fpA1_0, fpA1_0};
vector<vector<vector<double>>> fpA1_1m(16); //{fpA1_1, fpA1_1};
vector<vector<vector<double>>> fpA2m(16); //{fpA2, fpA2};