@@ -149,15 +149,15 @@ func (pl *PredictedLatency) ResponseBody(ctx context.Context, request *fwksched.
149149
150150 if predictedLatencyCtx .ttft == 0 {
151151 if pl .config .StreamingMode && ! response .EndOfStream {
152- processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now , pl . config . SamplingMean , pl . config . MaxDecodeTokenSamplesForPrediction )
152+ processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now )
153153 }
154154 } else {
155- processTokenForLatencyPrediction (ctx , pl . typedName . Name , pl . typedName . Type , pl . latencypredictor , pl . config . EndpointRoleLabel , predictedLatencyCtx , targetMetadata , now , pl . config . SamplingMean , pl . config . MaxDecodeTokenSamplesForPrediction )
155+ processTokenForLatencyPrediction (ctx , predictedLatencyCtx , now )
156156 }
157157
158158 if response .EndOfStream {
159159 if ! pl .config .StreamingMode {
160- processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now , pl . config . SamplingMean , pl . config . MaxDecodeTokenSamplesForPrediction )
160+ processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now )
161161 }
162162
163163 if predictedLatencyCtx .ttft > 0 {
@@ -247,12 +247,9 @@ func processFirstTokenForLatencyPrediction(
247247 endpointRoleLabel string ,
248248 predictedLatencyCtx * predictedLatencyCtx ,
249249 now time.Time ,
250- samplingMean float64 ,
251- maxDecodeTokenSamplesForPrediction int ,
252250) {
253251 logger := log .FromContext (ctx )
254252
255- initializeSampler (ctx , predictedLatencyCtx , samplingMean , maxDecodeTokenSamplesForPrediction )
256253 predictedLatencyCtx .ttft = float64 (now .Sub (predictedLatencyCtx .requestReceivedTimestamp ).Milliseconds ())
257254 predictedLatencyCtx .generatedTokenCount = 1
258255
@@ -286,15 +283,6 @@ func processFirstTokenForLatencyPrediction(
286283 refreshLastSeenMetrics (ctx , predictedLatencyCtx )
287284}
288285
289- func initializeSampler (ctx context.Context , predictedLatencyCtx * predictedLatencyCtx , samplingMean float64 , maxDecodeTokenSamplesForPrediction int ) {
290- if predictedLatencyCtx .decodeTokenSampler == nil {
291- logger := log .FromContext (ctx )
292- requestID := predictedLatencyCtx .schedulingRequest .Headers [reqcommon .RequestIDHeaderKey ]
293- predictedLatencyCtx .decodeTokenSampler = newDecodeTokenSampler (requestID , samplingMean , maxDecodeTokenSamplesForPrediction )
294- logger .V (logutil .DEBUG ).Info ("Initialized token sampler for first token" , "request_id" , requestID , "next_prediction_token" , predictedLatencyCtx .decodeTokenSampler .getNextSampleToken ())
295- }
296- }
297-
298286func predictFirstTPOT (ctx context.Context , predictedLatencyCtx * predictedLatencyCtx ) {
299287 logger := log .FromContext (ctx )
300288 targetName := predictedLatencyCtx .targetMetadata .NamespacedName .Name
@@ -309,67 +297,20 @@ func predictFirstTPOT(ctx context.Context, predictedLatencyCtx *predictedLatency
309297 }
310298}
311299
312- // processTokenForLatencyPrediction records actual inter-token latency, sampled predictions, and advances timestamp.
300+ // processTokenForLatencyPrediction records the actual TPOT for the token and advances the timestamp.
313301func processTokenForLatencyPrediction (
314302 ctx context.Context ,
315- pluginName , pluginType string ,
316- predictor latencypredictor.PredictorInterface ,
317- endpointRoleLabel string ,
318303 predictedLatencyCtx * predictedLatencyCtx ,
319- targetEndpointMetadata * fwkdl.EndpointMetadata ,
320304 now time.Time ,
321- samplingMean float64 ,
322- maxDecodeTokenSamplesForPrediction int ,
323305) {
324306 logger := log .FromContext (ctx )
325307
326- if predictedLatencyCtx .decodeTokenSampler == nil {
327- requestID := predictedLatencyCtx .schedulingRequest .Headers [reqcommon .RequestIDHeaderKey ]
328- predictedLatencyCtx .decodeTokenSampler = newDecodeTokenSampler (requestID , samplingMean , maxDecodeTokenSamplesForPrediction )
329- logger .V (logutil .DEBUG ).Info ("Initialized token sampler for subsequent tokens" , "request_id" , requestID , "next_prediction_token" , predictedLatencyCtx .decodeTokenSampler .getNextSampleToken ())
330- }
331-
332308 latencyMs := float64 (now .Sub (predictedLatencyCtx .lastTokenTimestamp ).Milliseconds ())
333309 predictedLatencyCtx .generatedTokenCount ++
334310
335- if predictedLatencyCtx .generatedTokenCount == 2 || predictedLatencyCtx .decodeTokenSampler .shouldPredict (predictedLatencyCtx .generatedTokenCount ) {
336- predictedLatencyCtx .tpotObservations = append (predictedLatencyCtx .tpotObservations , latencyMs )
337- }
338311 if predictedLatencyCtx .generatedTokenCount == 2 {
339312 logger .V (logutil .DEBUG ).Info ("First inter-token latency observed" ,
340- "actual_tpot_ms" , latencyMs ,
341- "predicted_tpot_ms" , predictedLatencyCtx .avgPredictedTPOT )
342- }
343-
344- m , err := getLatestMetricsForProfile (predictedLatencyCtx , "" )
345- if err != nil {
346- logger .V (logutil .DEBUG ).Info ("Skipping TPOT prediction due to missing metrics or schedulingResult" , "error" , err )
347- return
348- }
349-
350- if predictedLatencyCtx .decodeTokenSampler .shouldPredict (predictedLatencyCtx .generatedTokenCount ) {
351- in := buildPredictionRequest (
352- endpointRoleLabel ,
353- targetEndpointMetadata ,
354- m ,
355- predictedLatencyCtx .inputTokenCount ,
356- predictedLatencyCtx .generatedTokenCount ,
357- 0 ,
358- )
359- start := time .Now ()
360- p , err := predictor .Predict (ctx , in )
361- dur := time .Since (start )
362- if err != nil || p == nil {
363- logger .V (logutil .DEBUG ).Error (err , "TPOT predict failed" , "duration_ms" , dur .Milliseconds ())
364- predictedLatencyCtx .predictedTPOTObservations = append (predictedLatencyCtx .predictedTPOTObservations , 0 )
365- predictedLatencyCtx .avgPredictedTPOT = calculateRunningAverage (predictedLatencyCtx .avgPredictedTPOT , 0 , len (predictedLatencyCtx .predictedTPOTObservations ))
366- } else {
367- logger .V (logutil .DEBUG ).Info ("TPOT predict succeeded" , "value_ms" , p .TPOT , "duration_ms" , dur .Milliseconds ())
368- predictedLatencyCtx .predictedTPOTObservations = append (predictedLatencyCtx .predictedTPOTObservations , p .TPOT )
369- predictedLatencyCtx .avgPredictedTPOT = calculateRunningAverage (predictedLatencyCtx .avgPredictedTPOT , p .TPOT , len (predictedLatencyCtx .predictedTPOTObservations ))
370- }
371- recordRequestTPOTPredictionDuration (ctx , pluginName , pluginType , predictedLatencyCtx .schedulingRequest .TargetModel , predictedLatencyCtx .incomingModelName , dur .Seconds ())
372- predictedLatencyCtx .decodeTokenSampler .recordPrediction (predictedLatencyCtx .generatedTokenCount )
313+ "actual_tpot_ms" , latencyMs )
373314 }
374315
375316 predictedLatencyCtx .lastTokenTimestamp = now
0 commit comments