Skip to content

Commit 2c1b164

Browse files
authored
[diffusion] improve: skip negative prompt encoding when guidance_scale <= 1.0 or negative_prompt is None (#16919)
Signed-off-by: zhuyuhua-v <yuhzhu@amd.com>
1 parent bcc6d84 commit 2c1b164

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

python/sglang/multimodal_gen/runtime/pipelines_core/stages/image_encoding.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,16 @@ def forward(
125125
elif self.text_encoder:
126126
# if a text encoder is provided, e.g. Qwen-Image-Edit
127127
# 1. neg prompt embeds
128-
neg_image_processor_kwargs = (
129-
server_args.pipeline_config.prepare_image_processor_kwargs(
130-
batch, neg=True
128+
if batch.do_classifier_free_guidance:
129+
neg_image_processor_kwargs = (
130+
server_args.pipeline_config.prepare_image_processor_kwargs(
131+
batch, neg=True
132+
)
131133
)
132-
)
133134

134-
neg_image_inputs = self.image_processor(
135-
images=image, return_tensors="pt", **neg_image_processor_kwargs
136-
).to(cuda_device)
135+
neg_image_inputs = self.image_processor(
136+
images=image, return_tensors="pt", **neg_image_processor_kwargs
137+
).to(cuda_device)
137138

138139
with set_forward_context(current_timestep=0, attn_metadata=None):
139140
outputs = self.text_encoder(
@@ -143,20 +144,22 @@ def forward(
143144
image_grid_thw=image_inputs.image_grid_thw,
144145
output_hidden_states=True,
145146
)
146-
neg_outputs = self.text_encoder(
147-
input_ids=neg_image_inputs.input_ids,
148-
attention_mask=neg_image_inputs.attention_mask,
149-
pixel_values=neg_image_inputs.pixel_values,
150-
image_grid_thw=neg_image_inputs.image_grid_thw,
151-
output_hidden_states=True,
152-
)
147+
if batch.do_classifier_free_guidance:
148+
neg_outputs = self.text_encoder(
149+
input_ids=neg_image_inputs.input_ids,
150+
attention_mask=neg_image_inputs.attention_mask,
151+
pixel_values=neg_image_inputs.pixel_values,
152+
image_grid_thw=neg_image_inputs.image_grid_thw,
153+
output_hidden_states=True,
154+
)
153155
batch.prompt_embeds.append(
154156
self.encoding_qwen_image_edit(outputs, image_inputs)
155157
)
156158

157-
batch.negative_prompt_embeds.append(
158-
self.encoding_qwen_image_edit(neg_outputs, neg_image_inputs)
159-
)
159+
if batch.do_classifier_free_guidance:
160+
batch.negative_prompt_embeds.append(
161+
self.encoding_qwen_image_edit(neg_outputs, neg_image_inputs)
162+
)
160163

161164
self.offload_model()
162165

0 commit comments

Comments
 (0)