Skip to content

[cp] Allow PTRR load balancer to select a mask from a dict - #3972

Merged
fegin merged 2 commits into
pytorch:mainfrom
jinsooihm:ptrr-dict-attention-masks
Jul 24, 2026
Merged

fegin merged 2 commits into
pytorch:mainfrom
jinsooihm:ptrr-dict-attention-masks

Conversation

@jinsooihm

@jinsooihm jinsooihm commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Context parallelism's PTRR load balancer previously rejected any model whose attention masks are provided as a dict of named masks, since the balancer can only be built from a single mask. Models like GPT-OSS or Olmo3 (#3966) with flex attention legitimately produce multiple masks (e.g. a full-causal mask plus a sliding-window mask), so PTRR was unusable for them.

This adds a configuration option letting the user name which mask in the dict PTRR should build its balancer from. The resulting balancer is then used to shard every mask in the dict as well as the inputs. When the masks are a dict but no key is specified (or the key is invalid), we fail loudly and list the available keys instead of silently guessing.

For example,

--parallelism.context-parallel-load-balancer ptrr --parallelism.context-parallel-ptrr-mask-key basic_mask

would allow the user to choose attention_masks["basic_mask"] to use as load balancing decision in PTRR.

Justification:

Consider the models like GPT-OSS or Olmo3, that alternates between sliding window and global causal mask.
For example, say the input sequence consists of documents of length [4, 8] and sliding window size is 3.

global causal mask                  work per row       relative order (decreasing)
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  1                     11       
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  2                     9
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  3                     7
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]  ->  4                     5
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]  ->  1                     11
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]  ->  2                     9
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0]  ->  3                     7
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0]  ->  4                     5
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]  ->  5                     4
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0]  ->  6                     3
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0]  ->  7                     2
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]  ->  8                     1
sliding window mask                  work per row
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  1
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  2
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  2
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]  ->  2
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]  ->  1
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]  ->  2
[0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]  ->  2
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]  ->  2
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0]  ->  2
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]  ->  2
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]  ->  2
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]  ->  2

Suppose we apply the global causal mask after every $k$ application of the sliding window mask.
Then, a reasonable cost model for work for a row $r$ is $c_r := k c_r^s + c_r^g$ where $c_r^s$ is the cost for row $r$ for sliding window mask and $c_r^g$ is the cost for row $r$ for global causal mask.

For a concrete example, let $k = 3$ as in the case of Olmo 3.

combined cost                  work per row              relative order (decreasing)
[4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  1 + 3 = 4                     11
[4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  2 + 6 = 8                     9
[1, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0]  ->  3 + 6 = 9                     7
[1, 1, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0]  ->  4 + 6 = 10                    5
[0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]  ->  1 + 3 = 4                     11
[0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0]  ->  2 + 6 = 8                     9
[0, 0, 0, 0, 1, 4, 4, 0, 0, 0, 0, 0]  ->  3 + 6 = 9                     7
[0, 0, 0, 0, 1, 1, 4, 4, 0, 0, 0, 0]  ->  4 + 6 = 10                    5
[0, 0, 0, 0, 1, 1, 1, 4, 4, 0, 0, 0]  ->  5 + 6 = 11                    4
[0, 0, 0, 0, 1, 1, 1, 1, 4, 4, 0, 0]  ->  6 + 6 = 12                    3
[0, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 0]  ->  7 + 6 = 13                    2
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 4, 4]  ->  8 + 6 = 14                    1

Note that the relative order of $(c_r)$ is same as the relative order of $(c_r^g)$.
One can show this holds true in general when we take a weighted combination of a global causal mask and a sliding window mask.
This means that if we use a sorting based load balancing algorithm such as PTRR on the costs $(c_r^g)$ or $(c_r)$, we get the same output.
Therefore, it may be reasonable to give the user a choice to still use PTRR when there are multiple masks present in the model.

Test Plan:
Loss decreases in a similarly, though not identically, in running

MODULE=gpt_oss CONFIG=gpt_oss_debugmodel_flex ./run_train.sh --parallelism.context-parallel-degree 2

and

MODULE=gpt_oss CONFIG=gpt_oss_debugmodel_flex ./run_train.sh --parallelism.context-parallel-degree 2 --parallelism.context-parallel-load-balancer ptrr --parallelism.context-parallel-ptrr-mask-key basic_mask

Also, tested in tests/integration_tests/model.py.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 23, 2026
- None: Disable load balancing
"""

context_parallel_ptrr_mask_key: str | None = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you test this in CI for gpt-oss:

  • let's modify (in-place) this FSDP 4 to "FSDP 2 CP 2" or "CP 4", and apply global mask as the key.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added it.

Summary:
Context parallelism's PTRR load balancer previously rejected any model
whose attention masks are provided as a dict of named masks, since the
balancer can only be built from a single mask. Models like GPT-OSS with
flex attention legitimately produce multiple masks (e.g. a full-causal
mask plus a sliding-window mask), so PTRR was unusable for them.

This adds a configuration option letting the user name which mask in the
dict PTRR should build its balancer from. The resulting balancer is then
used to shard every mask in the dict as well as the inputs. When the
masks are a dict but no key is specified (or the key is invalid), we fail
loudly and list the available keys instead of silently guessing.

It also converts the gpt-oss flex integration test to run under context
parallelism (FSDP 2 x CP 2) with the ptrr balancer and the global
(basic_mask) key, so CI actually exercises the multi-mask CP path.

Test Plan:
Added integration test 'gpt_oss_pp+fsdp+cp+ep+sacop' which runs
gpt_oss_debugmodel_flex (dict of basic_mask + sliding_window_mask) under
FSDP 2 x CP 2 + PP 2 + EP 4 with ptrr and
context_parallel_ptrr_mask_key=basic_mask. Run on 8 GPUs:

  python -m tests.integration_tests.run_tests <out_dir> \
    --test_suite models --test_name 'gpt_oss_pp+fsdp+cp+ep+sacop' --ngpu 8

TODO: paste CI / local run results once executed.
@jinsooihm
jinsooihm force-pushed the ptrr-dict-attention-masks branch from 4b6cbb4 to 9d9b879 Compare July 23, 2026 22:00
@felipemello1

Copy link
Copy Markdown
Contributor

@wwwjn related to the dp balancing, no?

if attention_masks is None:
raise ValueError(
"PTRRLoadBalancer requires attention_masks to be a "
"BlockMask, but got None or dict[str, BlockMask]"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why removing dict[str, BlockMask]?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed the comment

@fegin

fegin commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

@felipemello1 No, this is purely CP load balancing.

@fegin
fegin merged commit 4c64811 into pytorch:main Jul 24, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants