Felldude commited on
Commit
703495c
·
verified ·
1 Parent(s): 75c887e

Delete combine.py

Browse files
Files changed (1) hide show
  1. combine.py +0 -42
combine.py DELETED
@@ -1,42 +0,0 @@
1
- import torch
2
- import os
3
- from safetensors.torch import save_file, load_file
4
-
5
- def load_shards(shard_dir, extension='.bin'):
6
- """Load all model shard files from a directory, either .bin or .safetensors."""
7
- state_dict = {}
8
- for filename in os.listdir(shard_dir):
9
- if filename.endswith(extension):
10
- filepath = os.path.join(shard_dir, filename)
11
- print(f"Loading shard {filepath}...")
12
- if extension == '.bin':
13
- shard_dict = torch.load(filepath, map_location='cpu')
14
- elif extension == '.safetensors':
15
- shard_dict = load_file(filepath)
16
- state_dict.update(shard_dict)
17
- return state_dict
18
-
19
- def convert_shards_to_safetensors(shard_dir, output_safetensors_path):
20
- """Convert multiple PyTorch .bin or Safetensors .safetensors shard files to a single Safetensors .safetensors file."""
21
-
22
- # Load .bin + .safetensors shards
23
- state_dict = load_shards(shard_dir, extension='.bin')
24
- state_dict.update(load_shards(shard_dir, extension='.safetensors'))
25
-
26
- # Clone tensors only — do NOT convert dtype
27
- for key in state_dict.keys():
28
- # Ensure no shared storage issues; dtype is kept as-is
29
- state_dict[key] = state_dict[key].clone()
30
-
31
- # Save combined shards
32
- print(f"Saving combined model to {output_safetensors_path}...")
33
- save_file(state_dict, output_safetensors_path)
34
-
35
- print("Conversion successful! (Dtypes preserved)")
36
-
37
- if __name__ == "__main__":
38
- current_directory = os.path.dirname(os.path.abspath(__file__))
39
- shard_directory = current_directory
40
- output_safetensors_file = os.path.join(current_directory, "combined_model.safetensors")
41
-
42
- convert_shards_to_safetensors(shard_directory, output_safetensors_file)