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

887. Largest decode batch under a latency SLO

MediumReal-world constraints Pro

Why it matters Serving teams trade throughput (cost per token) against latency (user experience). An SLO turns that trade-off into a search for the largest safe batch.

Bigger decode batches raise throughput but make every request slower. Use this simple latency model:
- one decode step with b sequences takes step(b) = base_step_ms + per_seq_step_ms * b milliseconds
- a request's latency is prefill_ms + out_tokens * step(b)
- throughput is b * 1000 / step(b) tokens per second

Write best_decode_batch(prefill_ms, base_step_ms, per_seq_step_ms, out_tokens, slo_ms, max_batch). Find the largest batch b in 1 .. max_batch whose latency is <= slo_ms (a latency exactly at the SLO is allowed).

Inputs: all numbers are ints or floats >= 0, out_tokens and max_batch are ints >= 1.

Return a dict {"batch": b, "latency_ms": float, "tokens_per_s": float}, both floats rounded to 1 decimal. If even b = 1 misses the SLO, return {"batch": 0, "latency_ms": latency at b = 1, "tokens_per_s": 0.0}.

Functiondef best_decode_batch(prefill_ms, base_step_ms, per_seq_step_ms, out_tokens, slo_ms, max_batch)

Example 1

Input: best_decode_batch(200, 20, 0.5, 256, 9000, 64)

Output: {'batch': 28, 'latency_ms': 8904.0, 'tokens_per_s': 823.5}

Example 2

Input: prefill_ms = 200, base_step_ms = 20, per_seq_step_ms = 0.5, out_tokens = 256, slo_ms = 9000, max_batch = 16

Output: {'batch': 16, 'latency_ms': 7368.0, 'tokens_per_s': 571.4}

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

Write latency(b) as a small helper.

Hint 2

Scan b from 1 to max_batch and keep the largest that meets the SLO.

Hint 3

Compute throughput from the chosen b only.

Common mistakes
  • Using < instead of <= at the SLO.
  • Forgetting the prefill time.
  • Returning the batch with the best throughput regardless of the SLO.
Understand the idea

Decode is memory-bound, so a step costs a fixed base (reading the weights) plus a small amount per sequence (reading each KV cache). Throughput b / step(b) rises with b and flattens, while latency grows linearly. With prefill 200 ms, base 20 ms, 0.5 ms per sequence, 256 output tokens and a 9 s SLO, b = 29 gives 200 + 256 * 34.5 = 9032 ms, which is over, so the answer is b = 28 at 8904 ms.

Shapes and data flow
Scalars in, dict out.
Topics
Real-world constraints
Similar questions
  • Cheapest GPU fleet that meets throughput and memoryMedium
  • 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
887 / 903
Python 3solution.pyPython idle
1
2
3
4
def best_decode_batch(prefill_ms, base_step_ms, per_seq_step_ms, out_tokens, slo_ms, max_batch):
# 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