[feat] Refactor training framework into fastvideo/train#1159
[feat] Refactor training framework into fastvideo/train#1159jzhang38 merged 31 commits intohao-ai-lab:mainfrom
Conversation
|
Related Issue: #1158 |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant architectural overhaul to the training framework, moving from rigid, hardcoded pipelines to a highly modular and configurable system. The primary goal is to enhance flexibility and extensibility, allowing users to easily combine different models and training algorithms through declarative YAML configurations. This refactoring streamlines the process of experimenting with new models and methods, while also standardizing infrastructure concerns like distributed training, checkpointing, and validation. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a major and well-designed refactoring of the training framework, making it modular and YAML-driven. The separation of concerns into models, methods, and infrastructure is a significant improvement. The code is generally of high quality.
My review focuses on a few areas to improve portability and maintainability:
- Hardcoded Paths: Several example configuration files and a shell script contain user-specific absolute paths, which should be replaced with placeholders or relative paths to make them portable.
- Code Encapsulation: One of the entrypoint scripts imports private functions from another module, which could be refactored to improve encapsulation and reduce code duplication.
- Documentation Formatting: There are minor markdown formatting issues in one of the documentation files.
Note: Security Review did not run due to the size of the PR.
| def _run_config_from_raw( | ||
| raw: dict[str, Any], | ||
| ) -> Any: | ||
| """Reconstruct a RunConfig from a raw config dict. | ||
|
|
||
| This mirrors ``load_run_config`` but operates on an | ||
| already-parsed dict (from metadata.json) instead of | ||
| reading from a YAML file. | ||
| """ | ||
| from fastvideo.train.utils.config import ( | ||
| RunConfig, | ||
| _build_training_config, | ||
| _parse_pipeline_config, | ||
| _require_mapping, | ||
| _require_str, | ||
| ) | ||
|
|
||
| models_raw = _require_mapping( | ||
| raw.get("models"), where="models", | ||
| ) | ||
| models: dict[str, dict[str, Any]] = {} | ||
| for role_key, model_cfg_raw in models_raw.items(): | ||
| role_str = _require_str( | ||
| role_key, where="models.<role>", | ||
| ) | ||
| model_cfg = _require_mapping( | ||
| model_cfg_raw, | ||
| where=f"models.{role_str}", | ||
| ) | ||
| models[role_str] = dict(model_cfg) | ||
|
|
||
| method_raw = _require_mapping( | ||
| raw.get("method"), where="method", | ||
| ) | ||
| method = dict(method_raw) | ||
|
|
||
| callbacks_raw = raw.get("callbacks", None) | ||
| callbacks: dict[str, dict[str, Any]] = ( | ||
| _require_mapping( | ||
| callbacks_raw, where="callbacks", | ||
| ) | ||
| if callbacks_raw is not None | ||
| else {} | ||
| ) | ||
|
|
||
| pipeline_config = _parse_pipeline_config( | ||
| raw, models=models, | ||
| ) | ||
|
|
||
| training_raw = _require_mapping( | ||
| raw.get("training"), where="training", | ||
| ) | ||
| t = dict(training_raw) | ||
| training = _build_training_config( | ||
| t, | ||
| models=models, | ||
| pipeline_config=pipeline_config, | ||
| ) | ||
|
|
||
| return RunConfig( | ||
| models=models, | ||
| method=method, | ||
| training=training, | ||
| callbacks=callbacks, | ||
| raw=raw, | ||
| ) |
There was a problem hiding this comment.
The function _run_config_from_raw and its usage of private functions (e.g., _build_training_config, _parse_pipeline_config) from fastvideo.train.utils.config suggests a need for refactoring. Importing private members from other modules can lead to fragile code.
Consider one of the following approaches:
- Make the helper functions in
fastvideo.train.utils.configpublic if they are intended for reuse. - Refactor
load_run_configto accept either a file path or a pre-loaded dictionary, which would eliminate the need for_run_config_from_rawand the private imports.
…rain-clean-refactor
…esearch/FastVideo into train-clean-refactor
…esearch/FastVideo into train-clean-refactor
Summary
Introduces
fastvideo/train, a refactored training framework that replaces the monolithic training/distillation pipelines with a modular, YAML-driven architecture.Key design changes
_target_-based instantiation: Models and methods are selected via_target_keys in YAML (e.g.,fastvideo.train.models.wan.WanModel,fastvideo.train.methods.distribution_matching.dmd2.DMD2Method), making it easy to add new models/methods without modifying framework code.models/), methods (methods/), callbacks (callbacks/), and the training loop (trainer.py) are fully decoupled. The trainer callsmethod.train_one_step()without knowing which method is running.callbacks/) rather than hardcoded in the training loop. Configured via thecallbacks:section in YAML.
TrainingConfigdataclass (utils/training_config.py) provides typed defaults for all training parameters. The fully-resolved config(with defaults filled in) is logged to W&B.
CheckpointManager, plusdcp_to_diffusers.pyfor converting checkpoints to Diffusers format.Supported models & methods
Bug fixes
real_score_guidance_scalein DMD2 and self-forcing to use the standard formulauncond + scale * (cond - uncond)instead ofcond + scale * (cond - uncond)(which silently added +1 to the effective guidance scale).File structure
fastvideo/train/
trainer.py
models/{base, wan/, wangame/}
methods/{base, distribution_matching/, fine_tuning/}
callbacks/{callback, grad_clip, validation, ema}
entrypoint/{train, dcp_to_diffusers}
utils/{config, builder, training_config, checkpoint, dataloader, optimizer, tracking, ...}
Usage
torchrun --nproc_per_node=8 -m fastvideo.train.entrypoint.train \ --config examples/distillation/refactor/distill_wan2.1_t2v_1.3B_dmd2.yaml Test plan - DMD2 8-step distillation on Wan 2.1 T2V 1.3B matches legacy training loss curves - VSA finetuning on Wan produces equivalent results to legacy pipeline - Self-forcing distillation on WanGame runs without errors - DFSFT on WanGame runs without errors - Checkpoint save/resume round-trips correctly - W&B logging shows fully-resolved config with defaultsThanks to @jzhang38 heavily discussing, reviewing and modifying code!