Skip to content

Refactor hybrid layers to use per-layer configs - #6313

Open
Phlip79 wants to merge 20 commits into
NVIDIA:mainfrom
Phlip79:philip/per-layer-hybrid-configs
Open

Refactor hybrid layers to use per-layer configs#6313
Phlip79 wants to merge 20 commits into
NVIDIA:mainfrom
Phlip79:philip/per-layer-hybrid-configs

Conversation

@Phlip79

@Phlip79 Phlip79 commented Aug 6, 2026

Copy link
Copy Markdown
Member
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

See Part 2 of design doc.

Previous PRs: #6410, #6419, #6423.

This PR changes when and how layer configs are constructed. Instead of duplicating the same TransformerConfig for each layer (no matter the layer type), now each layer has its own config. Even after this PR, each layer's config will be identical. Each layer config just inherits TransformerConfig; however, this enables two features:

  • future layers will add their arguments to its respective layer class, instead of adding an argument to TransformerConfig
  • enable heterogeneous layer construction, as now we have a list of configs
    • in a future PR, we will add the ability to construct custom layer configs

Here is a simplified view of the new training workflow construction:

legacy_hybrid

Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

Comment thread megatron/core/inference/contexts/dynamic_context.py
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
@Phlip79

Phlip79 commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

/claude strict-review

Phlip79 added 2 commits August 7, 2026 00:16
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
@Phlip79

Phlip79 commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

/ok to test 48cd231

Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
@Phlip79

Phlip79 commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

/ok to test ce13069

Phlip79 added 2 commits August 9, 2026 22:07
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
@Phlip79

Phlip79 commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

/ok to test 0a78f80

Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
@Phlip79

Phlip79 commented Aug 15, 2026

Copy link
Copy Markdown
Member Author

/ok to test 67c5665

@Phlip79
Phlip79 removed the request for review from a team August 18, 2026 18:05

@janEbert janEbert left a comment

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.

One general comment: I often see type(obj) is LayerConfig instead of isinstance(obj, LayerConfig). The latter seems more canonical to me; is there a reason to prefer the former? (Note, I'm totally fine with keeping the type calls, just feels slightly less pythonic.)

Ah it's to explicitly avoid child classes also matching, isn't it? Could that bite us later?

affected a shared config, without coupling independently supplied config lists.
"""

synchronize_shared_config_mutations = True

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.

This attribute is only queried once, but the code never sets it to False. Do we need the _HybridLayerConfigList abstraction class surrounding it? In general I like the idea of having a type alias, but it's also not used for this.

Comment thread megatron/core/inference/contexts/dynamic_context.py Outdated
Comment on lines +380 to +381
layer_config = deepcopy(config)
layer_config.__class__ = LAYER_SYMBOL_TO_CONFIG_CLASS[layer_symbol]

@janEbert janEbert Aug 19, 2026

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.

I personally find this too hacky and would prefer a cleaner solution, e.g., a separate function that creates the class and then populates the attributes from the deep-copied config.

@janEbert janEbert left a comment

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.

Approved since I find the design is solid, but please take a look at the comments. :)

Comment thread megatron/core/inference/config.py Outdated
Comment thread megatron/core/models/hybrid/hybrid_block.py Outdated
Comment thread megatron/core/models/hybrid/hybrid_block.py Outdated
Comment thread tests/unit_tests/models/test_hybrid_model.py Outdated
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants