New to Claude Skills? Learn how to install them →

Hnvidia on GitHub

Hierarchical Context Parallel

OfficialFree

Optimize Megatron-Bridge for hierarchical context parallelism.

by nvidia2.8k stars on nvidia/skills
1 views
Updated Aug 7, 2026
Get this skill

Free · Opens the source repo

What Hierarchical Context Parallel does

The Hierarchical Context Parallel skill provides a detailed operational guide for implementing hierarchical context parallelism (HCP) within the Megatron-Bridge framework. This skill is particularly useful for developers and researchers working with large-scale transformer models who need to optimize their training processes. It outlines the configuration settings necessary to enable HCP, including the required parameters for the cp_comm_type and hierarchical_context_parallel_sizes. By leveraging these settings, users can effectively manage nested context-parallel process groups, which can significantly enhance the performance of distributed training tasks.

This skill includes practical code anchors and validation steps that ensure users can implement HCP correctly. For instance, it highlights the importance of matching the product of hierarchical_context_parallel_sizes with the context_parallel_size, and it provides specific code snippets that demonstrate how to set up the necessary configurations in Python. Additionally, the skill outlines common pitfalls that users may encounter, such as issues with using decentralized process groups or silent failures when configurations are not properly set.

Verification processes are also included, guiding users through logging checks and unit tests to confirm that hierarchical context parallelism is functioning as expected. By following these guidelines, users can ensure that their implementation is robust and that they are leveraging the full capabilities of the Megatron-Bridge framework for efficient model training.

Overall, this skill is tailored for developers and data scientists who are familiar with distributed training and are looking to optimize their workflows using advanced parallelism techniques in deep learning.

When to use it

Use this skill when configuring Megatron-Bridge for distributed training with hierarchical context parallelism to improve performance.

When not to use it

Avoid this skill if you are not using Megatron-Bridge or if your training setup does not require hierarchical context parallelism.

What you can build with it

Configuring Megatron-Bridge for HCP

Use this skill to set up your Megatron-Bridge configuration for hierarchical context parallelism, ensuring optimal performance.

Validating HCP Implementation

Follow the verification steps outlined in this skill to confirm that your hierarchical context parallelism is functioning correctly.

Troubleshooting HCP Issues

Refer to the pitfalls section of this skill to address common issues encountered when enabling hierarchical context parallelism.

How to install Hierarchical Context Parallel

View source

1. Install with the skills CLI

npx skills add nvidia/skills/nemo-mbridge-perf-hierarchical-context-parallel --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by nvidia

Hierarchical Context Parallel Skill

This skill covers hierarchical context parallelism: nested context-parallel process groups used by cp_comm_type="a2a+p2p" and configured with hierarchical_context_parallel_sizes.

For what hierarchical CP is, when to use it, and the decision tree (a2a+p2p vs pure a2a vs p2p), see:

  • @docs/training/hierarchical-context-parallel.md
  • @skills/nemo-mbridge-perf-hierarchical-context-parallel/card.yaml

Enablement

Minimal Bridge override:

cfg.model.context_parallel_size = 4
cfg.model.cp_comm_type = "a2a+p2p"
cfg.model.hierarchical_context_parallel_sizes = [2, 2]
cfg.dist.use_decentralized_pg = False

Required constraints:

  • prod(hierarchical_context_parallel_sizes) == context_parallel_size
  • seq_length % (2 * context_parallel_size) == 0
  • Transformer Engine >= 1.12.0

Code Anchors

Upstream config and validation:

context_parallel_size: int = 1
"""Splits network input along sequence dimension across GPU ranks."""

hierarchical_context_parallel_sizes: Optional[list[int]] = None
"""Degrees of the hierarchical context parallelism. Users should provide a list to specify 
   the sizes for different levels. Taking the a2a+p2p cp comm type as example, it contains
   groups of two levels, so the first value of the list indicates the group size of the a2a
   communication type, and the second value indicates the group size of the p2p communication
   type.
"""
if args.hierarchical_context_parallel_sizes:
    from numpy import prod
    assert args.context_parallel_size == prod(args.hierarchical_context_parallel_sizes)
if "a2a+p2p" in args.cp_comm_type:
    assert args.hierarchical_context_parallel_sizes is not None, \
    "--hierarchical-context-parallel-sizes must be set when a2a+p2p is used in cp comm"

Bridge MPU path:

parallel_state.initialize_model_parallel(
    ...
    context_parallel_size=model_config.context_parallel_size,
    hierarchical_context_parallel_sizes=model_config.hierarchical_context_parallel_sizes,
    ...
)
...
return ProcessGroupCollection.use_mpu_process_groups()

Bridge decentralized-PG path:

pg_collection = ProcessGroupCollection(
    ...
    cp=cp_pg,
    tp_cp=tp_cp_pg,
    hcp=None,
    ep=ep_pg,
    ...
)

Implementation Map

The code anchors above show the config declarations and argument validation.

Validation (MCore)

TransformerConfig.__post_init__ enforces that a2a+p2p requires HCP sizes and the product matches CP.

Process group creation

parallel_state.initialize_model_parallel creates hierarchical CP sub-groups when HCP sizes are provided via create_hierarchical_groups. Bridge currently gets those groups through the MPU-backed ProcessGroupCollection.

TE integration

TEDotProductAttention passes the hierarchical groups to Transformer Engine when a2a+p2p is used. Requires Transformer Engine >= 1.12.0.

Pitfalls

  1. Bridge HCP is MPU-only today: If use_decentralized_pg=True, Bridge initializes flat CP groups and leaves HCP unset.
  2. No checked-in Bridge recipe currently exercises HCP directly.
  3. Single-GPU load helpers clear hierarchical_context_parallel_sizes.
  4. Silent broken training on old stacks: If you use a2a+p2p without setting hierarchical_context_parallel_sizes, MCore now asserts. Older versions would silently disable CP communication, so each rank attended only to its local chunk and produced artificially high throughput with broken gradients.
  5. Product must match: prod(hierarchical_context_parallel_sizes) must exactly equal context_parallel_size. A mismatch triggers an assertion.
  6. Verify in logs: Look for the process group initialization output. You should see HIERARCHICAL_CONTEXT_PARALLEL_GROUPS being created. If you only see CONTEXT_PARALLEL_GROUP, HCP is not active.

Verification

No dedicated Bridge end-to-end test exists yet for HCP (see @skills/nemo-mbridge-perf-hierarchical-context-parallel/card.yaml follow_up_validation). Use the existing unit tests and log inspection instead.

Run the decentralized-PG unit test to confirm the flat-CP behavior is preserved:

uv run python -m pytest tests/unit_tests/training/test_decentralized_pg.py -q

For a manual smoke check, launch a 4-GPU run with a small recipe and cp_comm_type=a2a+p2p plus hierarchical_context_parallel_sizes=[2,2]:

CUDA_VISIBLE_DEVICES=0,1,2,3 uv run python -m torch.distributed.run --nproc_per_node=4 \
  scripts/training/run_recipe.py \
  --recipe llama32_1b_pretrain_config \
  model.context_parallel_size=4 \
  model.cp_comm_type=a2a+p2p \
  "model.hierarchical_context_parallel_sizes=[2,2]" \
  train.train_iters=2

Success criteria:

  • Logs show HIERARCHICAL_CONTEXT_PARALLEL_GROUPS being created
  • Training completes at least one step without error
  • If you only see CONTEXT_PARALLEL_GROUP, HCP is not active

Frequently asked questions about Hierarchical Context Parallel

Similar skills