Skip to content
Merged
Changes from all 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
7 changes: 5 additions & 2 deletions python/sglang/srt/models/deepseek_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,13 @@ def forward_deepep(
topk_weights=topk_weights,
forward_mode=forward_mode,
)
final_hidden_states *= self.routed_scaling_factor

if shared_output is not None:
final_hidden_states = final_hidden_states + shared_output
x = shared_output
x.add_(final_hidden_states, alpha=self.routed_scaling_factor)
final_hidden_states = x
Comment on lines +425 to +427
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new logic for when shared_output is not None modifies shared_output in-place via the reference x before assigning it to final_hidden_states. This approach (shared_output_original + final_hidden_states_unscaled * scale) is mathematically equivalent to the previous logic ((final_hidden_states_unscaled * scale) + shared_output_original) and can be more memory-efficient by avoiding an intermediate tensor allocation.

Could you confirm that shared_output is an intermediate tensor that can be safely modified in-place here and is not used elsewhere in its original form after this operation?

If so, this is a good optimization. For slightly more directness, you could consider directly operating on shared_output without the intermediate x variable, as shown in the suggestion.

Suggested change
x = shared_output
x.add_(final_hidden_states, alpha=self.routed_scaling_factor)
final_hidden_states = x
# Update shared_output in-place, then assign to final_hidden_states.
# This is equivalent to:
# final_hidden_states = shared_output_original + final_hidden_states_unscaled * self.routed_scaling_factor
# and can be more memory-efficient if shared_output is an intermediate tensor.
shared_output.add_(final_hidden_states, alpha=self.routed_scaling_factor)
final_hidden_states = shared_output

else:
final_hidden_states *= self.routed_scaling_factor

return final_hidden_states

Expand Down
Loading