@@ -190,17 +190,20 @@ def inference(self,
190190 num_return_sequences = getattr (self .model , 'generation_kwargs' ,
191191 {}).get ('num_return_sequences' , 1 )
192192 # 5-3. Save current output
193- for prompt , prediction , gold in zip (
194- parsed_entries , batched (generated , num_return_sequences ) ,
195- golds ):
193+ for batch_idx , ( prompt , prediction , gold ) in enumerate (
194+ zip ( parsed_entries , batched (generated ,
195+ num_return_sequences ), golds ) ):
196196 if num_return_sequences == 1 :
197197 prediction = prediction [0 ]
198198
199199 if self .dump_res_length :
200- input_length = 0
201- if isinstance (prompt , str ):
200+ if self .multiround and isinstance (prompt , list ):
201+ input_length = self ._compute_multiround_input_lengths (
202+ entry [batch_idx ])
203+ elif isinstance (prompt , str ):
202204 input_length = self .model .get_token_len (prompt )
203205 elif isinstance (prompt , list ):
206+ input_length = 0
204207 for i in range (len (prompt )):
205208 if 'prompt' in prompt [i ]:
206209 prompt [i ][
@@ -275,6 +278,29 @@ def inference(self,
275278 for sample in output_handler .results_dict .values ()
276279 ]
277280
281+ def _compute_multiround_input_lengths (self , chat : List ) -> List [int ]:
282+ """Compute cumulative input token length at each generation turn.
283+
284+ Expects ``chat`` to be the filled multi-round conversation where
285+ assistant slots already contain the generated responses. Returns a
286+ list of cumulative token counts, one per generation turn, i.e. the
287+ input length the model actually sees when generating each turn.
288+ """
289+ input_lengths = []
290+ cumulative = 0
291+ for msg in chat :
292+ if isinstance (msg , dict ):
293+ role = msg .get ('role' , '' )
294+ content = msg .get ('content' , msg .get ('prompt' , '' ))
295+ else :
296+ role , content = '' , msg
297+ if not isinstance (content , str ):
298+ content = str (content )
299+ if role == 'assistant' :
300+ input_lengths .append (cumulative )
301+ cumulative += self .model .get_token_len (content )
302+ return input_lengths
303+
278304 def _generate_multiround (self , entry : List ,
279305 extra_gen_kwargs : dict ) -> List [List [str ]]:
280306 """Multi-turn generation with dynamic turn-level scheduling.
0 commit comments