BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Real-world constraints1 of 10 in topic

886. Cheapest GPU fleet that meets throughput and memory

MediumReal-world constraints Pro

Why it matters Capacity planning questions in ML systems interviews always combine at least two limits. Real fleets are sized by whichever of memory or throughput binds first.

You must serve one model and pick the cheapest GPU type for it. Each replica runs on one GPU (no tensor parallelism).

Memory per replica: usable memory is 90 percent of the GPU's memory, mem_gb * 1e9 * 0.9 bytes. Weights take params_b * 1e9 * bytes_per_param bytes. Each concurrent sequence reserves a full-context KV cache of 2 * layers * kv_heads * head_dim * kv_bytes * context bytes. So max_seqs = floor((usable - weights) / kv_per_seq). A GPU with max_seqs < 1 cannot host the model.

Replicas needed for a GPU type = max(ceil(target_tps / tokens_per_s), ceil(concurrency / max_seqs)). Hourly cost = replicas * hourly_usd.

Write plan_serving_fleet(model, gpus, target_tps, concurrency).

Inputs:
- model: dict with keys params_b, bytes_per_param, layers, kv_heads, head_dim, kv_bytes, context (numbers).
- gpus: list of dicts with keys name (str), mem_gb, hourly_usd, tokens_per_s (tokens per second one replica of this model delivers).
- target_tps: total tokens per second needed. concurrency: number of sequences that must be live at once (int >= 1).

Return a dict {"gpu": name, "replicas": int, "max_seqs_per_replica": int, "hourly_usd": float rounded to 2 decimals} for the cheapest option. Ties on cost go to fewer replicas, then to the alphabetically first name. Return None if no GPU can host the model.

Functiondef plan_serving_fleet(model, gpus, target_tps, concurrency)

Example 1

Input: plan_serving_fleet({'params_b': 8, 'bytes_per_param': 2, 'layers': 32, 'kv_heads': 8, 'head_dim': 128, 'kv_bytes': 2, 'context': 8192}, [{'name': 'A10G', 'mem_gb': 24, 'hourly_usd': 1.0, 'tokens_per_s': 900}, {'name': 'L40S', 'mem_gb': 48, 'hourly_usd': 1.9, 'tokens_per_s': 2000}, {'name': 'H100', 'mem_gb': 80, 'hourly_usd': 3.5, 'tokens_per_s': 4500}], 9000, 40)

Output: {'gpu': 'H100', 'replicas': 2, 'max_seqs_per_replica': 52, 'hourly_usd': 7.0}

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Compute KV bytes per sequence once from the model dict.

Hint 2

For each GPU: max_seqs, then replicas from both constraints, then cost.

Hint 3

Compare with a key (cost, replicas, name) so ties break as the spec says.

Common mistakes
  • Forgetting the factor 2 for keys and values.
  • Using 2 ** 30 instead of 1e9.
  • Taking only the throughput constraint.
Understand the idea

Two constraints decide replica count: throughput (tokens per second) and memory (how many sequences each replica can hold at full context). For an 8B fp16 model with 32 layers, 8 KV heads and head dim 128, one 8192-token sequence needs 2 * 32 * 8 * 128 * 2 * 8192 = about 1.07 GB of KV cache. A 24 GB card has 21.6 GB usable, minus 16 GB of weights, so it holds 5 sequences. The cheapest card per hour is not always the cheapest fleet.

Shapes and data flow
Dicts and lists in, one dict out.
Topics
Real-world constraints
Similar questions
  • Largest decode batch under a latency SLOMedium
  • Schedule requests under RPM and TPM limitsMedium
  • Pick the cheapest cascade threshold that meets accuracyMedium
  • Spot or on-demand GPUs for a training jobMedium
All Real-world constraints problems
Report
886 / 903
Python 3solution.pyPython idle
1
2
3
4
5
import math
def plan_serving_fleet(model, gpus, target_tps, concurrency):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS