Waiting for Company

Deep Learning in the Real WorldHard+30 XP+30

Waiting for Company

A model sits behind an endpoint on one GPU. Live requests arrive one at a time, and a batch of one is the least efficient case there is: every run of the model pays a fixed overhead however many items it carries, and a single item leaves most of the hardware idle.

So the server batches. It holds requests briefly and runs them together, which makes each user wait a little longer and lets the same GPU serve far more of them. This problem is about exactly how much longer each user waits.

The server follows these rules:

  • arrivals lists the time, in milliseconds, at which each request arrives. It is sorted from earliest to latest (ties allowed) and holds at least one request.
  • The server runs one batch at a time. A batch of nnn requests takes fixed_ms + per_item_ms * n milliseconds. Requests that arrive while a batch is running wait in the queue.
  • A batch launches at the first moment the server is free and at least one of these is true:
    • at least max_batch requests are waiting, or
    • the oldest waiting request has been waiting for max_wait milliseconds.
  • The batch takes the oldest waiting requests, up to max_batch of them. Anyone else stays in the queue for a later batch. A request arriving at the very instant a batch launches is in time to join it, if there is room.
  • The server starts out idle.
  • A request's latency is the moment its batch finishes minus the moment it arrived.

Task: write batched_latencies(arrivals, max_batch, max_wait, fixed_ms, per_item_ms), returning the latency of every request, in arrival order, each as a float rounded to 4 decimal places. max_batch is a whole number of at least 1, and the other settings are non-negative numbers.

Worked through, batched_latencies([0, 1, 3, 9], 2, 4, 5, 1):

  • Batch 1. At t=1t = 1t=1 two requests are waiting, which fills a batch, and the server is free, so it launches at once. It runs for 5+1×2=75 + 1 \times 2 = 75+1×2=7 ms and finishes at t=8t = 8t=8. Latencies: 8−0=88 - 0 = 88−0=8 and 8−1=78 - 1 = 78−1=7.
  • Batch 2. The request from t=3t = 3t=3 has waited its 444 ms by t=7t = 7t=7, but the server is busy until t=8t = 8t=8, so this batch launches at t=8t = 8t=8. The next request is not due until t=9t = 9t=9, so the batch holds one request, runs for 5+1=65 + 1 = 65+1=6 ms and finishes at t=14t = 14t=14. Latency: 111111.
  • Batch 3. The request from t=9t = 9t=9 has waited its 444 ms by t=13t = 13t=13; the server frees up at t=14t = 14t=14 and launches then, finishing at t=20t = 20t=20. Latency: 111111.
  • The answer is [8.0, 7.0, 11.0, 11.0].

With max_batch set to 1 there is no batching at all: every request runs alone and pays the whole fixed overhead for itself, so a burst of traffic piles up behind one request at a time. A batching server pays that overhead once per batch and clears the same burst far sooner. The price is some extra waiting when traffic is light, and that is the trade this function lets you measure.

latency
50 ms
throughput
100/s
wait 20 ms · batch 5

Hold requests briefly and run them together. Every user waits a little longer, and the system serves far more of them.

Examples

arrivals = [0, 2, 3, 20, 21]
max_batch = 4
max_wait = 5
fixed_ms = 6
per_item_ms = 1
returns [14.0, 12.0, 11.0, 13.0, 12.0]
arrivals = [0, 0, 0, 0, 0, 1, 1, 2]
max_batch = 3
max_wait = 10
fixed_ms = 4
per_item_ms = 2
returns [10.0, 10.0, 10.0, 20.0, 20.0, 19.0, 27.0, 26.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run