DeepSeek-V3/inference/convert.py

128 lines
4.9 KiB
Python
Raw Normal View History

2024-12-26 06:01:57 -05:00
import os
import shutil
import mmap
import threading
2024-12-26 06:01:57 -05:00
from argparse import ArgumentParser
from pathlib import Path
2024-12-26 06:01:57 -05:00
from tqdm import tqdm, trange
from concurrent.futures import ThreadPoolExecutor, as_completed
2024-12-26 06:01:57 -05:00
import torch
from safetensors.torch import safe_open, save_file
from collections import defaultdict
2024-12-26 06:01:57 -05:00
mapping = {
"embed_tokens": ("embed", 0),
"input_layernorm": ("attn_norm", None),
"post_attention_layernorm": ("ffn_norm", None),
"q_proj": ("wq", 0),
"q_a_proj": ("wq_a", None),
"q_a_layernorm": ("q_norm", None),
"q_b_proj": ("wq_b", 0),
"kv_a_proj_with_mqa": ("wkv_a", None),
"kv_a_layernorm": ("kv_norm", None),
"kv_b_proj": ("wkv_b", 0),
"o_proj": ("wo", 1),
"gate": ("gate", None),
"gate_proj": ("w1", 0),
"down_proj": ("w2", 1),
"up_proj": ("w3", 0),
"norm": ("norm", None),
"lm_head": ("head", 0),
"scale": ("scale", None),
}
# Thread Lock for Safe Dictionary Access
state_lock = threading.Lock()
def fast_copy(src: Path, dst: Path):
"""Efficiently copies large files using shutil for optimal memory usage"""
if dst.exists():
dst.unlink() # Remove file if it already exists
if src.stat().st_size < 10 * 1024 * 1024: # If file < 10MB, use shutil
shutil.copyfile(src, dst)
else:
with open(src, "rb") as f_src, open(dst, "wb") as f_dst:
shutil.copyfileobj(f_src, f_dst, length=16*1024*1024)
def copy_token_file(file_path, save_path):
"""Helper function for parallel copying of token files"""
fast_copy(file_path, Path(save_path) / file_path.name)
def inner_safe_open(name: str, f, mp, state_dicts, n_local_experts):
"""Processes tensor files and maps keys correctly"""
with torch.no_grad():
param: torch.Tensor = f.get_tensor(name)
name = name[len("model."):] if name.startswith("model.") else name
name = name.replace("self_attn", "attn").replace("mlp", "ffn")
name = name.replace("weight_scale_inv", "scale").replace("e_score_correction_bias", "bias")
key = name.split(".")[-2]
assert key in mapping
new_key, dim = mapping[key]
name = name.replace(key, new_key)
for i in range(mp):
new_param = param
if "experts" in name and "shared_experts" not in name:
idx = int(name.split(".")[-3])
if idx < i * n_local_experts or idx >= (i + 1) * n_local_experts:
continue
elif dim is not None:
shard_size = param.size(dim) // mp
new_param = param.narrow(dim, i * shard_size, shard_size).contiguous()
# Lock to avoid race conditions
with state_lock:
state_dicts[i][name] = new_param
def process_file(file_path, mp, state_dicts, n_local_experts):
"""Processes a single safetensor file"""
with safe_open(file_path, framework="pt", device="cpu") as f:
for name in f.keys():
if "model.layers.61" not in name:
inner_safe_open(name, f, mp, state_dicts, n_local_experts)
2024-12-26 06:01:57 -05:00
def main(hf_ckpt_path, save_path, n_experts, mp):
"""Converts and saves model checkpoint files into a specified format."""
2024-12-26 06:01:57 -05:00
n_local_experts = n_experts // mp
# Use defaultdict to prevent key errors in multi-threading
state_dicts = [defaultdict(dict) for _ in range(mp)]
file_list = list(Path(hf_ckpt_path).glob("*.safetensors"))
token_files = list(Path(hf_ckpt_path).glob("*token*"))
2024-12-26 06:01:57 -05:00
Path(save_path).mkdir(parents=True, exist_ok=True)
2024-12-26 06:01:57 -05:00
# Parallel Processing with ThreadPoolExecutor
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(process_file, file, mp, state_dicts, n_local_experts): file
for file in file_list
}
for future in tqdm(as_completed(futures), desc="Processing safetensors", total=len(file_list)):
future.result() # Ensure exceptions are raised
# Save processed model shards
for i in trange(mp, desc="Saving model shards"):
save_file(state_dicts[i], os.path.join(save_path, f"model{i}-mp{mp}.safetensors"))
# Parallel Token File Copying
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(copy_token_file, file, save_path): file
for file in token_files
}
for future in tqdm(as_completed(futures), desc="Copying token files", total=len(token_files)):
future.result() # Ensure exceptions are raised
2024-12-26 06:01:57 -05:00
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--hf-ckpt-path", type=str, required=True)
parser.add_argument("--save-path", type=str, required=True)
parser.add_argument("--n-experts", type=int, required=True)
2024-12-31 05:05:55 -05:00
parser.add_argument("--model-parallel", type=int, required=True)
2024-12-26 06:01:57 -05:00
args = parser.parse_args()
assert args.n_experts % args.model_parallel == 0, "n_experts must be divisible by model_parallel"
2024-12-26 06:01:57 -05:00
main(args.hf_ckpt_path, args.save_path, args.n_experts, args.model_parallel)