5.3.16 · D4MLOps & Deployment

Exercises — Cost optimization and inference latency

3,591 words16 min readBack to topic

Level 1 — Recognition

(Can you name the right tool and plug in?)

Recall Solution L1.1

WHAT tool: the definition — " of individual calls exceed ms" means each call has probability of being slow. WHY this and not the mean: the mean hides the tail; the question is about any slow call across many, which is a probability-of-at-least-one problem. A page is fast only if all calls are fast: So Lesson: a "rare" tail becomes the common case under fan-out.

Recall Solution L1.2

WHAT tool: — work over achievable rate. WHY not : real kernels never hit peak (memory stalls, launch overhead), so we divide by what the chip actually delivers.

Recall Solution L1.3

WHAT tool: — fraction of time the single server is busy. WHY multiply: is seconds-of-work per request, is requests per second, so is seconds-of-work arriving per second = busy fraction. Convert s: Server is busy — safely below the danger zone.


Level 2 — Application

(Chain two formulas together.)

Recall Solution L2.1

WHAT tool: (M/M/1 mean, derived in the intro from Little's Law × the M/M/1 backlog ). At : Note the mean is ms, but the tail would be several times larger — the mean is a sizing guide, not the SLA number. Double the load: — that's not allowed. means work arrives faster than the server can clear it (); the queue grows without bound and . The server is unstable. You must add a replica or shed load before reaches .

Recall Solution L2.2

WHAT tool: . WHY: cost per inference = (dollars/second) ÷ (inferences/second) = ; scale to a million.

Recall Solution L2.3

WHAT tool: invert to solve for . WHY invert: we know the target latency and want the allowed load — the unknown moved to the other side. You may run up to busy and still (in expectation) meet the mean budget. Caveat: if the real SLA is on , run lower than and measure the tail — the mean is not the percentile.


Level 3 — Analysis

(Compare configurations; reason about the trade-off.)

Recall Solution L3.1

Apply :

  • : ms.
  • : ms.
  • : ms.
  • : ms.

Growth factor: . Read the figure (s01) carefully. The horizontal axis is utilization from to on a linear scale; the vertical axis is mean latency in ms (also linear), fixed at ms. The blue curve is . Three features carry the lesson: (1) the green-shaded safe zone where the curve is nearly flat — adding load barely changes latency; (2) the sharp upward knee near ; (3) the near-vertical wall as where the four marked dots climb ms. Toward the far left () the curve flattens to ms — the no-queue floor: an almost-idle server just returns the raw service time, you can never go below . (See Little's Law and Queueing Theory.)

Figure — Cost optimization and inference latency
Recall Solution L3.2

WHAT tool: throughput = items ÷ time; per-item compute latency = batch time (each item waits the whole batch). Unit analysis for the throughput conversion (worked through): a batch of items finishes in milliseconds. Raw rate is . To convert ms→s we use the identity , i.e. multiply by : The "ms" cancels, leaving items/s — the factor is not arbitrary, it is this unit conversion. Now:

  • : ms, items/s. Latency ms.
  • : ms, items/s. Latency ms.
  • : ms, items/s. Latency ms.

Read the figure (s02). Horizontal axis: batch size (linear, to ). It has two vertical axes: the left one (green) is throughput in items/s, the right one (red) is per-item latency in ms. The green throughput curve rises steeply then flattens (it saturates toward items/s as the fixed ms is amortized away); the red latency line climbs straight and never bends ( is linear). The yellow dots mark . The takeaway lives in the gap between the two curves: going multiplies throughput (so cost per item drops , since ) but per-item latency grows ms (). Throughput gains saturate while latency keeps climbing — the essence of the batching trade-off. See Batching and Throughput.

Figure — Cost optimization and inference latency
Recall Solution L3.3

Baseline floor: ms. (a) Quantization: ms — a speedup (see Model Quantization and Pruning). (b) Distillation: ms — a speedup (see Model Distillation). Distillation wins on raw latency here ( vs ), because it attacks directly. But it needs retraining and risks accuracy loss; quantization is cheaper to deploy. In practice you often stack both: FLOPs at and hardware rate ms ().


Level 4 — Synthesis

(Combine cost, latency, and scaling into a plan.)

Recall Solution L4.1

WHAT tool: per-replica capacity at target utilization. Recall the service rate from the intro — the requests one replica clears per second when always busy. Here RPS. At the target we only let each replica take RPS. WHY the cap: running at keeps ms — inside budget and with headroom to absorb the random bunching of arrivals (short bursts). The idle is deliberate insurance, not waste.

Recall Solution L4.2

Total hourly spend = R \cdot H = 8 \times 2.40 = \19.20X = 300 RPS. $$C_{1M} = \frac{10^6 \times 19.20}{3600 \times 300} = \frac{1.92\times10^{7}}{1.08\times10^{6}} \approx \17.78.$$ Sanity note: we paid for RPS of capacity and used all — the idle headroom is baked into needing (not ) machines, i.e. it's the price of meeting latency.

Recall Solution L4.3

(a) replicas. (b) Hourly = 4 \times 2.40 = \9.60X = 300: $$C_{1M} = \frac{10^6 \times 9.60}{3600 \times 300} = \frac{9.6\times10^{6}}{1.08\times10^{6}} \approx \8.89.$$ Cost halved (\17.78 \to $8.89\approx 80 + 30 = 110p99p99110150p99 \le 150p99$** in load tests, not add means (see SLA SLO and Monitoring). The cost win is real; the latency claim is provisional until measured.


Level 5 — Mastery

(Full end-to-end reasoning under a hard constraint.)

Recall Solution L5.1

Step 1 — max safe per option (invert ):

  • A: .
  • B: .

WHY B tolerates higher : its service is so fast ( ms) that even a busy queue stays under budget.

Step 2 — per-replica served rate :

  • A: RPS.
  • B: RPS.

Step 3 — replicas (round up):

  • A: .
  • B: .

Step 4 — cost per 1M ( served, hourly ):

  • A: C_{1M} = \frac{10^6 \times (30\times 3)}{3600\times 500} = \frac{9\times10^{7}}{1.8\times10^{6}} = \50.00$.
  • B: C_{1M} = \frac{10^6 \times (9\times 3)}{3600\times 500} = \frac{2.7\times10^{7}}{1.8\times10^{6}} = \15.00.$

Verdict: Option B is cheaper (\15$50s\rho1.5%p99$ meets the SLA, since the mean under-states the tail.


Recall One-line recap of every formula used

Compute floor ::: Service rate ::: (requests one server clears per second when busy) Queue latency ::: , with Max safe from a mean budget ::: Replicas needed ::: Cost per 1M ::: Why mean ≠ SLA ::: percentile SLAs () run above the mean; size on the mean, then measure the tail.