[megatron, ckpt] refactor: optimize mcore ckpt manager impl#6014
[megatron, ckpt] refactor: optimize mcore ckpt manager impl#6014ETOgaosion wants to merge 2 commits intoverl-project:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors MegatronCheckpointManager to support modular checkpointing backends, primarily introducing use_mbridge for HuggingFace format weights. The update replaces monolithic state dict generation with composable builders to avoid unnecessary overhead and includes a new suite of unit tests. Technical feedback recommends using a try...finally block to ensure transformer configuration attributes are restored after serialization and removing redundant type checks for callable objects.
| for k in bypass_keys: | ||
| if hasattr(self.transformer_config, k): | ||
| backup[k] = getattr(self.transformer_config, k, None) | ||
| delattr(self.transformer_config, k) | ||
| transformer_config_dict = asdict(self.transformer_config) | ||
| for k in backup: | ||
| setattr(self.transformer_config, k, backup[k]) |
There was a problem hiding this comment.
The current implementation of _save_transformer_config temporarily removes attributes from self.transformer_config to facilitate JSON serialization via asdict. However, if an exception occurs during the asdict call, these attributes will not be restored, potentially leaving the transformer_config object in a corrupted state for the remainder of the training process. It is safer to use a try...finally block to ensure attributes are always restored.
| for k in bypass_keys: | |
| if hasattr(self.transformer_config, k): | |
| backup[k] = getattr(self.transformer_config, k, None) | |
| delattr(self.transformer_config, k) | |
| transformer_config_dict = asdict(self.transformer_config) | |
| for k in backup: | |
| setattr(self.transformer_config, k, backup[k]) | |
| try: | |
| for k in bypass_keys: | |
| if hasattr(self.transformer_config, k): | |
| backup[k] = getattr(self.transformer_config, k, None) | |
| delattr(self.transformer_config, k) | |
| transformer_config_dict = asdict(self.transformer_config) | |
| finally: | |
| for k in backup: | |
| setattr(self.transformer_config, k, backup[k]) |
| if type(value) in ignore_types: | ||
| pop_keys.append(key) | ||
| if callable(value): | ||
| pop_keys.append(key) |
There was a problem hiding this comment.
The type check type(value) in ignore_types (where ignore_types contains Callable) is ineffective for identifying callable objects. In Python, the type of a function is types.FunctionType, not collections.abc.Callable. While the subsequent callable(value) check correctly identifies functions, this specific block is redundant and relies on an incorrect type comparison logic. It is better to rely solely on the callable() check.
| if type(value) in ignore_types: | |
| pop_keys.append(key) | |
| if callable(value): | |
| pop_keys.append(key) | |
| if callable(value): | |
| pop_keys.append(key) |
What does this PR do?
Checklist Before Starting
[{modules}] {type}: {description}(This will be checked by the CI){modules}includefsdp,megatron,veomni,sglang,vllm,rollout,trainer,ci,training_utils,recipe,hardware,deployment,ray,worker,single_controller,misc,perf,model,algo,env,tool,ckpt,doc,data,cfg,reward,fully_async,one_step_off,like[megatron, fsdp, doc]{type}is infeat,fix,refactor,chore,test[BREAKING]to the beginning of the title.[BREAKING][fsdp, megatron] feat: dynamic batchingTest
API and Usage Example
# Add code snippet or script demonstrating how to use thisDesign & Code Changes
Checklist Before Submitting
Important
Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.
pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=alwaysci-requestchannel in theverlSlack workspace. (If not accessible, please try the Feishu group (飞书群).)recipesubmodule, please also update the reference to the submodule commit viagit submodule update --remoteorcd recipe && git pull origin main.