(model_path, tmp_path, split_size)
| 23 | |
| 24 | |
| 25 | def split_files(model_path, tmp_path, split_size): |
| 26 | if not os.path.exists(model_path): |
| 27 | model_path = snapshot_download(repo_id=model_path) |
| 28 | if not os.path.exists(tmp_path): |
| 29 | os.makedirs(tmp_path) |
| 30 | |
| 31 | file_pattern = os.path.join(model_path, "pytorch_model-*.bin") |
| 32 | files = glob.glob(file_pattern) |
| 33 | |
| 34 | part = 0 |
| 35 | try: |
| 36 | for file_path in tqdm(files): |
| 37 | state_dict = torch.load(file_path) |
| 38 | new_state_dict = {} |
| 39 | |
| 40 | current_size = 0 |
| 41 | for name, param in state_dict.items(): |
| 42 | param_size = param.numel() * param.element_size() |
| 43 | |
| 44 | if current_size + param_size > split_size: |
| 45 | new_file_name = f"pytorch_model-{part}.bin" |
| 46 | new_file_path = os.path.join(tmp_path, new_file_name) |
| 47 | torch.save(new_state_dict, new_file_path) |
| 48 | current_size = 0 |
| 49 | new_state_dict = None |
| 50 | gc.collect() |
| 51 | new_state_dict = {} |
| 52 | part += 1 |
| 53 | |
| 54 | new_state_dict[name] = param |
| 55 | current_size += param_size |
| 56 | |
| 57 | new_file_name = f"pytorch_model-{part}.bin" |
| 58 | new_file_path = os.path.join(tmp_path, new_file_name) |
| 59 | torch.save(new_state_dict, new_file_path) |
| 60 | new_state_dict = None |
| 61 | gc.collect() |
| 62 | new_state_dict = {} |
| 63 | part += 1 |
| 64 | except Exception as e: |
| 65 | print(f"An error occurred during split_files: {e}") |
| 66 | shutil.rmtree(tmp_path) |
| 67 | raise |
| 68 | |
| 69 | |
| 70 | def apply_delta_low_cpu_mem(base_model_path, target_model_path, delta_path): |
no outgoing calls
no test coverage detected