1 lightning.training.trainer
lightning.training.trainer
Construct the external Lightning trainer and attach run-owned services.
This module provides one config-as-factory surface for loop limits, devices, precision, validation cadence, deterministic/debug behavior, callbacks, and optional W&B logging. It owns trainer construction only; the experiment owns run paths and seeding, while the module owns scorer/loss/optimizer state.
1.1 Classes
| Name | Description |
|---|---|
| TrainerFactoryConfig | Configure one external Lightning trainer and its attached services. |
1.1.1 TrainerFactoryConfig
lightning.training.trainer.TrainerFactoryConfig()Configure one external Lightning trainer and its attached services.
1.1.1.1 Attributes
| Name | Description |
|---|---|
| target_type | Return the external Lightning trainer factory target. |
| is_debug | Force CPU fast-dev execution, anomaly detection, and no checkpoint callback. |
| fast_dev_run | Ask Lightning to run one batch per loop as an integration smoke test. |
| accelerator | Lightning accelerator selector such as "auto", "cpu", or "gpu". |
| devices | Device count, selector, or explicit device-index sequence passed to Lightning. |
| strategy | Optional distributed-execution strategy; "auto" delegates selection. |
| max_epochs | Maximum training epochs; None delegates the unbounded policy to Lightning. |
| max_steps | Exact optimizer-step ceiling; -1 leaves the existing epoch-only policy. |
| precision | Lightning numeric-precision policy, including mixed-precision strings. |
| default_root_dir | Root directory used by Lightning for logger-free run artifacts. |
| tf32_matmul_precision | Optional process-wide float32 matmul precision set before trainer construction. |
| gradient_clip_val | Optional gradient clipping threshold applied by Lightning. |
| accumulate_grad_batches | Positive number of batches accumulated per optimizer step. |
| log_every_n_steps | Trainer logging cadence in optimizer/training steps. |
| deterministic | Lightning deterministic-algorithm policy; None preserves framework defaults. |
| limit_train_batches | Optional absolute count or fraction limiting each training epoch. |
| limit_val_batches | Optional absolute count or fraction limiting each validation epoch. |
| check_val_every_n_epoch | Epoch cadence for validation when validation is enabled. |
| num_sanity_val_steps | Validation batches run before training; -1 checks the entire loader. |
| use_distributed_sampler | Allow Lightning to replace data-loader samplers during distributed execution. |
| enable_validation | Enable validation; false zeroes validation limits/cadence during validation. |
| enable_model_summary | Enable Lightning’s default model summary callback. |
| callbacks | Callback collection factory attached to the constructed trainer. |
| use_wandb | Use the configured W&B logger outside debug mode. |
| wandb_config | W&B logger config instantiated only when use_wandb is true. |
1.1.1.2 Methods
| Name | Description |
|---|---|
| setup_target | Instantiate a Trainer after logger and callback ownership is resolved. |
1.1.1.2.1 setup_target
lightning.training.trainer.TrainerFactoryConfig.setup_target(
experiment=None,
*,
trial=None,
optuna_config=None,
)Instantiate a Trainer after logger and callback ownership is resolved.
1.1.1.2.1.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| experiment | Any | None | Optional experiment providing an optuna_config fallback. |
None |
| trial | 'Trial | None' | Optional Optuna trial forwarded to callback construction. | None |
| optuna_config | 'OptunaConfig | None' | Explicit pruning configuration, preferred over the experiment fallback. | None |
1.1.1.2.1.2 Returns
| Name | Type | Description |
|---|---|---|
pl.Trainer |
External Lightning trainer owning the fresh logger and callbacks. |
1.1.1.2.1.3 Notes
TF32 precision is process-wide. Debug mode uses Lightning’s default logger; normal mode uses W&B only when configured. Checkpointing is enabled precisely when callback construction produced a checkpoint callback.