perf(adapter/pytorch): offload spawn manager creation to background thread (threading.Thread)#73
Conversation
…hread Why this change is necessary: 1. Using 'spawn' context is required to prevent CUDA OOM during NVRX restarts, but it introduces a significant ~15s delay. 2. The previous implementation created the Manager synchronously, blocking the main trainer setup. Change-Id: I9cdf24a9da40fa19d5b93d5ef9ad7c6f42641b1b
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the performance of the trainer setup by converting the synchronous creation of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a performance improvement by offloading the creation of the torch.multiprocessing.Manager to a background thread. This avoids blocking the main trainer setup, which previously caused a significant delay. The implementation uses a concurrent.futures.Future to manage the asynchronous creation of the manager, and the MemoryStorageWriter is updated to lazily initialize its shared state by waiting on this future. The changes are logical and well-implemented. I have a couple of minor suggestions to improve code clarity and formatting.
| if self._write_events_per_checkpoint_id is None and self._main_process_torchmp_manager_future is not None: | ||
| self._write_events_per_checkpoint_id = self._main_process_torchmp_manager_future.result().dict() | ||
|
|
||
| if self._write_results_per_checkpoint_id is None and self._main_process_torchmp_manager_future is not None: | ||
| self._write_results_per_checkpoint_id = self._main_process_torchmp_manager_future.result().dict() |
There was a problem hiding this comment.
These two if statements can be combined. Since _write_events_per_checkpoint_id and _write_results_per_checkpoint_id are initialized together in __init__ and this is the only place they are assigned from None, you can check for one and initialize both. This also avoids calling _main_process_torchmp_manager_future.result() twice, making the code cleaner and slightly more efficient.
| if self._write_events_per_checkpoint_id is None and self._main_process_torchmp_manager_future is not None: | |
| self._write_events_per_checkpoint_id = self._main_process_torchmp_manager_future.result().dict() | |
| if self._write_results_per_checkpoint_id is None and self._main_process_torchmp_manager_future is not None: | |
| self._write_results_per_checkpoint_id = self._main_process_torchmp_manager_future.result().dict() | |
| if self._write_events_per_checkpoint_id is None and self._main_process_torchmp_manager_future is not None: | |
| mp_manager = self._main_process_torchmp_manager_future.result() | |
| self._write_events_per_checkpoint_id = mp_manager.dict() | |
| self._write_results_per_checkpoint_id = mp_manager.dict() |
Change-Id: I10b64985354fb808873fc3a1578e0cba9e069bb8
Change-Id: I178993a77f184a355aeae0df99f306cab18eeb49
| mp_manager_future.set_result(ctx.Manager()) | ||
|
|
||
| thread = threading.Thread(target=start_manager, daemon=True) | ||
| thread.start() |
There was a problem hiding this comment.
doesnt this approach cause errors?
There was a problem hiding this comment.
On errors on our side
Change-Id: I6a2b9b4f3730eacb3cf1d75e5dcf3659f5bf0c4e
| state = self.__dict__.copy() | ||
| if "_main_process_torchmp_manager" in state: | ||
| del state["_main_process_torchmp_manager"] | ||
| if "_main_process_torchmp_manager_future" in state: |
There was a problem hiding this comment.
nit: a one-liner alternative to the if-then-del is
state.pop("_main_process_torchmp_manager_future", None)
| # Mimicking existing StorageWriter impls (e.g. `_FileSystemWriter`) by using a random ID as the save ID. | ||
| self._current_save_id = generate_hfid("memwritersave") | ||
|
|
||
| if self._write_events_per_checkpoint_id is None and self._main_process_torchmp_manager_future is not None: |
There was a problem hiding this comment.
make sure there's a test for the diff conditions here: where each condition is T/F (4 scenarios)
| # Manually initialize the event for checkpoint_id1, as prepare_write_data_buckets is not called | ||
| writer._write_events_per_checkpoint_id[checkpoint_id1] = writer._main_process_torchmp_manager.Event() | ||
| writer._write_events_per_checkpoint_id[checkpoint_id1] = ( | ||
| writer._main_process_torchmp_manager_future.result().Event() |
There was a problem hiding this comment.
style nit: a cleaner way to access could be to add a property that automatically waits for the future (keep it private tho):
@property
def _main_process_torchmp_manager(self):
"""Returns the torch multiprocessing Manager from the Future, blocking if necessary."""
return self._main_process_torchmp_manager_future.result()
then you can access it the same way you were before, and wouldn't have to modify these lines : writer._main_process_torchmp_manager.Event()
There was a problem hiding this comment.
Will open a new PR for this, as the updates affect multiple areas of the codebase and tests.
Change-Id: I1ed36f191e66ba16107f976ecc3a54ae07e8975e
Python Code Coverage Summary
Minimum allowed line rate is |
C++ Code Coverage Summary
Minimum allowed line rate is |
Why this change is necessary:
Change-Id: I9cdf24a9da40fa19d5b93d5ef9ad7c6f42641b1b
Fixes #<issue_number_goes_here>