Debian Codegraph @

benchmarks/fsdp2/README.md

raw
1 # FSDP2 Benchmarks
2
3 This benchmark showcases `FSDP2` in 🤗 `accelerate` and compares it to `torch` baseline.
4
5 ## Overview
6
7 This benchmark consists of two parts:
8 - `main.py` is the main script that runs the benchmark
9 - `visualize.py` is the script that visualizes the results (if `--output_dir` was specified for the previous command)
10
11 ## Motivation
12
13 We want to showcase that 🤗 `accelerate`'s integration of `FSDP2` is on par raw PyTorch, and highlight a "broken" part in PyTorch that creating an optimizer before applying `FSDP2` **doesn't result in a working training loop**. (more on this later)
14 This script showcases **matching memory usage and convergence between `accelerate` and `torch`'s baseline.**
15 To deal with this breaking change (and maintain backward compatibility with FSDP1 in terms of an API), `accelerate` had to come up with a workaround since `accelerate` assumes that the user will nearly always create a model, optimizer, scheduler, etc beforehand and bring them themselves. This lead to an issue of a stark increase in memory as well as the model not even training if the user creates an optimizer beforehand.
16 To workaround this, we replace the parameters inside the optimizer with the newly created FSDP2 sharded ones. More about this can be found in this [blog post (TBD)](TODO)
17 > [!WARNING]
18 > This script is intended to fit on 2x 24GB GPUs, though on so few GPUs it's not possible to see the memory difference (discrepancies in grad allocation result in lower memory usage in the non-fixed case), only the difference in convergence. Below are attached results from 8x H100 GPUs where the difference is visible.
19 > TLDR: more GPUs = bigger memory difference between fixed and non-fixed cases.
20
21 ## Results
22
23 Here are the results from running the benchmark on 8x H100 GPUs:
24
25 <p align="center">
26 <img src="imgs/allocated_memory.png" width="80%" alt="Allocated Memory Usage">
27 </p>
28 <p align="center">
29 <img src="imgs/reserved_memory.png" width="80%" alt="Reserved Memory Usage">
30 </p>
31
32 As you can see, the memory usage of `accelerate` and `torch_post_shard` (the **intended** way) are very similar, while `torch_pre_shard_not_fixed` uses significantly more memory. Our fix in `torch_pre_shard_fixed` brings the memory usage back in line with the **intended** approach.
33
34 > [!WARNING]
35 > Timing discrepancies are due to the benchmarks being ran in 1 script.
36
37
38 ## Running
39
40 To run the benchmark, you can either use `accelerate launch` or `torchrun`:
41 ```bash
42 accelerate launch main.py
43 ```
44 ```bash
45 # For two GPUs
46 torchrun --nproc_per_node 2 main.py
47 ```
48
49 This supports multiple configurable options, you can learn about them by running:
50 ```bash
51 python3 main.py --help
52 ```
53
54 This script will run 4 different benchmarks:
55 - `torch_optimizer_after_fsdp`: `torch` baseline where optimizer is created after applying `FSDP2`, this is the **intended** way to do it
56 - `torch_optimizer_before_fsdp_not_fixed`: `torch` baseline where optimizer is created before applying `FSDP2` without fixing the optimizer parameters
57 - `torch_optimizer_before_fsdp_fixed`: `torch` baseline where optimizer is created before applying `FSDP2` with our fix to the optimizer
58 - `accelerate`: `accelerate`'s own integration of `FSDP2` where optimizer is created before applying `FSDP2`, but we apply our fix to the optimizer
59
60 Memory results are saved in a folder specified by `--output_dir` argument.
61 Optionally, you can specify `--save_memory_snapshot` to save the torch memory snapshot, which can then be viewed using [`torch memory viz`](https://pytorch.org/memory_viz)
62
63 ## Visualizing results
64
65 To visualize the results, you can run:
66
67 ```bash
68 python3 visualize.py --dir <path_to_output_dir>
69 ```
70
71 This will then create two plots, showcasing allocated and reserved memory usage between all the different benchmarks discussed above.
72
73
74