-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix replication on router and checkpointing
Signed-off-by: Yu Chin Fabian Lim <flim@sg.ibm.com>
- Loading branch information
Showing
5 changed files
with
207 additions
and
9 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
plugins/accelerated-moe/src/fms_acceleration_moe/megablocks_utils/checkpoint_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright The FMS HF Tuning Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Standard | ||
import os | ||
|
||
# Third Party | ||
from accelerate.logging import get_logger | ||
from accelerate.utils.constants import ( | ||
FSDP_MODEL_NAME, | ||
OPTIMIZER_NAME, | ||
) | ||
from torch.distributed.checkpoint.state_dict import ( | ||
get_state_dict, | ||
set_state_dict, | ||
) | ||
import torch.distributed.checkpoint as dcp | ||
|
||
logger = get_logger(__name__) | ||
|
||
MODEL_INDEX = None | ||
|
||
def save_fsdp_model( | ||
fsdp_plugin, accelerator, model, output_dir, model_index=0, adapter_only=False | ||
): | ||
|
||
# pylint: disable=global-statement | ||
global MODEL_INDEX | ||
MODEL_INDEX = model_index | ||
|
||
def save_fsdp_optimizer( | ||
fsdp_plugin, accelerator, optimizer, model, output_dir, optimizer_index=0 | ||
): | ||
(model_state_dict, optimizer_state_dict) = get_state_dict(model, optimizer) | ||
|
||
ckpt_model = os.path.join(output_dir, f"{FSDP_MODEL_NAME}_{MODEL_INDEX}") | ||
os.makedirs(ckpt_model, exist_ok=True) | ||
logger.info(f"Saving model to {ckpt_model}") | ||
dcp.save({"model": model_state_dict}, checkpoint_id=ckpt_model) | ||
logger.info(f"Model saved to {ckpt_model}") | ||
|
||
ckpt_opt = os.path.join(output_dir, f"{OPTIMIZER_NAME}_{optimizer_index}") | ||
os.makedirs(ckpt_opt, exist_ok=True) | ||
logger.info(f"Saving Optimizer state to {ckpt_opt}") | ||
dcp.save({"optimizer": optimizer_state_dict}, checkpoint_id=ckpt_opt) | ||
logger.info(f"Optimizer state saved in {ckpt_opt}") | ||
|
||
|
||
# accelerate.utils.fsdp_utils.py | ||
def load_fsdp_model( | ||
fsdp_plugin, accelerator, model, input_dir, model_index=0, adapter_only=False | ||
): | ||
# pylint: disable=global-statement | ||
global MODEL_INDEX | ||
MODEL_INDEX = model_index | ||
|
||
|
||
# accelerate.utils.fsdp_utils.py | ||
def load_fsdp_optimizer( | ||
fsdp_plugin, | ||
accelerator, | ||
optimizer, | ||
model, | ||
input_dir, | ||
optimizer_index=0, | ||
adapter_only=False, | ||
): | ||
|
||
model_state_dict, optimizer_state_dict = get_state_dict(model, optimizer) | ||
ckpt_model = os.path.join(input_dir, f"{FSDP_MODEL_NAME}_{MODEL_INDEX}") | ||
dcp.load({"model": model_state_dict}, checkpoint_id=ckpt_model) | ||
ckpt_opt = os.path.join(input_dir, f"{OPTIMIZER_NAME}_{optimizer_index}") | ||
dcp.load({"optimizer": optimizer_state_dict}, checkpoint_id=ckpt_opt) | ||
set_state_dict( | ||
model, | ||
optimizer, | ||
model_state_dict=model_state_dict, | ||
optim_state_dict=optimizer_state_dict, | ||
) | ||
|
||
# HACK for now | ||
# - if seems that if params is empty, then the loading has someo | ||
# problems | ||
# - so for now, we just dump some random defaults | ||
for group in optimizer.param_groups: | ||
if len(group["params"]) == 0: | ||
group["betas"] = (0.9, 0.999) | ||
group["lr"] = 0.0 | ||
group["initial_lr"] = 0.0 | ||
group["eps"] = 1e-8 | ||
group["weight_decay"] = 0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
plugins/accelerated-moe/src/fms_acceleration_moe/megablocks_utils/sparse_mlp2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters