Batching and serving algorithms · Easy

TTFT and Decode Latency

Problem 42 of 50 · Python and NumPy

Problem

Compute latency metrics for one token stream using timestamps in seconds. Return {"ttft": first token time minus arrival_time, "itl": consecutive token-time differences, "tpot": mean inter-token latency}.

With zero tokens, ttft and tpot are null (Python None) and itl is []. With one token, ttft is defined but tpot remains null. Coincident token timestamps produce zero intervals. TTFT is excluded from TPOT.

def stream_latencies(arrival_time, token_times):

Constraints

  • 0..64 finite token times in nondecreasing order, each ≥ arrival_time.
  • arrival_time is finite and nonnegative; all timestamps are at most 100000.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  10,
  [
    11,
    11.5,
    12.5
  ]
]
Expected result: {
  "ttft": 1,
  "itl": [
    0.5,
    1
  ],
  "tpot": 0.75
}

The first token arrives 1 second after the request. Intervals are 0.5 and 1 second, so TPOT is 0.75 seconds.

Example 2

Arguments: [
  0,
  [
    0.25
  ]
]
Expected result: {
  "ttft": 0.25,
  "itl": [],
  "tpot": null
}

A single token provides TTFT=0.25 but no inter-token interval, so TPOT is null.

Sign in to solve this problem

Browse all coding problems · Learn the foundations