MCPcopy Create free account
hub / github.com/OpenBMB/ToolBench / split_files

Function split_files

toolbench/model/apply_delta.py:25–67  ·  view source on GitHub ↗
(model_path, tmp_path, split_size)

Source from the content-addressed store, hash-verified

23
24
25def 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
70def apply_delta_low_cpu_mem(base_model_path, target_model_path, delta_path):

Callers 1

apply_delta_low_cpu_memFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected