-
Notifications
You must be signed in to change notification settings - Fork 8
/
lm_zoo_spec.py
70 lines (63 loc) · 1.86 KB
/
lm_zoo_spec.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
import argparse
import json
import torch
import subprocess
from datetime import datetime
from data import SentencePieceVocabulary
parser = argparse.ArgumentParser()
parser.add_argument('--model', default='rnng.pt', required=True)
DEFAULT_SPEC = {
"name": "rnng-pytorch",
"ref_url": "https://github.com/hiroshinoji/rnng-pytorch",
"image": {
"maintainer": "[email protected]",
"version": "<image.sha1>",
"checksum": "<image.files_sha1>",
"datetime": "<image.datetime>",
"gpu": {
"required": True,
"supported": True
},
"supported_features": {
"tokenize": True,
"unkify": True,
"get_surprisals": True,
"get_predictions": False,
"mount_checkpoint": True
}
},
"vocabulary": {
"unk_types": ["<unk>"],
"prefix_types": [],
"suffix_types": [],
"special_types": []
},
"tokenizer": {
"type": "word",
"cased": True
}
}
def mk_spec(model_path, vocab):
j = DEFAULT_SPEC
j["image"]["checksum"] = (subprocess.check_output("cat {} | sha1sum".format(model_path), shell=True)
.decode('utf-8').split()[0])
j["image"]["version"] = (subprocess.check_output("git rev-parse --verify HEAD", shell=True)
.decode('utf-8').strip())
j["image"]["datetime"] = str(datetime.now())
if isinstance(vocab, SentencePieceVocabulary):
j["vocabulary"]["unk_types"] = [vocab.unktoken]
j["is_subword"] = True
else:
j["vocabulary"]["unk_types"] = vocab.specials
j["is_subword"] = False
assert '<unk>' in j["vocabulary"]["unk_types"]
j["vocabulary"]["items"] = [w for w in vocab.i2w if w not in vocab.specials]
return j
def main(args):
checkpoint = torch.load(args.model)
vocab = checkpoint['vocab']
spec = mk_spec(args.model, vocab)
print(json.dumps(spec))
if __name__ == '__main__':
args = parser.parse_args()
main(args)