-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
214 lines (197 loc) · 8.26 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
import os, sys
from torch import optim
from argparse import ArgumentParser
sys.path.append(".")
import importlib
import torch
from nmtlab import MTTrainer
from nmtlab.utils import OPTS
from nmtlab.utils import is_root_node
from lib_treeautoencoder import TreeAutoEncoder
from lib_treelstm_dataloader import BilingualTreeDataLoader
from datasets import get_dataset_paths
DATA_ROOT = "./mydata"
ap = ArgumentParser()
ap.add_argument("--resume", action="store_true")
ap.add_argument("--test", action="store_true")
ap.add_argument("--test_nbest", action="store_true")
ap.add_argument("--train", action="store_true")
ap.add_argument("--evaluate", action="store_true")
ap.add_argument("--export_code", action="store_true")
ap.add_argument("--make_target", action="store_true")
ap.add_argument("--make_oracle_codes", action="store_true")
ap.add_argument("--all", action="store_true")
ap.add_argument("--opt_dtok", default="aspec", type=str)
ap.add_argument("--opt_seed", type=int, default=3)
ap.add_argument("--opt_hiddensz", type=int, default=256)
ap.add_argument("--opt_without_source", action="store_true")
ap.add_argument("--opt_codebits", type=int, default=0)
ap.add_argument("--opt_limit_tree_depth", type=int, default=0)
ap.add_argument("--opt_limit_datapoints", type=int, default=-1)
ap.add_argument("--opt_load_pretrain", action="store_true")
ap.add_argument("--model_path",
default="{}/tree2code.pt".format(DATA_ROOT))
ap.add_argument("--result_path",
default="{}/tree2code.result".format(DATA_ROOT))
OPTS.parse(ap)
n_valid_per_epoch = 4
# Define datasets
DATA_ROOT = "./mydata"
dataset_paths = get_dataset_paths(DATA_ROOT, OPTS.dtok)
# Using horovod for training, automatically occupy all GPUs
# Determine the local rank
horovod_installed = importlib.util.find_spec("horovod") is not None
if torch.cuda.is_available() and horovod_installed:
import horovod.torch as hvd
hvd.init()
torch.cuda.set_device(hvd.local_rank())
part_index = hvd.rank()
part_num = hvd.size()
gpu_num = hvd.size()
else:
part_index = 0
part_num = 1
gpu_num = 1
if is_root_node():
print("Running on {} GPUs".format(gpu_num))
# Define dataset
dataset = BilingualTreeDataLoader(
src_path=dataset_paths["train_src_corpus"],
cfg_path=dataset_paths["train_cfg_corpus"],
src_vocab_path=dataset_paths["src_vocab_path"],
treelstm_vocab_path=dataset_paths["cfg_vocab_path"],
cache_path=None,
batch_size=128 * gpu_num,
part_index=part_index,
part_num=part_num,
max_tokens=60,
limit_datapoints=OPTS.limit_datapoints,
limit_tree_depth=OPTS.limit_tree_depth
)
# Load the tree autoencoder onto GPU
autoencoder = TreeAutoEncoder(dataset, hidden_size=OPTS.hiddensz, code_bits=OPTS.codebits, without_source=OPTS.without_source)
if torch.cuda.is_available():
autoencoder.cuda()
# Train the model
if OPTS.train or OPTS.all:
# Training code
from nmtlab.schedulers import SimpleScheduler
scheduler = SimpleScheduler(30)
weight_decay = 1e-5 if OPTS.weightdecay else 0
optimizer = optim.Adagrad(autoencoder.parameters(), lr=0.05)
trainer = MTTrainer(autoencoder, dataset, optimizer, scheduler=scheduler, multigpu=gpu_num > 1)
OPTS.trainer = trainer
trainer.configure(
save_path=OPTS.model_path,
n_valid_per_epoch=n_valid_per_epoch,
criteria="loss",
)
if OPTS.load_pretrain:
import re
pretrain_path = re.sub(r"_codebits-\d", "", OPTS.model_path)
pretrain_path = pretrain_path.replace("_load_pretrain", "")
if is_root_node():
print("loading pretrained model in ", pretrain_path)
autoencoder.load_pretrain(pretrain_path)
else:
scheduler = SimpleScheduler(10)
if OPTS.resume:
trainer.load()
trainer.run()
if OPTS.export_code or OPTS.all:
from nmtlab.utils import Vocab
import torch
assert os.path.exists(OPTS.model_path)
autoencoder.load(OPTS.model_path)
out_path = "{}/{}.codes".format(DATA_ROOT, os.path.basename(OPTS.model_path).split(".")[0])
if is_root_node():
autoencoder.train(False)
if torch.cuda.is_available():
autoencoder.cuda()
c = 0
c1 = 0
with open(out_path, "w") as outf:
print("code path", out_path)
for batch in dataset.yield_all_batches(batch_size=512):
src_lines, cfg_lines, src_batch, enc_tree, dec_tree = batch
out = autoencoder(src_batch.cuda(), enc_tree, dec_tree, return_code=True)
codes = out["codes"]
for i in range(len(src_lines)):
src = src_lines[i]
cfg = cfg_lines[i]
code = str(codes[i].int().cpu().numpy())
outf.write("{}\t{}\t{}\n".format(src, cfg, code))
outf.flush()
c += len(src_lines)
if c - c1 > 10000:
sys.stdout.write(".")
sys.stdout.flush()
c1 = c
sys.stdout.write("\n")
if OPTS.make_target or OPTS.all:
if is_root_node():
export_path = "{}/{}.codes".format(DATA_ROOT, os.path.basename(OPTS.model_path).split(".")[0])
out_path = "{}/{}.tgt".format(DATA_ROOT, os.path.basename(OPTS.model_path).split(".")[0])
print("out path", out_path)
export_map = {}
for line in open(export_path):
if len(line.strip().split("\t")) < 3:
continue
src, cfg, code = line.strip().rsplit("\t", maxsplit=2)
code_str = " ".join(["<c{}>".format(int(c) + 1) for c in code.split()])
export_map["{}\t{}".format(src, cfg)] = code_str
with open(out_path, "w") as outf:
src_path = dataset_paths["train_src_corpus"]
tgt_path = dataset_paths["train_tgt_corpus"]
cfg_path = dataset_paths["train_cfg_corpus"]
for src, tgt, cfg in zip(open(src_path), open(tgt_path), open(cfg_path)):
key = "{}\t{}".format(src.strip(), cfg.strip())
if key in export_map:
outf.write("{} <eoc> {}\n".format(export_map[key], tgt.strip()))
else:
outf.write("\n")
if OPTS.make_oracle_codes:
if is_root_node():
from nmtlab.utils import Vocab
from lib_treedata import TreeDataGenerator
import torch
treegen = TreeDataGenerator(dataset_paths["test_cfg_corpus"], dataset_paths["cfg_vocab_path"])
src_vocab = Vocab(dataset_paths["src_vocab_path"])
samples = list(zip(open(dataset_paths["test_src_corpus"]), open(dataset_paths["test_cfg_corpus"])))
print("loading", OPTS.model_path)
assert os.path.exists(OPTS.model_path)
autoencoder.load(OPTS.model_path)
out_path = "{}/{}.test.export".format(DATA_ROOT,
os.path.basename(OPTS.model_path).split(".")[0])
autoencoder.train(False)
if torch.cuda.is_available():
autoencoder.cuda()
with open(out_path, "w") as outf:
print("code path", out_path)
for i in range(0, len(samples), 512):
sub_samples = samples[i: i + 512]
src_lines = [x[0] for x in sub_samples]
cfg_lines = [x[1] for x in sub_samples]
processed_samples = []
for src, cfg in sub_samples:
src = src.strip()
cfg = cfg.strip()
src_ids = src_vocab.encode(src.split())
enc_tree, dec_tree = treegen.build_trees(cfg)
processed_samples.append((src_ids, enc_tree, dec_tree))
src_batch, enc_batch, dec_batch = dataset.batch(processed_samples)
out = autoencoder(src_batch.cuda(), enc_batch, dec_batch, return_code=True)
codes = out["codes"]
for j in range(len(src_lines)):
src = src_lines[j]
cfg = cfg_lines[j]
code = codes[j].int()
outf.write("{}\n".format(code))
outf.flush()
sys.stdout.write(".")
sys.stdout.flush()
sys.stdout.write("\n")