Skip to content

Commit 4d303c4

Browse files
authored
Fix token usage with jump forward (#174)
1 parent 37b4229 commit 4d303c4

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

python/sglang/srt/managers/router/infer_batch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def __init__(self, rid, input_text, input_ids):
2727
self.input_ids = input_ids
2828
self.output_ids = []
2929

30+
# for accumulated prompt tokens from jump forward
31+
self.orig_prompt_tokens = len(input_ids)
32+
3033
# For vision input
3134
self.pixel_values = None
3235
self.image_size = None

python/sglang/srt/managers/router/model_rpc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,16 @@ def handle_finished_requests(self, batch: Batch):
534534
output_skip_special_tokens.append(
535535
req.sampling_params.skip_special_tokens
536536
)
537+
538+
# For the length of input_ids, which will be accumulated during jump-forward.
539+
# Use the original length of input_ids to calculate the token usage info.
537540
meta_info = {
538-
"prompt_tokens": len(req.input_ids),
539-
"completion_tokens": len(req.output_ids),
541+
"prompt_tokens": req.orig_prompt_tokens,
542+
"completion_tokens": len(req.input_ids)
543+
+ len(req.output_ids)
544+
- req.orig_prompt_tokens,
540545
}
546+
541547
if req.return_logprob:
542548
meta_info["prompt_logprob"] = req.logprob
543549
meta_info["token_logprob"] = req.token_logprob

0 commit comments

Comments
 (0)