-
Notifications
You must be signed in to change notification settings - Fork 9
perf(core): Explicitly set PyTorch intra-op threads. #21
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bc9e8d3
feat(core): Explicitly set PyTorch intra-op threads.
Leahlijuan 6d08ad6
Set num_ranks default value.
Leahlijuan 5a0f9cc
Merge remote-tracking branch 'origin/main' into feature/torchthreads
Leahlijuan d2c72f3
Merge branch 'main' into feature/torchthreads
Leahlijuan c7c9559
Change "x or 1" to max(x, 1)
Leahlijuan ed8a83f
ci: add license header validation check (#27)
g-husam 136a687
docs(site): clarify perf points (#24)
g-husam f21e7a7
refactor(core): Move context recovery logic strictly to the NeMo laye…
Leahlijuan 5458ad2
Resolve comments.
Leahlijuan 3852fcb
Modify log
Leahlijuan 3181522
Merge remote-tracking branch 'origin/main' into feature/torchthreads
Leahlijuan 7564f52
Merge branch 'main' into feature/torchthreads
Leahlijuan ff20040
Merge branch 'main' into feature/torchthreads
Leahlijuan 58b6a4c
Merge branch 'main' into feature/torchthreads
Leahlijuan 5777559
resolve comments
Leahlijuan f17ccc6
Add tests for write_data when thread_count more than 1.
Leahlijuan ef09b5b
Apply suggestions from code review
Leahlijuan cdafb83
Merge branch 'main' into feature/torchthreads
Leahlijuan 2ccb07b
Format
Leahlijuan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -426,67 +426,80 @@ def write_data( | |
| thread_count: int = 1, | ||
| ) -> list[WriteResult]: | ||
| thread_count = max(thread_count, 1) | ||
| num_cpus = os.cpu_count() or 1 | ||
| num_ranks = torch.cuda.device_count() | ||
| torch_thread_count = max(1, num_cpus // 2 // num_ranks // thread_count) | ||
|
Leahlijuan marked this conversation as resolved.
Collaborator
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. as a future improvement, we can make the percentage of CPUs used configurable. here we are assuming 50% usage (by dividing by 2), but some scenarios may desire using more or less. |
||
| original_num_threads = torch.get_num_threads() | ||
| # Explicitly set PyTorch intra-op threads to optimize for performance. | ||
| # This also avoids potential runtime errors in tensor.copy_() with concurrent writers | ||
| torch.set_num_threads(torch_thread_count) | ||
| _LOGGER.debug( | ||
| "%s starting multi-threaded write_data with thread_count: %d", | ||
| self.__class__.__name__, | ||
| "original_num_threads: %d, thread_count: %d, num_cpus: %d, num_ranks: %d, torch_thread_count: %d", | ||
|
Leahlijuan marked this conversation as resolved.
Outdated
|
||
| original_num_threads, | ||
| thread_count, | ||
| num_cpus, | ||
| num_ranks, | ||
| torch_thread_count, | ||
| ) | ||
| try: | ||
| # Queue of ObjectWriteBuckets | ||
| object_items_queue: queue.Queue = queue.Queue() | ||
| for bucket in write_buckets: | ||
| object_items_queue.put(bucket) | ||
|
|
||
| # NOTE: There is support for multiple threads, to simplify modifying that setting, but we typically | ||
| # only use 1 thread. | ||
|
|
||
| results_from_threads: queue.Queue = queue.Queue() # Queue for tuple[List[WriteResult], Exception] | ||
| threads = [] | ||
|
|
||
| # Kick off additional threads to main thread, if any. | ||
| _LOGGER.debug("Spawning %d extra writer threads (in addition to the main thread).", thread_count - 1) | ||
| for i in range(1, thread_count): | ||
| thread = threading.Thread( | ||
| target=self._write_to_buffer_from_queue_worker, | ||
| args=(object_items_queue, results_from_threads, replicate_after_write, self._use_optimized_save), | ||
| name=f"{self.__class__.__name__}-Thread-{i}", | ||
| ) | ||
| threads.append(thread) | ||
| thread.start() | ||
|
|
||
| # Queue of ObjectWriteBuckets | ||
| object_items_queue: queue.Queue = queue.Queue() | ||
| for bucket in write_buckets: | ||
| object_items_queue.put(bucket) | ||
|
|
||
| # NOTE: There is support for multiple threads, to simplify modifying that setting, but we typically | ||
| # only use 1 thread. | ||
|
|
||
| results_from_threads: queue.Queue = queue.Queue() # Queue for tuple[List[WriteResult], Exception] | ||
| threads = [] | ||
|
|
||
| # Kick off additional threads to main thread, if any. | ||
| _LOGGER.debug("Spawning %d extra writer threads (in addition to the main thread).", thread_count - 1) | ||
| for i in range(1, thread_count): | ||
| thread = threading.Thread( | ||
| target=self._write_to_buffer_from_queue_worker, | ||
| args=(object_items_queue, results_from_threads, replicate_after_write, self._use_optimized_save), | ||
| name=f"{self.__class__.__name__}-Thread-{i}", | ||
| ) | ||
| threads.append(thread) | ||
| thread.start() | ||
|
|
||
| # Main thread execution. | ||
| self._write_to_buffer_from_queue_worker( | ||
| object_items_queue, results_from_threads, replicate_after_write, self._use_optimized_save | ||
| ) | ||
|
|
||
| for thread in threads: | ||
| thread.join() | ||
|
|
||
| all_results: list[WriteResult] = [] | ||
| exceptions_raised: list[Exception] = [] | ||
| # Collect all results, replication metadata, and exceptions | ||
| while not results_from_threads.empty(): | ||
| try: | ||
| results, exception = results_from_threads.get_nowait() | ||
| if exception: | ||
| exceptions_raised.append(exception) | ||
| elif results: | ||
| all_results.extend(results) | ||
| except queue.Empty: | ||
| break | ||
|
|
||
| if exceptions_raised: | ||
| _LOGGER.error( | ||
| "'%s' encountered %d error(s) during multi-threaded write (will propagate the first one):\n%s.", | ||
| self.__class__.__name__, | ||
| len(exceptions_raised), | ||
| exceptions_raised, | ||
| # Main thread execution. | ||
| self._write_to_buffer_from_queue_worker( | ||
| object_items_queue, results_from_threads, replicate_after_write, self._use_optimized_save | ||
| ) | ||
| # Propagate the first exception encountered. | ||
| # TODO: propagate some combined exception, then update log msg (for now they are all logged above at least) | ||
| raise exceptions_raised[0] | ||
|
|
||
| return all_results | ||
| for thread in threads: | ||
| thread.join() | ||
|
|
||
| all_results: list[WriteResult] = [] | ||
| exceptions_raised: list[Exception] = [] | ||
| # Collect all results, replication metadata, and exceptions | ||
| while not results_from_threads.empty(): | ||
| try: | ||
| results, exception = results_from_threads.get_nowait() | ||
| if exception: | ||
| exceptions_raised.append(exception) | ||
| elif results: | ||
| all_results.extend(results) | ||
| except queue.Empty: | ||
| break | ||
|
|
||
| if exceptions_raised: | ||
| _LOGGER.error( | ||
| "'%s' encountered %d error(s) during multi-threaded write (will propagate the first one):\n%s.", | ||
| self.__class__.__name__, | ||
| len(exceptions_raised), | ||
| exceptions_raised, | ||
| ) | ||
| # Propagate the first exception encountered. | ||
| # TODO: propagate some combined exception, then update log msg | ||
| # (for now they are all logged above at least) | ||
| raise exceptions_raised[0] | ||
|
|
||
| return all_results | ||
| finally: | ||
| torch.set_num_threads(original_num_threads) | ||
|
Leahlijuan marked this conversation as resolved.
|
||
|
|
||
| @log_execution_time(logger=_LOGGER, name="async_replicate_object") | ||
| def async_replicate_object(self, object_id: CheckpointObjectId) -> list[concurrent.futures.Future]: | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.