-
Notifications
You must be signed in to change notification settings - Fork 594
refactor(pt): refactor training code #5216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
iProzd
wants to merge
3
commits into
deepmodeling:master
Choose a base branch
from
iProzd:0210_training_code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,7 @@ | |
| from deepmd.pt.train import ( | ||
| training, | ||
| ) | ||
| from deepmd.pt.train.trainer import Trainer as NewTrainer | ||
| from deepmd.pt.train.wrapper import ( | ||
| ModelWrapper, | ||
| ) | ||
|
|
@@ -106,6 +107,7 @@ def get_trainer( | |
| init_frz_model: str | None = None, | ||
| shared_links: dict[str, Any] | None = None, | ||
| finetune_links: dict[str, Any] | None = None, | ||
| use_legacy: bool = False, | ||
| ) -> training.Trainer: | ||
| multi_task = "model_dict" in config.get("model", {}) | ||
|
|
||
|
|
@@ -200,19 +202,34 @@ def prepare_trainer_input_single( | |
| seed=data_seed, | ||
| ) | ||
|
|
||
| trainer = training.Trainer( | ||
| config, | ||
| train_data, | ||
| stat_file_path=stat_file_path, | ||
| validation_data=validation_data, | ||
| init_model=init_model, | ||
| restart_model=restart_model, | ||
| finetune_model=finetune_model, | ||
| force_load=force_load, | ||
| shared_links=shared_links, | ||
| finetune_links=finetune_links, | ||
| init_frz_model=init_frz_model, | ||
| ) | ||
| if use_legacy: | ||
| trainer = training.Trainer( | ||
| config, | ||
| train_data, | ||
| stat_file_path=stat_file_path, | ||
| validation_data=validation_data, | ||
| init_model=init_model, | ||
| restart_model=restart_model, | ||
| finetune_model=finetune_model, | ||
| force_load=force_load, | ||
| shared_links=shared_links, | ||
| finetune_links=finetune_links, | ||
| init_frz_model=init_frz_model, | ||
| ) | ||
| else: | ||
| trainer = NewTrainer( | ||
| config, | ||
| train_data, | ||
| stat_file_path=stat_file_path, | ||
| validation_data=validation_data, | ||
| init_model=init_model, | ||
| restart_model=restart_model, | ||
| finetune_model=finetune_model, | ||
| force_load=force_load, | ||
| shared_links=shared_links, | ||
| finetune_links=finetune_links, | ||
| init_frz_model=init_frz_model, | ||
| ) | ||
|
Comment on lines
+205
to
+232
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code for creating the trainer_class = training.Trainer if use_legacy else NewTrainer
trainer = trainer_class(
config,
train_data,
stat_file_path=stat_file_path,
validation_data=validation_data,
init_model=init_model,
restart_model=restart_model,
finetune_model=finetune_model,
force_load=force_load,
shared_links=shared_links,
finetune_links=finetune_links,
init_frz_model=init_frz_model,
) |
||
| return trainer | ||
|
|
||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,102 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """PyTorch training module with modular, extensible design. | ||
|
|
||
| This module provides a clean, component-based training system: | ||
|
|
||
| - TrainingConfig: Configuration management with validation | ||
| - DataManager: Data loading and batch iteration | ||
| - OptimizerFactory: Strategy pattern for optimizer creation | ||
| - CheckpointManager: Model persistence and recovery | ||
| - TrainingLoop: Specialized training step implementations | ||
| - HookManager: Extensible callback system | ||
| - TrainingLogger: Formatted output and file I/O | ||
| - Trainer: Main orchestrator coordinating all components | ||
|
|
||
| Example: | ||
| >>> from deepmd.pt.train import Trainer, TrainingConfig | ||
| >>> | ||
| >>> # Create trainer | ||
| >>> trainer = Trainer( | ||
| ... config=config_dict, | ||
| ... training_data=train_dataset, | ||
| ... validation_data=valid_dataset, | ||
| ... ) | ||
| >>> | ||
| >>> # Run training | ||
| >>> trainer.run() | ||
|
|
||
| Future extensions for multi-backend support: | ||
| - AbstractTrainingLoop can be extended for JAX/NumPy | ||
| - OptimizerFactory can support backend-specific optimizers | ||
| - DataManager can use backend-specific data loading | ||
| """ | ||
|
|
||
| from deepmd.pt.train.checkpoint_manager import ( | ||
| CheckpointManager, | ||
| ) | ||
| from deepmd.pt.train.config import ( | ||
| CheckpointConfig, | ||
| DisplayConfig, | ||
| LearningRateConfig, | ||
| OptimizerConfig, | ||
| TrainingConfig, | ||
| ) | ||
| from deepmd.pt.train.data_manager import ( | ||
| DataManager, | ||
| ) | ||
| from deepmd.pt.train.hooks import ( | ||
| HookManager, | ||
| HookPriority, | ||
| TensorBoardHook, | ||
| TimingHook, | ||
| TrainingHook, | ||
| ) | ||
| from deepmd.pt.train.logger import ( | ||
| LossAccumulator, | ||
| TrainingLogger, | ||
| ) | ||
| from deepmd.pt.train.optimizer_factory import ( | ||
| OptimizerFactory, | ||
| ) | ||
| from deepmd.pt.train.trainer import ( | ||
| Trainer, | ||
| ) | ||
|
|
||
| # Keep old Trainer available for backward compatibility during transition | ||
| from deepmd.pt.train.training import Trainer as LegacyTrainer | ||
| from deepmd.pt.train.training_loop import ( | ||
| AdamTrainingLoop, | ||
| BaseTrainingLoop, | ||
| LKFEnergyTrainingLoop, | ||
| TrainingLoopFactory, | ||
| ) | ||
| from deepmd.pt.train.wrapper import ( | ||
| ModelWrapper, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| # New modular components | ||
| "AdamTrainingLoop", | ||
| "BaseTrainingLoop", | ||
| "CheckpointConfig", | ||
| "CheckpointManager", | ||
| "DataManager", | ||
| "DisplayConfig", | ||
| "HookManager", | ||
| "HookPriority", | ||
| "LKFEnergyTrainingLoop", | ||
| "LearningRateConfig", | ||
| # Legacy support | ||
| "LegacyTrainer", | ||
| "LossAccumulator", | ||
| "ModelWrapper", | ||
| "OptimizerConfig", | ||
| "OptimizerFactory", | ||
| "TensorBoardHook", | ||
| "TimingHook", | ||
| "Trainer", | ||
| "TrainingConfig", | ||
| "TrainingHook", | ||
| "TrainingLogger", | ||
| "TrainingLoopFactory", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type annotation of
get_trainer()istraining.Trainer, but the function can now return the new modulardeepmd.pt.train.trainer.Trainerwhenuse_legacy=False. Update the annotation to a union/protocol (or a common base type) to keep typing accurate for downstream users.