Naphula commited on
Commit
2e55c41
·
verified ·
1 Parent(s): ca27cf1

Upload 2 files

Browse files
Files changed (2) hide show
  1. model_tools.md +3 -0
  2. pytorch_to_safetensors.py +49 -0
model_tools.md CHANGED
@@ -26,6 +26,9 @@ Tools to enhance LLM quantizations and merging
26
  # [fp32_to_fp16.py](https://huggingface.co/spaces/Naphula/model_tools/blob/main/fp32_to_fp16.py)
27
  - Converts FP32 to FP16 safetensors
28
 
 
 
 
29
  # [textonly_ripper_v2.py](https://huggingface.co/spaces/Naphula/model_tools/blob/main/textonly_ripper_v2.py)
30
  - Converts a sharded, multimodal (text and vision) model into a text-only version. Readme at [textonly_ripper.md](https://huggingface.co/spaces/Naphula/model_tools/blob/main/textonly_ripper.md)
31
 
 
26
  # [fp32_to_fp16.py](https://huggingface.co/spaces/Naphula/model_tools/blob/main/fp32_to_fp16.py)
27
  - Converts FP32 to FP16 safetensors
28
 
29
+ # [pytorch_to_safetensors.py](https://huggingface.co/spaces/Naphula/model_tools/blob/main/pytorch_to_safetensors.py)
30
+ - Converts pytorch bin to safetensors format
31
+
32
  # [textonly_ripper_v2.py](https://huggingface.co/spaces/Naphula/model_tools/blob/main/textonly_ripper_v2.py)
33
  - Converts a sharded, multimodal (text and vision) model into a text-only version. Readme at [textonly_ripper.md](https://huggingface.co/spaces/Naphula/model_tools/blob/main/textonly_ripper.md)
34
 
pytorch_to_safetensors.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+
4
+ # --- AGGRESSIVE FIX: Bypass Security Check ---
5
+ # We must import these modules specifically to patch the function where it is used
6
+ import transformers.modeling_utils
7
+ import transformers.utils.import_utils
8
+
9
+ # Disable the check in both locations
10
+ transformers.modeling_utils.check_torch_load_is_safe = lambda: None
11
+ transformers.utils.import_utils.check_torch_load_is_safe = lambda: None
12
+ # ---------------------------------------------
13
+
14
+ from transformers import AutoModelForCausalLM, AutoTokenizer
15
+
16
+ # 1. Path to your local PyTorch model
17
+ input_path = r"B:\7B\!models--Gryphe--Tiamat-7b"
18
+
19
+ # 2. Path where you want the SafeTensors version
20
+ output_path = r"B:\7B\!models--Gryphe--Tiamat-7b\safe"
21
+
22
+ print(f"Loading model from {input_path}...")
23
+
24
+ # Load the model
25
+ model = AutoModelForCausalLM.from_pretrained(
26
+ input_path,
27
+ torch_dtype=torch.bfloat16,
28
+ device_map="cpu",
29
+ low_cpu_mem_usage=True
30
+ )
31
+
32
+ # Load the tokenizer
33
+ tokenizer = AutoTokenizer.from_pretrained(input_path)
34
+
35
+ print(f"Saving to {output_path}...")
36
+
37
+ if not os.path.exists(output_path):
38
+ os.makedirs(output_path)
39
+
40
+ # 3. Save with safe_serialization=True
41
+ model.save_pretrained(
42
+ output_path,
43
+ safe_serialization=True,
44
+ max_shard_size="5GB"
45
+ )
46
+
47
+ tokenizer.save_pretrained(output_path)
48
+
49
+ print("Conversion complete.")