Skip to content

[CP] Rewrite ring attention backward algorithm and enablement APIs - #131351

Closed
fegin wants to merge 15 commits into
gh/fegin/272/basefrom
gh/fegin/272/head
Closed

fegin wants to merge 15 commits into
gh/fegin/272/basefrom
gh/fegin/272/head

Conversation

@fegin

@fegin fegin commented Jul 22, 2024 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

What does this PR achieve

  1. This PR rewrite ring attention backward algorithm to fuse the alltoall and overlap the gradient communication with computation.

  2. Enables memory efficient attention with CP by templating the ring attention backward to verify the accuracy as fp32 gives us higher confident about the implementation correctness.

  3. Provides some experimental APIs to enable context parallelism.

  4. Ensures CP work with torch.compiler. The combination of causal masking and torch.compiler has not
    yet worked.

cc @XilunWu @H-Huang @awgu @kwen2501 @wanchaol @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 22, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/131351

Note: Links to docs will display an error until the docs builds have been completed.

❌ 4 New Failures

As of commit 1981b5b with merge base dfc5bb0 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/inductor oncall: distributed Add this issue/PR to distributed oncall triage queue labels Jul 22, 2024
@fegin
fegin marked this pull request as draft July 22, 2024 18:11
[ghstack-poisoned]
[ghstack-poisoned]
@fegin fegin added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 30, 2024
fegin added 2 commits July 30, 2024 15:24
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
fegin added 3 commits August 1, 2024 18:16
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin marked this pull request as ready for review August 5, 2024 18:33

@wanchaol wanchaol left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Have some comments and suggestions inlined

Comment thread torch/distributed/distributed_c10d.py Resolved
[ghstack-poisoned]
fegin added 2 commits August 14, 2024 22:26
[ghstack-poisoned]
[ghstack-poisoned]
@fegin

fegin commented Aug 15, 2024

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f " The failing tests are not related"

@fegin

fegin commented Aug 15, 2024

Copy link
Copy Markdown
Contributor Author

Add a TODO to track the propagation rule issue.

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions
github-actions Bot deleted the gh/fegin/272/head branch September 17, 2024 01:55
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 21, 2024
**Summary** (WIP)
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

TODO: 
1. Add seq_len scalability, loss convergence, and WPS performance benchmark.
2. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 21, 2024
**Summary** (WIP)
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

TODO: 
1. Add seq_len scalability, loss convergence, and WPS performance benchmark.
2. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*



3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*



3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*



3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*



3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*



3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*



3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image" src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to our pick of the number of steps and picking an appropriate number of warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image" src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to our pick of the number of steps and picking an appropriate number of warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image" src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to our pick of the number of steps and picking an appropriate number of warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image" src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to our pick of the number of steps and picking an appropriate number of warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image" src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to our pick of the number of steps and picking an appropriate number of warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 22, 2024
**Summary**
This PR adds DTensor Context Parallel (pytorch/pytorch#131351) option to torchtitan. 

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section in `.toml` file, assign the desired Context Parallel degree (i.e. the # of GPUs along the Context Parallel dimension). 

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the CP dimension allows longer input seq_len and this growth is linearly scalable. We use FSDP to shard the model and fix `data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs (i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len w/o GPU OOM. We adjust the CP degree by modifying the `context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image" src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image" src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it also decreases the WPS on each device. This is the price paid for longer input*

<img width="593" alt="image" src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by showing the loss curve converges just as FSDP + TP. This experiment uses 64 H100 GPUs and fix TP degree and local batch size to 8 (`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree, we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image" src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM" src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to our pick of the number of steps and picking an appropriate number of warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to choose and what WPS to expect on a specific set of devices (e.g. number of devices, device types, etc). Here we use H100 as an example but it is expected to observer similar trade-off curves on other hardware. 

<img width="830" alt="image" src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of reducing WPS on each device. The exception is pure CP -- the seq_len increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due to our implementation, and we're working on enabling performant CP to support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and all-gather will be added once pytorch/pytorch#132820 is landed. 
2. Investigate the limitation in pure CP, to enable ultra-long input sequence.



[ghstack-poisoned]
XilunWu added a commit to pytorch/torchtitan that referenced this pull request Oct 23, 2024
Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at
bottom):
* __->__ #592

**Summary**
This PR adds DTensor Context Parallel
(pytorch/pytorch#131351) option to torchtitan.

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section
in `.toml` file, assign the desired Context Parallel degree (i.e. the #
of GPUs along the Context Parallel dimension).

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as
below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the
longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the
CP dimension allows longer input seq_len and this growth is linearly
scalable. We use FSDP to shard the model and fix
`data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs
(i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len
w/o GPU OOM. We adjust the CP degree by modifying the
`context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image"
src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also
increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image"
src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it
also decreases the WPS on each device. This is the price paid for longer
input*

<img width="593" alt="image"
src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by
showing the loss curve converges just as FSDP + TP. This experiment uses
64 H100 GPUs and fix TP degree and local batch size to 8
(`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree,
we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also
make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image"
src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of
parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM"
src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to
our pick of the number of steps and picking an appropriate number of
warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to
choose and what WPS to expect on a specific set of devices (e.g. number
of devices, device types, etc). Here we use H100 as an example but it is
expected to observer similar trade-off curves on other hardware.

<img width="830" alt="image"
src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of
reducing WPS on each device. The exception is pure CP -- the seq_len
increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due
to our implementation, and we're working on enabling performant CP to
support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and
all-gather will be added once
pytorch/pytorch#132820 is landed.
2. Investigate the limitation in pure CP, to enable ultra-long input
sequence.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at
bottom):
* __->__ pytorch#592

**Summary**
This PR adds DTensor Context Parallel
(pytorch/pytorch#131351) option to torchtitan.

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section
in `.toml` file, assign the desired Context Parallel degree (i.e. the #
of GPUs along the Context Parallel dimension).

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as
below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the
longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the
CP dimension allows longer input seq_len and this growth is linearly
scalable. We use FSDP to shard the model and fix
`data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs
(i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len
w/o GPU OOM. We adjust the CP degree by modifying the
`context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image"
src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also
increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image"
src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it
also decreases the WPS on each device. This is the price paid for longer
input*

<img width="593" alt="image"
src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by
showing the loss curve converges just as FSDP + TP. This experiment uses
64 H100 GPUs and fix TP degree and local batch size to 8
(`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree,
we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also
make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image"
src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of
parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM"
src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to
our pick of the number of steps and picking an appropriate number of
warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to
choose and what WPS to expect on a specific set of devices (e.g. number
of devices, device types, etc). Here we use H100 as an example but it is
expected to observer similar trade-off curves on other hardware.

<img width="830" alt="image"
src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of
reducing WPS on each device. The exception is pure CP -- the seq_len
increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due
to our implementation, and we're working on enabling performant CP to
support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and
all-gather will be added once
pytorch/pytorch#132820 is landed.
2. Investigate the limitation in pure CP, to enable ultra-long input
sequence.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at
bottom):
* __->__ pytorch#592

**Summary**
This PR adds DTensor Context Parallel
(pytorch/pytorch#131351) option to torchtitan.

**Instructions**
Find the `context_parallel_degree` option in the `experimental` section
in `.toml` file, assign the desired Context Parallel degree (i.e. the #
of GPUs along the Context Parallel dimension).

**Benchmark Settings**
The benchmark uses `train_configs/llama3_8b.toml` with extra changes as
below:

- norm_type = "fused_rmsnorm"
- mode = 'full' (using full activation checkpoint allows us to find the
longest possible input sequence length on a device)

**Benchmark Results**
1. *max seq_len scalability*
The first set of benchmark shows that with CP, adding more GPUs on the
CP dimension allows longer input seq_len and this growth is linearly
scalable. We use FSDP to shard the model and fix
`data_parallel_shard_degree` to be 8, and we use 8, 16, 32, 64 H100 GPUs
(i.e. CP degree=1,2,4,8) to measure the longest possible input seq_len
w/o GPU OOM. We adjust the CP degree by modifying the
`context_parallel_degree` in the `experimental` section in `.toml` file.
<img width="600" alt="image"
src="https://github.com/user-attachments/assets/03f68062-3368-4ea5-a324-9d2346806af3">

*As Context Parallel degrees goes up (1, 2, 4, 8), the max seq_len also
increases (32k, 80k, 144k, 300k)*

<img width="597" alt="image"
src="https://github.com/user-attachments/assets/b48078d3-ab4d-40dc-97cf-2d477b713e00">

*While increasing Context Parallel degree increases the max seq_len, it
also decreases the WPS on each device. This is the price paid for longer
input*

<img width="593" alt="image"
src="https://github.com/user-attachments/assets/edef69ef-f0f0-41ec-9114-f7c420929064">

*However, increasing CP degree doesn't significantly affect MFU.*

2. *loss curve convergence*
The second set of benchmark proves that our implementation is correct by
showing the loss curve converges just as FSDP + TP. This experiment uses
64 H100 GPUs and fix TP degree and local batch size to 8
(`tensor_parallel_degree=8`, `batch_size=8`). As we increase CP degree,
we decrease DP degree to make sure that `CP * DP = 8`. Besides, we also
make sure `seq_len` increases such that `seq_len = 8192 * CP`.

<img width="1061" alt="image"
src="https://github.com/user-attachments/assets/8a25e0b1-cbd4-46d5-a444-8dd6f5277227">

*The training loss curve matches among the 4 combination of
parallelisms: DP=8; DP=4, CP=2; DP=2, CP=4; CP=8 (TP is fixed to 8).*

<img width="2292" alt="Screenshot 2024-10-07 at 12 42 37 PM"
src="https://github.com/user-attachments/assets/74a62cc8-562b-4441-babf-d601ec9bf375">

*The only observed difference is among the warm-up steps. This is due to
our pick of the number of steps and picking an appropriate number of
warm-up steps should smooth out this.*

3. *max seq_len vs. local WPS on a fixed number of GPUs*
The third set of benchmark gives users a guide on what CP degree to
choose and what WPS to expect on a specific set of devices (e.g. number
of devices, device types, etc). Here we use H100 as an example but it is
expected to observer similar trade-off curves on other hardware.

<img width="830" alt="image"
src="https://github.com/user-attachments/assets/b1ea2320-e0ae-439f-9434-4194bd4932a0">

*Increasing CP degree allows longer input sequence length at the cost of
reducing WPS on each device. The exception is pure CP -- the seq_len
increase is very limited compared to DP=2, CP=4 on 8 GPUs. This is due
to our implementation, and we're working on enabling performant CP to
support ultra-long input sequence.*

TODO: 
1. An option for CP tensor rotate implementation between all-to-all and
all-gather will be added once
pytorch/pytorch#132820 is landed.
2. Investigate the limitation in pure CP, to enable ultra-long input
sequence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (c10d) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants