Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[checkpointio] support asyncio for 3d #6144

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 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
80 changes: 58 additions & 22 deletions colossalai/checkpoint_io/general_checkpoint_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import torch.nn as nn
from torch.optim import Optimizer

from colossalai.utils.safetensors import move_and_save
from colossalai.utils.safetensors import load_flat

from .checkpoint_io_base import CheckpointIO
from .index_file import CheckpointIndexFile
from .utils import (
async_save_state_dict,
async_save_state_dict_shards,
create_pinned_state_dict,
get_model_base_filenames,
get_optimizer_base_filenames,
is_safetensors_available,
Expand Down Expand Up @@ -54,14 +54,16 @@ def save_unsharded_model(
pass

if use_async:
from tensornvme.async_file_io import AsyncFileWriter

writer = AsyncFileWriter(open(checkpoint, "wb"), self.N_WRITE_ENTRIES, backend="pthread")
if id(model) not in self.pinned_state_dicts:
self.pinned_state_dicts[id(model)] = create_pinned_state_dict(state_dict)
self.async_writers.append(writer)
move_and_save(writer, state_dict, self.pinned_state_dicts[id(model)])

pinned_state_dict = self.pinned_state_dicts.get(id(model), None)
new_pinned_state_dict, writers = async_save_state_dict(
state_dict,
checkpoint,
pinned_state_dict,
self.N_WRITE_ENTRIES,
shard_preprocess=False,
)
self.pinned_state_dicts[id(model)] = new_pinned_state_dict
self.async_writers.extend(writers)
else:
# save the checkpoint
save_state_dict(state_dict, checkpoint, use_safetensors)
Expand All @@ -86,7 +88,10 @@ def load_sharded_optimizer(self, optimizer: Optimizer, index_file_path: str, pre
checkpoint_files, _ = ckpt_index_file.get_checkpoint_filenames()

for shard_file in checkpoint_files:
state_dict = load_shard_state_dict(Path(shard_file), use_safetensors=False)
if shard_file.endswith(".safetensors"):
state_dict = load_flat(shard_file)
else:
state_dict = load_shard_state_dict(Path(shard_file), use_safetensors=False)
load_states_into_optimizer(optimizer, state_dict, id_map)

sharded_optimizer_loading_epilogue(optimizer)
Expand Down Expand Up @@ -119,7 +124,7 @@ def save_sharded_optimizer(
sharded_state = shard_optimizer_checkpoint(state_dict, max_shard_size=size_per_shard)

# Preparing file paths and index file.
states_name, save_index_file, param_group_file = get_optimizer_base_filenames(prefix)
states_name, save_index_file, param_group_file = get_optimizer_base_filenames(prefix, use_safetensors=use_async)
index_file = CheckpointIndexFile(checkpoint)

# Store the information of param groups to param_group_file.
Expand All @@ -129,14 +134,29 @@ def save_sharded_optimizer(

# Save shards of optimizer states.
# In general cases, is_master is set to True to get the right behavior.
total_size = save_state_dict_shards(
sharded_state_dict=sharded_state,
checkpoint=checkpoint,
index_file=index_file,
base_filename=states_name,
is_master=True,
use_safetensors=False,
)
if use_async:
pinned_state_dict = self.pinned_state_dicts.get(id(optimizer), None)
total_size, new_pinned_state_dict, writers = async_save_state_dict_shards(
sharded_state_dict=sharded_state,
checkpoint=checkpoint,
index_file=index_file,
base_filename=states_name,
is_master=True,
pinned_state_dict=pinned_state_dict,
n_write_entries=self.N_WRITE_ENTRIES,
shard_preprocess=True,
)
self.pinned_state_dicts[id(optimizer)] = new_pinned_state_dict
self.async_writers.extend(writers)
else:
total_size = save_state_dict_shards(
sharded_state_dict=sharded_state,
checkpoint=checkpoint,
index_file=index_file,
base_filename=states_name,
is_master=True,
use_safetensors=False,
)

# Wrap up index file.
index_file.append_meta_data("total_size", total_size)
Expand All @@ -148,7 +168,10 @@ def save_sharded_optimizer(
)

def load_unsharded_optimizer(self, optimizer: Optimizer, checkpoint: Path):
checkpoint = load_state_dict(checkpoint)
if checkpoint.endswith(".safetensors"):
checkpoint = load_flat(checkpoint)
else:
checkpoint = load_state_dict(checkpoint)
optimizer.load_state_dict(checkpoint)

def save_unsharded_optimizer(
Expand All @@ -159,7 +182,20 @@ def save_unsharded_optimizer(
use_async: bool = False,
):
# TODO(FrankLeeeee): handle distributed tensors
save_state_dict(optimizer.state_dict(), checkpoint, use_safetensors=False)
if use_async:
state_dict = optimizer.state_dict()
pinned_state_dict = self.pinned_state_dicts.get(id(optimizer), None)
new_pinned_state_dict, writers = async_save_state_dict(
state_dict,
checkpoint,
pinned_state_dict,
self.N_WRITE_ENTRIES,
shard_preprocess=True,
)
self.pinned_state_dicts[id(optimizer)] = new_pinned_state_dict
self.async_writers.extend(writers)
else:
save_state_dict(optimizer.state_dict(), checkpoint, use_safetensors=False)

def save_sharded_model(
self,
Expand Down
Loading
Loading