| 35 | return bit_size // 8 |
| 36 | |
| 37 | def save_hf_format(model, tokenizer, args, sub_folder=""): |
| 38 | model_to_save = model.module if hasattr(model, 'module') else model |
| 39 | CONFIG_NAME = "config.json" |
| 40 | output_dir = os.path.join(args.output_dir, sub_folder) |
| 41 | os.makedirs(output_dir, exist_ok=True) |
| 42 | output_config_file = os.path.join(output_dir, CONFIG_NAME) |
| 43 | save_dict = model_to_save.state_dict() |
| 44 | for key in list(save_dict.keys()): |
| 45 | if "lora_" in key: |
| 46 | del save_dict[key] |
| 47 | model_to_save.save_pretrained(output_dir, state_dict=save_dict) |
| 48 | model_to_save.config.to_json_file(output_config_file) |
| 49 | tokenizer.save_pretrained(output_dir) |
| 50 | # for models not in AutoModel, copy python module files |
| 51 | train_from_model_path = model_to_save.config._name_or_path |
| 52 | if os.path.exists(train_from_model_path): |
| 53 | for filename in os.listdir(train_from_model_path): |
| 54 | if filename.endswith(".py"): |
| 55 | shutil.copy(os.path.join(train_from_model_path, filename), os.path.join(output_dir, filename)) |
| 56 | |
| 57 | |
| 58 | def set_random_seed(seed): |