Skip to content

Instantly share code, notes, and snippets.

@rpatrik96
Last active July 28, 2022 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpatrik96/aa4c25803e2586179b53cc33fbdc738b to your computer and use it in GitHub Desktop.
Save rpatrik96/aa4c25803e2586179b53cc33fbdc738b to your computer and use it in GitHub Desktop.
PyTorch Lightning script for logging with Weights&Biases in offline mode and automatic sync at the end of training
import pytorch_lightning as pl
import wandb
from os.path import dirname
import subprocess
from pytorch_lightning.loggers.wandb import WandbLogger
from pytorch_lightning.utilities.cli import LightningCLI
class WandBOfflineSync(pl.LightningModule):
def __init__(self, offline: bool = True):
super().__init__()
self.save_hyperparameters()
def on_fit_end(self) -> None:
if self.hparams.offline is True:
# Syncing W&B at the end
# 1. save sync dir (after marking a run finished, the W&B object changes (is teared down?)
sync_dir = dirname(self.logger.experiment.dir)
# 2. mark run complete
wandb.finish()
# 3. call the sync command for the run directory
subprocess.check_call(["wandb", "sync", sync_dir])
class MyLightningCLI(LightningCLI):
def before_fit(self):
if isinstance(self.trainer.logger, WandbLogger) is True:
if self.config[self.subcommand].model.offline is True:
self.trainer.logger.__dict__["_wandb_init"]["mode"] = "offline"
else:
self.trainer.logger.__dict__["_wandb_init"]["mode"] = "online"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment