PyTorch Lightning
Log and compare PyTorch Lightning training runs in InstantML with a one-line logger that drops into the Lightning Trainer.
Pass im.LightningLogger to the Lightning Trainer and everything you self.log(...) lands in InstantML. The logger implements the Lightning Logger interface and is rank-zero safe: in distributed training only the main process logs.
Install
python -m pip install "instantml[frameworks]"Authenticate with `instantml login` or INSTANTML_API_KEY if you haven't already.
Wire the logger into the Trainer
This complete example trains a tiny regressor on synthetic data and logs train/loss every step:
import instantml as im
import lightning as L
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset
class TinyRegressor(L.LightningModule):
def __init__(self, lr=1e-3):
super().__init__()
self.save_hyperparameters()
self.layer = nn.Linear(8, 1)
def training_step(self, batch, batch_idx):
x, y = batch
loss = nn.functional.mse_loss(self.layer(x), y)
self.log("train/loss", loss)
return loss
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=self.hparams.lr)
dataset = TensorDataset(torch.randn(256, 8), torch.randn(256, 1))
logger = im.LightningLogger(
project="lightning-demo",
name="tiny-regressor",
tags=["lightning"],
)
trainer = L.Trainer(max_epochs=5, logger=logger, log_every_n_steps=1)
trainer.fit(TinyRegressor(), DataLoader(dataset, batch_size=32))Open the lightning-demo project at instantml.ai to watch the train/loss chart live. The run is finished automatically when Lightning calls finalize(...) at the end of training.
LightningLogger accepts the same keyword arguments as im.init(...) (name, config, tags, notes, api_key, ...) and forwards them when it creates the run on first use.
What gets logged
- Metrics β everything you pass to
self.log(...)inside yourLightningModuleis forwarded throughlog_metrics(...)at the current step. - Hyperparameters β
log_hyperparams(...)records them as run config, soself.save_hyperparameters()in your module captures your config automatically. - Media β Lightning's
log_image,log_audio, andlog_videomap to InstantML rich objects.
Reuse an existing run
Pass an already-created run when you want the logger to share a run with other code instead of creating its own:
run = im.init(project="lightning-demo", name="tiny-regressor")
logger = im.LightningLogger(run=run)
trainer = L.Trainer(logger=logger)