5.7 KiB
5.7 KiB
fastvideo/train/ — Modular Training Framework
YAML-driven trainer composed from interchangeable methods × models × callbacks. Preferred location for new training code. (See sibling fastvideo/training/AGENTS.md for the legacy stack.)
Layout
train/
├── trainer.py # Core training loop coordinator
├── README.md # User-facing overview (legacy → new diff)
├── entrypoint/ # train.py + dcp_to_diffusers.py CLI entrypoints
├── methods/
│ ├── base.py # TrainingMethod ABC
│ ├── fine_tuning/ # FineTuneMethod, DiffusionForcingSFTMethod
│ ├── distribution_matching/ # DMD2Method, SelfForcingMethod
│ ├── knowledge_distillation/ # KDMethod, KDCausalMethod
│ ├── consistency_model/ # Consistency-model training methods
│ └── rl/ # RL methods, sampling helpers, rewards
├── models/
│ ├── base.py # ModelBase / CausalModelBase wrappers
│ ├── wan/, hunyuan/, cosmos/ # Per-family training wrappers
├── callbacks/ # callback.py base + ema, grad_clip, validation
└── utils/
├── training_config.py # Hierarchical YAML config dataclasses
├── builder.py # Build trainer from config
├── checkpoint.py # DCP save/load
├── optimizer.py # AdamW / fused optimizer factory
├── tracking.py # build_tracker (W&B / TensorBoard)
└── dataloader.py # StatefulDataLoader wiring
Composition Model
Trainer = Method × Model × [Callback...] × Config
- A Method owns the loss + optimizer step (
compute_loss,step_post_grad). - A Model owns the forward + parameter-grouping (
forward,trainable_parameters). - Callbacks subscribe to lifecycle hooks (
on_train_start,on_training_step_end,on_before_optimizer_step,on_validation_begin,on_validation_end,on_train_end) and compose freely. - Config is a Pydantic-style hierarchical YAML resolved by
utils/training_config.py. Dotted-key CLI overrides go throughparse_overrides.
Adding a New Model Plugin
- Subclass
ModelBase(orCausalModelBase) inmodels/<family>/. - Wrap the existing inference DiT from
fastvideo/models/dits/. Do not reimplement. - Expose
trainable_parameters()so the optimizer factory can group them. - Register in
utils/builder.pyif the trainer dispatches by name.
Adding a New Method
- Subclass
TrainingMethodinmethods/<family>/. - Implement
compute_loss(batch, model_outputs) -> dict. - If the method needs a teacher / second model, expose a
build_extras(cfg)classmethod — never instantiate inside__init__.
RL/RLHF Boundaries
- RL methods live under
methods/rl/, subclassTrainingMethod, and own reward collection, advantage computation, policy loss, KL/reference terms, and optimizer cadence. - Keep model-family forward logic and latent decoding in
ModelBasewrappers. RL methods must useModelBase.decode_latentsrather than reach into VAE normalization internals or inference pipelines. - Shared generation, grouped prompt sampling, validation helpers, and scheduler
math belong under
methods/rl/common/. - Use
common/sampling.pyfor generation andcommon/prompt_sampling.pyfor reusable patterns such as DiffusionNFT K-repeat. - Return
manages_optimization() == Trueonly for a genuinely nonstandard outer/inner loop, implemented throughmanaged_train_step(data_stream, iteration). Existing callbacks, checkpointing, tracking, and validation must continue to work.
Sampling and configuration
- Put method knobs under
methodand sampler knobs undermethod.sampling. - A diffusers-style scheduler owns both its schedule and
step()solver. Usetrajectoryonly for higher-level ODE versus explicit re-noise behavior; missing timesteps means to ask the scheduler for its defaults. - Avoid fixed timestep lists in examples unless reproducing a known baseline.
- Do not put scheduler or trajectory policy in model configs.
Reward models
- Put reusable rewards under
methods/rl/rewards/and export their builders from that package's__init__.py. Keep method-specific aggregation and advantage logic out of reward classes. - Rewards receive decoded media and return one scalar per prompt/sample.
Support
[B, C, H, W]images and[B, C, T, H, W]videos when practical. Each reward must explicitly choose whether it evaluates the first frame, sampled frames, or the full video. - Preserve upstream attribution and SPDX headers for ported reward code.
RL tests
- Add fake-model tests for sampler/method behavior and fake-scorer injection for reward aggregation.
- Test config parsing, tensor layouts, weighted reward metrics, prompt grouping, and managed-optimization opt-in without loading heavyweight checkpoints.
- Confirm existing non-RL methods remain on the default Trainer path with
manages_optimization() == False.
Configs
YAML lives under examples/train/. Schemas in utils/training_config.py. Add
new fields with explicit defaults; agents and humans both rely on the dataclass
to discover knobs.
Anti-Patterns
- Importing from
fastvideo.training.*(legacy stack). Forbidden cross-import. - Adding a new training method as a fork of an existing pipeline file. Compose
via
Methodinstead. - Logging via stdlib
logging— useinit_logger(__name__). - Mutating the global state of a model in a callback. Callbacks operate on the trainer state object passed in.
- Binding RL methods to inference pipeline classes.
- Hardcoding scheduler timesteps or embedding reward-model code inside one RL method.