NemoVonNirgend commited on
Commit
381d23f
·
verified ·
1 Parent(s): c4b35cb

Upload merge_ministral.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. merge_ministral.py +60 -0
merge_ministral.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # dependencies = ["torch", "transformers>=5.0.0rc0", "peft", "accelerate", "huggingface_hub", "safetensors", "mistral-common>=1.8.6"]
3
+ # ///
4
+
5
+ import torch
6
+ import os
7
+ from peft import PeftModel
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
+ from huggingface_hub import HfApi
10
+
11
+ print("=== Ministral 14B LoRA Merge ===")
12
+ print("Base: mistralai/Ministral-3-14B-Base-2512")
13
+ print("LoRA: RoleModel/ministral-14b-reasoning-merged")
14
+ print("Output: RoleModel/ministral-14b-merged-official")
15
+
16
+ print("
17
+ [1/5] Loading base model...")
18
+ base = AutoModelForCausalLM.from_pretrained(
19
+ "mistralai/Ministral-3-14B-Base-2512",
20
+ torch_dtype=torch.bfloat16,
21
+ device_map="auto",
22
+ trust_remote_code=True
23
+ )
24
+ print(f"Base model class: {base.__class__.__name__}")
25
+
26
+ print("
27
+ [2/5] Loading LoRA adapter...")
28
+ model = PeftModel.from_pretrained(base, "RoleModel/ministral-14b-reasoning-merged")
29
+ print("LoRA adapter loaded")
30
+
31
+ print("
32
+ [3/5] Merging weights...")
33
+ merged = model.merge_and_unload()
34
+ print("Merge complete")
35
+
36
+ print("
37
+ [4/5] Saving merged model...")
38
+ merged.save_pretrained("./merged-model", safe_serialization=True)
39
+ print("Model saved locally")
40
+
41
+ print("
42
+ [4b/5] Saving tokenizer...")
43
+ tok = AutoTokenizer.from_pretrained("mistralai/Ministral-3-14B-Base-2512", trust_remote_code=True)
44
+ tok.save_pretrained("./merged-model")
45
+ print("Tokenizer saved")
46
+
47
+ print("
48
+ [5/5] Pushing to Hub...")
49
+ api = HfApi()
50
+ api.create_repo("RoleModel/ministral-14b-merged-official", exist_ok=True, private=True, token=os.environ.get("HF_TOKEN"))
51
+ api.upload_folder(
52
+ folder_path="./merged-model",
53
+ repo_id="RoleModel/ministral-14b-merged-official",
54
+ repo_type="model",
55
+ token=os.environ.get("HF_TOKEN")
56
+ )
57
+
58
+ print("
59
+ === DONE ===")
60
+ print("Merged model: https://huggingface.co/RoleModel/ministral-14b-merged-official")