notforu777's picture
Card, and the conversion mapping for the public checkpoint (prep.py, converter patch)
7fb7a5a verified
Raw History Blame Contribute Delete
3.78 kB
"""Make the public tencent/Hy4-flash checkpoint readable by the converter of llama.cpp PR #32517.
The PR's converter was written against Tencent's internal export. The public release (rev d88c9dfd) differs in
four ways, all of them naming; the weights are the same tensors:
1. architectures HYV4FlashForConditionalGeneration / HYV4FlashForCausalLM (PR: HYV4NextVL... / HYV4Next...)
2. layer_types and rope_parameters say "deepseek_sparse_attention" (PR: "sparse_attention")
3. no engram_enable key (PR: engram_enable true or Engram is dropped)
4. the Engram token map is the I32 tensor model.layers.{i-1}.engram_layer.lookup_table, the same in all four
Engram layers (PR: engram_vocab_map.npz next to the config)
The compressed vocabulary size is lookup_table.max() + 1, as vLLM's loader takes it (vllm PR #68296, engram.py
load_lookup); validate.py checks the resulting token map and hash multipliers against the vendor's own GGUF.
The vision tower converter wants preprocessor_config.json (min_pixels / max_pixels): it is the image_processor
section of processor_config.json.
python prep.py /work/src # idempotent; the original config stays as config.json.orig
"""
import json
import os
import shutil
import sys
import numpy as np
from safetensors import safe_open
src = sys.argv[1]
cfg_p = os.path.join(src, "config.json")
if not os.path.exists(cfg_p + ".orig"):
shutil.copy(cfg_p, cfg_p + ".orig")
cfg = json.load(open(cfg_p + ".orig"))
tc = cfg["text_config"]
assert cfg["architectures"] == ["HYV4FlashForConditionalGeneration"], cfg["architectures"]
assert tc["architectures"] == ["HYV4FlashForCausalLM"], tc["architectures"]
assert "engram_enable" not in tc and not os.path.exists(os.path.join(src, "engram_vocab_map.npz.orig"))
cfg["architectures"] = ["HYV4NextVLForConditionalGeneration"]
tc["architectures"] = ["HYV4NextForCausalLM"]
types = set(tc["layer_types"])
assert types == {"deepseek_sparse_attention", "sliding_attention"}, types
tc["layer_types"] = ["sparse_attention" if t == "deepseek_sparse_attention" else t for t in tc["layer_types"]]
rp = tc["rope_parameters"]
assert set(rp) == {"deepseek_sparse_attention", "sliding_attention"}, set(rp)
rp["sparse_attention"] = rp.pop("deepseek_sparse_attention")
tc["engram_enable"] = True
# the token map
wm = json.load(open(os.path.join(src, "model.safetensors.index.json")))["weight_map"]
maps = []
for i in sorted(int(x) for x in tc["engram_layer_ids"]):
name = f"model.layers.{i - 1}.engram_layer.lookup_table"
with safe_open(os.path.join(src, wm[name]), "pt") as f:
maps.append(f.get_tensor(name).numpy().astype(np.int64))
for m in maps[1:]:
assert np.array_equal(m, maps[0]), "Engram layers carry different token maps"
table = maps[0]
assert table.ndim == 1 and table.min() >= 0 and len(table) <= tc["vocab_size"], (table.shape, table.min())
compressed = int(table.max()) + 1
meta = {"compressed_vocab_size": compressed, "engram_seed": int(tc.get("engram_seed", 0)),
"source": "tencent/Hy4-flash lookup_table tensor (prep.py)"}
np.savez(os.path.join(src, "engram_vocab_map.npz"), lookup_table=table,
metadata=np.frombuffer(json.dumps(meta).encode(), dtype=np.uint8))
pc = json.load(open(os.path.join(src, "processor_config.json")))
json.dump(pc["image_processor"], open(os.path.join(src, "preprocessor_config.json"), "w"), indent=1)
json.dump(cfg, open(cfg_p, "w"), indent=1)
print(json.dumps({"token_map_len": len(table), "compressed_vocab_size": compressed, "vocab_size": tc["vocab_size"],
"engram_layers": tc["engram_layer_ids"], "sparse_layers": tc["layer_types"].count("sparse_attention")}))