/* * Fine-Tuning Cost Calculator (huggingface.co/spaces/fastgpu/fine-tuning-cost-calculator). * * A static page: the form goes to FastGPU's keyless API, GET https://fastgpu.co/api/v1/finetune-cost, the same * executor as fastgpu.co/fine-tuning-cost-calculator (apps/app/src/lib/gpu/finetune-quote.ts), so this Space and the * site never disagree. Every row rents through the counted redirect the API returns (rent_url, src=hf-finetune). * The models the card lists (models.js, from scripts/models.py) carry their Hub parameter count, sent as params_b * so the API need not look the repo up. */ (function () { "use strict"; const API = "https://fastgpu.co/api/v1/finetune-cost"; const UTM = "utm_source=huggingface&utm_medium=space&utm_campaign=finetune-cost-calculator"; const SHOWN = 12; const MODELS = Array.isArray(window.FT_MODELS) ? window.FT_MODELS : []; const SIZE = new Map(MODELS.map((m) => [m[0].toLowerCase(), m[1]])); const DEFAULT_MODEL = "meta-llama/Llama-3.1-8B-Instruct"; const PICKS = ["meta-llama/Llama-3.1-8B-Instruct", "Qwen/Qwen3-8B", "Qwen/Qwen2.5-7B-Instruct", "mistralai/Mistral-7B-Instruct-v0.3", "google/gemma-3-12b-it", "meta-llama/Llama-3.3-70B-Instruct"]; const $ = (id) => document.getElementById(id); const el = { form: $("calc"), model: $("model"), list: $("models"), picks: $("picks"), method: $("method"), examples: $("examples"), perExample: $("perExample"), epochs: $("epochs"), seq: $("seq"), batch: $("batch"), mfu: $("mfu"), status: $("status"), result: $("result"), }; const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]); /** The API answers absolute links; a relative one (a preview build) is FastGPU's. */ const abs = (url) => (String(url || "").startsWith("/") ? "https://fastgpu.co" + url : String(url || "")); const withUtm = (url) => abs(url) + (url.includes("?") ? "&" : "?") + UTM; const num = (s) => { const n = Number(String(s || "").replace(/[_,\s]/g, "")); return Number.isFinite(n) && n > 0 ? n : null; }; /** A price to the cent, as FastGPU shows it. */ const usd = (n) => "$" + (Math.round(n * 100) / 100).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 }); const perHr = (n) => (n > 0 && n < 0.005 ? "<$0.01" : usd(n)) + "/hr"; function duration(h) { if (h < 1) return Math.max(1, Math.round(h * 60)) + " min"; if (h < 48) return (h < 10 ? Math.round(h * 10) / 10 : Math.round(h)) + " h"; const d = h / 24; return (d < 10 ? Math.round(d * 10) / 10 : Math.round(d)) + " days"; } function tokens(t) { const f = (v) => (v >= 100 ? Math.round(v) : v >= 10 ? Math.round(v * 10) / 10 : Math.round(v * 100) / 100); if (t >= 1e12) return f(t / 1e12) + "T"; if (t >= 1e9) return f(t / 1e9) + "B"; if (t >= 1e6) return f(t / 1e6) + "M"; if (t >= 1e3) return f(t / 1e3) + "K"; return String(Math.round(t)); } const size = (b) => (b >= 10 ? Math.round(b) : Math.round(b * 10) / 10) + "B"; // "https://huggingface.co/org/name/tree/main" (a model page's address) names the repo org/name. const HUB_PAGES = /^(models|spaces|datasets|docs|blog|posts|collections|settings|organizations|new|join|login|pricing|tasks|papers|learn|chat|api)$/i; function modelOf(text) { const t = text.trim(); const m = t.match(/^(?:https?:\/\/)?(?:www\.)?(?:huggingface\.co|hf\.co)\/([\w.-]+)\/([\w.-]+)/i); return m && !HUB_PAGES.test(m[1]) ? m[1] + "/" + m[2] : t; } function sizeQuery(text) { const m = text.trim().match(/^~?(\d+(?:\.\d+)?)\s*([bt])?(?:\s*(?:params?|parameters?))?$/i); if (!m) return null; const n = parseFloat(m[1]) * (m[2] && m[2].toLowerCase() === "t" ? 1000 : 1); return n >= 0.05 && n <= 5000 ? n : null; } // ------------------------------------------------------------------ suggestions el.list.innerHTML = MODELS.map((m) => '").join(""); el.picks.innerHTML = 'Popular:' + PICKS.map((p) => '").join(""); el.picks.addEventListener("click", (e) => { const b = e.target.closest("button[data-model]"); if (!b) return; el.model.value = b.getAttribute("data-model"); run(); }); // ------------------------------------------------------------------ query function params() { const p = new URLSearchParams(); const raw = modelOf(el.model.value || DEFAULT_MODEL); const asSize = sizeQuery(raw); if (asSize) p.set("params_b", String(asSize)); else { p.set("model", raw); const known = SIZE.get(raw.toLowerCase()); if (known) p.set("params_b", String(known)); } p.set("method", el.method.value); const ex = num(el.examples.value), per = num(el.perExample.value), ep = num(el.epochs.value); if (ex) p.set("examples", String(ex)); if (per) p.set("tokens_per_example", String(per)); if (ep) p.set("epochs", String(ep)); p.set("seq", el.seq.value); p.set("batch", el.batch.value); p.set("mfu", el.mfu.value); return p; } let inflight = null; async function run() { const p = params(); saveHash(p); if (inflight) inflight.abort(); const ctl = new AbortController(); inflight = ctl; el.result.classList.add("loading"); el.status.textContent = "Pricing it on every GPU…"; p.set("src", "hf-finetune"); try { const r = await fetch(API + "?" + p.toString(), { signal: ctl.signal }); const d = await r.json(); if (ctl !== inflight) return; if (!r.ok || d.error) { el.status.textContent = ""; el.result.innerHTML = '
| Setup | Time | Job cost |
|---|
' + esc(w.method_label) + " · " + esc(size(w.params_b)) + " parameters" + (moe ? ", " + esc(size(w.active_params_b)) + " active per token" : "") + " · " + esc(tokens(w.tokens)) + " training tokens · about " + esc(Number(w.vram_required_gb).toLocaleString("en-US")) + " GB of GPU memory
' + esc(usd(cheapest.total_usd)) + "
" + 'for the cheapest run: ' + esc(setup(cheapest)) + " on " + esc(cheapest.provider_label) + ", " + esc(duration(cheapest.hours)) + (w.method === "qlora" ? " or more" : "") + " at " + Math.round(w.mfu * 100) + "% utilization
" + (fastest && fastest !== cheapest ? 'Fastest: ' + esc(setup(fastest)) + ", " + esc(duration(fastest.hours)) + " for " + esc(usd(fastest.total_usd)) + ".
" : "") + 'Time = training operations / (GPUs × the card\'s dense 16-bit peak × ' + Math.round(w.mfu * 100) + "% utilization, an assumption). " + (w.method === "qlora" ? "QLoRA runs slower than this LoRA arithmetic, so its times are the least it takes. " : "") + "Job cost is GPU rental only, billed under each provider's own meter, at its cheapest rentable offer right now. " + 'How the estimate works.' + (d.updated_at ? " Prices updated " + esc(new Date(d.updated_at).toUTCString().replace(/:\d\d GMT$/, " UTC")) + "." : "") + "
"; el.result.innerHTML = h; } // ------------------------------------------------------------------ state in the address function saveHash(p) { const h = new URLSearchParams(p); h.delete("params_b"); try { history.replaceState(null, "", "#" + h.toString()); } catch (e) { /* sandboxed frame */ } } function loadHash() { const raw = (location.hash || "").replace(/^#/, "") || (location.search || "").replace(/^\?/, ""); const h = new URLSearchParams(raw); if (h.get("model")) el.model.value = h.get("model"); const sel = (node, v) => { if (v && Array.from(node.options).some((o) => o.value === v)) node.value = v; }; sel(el.method, h.get("method")); sel(el.seq, h.get("seq")); sel(el.batch, h.get("batch")); sel(el.mfu, h.get("mfu")); if (num(h.get("examples"))) el.examples.value = Number(h.get("examples")).toLocaleString("en-US"); if (num(h.get("tokens_per_example"))) el.perExample.value = Number(h.get("tokens_per_example")).toLocaleString("en-US"); if (num(h.get("epochs"))) el.epochs.value = h.get("epochs"); } el.form.addEventListener("submit", (e) => { e.preventDefault(); run(); }); [el.method, el.seq, el.batch, el.mfu].forEach((c) => c.addEventListener("change", run)); loadHash(); if (!el.model.value) el.model.value = DEFAULT_MODEL; run(); })();