forked from SeanNaren/deepspeech.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (33 loc) · 1.15 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
import torch
import torch.distributed as dist
from model import DeepSpeech
def reduce_tensor(tensor, world_size, reduce_op_max=False):
rt = tensor.clone()
dist.all_reduce(rt, op=dist.reduce_op.MAX if reduce_op_max is True else dist.reduce_op.SUM) # Default to sum
if not reduce_op_max:
rt /= world_size
return rt
def check_loss(loss, loss_value):
"""
Check that warp-ctc loss is valid and will not break training
:return: Return if loss is valid, and the error in case it is not
"""
loss_valid = True
error = ''
if loss_value == float("inf") or loss_value == float("-inf"):
loss_valid = False
error = "WARNING: received an inf loss"
elif torch.isnan(loss).sum() > 0:
loss_valid = False
error = 'WARNING: received a nan loss, setting loss value to 0'
elif loss_value < 0:
loss_valid = False
error = "WARNING: received a negative loss"
return loss_valid, error
def load_model(device, model_path, use_half):
model = DeepSpeech.load_model(model_path)
model.eval()
model = model.to(device)
if use_half:
model = model.half()
return model