class_docstr: Log using `Weights and Biases <https://docs.wandb.ai/integrations/lightning>`_.
  
  **Installation and set-up**
  
  Install with pip:
  
  .. code-block:: bash
  
      pip install wandb
  
  Create a `WandbLogger` instance:
  
  .. code-block:: python
  
      from lightning.pytorch.loggers import WandbLogger
  
      wandb_logger = WandbLogger(project="MNIST")
  
  Pass the logger instance to the `Trainer`:
  
  .. code-block:: python
  
      trainer = Trainer(logger=wandb_logger)
  
  A new W&B run will be created when training starts if you have not created one manually before with `wandb.init()`.
  
  **Log metrics**
  
  Log from :class:`~lightning.pytorch.core.LightningModule`:
  
  .. code-block:: python
  
      class LitModule(LightningModule):
          def training_step(self, batch, batch_idx):
              self.log("train/loss", loss)
  
  Use directly wandb module:
  
  .. code-block:: python
  
      wandb.log({"train/loss": loss})
  
  **Log hyper-parameters**
  
  Save :class:`~lightning.pytorch.core.LightningModule` parameters:
  
  .. code-block:: python
  
      class LitModule(LightningModule):
          def __init__(self, *args, **kwarg):
              self.save_hyperparameters()
  
  Add other config parameters:
  
  .. code-block:: python
  
      # add one parameter
      wandb_logger.experiment.config["key"] = value
  
      # add multiple parameters
      wandb_logger.experiment.config.update({key1: val1, key2: val2})
  
      # use directly wandb module
      wandb.config["key"] = value
      wandb.config.update()
  
  **Log gradients, parameters and model topology**
  
  Call the `watch` method for automatically tracking gradients:
  
  .. code-block:: python
  
      # log gradients and model topology
      wandb_logger.watch(model)
  
      # log gradients, parameter histogram and model topology
      wandb_logger.watch(model, log="all")
  
      # change log frequency of gradients and parameters (100 steps by default)
      wandb_logger.watch(model, log_freq=500)
  
      # do not log graph (in case of errors)
      wandb_logger.watch(model, log_graph=False)
  
  The `watch` method adds hooks to the model which can be removed at the end of training:
  
  .. code-block:: python
  
      wandb_logger.experiment.unwatch(model)
  
  **Log model checkpoints**
  
  Log model checkpoints at the end of training:
  
  .. code-block:: python
  
      wandb_logger = WandbLogger(log_model=True)
  
  Log model checkpoints as they get created during training:
  
  .. code-block:: python
  
      wandb_logger = WandbLogger(log_model="all")
  
  Custom checkpointing can be set up through :class:`~lightning.pytorch.callbacks.ModelCheckpoint`:
  
  .. code-block:: python
  
      # log model only if `val_accuracy` increases
      wandb_logger = WandbLogger(log_model="all")
      checkpoint_callback = ModelCheckpoint(monitor="val_accuracy", mode="max")
      trainer = Trainer(logger=wandb_logger, callbacks=[checkpoint_callback])
  
  `latest` and `best` aliases are automatically set to easily retrieve a model checkpoint:
  
  .. code-block:: python
  
      # reference can be retrieved in artifacts panel
      # "VERSION" can be a version (ex: "v2") or an alias ("latest or "best")
      checkpoint_reference = "USER/PROJECT/MODEL-RUN_ID:VERSION"
  
      # download checkpoint locally (if not already cached)
      run = wandb.init(project="MNIST")
      artifact = run.use_artifact(checkpoint_reference, type="model")
      artifact_dir = artifact.download()
  
      # load checkpoint
      model = LitModule.load_from_checkpoint(Path(artifact_dir) / "model.ckpt")
  
  **Log media**
  
  Log text with:
  
  .. code-block:: python
  
      # using columns and data
      columns = ["input", "label", "prediction"]
      data = [["cheese", "english", "english"], ["fromage", "french", "spanish"]]
      wandb_logger.log_text(key="samples", columns=columns, data=data)
  
      # using a pandas DataFrame
      wandb_logger.log_text(key="samples", dataframe=my_dataframe)
  
  Log images with:
  
  .. code-block:: python
  
      # using tensors, numpy arrays or PIL images
      wandb_logger.log_image(key="samples", images=[img1, img2])
  
      # adding captions
      wandb_logger.log_image(key="samples", images=[img1, img2], caption=["tree", "person"])
  
      # using file path
      wandb_logger.log_image(key="samples", images=["img_1.jpg", "img_2.jpg"])
  
  More arguments can be passed for logging segmentation masks and bounding boxes. Refer to
  `Image Overlays documentation <https://docs.wandb.ai/guides/track/log/media#image-overlays>`_.
  
  **Log Tables**
  
  `W&B Tables <https://docs.wandb.ai/guides/tables/visualize-tables>`_ can be used to log,
  query and analyze tabular data.
  
  They support any type of media (text, image, video, audio, molecule, html, etc) and are great for storing,
  understanding and sharing any form of data, from datasets to model predictions.
  
  .. code-block:: python
  
      columns = ["caption", "image", "sound"]
      data = [["cheese", wandb.Image(img_1), wandb.Audio(snd_1)], ["wine", wandb.Image(img_2), wandb.Audio(snd_2)]]
      wandb_logger.log_table(key="samples", columns=columns, data=data)
  
  
  **Downloading and Using Artifacts**
  
  To download an artifact without starting a run, call the ``download_artifact``
  function on the class:
  
  .. code-block:: python
  
      from lightning.pytorch.loggers import WandbLogger
  
      artifact_dir = WandbLogger.download_artifact(artifact="path/to/artifact")
  
  To download an artifact and link it to an ongoing run call the ``download_artifact``
  function on the logger instance:
  
  .. code-block:: python
  
      class MyModule(LightningModule):
          def any_lightning_module_function_or_hook(self):
              self.logger.download_artifact(artifact="path/to/artifact")
  
  To link an artifact from a previous run you can use ``use_artifact`` function:
  
  .. code-block:: python
  
      from lightning.pytorch.loggers import WandbLogger
  
      wandb_logger = WandbLogger(project="my_project", name="my_run")
      wandb_logger.use_artifact(artifact="path/to/artifact")
  
  See Also:
      - `Demo in Google Colab <http://wandb.me/lightning>`__ with hyperparameter search and model logging
      - `W&B Documentation <https://docs.wandb.ai/integrations/lightning>`__
  
  Args:
      name: Display name for the run.
      save_dir: Path where data is saved.
      version: Sets the version, mainly used to resume a previous run.
      offline: Run offline (data can be streamed later to wandb servers).
      dir: Same as save_dir.
      id: Same as version.
      anonymous: Enables or explicitly disables anonymous logging.
      project: The name of the project to which this run will belong. If not set, the environment variable
          `WANDB_PROJECT` will be used as a fallback. If both are not set, it defaults to ``'lightning_logs'``.
      log_model: Log checkpoints created by :class:`~lightning.pytorch.callbacks.ModelCheckpoint`
          as W&B artifacts. `latest` and `best` aliases are automatically set.
  
          * if ``log_model == 'all'``, checkpoints are logged during training.
          * if ``log_model == True``, checkpoints are logged at the end of training, except when
            :paramref:`~lightning.pytorch.callbacks.ModelCheckpoint.save_top_k` ``== -1``
            which also logs every checkpoint during training.
          * if ``log_model == False`` (default), no checkpoint is logged.
  
      prefix: A string to put at the beginning of metric keys.
      experiment: WandB experiment object. Automatically set when creating a run.
      checkpoint_name: Name of the model checkpoint artifact being logged.
      \**kwargs: Arguments passed to :func:`wandb.init` like `entity`, `group`, `tags`, etc.
  
  Raises:
      ModuleNotFoundError:
          If required WandB package is not installed on the device.
      MisconfigurationException:
          If both ``log_model`` and ``offline`` is set to ``True``.
class_docstr_tok: ["Log","using","`","Weights","and","Biases","<","https",":","\/\/docs.wandb.ai\/integrations\/lightning",">","`","_",".","*","*","Installation","and","set-up","*","*","Install","with","pip",":","..","code-block",":",":","bash","pip","install","wandb","Create","a","`","WandbLogger","`","instance",":","..","code-block",":",":","python","from","lightning.pytorch.loggers","import","WandbLogger","wandb_logger","=","WandbLogger","(","project=","''","MNIST","''",")","Pass","the","logger","instance","to","the","`","Trainer","`",":","..","code-block",":",":","python","trainer","=","Trainer","(","logger=wandb_logger",")","A","new","W","&","B","run","will","be","created","when","training","starts","if","you","have","not","created","one","manually","before","with","`","wandb.init","(",")","`",".","*","*","Log","metrics","*","*","Log","from",":","class",":","`","~lightning.pytorch.core.LightningModule","`",":","..","code-block",":",":","python","class","LitModule","(","LightningModule",")",":","def","training_step","(","self",",","batch",",","batch_idx",")",":","self.log","(","``","train\/loss","''",",","loss",")","Use","directly","wandb","module",":","..","code-block",":",":","python","wandb.log","(","{","``","train\/loss","''",":","loss","}",")","*","*","Log","hyper-parameters","*","*","Save",":","class",":","`","~lightning.pytorch.core.LightningModule","`","parameters",":","..","code-block",":",":","python","class","LitModule","(","LightningModule",")",":","def","__init__","(","self",",","*","args",",","*","*","kwarg",")",":","self.save_hyperparameters","(",")","Add","other","config","parameters",":","..","code-block",":",":","python","#","add","one","parameter","wandb_logger.experiment.config","[","``","key","''","]","=","value","#","add","multiple","parameters","wandb_logger.experiment.config.update","(","{","key1",":","val1",",","key2",":","val2","}",")","#","use","directly","wandb","module","wandb.config","[","``","key","''","]","=","value","wandb.config.update","(",")","*","*","Log","gradients",",","parameters","and","model","topology","*","*","Call","the","`","watch","`","method","for","automatically","tracking","gradients",":","..","code-block",":",":","python","#","log","gradients","and","model","topology","wandb_logger.watch","(","model",")","#","log","gradients",",","parameter","histogram","and","model","topology","wandb_logger.watch","(","model",",","log=","''","all","''",")","#","change","log","frequency","of","gradients","and","parameters","(","100","steps","by","default",")","wandb_logger.watch","(","model",",","log_freq=500",")","#","do","not","log","graph","(","in","case","of","errors",")","wandb_logger.watch","(","model",",","log_graph=False",")","The","`","watch","`","method","adds","hooks","to","the","model","which","can","be","removed","at","the","end","of","training",":","..","code-block",":",":","python","wandb_logger.experiment.unwatch","(","model",")","*","*","Log","model","checkpoints","*","*","Log","model","checkpoints","at","the","end","of","training",":","..","code-block",":",":","python","wandb_logger","=","WandbLogger","(","log_model=True",")","Log","model","checkpoints","as","they","get","created","during","training",":","..","code-block",":",":","python","wandb_logger","=","WandbLogger","(","log_model=","''","all","''",")","Custom","checkpointing","can","be","set","up","through",":","class",":","`","~lightning.pytorch.callbacks.ModelCheckpoint","`",":","..","code-block",":",":","python","#","log","model","only","if","`","val_accuracy","`","increases","wandb_logger","=","WandbLogger","(","log_model=","''","all","''",")","checkpoint_callback","=","ModelCheckpoint","(","monitor=","''","val_accuracy","''",",","mode=","''","max","''",")","trainer","=","Trainer","(","logger=wandb_logger",",","callbacks=","[","checkpoint_callback","]",")","`","latest","`","and","`","best","`","aliases","are","automatically","set","to","easily","retrieve","a","model","checkpoint",":","..","code-block",":",":","python","#","reference","can","be","retrieved","in","artifacts","panel","#","``","VERSION","''","can","be","a","version","(","ex",":","``","v2","''",")","or","an","alias","(","``","latest","or","``","best","''",")","checkpoint_reference","=","``","USER\/PROJECT\/MODEL-RUN_ID",":","VERSION","''","#","download","checkpoint","locally","(","if","not","already","cached",")","run","=","wandb.init","(","project=","''","MNIST","''",")","artifact","=","run.use_artifact","(","checkpoint_reference",",","type=","''","model","''",")","artifact_dir","=","artifact.download","(",")","#","load","checkpoint","model","=","LitModule.load_from_checkpoint","(","Path","(","artifact_dir",")","\/","``","model.ckpt","''",")","*","*","Log","media","*","*","Log","text","with",":","..","code-block",":",":","python","#","using","columns","and","data","columns","=","[","``","input","''",",","``","label","''",",","``","prediction","''","]","data","=","[","[","``","cheese","''",",","``","english","''",",","``","english","''","]",",","[","``","fromage","''",",","``","french","''",",","``","spanish","''","]","]","wandb_logger.log_text","(","key=","''","samples","''",",","columns=columns",",","data=data",")","#","using","a","pandas","DataFrame","wandb_logger.log_text","(","key=","''","samples","''",",","dataframe=my_dataframe",")","Log","images","with",":","..","code-block",":",":","python","#","using","tensors",",","numpy","arrays","or","PIL","images","wandb_logger.log_image","(","key=","''","samples","''",",","images=","[","img1",",","img2","]",")","#","adding","captions","wandb_logger.log_image","(","key=","''","samples","''",",","images=","[","img1",",","img2","]",",","caption=","[","``","tree","''",",","``","person","''","]",")","#","using","file","path","wandb_logger.log_image","(","key=","''","samples","''",",","images=","[","``","img_1.jpg","''",",","``","img_2.jpg","''","]",")","More","arguments","can","be","passed","for","logging","segmentation","masks","and","bounding","boxes",".","Refer","to","`","Image","Overlays","documentation","<","https",":","\/\/docs.wandb.ai\/guides\/track\/log\/media","#","image-overlays",">","`","_",".","*","*","Log","Tables","*","*","`","W","&","B","Tables","<","https",":","\/\/docs.wandb.ai\/guides\/tables\/visualize-tables",">","`","_","can","be","used","to","log",",","query","and","analyze","tabular","data",".","They","support","any","type","of","media","(","text",",","image",",","video",",","audio",",","molecule",",","html",",","etc",")","and","are","great","for","storing",",","understanding","and","sharing","any","form","of","data",",","from","datasets","to","model","predictions",".","..","code-block",":",":","python","columns","=","[","``","caption","''",",","``","image","''",",","``","sound","''","]","data","=","[","[","``","cheese","''",",","wandb.Image","(","img_1",")",",","wandb.Audio","(","snd_1",")","]",",","[","``","wine","''",",","wandb.Image","(","img_2",")",",","wandb.Audio","(","snd_2",")","]","]","wandb_logger.log_table","(","key=","''","samples","''",",","columns=columns",",","data=data",")","*","*","Downloading","and","Using","Artifacts","*","*","To","download","an","artifact","without","starting","a","run",",","call","the","``","download_artifact","``","function","on","the","class",":","..","code-block",":",":","python","from","lightning.pytorch.loggers","import","WandbLogger","artifact_dir","=","WandbLogger.download_artifact","(","artifact=","''","path\/to\/artifact","''",")","To","download","an","artifact","and","link","it","to","an","ongoing","run","call","the","``","download_artifact","``","function","on","the","logger","instance",":","..","code-block",":",":","python","class","MyModule","(","LightningModule",")",":","def","any_lightning_module_function_or_hook","(","self",")",":","self.logger.download_artifact","(","artifact=","''","path\/to\/artifact","''",")","To","link","an","artifact","from","a","previous","run","you","can","use","``","use_artifact","``","function",":","..","code-block",":",":","python","from","lightning.pytorch.loggers","import","WandbLogger","wandb_logger","=","WandbLogger","(","project=","''","my_project","''",",","name=","''","my_run","''",")","wandb_logger.use_artifact","(","artifact=","''","path\/to\/artifact","''",")","See","Also",":","-","`","Demo","in","Google","Colab","<","http",":","\/\/wandb.me\/lightning",">","`","__","with","hyperparameter","search","and","model","logging","-","`","W","&","B","Documentation","<","https",":","\/\/docs.wandb.ai\/integrations\/lightning",">","`","__","Args",":","name",":","Display","name","for","the","run",".","save_dir",":","Path","where","data","is","saved",".","version",":","Sets","the","version",",","mainly","used","to","resume","a","previous","run",".","offline",":","Run","offline","(","data","can","be","streamed","later","to","wandb","servers",")",".","dir",":","Same","as","save_dir",".","id",":","Same","as","version",".","anonymous",":","Enables","or","explicitly","disables","anonymous","logging",".","project",":","The","name","of","the","project","to","which","this","run","will","belong",".","If","not","set",",","the","environment","variable","`","WANDB_PROJECT","`","will","be","used","as","a","fallback",".","If","both","are","not","set",",","it","defaults","to","``","'lightning_logs","'","``",".","log_model",":","Log","checkpoints","created","by",":","class",":","`","~lightning.pytorch.callbacks.ModelCheckpoint","`","as","W","&","B","artifacts",".","`","latest","`","and","`","best","`","aliases","are","automatically","set",".","*","if","``","log_model","==","'all","'","``",",","checkpoints","are","logged","during","training",".","*","if","``","log_model","==","True","``",",","checkpoints","are","logged","at","the","end","of","training",",","except","when",":","paramref",":","`","~lightning.pytorch.callbacks.ModelCheckpoint.save_top_k","`","``","==","-1","``","which","also","logs","every","checkpoint","during","training",".","*","if","``","log_model","==","False","``","(","default",")",",","no","checkpoint","is","logged",".","prefix",":","A","string","to","put","at","the","beginning","of","metric","keys",".","experiment",":","WandB","experiment","object",".","Automatically","set","when","creating","a","run",".","checkpoint_name",":","Name","of","the","model","checkpoint","artifact","being","logged",".","\\","*","*","kwargs",":","Arguments","passed","to",":","func",":","`","wandb.init","`","like","`","entity","`",",","`","group","`",",","`","tags","`",",","etc",".","Raises",":","ModuleNotFoundError",":","If","required","WandB","package","is","not","installed","on","the","device",".","MisconfigurationException",":","If","both","``","log_model","``","and","``","offline","``","is","set","to","``","True","``","."]
code: def __init__(
      self,
      name: Optional[str] = None,
      save_dir: _PATH = ".",
      version: Optional[str] = None,
      offline: bool = False,
      dir: Optional[_PATH] = None,
      id: Optional[str] = None,
      anonymous: Optional[bool] = None,
      project: Optional[str] = None,
      log_model: Union[Literal["all"], bool] = False,
      experiment: Union["Run", "RunDisabled", None] = None,
      prefix: str = "",
      checkpoint_name: Optional[str] = None,
      **kwargs: Any,
  ) -> None:
      if not _WANDB_AVAILABLE:
          raise ModuleNotFoundError(str(_WANDB_AVAILABLE))
  
      if offline and log_model:
          raise MisconfigurationException(
              f"Providing log_model={log_model} and offline={offline} is an invalid configuration"
              " since model checkpoints cannot be uploaded in offline mode.\n"
              "Hint: Set `offline=False` to log your model."
          )
  
      super().__init__()
      self._offline = offline
      self._log_model = log_model
      self._prefix = prefix
      self._experiment = experiment
      self._logged_model_time: Dict[str, float] = {}
      self._checkpoint_callback: Optional[ModelCheckpoint] = None
  
      # paths are processed as strings
      if save_dir is not None:
          save_dir = os.fspath(save_dir)
      elif dir is not None:
          dir = os.fspath(dir)
  
      project = project or os.environ.get(
          "WANDB_PROJECT", "lightning_logs"
      )
  
      # set wandb init arguments
      self._wandb_init: Dict[str, Any] = {
          "name": name,
          "project": project,
          "dir": save_dir or dir,
          "id": version or id,
          "resume": "allow",
          "anonymous": ("allow" if anonymous else None),
      }
      self._wandb_init.update(**kwargs)
      # extract parameters
      self._project = self._wandb_init.get("project")
      self._save_dir = self._wandb_init.get("dir")
      self._name = self._wandb_init.get("name")
      self._id = self._wandb_init.get("id")
      self._checkpoint_name = checkpoint_name
  
imports: import os
  from argparse import Namespace
  from pathlib import Path
  from typing import TYPE_CHECKING, Any, Dict, List, Literal, Mapping, Optional, Union
  import torch.nn
  from lightning_utilities.core.imports import RequirementCache
  from torch import Tensor
  from typing_extensions import override
  from lightning.fabric.utilities.logger import _add_prefix, _convert_params, _sanitize_callable_params
  from lightning.fabric.utilities.types import _PATH
  from lightning.pytorch.callbacks.model_checkpoint import ModelCheckpoint
  from lightning.pytorch.loggers.logger import Logger, rank_zero_experiment
  from lightning.pytorch.loggers.utilities import _scan_checkpoints
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.rank_zero import rank_zero_only, rank_zero_warn
label_desc: Use image node_id 1 to create a new WandbLogger object from inherited base classes: Logger with example: obj = WandbLogger(name, save_dir, version, offline, dir, id, anonymous, project, log_model, experiment, prefix, checkpoint_name)