Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions python/sglang/multimodal_gen/runtime/models/dits/qwen_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

logger = init_logger(__name__) # pylint: disable=invalid-name


try:
from nunchaku.models.attention import NunchakuFeedForward # type: ignore[import]
except Exception:
Expand Down Expand Up @@ -802,7 +803,7 @@ def _modulate(
is_scale_residual = isinstance(norm_module, ScaleResidualLayerNormScaleShift)

shift, scale, gate = mod_params.chunk(3, dim=-1)
if index is not None:
if index is not None and x.is_cuda:
Comment thread
ping1jing2 marked this conversation as resolved.
Resolved
actual_batch = x.shape[0]
shift0, shift1 = (
shift[:actual_batch],
Expand Down Expand Up @@ -856,9 +857,32 @@ def _modulate(
)
return x, gate_result
else:
shift_result = shift.unsqueeze(1)
scale_result = scale.unsqueeze(1)
gate_result = gate.unsqueeze(1)
if index is not None:
actual_batch = x.shape[0]
shift0, shift1 = (
shift[:actual_batch],
shift[actual_batch : 2 * actual_batch],
)
scale0, scale1 = (
scale[:actual_batch],
scale[actual_batch : 2 * actual_batch],
)
gate0, gate1 = (
gate[:actual_batch],
gate[actual_batch : 2 * actual_batch],
)
index = index.to(dtype=torch.bool).unsqueeze(-1)
shift_result = torch.where(
index, shift1.unsqueeze(1), shift0.unsqueeze(1)
)
scale_result = torch.where(
index, scale1.unsqueeze(1), scale0.unsqueeze(1)
)
gate_result = torch.where(index, gate1.unsqueeze(1), gate0.unsqueeze(1))
else:
shift_result = shift.unsqueeze(1)
scale_result = scale.unsqueeze(1)
gate_result = gate.unsqueeze(1)
if is_scale_residual:
modulated, residual_out = norm_module(
residual=residual_x,
Expand Down Expand Up @@ -1104,8 +1128,8 @@ def build_modulate_index(self, img_shapes: tuple[int, int, int], device):
first_size = sample[0][0] * sample[0][1] * sample[0][2]
total_size = sum(s[0] * s[1] * s[2] for s in sample)
if sp_world_size > 1:
first_local_size = _local_seq_len(first_size)
tail_local_size = _local_seq_len(total_size - first_size)
first_local_size = _local_seq_len(first_size, sp_world_size)
tail_local_size = _local_seq_len(total_size - first_size, sp_world_size)
idx = torch.cat(
[
torch.zeros(first_local_size, device=device, dtype=torch.int),
Expand Down
Loading