|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | +import torchvision.models as models |
| 5 | +import timm |
| 6 | +from transformers import BertModel, BertTokenizer, BertConfig |
| 7 | +import os |
| 8 | +import json |
| 9 | +import custom_models as cm |
| 10 | + |
| 11 | +torch.hub._validate_not_a_forked_repo = lambda a, b, c: True |
| 12 | + |
| 13 | +torch_version = torch.__version__ |
| 14 | + |
| 15 | +# Detect case of no GPU before deserialization of models on GPU |
| 16 | +if not torch.cuda.is_available(): |
| 17 | + raise Exception("No GPU found. Please check if installed torch version is compatible with CUDA version") |
| 18 | + |
| 19 | +# Downloads all model files again if manifest file is not present |
| 20 | +MANIFEST_FILE = 'model_manifest.json' |
| 21 | + |
| 22 | +BENCHMARK_MODELS = { |
| 23 | + "vgg16": { |
| 24 | + "model": models.vgg16(weights=None), |
| 25 | + "path": "script" |
| 26 | + }, |
| 27 | + "resnet50": { |
| 28 | + "model": models.resnet50(weights=None), |
| 29 | + "path": "script" |
| 30 | + }, |
| 31 | + "efficientnet_b0": { |
| 32 | + "model": timm.create_model('efficientnet_b0', pretrained=True), |
| 33 | + "path": "script" |
| 34 | + }, |
| 35 | + "vit": { |
| 36 | + "model": timm.create_model('vit_base_patch16_224', pretrained=True), |
| 37 | + "path": "script" |
| 38 | + }, |
| 39 | + "bert_base_uncased": { |
| 40 | + "model": cm.BertModule(), |
| 41 | + "path": "trace" |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +def get(n, m, manifest): |
| 47 | + print("Downloading {}".format(n)) |
| 48 | + traced_filename = "models/" + n + '_traced.jit.pt' |
| 49 | + script_filename = "models/" + n + '_scripted.jit.pt' |
| 50 | + x = torch.ones((1, 3, 300, 300)).cuda() |
| 51 | + if n == "bert-base-uncased": |
| 52 | + traced_model = m["model"] |
| 53 | + torch.jit.save(traced_model, traced_filename) |
| 54 | + manifest.update({n: [traced_filename]}) |
| 55 | + else: |
| 56 | + m["model"] = m["model"].eval().cuda() |
| 57 | + if m["path"] == "both" or m["path"] == "trace": |
| 58 | + trace_model = torch.jit.trace(m["model"], [x]) |
| 59 | + torch.jit.save(trace_model, traced_filename) |
| 60 | + manifest.update({n: [traced_filename]}) |
| 61 | + if m["path"] == "both" or m["path"] == "script": |
| 62 | + script_model = torch.jit.script(m["model"]) |
| 63 | + torch.jit.save(script_model, script_filename) |
| 64 | + if n in manifest.keys(): |
| 65 | + files = list(manifest[n]) if type(manifest[n]) != list else manifest[n] |
| 66 | + files.append(script_filename) |
| 67 | + manifest.update({n: files}) |
| 68 | + else: |
| 69 | + manifest.update({n: [script_filename]}) |
| 70 | + return manifest |
| 71 | + |
| 72 | + |
| 73 | +def download_models(version_matches, manifest): |
| 74 | + # Download all models if torch version is different than model version |
| 75 | + if not version_matches: |
| 76 | + for n, m in BENCHMARK_MODELS.items(): |
| 77 | + manifest = get(n, m, manifest) |
| 78 | + else: |
| 79 | + for n, m in BENCHMARK_MODELS.items(): |
| 80 | + scripted_filename = "models/" + n + "_scripted.jit.pt" |
| 81 | + traced_filename = "models/" + n + "_traced.jit.pt" |
| 82 | + # Check if model file exists on disk |
| 83 | + if (m["path"] == "both" and os.path.exists(scripted_filename) and os.path.exists(traced_filename)) or \ |
| 84 | + (m["path"] == "script" and os.path.exists(scripted_filename)) or \ |
| 85 | + (m["path"] == "trace" and os.path.exists(traced_filename)): |
| 86 | + print("Skipping {} ".format(n)) |
| 87 | + continue |
| 88 | + manifest = get(n, m, manifest) |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + manifest = None |
| 93 | + version_matches = False |
| 94 | + manifest_exists = False |
| 95 | + |
| 96 | + # Check if Manifest file exists or is empty |
| 97 | + if not os.path.exists(MANIFEST_FILE) or os.stat(MANIFEST_FILE).st_size == 0: |
| 98 | + manifest = {"version": torch_version} |
| 99 | + |
| 100 | + # Creating an empty manifest file for overwriting post setup |
| 101 | + os.system('touch {}'.format(MANIFEST_FILE)) |
| 102 | + else: |
| 103 | + manifest_exists = True |
| 104 | + |
| 105 | + # Load manifest if already exists |
| 106 | + with open(MANIFEST_FILE, 'r') as f: |
| 107 | + manifest = json.load(f) |
| 108 | + if manifest['version'] == torch_version: |
| 109 | + version_matches = True |
| 110 | + else: |
| 111 | + print("Torch version: {} mismatches \ |
| 112 | + with manifest's version: {}. Re-downloading \ |
| 113 | + all models".format(torch_version, manifest['version'])) |
| 114 | + |
| 115 | + # Overwrite the manifest version as current torch version |
| 116 | + manifest['version'] = torch_version |
| 117 | + |
| 118 | + download_models(version_matches, manifest) |
| 119 | + |
| 120 | + # Write updated manifest file to disk |
| 121 | + with open(MANIFEST_FILE, 'r+') as f: |
| 122 | + data = f.read() |
| 123 | + f.seek(0) |
| 124 | + record = json.dumps(manifest) |
| 125 | + f.write(record) |
| 126 | + f.truncate() |
| 127 | + |
| 128 | + |
| 129 | +main() |
0 commit comments