From 25a5641cf290eeba0c1cc9ca0660447f0482d432 Mon Sep 17 00:00:00 2001 From: Byron Hsu Date: Sat, 12 Sep 2026 10:31:55 -0700 Subject: [PATCH] [Session + MM] Fix text positions in session continuations (#39144) Co-authored-by: Byron Hsu Co-authored-by: Manik Singhal <3400497+Manikvsin@users.noreply.github.com> --- .../srt/model_executor/forward_batch_info.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/model_executor/forward_batch_info.py b/python/sglang/srt/model_executor/forward_batch_info.py index 5fff91290..42318607a 100644 --- a/python/sglang/srt/model_executor/forward_batch_info.py +++ b/python/sglang/srt/model_executor/forward_batch_info.py @@ -1330,7 +1330,21 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin): :, extend_prefix_len : extend_prefix_len + extend_seq_len, ] - if mrope_positions.numel() == 0: + if ( + batch.reqs[batch_idx].session is not None + and mrope_positions.shape[1] < extend_seq_len + ): + # Session history includes generated and appended text that + # is not covered by the saved prompt positions. + tail_len = extend_seq_len - mrope_positions.shape[1] + tail_start = extend_prefix_len + mrope_positions.shape[1] + text_positions = self._expand_mrope_from_input( + mm_input, tail_start + 1 + ) + torch.arange(tail_len) + mrope_positions = torch.cat( + [mrope_positions, text_positions], dim=1 + ) + elif mrope_positions.numel() == 0: mrope_positions = self._expand_mrope_from_input( mm_input, seq_lens_cpu[batch_idx] )