-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for custom checkpointing. (#137)
* support for custom checkpointing. * add docs for custom checkpointing. * add finalization code.
- Loading branch information
1 parent
937e6c2
commit 95fdd59
Showing
19 changed files
with
504 additions
and
129 deletions.
There are no files selected for viewing
Empty file.
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,90 @@ | ||
""" | ||
Copyright (c) 2022, UChicago Argonne, LLC | ||
All Rights Reserved | ||
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. | ||
""" | ||
import os | ||
from abc import ABC, abstractmethod | ||
|
||
from dlio_benchmark.common.enumerations import CheckpointLocationType | ||
from dlio_benchmark.storage.storage_factory import StorageFactory | ||
from dlio_benchmark.utils.config import ConfigArguments | ||
from dlio_benchmark.utils.utility import DLIOMPI | ||
|
||
|
||
class BaseCheckpointing(ABC): | ||
|
||
def __init__(self, ext): | ||
self.ext = ext | ||
self.args = ConfigArguments.get_instance() | ||
checkpoint_storage = StorageFactory().get_storage(self.args.storage_type, self.args.checkpoint_folder, | ||
self.args.framework) | ||
checkpoint_storage.create_namespace(exist_ok=True) | ||
rank_to_checkpoint = self.args.my_rank | ||
if self.args.checkpoint_type == CheckpointLocationType.RANK_ZERO: | ||
rank_to_checkpoint = 0 | ||
if rank_to_checkpoint == self.args.my_rank: | ||
self.model_state = None | ||
if self.args.model_size > 0: | ||
self.model_state = {"a": self.get_tensor(self.args.model_size)} | ||
self.optimization_state = None | ||
if len(self.args.optimization_groups) > 0: | ||
self.optimization_state = dict() | ||
tensor_array_size = 0 | ||
for index, state in enumerate(self.args.optimization_groups): | ||
if state > 0: | ||
self.optimization_state[str(index)] = {'a': self.get_tensor(state), | ||
'b': self.get_tensor(state)} | ||
tensor_array_size += state | ||
self.optimization_state["combined"] = self.get_tensor(tensor_array_size) | ||
self.layer_state = None | ||
if len(self.args.layer_parameters) > 0: | ||
self.layer_state = dict() | ||
for index, state in enumerate(self.args.layer_parameters): | ||
if state > 0: | ||
self.layer_state[str(index)] = self.get_tensor(state) | ||
|
||
@abstractmethod | ||
def get_tensor(self, size): | ||
return [] | ||
|
||
@abstractmethod | ||
def save_state(self, suffix, state): | ||
pass | ||
|
||
def get_name(self, suffix): | ||
return os.path.join(self.args.checkpoint_folder, f"{suffix}.{self.ext}") | ||
|
||
@abstractmethod | ||
def checkpoint(self, epoch, step_number): | ||
rank_to_checkpoint = DLIOMPI.get_instance().rank() | ||
if self.args.checkpoint_type == CheckpointLocationType.RANK_ZERO: | ||
rank_to_checkpoint = 0 | ||
if rank_to_checkpoint == DLIOMPI.get_instance().rank(): | ||
my_rank = DLIOMPI.get_instance().rank() | ||
if self.model_state: | ||
self.save_state(suffix=f"model-{epoch}-{step_number}-{my_rank}", state=self.model_state) | ||
if self.optimization_state: | ||
self.save_state(suffix=f"optimizer-{epoch}-{step_number}-{my_rank}", state=self.optimization_state) | ||
if rank_to_checkpoint % self.args.pipeline_parallelism == 0: | ||
if self.layer_state and self.args.num_layers > 0: | ||
total_layers = self.args.num_layers | ||
if self.args.tensor_parallelism > 1: | ||
total_layers = total_layers + self.args.tensor_parallelism | ||
for layer in range(total_layers): | ||
self.save_state(suffix=f"layer-{layer}-{epoch}-{step_number}-{my_rank}", state=self.layer_state) | ||
|
||
@abstractmethod | ||
def finalize(self): | ||
pass |
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,43 @@ | ||
""" | ||
Copyright (c) 2022, UChicago Argonne, LLC | ||
All Rights Reserved | ||
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. | ||
""" | ||
import logging | ||
|
||
from dlio_benchmark.common.enumerations import CheckpointMechanismType | ||
from dlio_benchmark.common.error_code import ErrorCodes | ||
from dlio_benchmark.utils.config import ConfigArguments | ||
from dlio_benchmark.utils.utility import utcnow | ||
|
||
|
||
class CheckpointingFactory(object): | ||
def __init__(self): | ||
pass | ||
|
||
@staticmethod | ||
def get_mechanism(checkpoint_mechanism_type): | ||
_args = ConfigArguments.get_instance() | ||
if _args.checkpoint_mechanism_class is not None: | ||
logging.info(f"{utcnow()} Running DLIO with custom checkpointing mechanism " | ||
f"class {_args.checkpoint_mechanism_class.__name__}") | ||
return _args.checkpoint_mechanism_class.get_instance() | ||
elif checkpoint_mechanism_type == CheckpointMechanismType.TF_SAVE: | ||
from dlio_benchmark.checkpointing.tf_checkpointing import TFCheckpointing | ||
return TFCheckpointing.get_instance() | ||
elif checkpoint_mechanism_type == CheckpointMechanismType.PT_SAVE: | ||
from dlio_benchmark.checkpointing.pytorch_checkpointing import PyTorchCheckpointing | ||
return PyTorchCheckpointing.get_instance() | ||
else: | ||
raise Exception(str(ErrorCodes.EC1005)) |
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,61 @@ | ||
""" | ||
Copyright (c) 2022, UChicago Argonne, LLC | ||
All Rights Reserved | ||
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. | ||
""" | ||
import os | ||
import torch | ||
|
||
from dlio_benchmark.checkpointing.base_checkpointing import BaseCheckpointing | ||
from dlio_profiler.logger import fn_interceptor as Profile | ||
|
||
from dlio_benchmark.common.constants import MODULE_CHECKPOINT | ||
from dlio_benchmark.common.enumerations import CheckpointLocationType | ||
from dlio_benchmark.utils.utility import DLIOMPI | ||
|
||
dlp = Profile(MODULE_CHECKPOINT) | ||
|
||
|
||
class PyTorchCheckpointing(BaseCheckpointing): | ||
__instance = None | ||
|
||
@staticmethod | ||
def get_instance(): | ||
""" Static access method. """ | ||
if PyTorchCheckpointing.__instance is None: | ||
PyTorchCheckpointing.__instance = PyTorchCheckpointing() | ||
return PyTorchCheckpointing.__instance | ||
|
||
@dlp.log_init | ||
def __init__(self): | ||
super().__init__("pt") | ||
|
||
@dlp.log | ||
def get_tensor(self, size): | ||
return torch.randint(high=1, size=(size,), dtype=torch.int8) | ||
|
||
@dlp.log | ||
def save_state(self, suffix, state): | ||
name = self.get_name(suffix) | ||
with open(name, "wb") as f: | ||
torch.save(state, f) | ||
|
||
@dlp.log | ||
def checkpoint(self, epoch, step_number): | ||
super().checkpoint(epoch, step_number) | ||
|
||
@dlp.log | ||
def finalize(self): | ||
super().finalize() | ||
|
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,61 @@ | ||
""" | ||
Copyright (c) 2022, UChicago Argonne, LLC | ||
All Rights Reserved | ||
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. | ||
""" | ||
import os | ||
|
||
from dlio_benchmark.checkpointing.base_checkpointing import BaseCheckpointing | ||
from dlio_profiler.logger import fn_interceptor as Profile | ||
import tensorflow as tf | ||
|
||
from dlio_benchmark.common.constants import MODULE_CHECKPOINT | ||
from dlio_benchmark.common.enumerations import CheckpointLocationType | ||
from dlio_benchmark.utils.utility import DLIOMPI | ||
|
||
dlp = Profile(MODULE_CHECKPOINT) | ||
|
||
|
||
class TFCheckpointing(BaseCheckpointing): | ||
__instance = None | ||
|
||
@staticmethod | ||
def get_instance(): | ||
""" Static access method. """ | ||
if TFCheckpointing.__instance is None: | ||
TFCheckpointing.__instance = TFCheckpointing() | ||
return TFCheckpointing.__instance | ||
|
||
@dlp.log_init | ||
def __init__(self): | ||
super().__init__("pb") | ||
|
||
@dlp.log | ||
def get_tensor(self, size): | ||
return tf.random.uniform((int(size / 4),), maxval=100, dtype=tf.dtypes.int32) | ||
|
||
@dlp.log | ||
def save_state(self, suffix, state): | ||
name = self.get_name(suffix) | ||
checkpoint = tf.train.Checkpoint() | ||
checkpoint.mapped = state | ||
checkpoint.save(name) | ||
|
||
@dlp.log | ||
def checkpoint(self, epoch, step_number): | ||
super().checkpoint(epoch, step_number) | ||
|
||
@dlp.log | ||
def finalize(self): | ||
super().finalize() |
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
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
Oops, something went wrong.