Spaces:
Sleeping
Sleeping
Sahil Seemant commited on
Commit ·
7e69116
1
Parent(s): 0f06dcb
Final cloud stability fixes for PEFT adapter loading
Browse files- convert_adapters_to_peft.py +99 -0
convert_adapters_to_peft.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import argparse
|
| 4 |
+
from safetensors.torch import load_file, save_file
|
| 5 |
+
|
| 6 |
+
def convert_mlx_to_peft(mlx_dir, peft_dir, base_model):
|
| 7 |
+
os.makedirs(peft_dir, exist_ok=True)
|
| 8 |
+
|
| 9 |
+
# 1. Load MLX weights
|
| 10 |
+
try:
|
| 11 |
+
mlx_weights = load_file(os.path.join(mlx_dir, "adapters.safetensors"))
|
| 12 |
+
except Exception as e:
|
| 13 |
+
print(f"Error loading {mlx_dir}/adapters.safetensors: {e}")
|
| 14 |
+
return
|
| 15 |
+
|
| 16 |
+
# 2. Convert keys to PEFT format
|
| 17 |
+
peft_weights = {}
|
| 18 |
+
target_modules = set()
|
| 19 |
+
|
| 20 |
+
for k, v in mlx_weights.items():
|
| 21 |
+
is_a = k.endswith(".lora_a")
|
| 22 |
+
is_b = k.endswith(".lora_b")
|
| 23 |
+
is_A = k.endswith(".A")
|
| 24 |
+
is_B = k.endswith(".B")
|
| 25 |
+
|
| 26 |
+
if is_a:
|
| 27 |
+
module_path = k[:-7] # remove .lora_a
|
| 28 |
+
new_suffix = ".lora_A.weight"
|
| 29 |
+
elif is_b:
|
| 30 |
+
module_path = k[:-7] # remove .lora_b
|
| 31 |
+
new_suffix = ".lora_B.weight"
|
| 32 |
+
elif is_A:
|
| 33 |
+
module_path = k[:-2] # remove .A
|
| 34 |
+
new_suffix = ".lora_A.weight"
|
| 35 |
+
elif is_B:
|
| 36 |
+
module_path = k[:-2] # remove .B
|
| 37 |
+
new_suffix = ".lora_B.weight"
|
| 38 |
+
else:
|
| 39 |
+
print(f"Skipping unknown key: {k}")
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
target_module_name = module_path.split(".")[-1]
|
| 43 |
+
target_modules.add(target_module_name)
|
| 44 |
+
|
| 45 |
+
new_k = f"base_model.model.{module_path}{new_suffix}"
|
| 46 |
+
peft_weights[new_k] = v
|
| 47 |
+
|
| 48 |
+
save_file(peft_weights, os.path.join(peft_dir, "adapter_model.safetensors"))
|
| 49 |
+
|
| 50 |
+
# 3. Read MLX config to get rank, alpha, dropout
|
| 51 |
+
with open(os.path.join(mlx_dir, "adapter_config.json"), "r") as f:
|
| 52 |
+
mlx_config = json.load(f)
|
| 53 |
+
|
| 54 |
+
# 4. Write PEFT config
|
| 55 |
+
peft_config = {
|
| 56 |
+
"alpha_pattern": {},
|
| 57 |
+
"auto_mapping": None,
|
| 58 |
+
"base_model_name_or_path": base_model,
|
| 59 |
+
"bias": "none",
|
| 60 |
+
"fan_in_fan_out": False,
|
| 61 |
+
"inference_mode": True,
|
| 62 |
+
"init_lora_weights": True,
|
| 63 |
+
"layer_replication": None,
|
| 64 |
+
"layers_pattern": None,
|
| 65 |
+
"layers_to_transform": None,
|
| 66 |
+
"loftq_config": {},
|
| 67 |
+
"lora_alpha": mlx_config.get("alpha", 16),
|
| 68 |
+
"lora_dropout": mlx_config.get("dropout", 0.05),
|
| 69 |
+
"megatron_config": None,
|
| 70 |
+
"megatron_core": "megatron.core",
|
| 71 |
+
"modules_to_save": None,
|
| 72 |
+
"peft_type": "LORA",
|
| 73 |
+
"r": mlx_config.get("rank", 8),
|
| 74 |
+
"rank_pattern": {},
|
| 75 |
+
"revision": None,
|
| 76 |
+
"target_modules": list(target_modules),
|
| 77 |
+
"task_type": "CAUSAL_LM",
|
| 78 |
+
"use_dora": False,
|
| 79 |
+
"use_rslora": False
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
with open(os.path.join(peft_dir, "adapter_config.json"), "w") as f:
|
| 83 |
+
json.dump(peft_config, f, indent=2)
|
| 84 |
+
|
| 85 |
+
print(f"✅ Converted {mlx_dir} to {peft_dir} successfully!")
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
configs = [
|
| 89 |
+
("ministral_adapters_v2", "peft_ministral_adapters_v2", "mistralai/Ministral-3-3B-Instruct-2512"),
|
| 90 |
+
("ministral_adapters_v3", "peft_ministral_adapters_v3", "mistralai/Ministral-3-3B-Instruct-2512"),
|
| 91 |
+
("ministral_dpo_adapters", "peft_ministral_dpo_adapters", "mistralai/Ministral-3-3B-Instruct-2512"),
|
| 92 |
+
("adapters", "peft_adapters", "Qwen/Qwen3.5-4B-Instruct")
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
for in_dir, out_dir, base in configs:
|
| 96 |
+
if os.path.exists(in_dir):
|
| 97 |
+
convert_mlx_to_peft(in_dir, out_dir, base)
|
| 98 |
+
else:
|
| 99 |
+
print(f"Directory not found, skipping: {in_dir}")
|