lightning_policy
lightning_policy
¶
PyTorch Lightning wrapper for Policy.
LightningPolicy
¶
Bases: LightningModule
PyTorch Lightning wrapper around Policy.
This wrapper handles: - Training and validation steps - Optimizer configuration with parameter groups - Learning rate scheduling - Metric accumulation and logging - Gradient clipping (via Lightning Trainer)
Initialize LightningPolicy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Policy
|
The policy to train |
required |
training_config
|
TrainingConfig
|
Training configuration |
required |
total_training_steps
|
int | None
|
Total number of training steps for LR scheduling. Calculated as ceil(len(train_loader) / gradient_accumulate_every) * num_epochs, because Lightning flushes the final partial accumulation window each epoch. If None, will use trainer.estimated_stepping_batches as fallback. |
None
|
Source code in src/versatil/training/lightning_policy.py
on_train_epoch_start
¶
training_step
¶
Training step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, dict[str, Tensor]]
|
Batch dictionary with observations and actions |
required |
batch_idx
|
int
|
Batch index |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Total loss tensor |
Source code in src/versatil/training/lightning_policy.py
on_train_epoch_end
¶
Called at the end of training epoch to log accumulated metrics.
Source code in src/versatil/training/lightning_policy.py
validation_step
¶
Validation step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, dict[str, Tensor]]
|
Batch dictionary with observations and actions |
required |
batch_idx
|
int
|
Batch index |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Total loss tensor |
Source code in src/versatil/training/lightning_policy.py
on_validation_epoch_end
¶
Called at the end of validation epoch to log accumulated metrics.
Source code in src/versatil/training/lightning_policy.py
configure_optimizers
¶
Configure optimizers and learning rate schedulers.
Uses Hydra's get_class() to resolve optimizer from config.target_class. Supports parameter groups with different learning rates.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with optimizer and optional lr_scheduler |
Source code in src/versatil/training/lightning_policy.py
on_load_checkpoint
¶
Called when loading a checkpoint.
Ensures that observation_space and action_space are converted from OmegaConf DictConfig to proper dataclass instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint
|
dict[str, Any]
|
The loaded checkpoint dictionary |
required |
Source code in src/versatil/training/lightning_policy.py
forward
¶
Forward pass for inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_dict
|
dict[str, Tensor]
|
Observation dictionary |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Predicted actions |
Source code in src/versatil/training/lightning_policy.py
set_dataloaders
¶
Attach the dataloaders returned by the Lightning dataloader hooks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_loader
|
DataLoader
|
Training dataloader. |
required |
val_loader
|
DataLoader | None
|
Optional validation dataloader. |
required |
Source code in src/versatil/training/lightning_policy.py
train_dataloader
¶
val_dataloader
¶
Return validation dataloader for Lightning, or None if validation is disabled.