-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdecoder.h
1524 lines (1205 loc) · 54.6 KB
/
decoder.h
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
#ifndef DECODER_H
#define DECODER_H
#include <queue>
#include <vector>
#include <fstream>
#include <utility>
#include <float.h>
#include "fsa.hpp"
#include "format.h"
#include "memory_util.h"
#include "custom_kernels.h"
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
#include <thrust/system/cuda/execution_policy.h>
#include <thrust/system/cuda/memory.h>
//MARK:FSA related obj
template<typename dType>
class neuralMT_model;
//MARK: decoding related obj
//The decoder object type
template<typename dType>
struct dec_global_obj {
dType val;
dType score;
int beam_index;
int vocab_index;
state *s;
int viterbi_alignment;
dec_global_obj(dType _val,int _beam_index,int _vocab_index,int _viterbi_alignment) {
val = _val;
beam_index = _beam_index;
vocab_index = _vocab_index;
viterbi_alignment = _viterbi_alignment;
}
dec_global_obj(){}
bool operator==(const dec_global_obj &other) const
{
return (val == other.val
&& score == other.score
&& vocab_index == other.vocab_index
&& s->name == other.s->name );
}
};
template<typename dType>
struct dec_obj {
dType val;
dType score;
int vocab_index;
state *s;
int viterbi_alignment;
dec_obj(dType _val,int _vocab_index,int _viterbi_alignment) {
val = _val;
vocab_index = _vocab_index;
viterbi_alignment = _viterbi_alignment;
}
bool operator==(const dec_obj &other) const
{
return (val == other.val
&& score == other.score
&& vocab_index == other.vocab_index
&& s->name == other.s->name );
}
};
namespace std {
template<typename dType>
struct hash<dec_obj<dType>>
{
size_t operator()(const dec_obj<dType>& k) const
{
return (hash<float>()(k.val))
^ (hash<float>()(k.score) << 1)
^ (hash<int>()(k.vocab_index) << 2)
^ (hash<string>()(k.s->name) << 3) ;
}
};
template<typename dType>
struct hash<dec_global_obj<dType>>
{
size_t operator()(const dec_global_obj<dType>& k) const
{
// Compute individual hash values for first,
// second and third and combine them using XOR
// and bit shifting:
return (hash<float>()(k.val)) ^
(hash<float>()(k.score) << 1) ^
(hash<int>()(k.vocab_index) << 2) ^
(hash<string>()(k.s->name) << 3) ;
}
};
}
template<typename dType>
struct k_best {
dType score;
dType index;
k_best(dType _score,dType _index) {
score = _score;
index = _index;
}
};
template<typename dType>
struct eigen_mat_wrapper {
Eigen::Matrix<int, Eigen::Dynamic,1> hypothesis;
Eigen::Matrix<int, Eigen::Dynamic,1> viterbi_alignments;
// ct[0], ht[0], ct[1], ht[1]; just for one model;
std::vector<Eigen::Matrix<dType, Eigen::Dynamic, 1>> chts;
dType score; //log prob score along with a penalty
eigen_mat_wrapper(int size) {
hypothesis.resize(size);
viterbi_alignments.resize(size);
}
};
bool compare_pq(dec_obj<float> &a,dec_obj<float> &b)
{
return (a.val < b.val);
}
struct pq_compare_functor {
template<typename dType>
bool operator() (dec_obj<dType> &a,dec_obj<dType> &b) const { return (a.val < b.val); }
};
struct pq_global_compare_functor {
template<typename dType>
bool operator() (dec_global_obj<dType> &a,dec_global_obj<dType> &b) const { return (a.val < b.val); }
};
struct k_best_compare_functor {
template<typename dType>
bool operator() (k_best<dType> &a,k_best<dType> &b) const { return (a.score < b.score); }
};
template<typename dType>
struct decoder {
//global
int beam_size;
int vocab_size;
int start_symbol;
int end_symbol;
const int invalid_symbol = -1;
int max_decoding_length; //max size of a translation
dType min_decoding_ratio; //min size of a translation
int current_index; //The current length of the decoded target sentence
int num_hypotheses; //The number of hypotheses to be output for each translation
dType penalty;//penality term to encourage longer hypotheses, tune for bleu score
std::string output_file_name;
std::ofstream output;
bool print_score;
bool print_beam = false;
neuralMT_model<dType> *model = NULL;
//ThreadPool *pool;
//--- for fsa integration ---
float fsa_weight = 0.0;
fsa* fsa_model;
bool with_fsa = false;
bool with_fsa_compress = false;
bool fsa_can_prune = false;
bool fsa_log = true;
bool merge_state = true;
int invalid_number = 0;
bool end_transfer = false; // true if in fsa_line mode
//Timer
Timer timer;
// other weight
float alliteration_weight = 0.0;
float wordlen_weight = 0.0;
// for encourage list
bool encourage = false;
std::unordered_map<int,float> *encourage_list = NULL;
//float encourage_weight = 0;
std::unordered_map<std::string,int> tgt_mapping;
std::vector<state*> current_states;
std::priority_queue<dec_obj<dType>,std::vector<dec_obj<dType>>, pq_compare_functor> pq;
std::priority_queue<dec_global_obj<dType>,std::vector<dec_global_obj<dType>>, pq_global_compare_functor> pq_global;
std::priority_queue<dec_global_obj<dType>,std::vector<dec_global_obj<dType>>, pq_global_compare_functor> pqg; // used in expand_pq_global_gpu
std::unordered_set<dec_obj<dType>> pq_set;
std::unordered_set<dec_global_obj<dType>> pq_global_set;
std::unordered_set<dec_global_obj<dType>> pqg_set;
std::vector<eigen_mat_wrapper<dType>> hypotheses; //Stores all hypotheses
//CPU
Eigen::Matrix<int, Eigen::Dynamic,1> current_indices; //stores the current indicies for the beam size
//Size of (beam size)x(beam size)
//Each row is the indicies for one old hypothesis
//Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> top_words;
//size (beam size)x(max decoder length)
Eigen::Matrix<int,Eigen::Dynamic, Eigen::Dynamic> top_sentences;
Eigen::Matrix<int,Eigen::Dynamic, Eigen::Dynamic> top_sentences_temp; //Temp to copy old ones into this
//size (beam size)x(max decoder length)
Eigen::Matrix<int,Eigen::Dynamic, Eigen::Dynamic> top_sentences_viterbi;
Eigen::Matrix<int,Eigen::Dynamic, Eigen::Dynamic> top_sentences_temp_viterbi; //Temp to copy old ones into this
//size (beam size)x1, score are stored as log probabilities
Eigen::Matrix<dType,Eigen::Dynamic, 1> top_sentences_scores;
Eigen::Matrix<dType,Eigen::Dynamic, 1> top_sentences_scores_temp;
Eigen::Matrix<int,Eigen::Dynamic, 1> new_indicies_changes; //used to swap around hidden and cell states based on new beam results
//for repeat_penalty
bool penalize_repeat = true; // always true, but controlled by the weights;
precision repeat_penalty = 0.0;
float interactive_repeat_penalty;
precision adjacent_repeat_penalty = 0.0;
std::vector<std::unordered_map<int,int>> sentence_sets;
std::vector<std::unordered_map<int,int>> temp_sentence_sets;
//for source_length
int source_length = 0;
//GPU
int *h_current_indices;
int *d_current_indices;
// for vocab_shrink_2
int *h_current_indices_original;
int target_vocab_policy = 0;
dType *h_outputdist; // [vocab_size, beam_size] point to models[0].h_outputdist;
dType *d_outputdist; // need to allocate;
dType *d_outputdist_topk; // [vocab_size, beam_size] need to allocate;
dType *h_outputdist_topk; // need to allocate;
// to save vocab_index
int *d_dict; // [vocab_size, beam_size] need to allocate;
int *h_dict; // need to allocate;
// to save pointer
int *d_pointers;
int *h_pointers;
// to save beam_index
int *d_beams;
int *h_beams;
// to save valid_vocab_size;
int *d_valid_vocab_sizes;
int *h_valid_vocab_sizes;
// to save top_sentence_score;
dType *d_sentence_scores;
dType *h_sentence_scores;
// to save wordlen
int *d_wordlen; //[vocab_size]
int *h_wordlen;
// to save alliteration information, same word would have save bin number;
int *d_vocab_bin; // [vocab_size]
int *h_vocab_bin;
// to save repeat informaiton;
int *d_sentence_set; // beam_size * max_decoding_length * 3 [vocab, beam_index, occur_times]
int *h_sentence_set;
// to save encourange list
dType *h_encourage;
dType *d_encourage;
std::vector<cudaStream_t> streams;
decoder(int beam_size,int vocab_size,int start_symbol,int end_symbol,int max_decoding_length,dType min_decoding_ratio,
dType penalty,std::string output_file_name,int num_hypotheses,bool print_score, global_params ¶ms)
{
this->beam_size = beam_size;
this->vocab_size = vocab_size;
this->start_symbol = start_symbol;
this->end_symbol = end_symbol;
this->max_decoding_length = max_decoding_length;
this->min_decoding_ratio = min_decoding_ratio;
this->penalty = penalty;
this->repeat_penalty = params.repeat_penalty;
this->adjacent_repeat_penalty = params.adjacent_repeat_penalty;
this->wordlen_weight = params.wordlen_weight;
this->alliteration_weight = params.alliteration_weight;
this->output_file_name = output_file_name;
this->num_hypotheses = num_hypotheses;
this->print_score = print_score;
BZ_CUDA::logger << "Output file name for decoder: " << output_file_name << "\n";
output.open(output_file_name.c_str());
current_indices.resize(beam_size);
//top_words.resize(beam_size,beam_size);
top_sentences.resize(beam_size,max_decoding_length);
top_sentences_temp.resize(beam_size,max_decoding_length);
top_sentences_viterbi.resize(beam_size,max_decoding_length);
top_sentences_temp_viterbi.resize(beam_size,max_decoding_length);
top_sentences_scores.resize(beam_size);
top_sentences_scores_temp.resize(beam_size);
new_indicies_changes.resize(beam_size);
h_current_indices = (int *)malloc(beam_size*1*sizeof(int));
cudaMalloc((void**)&d_current_indices,beam_size*1*sizeof(int));//put void**
// repeat_penalty
for (int i = 0; i<beam_size; i++){
std::unordered_map<int,int>* sentence_set = new std::unordered_map<int,int>();
sentence_sets.push_back(*sentence_set);
temp_sentence_sets.push_back(*sentence_set);
}
// fsa
this->fsa_model = NULL;
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_dict, vocab_size*beam_size*1*sizeof(int)),"d_dict allocation failed\n");
h_dict = (int *)malloc(vocab_size*beam_size*1*sizeof(int));
//CUDA_ERROR_WRAPPER(cudaHostRegister(h_dict, vocab_size * beam_size * sizeof(int), cudaHostRegisterPortable),"h_dict pinned memeory error!");
// d_pointers
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_pointers, vocab_size*beam_size*1*sizeof(int)),"d_pointers allocation failed\n");
h_pointers = (int *)malloc(vocab_size*beam_size*1*sizeof(int));
//CUDA_ERROR_WRAPPER(cudaHostRegister(h_pointers, vocab_size * beam_size * sizeof(int), cudaHostRegisterPortable),"h_pointers pinned memeory error!");
// d_beams
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_beams, vocab_size*beam_size*1*sizeof(int)),"d_beams allocation failed\n");
h_beams = (int *)malloc(vocab_size*beam_size*1*sizeof(int));
//CUDA_ERROR_WRAPPER(cudaHostRegister(h_beams, vocab_size * beam_size * sizeof(int), cudaHostRegisterPortable),"h_beams pinned memeory error!");
// d_valid_vocab_sizes
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_valid_vocab_sizes, (beam_size+1)*1*sizeof(int)),"d_valid_vocab_sizes allocation failed\n");
h_valid_vocab_sizes = (int *)malloc((beam_size+1)*1*sizeof(int));
//CUDA_ERROR_WRAPPER(cudaHostRegister(h_valid_vocab_sizes, (beam_size+1) * sizeof(int), cudaHostRegisterPortable),"h_valid_vocab_sizes pinned memeory error!");
// d_sentence_scores
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_sentence_scores, beam_size*sizeof(dType)),"d_sentence_scores in decoder allocation failed\n");
h_sentence_scores = (dType *)malloc(beam_size*1*sizeof(dType));
// d_outputdist
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_outputdist, beam_size*vocab_size*sizeof(dType)),"d_outputdist in decoder allocation failed\n");
// d_outputdist_topk
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_outputdist_topk, vocab_size*beam_size*sizeof(dType)),"d_outputdist_topk in decoder allocation failed\n");
h_outputdist_topk = (dType *)malloc(vocab_size*beam_size*1*sizeof(dType));
//CUDA_ERROR_WRAPPER(cudaHostRegister(h_outputdist_topk, vocab_size * beam_size * sizeof(dType), cudaHostRegisterPortable),"h_outputdist_topk pinned memeory error!");
// d_wordlen
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_wordlen, vocab_size*1*sizeof(int)),"d_wordlen allocation failed\n");
h_wordlen = (int *)malloc(vocab_size*1*sizeof(int));
// d_vocab_bin
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_vocab_bin, vocab_size*1*sizeof(int)),"d_vocab_bin allocation failed\n");
h_vocab_bin = (int *)malloc(vocab_size*1*sizeof(int));
// d_sentence_set
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_sentence_set, beam_size * max_decoding_length*3*sizeof(int)),"d_sentence_set allocation failed\n");
h_sentence_set = (int *)malloc(beam_size * max_decoding_length*3*sizeof(int));
// d_encourage
CUDA_ERROR_WRAPPER(cudaMalloc((void**)&d_encourage, vocab_size*1*sizeof(dType)),"d_encourage allocation failed\n");
h_encourage = (dType *)malloc(vocab_size*1*sizeof(dType));
// for cuda streams at least 10 beams;
for (int i=0; i < std::max(beam_size,10); i++){
cudaStream_t stream;
cudaStreamCreate(&stream);
streams.push_back(stream);
}
// for target_vocab_shrink_policy
this->target_vocab_policy = params.target_vocab_policy;
if (this->target_vocab_policy == 2){
this->h_current_indices_original = (int*) malloc(beam_size * sizeof(int));
}
}
~decoder() {
output.close();
free(h_current_indices);
cudaFree(d_outputdist);
free(h_outputdist_topk);
cudaFree(d_outputdist_topk);
free(h_dict);
cudaFree(d_dict);
free(h_pointers);
cudaFree(d_pointers);
free(h_beams);
cudaFree(d_beams);
free(h_valid_vocab_sizes);
cudaFree(d_valid_vocab_sizes);
free(h_sentence_scores);
cudaFree(d_sentence_scores);
free(h_wordlen);
cudaFree(d_wordlen);
free(h_vocab_bin);
cudaFree(d_vocab_bin);
free(h_sentence_set);
cudaFree(d_sentence_set);
free(h_encourage);
cudaFree(d_encourage);
//for streams
for (int i=0; i < std::max(beam_size,10); i++){
cudaStreamDestroy(streams[i]);
}
if (this->target_vocab_policy == 2){
free(h_current_indices_original);
}
}
// for encourage list
void init_encourage_lists(std::vector<std::string> fns, std::vector<float> weights){
// even though len(fns) == 0, we still init h_encourage and d_encourage;
for (int i = 0; i < vocab_size; i ++ ){
h_encourage[i] = 0.0;
}
for (int i = 0; i< fns.size(); i++){
std::string encourage_file = fns[i];
float weight = weights[i];
if (i == 0){
this->init_encourage_list(encourage_file, weight);
} else {
this->init_encourage_list_additional(encourage_file, weight);
}
}
CUDA_ERROR_WRAPPER(cudaMemcpy(d_encourage, h_encourage,
vocab_size*sizeof(dType),
cudaMemcpyHostToDevice),
" h_encourage to d_encourage\n");
}
void init_encourage_list(std::string fn, float weight){
// should call after init_fsa();
if (fn == ""){
encourage = false;
//encourage_weight = 0;
if (this->encourage_list != NULL){
delete this->encourage_list;
this->encourage_list = NULL;
}
return;
}
encourage = true;
//encourage_weight = weight;
if (this->encourage_list != NULL){
delete this->encourage_list;
}
this->encourage_list = new std::unordered_map<int,float>();
std::ifstream fin(fn.c_str());
std::string line;
while(std::getline(fin,line)){
int index = 2 ; // <UNK>
if (this->tgt_mapping.count(line) > 0){
index = this->tgt_mapping[line];
}
if (index != 2){
(*(this->encourage_list))[index] = weight;
h_encourage[index] = weight;
}
}
fin.close();
BZ_CUDA::logger<< "Encourage Weight: " << weight <<"\n";
BZ_CUDA::logger<< "Encourage List Size: " <<(int)(encourage_list->size()) <<"\n";
}
void init_encourage_list_additional(std::string fn, float weight){
// if there's more than one encourage list, use this function to init the encourage_lists except the first one.
std::ifstream fin(fn.c_str());
std::string line;
while(std::getline(fin,line)){
int index = 2 ; // <UNK>
if (this->tgt_mapping.count(line) > 0){
index = this->tgt_mapping[line];
}
if (index != 2){
if (this->encourage_list->count(index) == 0){
(*(this->encourage_list))[index] = weight;
} else {
(*(this->encourage_list))[index] += weight;
}
h_encourage[index] += weight;
}
}
fin.close();
BZ_CUDA::logger<< "Encourage Weight: "<< weight <<"\n";
BZ_CUDA::logger<< "Encourage List Size: "<<(int)(encourage_list->size())<<"\n";
}
// for single fsa file
void init_fsa_inner(global_params ¶ms){
this->fsa_weight = params.fsa_weight;
this->fsa_log = params.fsa_log;
this->with_fsa = true;
if (this->fsa_weight >=0){
// also, the log(weight) on fsa's edge should < 0.0;
this->fsa_can_prune = true;
}
}
void init_fsa(fsa *fsa_model, std::unordered_map<std::string,int> &tgt_mapping,global_params ¶ms){
this->init_fsa_inner(params);
this->tgt_mapping = tgt_mapping;
this->fsa_model = fsa_model;
if (!params.interactive && !params.interactive_line){ // if it's interactive, this fsa_model is non_valid
this->load_fsa_model();
}
std::unordered_map<std::string, int> bins;
int bin_index =0 ;
// prepare vocab_bin and wordlen
for (const auto & item : tgt_mapping){
std::string word = item.first;
std::string key = std::string(1,word[0]);
int vocab_index = item.second;
h_wordlen[vocab_index] = word.size();
if (bins.count(key) == 0){
bins[key] = bin_index;
bin_index += 1;
}
h_vocab_bin[vocab_index] = bins[key];
}
CUDA_ERROR_WRAPPER(cudaMemcpy(d_wordlen, h_wordlen,
vocab_size*sizeof(int),
cudaMemcpyHostToDevice),
" h_wordlen to d_wordlen\n");
CUDA_ERROR_WRAPPER(cudaMemcpy(d_vocab_bin, h_vocab_bin,
vocab_size*sizeof(int),
cudaMemcpyHostToDevice),
" h_vocab_bin to d_vocab_bin\n");
}
void load_fsa_model()
{
this->fsa_model->convert_name_to_index(this->tgt_mapping);
this->fsa_model->log_space = this->fsa_log;
this->fsa_model->load_fsa();
}
void init_fsa_interactive(std::string fn_fsa){
if (this->fsa_model != NULL){
delete this->fsa_model;
this->fsa_model = NULL;
}
this->fsa_model = new fsa(fn_fsa);
this->load_fsa_model();
}
void empty_queue_pq(std::priority_queue<dec_obj<dType>,std::vector<dec_obj<dType>>, pq_compare_functor> &pq, std::unordered_set<dec_obj<dType>> &pq_set) {
while(!pq.empty()) {
pq.pop();
}
if (merge_state){
pq_set.clear();
}
}
void empty_queue_pq() {
while(!pq.empty()) {
pq.pop();
}
if (merge_state){
pq_set.clear();
}
}
void empty_queue_global(std::priority_queue<dec_global_obj<dType>,std::vector<dec_global_obj<dType>>, pq_global_compare_functor> &pq, std::unordered_set<dec_global_obj<dType>> &pq_set) {
while(!pq.empty()) {
pq.pop();
}
if (merge_state){
pq_set.clear();
}
}
void empty_queue_global() {
while(!pq_global.empty()) {
pq_global.pop();
}
if (merge_state){
pq_global_set.clear();
}
}
template<typename Derived>
void finish_current_hypotheses(const Eigen::MatrixBase<Derived> &outputDist,std::vector<int> &viterbi_alignments) {
if (this->with_fsa){
for(int i=0; i<beam_size; i++) {
int symbol = this->current_indices(i);
if (symbol == this->invalid_symbol){
continue;
}
state* istate = this->current_states[i];
std::vector<sw> sws;
this->fsa_model->next_states(istate,end_symbol,sws);
for (auto const & s: sws){
if ( s.s->name == this->fsa_model->end_state->name){
// here start_symbol means it didn't end naturally.
top_sentences(i,current_index+1) = start_symbol;
dType base_score = std::log(outputDist(end_symbol,i));
dType fsa_score = 0.0;
fsa_score = this->fsa_weight * s.weight;
base_score += fsa_score;
if (!end_transfer){
top_sentences_scores(i) += base_score + penalty;
}
hypotheses.push_back(eigen_mat_wrapper<dType>(current_index+2));
hypotheses.back().hypothesis = top_sentences.block(i,0,1,current_index+2).transpose();//.row(temp.beam_index);
hypotheses.back().viterbi_alignments = top_sentences_viterbi.block(i,0,1,current_index+2).transpose();
hypotheses.back().viterbi_alignments(current_index+1) = viterbi_alignments[i];
hypotheses.back().score = top_sentences_scores(i);
if (end_transfer){
// [HERE]
model->get_chts(hypotheses.back().chts, i, beam_size);
}
break;
}
}
}
} else{
for(int i=0; i<beam_size; i++) {
int symbol = this->current_indices(i);
if (symbol == this->invalid_symbol){
continue;
}
top_sentences(i,current_index+1) = end_symbol;
top_sentences_scores(i) += std::log(outputDist(1,i)) + penalty;
hypotheses.push_back(eigen_mat_wrapper<dType>(current_index+2));
hypotheses.back().hypothesis = top_sentences.block(i,0,1,current_index+2).transpose();//.row(temp.beam_index);
hypotheses.back().viterbi_alignments = top_sentences_viterbi.block(i,0,1,current_index+2).transpose();
hypotheses.back().viterbi_alignments(current_index+1) = viterbi_alignments[i];
hypotheses.back().score = top_sentences_scores(i);
}
}
current_index+=1;
}
// expand_hypothesis
template<typename Derived>
void expand_hypothesis(const Eigen::MatrixBase<Derived> &outputDist,int index,std::vector<int> &viterbi_alignments, dType *h_outputdist) {
this->h_outputdist = h_outputdist;
if (this->with_fsa){
expand_hypothesis_with_fsa(outputDist,index, viterbi_alignments);
} else {
expand_hypothesis_without_fsa(outputDist,index, viterbi_alignments);
}
}
template<typename Derived>
void print_matrix(Derived *mat, int size,std::string name){
std::cout<<name<<"\n";
for (int i = 0; i< size; i++){
std:: cout << mat[i] << " ";
}
std:: cout<< "\n";
}
template<typename Derived>
void expand_hypothesis_with_fsa(const Eigen::MatrixBase<Derived> &outputDist,int index,std::vector<int> &viterbi_alignments) {
///timer.clear();
///timer.start("expand_with_fsa");
// viterbi_alignments check
if(viterbi_alignments.size()!=0 && viterbi_alignments.size()!=beam_size) {
BZ_CUDA::logger << "VITERBI ALIGNMENT ERROR\n";
exit (EXIT_FAILURE);
}
if(viterbi_alignments.size()==0) {
for(int i=0; i<beam_size; i++) {
viterbi_alignments.push_back(-1);
}
}
int cols=outputDist.cols();
if(true){ // gpu expand
if(index==0) {
cols = 1;
} else {
cols = 0;
for (int i = 0; i <beam_size; i++){
int symbol = this->current_indices(i);
if (symbol == this->invalid_symbol){
break;
}
cols += 1;
}
}
std::cout<< "cols " << cols << "\n";
this->expand_pq_global_gpu(pqg,pqg_set, viterbi_alignments, cols);
this->invalid_number = 0;
empty_queue_global();
while(!pqg.empty()) {
dec_global_obj<dType> temp = pqg.top();
pqg.pop();
temp.val = -temp.val;
if (merge_state){
if (pq_global_set.count(temp) == 0){
pq_global.push(temp);
pq_global_set.insert(temp);
}
}
else {
pq_global.push( temp );
}
}
}
// filter the pq_global
// so that dec_global_obj in pq_global has unique (history, vocab_index, state.name)
// this is necessary: if two dec_global_obj have the same (history, vocab_index, state.name)
// then they will have the same future, but different history. If we don't merge this, the whole beam will be occuped by
// dec_global_obj with same (history, vocab_index, state.name).
///timer.start("filter");
std::cout<<"before: "<<pq_global.size()<<"\n";
int hash_index = 0;
std::unordered_map<std::string,int> history_to_hash;
std::unordered_map<int,int> beam_index_to_hash;
for(int i=0; i<cols; i++) {
std::string history = "";
for (int j = 0; j<= current_index; j++){
history += std::to_string(top_sentences(i,j)) + " ";
}
if (history_to_hash.count(history) == 0){
history_to_hash[history] = hash_index;
//std::cout <<"history: " <<i << " "<< history << " " <<hash_index << "\n";
hash_index += 1;
}
beam_index_to_hash[i] = history_to_hash[history];
}
std::unordered_map<std::string,dec_global_obj<dType>> filtered_queue;
while (!pq_global.empty()){
dec_global_obj<dType> dgobj = pq_global.top();
pq_global.pop();
std::string key = "";
key += std::to_string(beam_index_to_hash[dgobj.beam_index])+" ";
key += std::to_string(dgobj.vocab_index) + " ";
key += dgobj.s->name;
//BZ_CUDA::logger<<"key "<<key<<"\n";
if (filtered_queue.count(key) == 0){
filtered_queue[key] = dgobj;
} else {
dec_global_obj<dType> old_dgobj = filtered_queue[key];
//std::cout << "[-----]" << key << " "<< dgobj.val << " " << old_dgobj.val <<"\n";
if (dgobj.val > old_dgobj.val){
filtered_queue[key] = dgobj;
}
}
}
for (auto const & item: filtered_queue){
pq_global.push(item.second);
}
std::cout<<"after: "<<pq_global.size()<<"\n";
///timer.end("filter");
//Now have global heap with (beam size*beam size) elements
//Go through until (beam size) new hypotheses.
if (print_beam){
BZ_CUDA::logger<<"---------"<<index<<"------------\n";
}
if (pq_global.size() == 0){
this->invalid_number = this->beam_size;
}
///timer.start("for_loop_2");
int i = 0;
while(i < beam_size) {
if (pq_global.size() == 0)
{
//grow the sentences in the beam;
top_sentences_temp.row(i) = top_sentences.row(0);
top_sentences_temp(i,current_index+1) = start_symbol;
// vertibi
top_sentences_temp_viterbi.row(i) = top_sentences_viterbi.row(0);
//top_sentences_temp_viterbi(i,current_index+1);
// for current indices
current_indices(i) = invalid_symbol;
new_indicies_changes(i) = 0;
// sentence score
top_sentences_scores_temp(i) = 0;
// current state
current_states[i] = this->fsa_model->end_state;
i++;
}
else {
dec_global_obj<dType> temp = pq_global.top();
pq_global.pop();
//BZ_CUDA::logger <<"Value: " << temp.val << " Index: " << temp.vocab_index << " Beam: "<< temp.beam_index << "\n\n";
if(temp.vocab_index!=start_symbol) {
if(temp.vocab_index==end_symbol) {
if (print_beam){
BZ_CUDA::logger<<"[*]Cell:"<<i<<"\tF-Cell:"<<temp.beam_index<<"\tState:"<<temp.s->name<<"\tWord:"<<this->fsa_model->index2words[temp.vocab_index]<<"["<<temp.vocab_index<<"]"<<"\tScore:"<<temp.val<<"\n";
}
hypotheses.push_back(eigen_mat_wrapper<dType>(current_index+2));
hypotheses.back().hypothesis = top_sentences.block(temp.beam_index,0,1,current_index+2).transpose();//.row(temp.beam_index);
hypotheses.back().hypothesis(current_index+1) = end_symbol;
//viterbi
hypotheses.back().viterbi_alignments = top_sentences_viterbi.block(temp.beam_index,0,1,current_index+2).transpose();//.row(temp.beam_index);
hypotheses.back().viterbi_alignments(current_index+1) = temp.viterbi_alignment;
hypotheses.back().score = temp.score + penalty;
if (end_transfer){
hypotheses.back().score = temp.score - outputDist(end_symbol,temp.beam_index);
}
if (end_transfer){
// [HERE]
model->get_chts(hypotheses.back().chts, temp.beam_index, beam_size);
}
}
else {
if (print_beam){
BZ_CUDA::logger<<"Cell:"<<i<<"\tF-Cell:"<<temp.beam_index<<"\tState:"<<temp.s->name<<"\tWord:"<<this->fsa_model->index2words[temp.vocab_index]<<"["<<temp.vocab_index<<"]"<<"\tScore:"<<temp.val<<" "<<temp.score<<"\n";
}
top_sentences_temp.row(i) = top_sentences.row(temp.beam_index);
top_sentences_temp(i,current_index+1) = temp.vocab_index;
// vertibi
top_sentences_temp_viterbi.row(i) = top_sentences_viterbi.row(temp.beam_index);
top_sentences_temp_viterbi(i,current_index+1) = temp.viterbi_alignment;
// repeat penalty
if (penalize_repeat){
temp_sentence_sets[i] = sentence_sets[temp.beam_index];
if (temp_sentence_sets[i].count(temp.vocab_index) == 0){
temp_sentence_sets[i][temp.vocab_index] = 0;
}
temp_sentence_sets[i][temp.vocab_index] += 1;
}
current_indices(i) = temp.vocab_index;
new_indicies_changes(i) = temp.beam_index;
top_sentences_scores_temp(i) = temp.score + penalty;
current_states[i] = temp.s;
i++;
}
}
}
}
///timer.end("for_loop_2");
top_sentences = top_sentences_temp;
top_sentences_scores = top_sentences_scores_temp;
if (penalize_repeat){
sentence_sets = temp_sentence_sets;
}
current_index += 1;
for(int i=0; i<beam_size; i++) {
h_current_indices[i] = current_indices(i);
}
///timer.end("expand_with_fsa");
///timer.report();
/*
total_end= std::chrono::system_clock::now();
total_dur = total_end - total_start;
BZ_CUDA::logger<<"Epq: "<<epq_dur.count()<<"s \n";
BZ_CUDA::logger<<"Expand: "<<expand_dur.count()<<"s \n";
BZ_CUDA::logger<<"total: "<<total_dur.count()<<"s \n";
*/
//cudaMemcpy(d_current_indices,h_current_indices,beam_size*1*sizeof(int),cudaMemcpyHostToDevice);
}
//template<typename Derived>
void expand_pq_global_gpu(
std::priority_queue<dec_global_obj<dType>,std::vector<dec_global_obj<dType>>, pq_global_compare_functor> & pqg, std::unordered_set<dec_global_obj<dType>>& pqg_set, std::vector<int> &viterbi_alignments, int cols){
empty_queue_global(pqg,pqg_set);
if (cols <=0){
return;
}