import gradio as gr from estimator import (HARDWARE_NOTES, KV_PRECISIONS, MODELS, PRECISIONS, estimate, verdicts) UTM = "utm_source=huggingface&utm_medium=referral" HW_THEORY = f"https://theknowledgeproject.gumroad.com/l/cjyyly?{UTM}&utm_campaign=vramtool" HW_TOOLKIT = f"https://theknowledgeproject.gumroad.com/l/xxwjsu?{UTM}&utm_campaign=vramtool" SIGNUP = f"https://buttondown.com/yunusguides?{UTM}&utm_campaign=selfaudit" WEB_VERSION = f"https://yunus-free-tools.surge.sh/llm-memory-estimator.html?{UTM}&utm_campaign=vramtool" CUSTOM = "Custom (enter numbers below)" def fill_preset(name): if name == CUSTOM: return [gr.update()] * 5 m = MODELS[name] return m["total_b"], m["active_b"], m["layers"], m["kv_heads"], m["head_dim"] def run(name, total_b, active_b, layers, kv_heads, head_dim, precision, kv_precision, context, batch, overhead): if not total_b or total_b <= 0 or not layers or not kv_heads or not head_dim or not context: return "Enter positive numbers for parameters, layers, KV heads, head dim and context.", [] active_b = active_b if active_b and 0 < active_b <= total_b else total_b m = MODELS.get(name, {}) # sliding-window layers only apply when the architecture fields match the preset sliding = m.get("sliding") if m and (layers, kv_heads, head_dim) == (m["layers"], m["kv_heads"], m["head_dim"]) else None r = estimate(total_b, active_b, PRECISIONS[precision], int(layers), int(kv_heads), int(head_dim), int(context), int(batch), KV_PRECISIONS[kv_precision], overhead, sliding) lines = [ f"### Plan for about **{r['total_gib']:.1f} GB** of GPU / unified memory", f"- Weights: **{r['weights_gib']:.1f} GB** ({total_b:g}B params × {PRECISIONS[precision]:g} bytes)", f"- KV cache: **{r['kv_gib']:.2f} GB** ({int(context):,} tokens × batch {int(batch)})", f"- Runtime overhead: {overhead:g}% (activations, CUDA/Metal buffers, fragmentation)", ] if m and context > m["max_ctx"]: lines.append(f"- ⚠️ {name} supports up to {m['max_ctx']:,} tokens; this context is beyond its limit.") if sliding: lines.append(f"- {sliding[0]} of {int(layers)} layers use a {sliding[1]}-token sliding window, so their KV cache stays small.") if active_b < total_b: lines.append(f"- MoE: all {total_b:g}B params must be in memory, but only ~{active_b:g}B are read per token, so it generates faster than a dense model of the same size.") lines.append("\n*1 GB = 1024³ bytes. Speed figures below are a memory-bandwidth **ceiling** for single-stream decoding; real tokens/s are lower.*") return "\n".join(lines), verdicts(r) with gr.Blocks(title="LLM Memory (VRAM) Estimator") as demo: gr.Markdown( "# 🧮 LLM Memory (VRAM) Estimator\n" "Will this open model fit on my GPU, Mac or DGX Spark, and how fast can it possibly run? " "Pick a preset (architecture numbers come from each model's `config.json`) or enter your own." ) with gr.Row(): with gr.Column(): preset = gr.Dropdown([CUSTOM] + list(MODELS), value="Llama 3.1 8B", label="Model preset") precision = gr.Dropdown(list(PRECISIONS), value="Q4_K_M (~0.60 bytes)", label="Weight precision") kv_precision = gr.Dropdown(list(KV_PRECISIONS), value="FP16 / BF16 (2 bytes)", label="KV cache precision") context = gr.Slider(512, 131072, value=8192, step=512, label="Context length (tokens)") batch = gr.Slider(1, 32, value=1, step=1, label="Batch / parallel sequences") overhead = gr.Slider(0, 30, value=15, step=1, label="Overhead (%)") with gr.Accordion("Architecture (auto-filled by preset)", open=False): total_b = gr.Number(value=8.03, label="Total parameters (billions)") active_b = gr.Number(value=8.03, label="Active parameters per token (billions, MoE)") layers = gr.Number(value=32, label="Layers (num_hidden_layers)", precision=0) kv_heads = gr.Number(value=8, label="KV heads (num_key_value_heads)", precision=0) head_dim = gr.Number(value=128, label="Head dim (head_dim or hidden_size / heads)", precision=0) with gr.Column(): summary = gr.Markdown() table = gr.Dataframe(headers=["Hardware", "Memory (GB)", "Usable (GB, approx.)", "Verdict", "Speed ceiling"], interactive=False, wrap=True) gr.Markdown(f"{HARDWARE_NOTES} ✅ = under 90% of usable, ⚠️ = 90–100%.") inputs = [preset, total_b, active_b, layers, kv_heads, head_dim, precision, kv_precision, context, batch, overhead] preset.change(fill_preset, preset, [total_b, active_b, layers, kv_heads, head_dim]).then(run, inputs, [summary, table]) for c in inputs[1:]: c.change(run, inputs, [summary, table]) demo.load(run, inputs, [summary, table]) gr.Markdown( "**Formula:** weights = params × bytes/param · KV cache = 2 × layers × kv_heads × head_dim × context × batch × bytes · " "total = (weights + KV) × (1 + overhead). Estimates for comparison, not guarantees.\n\n" "---\n" f"Buying hardware for local LLMs? [HARDWARE THEORY]({HW_THEORY}) ($4.99) and the " f"[Hardware Buying toolkit]({HW_TOOLKIT}) ($24) by the author of this tool. " f"Free: the [AI-proof career self-audit]({SIGNUP}) by email. " f"Also available as a [single-page web version]({WEB_VERSION})." ) if __name__ == "__main__": demo.launch()