@@ -138,7 +138,7 @@ func (pl *PredictedLatency) ResponseBody(ctx context.Context, request *fwksched.
138138
139139 if predictedLatencyCtx .ttft == 0 {
140140 if pl .config .StreamingMode && ! response .EndOfStream {
141- processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now , pl . config . SamplingMean , pl . config . MaxDecodeTokenSamplesForPrediction )
141+ processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now )
142142
143143 // Only decrement if PreRequest actually incremented the prefill pod counter.
144144 // If Produce timed out, PreRequest may have skipped incrementing, and
@@ -149,13 +149,13 @@ func (pl *PredictedLatency) ResponseBody(ctx context.Context, request *fwksched.
149149 }
150150 }
151151 } else {
152- processTokenForLatencyPrediction (ctx , pl . typedName . Name , pl . typedName . Type , pl . latencypredictor , pl . config . EndpointRoleLabel , predictedLatencyCtx , targetMetadata , now , pl . config . SamplingMean , pl . config . MaxDecodeTokenSamplesForPrediction )
152+ processTokenForLatencyPrediction (ctx , predictedLatencyCtx , now )
153153 }
154154
155155 if response .EndOfStream {
156156 ttftNotYetRecorded := predictedLatencyCtx .ttft == 0
157157 if ! pl .config .StreamingMode {
158- processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now , pl . config . SamplingMean , pl . config . MaxDecodeTokenSamplesForPrediction )
158+ processFirstTokenForLatencyPrediction (ctx , pl .latencypredictor , pl .config .StreamingMode , pl .config .EndpointRoleLabel , predictedLatencyCtx , now )
159159 }
160160
161161 if predictedLatencyCtx .ttft > 0 {
@@ -257,12 +257,9 @@ func processFirstTokenForLatencyPrediction(
257257 endpointRoleLabel string ,
258258 predictedLatencyCtx * predictedLatencyCtx ,
259259 now time.Time ,
260- samplingMean float64 ,
261- maxDecodeTokenSamplesForPrediction int ,
262260) {
263261 logger := log .FromContext (ctx )
264262
265- initializeSampler (ctx , predictedLatencyCtx , samplingMean , maxDecodeTokenSamplesForPrediction )
266263 predictedLatencyCtx .ttft = float64 (now .Sub (predictedLatencyCtx .requestReceivedTimestamp ).Milliseconds ())
267264 predictedLatencyCtx .generatedTokenCount = 1
268265
@@ -296,15 +293,6 @@ func processFirstTokenForLatencyPrediction(
296293 refreshLastSeenMetrics (ctx , predictedLatencyCtx )
297294}
298295
299- func initializeSampler (ctx context.Context , predictedLatencyCtx * predictedLatencyCtx , samplingMean float64 , maxDecodeTokenSamplesForPrediction int ) {
300- if predictedLatencyCtx .decodeTokenSampler == nil {
301- logger := log .FromContext (ctx )
302- requestID := predictedLatencyCtx .schedulingRequest .Headers [reqcommon .RequestIDHeaderKey ]
303- predictedLatencyCtx .decodeTokenSampler = newDecodeTokenSampler (requestID , samplingMean , maxDecodeTokenSamplesForPrediction )
304- logger .V (logutil .DEBUG ).Info ("Initialized token sampler for first token" , "request_id" , requestID , "next_prediction_token" , predictedLatencyCtx .decodeTokenSampler .getNextSampleToken ())
305- }
306- }
307-
308296func predictFirstTPOT (ctx context.Context , predictedLatencyCtx * predictedLatencyCtx ) {
309297 logger := log .FromContext (ctx )
310298 targetName := predictedLatencyCtx .targetMetadata .NamespacedName .Name
@@ -319,67 +307,20 @@ func predictFirstTPOT(ctx context.Context, predictedLatencyCtx *predictedLatency
319307 }
320308}
321309
322- // processTokenForLatencyPrediction records actual inter-token latency, sampled predictions, and advances timestamp.
310+ // processTokenForLatencyPrediction records actual inter-token latency and advances the timestamp.
323311func processTokenForLatencyPrediction (
324312 ctx context.Context ,
325- pluginName , pluginType string ,
326- predictor latencypredictor.PredictorInterface ,
327- endpointRoleLabel string ,
328313 predictedLatencyCtx * predictedLatencyCtx ,
329- targetEndpointMetadata * fwkdl.EndpointMetadata ,
330314 now time.Time ,
331- samplingMean float64 ,
332- maxDecodeTokenSamplesForPrediction int ,
333315) {
334316 logger := log .FromContext (ctx )
335317
336- if predictedLatencyCtx .decodeTokenSampler == nil {
337- requestID := predictedLatencyCtx .schedulingRequest .Headers [reqcommon .RequestIDHeaderKey ]
338- predictedLatencyCtx .decodeTokenSampler = newDecodeTokenSampler (requestID , samplingMean , maxDecodeTokenSamplesForPrediction )
339- logger .V (logutil .DEBUG ).Info ("Initialized token sampler for subsequent tokens" , "request_id" , requestID , "next_prediction_token" , predictedLatencyCtx .decodeTokenSampler .getNextSampleToken ())
340- }
341-
342318 latencyMs := float64 (now .Sub (predictedLatencyCtx .lastTokenTimestamp ).Milliseconds ())
343319 predictedLatencyCtx .generatedTokenCount ++
344320
345- if predictedLatencyCtx .generatedTokenCount == 2 || predictedLatencyCtx .decodeTokenSampler .shouldPredict (predictedLatencyCtx .generatedTokenCount ) {
346- predictedLatencyCtx .tpotObservations = append (predictedLatencyCtx .tpotObservations , latencyMs )
347- }
348321 if predictedLatencyCtx .generatedTokenCount == 2 {
349322 logger .V (logutil .DEBUG ).Info ("First inter-token latency observed" ,
350- "actual_tpot_ms" , latencyMs ,
351- "predicted_tpot_ms" , predictedLatencyCtx .avgPredictedTPOT )
352- }
353-
354- m , err := getLatestMetricsForProfile (predictedLatencyCtx , "" )
355- if err != nil {
356- logger .V (logutil .DEBUG ).Info ("Skipping TPOT prediction due to missing metrics or schedulingResult" , "error" , err )
357- return
358- }
359-
360- if predictedLatencyCtx .decodeTokenSampler .shouldPredict (predictedLatencyCtx .generatedTokenCount ) {
361- in := buildPredictionRequest (
362- endpointRoleLabel ,
363- targetEndpointMetadata ,
364- m ,
365- predictedLatencyCtx .inputTokenCount ,
366- predictedLatencyCtx .generatedTokenCount ,
367- 0 ,
368- )
369- start := time .Now ()
370- p , err := predictor .Predict (ctx , in )
371- dur := time .Since (start )
372- if err != nil || p == nil {
373- logger .V (logutil .DEBUG ).Error (err , "TPOT predict failed" , "duration_ms" , dur .Milliseconds ())
374- predictedLatencyCtx .predictedTPOTObservations = append (predictedLatencyCtx .predictedTPOTObservations , 0 )
375- predictedLatencyCtx .avgPredictedTPOT = calculateRunningAverage (predictedLatencyCtx .avgPredictedTPOT , 0 , len (predictedLatencyCtx .predictedTPOTObservations ))
376- } else {
377- logger .V (logutil .DEBUG ).Info ("TPOT predict succeeded" , "value_ms" , p .TPOT , "duration_ms" , dur .Milliseconds ())
378- predictedLatencyCtx .predictedTPOTObservations = append (predictedLatencyCtx .predictedTPOTObservations , p .TPOT )
379- predictedLatencyCtx .avgPredictedTPOT = calculateRunningAverage (predictedLatencyCtx .avgPredictedTPOT , p .TPOT , len (predictedLatencyCtx .predictedTPOTObservations ))
380- }
381- recordRequestTPOTPredictionDuration (ctx , pluginName , pluginType , predictedLatencyCtx .schedulingRequest .TargetModel , predictedLatencyCtx .incomingModelName , dur .Seconds ())
382- predictedLatencyCtx .decodeTokenSampler .recordPrediction (predictedLatencyCtx .generatedTokenCount )
323+ "actual_tpot_ms" , latencyMs )
383324 }
384325
385326 predictedLatencyCtx .lastTokenTimestamp = now
0 commit comments