(hf_model_path, new_config_path, output_path, trust_remote_code: bool = False)
| 75 | |
| 76 | |
| 77 | def init_random_model(hf_model_path, new_config_path, output_path, trust_remote_code: bool = False): |
| 78 | config = AutoConfig.from_pretrained(hf_model_path, trust_remote_code=trust_remote_code) |
| 79 | tokenizer = AutoTokenizer.from_pretrained(hf_model_path, trust_remote_code=trust_remote_code) |
| 80 | config_dict = PretrainedConfig.get_config_dict(hf_model_path)[0] |
| 81 | print(config_dict) |
| 82 | with open(new_config_path) as f: |
| 83 | new_config_dict = json.load(f) |
| 84 | check_configs(config_dict, new_config_dict) |
| 85 | config_dict.update(new_config_dict) |
| 86 | new_confg = config.from_dict(config_dict) |
| 87 | print(f"new_config: {new_confg}") |
| 88 | if trust_remote_code: |
| 89 | model = AutoModelForCausalLM.from_pretrained( |
| 90 | hf_model_path, config=new_confg, trust_remote_code=trust_remote_code |
| 91 | ) |
| 92 | else: |
| 93 | model = AutoModelForCausalLM.from_config(new_confg) |
| 94 | model.save_pretrained(output_path) |
| 95 | tokenizer.save_pretrained(output_path) |
| 96 | new_confg.save_pretrained(output_path) |
| 97 | print(f"Random model initialized and saved to {output_path}") |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
no test coverage detected