fastgpu's picture
Fine-Tuning Cost Calculator: LoRA, QLoRA and full fine-tunes priced live
efd7283 verified
Raw History Blame Contribute Delete
12.5 kB
/*
* 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) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[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) => '<option value="' + esc(m[0]) + '">' + esc(size(m[1])) + "</option>").join("");
el.picks.innerHTML = '<span class="picks-label">Popular:</span>' + PICKS.map((p) => '<button type="button" class="chip" data-model="' + esc(p) + '">' + esc(p.split("/").pop()) + "</button>").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 = '<div class="notice">' + esc(d.error || "FastGPU could not price that run.") + "</div>";
} else {
el.status.textContent = "";
render(d);
}
} catch (e) {
if (e && e.name === "AbortError") return;
el.status.textContent = "";
el.result.innerHTML = '<div class="notice">Live prices could not be reached. Try again in a moment.</div>';
} finally {
if (ctl === inflight) {
el.result.classList.remove("loading");
inflight = null;
}
}
}
// ------------------------------------------------------------------ result
function setup(g) {
return (g.gpu_count > 1 ? g.gpu_count + "× " : "") + g.gpu;
}
function row(g, fastest, cheapest) {
const cls = g === cheapest ? ' class="cheapest"' : "";
return "<tr" + cls + ">" +
'<th scope="row"><a class="setup" href="' + esc(withUtm(g.page_url)) + '" target="_blank" rel="noopener">' + esc(setup(g)) + "</a>" +
'<span class="sub">' + esc(g.provider_label) + (g.partner ? '<span class="partner" title="FastGPU earns a referral fee if you rent here, and partners are placed first when their price is close. The price shown is always the real live rate.">partner</span>' : "") +
(g.pcie_multi_gpu ? " · PCIe cards: slower than shown" : "") + "</span></th>" +
'<td class="num"><span class="mono">' + esc(duration(g.hours)) + "</span>" + (g === fastest ? '<span class="sub">fastest</span>' : "") + "</td>" +
'<td class="num cost"><a class="price mono" href="' + esc(withUtm(g.page_url)) + '" target="_blank" rel="noopener">' + esc(usd(g.total_usd)) + "</a>" +
'<span class="sub">' + esc(perHr(g.usd_per_gpu_hr)) + " per GPU</span></td>" +
'<td class="num"><a class="rent" href="' + esc(abs(g.rent_url)) + '" target="_blank" rel="sponsored noopener">Rent →</a></td>' +
"</tr>";
}
function table(rows, fastest, cheapest, caption) {
return '<table><caption class="visually-hidden">' + esc(caption) + "</caption><thead><tr><th>Setup</th><th class=\"num\">Time</th><th class=\"num\">Job cost</th><th></th></tr></thead><tbody>" +
rows.map((g) => row(g, fastest, cheapest)).join("") + "</tbody></table>";
}
function render(d) {
const w = d.workload;
const gpus = d.gpus || [];
const sound = gpus.filter((g) => !g.pcie_multi_gpu);
const pcie = gpus.filter((g) => g.pcie_multi_gpu);
const pool = sound.length ? sound : gpus;
const cheapest = pool[0];
const fastest = pool.reduce((a, b) => (!a || b.hours < a.hours ? b : a), null);
const moe = w.active_params_b < w.params_b;
let h = '<div class="model"><span class="model-id">' + esc(w.model) + "</span>" +
'<p class="facts">' + 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</p></div>";
if (!cheapest) {
h += '<div class="notice">No rentable GPU setup can hold this run right now. A shorter sequence length, a smaller micro-batch or QLoRA needs less memory.</div>';
el.result.innerHTML = h;
return;
}
h += '<div class="need headline"><div><p class="need-num"><span class="mono">' + esc(usd(cheapest.total_usd)) + "</span></p>" +
'<p class="need-what">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</p>" +
(fastest && fastest !== cheapest ? '<p class="fineprint">Fastest: ' + esc(setup(fastest)) + ", " + esc(duration(fastest.hours)) + " for " + esc(usd(fastest.total_usd)) + ".</p>" : "") +
'</div><a class="button" href="' + esc(abs(cheapest.rent_url)) + '" target="_blank" rel="sponsored noopener">Rent on ' + esc(cheapest.provider_label) + "</a></div>";
h += '<div class="table-head"><h2>Every GPU that can run it, cheapest job first</h2></div>';
h += table(sound.slice(0, SHOWN), fastest, cheapest, "Fine-tuning time and cost by GPU");
if (sound.length > SHOWN) {
h += '<details class="pcie"><summary>' + (sound.length - SHOWN) + " more GPUs</summary>" + table(sound.slice(SHOWN), fastest, cheapest, "More GPUs") + "</details>";
}
if (pcie.length) {
h += '<details class="pcie"><summary>' + pcie.length + " setups split across several PCIe cards (cheaper per hour, slower than shown)</summary>" + table(pcie, fastest, cheapest, "PCIe multi-card setups") + "</details>";
}
h += '<p class="fineprint">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. " +
'<a href="' + esc(withUtm(d.method_url)) + '" target="_blank" rel="noopener">How the estimate works</a>.' +
(d.updated_at ? " Prices updated " + esc(new Date(d.updated_at).toUTCString().replace(/:\d\d GMT$/, " UTC")) + "." : "") + "</p>";
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();
})();