This repository was archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 455
Checkpoint removal 2 #250
Draft
tmarkovich
wants to merge
3
commits into
facebookresearch:main
Choose a base branch
from
tmarkovich:checkpoint-removal-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Checkpoint removal 2 #250
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -553,8 +553,9 @@ def train(self) -> None: | |
eval_stats_after, | ||
eval_stats_chunk_avg, | ||
) | ||
|
||
first = True | ||
for epoch_idx, edge_path_idx, edge_chunk_idx in iteration_manager: | ||
epoch_start = time.perf_counter() | ||
logger.info( | ||
f"Starting epoch {epoch_idx + 1} / {iteration_manager.num_epochs}, " | ||
f"edge path {edge_path_idx + 1} / {iteration_manager.num_edge_paths}, " | ||
|
@@ -600,8 +601,17 @@ def train(self) -> None: | |
bucket_logger = BucketLogger(logger, bucket=cur_b) | ||
self.bucket_logger = bucket_logger | ||
|
||
io_bytes = self._swap_partitioned_embeddings(old_b, cur_b, old_stats) | ||
io_bytes = 0 | ||
if first: | ||
start = time.perf_counter() | ||
io_bytes = self._swap_partitioned_embeddings(old_b, cur_b, old_stats) | ||
end = time.perf_counter() | ||
logger.debug(f"Loading embedings took {(end - start):.2f} seconds") | ||
first = False | ||
start = time.perf_counter() | ||
self.model.set_all_embeddings(holder, cur_b) | ||
end = time.perf_counter() | ||
logger.debug(f"Setting all embeddings took {(end - start):.2f} seconds") | ||
|
||
current_index = ( | ||
(iteration_manager.iteration_idx + 1) * total_buckets | ||
|
@@ -610,13 +620,16 @@ def train(self) -> None: | |
) | ||
|
||
bucket_logger.debug("Loading edges") | ||
start = time.perf_counter() | ||
edges = edge_storage.load_chunk_of_edges( | ||
cur_b.lhs, | ||
cur_b.rhs, | ||
edge_chunk_idx, | ||
iteration_manager.num_edge_chunks, | ||
shared=True, | ||
) | ||
end = time.perf_counter() | ||
logger.debug(f"Loading edges took {(end - start):.2f} seconds") | ||
num_edges = len(edges) | ||
|
||
# this might be off in the case of tensorlist or extra edge fields | ||
|
@@ -686,7 +699,9 @@ def train(self) -> None: | |
f"io: {io_time:.2f} s for {io_bytes:,} bytes ( {io_bytes / io_time / 1e6:.2f} MB/sec )" | ||
) | ||
|
||
self.model.clear_all_embeddings() | ||
if total_buckets > 1: | ||
logger.info("Clearing all embeddings") | ||
self.model.clear_all_embeddings() | ||
|
||
cur_stats = BucketStats( | ||
lhs_partition=cur_b.lhs, | ||
|
@@ -698,16 +713,32 @@ def train(self) -> None: | |
) | ||
|
||
# release the final bucket | ||
self._swap_partitioned_embeddings(cur_b, None, cur_stats) | ||
|
||
final: bool = (epoch_idx + 1 == iteration_manager.num_epochs) \ | ||
and (edge_path_idx + 1 == iteration_manager.num_edge_paths) \ | ||
and (edge_chunk_idx + 1 == iteration_manager.num_edge_chunks) | ||
to_write: bool = (final == True) or ((epoch_idx + 1) % 5 == 0 and edge_chunk_idx == 0) | ||
if to_write: | ||
logger.debug("Nondestructively writing the embeddings") | ||
start = time.perf_counter() | ||
self._nondestructive_write_embedding(cur_b) | ||
end = time.perf_counter() | ||
logger.debug(f"Writing embeddings took {(end - start):.2f} seconds") | ||
|
||
self._write_stats(cur_b, cur_stats) | ||
# self._swap_partitioned_embeddings(cur_b, None, cur_stats, to_write) | ||
|
||
# Distributed Processing: all machines can leave the barrier now. | ||
self._barrier() | ||
|
||
current_index = (iteration_manager.iteration_idx + 1) * total_buckets - 1 | ||
|
||
start = time.perf_counter() | ||
self._maybe_write_checkpoint( | ||
epoch_idx, edge_path_idx, edge_chunk_idx, current_index | ||
) | ||
end = time.perf_counter() | ||
logger.debug(f"Writing checkpoint took {(end - start):.2f} seconds") | ||
logger.debug(f"Epoch took {(end - start):.2f} seconds") | ||
|
||
# now we're sure that all partition files exist, | ||
# so be strict about loading them | ||
|
@@ -770,11 +801,35 @@ def _load_embeddings( | |
optimizer.load_state_dict(optim_state) | ||
return embs, optimizer | ||
|
||
def _write_single_embedding( | ||
self, | ||
holder: EmbeddingHolder, | ||
entity: EntityName, | ||
part: Partition): | ||
embs = holder.partitioned_embeddings[(entity, part)] | ||
optimizer = self.trainer.partitioned_optimizers[(entity, part)] | ||
self.checkpoint_manager.write( | ||
entity, part, embs.detach(), optimizer.state_dict() | ||
) | ||
|
||
def _nondestructive_write_embedding(self, bucket: Bucket): | ||
parts: Set[Tuple[EntityName, Partition]] = set() | ||
parts.update((e, bucket.lhs) for e in self.holder.lhs_partitioned_types) | ||
parts.update((e, bucket.rhs) for e in self.holder.rhs_partitioned_types) | ||
for entity, part in parts: | ||
self._write_single_embedding(self.holder, entity, part) | ||
|
||
def _write_stats(self, bucket: Optional[Bucket], stats: Optional[BucketStats]): | ||
if bucket is not None: | ||
if stats is not None: | ||
self.bucket_scheduler.release_bucket(bucket, stats) | ||
|
||
def _swap_partitioned_embeddings( | ||
self, | ||
old_b: Optional[Bucket], | ||
new_b: Optional[Bucket], | ||
old_stats: Optional[BucketStats], | ||
write: bool = True, | ||
) -> int: | ||
io_bytes = 0 | ||
logger.info(f"Swapping partitioned embeddings {old_b} {new_b}") | ||
|
@@ -797,19 +852,13 @@ def _swap_partitioned_embeddings( | |
logger.info("Saving partitioned embeddings to checkpoint") | ||
for entity, part in old_parts - new_parts: | ||
logger.debug(f"Saving ({entity} {part})") | ||
embs = holder.partitioned_embeddings.pop((entity, part)) | ||
optimizer = self.trainer.partitioned_optimizers.pop((entity, part)) | ||
self.checkpoint_manager.write( | ||
entity, part, embs.detach(), optimizer.state_dict() | ||
) | ||
self._write_single_embedding(holder, entity, part) | ||
self.embedding_storage_freelist[entity].add(embs.storage()) | ||
io_bytes += embs.numel() * embs.element_size() # ignore optim state | ||
# these variables are holding large objects; let them be freed | ||
del embs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do these lines work if you don't define |
||
del optimizer | ||
|
||
self.bucket_scheduler.release_bucket(old_b, old_stats) | ||
|
||
if new_b is not None: | ||
logger.info("Loading partitioned embeddings from checkpoint") | ||
for entity, part in new_parts - old_parts: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This naming is misleading... I think it does more than write stats in the distributed scheduler.