| 1 |
# What are these scripts? |
| 2 |
|
| 3 |
All scripts in this folder originate from the `nlp_example.py` file, as it is a very simplistic NLP training example using Accelerate with zero extra features. |
| 4 |
|
| 5 |
From there, each further script adds in just **one** feature of Accelerate, showing how you can quickly modify your own scripts to implement these capabilities. |
| 6 |
|
| 7 |
A full example with all of these parts integrated together can be found in the `complete_nlp_example.py` script and `complete_cv_example.py` script. |
| 8 |
|
| 9 |
Adjustments to each script from the base `nlp_example.py` file can be found quickly by searching for "# New Code #" |
| 10 |
|
| 11 |
## Example Scripts by Feature and their Arguments |
| 12 |
|
| 13 |
### Base Example (`../nlp_example.py`) |
| 14 |
|
| 15 |
- Shows how to use `Accelerator` in an extremely simplistic PyTorch training loop |
| 16 |
- Arguments available: |
| 17 |
- `mixed_precision`, whether to use mixed precision. ("no", "fp16", or "bf16") |
| 18 |
- `cpu`, whether to train using only the CPU. (yes/no/1/0) |
| 19 |
|
| 20 |
All following scripts also accept these arguments in addition to their added ones. |
| 21 |
|
| 22 |
These arguments should be added at the end of any method for starting the python script (such as `python`, `accelerate launch`, `python -m torch.distributed.run`), such as: |
| 23 |
|
| 24 |
```bash |
| 25 |
accelerate launch ../nlp_example.py --mixed_precision fp16 --cpu 0 |
| 26 |
``` |
| 27 |
|
| 28 |
### Checkpointing and Resuming Training (`checkpointing.py`) |
| 29 |
|
| 30 |
- Shows how to use `Accelerator.save_state` and `Accelerator.load_state` to save or continue training |
| 31 |
- **It is assumed you are continuing off the same training script** |
| 32 |
- Arguments available: |
| 33 |
- `checkpointing_steps`, after how many steps the various states should be saved. ("epoch", 1, 2, ...) |
| 34 |
- `output_dir`, where saved state folders should be saved to, default is current working directory |
| 35 |
- `resume_from_checkpoint`, what checkpoint folder to resume from. ("epoch_0", "step_22", ...) |
| 36 |
|
| 37 |
These arguments should be added at the end of any method for starting the python script (such as `python`, `accelerate launch`, `python -m torchrun`), such as: |
| 38 |
|
| 39 |
(Note, `resume_from_checkpoint` assumes that we've ran the script for one epoch with the `--checkpointing_steps epoch` flag) |
| 40 |
|
| 41 |
```bash |
| 42 |
accelerate launch ./checkpointing.py --checkpointing_steps epoch output_dir "checkpointing_tutorial" --resume_from_checkpoint "checkpointing_tutorial/epoch_0" |
| 43 |
``` |
| 44 |
|
| 45 |
### Cross Validation (`cross_validation.py`) |
| 46 |
|
| 47 |
- Shows how to use `Accelerator.free_memory` and run cross validation efficiently with `datasets`. |
| 48 |
- Arguments available: |
| 49 |
- `num_folds`, the number of folds the training dataset should be split into. |
| 50 |
|
| 51 |
These arguments should be added at the end of any method for starting the python script (such as `python`, `accelerate launch`, `python -m torchrun`), such as: |
| 52 |
|
| 53 |
```bash |
| 54 |
accelerate launch ./cross_validation.py --num_folds 2 |
| 55 |
``` |
| 56 |
|
| 57 |
### Experiment Tracking (`tracking.py`) |
| 58 |
|
| 59 |
- Shows how to use `Accelerate.init_trackers` and `Accelerator.log` |
| 60 |
- Can be used with Weights and Biases, TensorBoard, or CometML. |
| 61 |
- Arguments available: |
| 62 |
- `with_tracking`, whether to load in all available experiment trackers from the environment. |
| 63 |
|
| 64 |
These arguments should be added at the end of any method for starting the python script (such as `python`, `accelerate launch`, `python -m torchrun`), such as: |
| 65 |
|
| 66 |
```bash |
| 67 |
accelerate launch ./tracking.py --with_tracking |
| 68 |
``` |
| 69 |
|
| 70 |
### Gradient Accumulation (`gradient_accumulation.py`) |
| 71 |
|
| 72 |
- Shows how to use `Accelerator.no_sync` to prevent gradient averaging in a distributed setup. |
| 73 |
- Arguments available: |
| 74 |
- `gradient_accumulation_steps`, the number of steps to perform before the gradients are accumulated and the optimizer and scheduler are stepped + zero_grad |
| 75 |
|
| 76 |
These arguments should be added at the end of any method for starting the python script (such as `python`, `accelerate launch`, `python -m torchrun`), such as: |
| 77 |
|
| 78 |
```bash |
| 79 |
accelerate launch ./gradient_accumulation.py --gradient_accumulation_steps 5 |
| 80 |
``` |
| 81 |
|
| 82 |
### LocalSGD (`local_sgd.py`) |
| 83 |
- Shows how to use `Accelerator.no_sync` to prevent gradient averaging in a distributed setup. However, unlike gradient accumulation, this method does not change the effective batch size. Local SGD can be combined with gradient accumulation. |
| 84 |
|
| 85 |
These arguments should be added at the end of any method for starting the python script (such as `python`, `accelerate launch`, `python -m torchrun`), such as: |
| 86 |
|
| 87 |
```bash |
| 88 |
accelerate launch ./local_sgd.py --local_sgd_steps 4 |
| 89 |
``` |
| 90 |
|
| 91 |
### DDP Communication Hook (`ddp_comm_hook.py`) |
| 92 |
|
| 93 |
- Shows how to use DDP Communication Hooks to control and optimize gradient communication across workers in a DistributedDataParallel setup. |
| 94 |
- Arguments available: |
| 95 |
- `ddp_comm_hook`, the type of DDP communication hook to use. Choose between `no`, `fp16`, `bf16`, `power_sgd`, and `batched_power_sgd`. |
| 96 |
|
| 97 |
These arguments should be added at the end of any method for starting the python script (such as `accelerate launch`, `python -m torch.distributed.run`), such as: |
| 98 |
|
| 99 |
```bash |
| 100 |
accelerate launch ./ddp_comm_hook.py --mixed_precision fp16 --ddp_comm_hook power_sgd |
| 101 |
``` |
| 102 |
|
| 103 |
### Profiler (`profiler.py`) |
| 104 |
|
| 105 |
- Shows how to use the profiling capabilities of `Accelerate` to profile PyTorch models during training. |
| 106 |
- Uses the `ProfileKwargs` handler to customize profiling options, including activities, scheduling, and additional profiling options. |
| 107 |
- Can generate and save profiling traces in JSON format for visualization in Chrome's tracing tool. |
| 108 |
|
| 109 |
Arguments available: |
| 110 |
- `--record_shapes`: If passed, records shapes for profiling. |
| 111 |
- `--profile_memory`: If passed, profiles memory usage. |
| 112 |
- `--with_stack`: If passed, profiles stack traces. |
| 113 |
- `--with_flops`: If passed, profiles floating point operations (FLOPS). |
| 114 |
- `--output_trace_dir`: If specified, saves the profiling trace to the given dir in JSON format. |
| 115 |
- `--cpu`: If passed, trains on the CPU instead of GPU. |
| 116 |
|
| 117 |
These arguments should be added at the end of any method for starting the Python script (such as `python`, `accelerate launch`, `python -m torchrun`), such as: |
| 118 |
|
| 119 |
```bash |
| 120 |
accelerate launch ./profiler.py --record_shapes --profile_memory --with_flops --output_trace_dir "profiler" |
| 121 |
``` |