Record the max core and memory clocks, sample the memory clock during the run, and add a max_ctx note: vLLM's KV cache size from its log, or for llama.cpp an estimate from free VRAM and the GGUF's KV bytes per token (dev/gguf_kv.py). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d6cf8777be
commit
a677cdcb2a
12 files changed
+527
-54
No files matched your search
@@ -0,0 +1,73 @@
|
||||
"""gguf_kv.py: the header reader and the KV elements per token."""
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import gguf_kv # noqa: E402
|
||||
|
||||
|
||||
def write_gguf(path, meta):
|
||||
def s(x):
|
||||
b = x.encode()
|
||||
return struct.pack("<Q", len(b)) + b
|
||||
|
||||
def val(v):
|
||||
if isinstance(v, bool):
|
||||
return struct.pack("<I?", 7, v)
|
||||
if isinstance(v, int):
|
||||
return struct.pack("<II", 4, v)
|
||||
if isinstance(v, str):
|
||||
return struct.pack("<I", 8) + s(v)
|
||||
if isinstance(v, list) and v and isinstance(v[0], str):
|
||||
return struct.pack("<IIQ", 9, 8, len(v)) + b"".join(s(x) for x in v)
|
||||
if isinstance(v, list) and v and isinstance(v[0], bool):
|
||||
return struct.pack("<IIQ", 9, 7, len(v)) + b"".join(struct.pack("<?", x) for x in v)
|
||||
return struct.pack("<IIQ", 9, 4, len(v)) + b"".join(struct.pack("<I", x) for x in v)
|
||||
|
||||
body = b"".join(s(k) + val(v) for k, v in meta.items())
|
||||
path.write_bytes(b"GGUF" + struct.pack("<IQQ", 3, 0, len(meta)) + body)
|
||||
return str(path)
|
||||
|
||||
|
||||
def test_dense(tmp_path):
|
||||
p = write_gguf(tmp_path / "m.gguf", {
|
||||
"general.architecture": "llama", "llama.block_count": 32, "llama.embedding_length": 4096,
|
||||
"llama.attention.head_count": 32, "llama.attention.head_count_kv": 8,
|
||||
"tokenizer.ggml.tokens": ["a"] * 5000}) # a big array is skipped
|
||||
assert gguf_kv.kv_elems(gguf_kv.read_meta(p)) == [32 * 8 * 128, 32 * 8 * 128, 0, 0]
|
||||
|
||||
|
||||
def test_hybrid_counts_only_attention_layers(tmp_path):
|
||||
# qwen3next-style: every 4th layer is full attention, the rest linear
|
||||
p = write_gguf(tmp_path / "m.gguf", {
|
||||
"general.architecture": "qwen3next", "qwen3next.block_count": 8,
|
||||
"qwen3next.embedding_length": 2048, "qwen3next.attention.head_count": 16,
|
||||
"qwen3next.attention.head_count_kv": 2, "qwen3next.attention.key_length": 256,
|
||||
"qwen3next.attention.value_length": 256, "qwen3next.full_attention_interval": 4})
|
||||
assert gguf_kv.kv_elems(gguf_kv.read_meta(p)) == [2 * 2 * 256, 2 * 2 * 256, 0, 0]
|
||||
# per-layer KV heads with 0 for recurrent layers (lfm2, granite hybrid, ...)
|
||||
p = write_gguf(tmp_path / "h.gguf", {
|
||||
"general.architecture": "lfm2", "lfm2.block_count": 4, "lfm2.embedding_length": 1024,
|
||||
"lfm2.attention.head_count": 16, "lfm2.attention.head_count_kv": [0, 8, 0, 8]})
|
||||
assert gguf_kv.kv_elems(gguf_kv.read_meta(p)) == [2 * 8 * 64, 2 * 8 * 64, 0, 0]
|
||||
|
||||
|
||||
def test_sliding_window_split(tmp_path):
|
||||
# gemma3: 5 of 6 layers are sliding-window (pattern set in code)
|
||||
p = write_gguf(tmp_path / "m.gguf", {
|
||||
"general.architecture": "gemma3", "gemma3.block_count": 12, "gemma3.embedding_length": 2560,
|
||||
"gemma3.attention.head_count": 8, "gemma3.attention.head_count_kv": 4,
|
||||
"gemma3.attention.key_length": 256, "gemma3.attention.value_length": 256,
|
||||
"gemma3.attention.sliding_window": 1024})
|
||||
assert gguf_kv.kv_elems(gguf_kv.read_meta(p)) == [2 * 4 * 256, 2 * 4 * 256, 10 * 4 * 256, 10 * 4 * 256]
|
||||
|
||||
|
||||
def test_not_gguf(tmp_path):
|
||||
(tmp_path / "x").write_bytes(b"nope")
|
||||
try:
|
||||
gguf_kv.read_meta(str(tmp_path / "x"))
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError
|
||||
@@ -104,3 +104,32 @@ def test_run_note_betterbench_build(tmp_path):
|
||||
assert notes["run"] == "full · concurrency on · bench-standard 0.4.1 · betterbench 0.6.1"
|
||||
notes, _, _ = run(tmp_path, m={"betterbench_build": "v0.6.1-3-gabc1234-dirty"})
|
||||
assert notes["run"].endswith("betterbench 0.6.1 @ v0.6.1-3-gabc1234-dirty")
|
||||
|
||||
|
||||
def test_gpu_note_clocks(tmp_path):
|
||||
notes, _, _ = run(tmp_path, c={"gpu_vram_gb": "32", "gpu_max_core_mhz": "2800",
|
||||
"gpu_max_mem_mhz": "19000"})
|
||||
assert notes["gpu"] == "1x Intel Arc Pro B65 · 32 GB · max 2800 MHz core · 19000 MHz mem"
|
||||
|
||||
|
||||
def test_max_ctx_vllm_from_engine_log(tmp_path):
|
||||
notes, _, _ = run(tmp_path, cmd=VLLM_CMD, m={"kv": "int8_k_int4_v"},
|
||||
c={"engine": "vllm", "ctx": "262144", "kv_cache_tokens": "412416"})
|
||||
assert notes["max_ctx"] == "412k tokens (engine KV cache) · 262k set"
|
||||
|
||||
|
||||
def test_max_ctx_llama_estimate(tmp_path):
|
||||
# 4 GiB free; q8_0 K and V, 16384 elements each per token = 34816 bytes
|
||||
c = {"ctx": "32768", "kv_elems_per_token": "16384 16384 8192 8192",
|
||||
"gpu_vram_total_mib": "32768", "gpu_vram_used_mib": "28672"}
|
||||
notes, _, _ = run(tmp_path, c=c)
|
||||
est = 32768 + 4 * 2**30 // 34816
|
||||
assert notes["max_ctx"] == f"≈ {est / 1000:.0f}k tokens est. · 33k set · 4.3 GB free · 34 KiB/token"
|
||||
# --swa-full keeps the sliding-window layers at full size too
|
||||
notes, _, _ = run(tmp_path, cmd=LLAMA_CMD + " --swa-full", c=c)
|
||||
assert notes["max_ctx"].endswith("51 KiB/token")
|
||||
|
||||
|
||||
def test_max_ctx_left_out_without_data(tmp_path):
|
||||
notes, _, _ = run(tmp_path)
|
||||
assert "max_ctx" not in notes
|
||||
Reference in new issue
Block a user