-
Notifications
You must be signed in to change notification settings - Fork 17
/
rebatch_inits_for_beamsearch.py
72 lines (55 loc) · 2.08 KB
/
rebatch_inits_for_beamsearch.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
import argparse
import logging
from typing import List, Tuple
import torch
logger = logging.getLogger(__name__)
# usage: python scripts/rebatch_inits_for_beamsearch.py data/heldout_40.cache --batch_size 4 --out bs_4.cache
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("cache", type=str)
parser.add_argument("--batch_size", type=int, required=True)
parser.add_argument("--out", type=str, required=True)
return parser.parse_args()
def main():
args = parse_args()
cache = torch.load(args.cache)
all_inits = []
all_ends = []
for inits, ends in cache:
assert inits.shape[0] == len(ends)
all_inits.extend(inits.tolist())
all_ends.extend(ends)
assert len(all_inits) == len(all_ends)
if args.batch_size > 1:
all_batches = []
batch = []
for init, end in zip(all_inits, all_ends):
if len(batch) >= args.batch_size:
all_batches.append(batch)
batch = []
if not batch or end == batch[-1][1]:
batch.append((init[:(end+1)], end))
else:
all_batches.append(batch)
batch = [(init[:(end+1)], end)]
if batch:
all_batches.append(batch)
tensor_batches = []
for batch in all_batches:
inits = torch.tensor([x[0] for x in batch])
ends = [x[1] for x in batch]
assert inits.shape[0] <= args.batch_size
assert inits.shape[0] == len(ends)
tensor_batches.append((inits, ends))
assert sum(x[0].shape[0] for x in tensor_batches) == sum(x[0].shape[0] for x in cache)
for init, end in tensor_batches:
assert all(x == end[0] for x in end)
elif args.batch_size == 1:
tensor_batches = []
for i, (init, end) in enumerate(zip(all_inits, all_ends)):
tensor_batches.append((
torch.tensor(init[:end+1]).unsqueeze(0), [end]
))
torch.save(tensor_batches, args.out)
if __name__ == '__main__':
main()