forked from mozilla/translations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
806 lines (707 loc) · 33 KB
/
Snakefile
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
import yaml
import os
from snakemake.utils import min_version
from pipeline.bicleaner import packs
min_version("6.6.1")
# `include` directive is not supported by Pycharm plugin, moving all rules to one file to enable live checks
# https://github.com/JetBrains-Research/snakecharm/issues/195
### configuration
container: 'Singularity.sif'
install_deps = config['deps'] == 'true'
data_root_dir = config['root']
cuda_dir = config['cuda']
cudnn_dir = config['cudnn']
gpus_num = config['numgpus']
# marian occupies all GPUs on a machine if `gpus` are not specified
gpus = config['gpus'] if config['gpus'] else ' '.join([str(n) for n in range(int(gpus_num))])
workspace = config['workspace']
marian_cmake = config['mariancmake']
# experiment
src = config['experiment']['src']
trg = config['experiment']['trg']
experiment = config['experiment']['name']
mono_max_sent_src = config['experiment']['mono-max-sentences-src']
mono_max_sent_trg = config['experiment']['mono-max-sentences-trg']
bicl_default_threshold = config['experiment']['bicleaner']['default-threshold']
bicl_dataset_thresholds = config['experiment']['bicleaner']['dataset-thresholds']
backward_pretrained = config['experiment']['backward-model']
vocab_pretrained = config['experiment']['vocab']
experiment_dir=f"{data_root_dir}/experiments/{src}-{trg}/{experiment}"
# override marian cofings
marian_args = {name: ' '.join([f'--{k} {v}' for k,v in conf.items() ])
for name, conf in config['marian-args'].items()}
# datasets
train_datasets = config['datasets']['train']
valid_datasets = config['datasets']['devtest']
eval_datasets = config['datasets']['test']
mono_src_datasets = config['datasets']['mono-src']
mono_trg_datasets = config['datasets']['mono-trg']
mono_datasets = {src: mono_src_datasets, trg: mono_trg_datasets}
mono_max_sent = {src: mono_max_sent_src, trg: mono_max_sent_trg}
# parallelization
ensemble = list(range(config['experiment']['teacher-ensemble']))
split_length = config['experiment']['split-length']
# logging
log_dir = f"{data_root_dir}/logs/{src}-{trg}/{experiment}"
reports_dir = f"{data_root_dir}/reports/{src}-{trg}/{experiment}"
# binaries
cwd = os.getcwd()
third_party_dir = f'{cwd}/3rd_party'
marian_dir = f'{third_party_dir}/marian-dev/build'
bmt_marian_dir = f'{third_party_dir}/browsermt-marian-dev/build'
trainer = f'{marian_dir}/marian'
decoder = f'{marian_dir}/marian-decoder'
scorer = f'{marian_dir}/marian-scorer'
spm_encoder = f'{marian_dir}/spm_encode'
spm_trainer = f'{marian_dir}/spm_train'
spm_exporter = f'{marian_dir}/spm_export_vocab'
bmt_decoder = f'{bmt_marian_dir}/marian-decoder'
bmt_converter = f'{bmt_marian_dir}/marian-conv'
kenlm = f'{third_party_dir}/kenlm'
fast_align_build = f'{third_party_dir}/fast_align/build'
extract_lex_build = f'{third_party_dir}/extract-lex/build'
preprocess_build_dir=f'{third_party_dir}/preprocess/build'
bin = f'{cwd}/bin'
deduper = f'{cwd}/bin/dedupe'
# data
data_dir = f"{data_root_dir}/data/{src}-{trg}/{experiment}"
clean = f"{data_dir}/clean"
biclean = f"{data_dir}/biclean"
cache_dir = f"{data_dir}/cache"
original = f"{data_dir}/original"
translated = f"{data_dir}/translated"
augmented = f"{data_dir}/augmented"
merged = f"{data_dir}/merged"
filtered = f'{data_dir}/filtered'
align_dir = f"{data_dir}/alignment"
# models
models_dir = f"{data_root_dir}/models/{src}-{trg}/{experiment}"
teacher_base_dir = f"{models_dir}/teacher-base"
teacher_finetuned_dir = f"{models_dir}/teacher-finetuned"
student_dir = f"{models_dir}/student"
student_finetuned_dir = f"{models_dir}/student-finetuned"
speed_dir = f"{models_dir}/speed"
exported_dir = f"{models_dir}/exported"
best_model = f"model.npz.best-{config['experiment']['best-model']}.npz"
backward_dir = f'{models_dir}/backward'
spm_sample_size=config['experiment']['spm-sample-size']
vocab_path=vocab_pretrained or f"{models_dir}/vocab/vocab.spm"
#evaluation
eval_data_dir = f"{original}/eval"
eval_res_dir = f"{models_dir}/evaluation"
eval_backward_dir = f'{eval_res_dir}/backward'
eval_student_dir = f'{eval_res_dir}/student'
eval_student_finetuned_dir = f'{eval_res_dir}/student-finetuned'
eval_speed_dir = f'{eval_res_dir}/speed'
eval_teacher_ens_dir = f'{eval_res_dir}/teacher-ensemble'
# set common environment variables
envs = f'''SRC={src} TRG={trg} MARIAN="{marian_dir}" BMT_MARIAN="{bmt_marian_dir}" GPUS="{gpus}" WORKSPACE={workspace} \
BIN="{bin}" CUDA_DIR="{cuda_dir}" CUDNN_DIR="{cudnn_dir}" '''
# CUDA_VISIBLE_DEVICES is used by bicleaner ai. slurm sets this variable
# it can be overriden manually by 'gpus' config setting to split GPUs in local mode
if config['gpus']:
envs += f' CUDA_VISIBLE_DEVICES="{gpus}" '
### workflow options
results = [f'{exported_dir}/model.{src}{trg}.intgemm.alphas.bin.gz',
f'{exported_dir}/lex.50.50.{src}{trg}.s2t.bin.gz',
f'{exported_dir}/vocab.{src}{trg}.spm.gz',
f'{experiment_dir}/config.yml',
*expand(f'{eval_res_dir}/teacher-base{{ens}}/{{dataset}}.metrics',ens=ensemble, dataset=eval_datasets),
*expand(f'{eval_student_dir}/{{dataset}}.metrics', dataset=eval_datasets),
*expand(f'{eval_student_finetuned_dir}/{{dataset}}.metrics', dataset=eval_datasets),
*expand(f'{eval_speed_dir}/{{dataset}}.metrics', dataset=eval_datasets)
]
if len(ensemble) > 1:
results.extend(expand(f'{eval_teacher_ens_dir}/{{dataset}}.metrics', dataset=eval_datasets))
if install_deps:
results.append("/tmp/flags/setup.done")
if not backward_pretrained:
# don't evaluate pretrained model
results.extend(expand(f'{eval_backward_dir}/{{dataset}}.metrics',dataset=eval_datasets))
do_train_backward=True
else:
do_train_backward = False
backward_dir = backward_pretrained
# bicleaner
bicleaner_type = packs.find(src, trg)
bicleaner_env = "envs/bicleaner-ai.yml" if bicleaner_type == 'bicleaner-ai' else 'envs/bicleaner.yml'
if bicleaner_type:
clean_corpus_prefix = f'{biclean}/corpus'
teacher_corpus = f'{biclean}/corpus'
use_bicleaner = True
else:
clean_corpus_prefix = f'{clean}/corpus'
teacher_corpus = f'{clean}/corpus'
use_bicleaner = False
clean_corpus_src = f'{clean_corpus_prefix}.{src}.gz'
clean_corpus_trg = f'{clean_corpus_prefix}.{trg}.gz'
# augmentation
if mono_trg_datasets:
teacher_corpus = f'{augmented}/corpus'
augment_corpus = True
final_teacher_dir = teacher_finetuned_dir
results.extend(expand(f'{eval_res_dir}/teacher-finetuned{{ens}}/{{dataset}}.metrics',ens=ensemble, dataset=eval_datasets))
else:
augment_corpus = False
final_teacher_dir = teacher_base_dir
### helper functions
def find_parts(wildcards, checkpoint):
checkpoint_output = checkpoint.get(**wildcards).output[0]
return glob_wildcards(os.path.join(checkpoint_output,"file.{part,\d+}")).part
def dataset_norm(name: str):
return name.replace('/','_')
def get_args(section):
return marian_args.get(section) or ""
### rules
shell.prefix(f"{envs} ")
rule all:
input: results
localrules: experiment
rule experiment:
message: "Saving experiment metadata"
output: f'{experiment_dir}/config.yml'
priority: 100
run:
os.makedirs(experiment_dir, exist_ok=True)
with open(f'{experiment_dir}/config.yml', 'w') as f:
yaml.dump(config, f)
# todo: fix jobs grouping in cluster mode
# setup
if install_deps:
rule setup:
message: "Installing dependencies"
log: f"{log_dir}/install-deps.log"
conda: "envs/base.yml"
priority: 99
# group: 'setup'
output: touch("/tmp/flags/setup.done") # specific to local machine
shell: 'bash pipeline/setup/install-deps.sh >> {log} 2>&1'
rule marian:
message: "Compiling marian"
log: f"{log_dir}/compile-{{marian_type}}.log"
conda: "envs/base.yml"
threads: 16
resources: gpu=1
# group: 'setup'
output:
trainer=protected(f"{third_party_dir}/{{marian_type}}/build/marian"),
decoder=protected(f"{third_party_dir}/{{marian_type}}/build/marian-decoder"),
scorer=protected(f"{third_party_dir}/{{marian_type}}/build/marian-scorer"),
converter=protected(f'{third_party_dir}/{{marian_type}}/build/marian-conv'),
spm_trainer=protected(f'{third_party_dir}/{{marian_type}}/build/spm_train'),
spm_encoder=protected(f'{third_party_dir}/{{marian_type}}/build/spm_encode'),
spm_exporter=protected(f'{third_party_dir}/{{marian_type}}/build/spm_export_vocab')
params: build_dir=f'{third_party_dir}/{{marian_type}}/build'
shell: 'bash pipeline/setup/compile-marian.sh {params.build_dir} {threads} {marian_cmake} >> {log} 2>&1'
rule fast_align:
message: "Compiling fast align"
log: f"{log_dir}/compile-fast-align.log"
conda: "envs/base.yml"
threads: 4
# group: 'setup'
output: fast_align=protected(f"{bin}/fast_align"), atools=protected(f"{bin}/atools")
shell: 'bash pipeline/setup/compile-fast-align.sh {fast_align_build} {threads} >> {log} 2>&1'
rule compile_preprocess:
message: "Compiling preprocess"
log: f"{log_dir}/compile-preprocess.log"
conda: "envs/base.yml"
threads: 4
# group: 'setup'
output: deduper=f'{bin}/dedupe'
shell: 'bash pipeline/setup/compile-preprocess.sh {preprocess_build_dir} {threads} >> {log} 2>&1'
rule extract_lex:
message: "Compiling fast align"
log: f"{log_dir}/compile-extract-lex.log"
conda: "envs/base.yml"
threads: 4
# group: 'setup'
output: protected(f"{bin}/extract_lex")
shell: 'bash pipeline/setup/compile-extract-lex.sh {extract_lex_build} {threads} >> {log} 2>&1'
# data downloading
rule download_corpus:
message: "Downloading parallel corpus"
log: f"{log_dir}/download_corpus/{{kind}}/{{dataset}}.log"
conda: "envs/base.yml"
threads: 1
# group: 'data'
cache: False # caching is broken in snakemake
wildcard_constraints: kind="corpus|devset|eval"
output: multiext(f"{original}/{{kind}}/{{dataset}}", f".{src}.gz", f".{trg}.gz")
params: prefix=f"{original}/{{kind}}/{{dataset}}", dataset="{dataset}"
shell: 'bash pipeline/data/download-corpus.sh "{params.dataset}" "{params.prefix}" >> {log} 2>&1'
rule download_mono:
message: "Downloading monolingual dataset"
log: f"{log_dir}/download_mono/{{dataset}}.{{lang}}.log"
conda: "envs/base.yml"
threads: 1
# group: 'data'
cache: False # caching is broken in snakemake
wildcard_constraints: lang=f"{src}|{trg}"
output: f'{original}/mono/{{dataset}}.{{lang}}.gz'
params: max_sent=lambda wildcards: mono_max_sent[wildcards.lang], dataset='{dataset}', lang='{lang}'
shell: '''bash pipeline/data/download-mono.sh \
"{params.dataset}" {params.lang} {params.max_sent} "{output}" >> {log} 2>&1'''
# cleaning
rule clean_corpus:
message: "Cleaning dataset"
log: f"{log_dir}/clean_corpus/{{dataset}}.log"
conda: "envs/base.yml"
# group: "clean_corpus"
threads: workflow.cores
input: multiext(f"{original}/corpus/{{dataset}}", f".{src}.gz", f".{trg}.gz")
output: multiext(f"{clean}/corpus/{{dataset}}", f".{src}.gz", f".{trg}.gz")
params: prefix_input=f"{original}/corpus/{{dataset}}",prefix_output=f"{clean}/corpus/{{dataset}}",
dataset=lambda wildcards: dataset_norm(wildcards.dataset)
shell: '''bash pipeline/clean/clean-corpus.sh "{params.prefix_input}" "{params.prefix_output}" {threads} {params.dataset} \
>> {log} 2>&1'''
rule clean_mono:
message: "Cleaning monolingual dataset"
log: f"{log_dir}/clean_mono/{{dataset}}.{{lang}}.log"
conda: "envs/base.yml"
threads: workflow.cores
# group: "clean_mono{lang}"
cache: False
wildcard_constraints: lang=f"{src}|{trg}"
input: f'{original}/mono/{{dataset}}.{{lang}}.gz'
output: f'{clean}/mono/{{dataset}}.{{lang}}.gz'
params: prefix_input=f"{original}/mono/{{dataset}}", prefix_output=f"{clean}/mono/{{dataset}}",
dataset=lambda wildcards: dataset_norm(wildcards.dataset)
shell: '''bash pipeline/clean/clean-mono.sh {wildcards.lang} "{params.prefix_input}" "{params.prefix_output}" \
{threads} {params.dataset} >> {log} 2>&1'''
if use_bicleaner:
rule kenlm:
message: "Installing kenlm"
log: f"{log_dir}/kenlm.log"
conda: bicleaner_env
threads: 4
# group: 'setup'
output: directory(f"{bin}/kenlm")
shell: 'bash pipeline/setup/install-kenlm.sh {kenlm} {threads} >> {log} 2>&1'
rule bicleaner_pack:
message: f"Downloading language pack for bicleaner"
log: f"{log_dir}/bicleaner_pack.log"
conda: bicleaner_env
# group: "clean_corpus"
threads: 1
input: rules.kenlm.output
output: directory(f"{biclean}/pack")
shell: '''bash pipeline/bicleaner/download-pack.sh "{output}" {bicleaner_type} >> {log} 2>&1'''
rule bicleaner:
message: f"Cleaning corpus using {bicleaner_type}"
log: f"{log_dir}/bicleaner/{{dataset}}.log"
conda: bicleaner_env
# group: "bicleaner"
threads: gpus_num * 2 if bicleaner_type == "bicleaner-ai" else workflow.cores
resources: gpu=gpus_num if bicleaner_type == "bicleaner-ai" else 0
input: ancient(rules.kenlm.output), multiext(f"{clean}/corpus/{{dataset}}", f".{src}.gz", f".{trg}.gz"),
pack_dir=rules.bicleaner_pack.output
output: multiext(f"{biclean}/corpus/{{dataset}}", f".{src}.gz", f".{trg}.gz")
params:
prefix_input=f"{clean}/corpus/{{dataset}}",prefix_output=f"{biclean}/corpus/{{dataset}}",
threshold=lambda wildcards: bicl_dataset_thresholds[wildcards.dataset]
if wildcards.dataset in bicl_dataset_thresholds
else bicl_default_threshold
shell: '''bash pipeline/bicleaner/bicleaner.sh \
"{params.prefix_input}" "{params.prefix_output}" {params.threshold} {bicleaner_type} {threads} \
"{input.pack_dir}" >> {log} 2>&1'''
rule merge_corpus:
message: "Merging clean parallel datasets"
log: f"{log_dir}/merge_corpus.log"
conda: "envs/base.yml"
threads: workflow.cores
# group: "clean_corpus"
input: expand(f"{clean_corpus_prefix}/{{dataset}}.{{lang}}.gz", dataset=train_datasets, lang=[src, trg]),
bin=ancient(deduper)
output: src=clean_corpus_src,trg=clean_corpus_trg
params: prefix_output=clean_corpus_prefix, prefixes=expand(f"{clean_corpus_prefix}/{{dataset}}", dataset=train_datasets)
shell: '''bash pipeline/clean/merge-corpus.sh "{params.prefix_output}" {params.prefixes} >> {log} 2>&1'''
rule merge_devset:
message: "Merging devsets"
log: f"{log_dir}/merge_devset.log"
conda: "envs/base.yml"
threads: workflow.cores
# group: "clean_corpus"
input: expand(f"{original}/devset/{{dataset}}.{{lang}}.gz", dataset=valid_datasets, lang=[src, trg]),
bin=ancient(deduper)
output: multiext(f"{original}/devset", f".{src}.gz", f".{trg}.gz")
params: prefix_output=f"{original}/devset", prefixes=expand(f"{original}/devset/{{dataset}}", dataset=valid_datasets)
shell: '''bash pipeline/clean/merge-corpus.sh "{params.prefix_output}" {params.prefixes} >> {log} 2>&1'''
rule merge_mono:
message: "Merging clean monolingual datasets"
log: f"{log_dir}/merge_mono_{{lang}}.log"
conda: "envs/base.yml"
threads: workflow.cores
#group "clean_mono{lang}"
input:
corpora=lambda wildcards: expand(f"{clean}/mono/{{dataset}}.{{lang}}.gz",
dataset=mono_datasets[wildcards.lang], lang=wildcards.lang),
bin=ancient(deduper)
output: f"{clean}/mono.{{lang}}.gz"
params: max_sent=lambda wildcards: mono_max_sent[wildcards.lang]
shell: '''bash pipeline/clean/merge-mono.sh "{output}" {params.max_sent} {input.corpora} >> {log} 2>&1'''
# augmentation and teacher training
if not vocab_pretrained:
rule train_vocab:
message: "Training spm vocab"
log: f"{log_dir}/train_vocab.log"
conda: "envs/base.yml"
threads: 2
input: bin=ancient(spm_trainer), corpus_src=clean_corpus_src, corpus_trg=clean_corpus_trg
output: vocab_path
params: prefix_train=clean_corpus_prefix,prefix_test=f"{original}/devset"
shell: '''bash pipeline/train/spm-vocab.sh "{input.corpus_src}" "{input.corpus_trg}" "{output}" {spm_sample_size} \
>> {log} 2>&1'''
if do_train_backward:
rule train_backward:
message: "Training backward model"
log: f"{log_dir}/train_backward.log"
conda: "envs/base.yml"
threads: gpus_num * 2
resources: gpu=gpus_num
#group 'backward'
input:
rules.merge_devset.output, train_src=clean_corpus_src,train_trg=clean_corpus_trg,
bin=ancient(trainer), vocab=vocab_path,
output: model=f'{backward_dir}/{best_model}'
params: prefix_train=clean_corpus_prefix,prefix_test=f"{original}/devset",
args=get_args("training-backward")
shell: '''bash pipeline/train/train.sh \
backward train {trg} {src} "{params.prefix_train}" "{params.prefix_test}" "{backward_dir}" \
"{input.vocab}" {params.args} >> {log} 2>&1'''
if augment_corpus:
checkpoint split_mono_trg:
message: "Splitting monolingual trg dataset"
log: f"{log_dir}/split_mono_trg.log"
conda: "envs/base.yml"
threads: 1
input: corpora=f"{clean}/mono.{trg}.gz", bin=ancient(deduper)
output: directory(f'{translated}/mono_trg')
shell: 'bash pipeline/translate/split-mono.sh {input.corpora} {output} {split_length} >> {log} 2>&1'
rule translate_mono_trg:
message: "Translating monolingual trg dataset with backward model"
log: f"{log_dir}/translate_mono_trg/{{part}}.log"
conda: "envs/base.yml"
threads: gpus_num * 2
resources: gpu=gpus_num
input:
bin=ancient(decoder), file=f'{translated}/mono_trg/file.{{part}}',
vocab=vocab_path, model=f'{backward_dir}/{best_model}'
output: f'{translated}/mono_trg/file.{{part}}.out'
params: args = get_args("decoding-backward")
shell: '''bash pipeline/translate/translate.sh "{input.file}" "{input.vocab}" {input.model} {params.args} \
>> {log} 2>&1'''
rule collect_mono_trg:
message: "Collecting translated mono trg dataset"
log: f"{log_dir}/collect_mono_trg.log"
conda: "envs/base.yml"
threads: 4
#group 'mono_trg'
input:
lambda wildcards: expand(f"{translated}/mono_trg/file.{{part}}.out",
part=find_parts(wildcards, checkpoints.split_mono_trg))
output: f'{translated}/mono.{src}.gz'
params: src_mono=f"{clean}/mono.{trg}.gz",dir=directory(f'{translated}/mono_trg')
shell: 'bash pipeline/translate/collect.sh "{params.dir}" "{output}" "{params.src_mono}" >> {log} 2>&1'
rule merge_augmented:
message: "Merging augmented dataset"
log: f"{log_dir}/merge_augmented.log"
conda: "envs/base.yml"
threads: 4
#group 'mono_trg'
input:
src1=clean_corpus_src,src2=rules.collect_mono_trg.output,
trg1=clean_corpus_trg,trg2=rules.split_mono_trg.input,
bin=ancient(deduper)
output: res_src=f'{augmented}/corpus.{src}.gz',res_trg=f'{augmented}/corpus.{trg}.gz'
shell: '''bash pipeline/translate/merge-corpus.sh \
"{input.src1}" "{input.src2}" "{input.trg1}" "{input.trg2}" "{output.res_src}" "{output.res_trg}" \
>> {log} 2>&1'''
rule train_teacher:
message: "Training teacher on all data"
log: f"{log_dir}/train_teacher{{ens}}.log"
conda: "envs/base.yml"
threads: gpus_num*2
resources: gpu=gpus_num
input:
rules.merge_devset.output, train_src=f'{teacher_corpus}.{src}.gz',train_trg=f'{teacher_corpus}.{trg}.gz',
bin=ancient(trainer), vocab=vocab_path
output: model=f'{teacher_base_dir}{{ens}}/{best_model}'
params: prefix_train=teacher_corpus, prefix_test=f"{original}/devset", dir=directory(f'{teacher_base_dir}{{ens}}'),
args=get_args("training-teacher-base")
shell: '''bash pipeline/train/train.sh \
teacher train {src} {trg} "{params.prefix_train}" "{params.prefix_test}" "{params.dir}" \
"{input.vocab}" {params.args} >> {log} 2>&1'''
if augment_corpus:
rule finetune_teacher:
message: "Finetune teacher on parallel corpus"
log: f"{log_dir}/finetune_teacher{{ens}}.log"
conda: "envs/base.yml"
threads: gpus_num * 2
resources: gpu=gpus_num
input:
rules.merge_devset.output, model=f'{teacher_base_dir}{{ens}}/{best_model}',
train_src=clean_corpus_src, train_trg=clean_corpus_trg,
bin=ancient(trainer), vocab=vocab_path
output: model=f'{teacher_finetuned_dir}{{ens}}/{best_model}'
params: prefix_train=clean_corpus_prefix, prefix_test=f"{original}/devset",
dir=directory(f'{teacher_finetuned_dir}{{ens}}'),
args=get_args("training-teacher-finetuned")
shell: '''bash pipeline/train/train.sh \
teacher train {src} {trg} "{params.prefix_train}" "{params.prefix_test}" "{params.dir}" \
"{input.vocab}" --pretrained-model "{input.model}" {params.args} >> {log} 2>&1'''
### translation with teacher
# corpus
checkpoint split_corpus:
message: "Splitting the corpus to translate"
log: f"{log_dir}/split_corpus.log"
conda: "envs/base.yml"
threads: 1
input: corpus_src=clean_corpus_src,corpus_trg=clean_corpus_trg
output: directory(f"{translated}/corpus")
shell: '''bash pipeline/translate/split-corpus.sh \
{input.corpus_src} {input.corpus_trg} {output} {split_length} >> {log} 2>&1'''
rule translate_corpus:
message: "Translating corpus with teacher"
log: f"{log_dir}/translate_corpus/{{part}}.log"
conda: "envs/base.yml"
threads: gpus_num*2
resources: gpu=gpus_num
input:
ancient(decoder),
file=f'{translated}/corpus/file.{{part}}',
vocab=vocab_path,
teacher_models=expand(f"{final_teacher_dir}{{ens}}/{best_model}",ens=ensemble)
output: f'{translated}/corpus/file.{{part}}.nbest'
params: args=get_args('decoding-teacher')
shell: '''bash pipeline/translate/translate-nbest.sh \
"{input.file}" "{input.vocab}" {input.teacher_models} {params.args} >> {log} 2>&1'''
rule extract_best:
message: "Extracting best translations for the corpus"
log: f"{log_dir}/extract_best/{{part}}.log"
conda: "envs/base.yml"
threads: 1
#group 'translate_corpus'
input: nbest=f"{translated}/corpus/file.{{part}}.nbest", ref=f"{translated}/corpus/file.{{part}}.ref"
output: f"{translated}/corpus/file.{{part}}.nbest.out"
shell: 'python pipeline/translate/bestbleu.py -i {input.nbest} -r {input.ref} -m bleu -o {output} >> {log} 2>&1'
rule collect_corpus:
message: "Collecting translated corpus"
log: f"{log_dir}/collect_corpus.log"
conda: "envs/base.yml"
threads: 4
#group 'translate_corpus'
input:
lambda wildcards: expand(f"{translated}/corpus/file.{{part}}.nbest.out",
part=find_parts(wildcards, checkpoints.split_corpus))
output: f'{translated}/corpus.{trg}.gz'
params: src_corpus=clean_corpus_src
shell: 'bash pipeline/translate/collect.sh {translated}/corpus {output} {params.src_corpus} >> {log} 2>&1'
# mono
checkpoint split_mono_src:
message: "Splitting monolingual src dataset"
log: f"{log_dir}/split_mono_src.log"
conda: "envs/base.yml"
threads: 1
input: corpora=f"{clean}/mono.{src}.gz", bin=ancient(deduper)
output: directory(f'{translated}/mono_src')
shell: 'bash pipeline/translate/split-mono.sh {input.corpora} {output} {split_length} >> {log} 2>&1'
rule translate_mono_src:
message: "Translating monolingual src dataset with teacher"
log: f"{log_dir}/translate_mono_src/{{part}}.log"
conda: "envs/base.yml"
threads: gpus_num*2
resources: gpu=gpus_num
input:
file=f'{translated}/mono_src/file.{{part}}',vocab=vocab_path,
teacher_models=expand(f"{final_teacher_dir}{{ens}}/{best_model}",ens=ensemble),
bin=ancient(decoder)
output: f'{translated}/mono_src/file.{{part}}.out'
params: args=get_args('decoding-teacher')
shell: '''bash pipeline/translate/translate.sh "{input.file}" "{input.vocab}" {input.teacher_models} \
{params.args} >> {log} 2>&1'''
rule collect_mono_src:
message: "Collecting translated mono src dataset"
log: f"{log_dir}/collect_mono_src.log"
conda: "envs/base.yml"
threads: 4
#group 'mono_src'
input:
lambda wildcards: expand(f"{translated}/mono_src/file.{{part}}.out",
part=find_parts(wildcards, checkpoints.split_mono_src))
output: f'{translated}/mono.{trg}.gz'
params: src_mono=f"{clean}/mono.{src}.gz",dir=f'{translated}/mono_src'
shell: 'bash pipeline/translate/collect.sh "{params.dir}" "{output}" "{params.src_mono}" >> {log} 2>&1'
# merge
rule merge_translated:
message: "Merging translated datasets"
log: f"{log_dir}/merge_translated.log"
conda: "envs/base.yml"
threads: 4
#group 'mono_src'
input:
src1=clean_corpus_src,src2=f"{clean}/mono.{src}.gz",
trg1=rules.collect_corpus.output,trg2=rules.collect_mono_src.output,
bin=ancient(deduper)
output: res_src=f'{merged}/corpus.{src}.gz',res_trg=f'{merged}/corpus.{trg}.gz'
shell: '''bash pipeline/translate/merge-corpus.sh \
"{input.src1}" "{input.src2}" "{input.trg1}" "{input.trg2}" "{output.res_src}" "{output.res_trg}" \
>> {log} 2>&1'''
# train student
rule score:
message: "Scoring"
log: f"{log_dir}/score.log"
conda: "envs/base.yml"
threads: gpus_num*2
resources: gpu=gpus_num
input:
ancient(scorer),
model=f'{backward_dir}/{best_model}', vocab=vocab_path,
src_corpus=rules.merge_translated.output.res_src, trg_corpus=rules.merge_translated.output.res_trg
output: f"{filtered}/scores.txt"
params: input_prefix=f'{merged}/corpus'
shell: '''bash pipeline/cefilter/score.sh \
"{input.model}" "{input.vocab}" "{params.input_prefix}" "{output}" >> {log} 2>&1'''
rule ce_filter:
message: "Cross entropy filtering"
log: f"{log_dir}/ce_filter.log"
conda: "envs/base.yml"
threads: workflow.cores
resources: mem_mb=workflow.cores*5000
input:
src_corpus=rules.merge_translated.output.res_src,trg_corpus=rules.merge_translated.output.res_trg,
scores=rules.score.output
output: src_corpus=f"{filtered}/corpus.{src}.gz",trg_corpus=f"{filtered}/corpus.{trg}.gz"
params: input_prefix=f'{merged}/corpus',output_prefix=f'{filtered}/corpus'
shell: '''bash pipeline/cefilter/ce-filter.sh \
"{params.input_prefix}" "{params.output_prefix}" "{input.scores}" >> {log} 2>&1'''
rule alignments:
message: 'Training word alignment and lexical shortlists'
log: f"{log_dir}/alignments.log"
conda: "envs/base.yml"
threads: workflow.cores
input:
ancient(spm_encoder), ancient(spm_exporter),
src_corpus=rules.ce_filter.output.src_corpus,trg_corpus=rules.ce_filter.output.trg_corpus,
vocab=vocab_path,
fast_align=ancient(rules.fast_align.output.fast_align), atools=ancient(rules.fast_align.output.atools),
extract_lex=ancient(rules.extract_lex.output)
output: alignment=f'{align_dir}/corpus.aln.gz',shortlist=f'{align_dir}/lex.s2t.pruned.gz'
params: input_prefix=f'{filtered}/corpus'
shell: '''bash pipeline/alignment/generate-alignment-and-shortlist.sh \
"{params.input_prefix}" "{input.vocab}" "{align_dir}" {threads} >> {log} 2>&1'''
rule train_student:
message: "Training student"
log: f"{log_dir}/train_student.log"
conda: "envs/base.yml"
threads: gpus_num*2
resources: gpu=gpus_num
#group 'student'
input:
rules.merge_devset.output, ancient(trainer),
train_src=rules.ce_filter.output.src_corpus, train_trg=rules.ce_filter.output.trg_corpus,
alignments=rules.alignments.output.alignment,
vocab=vocab_path
output: model=f'{student_dir}/{best_model}'
params: prefix_train=rules.ce_filter.params.output_prefix,prefix_test=f"{original}/devset",
args=get_args("training-student")
shell: '''bash pipeline/train/train-student.sh \
"{input.alignments}" student train {src} {trg} "{params.prefix_train}" "{params.prefix_test}" \
"{student_dir}" "{input.vocab}" {params.args} >> {log} 2>&1'''
# quantize
rule finetune_student:
message: "Fine-tuning student"
log: f"{log_dir}/finetune_student.log"
conda: "envs/base.yml"
threads: gpus_num*2
resources: gpu=gpus_num
#group 'student-finetuned'
input:
rules.merge_devset.output, ancient(trainer),
train_src=rules.ce_filter.output.src_corpus, train_trg=rules.ce_filter.output.trg_corpus,
alignments=rules.alignments.output.alignment, student_model=rules.train_student.output.model,
vocab=vocab_path
output: model=f'{student_finetuned_dir}/{best_model}'
params: prefix_train=rules.ce_filter.params.output_prefix,prefix_test=f"{original}/devset",
args=get_args("training-student-finetuned")
shell: '''bash pipeline/train/train-student.sh \
"{input.alignments}" student finetune {src} {trg} "{params.prefix_train}" "{params.prefix_test}" \
"{student_finetuned_dir}" "{input.vocab}" --pretrained-model "{input.student_model}" {params.args} >> {log} 2>&1'''
rule quantize:
message: "Quantization"
log: f"{log_dir}/quntize.log"
conda: "envs/base.yml"
threads: 1
input:
ancient(bmt_decoder), ancient(bmt_converter),
shortlist=rules.alignments.output.shortlist, model=rules.finetune_student.output.model,
vocab=vocab_path, devset=f"{original}/devset.{src}.gz"
output: model=f'{speed_dir}/model.intgemm.alphas.bin'
shell: '''bash pipeline/quantize/quantize.sh \
"{input.model}" "{input.vocab}" "{input.shortlist}" "{input.devset}" "{speed_dir}" >> {log} 2>&1'''
rule export:
message: "Exporting models"
log: f"{log_dir}/export.log"
conda: "envs/base.yml"
#group 'export'
threads: 1
input:
model=rules.quantize.output.model,shortlist=rules.alignments.output.shortlist,
vocab=vocab_path,marian=bmt_converter
output:
model=f'{exported_dir}/model.{src}{trg}.intgemm.alphas.bin.gz',
shortlist=f'{exported_dir}/lex.50.50.{src}{trg}.s2t.bin.gz',
vocab=f'{exported_dir}/vocab.{src}{trg}.spm.gz'
shell:
'bash pipeline/quantize/export.sh "{speed_dir}" "{input.shortlist}" "{input.vocab}" "{exported_dir}" >> {log} 2>&1'
### evaluation
rule evaluate:
message: "Evaluating a model"
log: f"{log_dir}/eval/eval_{{model}}_{{dataset}}.log"
conda: "envs/base.yml"
threads: gpus_num * 2
resources: gpu=gpus_num
#group '{model}'
priority: 50
wildcard_constraints:
model="[\w-]+"
input:
ancient(decoder),
data=multiext(f'{eval_data_dir}/{{dataset}}',f".{src}.gz",f".{trg}.gz"),
models=lambda wildcards: f'{models_dir}/{wildcards.model}/{best_model}'
if wildcards.model != 'teacher-ensemble'
else [f'{final_teacher_dir}{ens}/{best_model}' for ens in ensemble]
output:
report(f'{eval_res_dir}/{{model}}/{{dataset}}.metrics',
category='evaluation', subcategory='{model}', caption='reports/evaluation.rst')
params:
dataset_prefix=f'{eval_data_dir}/{{dataset}}',
res_prefix=f'{eval_res_dir}/{{model}}/{{dataset}}',
src_lng=lambda wildcards: src if wildcards.model != 'backward' else trg,
trg_lng=lambda wildcards: trg if wildcards.model != 'backward' else src,
decoder_config=lambda wildcards: f'{models_dir}/{wildcards.model}/{best_model}.decoder.yml'
if wildcards.model != 'teacher-ensemble'
else f'{final_teacher_dir}0/{best_model}.decoder.yml'
shell: '''bash pipeline/eval/eval-gpu.sh "{params.res_prefix}" "{params.dataset_prefix}" \
{params.src_lng} {params.trg_lng} "{params.decoder_config}" {input.models} >> {log} 2>&1'''
rule eval_quantized:
message: "Evaluating qunatized student model"
log: f"{log_dir}/eval_quantized_{{dataset}}.log"
conda: "envs/base.yml"
#group 'export'
threads: 1
priority: 50
input:
ancient(bmt_decoder),
data=multiext(f'{eval_data_dir}/{{dataset}}',f".{src}.gz",f".{trg}.gz"),
model=rules.quantize.output.model,
shortlist=rules.alignments.output.shortlist,
vocab=vocab_path
output:
report(f'{eval_speed_dir}/{{dataset}}.metrics', category='evaluation',
subcategory='quantized', caption='reports/evaluation.rst')
params:
dataset_prefix=f'{eval_data_dir}/{{dataset}}',
res_prefix=f'{eval_speed_dir}/{{dataset}}',
decoder_config='../quantize/decoder.yml'
shell: '''bash pipeline/eval/eval-quantized.sh "{input.model}" "{input.shortlist}" "{params.dataset_prefix}" \
"{input.vocab}" "{params.res_prefix}" "{params.decoder_config}" >> {log} 2>&1'''