Skip to content

Conversation

ravi-mosaicml
Copy link
Contributor

@ravi-mosaicml ravi-mosaicml commented Nov 20, 2021

The Training Loop Profiler gives a breakdown of how long is spent on each part of the training loop, and how long is spent on each algorithm and callback for each event. It is implemented through the engine and wraps calls around each event. It also can profile system performance metrics through psutil and incorporates the pytorch profiler.

TODO:

Examples of the visualization

image
image

Closes #11

This PR helps clean up some of the tests, rank zero callbacks, and will be used by future profiling work.
#65 made the global rank available in the process start, so it is no longer necessarry to wait until training_start() to create the dataloader. Instead, dataloaders are now initialized in __init__.

This change will help with dataloader profiling, as now the dataloader will be immediately bound to the state.
Added uploading of the run directory to various cloud providers via a callback. Depends on the LibCloud plugin.

Closes #98. Depends on #85 and (for tests) #92.
2. Renamed the `MosaicProfiler` to `Profiler`
The pytorch profiler uses `os.getpid()` for the thread id. Updating the training loop profiler to be consistent so the events will interleave.

Updated the merge script to replace the PID with the global rank. This ensures that GPU streams will show up under the correct rank, since pytorch by default uses the local GPU rank as the PID. This change also ensures that traces will merge properly across nodes where PIDs could conflict.
@bandish-shah bandish-shah requested a review from dskhudia January 10, 2022 22:37
@bandish-shah
Copy link
Contributor

bandish-shah commented Jan 11, 2022

Tried running as follows with the torch profiler disabled:
python3 -m composer.cli.launcher -n 1 examples/run_mosaic_profiler.py -f composer/yamls/models/classify_mnist.yaml --profiler true --profiler.profilers dataloader system --datadir ~/datasets

Getting the following error:

Epoch 1: 100%|██████████| 9/9 [00:00<00:00, 10.57it/s, loss/train=1.8302]
Epoch 2: 100%|██████████| 9/9 [00:00<00:00, 18.80it/s, loss/train=0.6712]
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/pin_memory.py", line 28, in _pin_memory_loop
r = in_queue.get(timeout=MP_STATUS_CHECK_INTERVAL)
File "/usr/lib/python3.8/multiprocessing/queues.py", line 116, in get
return _ForkingPickler.loads(res)
File "/usr/local/lib/python3.8/dist-packages/torch/multiprocessing/reductions.py", line 289, in rebuild_storage_fd
fd = df.detach()
File "/usr/lib/python3.8/multiprocessing/resource_sharer.py", line 57, in detach
with _resource_sharer.get_connection(self._id) as conn:
File "/usr/lib/python3.8/multiprocessing/resource_sharer.py", line 87, in get_connection
c = Client(address, authkey=process.current_process().authkey)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 508, in Client
answer_challenge(c, authkey)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 752, in answer_challenge
message = connection.recv_bytes(256) # reject large message
File "/usr/lib/python3.8/multiprocessing/connection.py", line 216, in recv_bytes
buf = self._recv_bytes(maxlength)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 414, in _recv_bytes
buf = self._recv(4)
File "/usr/lib/python3.8/multiprocessing/connection.py", line 379, in _recv
chunk = read(handle, remaining)
ConnectionResetError: [Errno 104] Connection reset by peer
Waiting up to 30 seconds for all training processes to terminate...`

Works fine if the torch profiler is enabled.

@ravi-mosaicml
Copy link
Contributor Author

Confirmed that the dataloader error @bandish-shah saw is an issue with the Pytorch dataloader. I suspect that we hit it with the profiler as we aren't spinning the dataloaders as long. See pytorch/pytorch#1551 (comment).

Copy link
Contributor

@bandish-shah bandish-shah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for addressing the various issues and debugging the dataloader error!

@hanlint hanlint merged commit de39bf6 into dev Jan 19, 2022
@hanlint hanlint deleted the ravi/instrumentation_point branch January 19, 2022 23:05
coryMosaicML pushed a commit to coryMosaicML/composer that referenced this pull request Feb 23, 2022
…aicml#105)

Before mosaicml#65, composer.trainer.ddp ensured that DDP functionality was accessed only after ddp was initialized. Now, DDP is available from process start, so this class is no longer needed. Moved all the functionality from this class to the global composer.utils.ddp.

This change allows callbacks, algorithms, etc... to use DDP (such as barriers and reductions) as needed. mosaicml#97 and mosaicml#101 depend on this functionality.

Also removed DDP from the state, as that is available globally.
coryMosaicML pushed a commit to coryMosaicML/composer that referenced this pull request Feb 23, 2022
* Added `run_event` to callback

Closes #11

This PR helps clean up some of the tests, rank zero callbacks, and will be used by future profiling work.

* Removed callback helper methods

* Fixed tests

* Formatting

* Addressed PR feedback

* Fixed tests

* Formatting

* Fixed _run_event

* Formatting

* Removed ip

* Instrumentation WIP

* Stash

* Create dataloader on trainer __init__()

mosaicml#65 made the global rank available in the process start, so it is no longer necessarry to wait until training_start() to create the dataloader. Instead, dataloaders are now initialized in __init__.

This change will help with dataloader profiling, as now the dataloader will be immediately bound to the state.

* Stash

* Added JSON trace handler

* Formatting

* Fixed trace generation

* Prettified memory

* Fixed setup.py

* Changed setup.py

* testing

* Removed prepare

* Run Directory Uploader

Added uploading of the run directory to various cloud providers via a callback. Depends on the LibCloud plugin.

Closes mosaicml#98. Depends on mosaicml#85 and (for tests) mosaicml#92.

* Supporting both styles for callbacks
Removed deferred logging since rank is now known at the init event

* Minimizing Diff

* Fixed tests

* Added fasteners

* Fixed tests

* Formatting

* Lazy population of kwargs

* 1. Added object_name_prefix
2. Tested on google cloud storage
3. Added exponential backoff and retrying for transient errors

* Addressed PR feedback

* Remove the composer.trainer.ddp class

Before mosaicml#65, composer.trainer.ddp ensured that DDP functionality was accessed only after ddp was initialized. Now, DDP is available from process start, so this class is no longer needed. Moved all the functionality from this class to the global composer.utils.ddp.

This change allows callbacks, algroithms, etc... to use DDP (such as barriers and reductions) as needed. mosaicml#97 and mosaicml#101 depend on this functionality.

Also removed DDP from the state, as that is available globally.

* Added in DDP barrier

* Fixed tests

* Update composer/utils/ddp.py

* Update composer/utils/ddp.py

* Switched tqdm to using callback hooks
Added test case for TQDM

* Fixed pyright

* Fixed DDP barriers

* Increased timeout for run directory uploader

* Switched callback format for run directory uploader

* Replaced `atexit` with cleanup methods

When running the trainer multiple times, such as in interactive enviroments, `atexit` does not fire. Instead, replaced it with `.close()` and `.post_close()` hooks on callbacks.

`.close()` can be used to write and flush files. `.post_close()` can be used to backup the run directory and capture any changes that may have been made on `.close()`

* Uncommented code

* Running callbacks befor algorithms for the INIT event in the engine

* For the INIT event, run the callbacks first to initialize the loggers.
* For other events, run the algorithms first, so the callbacks have the state  after algorithms modify it.

* Fixed tests

* Addressed PR feedback

* Added in the scheduler

* Added instant events

* Fixes

* Fixed profile scheduling

* Added decorator option

* Formatting

* Added documentation for the profiler

* 1. Added test cases
2. Fixed trace files to be proper json on successful training runs

* Profiler entry point

* Ravi/instrumentation point (mosaicml#140)

1. Using `os.getpid()` for process IDs to enable synchronization with the pytorch profiler
2. Switched to using object format instead of array format for the traces
3. Added in extra metadata such as global rank and timestamps for clock syncing

* Writing metadata to a seperate file

* Fixed tests

* Removed the perf counter

* Recording IO stats

* Log global rank in each torch profiler file

* Merging process traces (mosaicml#144)

* Refactor the system profiler and dataloader profiler into callbacks
Configuring the pytorch profiler based off of the mosaic profiler hparams

* 1. Updated the merge script to merge pytorch trace files
2. Renamed the `MosaicProfiler` to `Profiler`

* Increased timeout

* Formatting

* Fixed the `run_mosaic_profiler`

* Added detailed option

* Added sort index

* Setting `pid` to global rank and `tid` to `os.getpid()`

The pytorch profiler uses `os.getpid()` for the thread id. Updating the training loop profiler to be consistent so the events will interleave.

Updated the merge script to replace the PID with the global rank. This ensures that GPU streams will show up under the correct rank, since pytorch by default uses the local GPU rank as the PID. This change also ensures that traces will merge properly across nodes where PIDs could conflict.

* Simplifying diff

* Put the backwards thread second

* Thread sorting in trace

* Fix

* Fixes

* Fixed tests

* Fixed the profiler

* Fixes

Co-authored-by: Jamie Bloxham <[email protected]>
Co-authored-by: Bandish Shah <[email protected]>
Co-authored-by: anisehsani <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants