Naphula commited on
Commit
12e0292
·
verified ·
1 Parent(s): 13c9739

Upload fp32_to_fp16.py

Browse files
Files changed (1) hide show
  1. fp32_to_fp16.py +40 -0
fp32_to_fp16.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import os
4
+
5
+ # --- YOU MUST UPDATE THESE TWO PATHS ---
6
+ # Path to the directory where your FP32 model is stored locally
7
+ input_dir = "A:\LLM\.cache\huggingface\hub\models--wzhouad--gemma-2-9b-it-WPO-HB"
8
+
9
+ # Path to the directory where the converted FP16 model will be saved
10
+ output_dir = "A:\LLM\.cache\huggingface\hub\models--wzhouad--gemma-2-9b-it-WPO-HB_FP16"
11
+ # -------------------------------------
12
+
13
+ # Make sure the output directory exists
14
+ if not os.path.exists(output_dir):
15
+ os.makedirs(output_dir)
16
+
17
+ # Load the tokenizer from the local path
18
+ print(f"Loading tokenizer from {input_dir}...")
19
+ tokenizer = AutoTokenizer.from_pretrained(input_dir)
20
+
21
+ # Load the model in FP32 from the local path
22
+ print(f"Loading FP32 model from {input_dir}...")
23
+ model = AutoModelForCausalLM.from_pretrained(
24
+ input_dir,
25
+ torch_dtype=torch.float32,
26
+ device_map="cpu"
27
+ # device_map="auto" # use this if you have enough GPU VRAM
28
+ )
29
+
30
+ # Convert the model to FP16 and save it to the new local directory
31
+ print("Converting model to FP16 and saving to disk...")
32
+ model.half().save_pretrained(
33
+ output_dir,
34
+ safe_serialization=True,
35
+ max_shard_size="5GB"
36
+ )
37
+ tokenizer.save_pretrained(output_dir)
38
+
39
+ print(f"Model successfully converted and saved to {output_dir}")
40
+ print("You can now use this new FP16 model in your mergekit config.yaml.")