DS metadata
Browse files- slimshard.py +91 -8
slimshard.py
CHANGED
|
@@ -2711,8 +2711,9 @@ _LAYER_SUFFIX_TO_GGUF = {
|
|
| 2711 |
"self_attn.o_proj.weight": "attn_output.weight",
|
| 2712 |
"attn.o_proj.weight": "attn_output.weight",
|
| 2713 |
"attention.wo.weight": "attn_output.weight",
|
| 2714 |
-
"attn.wo_b.weight": "
|
| 2715 |
-
"attn.wo_a.weight": "
|
|
|
|
| 2716 |
"self_attn.q_a_proj.weight": "attn_q_a.weight",
|
| 2717 |
"attn.wq_a.weight": "attn_q_a.weight",
|
| 2718 |
"self_attn.q_a_layernorm.weight": "attn_q_a_norm.weight",
|
|
@@ -2720,11 +2721,17 @@ _LAYER_SUFFIX_TO_GGUF = {
|
|
| 2720 |
"self_attn.q_b_proj.weight": "attn_q_b.weight",
|
| 2721 |
"attn.wq_b.weight": "attn_q_b.weight",
|
| 2722 |
"self_attn.kv_a_proj_with_mqa.weight": "attn_kv_a_mqa.weight",
|
| 2723 |
-
"attn.wkv.weight": "
|
| 2724 |
"self_attn.kv_a_layernorm.weight": "attn_kv_a_norm.weight",
|
| 2725 |
"attn.kv_norm.weight": "attn_kv_a_norm.weight",
|
| 2726 |
"self_attn.kv_b_proj.weight": "attn_kv_b.weight",
|
| 2727 |
"self_attn.g_proj.weight": "attn_gate.weight",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2728 |
"mlp.gate_proj.weight": "ffn_gate.weight",
|
| 2729 |
"feed_forward.w1.weight": "ffn_gate.weight",
|
| 2730 |
"mlp.up_proj.weight": "ffn_up.weight",
|
|
@@ -2777,7 +2784,9 @@ def _build_gguf_kv_pairs(
|
|
| 2777 |
model_name: str,
|
| 2778 |
) -> list[tuple[str, int, bytes]]:
|
| 2779 |
raw_mtype = str(arch.get("model_type", "llama")).lower()
|
| 2780 |
-
if "
|
|
|
|
|
|
|
| 2781 |
g_arch = "deepseek2"
|
| 2782 |
elif "qwen" in raw_mtype:
|
| 2783 |
g_arch = "qwen2moe" if int(arch.get("num_experts") or 0) > 0 else "qwen2"
|
|
@@ -2792,15 +2801,19 @@ def _build_gguf_kv_pairs(
|
|
| 2792 |
("general.quantization_version", 4, struct.pack("<I", 2)),
|
| 2793 |
]
|
| 2794 |
|
| 2795 |
-
def add_u32(k: str, v: int) -> None:
|
| 2796 |
-
if v is not None and int(v) > 0:
|
| 2797 |
kvs.append((k, 4, struct.pack("<I", int(v))))
|
| 2798 |
|
| 2799 |
def add_f32(k: str, v: float) -> None:
|
| 2800 |
if v is not None:
|
| 2801 |
kvs.append((k, 6, struct.pack("<f", float(v))))
|
| 2802 |
|
| 2803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2804 |
add_u32(f"{g_arch}.embedding_length", int(arch.get("hidden_size", 0)))
|
| 2805 |
add_u32(
|
| 2806 |
f"{g_arch}.feed_forward_length",
|
|
@@ -2821,11 +2834,45 @@ def _build_gguf_kv_pairs(
|
|
| 2821 |
f"{g_arch}.expert_used_count",
|
| 2822 |
int(arch.get("num_experts_per_token") or arch.get("num_experts_per_tok") or 0),
|
| 2823 |
)
|
| 2824 |
-
add_u32(f"{g_arch}.context_length", int(arch.get("max_position_embeddings", 32768)))
|
| 2825 |
add_u32(f"{g_arch}.vocab_size", int(arch.get("vocab_size", 0)))
|
| 2826 |
add_u32(f"{g_arch}.attention.q_lora_rank", int(arch.get("q_lora_rank") or 0))
|
| 2827 |
add_u32(f"{g_arch}.attention.kv_lora_rank", int(arch.get("kv_lora_rank") or 0))
|
| 2828 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2829 |
vocab_sz = int(arch.get("vocab_size", 0))
|
| 2830 |
if tokenizer_path is not None and tokenizer_path.exists():
|
| 2831 |
try:
|
|
@@ -2963,11 +3010,18 @@ def export_safetensors_to_gguf(
|
|
| 2963 |
for gk in expert_groups:
|
| 2964 |
expert_groups[gk].sort(key=lambda x: x[0])
|
| 2965 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2966 |
# Build work items: ("single", src_key, scale_key, gguf_name) or ("experts", (lidx, proj), items, gguf_name)
|
| 2967 |
work_items: list[tuple] = []
|
| 2968 |
for k in keys:
|
| 2969 |
if k in consumed_keys:
|
| 2970 |
continue
|
|
|
|
|
|
|
| 2971 |
sc_k = None
|
| 2972 |
if k.endswith(".weight"):
|
| 2973 |
for sfx in (".scale", ".weight_scale_inv", ".scales", ".weight_scale"):
|
|
@@ -3003,6 +3057,33 @@ def export_safetensors_to_gguf(
|
|
| 3003 |
data_offset = 0
|
| 3004 |
payload_cache: dict[tuple, tuple[int, tuple[int, ...], int, int]] = {}
|
| 3005 |
stats = {"tensors": 0, "q2_k": 0, "q4_0": 0, "bf16": 0, "f32": 0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3006 |
total_items = len(work_items)
|
| 3007 |
|
| 3008 |
def _dequant_single_to_f32_torch(w_key: str, s_key: str | None) -> "torch.Tensor":
|
|
@@ -3075,6 +3156,8 @@ def export_safetensors_to_gguf(
|
|
| 3075 |
_, w_key, s_key, gname = item
|
| 3076 |
sl = f.get_slice(w_key)
|
| 3077 |
t_shape = tuple(sl.get_shape())
|
|
|
|
|
|
|
| 3078 |
g_dims = tuple(reversed(t_shape)) if t_shape else (1,)
|
| 3079 |
tgt = _choose_gguf_target(gname, t_shape or (1,))
|
| 3080 |
|
|
|
|
| 2711 |
"self_attn.o_proj.weight": "attn_output.weight",
|
| 2712 |
"attn.o_proj.weight": "attn_output.weight",
|
| 2713 |
"attention.wo.weight": "attn_output.weight",
|
| 2714 |
+
"attn.wo_b.weight": "attn_output_b.weight",
|
| 2715 |
+
"attn.wo_a.weight": "attn_output_a.weight",
|
| 2716 |
+
"attn.attn_sink": "attn_sinks.weight",
|
| 2717 |
"self_attn.q_a_proj.weight": "attn_q_a.weight",
|
| 2718 |
"attn.wq_a.weight": "attn_q_a.weight",
|
| 2719 |
"self_attn.q_a_layernorm.weight": "attn_q_a_norm.weight",
|
|
|
|
| 2721 |
"self_attn.q_b_proj.weight": "attn_q_b.weight",
|
| 2722 |
"attn.wq_b.weight": "attn_q_b.weight",
|
| 2723 |
"self_attn.kv_a_proj_with_mqa.weight": "attn_kv_a_mqa.weight",
|
| 2724 |
+
"attn.wkv.weight": "attn_kv.weight",
|
| 2725 |
"self_attn.kv_a_layernorm.weight": "attn_kv_a_norm.weight",
|
| 2726 |
"attn.kv_norm.weight": "attn_kv_a_norm.weight",
|
| 2727 |
"self_attn.kv_b_proj.weight": "attn_kv_b.weight",
|
| 2728 |
"self_attn.g_proj.weight": "attn_gate.weight",
|
| 2729 |
+
"hc_attn_fn": "hc_attn_fn.weight",
|
| 2730 |
+
"hc_attn_base": "hc_attn_base.weight",
|
| 2731 |
+
"hc_attn_scale": "hc_attn_scale.weight",
|
| 2732 |
+
"hc_ffn_fn": "hc_ffn_fn.weight",
|
| 2733 |
+
"hc_ffn_base": "hc_ffn_base.weight",
|
| 2734 |
+
"hc_ffn_scale": "hc_ffn_scale.weight",
|
| 2735 |
"mlp.gate_proj.weight": "ffn_gate.weight",
|
| 2736 |
"feed_forward.w1.weight": "ffn_gate.weight",
|
| 2737 |
"mlp.up_proj.weight": "ffn_up.weight",
|
|
|
|
| 2784 |
model_name: str,
|
| 2785 |
) -> list[tuple[str, int, bytes]]:
|
| 2786 |
raw_mtype = str(arch.get("model_type", "llama")).lower()
|
| 2787 |
+
if "deepseek_v4" in raw_mtype or "deepseek4" in raw_mtype:
|
| 2788 |
+
g_arch = "deepseek4"
|
| 2789 |
+
elif "deepseek" in raw_mtype:
|
| 2790 |
g_arch = "deepseek2"
|
| 2791 |
elif "qwen" in raw_mtype:
|
| 2792 |
g_arch = "qwen2moe" if int(arch.get("num_experts") or 0) > 0 else "qwen2"
|
|
|
|
| 2801 |
("general.quantization_version", 4, struct.pack("<I", 2)),
|
| 2802 |
]
|
| 2803 |
|
| 2804 |
+
def add_u32(k: str, v: int, allow_zero: bool = False) -> None:
|
| 2805 |
+
if v is not None and (allow_zero or int(v) > 0):
|
| 2806 |
kvs.append((k, 4, struct.pack("<I", int(v))))
|
| 2807 |
|
| 2808 |
def add_f32(k: str, v: float) -> None:
|
| 2809 |
if v is not None:
|
| 2810 |
kvs.append((k, 6, struct.pack("<f", float(v))))
|
| 2811 |
|
| 2812 |
+
def add_bool(k: str, v: bool) -> None:
|
| 2813 |
+
kvs.append((k, 7, struct.pack("<B", 1 if v else 0)))
|
| 2814 |
+
|
| 2815 |
+
n_layers = int(arch.get("num_hidden_layers", 0))
|
| 2816 |
+
add_u32(f"{g_arch}.block_count", n_layers)
|
| 2817 |
add_u32(f"{g_arch}.embedding_length", int(arch.get("hidden_size", 0)))
|
| 2818 |
add_u32(
|
| 2819 |
f"{g_arch}.feed_forward_length",
|
|
|
|
| 2834 |
f"{g_arch}.expert_used_count",
|
| 2835 |
int(arch.get("num_experts_per_token") or arch.get("num_experts_per_tok") or 0),
|
| 2836 |
)
|
| 2837 |
+
add_u32(f"{g_arch}.context_length", min(int(arch.get("max_position_embeddings", 32768)), 65536))
|
| 2838 |
add_u32(f"{g_arch}.vocab_size", int(arch.get("vocab_size", 0)))
|
| 2839 |
add_u32(f"{g_arch}.attention.q_lora_rank", int(arch.get("q_lora_rank") or 0))
|
| 2840 |
add_u32(f"{g_arch}.attention.kv_lora_rank", int(arch.get("kv_lora_rank") or 0))
|
| 2841 |
|
| 2842 |
+
if g_arch == "deepseek4":
|
| 2843 |
+
head_dim = int(arch.get("head_dim", 512))
|
| 2844 |
+
rope_dim = int(arch.get("qk_rope_head_dim", 64))
|
| 2845 |
+
add_u32("deepseek4.attention.key_length", head_dim)
|
| 2846 |
+
add_u32("deepseek4.attention.value_length", head_dim)
|
| 2847 |
+
add_u32("deepseek4.rope.dimension_count", rope_dim)
|
| 2848 |
+
add_f32("deepseek4.rope.freq_base", float(arch.get("rope_theta", 10000.0)))
|
| 2849 |
+
add_u32("deepseek4.nextn_predict_layers", 0, allow_zero=True)
|
| 2850 |
+
add_u32("deepseek4.attention.sliding_window", int(arch.get("sliding_window", 128)))
|
| 2851 |
+
add_u32("deepseek4.expert_shared_count", int(arch.get("n_shared_experts", 1)))
|
| 2852 |
+
add_f32("deepseek4.expert_weights_scale", float(arch.get("routed_scaling_factor", 1.5)))
|
| 2853 |
+
add_bool("deepseek4.expert_weights_norm", bool(arch.get("norm_topk_prob", True)))
|
| 2854 |
+
add_f32("deepseek4.swiglu_clamp_exp", float(arch.get("swiglu_limit", 10.0)))
|
| 2855 |
+
add_f32("deepseek4.swiglu_clamp_shexp", float(arch.get("swiglu_limit", 10.0)))
|
| 2856 |
+
add_u32("deepseek4.attention.indexer.head_count", int(arch.get("index_n_heads", 32)))
|
| 2857 |
+
add_u32("deepseek4.attention.indexer.key_length", int(arch.get("index_head_dim", 128)))
|
| 2858 |
+
add_u32("deepseek4.attention.indexer.top_k", int(arch.get("index_topk", 512)))
|
| 2859 |
+
add_u32("deepseek4.attention.output_group_count", int(arch.get("o_groups", 8)))
|
| 2860 |
+
add_u32("deepseek4.attention.output_lora_rank", int(arch.get("o_lora_rank", 1024)))
|
| 2861 |
+
add_f32("deepseek4.attention.compress_rope_freq_base", float(arch.get("compress_rope_theta", 160000.0)))
|
| 2862 |
+
add_u32("deepseek4.hyper_connection.count", int(arch.get("hc_mult", 4)))
|
| 2863 |
+
add_u32("deepseek4.hyper_connection.sinkhorn_iterations", int(arch.get("hc_sinkhorn_iters", 20)))
|
| 2864 |
+
add_f32("deepseek4.hyper_connection.epsilon", float(arch.get("hc_eps", 1.0e-6)))
|
| 2865 |
+
add_u32("deepseek4.hash_layer_count", 0, allow_zero=True)
|
| 2866 |
+
c_ratios = np.zeros(max(n_layers, 1), dtype="<u4")
|
| 2867 |
+
kvs.append(
|
| 2868 |
+
(
|
| 2869 |
+
"deepseek4.attention.compress_ratios",
|
| 2870 |
+
9,
|
| 2871 |
+
struct.pack("<IQ", 4, len(c_ratios)) + c_ratios.tobytes(),
|
| 2872 |
+
)
|
| 2873 |
+
)
|
| 2874 |
+
add_u32("deepseek4.expert_gating_func", 4)
|
| 2875 |
+
|
| 2876 |
vocab_sz = int(arch.get("vocab_size", 0))
|
| 2877 |
if tokenizer_path is not None and tokenizer_path.exists():
|
| 2878 |
try:
|
|
|
|
| 3010 |
for gk in expert_groups:
|
| 3011 |
expert_groups[gk].sort(key=lambda x: x[0])
|
| 3012 |
|
| 3013 |
+
is_dsv4 = "deepseek_v4" in str(arch.get("model_type", "")).lower()
|
| 3014 |
+
o_groups = int(arch.get("o_groups", 8))
|
| 3015 |
+
o_lora_rank = int(arch.get("o_lora_rank", 1024))
|
| 3016 |
+
hc_mult = int(arch.get("hc_mult", 4))
|
| 3017 |
+
|
| 3018 |
# Build work items: ("single", src_key, scale_key, gguf_name) or ("experts", (lidx, proj), items, gguf_name)
|
| 3019 |
work_items: list[tuple] = []
|
| 3020 |
for k in keys:
|
| 3021 |
if k in consumed_keys:
|
| 3022 |
continue
|
| 3023 |
+
if is_dsv4 and (".compressor." in k or ".indexer." in k):
|
| 3024 |
+
continue
|
| 3025 |
sc_k = None
|
| 3026 |
if k.endswith(".weight"):
|
| 3027 |
for sfx in (".scale", ".weight_scale_inv", ".scales", ".weight_scale"):
|
|
|
|
| 3057 |
data_offset = 0
|
| 3058 |
payload_cache: dict[tuple, tuple[int, tuple[int, ...], int, int]] = {}
|
| 3059 |
stats = {"tensors": 0, "q2_k": 0, "q4_0": 0, "bf16": 0, "f32": 0}
|
| 3060 |
+
|
| 3061 |
+
if is_dsv4:
|
| 3062 |
+
last_l = max(int(arch.get("num_hidden_layers", 40)) - 1, 0)
|
| 3063 |
+
fn_k = f"layers.{last_l}.hc_ffn_fn"
|
| 3064 |
+
bs_k = f"layers.{last_l}.hc_ffn_base"
|
| 3065 |
+
sc_k = f"layers.{last_l}.hc_ffn_scale"
|
| 3066 |
+
if fn_k in keys_set and bs_k in keys_set and sc_k in keys_set:
|
| 3067 |
+
fn_t = f.get_tensor(fn_k).float()[:hc_mult].contiguous()
|
| 3068 |
+
bs_t = f.get_tensor(bs_k).float()[:hc_mult].contiguous()
|
| 3069 |
+
sc_t = f.get_tensor(sc_k).float()[:1].contiguous()
|
| 3070 |
+
for g_hc_name, hc_t in (
|
| 3071 |
+
("output_hc_fn.weight", fn_t),
|
| 3072 |
+
("output_hc_base.weight", bs_t),
|
| 3073 |
+
("output_hc_scale.weight", sc_t),
|
| 3074 |
+
):
|
| 3075 |
+
pad = _gguf_align(data_offset) - data_offset
|
| 3076 |
+
if pad > 0:
|
| 3077 |
+
tmp_data.write(b"\x00" * pad)
|
| 3078 |
+
data_offset += pad
|
| 3079 |
+
raw_hc = hc_t.cpu().numpy().astype("<f4").tobytes()
|
| 3080 |
+
gguf_tensors.append((g_hc_name, tuple(reversed(hc_t.shape)), _GGML_F32, data_offset))
|
| 3081 |
+
tmp_data.write(raw_hc)
|
| 3082 |
+
data_offset += len(raw_hc)
|
| 3083 |
+
stats["tensors"] += 1
|
| 3084 |
+
stats["f32"] += 1
|
| 3085 |
+
tmp_data.flush()
|
| 3086 |
+
|
| 3087 |
total_items = len(work_items)
|
| 3088 |
|
| 3089 |
def _dequant_single_to_f32_torch(w_key: str, s_key: str | None) -> "torch.Tensor":
|
|
|
|
| 3156 |
_, w_key, s_key, gname = item
|
| 3157 |
sl = f.get_slice(w_key)
|
| 3158 |
t_shape = tuple(sl.get_shape())
|
| 3159 |
+
if is_dsv4 and gname.endswith(".attn_output_a.weight") and len(t_shape) == 2 and t_shape[0] == o_groups * o_lora_rank:
|
| 3160 |
+
t_shape = (o_groups, o_lora_rank, t_shape[1])
|
| 3161 |
g_dims = tuple(reversed(t_shape)) if t_shape else (1,)
|
| 3162 |
tgt = _choose_gguf_target(gname, t_shape or (1,))
|
| 3163 |
|