[Bugfix][Rocm]Aiter MoE re-uses existing tensor addresses after weight update. (#40390)
Signed-off-by: Yuankai Chen <yuankach@amd.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
parent
e43a791284
commit
2e777d21a8
@@ -45,7 +45,10 @@ def set_weight_attrs(
|
||||
|
||||
|
||||
def replace_parameter(
|
||||
layer: torch.nn.Module, param_name: str, new_data: torch.Tensor | None
|
||||
layer: torch.nn.Module,
|
||||
param_name: str,
|
||||
new_data: torch.Tensor | None,
|
||||
prefer_copy: bool = False,
|
||||
):
|
||||
"""
|
||||
Replace a parameter of a layer while maintaining the ability to reload the weight.
|
||||
@@ -57,6 +60,12 @@ def replace_parameter(
|
||||
layer: Layer containing parameter to replace
|
||||
param_name: Name of parameter to replace
|
||||
new_data: New data of the new parameter, or None to set the parameter to None
|
||||
prefer_copy: If True and the existing parameter is compatible with
|
||||
``new_data`` (same shape, dtype, and device), copy ``new_data``
|
||||
into the existing parameter in place rather than re-registering
|
||||
a new parameter. This preserves the parameter's storage address
|
||||
(``data_ptr``), which is required for captured CUDA graphs to
|
||||
remain valid across weight updates (e.g. in RL training loops).
|
||||
"""
|
||||
# should not be used on a tied/shared param
|
||||
|
||||
@@ -67,9 +76,21 @@ def replace_parameter(
|
||||
|
||||
if isinstance(new_data, torch.nn.Parameter):
|
||||
new_data = new_data.data
|
||||
new_param = torch.nn.Parameter(new_data, requires_grad=False)
|
||||
|
||||
old_param: torch.nn.Parameter | None = getattr(layer, param_name, None)
|
||||
|
||||
if (
|
||||
prefer_copy
|
||||
and old_param is not None
|
||||
and old_param.shape == new_data.shape
|
||||
and old_param.dtype == new_data.dtype
|
||||
and old_param.device == new_data.device
|
||||
):
|
||||
old_param.copy_(new_data)
|
||||
return
|
||||
|
||||
new_param = torch.nn.Parameter(new_data, requires_grad=False)
|
||||
|
||||
if old_param is not None and hasattr(old_param, "weight_loader"):
|
||||
weight_loader = old_param.weight_loader
|
||||
set_weight_attrs(new_param, {"weight_loader": weight_loader})
|
||||
|
||||
Reference in New Issue
Block a user