-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
74 lines (53 loc) · 2.25 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
'''
This code is adapted from Facebook Fairseq-py
Visit https://github.com/facebookresearch/fairseq-py for more information
'''
from collections import defaultdict
import contextlib
import logging
import os
import torch
import traceback
from torch.autograd import Variable
from torch.serialization import default_restore_location
def make_variable(sample, cuda=False):
"""Wrap input tensors in Variable class."""
if len(sample) == 0:
return {}
def _make_variable(maybe_tensor):
if torch.is_tensor(maybe_tensor):
if cuda and torch.cuda.is_available():
maybe_tensor = maybe_tensor.cuda()
return Variable(maybe_tensor)
elif isinstance(maybe_tensor, dict):
return {
key: _make_variable(value)
for key, value in maybe_tensor.items()
}
elif isinstance(maybe_tensor, list):
return [_make_variable(x) for x in maybe_tensor]
else:
return maybe_tensor
return _make_variable(sample)
def strip_pad(tensor, pad):
return tensor[tensor.ne(pad)]
INCREMENTAL_STATE_INSTANCE_ID = defaultdict(lambda: 0)
def _get_full_incremental_state_key(module_instance, key):
module_name = module_instance.__class__.__name__
# assign a unique ID to each module instance, so that incremental state is
# not shared across module instances
if not hasattr(module_instance, '_fairseq_instance_id'):
INCREMENTAL_STATE_INSTANCE_ID[module_name] += 1
module_instance._fairseq_instance_id = INCREMENTAL_STATE_INSTANCE_ID[module_name]
return '{}.{}.{}'.format(module_name, module_instance._fairseq_instance_id, key)
def get_incremental_state(module, incremental_state, key):
"""Helper for getting incremental state for an nn.Module."""
full_key = _get_full_incremental_state_key(module, key)
if incremental_state is None or full_key not in incremental_state:
return None
return incremental_state[full_key]
def set_incremental_state(module, incremental_state, key, value):
"""Helper for setting incremental state for an nn.Module."""
if incremental_state is not None:
full_key = _get_full_incremental_state_key(module, key)
incremental_state[full_key] = value