Skip to content
Merged
Changes from 1 commit
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
21 changes: 13 additions & 8 deletions megatron/core/optimizer/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,20 +1161,25 @@ def _split_state_dict(self, state_dict):
state_dicts = [None] * len(self.chained_optimizers)
if state_dict is not None:
if len(self.model_chunks) == 1:
state_dicts[0] = state_dict
# When there is only one global model chunk, all sub-optimizers
# (e.g., dense and MoE parts) use the same model state dict.
state_dicts = [state_dict] * len(self.chained_optimizers)
Comment thread
kunlunl marked this conversation as resolved.
Resolved
else:
# Split state_dict if needed
# Split state_dict by chunk identity
prefix = "model" if "model0" in state_dict.keys() else "model_"
offset = 0
chunk_id_to_global_idx = {id(chunk): idx for idx, chunk in enumerate(self.model_chunks)}
Comment thread
kunlunl marked this conversation as resolved.
Resolved
for optimizer_idx, optimizer in enumerate(self.chained_optimizers):
if hasattr(optimizer, "model_chunks"):
d = {}
for chunk_idx in range(len(optimizer.model_chunks)):
for chunk_idx, model_chunk in enumerate(optimizer.model_chunks):
assert id(model_chunk) in chunk_id_to_global_idx, (
"Sub-optimizer model chunk was not found in chained optimizer model chunks"
)
global_idx = chunk_id_to_global_idx[id(model_chunk)]
assert (
f"{prefix}{offset}" in state_dict
), f"Wrong state_dict format, cannot find '{prefix}{offset}'"
d[f"{prefix}{chunk_idx}"] = state_dict[f"{prefix}{offset}"]
offset += 1
f"{prefix}{global_idx}" in state_dict
), f"Wrong state_dict format, cannot find '{prefix}{global_idx}'"
d[f"{prefix}{chunk_idx}"] = state_dict[f"{prefix}{global_idx}"]
if len(d) > 0:
state_dicts[optimizer_idx] = d
return state_dicts
Expand Down