This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
283 lines (241 loc) · 8.81 KB
/
utils.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
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
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import collections
from dataclasses import dataclass
import gtn
import importlib
import logging
import numpy as np
import os
import struct
import sys
import time
import torch
from criterions import ctc, asg, transducer
from models import rnn, tds, tds2d
def data_loader(dataset, config, world_rank=0, world_size=1):
num_samples = config["data"].get("num_samples", None)
if num_samples is not None:
logging.info(f"Using {num_samples} of {len(dataset)}.")
dataset = Subset(dataset, torch.randperm(len(dataset))[:num_samples])
return torch.utils.data.DataLoader(
dataset,
batch_sampler=BatchSortedSampler(
dataset, config["optim"]["batch_size"], world_rank, world_size
),
collate_fn=padding_collate,
num_workers=int(world_size > 1),
)
def module_from_file(module_name, file_path):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
sys.modules[module_name] = module
return module
class Subset(torch.utils.data.Subset):
def __init__(self, dataset, indices):
super(Subset, self).__init__(dataset, indices)
def sample_sizes(self):
"""
Returns a list of tuples containing the input size
(width, height) and the output length for each sample.
"""
sizes = list(self.dataset.sample_sizes())
for idx in self.indices:
yield sizes[idx]
class BatchSortedSampler(torch.utils.data.Sampler):
def __init__(self, dataset, batch_size, world_rank, world_size, shuffle=True):
local_batchsize = batch_size // world_size
widths = (in_size[0] for in_size, _ in dataset.sample_sizes())
sorted_dataset = sorted(enumerate(widths), key=lambda x: x[1])
sorted_indices, _ = zip(*sorted_dataset)
global_batches = [
sorted_indices[idx : idx + local_batchsize]
for idx in range(0, len(sorted_indices), local_batchsize)
]
self.length = len(global_batches) // world_size
# distribute the sample across the ranks
self.batches = [
global_batches[world_rank + i * world_size] for i in range(self.length)
]
self.shuffle = shuffle
def __iter__(self):
order = torch.randperm if self.shuffle else torch.arange
return (self.batches[i] for i in order(self.length))
def __len__(self):
return self.length
def padding_collate(samples):
inputs, targets = zip(*samples)
# collate inputs:
h = inputs[0].shape[1]
max_input_len = max(ip.shape[2] for ip in inputs)
batch_inputs = torch.zeros((len(inputs), inputs[0].shape[1], max_input_len))
for e, ip in enumerate(inputs):
batch_inputs[e, :, : ip.shape[2]] = ip
return batch_inputs, targets
@dataclass
class Meters:
loss = 0.0
num_samples = 0
num_tokens = 0
edit_distance_tokens = 0
num_words = 0
edit_distance_words = 0
def sync(self):
lst = [
self.loss,
self.num_samples,
self.num_tokens,
self.edit_distance_tokens,
self.num_words,
self.edit_distance_words,
]
# TODO: avoid this so that distributed cpu training also works
lst_tensor = torch.FloatTensor(lst).cuda()
torch.distributed.all_reduce(lst_tensor)
(
self.loss,
self.num_samples,
self.num_tokens,
self.edit_distance_tokens,
self.num_words,
self.edit_distance_words,
) = lst_tensor.tolist()
@property
def avg_loss(self):
return self.loss / self.num_samples if self.num_samples > 0 else 0
@property
def cer(self):
return (
self.edit_distance_tokens * 100.0 / self.num_tokens
if self.num_tokens > 0
else 0
)
@property
def wer(self):
return (
self.edit_distance_words * 100.0 / self.num_words
if self.num_words > 0
else 0
)
# A simple timer class inspired from `tnt.TimeMeter`
class CudaTimer:
def __init__(self, keys):
self.keys = keys
self.reset()
def start(self, key):
s = torch.cuda.Event(enable_timing=True)
s.record()
self.start_events[key].append(s)
return self
def stop(self, key):
e = torch.cuda.Event(enable_timing=True)
e.record()
self.end_events[key].append(e)
return self
def reset(self):
self.start_events = collections.defaultdict(list)
self.end_events = collections.defaultdict(list)
self.running_times = collections.defaultdict(float)
self.n = collections.defaultdict(int)
return self
def value(self):
self._synchronize()
return {k: self.running_times[k] / self.n[k] for k in self.keys}
def _synchronize(self):
torch.cuda.synchronize()
for k in self.keys:
starts = self.start_events[k]
ends = self.end_events[k]
if len(starts) == 0:
raise ValueError("Trying to divide by zero in TimeMeter")
if len(ends) != len(starts):
raise ValueError("Call stop before checking value!")
time = 0
for start, end in zip(starts, ends):
time += start.elapsed_time(end)
self.running_times[k] += time * 1e-3
self.n[k] += len(starts)
self.start_events = collections.defaultdict(list)
self.end_events = collections.defaultdict(list)
# Used to measure the time taken for multiple events
class Timer:
def __init__(self, keys):
self.keys = keys
self.n = {}
self.running_time = {}
self.total_time = {}
self.reset()
def start(self, key):
self.running_time[key] = time.time()
return self
def stop(self, key):
self.total_time[key] = time.time() - self.running_time[key]
self.n[key] += 1
self.running_time[key] = None
return self
def reset(self):
for k in self.keys:
self.total_time[k] = 0
self.running_time[k] = None
self.n[k] = 0
return self
def value(self):
vals = {}
for k in self.keys:
if self.n[k] == 0:
raise ValueError("Trying to divide by zero in TimeMeter")
else:
vals[k] = self.total_time[k] / self.n[k]
return vals
def load_model(model_type, input_size, output_size, config):
if model_type == "rnn":
return rnn.RNN(input_size, output_size, **config)
elif model_type == "tds":
return tds.TDS(input_size, output_size, **config)
elif model_type == "tds2d":
return tds2d.TDS2d(input_size, output_size, **config)
elif model_type == "tds2d_transducer":
return tds2d.TDS2dTransducer(input_size, output_size, **config)
else:
raise ValueError(f"Unknown model type {model_type}")
def load_criterion(criterion_type, preprocessor, config):
num_tokens = preprocessor.num_tokens
if criterion_type == "asg":
num_replabels = config.get("num_replabels", 0)
use_garbage = config.get("use_garbage", True)
return (
asg.ASG(num_tokens, num_replabels, use_garbage),
num_tokens + num_replabels + int(use_garbage),
)
elif criterion_type == "ctc":
use_pt = config.get("use_pt", True) # use pytorch implementation
return ctc.CTC(num_tokens, use_pt), num_tokens + 1 # account for blank
elif criterion_type == "transducer":
blank = config.get("blank", "none")
transitions = config.get("transitions", None)
if transitions is not None:
transitions = gtn.load(transitions)
criterion = transducer.Transducer(
preprocessor.tokens,
preprocessor.graphemes_to_index,
ngram=config.get("ngram", 0),
transitions=transitions,
blank=blank,
allow_repeats=config.get("allow_repeats", True),
reduction="mean",
)
return criterion, num_tokens + int(blank != "none")
else:
raise ValueError(f"Unknown model type {criterion_type}")
def load_from_checkpoint(model, criterion, checkpoint_path, load_last=False):
model_checkpoint = os.path.join(checkpoint_path, "model.checkpoint")
criterion_checkpoint = os.path.join(checkpoint_path, "criterion.checkpoint")
if not load_last:
model_checkpoint += ".best"
criterion_checkpoint += ".best"
model.load_state_dict(torch.load(model_checkpoint))
criterion.load_state_dict(torch.load(criterion_checkpoint))