|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +import torch |
| 6 | +import transformers |
| 7 | +from transformers import ( |
| 8 | + AutoConfig, |
| 9 | + AutoModelForCausalLM, |
| 10 | + AutoModelForQuestionAnswering, |
| 11 | + AutoModelForSequenceClassification, |
| 12 | + AutoModelForTokenClassification, |
| 13 | + AutoTokenizer, |
| 14 | + set_seed, |
| 15 | +) |
| 16 | + |
| 17 | +print("Transformers version", transformers.__version__) |
| 18 | +set_seed(1) |
| 19 | +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 20 | + |
| 21 | + |
| 22 | +def dir_path(path_str): |
| 23 | + if os.path.isdir(path_str): |
| 24 | + return path_str |
| 25 | + else: |
| 26 | + print(f"{path_str} does not exist, creating directory") |
| 27 | + os.makedirs(path_str) |
| 28 | + return path_str |
| 29 | + |
| 30 | + |
| 31 | +def transformers_model_dowloader( |
| 32 | + mode, |
| 33 | + pretrained_model_name, |
| 34 | + num_labels, |
| 35 | + do_lower_case, |
| 36 | + max_length, |
| 37 | + torchscript, |
| 38 | + hardware, |
| 39 | + batch_size, |
| 40 | + model_path, |
| 41 | +): |
| 42 | + """This function, save the checkpoint, config file along with tokenizer config and vocab files |
| 43 | + of a transformer model of your choice. |
| 44 | + """ |
| 45 | + print("Download model and tokenizer", pretrained_model_name) |
| 46 | + # loading pre-trained model and tokenizer |
| 47 | + if mode == "sequence_classification": |
| 48 | + config = AutoConfig.from_pretrained( |
| 49 | + pretrained_model_name, num_labels=num_labels, torchscript=torchscript |
| 50 | + ) |
| 51 | + model = AutoModelForSequenceClassification.from_pretrained( |
| 52 | + pretrained_model_name, config=config |
| 53 | + ) |
| 54 | + tokenizer = AutoTokenizer.from_pretrained( |
| 55 | + pretrained_model_name, do_lower_case=do_lower_case |
| 56 | + ) |
| 57 | + elif mode == "question_answering": |
| 58 | + config = AutoConfig.from_pretrained( |
| 59 | + pretrained_model_name, torchscript=torchscript |
| 60 | + ) |
| 61 | + model = AutoModelForQuestionAnswering.from_pretrained( |
| 62 | + pretrained_model_name, config=config |
| 63 | + ) |
| 64 | + tokenizer = AutoTokenizer.from_pretrained( |
| 65 | + pretrained_model_name, do_lower_case=do_lower_case |
| 66 | + ) |
| 67 | + elif mode == "token_classification": |
| 68 | + config = AutoConfig.from_pretrained( |
| 69 | + pretrained_model_name, num_labels=num_labels, torchscript=torchscript |
| 70 | + ) |
| 71 | + model = AutoModelForTokenClassification.from_pretrained( |
| 72 | + pretrained_model_name, config=config |
| 73 | + ) |
| 74 | + tokenizer = AutoTokenizer.from_pretrained( |
| 75 | + pretrained_model_name, do_lower_case=do_lower_case |
| 76 | + ) |
| 77 | + elif mode == "text_generation": |
| 78 | + config = AutoConfig.from_pretrained( |
| 79 | + pretrained_model_name, num_labels=num_labels, torchscript=torchscript |
| 80 | + ) |
| 81 | + model = AutoModelForCausalLM.from_pretrained( |
| 82 | + pretrained_model_name, config=config |
| 83 | + ) |
| 84 | + tokenizer = AutoTokenizer.from_pretrained( |
| 85 | + pretrained_model_name, do_lower_case=do_lower_case |
| 86 | + ) |
| 87 | + |
| 88 | + # NOTE : for demonstration purposes, we do not go through the fine-tune processing here. |
| 89 | + # A Fine_tunining process based on your needs can be added. |
| 90 | + # An example of Fine_tuned model has been provided in the README. |
| 91 | + |
| 92 | + print( |
| 93 | + "Save model and tokenizer/ Torchscript model based on the setting from setup_config", |
| 94 | + pretrained_model_name, |
| 95 | + "in directory", |
| 96 | + model_path, |
| 97 | + ) |
| 98 | + if save_mode == "pretrained": |
| 99 | + model.save_pretrained(model_path) |
| 100 | + tokenizer.save_pretrained(model_path) |
| 101 | + elif save_mode == "torchscript": |
| 102 | + dummy_input = "This is a dummy input for torch jit trace" |
| 103 | + inputs = tokenizer.encode_plus( |
| 104 | + dummy_input, |
| 105 | + max_length=int(max_length), |
| 106 | + pad_to_max_length=True, |
| 107 | + add_special_tokens=True, |
| 108 | + return_tensors="pt", |
| 109 | + ) |
| 110 | + model.to(device).eval() |
| 111 | + if hardware == "neuron": |
| 112 | + import torch_neuron |
| 113 | + |
| 114 | + input_ids = torch.cat([inputs["input_ids"]] * batch_size, 0).to(device) |
| 115 | + attention_mask = torch.cat([inputs["attention_mask"]] * batch_size, 0).to( |
| 116 | + device |
| 117 | + ) |
| 118 | + traced_model = torch_neuron.trace(model, (input_ids, attention_mask)) |
| 119 | + torch.jit.save( |
| 120 | + traced_model, |
| 121 | + os.path.join( |
| 122 | + NEW_DIR, |
| 123 | + "traced_{}_model_neuron_batch_{}.pt".format(model_name, batch_size), |
| 124 | + ), |
| 125 | + ) |
| 126 | + elif hardware == "neuronx": |
| 127 | + import torch_neuronx |
| 128 | + |
| 129 | + input_ids = torch.cat([inputs["input_ids"]] * batch_size, 0).to(device) |
| 130 | + attention_mask = torch.cat([inputs["attention_mask"]] * batch_size, 0).to( |
| 131 | + device |
| 132 | + ) |
| 133 | + traced_model = torch_neuronx.trace(model, (input_ids, attention_mask)) |
| 134 | + torch.jit.save( |
| 135 | + traced_model, |
| 136 | + os.path.join( |
| 137 | + NEW_DIR, |
| 138 | + "traced_{}_model_neuronx_batch_{}.pt".format( |
| 139 | + model_name, batch_size |
| 140 | + ), |
| 141 | + ), |
| 142 | + ) |
| 143 | + else: |
| 144 | + input_ids = inputs["input_ids"].to(device) |
| 145 | + attention_mask = inputs["attention_mask"].to(device) |
| 146 | + traced_model = torch.jit.trace(model, (input_ids, attention_mask)) |
| 147 | + torch.jit.save(traced_model, os.path.join(NEW_DIR, "traced_model.pt")) |
| 148 | + return |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + parser = argparse.ArgumentParser() |
| 153 | + parser.add_argument( |
| 154 | + "--model_path", |
| 155 | + "-o", |
| 156 | + type=dir_path, |
| 157 | + default="model", |
| 158 | + help="Output directory for downloaded model files", |
| 159 | + ) |
| 160 | + parser.add_argument("--cfg", "-c", type=str, required=True, help="Config") |
| 161 | + args = parser.parse_args() |
| 162 | + dirname = os.path.dirname(__file__) |
| 163 | + f = open(args.cfg) |
| 164 | + settings = json.load(f) |
| 165 | + mode = settings["mode"] |
| 166 | + model_name = settings["model_name"] |
| 167 | + num_labels = int(settings["num_labels"]) |
| 168 | + do_lower_case = settings["do_lower_case"] |
| 169 | + max_length = settings["max_length"] |
| 170 | + save_mode = settings["save_mode"] |
| 171 | + if save_mode == "torchscript": |
| 172 | + torchscript = True |
| 173 | + else: |
| 174 | + torchscript = False |
| 175 | + hardware = settings.get("hardware") |
| 176 | + batch_size = int(settings.get("batch_size", "1")) |
| 177 | + |
| 178 | + transformers_model_dowloader( |
| 179 | + mode, |
| 180 | + model_name, |
| 181 | + num_labels, |
| 182 | + do_lower_case, |
| 183 | + max_length, |
| 184 | + torchscript, |
| 185 | + hardware, |
| 186 | + batch_size, |
| 187 | + args.model_path, |
| 188 | + ) |
0 commit comments