"""Pure math for the LLM memory estimator (no Gradio import, easy to test).""" from __future__ import annotations GIB = 1024 ** 3 # Weight precisions: bytes per parameter. Quantised formats include scale overhead # (e.g. GGUF Q8_0 = 8.5 bits, Q4_K_M ~4.85 bits average, MXFP4 = 4.25 bits). PRECISIONS = { "FP32 (4 bytes)": 4.0, "FP16 / BF16 (2 bytes)": 2.0, "INT8 / Q8_0 (~1.06 bytes)": 1.0625, "Q6_K (~0.82 bytes)": 0.82, "Q4_K_M (~0.60 bytes)": 0.60, "MXFP4 (gpt-oss native, ~0.53 bytes)": 0.53, "Raw INT4, no overhead (0.5 bytes)": 0.5, } KV_PRECISIONS = {"FP16 / BF16 (2 bytes)": 2.0, "FP8 / Q8 (1 byte)": 1.0, "Q4 (~0.56 bytes)": 0.5625} # Architecture numbers from each repo's config.json on huggingface.co. # total_b / active_b = parameter counts in billions from the model cards (active < total for MoE). # sliding = (number of sliding-window layers, window tokens) where the model uses them. MODELS = { "Llama 3.1 8B": dict(repo="meta-llama/Llama-3.1-8B-Instruct", total_b=8.03, active_b=8.03, layers=32, kv_heads=8, head_dim=128, max_ctx=131072), "Llama 3.3 70B": dict(repo="meta-llama/Llama-3.3-70B-Instruct", total_b=70.6, active_b=70.6, layers=80, kv_heads=8, head_dim=128, max_ctx=131072), "Qwen2.5 7B": dict(repo="Qwen/Qwen2.5-7B-Instruct", total_b=7.62, active_b=7.62, layers=28, kv_heads=4, head_dim=128, max_ctx=32768), "Qwen2.5 14B": dict(repo="Qwen/Qwen2.5-14B-Instruct", total_b=14.7, active_b=14.7, layers=48, kv_heads=8, head_dim=128, max_ctx=32768), "Qwen2.5 32B": dict(repo="Qwen/Qwen2.5-32B-Instruct", total_b=32.5, active_b=32.5, layers=64, kv_heads=8, head_dim=128, max_ctx=32768), "Qwen2.5 72B": dict(repo="Qwen/Qwen2.5-72B-Instruct", total_b=72.7, active_b=72.7, layers=80, kv_heads=8, head_dim=128, max_ctx=32768), "Qwen3 8B": dict(repo="Qwen/Qwen3-8B", total_b=8.2, active_b=8.2, layers=36, kv_heads=8, head_dim=128, max_ctx=40960), "Qwen3 14B": dict(repo="Qwen/Qwen3-14B", total_b=14.8, active_b=14.8, layers=40, kv_heads=8, head_dim=128, max_ctx=40960), "Qwen3 32B": dict(repo="Qwen/Qwen3-32B", total_b=32.8, active_b=32.8, layers=64, kv_heads=8, head_dim=128, max_ctx=40960), "Mistral 7B v0.3": dict(repo="mistralai/Mistral-7B-Instruct-v0.3", total_b=7.25, active_b=7.25, layers=32, kv_heads=8, head_dim=128, max_ctx=32768), "Mixtral 8x7B (MoE)": dict(repo="mistralai/Mixtral-8x7B-Instruct-v0.1", total_b=46.7, active_b=12.9, layers=32, kv_heads=8, head_dim=128, max_ctx=32768), "DeepSeek-R1-Distill-Qwen-14B": dict(repo="deepseek-ai/DeepSeek-R1-Distill-Qwen-14B", total_b=14.8, active_b=14.8, layers=48, kv_heads=8, head_dim=128, max_ctx=131072), "DeepSeek-R1-Distill-Qwen-32B": dict(repo="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", total_b=32.8, active_b=32.8, layers=64, kv_heads=8, head_dim=128, max_ctx=131072), "DeepSeek-R1-Distill-Llama-70B": dict(repo="deepseek-ai/DeepSeek-R1-Distill-Llama-70B", total_b=70.6, active_b=70.6, layers=80, kv_heads=8, head_dim=128, max_ctx=131072), "gpt-oss-20b (MoE)": dict(repo="openai/gpt-oss-20b", total_b=21.0, active_b=3.6, layers=24, kv_heads=8, head_dim=64, max_ctx=131072, sliding=(12, 128)), "gpt-oss-120b (MoE)": dict(repo="openai/gpt-oss-120b", total_b=117.0, active_b=5.1, layers=36, kv_heads=8, head_dim=64, max_ctx=131072, sliding=(18, 128)), } # name: (memory GiB, roughly usable for the model GiB, bandwidth GB/s) HARDWARE = { "RTX 3090 (24 GB)": (24, 23.0, 936), "RTX 4090 (24 GB)": (24, 23.0, 1008), "RTX 5090 (32 GB)": (32, 31.0, 1792), "Mac 64 GB unified (M4 Max)": (64, 48.0, 546), "Mac 128 GB unified (M4 Max)": (128, 96.0, 546), "NVIDIA DGX Spark (64 GB unified)": (64, 56.0, 273), "NVIDIA DGX Spark (128 GB unified)": (128, 115.0, 273), "NVIDIA H100 (80 GB, SXM)": (80, 79.0, 3350), } HARDWARE_NOTES = ( "Usable memory is an assumption: discrete GPUs keep ~1 GB for the driver/display; macOS lets the GPU " "use about 75% of unified memory by default (it can be raised); DGX Spark shares its 64 GB or 128 GB with the OS." ) def kv_cache_bytes(layers, kv_heads, head_dim, context, batch, kv_bytes, sliding=None): """K and V tensors: 2 x layers x kv_heads x head_dim x tokens x batch x bytes. Sliding-window layers only cache the last `window` tokens.""" per_token_layer = 2 * kv_heads * head_dim * kv_bytes * batch if sliding: n_sliding, window = sliding full_layers = layers - n_sliding return per_token_layer * (full_layers * context + n_sliding * min(context, window)) return per_token_layer * layers * context def estimate(total_b, active_b, bytes_per_param, layers, kv_heads, head_dim, context, batch, kv_bytes, overhead_pct, sliding=None): weights = total_b * 1e9 * bytes_per_param kv = kv_cache_bytes(layers, kv_heads, head_dim, context, batch, kv_bytes, sliding) total = (weights + kv) * (1 + overhead_pct / 100.0) # bytes read per generated token ~ active weights + the KV cache being attended to read_per_token = active_b * 1e9 * bytes_per_param + kv return dict(weights_gib=weights / GIB, kv_gib=kv / GIB, total_gib=total / GIB, read_per_token_gib=read_per_token / GIB) def verdicts(result): rows = [] for name, (cap, usable, bw) in HARDWARE.items(): need = result["total_gib"] if need <= usable * 0.9: v = "✅ Fits" elif need <= usable: v = "⚠️ Tight" else: v = "❌ Doesn't fit" ceiling = bw / (result["read_per_token_gib"] * GIB / 1e9) rows.append([name, cap, usable, v, f"~{ceiling:.0f} tok/s" if need <= usable else "—"]) return rows