From 45194794d0bf51127930d99a6037e04283e11622 Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Sun, 31 May 2026 09:47:50 +0800 Subject: [PATCH] Apply gemma's position offset out-of-place instead of in-place (#26799) --- python/sglang/srt/environ.py | 1 + python/sglang/srt/models/gemma4_mm.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index 5e0b95f1d..f2c238a8c 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -199,6 +199,7 @@ class Envs: SGLANG_SORT_WEIGHT_FILES = EnvBool(False) SGLANG_DISABLED_MODEL_ARCHS = EnvTuple(tuple()) SGLANG_PREFETCH_BLOCK_SIZE_MB = EnvInt(16) + SGLANG_GEMMA_OUT_OF_PLACE_POSITION_MUTATION = EnvBool(False) # Logging Options SGLANG_LOG_GC = EnvBool(False) diff --git a/python/sglang/srt/models/gemma4_mm.py b/python/sglang/srt/models/gemma4_mm.py index cafc31f20..3629782ca 100644 --- a/python/sglang/srt/models/gemma4_mm.py +++ b/python/sglang/srt/models/gemma4_mm.py @@ -29,6 +29,7 @@ from transformers import ( ) from sglang.srt.distributed import get_pp_group +from sglang.srt.environ import envs from sglang.srt.layers.attention.triton_backend import TritonAttnBackend from sglang.srt.layers.layernorm import Gemma4RMSNorm from sglang.srt.layers.linear import ReplicatedLinear @@ -595,7 +596,11 @@ class Gemma4ForConditionalGeneration(PreTrainedModel): "You must specify exactly one of input_ids or inputs_embeds" ) - positions += 1 + if envs.SGLANG_GEMMA_OUT_OF_PLACE_POSITION_MUTATION.get(): + positions = positions + 1 + else: + positions += 1 + per_layer_inputs = None # PLE table and the per-layer projection live on the first rank only, # so non-first ranks must skip this and pull per_layer_inputs from the