alvarobartt's picture
download
raw
14.8 kB
import argparse
import csv
import hashlib
import json
import re
import shutil
import subprocess
from collections import Counter
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
GIB = 2**30
MIB = 2**20
def match(pattern: str, text: str, cast: Any = float) -> Any:
values = re.findall(pattern, text)
return cast(values[-1].replace(",", "")) if values else None
def scaled(pattern: str, text: str, unit: int = GIB) -> int | None:
value = match(pattern, text)
return round(value * unit) if value is not None else None
def dump(path: Path, value: Any) -> None:
path.write_text(json.dumps(value, indent=2, ensure_ascii=False, allow_nan=False) + "\n")
def write_csv(path: Path, rows: list[dict[str, Any]]) -> None:
fields = list(dict.fromkeys(key for row in rows for key in row))
with path.open("w") as file:
writer = csv.DictWriter(file, fieldnames=fields)
writer.writeheader()
writer.writerows(
{k: json.dumps(v) if isinstance(v, (list, dict)) else v for k, v in row.items()} for row in rows
)
def clean(text: str) -> str:
text = re.sub(r"\x1b\[[0-9;]*[A-Za-z]", "", text)
text = re.sub(r"\bhf_[A-Za-z0-9]{12,}\b", "[REDACTED]", text)
text = re.sub(r"(?:/home/[^/\s]+|/root|/tmp)(?:/[^\s,;\]\)\"']*)?", "[LOCAL_PATH]", text)
text = re.sub(r"\b(?:ghp_|github_pat_|sk-)[A-Za-z0-9_-]+", "[REDACTED]", text)
return text
def evidence(source: Path, destination: Path) -> dict[str, Any]:
pattern = re.compile(
r"HFMEM (?:CONFIG_START|SMI_|PROBE_OK|CONFIG_DIED|CONFIG_END|DONE)|"
r"Model loading took|Actual usage is|Available KV cache memory|CUDA graph pool memory|"
r"GPU KV cache size|Maximum concurrency for|Load weight end|KV Cache is allocated|"
r"Capture .*graph end|Memory pool end|available_gpu_mem=|"
r"(?:model|KV|RS|compute) buffer size\s*=|common_memory_breakdown_print:|"
r"common_params_print_info: build|llama_context: n_(?:ctx|seq_max)|"
r"^HFMEM_READY$|^\d+ MiB, \d+ MiB$"
)
destination.parent.mkdir(parents=True, exist_ok=True)
lines = source.read_text(errors="replace").splitlines()
selected = [f"{number}: {clean(line)}" for number, line in enumerate(lines, 1) if pattern.search(line)]
destination.write_text("\n".join(selected) + "\n")
return {
"source_file": source.name,
"source_sha256": hashlib.sha256(source.read_bytes()).hexdigest(),
"source_lines": len(lines),
}
def parse_mm(source: Path, out: Path) -> list[dict[str, Any]]:
mapping = {}
for line in (source / "jobs.txt").read_text().splitlines():
parts = line.split()
if len(parts) == 5:
engine, flavor, model, tag, _ = parts
mapping[f"{engine}-{tag}"] = (engine, flavor, model)
rows = []
for file in sorted((source / "logs").glob("*.log")):
engine, flavor, model = mapping[file.stem]
text = file.read_text(errors="replace")
relative = f"evidence/mm/{file.name}"
provenance = evidence(file, out / relative)
starts = list(re.finditer(r"HFMEM CONFIG_START (\S+) (\[[^\n]*\])", text))
for index, start in enumerate(starts):
section = text[start.start() : starts[index + 1].start() if index + 1 < len(starts) else len(text)]
config, args_json = start.groups()
args = json.loads(args_json)
def option(flag: str, default: str | None = None) -> str | None:
return args[args.index(flag) + 1] if flag in args else default
ctx = option("--max-model-len", option("--context-length"))
smi = {}
for phase, payload in re.findall(r"HFMEM SMI_(READY|TEXT|PROBE) \S+ (\[[^\n]+\])", section):
smi[phase.lower()] = json.loads(payload)
assert all(len(values) == 1 for values in smi.values())
date = re.search(r"\b(2026-\d\d-\d\d)\b", section)
if not date:
month_day = re.search(r"\b(11-\d\d) \d\d:\d\d:\d\d", section)
date_string = f"2026-{month_day.group(1)}" if month_day else None
else:
date_string = date.group(1)
row: dict[str, Any] = {
"run_id": f"mm/{file.stem}/{config}",
"engine": engine,
"model": model,
"hardware": "A100 80GB" if flavor == "a100-large" else "H200 141GB",
"hardware_flavor": flavor,
"device_index": 0,
"tensor_parallel_size": 1,
"config": config,
"arguments": args,
"max_model_len": int(ctx) if ctx else None,
"healthy": "ready" in smi,
"measurement_date": date_string,
"model_revision": None,
"model_revision_status": "not pinned in archived launch",
"engine_version": match(r"Initializing a V1 LLM engine \(v([^\)]+)\)", section, str)
if engine == "vllm"
else None,
"engine_image": "vllm/vllm-openai:latest" if engine == "vllm" else "lmsysorg/sglang:latest",
"engine_image_digest": None,
"evidence": relative,
**provenance,
}
for phase in ["ready", "text", "probe"]:
row[f"device_{phase}_bytes"] = smi[phase][0][0] * MIB if phase in smi else None
row["device_capacity_bytes"] = smi["ready"][0][1] * MIB if "ready" in smi else None
row["text_request_succeeded"] = bool(re.search(r"HFMEM PROBE_OK text\b", section))
probe = re.findall(r"HFMEM PROBE_(OK|FAIL) (image|audio)\b", section)
row["multimodal_probe"] = f"{probe[-1][1]}:{probe[-1][0].lower()}" if probe else None
if engine == "vllm":
row.update(
weights_reported_bytes=scaled(r"Model loading took ([\d.]+) GiB", section),
weights_plus_non_torch_reported_bytes=scaled(
r"Actual usage is ([\d.]+) GiB for consumed", section
),
activation_peak_reported_bytes=scaled(r"([\d.]+) GiB for peak activation", section),
cuda_graph_reported_bytes=scaled(
r"CUDA graph pool memory: ([\d.]+) GiB \(actual\)", section
),
cuda_graph_engine_estimate_bytes=scaled(
r"CUDA graph pool memory: [\d.]+ GiB \(actual\), ([\d.]+) GiB \(estimated\)", section
),
kv_pool_reported_bytes=scaled(r"Current kv cache memory in use is ([\d.]+) GiB", section),
kv_pool_tokens=match(r"GPU KV cache size: ([\d,]+) tokens", section, int),
max_num_batched_tokens=match(r"max_num_batched_tokens=(\d+)", section, int),
gpu_memory_utilization=match(r"Desired GPU memory utilization is \(([\d.]+),", section),
weights_dtype=match(r"dtype=torch\.([a-z0-9]+)", section, str),
kv_cache_dtype=match(r"kv_cache_dtype=([a-z0-9_]+)", section, str),
max_num_seqs=match(r"max_num_seqs=(\d+)", section, int),
)
if row["kv_pool_reported_bytes"] is None:
row["kv_pool_reported_bytes"] = scaled(r"Available KV cache memory: ([\d.]+) GiB", section)
consumed, weights = row["weights_plus_non_torch_reported_bytes"], row["weights_reported_bytes"]
row["non_torch_derived_bytes"] = (
consumed - weights if consumed is not None and weights is not None else None
)
else:
kv = re.findall(r"KV Cache is allocated[^\n]*", section)
kv_line = kv[-1] if kv else ""
k, v = scaled(r"K size: ([\d.]+) GB", kv_line), scaled(r"V size: ([\d.]+) GB", kv_line)
pool = scaled(r"KV size: ([\d.]+) GB", kv_line)
if pool is None and k is not None and v is not None:
pool = k + v
row.update(
weights_reported_bytes=scaled(r"Load weight end.*?mem usage=([\d.]+) GB", section),
kv_pool_reported_bytes=pool,
kv_pool_tokens=match(r"#tokens: (\d+)", kv_line, int),
cuda_graph_prefill_reported_bytes=scaled(
r"Capture target prefill CUDA graph end.*?mem usage=([\d.]+) GB", section
),
cuda_graph_decode_reported_bytes=scaled(
r"Capture (?:target decode )?(?:CUDA|cuda) graph end.*?mem usage=([\d.]+) GB", section
),
chunked_prefill_tokens=match(r"chunked_prefill_size=(\d+)", section, int),
mem_fraction_static=match(r"'mem_fraction_static': ([\d.]+)", section),
weights_dtype=match(r"'dtype': '([^']+)'", section, str),
kv_cache_dtype=match(r"dtype: torch\.([a-z0-9]+), #tokens:", kv_line, str),
)
ready, weights, pool = (
row.get(k) for k in ["device_ready_bytes", "weights_reported_bytes", "kv_pool_reported_bytes"]
)
row["other_device_memory_residual_bytes"] = (
ready - weights - pool
if ready is not None and weights is not None and pool is not None
else None
)
rows.append(row)
return rows
def parse_llama(source: Path, out: Path) -> list[dict[str, Any]]:
rows = []
for file in sorted((source / "logs").glob("llamacpp-v-*.log")):
text = file.read_text(errors="replace")
if "HFMEM_READY" not in text:
continue
repo, revision, name = re.findall(
r"llama_model_loader: loaded meta data[^\n]*models--([^/]+)/snapshots/([a-f0-9]{40})/([^\s\"']+\.gguf)",
text,
)[-1]
real_model = text[text.rfind("I llama_model_loader: loaded meta data") :]
kv = sum(float(v) * MIB for v in re.findall(r"CUDA0 KV buffer size =\s*([\d.]+) MiB", real_model))
recurrent = sum(
float(v) * MIB for v in re.findall(r"CUDA0 RS buffer size =\s*([\d.]+) MiB", real_model)
)
relative = f"evidence/llamacpp/{file.name}"
row: dict[str, Any] = {
"run_id": f"startup/{file.stem}",
"engine": "llama.cpp",
"model": repo.replace("--", "/"),
"file": name,
"model_revision": revision,
"model_revision_status": "snapshot SHA in native log",
"hardware": "A100 80GB",
"hardware_flavor": "a100-large",
"device_index": 0,
"tensor_parallel_size": 1,
"config": "startup",
"healthy": True,
"text_request_succeeded": None,
"multimodal_probe": None,
"measurement_date": "2026-11-03",
"measurement_date_basis": "archived job ID timestamp",
"engine_version": match(r"common_params_print_info: build ([^\n]+)", text, str),
"engine_image": "ghcr.io/ggml-org/llama.cpp:server-cuda",
"engine_image_digest": None,
"max_model_len": match(r"llama_context: n_ctx\s*=\s*(\d+)", text, int),
"batch_size": match(r"llama_context: n_seq_max\s*=\s*(\d+)", text, int),
"weights_dtype": "Q4_K_M",
"kv_cache_dtype": "f16",
"weights_reported_bytes": scaled(r"CUDA0 model buffer size =\s*([\d.]+) MiB", text, MIB),
"weights_host_mapped_bytes": scaled(r"CPU_Mapped model buffer size =\s*([\d.]+) MiB", text, MIB),
"kv_pool_reported_bytes": round(kv),
"recurrent_state_reported_bytes": round(recurrent),
"compute_buffer_reported_bytes": scaled(
r"sched_reserve: +CUDA0 compute buffer size =\s*([\d.]+) MiB", real_model, MIB
),
"encoder_compute_buffer_reported_bytes": scaled(
r"reserve_compute_meta: +CUDA0 compute buffer size =\s*([\d.]+) MiB", real_model, MIB
),
"evidence": relative,
**evidence(file, out / relative),
}
smi = re.findall(r"^(\d+) MiB, (\d+) MiB$", text, re.M)
row["device_ready_bytes"], row["device_capacity_bytes"] = (int(x) * MIB for x in smi[-1])
assert row["weights_reported_bytes"] is not None
row["other_device_memory_residual_bytes"] = (
row["device_ready_bytes"]
- row["weights_reported_bytes"]
- row["kv_pool_reported_bytes"]
- row["recurrent_state_reported_bytes"]
)
rows.append(row)
return rows
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("source", type=Path)
parser.add_argument("output", type=Path)
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
rows = parse_mm(args.source / "mm", args.output) + parse_llama(args.source / "jobs", args.output)
(args.output / "measurements.jsonl").write_text(
"".join(json.dumps(row, ensure_ascii=False, allow_nan=False) + "\n" for row in rows)
)
write_csv(args.output / "measurements.csv", rows)
supplementary = []
for file in sorted((args.source / "jobs/logs").glob("*.log")):
if not file.name.startswith(("vllm-", "sglang-")):
continue
target = f"supplementary/startup_evidence/{file.name}"
supplementary.append({"evidence": target, **evidence(file, args.output / target)})
dump(args.output / "supplementary/index.json", supplementary)
manifest = {
"schema_version": 1,
"exported_at": datetime.now(timezone.utc).isoformat(),
"hf_mem_version": "1.0.0",
"hf_mem_commit": subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip(),
"measurement_rows": len(rows),
"healthy_rows": sum(r["healthy"] for r in rows),
"by_engine": dict(Counter(r["engine"] for r in rows)),
"healthy_by_engine": dict(Counter(r["engine"] for r in rows if r["healthy"])),
"distinct_models": len({r["model"] for r in rows}),
"supplementary_logs": len(supplementary),
"measurement_scope": "Single-GPU archived startup/ready snapshots, not continuous inference peaks",
}
dump(args.output / "manifest.json", manifest)
if Path(__file__).resolve() != (args.output / "export.py").resolve():
shutil.copy2(__file__, args.output / "export.py")
print(json.dumps(manifest, indent=2))
if __name__ == "__main__":
main()

Xet Storage Details

Size:
14.8 kB
·
Xet hash:
ef64a870c137a7d650a3282bc04ea82a66aa7c8df996e5fd03da9186e932f8ee

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.