Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions deepspeed/module_inject/auto_ep_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def validate_autoep_config(
f"got '{config.combine_impl}'")

# Validate row_weighting_impl
valid_row_weighting_impl = ("auto", "eager", "fused")
valid_row_weighting_impl = ("auto", "eager", "fused", "activation")
if config.row_weighting_impl not in valid_row_weighting_impl:
raise ValueError(f"row_weighting_impl must be one of {valid_row_weighting_impl}, "
f"got '{config.row_weighting_impl}'")
Expand Down Expand Up @@ -246,6 +246,9 @@ def validate_autoep_config(
if isinstance(rows_per_expert, bool) or not isinstance(rows_per_expert, int) or rows_per_expert < 0:
raise ValueError(f"deferred_dw_rows_per_expert must be a non-negative integer, got {rows_per_expert!r}")
if flush_period > 1:
if config.row_weighting_impl == "activation":
raise ValueError("row_weighting_impl=\"activation\" has not been validated with deferred_dw_flush_period "
"> 1. Leave deferred_dw_flush_period at 1 or use the existing row-weighting path.")
if not config.use_grouped_mm:
raise ValueError("deferred_dw_flush_period stages the experts' torch._grouped_mm weight gradients, but "
"use_grouped_mm=false selects the sequential expert loop. Set use_grouped_mm=true, or "
Expand All @@ -261,13 +264,14 @@ def validate_autoep_config(
raise ValueError("deferred_dw_rows_per_expert sizes the staging that deferred_dw_flush_period > 1 enables. "
"Set deferred_dw_flush_period, or leave deferred_dw_rows_per_expert unset.")

if config.row_weighting_impl == "fused":
if config.row_weighting_impl in ("fused", "activation"):
impl = config.row_weighting_impl
if config.comm_backend != "deepep":
raise ValueError('row_weighting_impl="fused" only runs inside the DeepEP route, but '
raise ValueError(f'row_weighting_impl="{impl}" only runs inside the DeepEP route, but '
f'comm_backend="{config.comm_backend}" was selected. Set comm_backend to '
'"deepep", or leave row_weighting_impl unset.')
if config.autoep_size == 1:
raise ValueError('row_weighting_impl="fused" only runs when DeepEP dispatch is active '
raise ValueError(f'row_weighting_impl="{impl}" only runs when DeepEP dispatch is active '
'(autoep_size > 1), but autoep_size=1. Increase autoep_size, or leave '
"row_weighting_impl unset.")

Expand Down
29 changes: 23 additions & 6 deletions deepspeed/module_inject/auto_ep_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def resolve_combine_impl(
return "weighted_sum"


def resolve_row_weighting_impl(config_override: Literal["auto", "eager", "fused"]) -> Literal["eager", "fused"]:
def resolve_row_weighting_impl(
config_override: Literal["auto", "eager", "fused", "activation"]) -> Literal["eager", "fused", "activation"]:
"""Resolve DeepEP row weighting implementation from config override."""
if config_override != "auto":
return config_override
Expand Down Expand Up @@ -586,6 +587,11 @@ def __init__(
# The decoder layer holding this layer, set by attach_decoder_layer; held weakly, as the decoder layer
# owns this module.
self._decoder_layer = None
if self.row_weighting_impl == "activation" and self.score_apply != "post":
raise ValueError('row_weighting_impl="activation" applies the routing weights inside the expert '
'activation, after the experts\' first projections, so it requires score_apply to '
f'resolve to "post"; preset \'{spec.model_family}\' resolves it to '
f'"{self.score_apply}". Use "eager" or "fused" for this model.')
self._fused_combine_checked = False
self._fused_row_weighting_checked = False
route_norm = spec.route_norm if config.route_norm is None else config.route_norm
Expand Down Expand Up @@ -776,6 +782,9 @@ def __init__(
"overlap_weight_grad unset.")
self.dw_staging = None
if config.deferred_dw_flush_period > 1:
if self.row_weighting_impl == "activation":
raise ValueError("row_weighting_impl=\"activation\" has not been validated with "
"deferred_dw_flush_period > 1")
if self.experts.use_triton_grouped_mm or not self.experts.use_grouped_mm:
raise ValueError("deferred_dw_flush_period stages the experts' torch._grouped_mm weight gradients, "
"but this device selects the Triton grouped GEMM. Set "
Expand Down Expand Up @@ -1119,15 +1128,23 @@ def _deepep_route(self, tokens: torch.Tensor, ro: "RouterOutput") -> torch.Tenso
raise RuntimeError(f"DeepEP returned {counts.numel()} expert counts, but this rank owns "
f"{self.num_local_experts} experts")

if weight_grad_slot is None:
if weights is not None and self.row_weighting_impl == "activation":
# The down projection is linear and has no bias, so scaling its input rows by the routing weights
# scales its output rows the same way, without a separate pass over the expert output.
expert_output = self.experts(received,
counts,
weight_grad_slot=weight_grad_slot,
row_weights=weights.reshape(-1))
elif weight_grad_slot is None:
expert_output = self._run_experts(received, counts)
else:
expert_output = self.experts(received, counts, weight_grad_slot=weight_grad_slot)

if weights is not None and record_only:
expert_output = record_deepep_row_weights(expert_output, weights, self.row_weighting_impl)
elif weights is not None:
expert_output = apply_deepep_row_weights(expert_output, weights, self.row_weighting_impl)
if weights is not None and self.row_weighting_impl != "activation":
if record_only:
expert_output = record_deepep_row_weights(expert_output, weights, self.row_weighting_impl)
else:
expert_output = apply_deepep_row_weights(expert_output, weights, self.row_weighting_impl)

if record_only:
return deepep_record_combine(exchange, expert_output, handle)
Expand Down
2 changes: 1 addition & 1 deletion deepspeed/module_inject/auto_ep_presets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class AutoEPConfig:
route_scale: float = 1.0
score_apply: Literal["auto", "pre", "post"] = "auto"
combine_impl: Literal["auto", "weighted_sum", "fused_weighted_sum", "legacy_bmm"] = "auto"
row_weighting_impl: Literal["auto", "eager", "fused"] = "auto"
row_weighting_impl: Literal["auto", "eager", "fused", "activation"] = "auto"
gate_up_impl: Literal["separate", "fused"] = "separate"
overlap_weight_grad: bool = False
deferred_dw_flush_period: int = 1
Expand Down
Loading
Loading