Skip to content

[CP] Implement AllGather based context parallelism - #132820

Closed
fegin wants to merge 13 commits into
gh/fegin/281/basefrom
gh/fegin/281/head
Closed

fegin wants to merge 13 commits into
gh/fegin/281/basefrom
gh/fegin/281/head

Conversation

@fegin

@fegin fegin commented Aug 6, 2024 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary:

This implementation does not utilize the benefit that after allgather we can directly perform the SDPA without doing the ring-based SDPA, but we can overlap the communication with the first sharded kv computation. This implementation shows some performance benefit and memory saving compared to the original alltoall implementation in certain cases.

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 6, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 5 New Failures, 1 Unrelated Failure

As of commit 86b9bf9 with merge base 4abe38b (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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 Aug 6, 2024
fegin added a commit that referenced this pull request Aug 6, 2024
Summary:
As title

ghstack-source-id: ac4ceee
Pull Request resolved: #132820
@fegin
fegin marked this pull request as draft August 6, 2024 23:26
@fegin fegin added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 7, 2024
[ghstack-poisoned]
fegin added a commit that referenced this pull request Aug 7, 2024
Summary:
This implementation does not utitlize the benefit that after allgather
we can directly perform the SDPA without doing the ring-based SDPA

ghstack-source-id: 5242dc8
Pull Request resolved: #132820
@fegin fegin changed the title [CP] Implement AllGather based ring attention [CP] Implement AllGather based context parallelism Aug 7, 2024
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Aug 9, 2024
Summary:
This implementation does not utitlize the benefit that after allgather
we can directly perform the SDPA without doing the ring-based SDPA

ghstack-source-id: 294dec4
Pull Request resolved: #132820
@fegin
fegin marked this pull request as ready for review August 12, 2024 18:11
@fegin
fegin requested review from drisspg, wanchaol and yifuwang August 12, 2024 18:11
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Aug 15, 2024
Summary:
This implementation does not utitlize the benefit that after allgather
we can directly perform the SDPA without doing the ring-based SDPA

ghstack-source-id: b9efe84
Pull Request resolved: #132820
[ghstack-poisoned]
@fegin fegin added the release notes: distributed (dtensor) release notes category label Oct 18, 2024
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]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 21, 2024
Summary:
This implementation does not utitlize the benefit that after allgather
we can directly perform the SDPA without doing the ring-based SDPA

ghstack-source-id: a56d321
Pull Request resolved: #132820
@fegin

fegin commented Oct 22, 2024

Copy link
Copy Markdown
Contributor Author

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

@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

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.
SamGinzburg pushed a commit that referenced this pull request Oct 28, 2024
Summary:

This implementation does not utilize the benefit that after allgather we can directly perform the SDPA without doing the ring-based SDPA, but we can overlap the communication with the first sharded kv computation. This implementation shows some performance benefit and memory saving compared to the original alltoall implementation in certain cases.

Pull Request resolved: #132820
Approved by: https://github.com/XilunWu
@github-actions
github-actions Bot deleted the gh/fegin/281/head branch November 22, 2024 02:08
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 (dtensor) release notes category topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants