Bridge instantiate_utils: drop unexpected config keys with warning#1203
Merged
Conversation
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
…None in instantiate_utils.py and now let exceptions propagate, aligning with the new filtering behavior. Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
ananthsub
reviewed
Nov 6, 2025
Comment on lines
+383
to
+389
| if mode == InstantiationMode.LENIENT: | ||
| # Warn and drop the unexpected keys | ||
| warning_msg = f"Dropping unexpected config keys for target '{target_str}': {sorted(unexpected)}" | ||
| if full_key: | ||
| warning_msg += f"\nfull_key: {full_key}" | ||
| logging.warning(warning_msg) | ||
| return {k: v for k, v in kwargs.items() if k in allowed_keys} |
Contributor
There was a problem hiding this comment.
this avoids erroring out when fields are removed from the config. but in the case of changes like this, the semantics are lost: NVIDIA/Megatron-LM#1917
for example:
- a user previously used set
external_cuda_graph=Trueand the config did not yet havecuda_graph_scopeand has checkpoints saved with this config - in a newer version where
external_cuda_graphis removed, this PR will drop the arg in lenient mode.
it might not always be possible to infer the new setting from an old one so this might be inevitable, but we should make no claims about full reproducibility across versions in this case
ananthsub
reviewed
Nov 10, 2025
| @@ -0,0 +1,85 @@ | |||
| #!/usr/bin/env python3 | |||
Contributor
There was a problem hiding this comment.
move this into the existing test file: https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/tests/unit_tests/utils/test_instantiate_utils.py
Contributor
There was a problem hiding this comment.
this file can be removed now
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
Contributor
Author
|
/ok to test 70be68d |
ananthsub
previously approved these changes
Nov 12, 2025
Contributor
Author
|
/ok to test d96f333 |
Contributor
Author
|
/ok to test 6d92b19 |
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
Contributor
Author
|
/ok to test dc5e056 |
ananthsub
approved these changes
Nov 26, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Risks of Using Lenient Mode for Config Handling**
Using lenient mode when loading configs (instead of strict) can avoid errors when config fields are missing or changed, but it introduces several concerns:
✅ Pros
Hidden Config Mismatches
Reproducibility Concerns
Version Guarding Becomes MCore’s Problem
Technical Debt Over Time
Introduces defensive handling in
megatron.bridge.utils.instantiate_utils.instantiate_nodeto gracefully ignore unexpected config keys when instantiating targets.Emits a
logging.WARNINGidentifying dropped keys and the configfull_keyto aid debugging.Adds unit tests verifying both the drop-and-warn behavior and that targets accepting
**kwargsare unaffected.Why
use_megatron_fsdp) caused failures like:What changed
instantiate_utils.py_filter_kwargs_for_target(target, kwargs, full_key):inspect.signatureto determine accepted keyword parameters.**kwargs, forwards all keys unchanged.full_key._target_.tests/unit_tests/bridge/test_instantiate_utils.py:test_drops_unexpected_kwargs_and_warns: ensures unexpected keys are removed and a warning mentioning them is emitted.test_allows_kwargs_when_target_accepts_var_kwargs: ensures no warning is produced and keys are preserved when the target accepts**kwargs.