prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  W&B: Allow for passing experiment into the WandbLogger (and logging semantics)
  Currently, the WandbLogger will automatically create a new internal experiment (run) whenever you create a new WandbLogger.
  
  # Issue 
  
  If I instantiate a wandb experiment outside of the logger, then I will have two experiments when I train my model since there is no way to set the internal experiment of the WandbLogger to my current external experiment.
  
  # Potential Solution
  Allow for passing an experiment into the WandbLogger:
  
  ```
  class WandbLogger(LightningLoggerBase):
      def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False,
                   version=None, project=None, tags=None, experiment=None):
                .
                .
                .
                self._experiment = experiment
  ```
  Then I can do this:
  
  ```
  experiment = wandb.init(.......)
  wandb_logger = WandbLogger(experiment=experiment)
  ```
  I made this change locally, however, I wasn't sure if this was something you also wanted to implement as well. It works for me.
  
  # Another small note
  
  In the `WandbLogger.log_metrics` function, I would change:
  
  `self.experiment.history.add(metrics)` --> `self.experiment.log(metrics)`
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `pytorch_lightning/logging/wandb.py`
  Content:
  ```
  1 import os
  2 
  3 try:
  4     import wandb
  5 except ImportError:
  6     raise ImportError('Missing wandb package.')
  7 
  8 from .base import LightningLoggerBase, rank_zero_only
  9 
  10 
  11 class WandbLogger(LightningLoggerBase):
  12     """
  13     Logger for W&B.
  14 
  15     Args:
  16         name (str): display name for the run.
  17         save_dir (str): path where data is saved.
  18         offline (bool): run offline (data can be streamed later to wandb servers).
  19         id or version (str): sets the version, mainly used to resume a previous run.
  20         anonymous (bool): enables or explicitly disables anonymous logging.
  21         project (str): the name of the project to which this run will belong.
  22         tags (list of str): tags associated with this run.
  23 
  24     Example
  25     --------
  26     .. code-block:: python
  27 
  28         from pytorch_lightning.logging import WandbLogger
  29         from pytorch_lightning import Trainer
  30 
  31         wandb_logger = WandbLogger()
  32         trainer = Trainer(logger=wandb_logger)
  33     """
  34 
  35     def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False,
  36                  version=None, project=None, tags=None):
  37         super().__init__()
  38         self._name = name
  39         self._save_dir = save_dir
  40         self._anonymous = "allow" if anonymous else None
  41         self._id = version or id
  42         self._tags = tags
  43         self._project = project
  44         self._experiment = None
  45         self._offline = offline
  46 
  47     def __getstate__(self):
  48         state = self.__dict__.copy()
  49         # cannot be pickled
  50         state['_experiment'] = None
  51         # args needed to reload correct experiment
  52         state['_id'] = self.experiment.id
  53         return state
  54 
  55     @property
  56     def experiment(self):
  57         r"""
  58 
  59           Actual wandb object. To use wandb features do the following.
  60 
  61           Example::
  62 
  63               self.logger.experiment.some_wandb_function()
  64 
  65           """
  66         if self._experiment is None:
  67             if self._offline:
  68                 os.environ["WANDB_MODE"] = "dryrun"
  69             self._experiment = wandb.init(
  70                 name=self._name, dir=self._save_dir, project=self._project, anonymous=self._anonymous,
  71                 id=self._id, resume="allow", tags=self._tags)
  72         return self._experiment
  73 
  74     def watch(self, model, log="gradients", log_freq=100):
  75         wandb.watch(model, log, log_freq)
  76 
  77     @rank_zero_only
  78     def log_hyperparams(self, params):
  79         self.experiment.config.update(params)
  80 
  81     @rank_zero_only
  82     def log_metrics(self, metrics, step=None):
  83         metrics["global_step"] = step
  84         self.experiment.history.add(metrics)
  85 
  86     def save(self):
  87         pass
  88 
  89     @rank_zero_only
  90     def finalize(self, status='success'):
  91         try:
  92             exit_code = 0 if status == 'success' else 1
  93             wandb.join(exit_code)
  94         except TypeError:
  95             wandb.join()
  96 
  97     @property
  98     def name(self):
  99         return self.experiment.project_name()
  100 
  101     @property
  102     def version(self):
  103         return self.experiment.id
  104
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/pytorch_lightning/logging/wandb.py b/pytorch_lightning/logging/wandb.py
  --- a/pytorch_lightning/logging/wandb.py
  +++ b/pytorch_lightning/logging/wandb.py
  @@ -33,7 +33,7 @@
       """
   
       def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False,
  -                 version=None, project=None, tags=None):
  +                 version=None, project=None, tags=None, experiment=None):
           super().__init__()
           self._name = name
           self._save_dir = save_dir
  @@ -41,7 +41,7 @@
           self._id = version or id
           self._tags = tags
           self._project = project
  -        self._experiment = None
  +        self._experiment = experiment
           self._offline = offline
   
       def __getstate__(self):
  @@ -81,7 +81,7 @@
       @rank_zero_only
       def log_metrics(self, metrics, step=None):
           metrics["global_step"] = step
  -        self.experiment.history.add(metrics)
  +        self.experiment.log(metrics)
   
       def save(self):
           pass
  
verification_info: {"golden_diff": "diff --git a/pytorch_lightning/logging/wandb.py b/pytorch_lightning/logging/wandb.py\n--- a/pytorch_lightning/logging/wandb.py\n+++ b/pytorch_lightning/logging/wandb.py\n@@ -33,7 +33,7 @@\n     \"\"\"\n \n     def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False,\n-                 version=None, project=None, tags=None):\n+                 version=None, project=None, tags=None, experiment=None):\n         super().__init__()\n         self._name = name\n         self._save_dir = save_dir\n@@ -41,7 +41,7 @@\n         self._id = version or id\n         self._tags = tags\n         self._project = project\n-        self._experiment = None\n+        self._experiment = experiment\n         self._offline = offline\n \n     def __getstate__(self):\n@@ -81,7 +81,7 @@\n     @rank_zero_only\n     def log_metrics(self, metrics, step=None):\n         metrics[\"global_step\"] = step\n-        self.experiment.history.add(metrics)\n+        self.experiment.log(metrics)\n \n     def save(self):\n         pass\n", "issue": "W&B: Allow for passing experiment into the WandbLogger (and logging semantics)\nCurrently, the WandbLogger will automatically create a new internal experiment (run) whenever you create a new WandbLogger.\r\n\r\n# Issue \r\n\r\nIf I instantiate a wandb experiment outside of the logger, then I will have two experiments when I train my model since there is no way to set the internal experiment of the WandbLogger to my current external experiment.\r\n\r\n# Potential Solution\r\nAllow for passing an experiment into the WandbLogger:\r\n\r\n```\r\nclass WandbLogger(LightningLoggerBase):\r\n    def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False,\r\n                 version=None, project=None, tags=None, experiment=None):\r\n              .\r\n              .\r\n              .\r\n              self._experiment = experiment\r\n```\r\nThen I can do this:\r\n\r\n```\r\nexperiment = wandb.init(.......)\r\nwandb_logger = WandbLogger(experiment=experiment)\r\n```\r\nI made this change locally, however, I wasn't sure if this was something you also wanted to implement as well. It works for me.\r\n\r\n# Another small note\r\n\r\nIn the `WandbLogger.log_metrics` function, I would change:\r\n\r\n`self.experiment.history.add(metrics)` --> `self.experiment.log(metrics)`\n", "before_files": [{"content": "import os\n\ntry:\n    import wandb\nexcept ImportError:\n    raise ImportError('Missing wandb package.')\n\nfrom .base import LightningLoggerBase, rank_zero_only\n\n\nclass WandbLogger(LightningLoggerBase):\n    \"\"\"\n    Logger for W&B.\n\n    Args:\n        name (str): display name for the run.\n        save_dir (str): path where data is saved.\n        offline (bool): run offline (data can be streamed later to wandb servers).\n        id or version (str): sets the version, mainly used to resume a previous run.\n        anonymous (bool): enables or explicitly disables anonymous logging.\n        project (str): the name of the project to which this run will belong.\n        tags (list of str): tags associated with this run.\n\n    Example\n    --------\n    .. code-block:: python\n\n        from pytorch_lightning.logging import WandbLogger\n        from pytorch_lightning import Trainer\n\n        wandb_logger = WandbLogger()\n        trainer = Trainer(logger=wandb_logger)\n    \"\"\"\n\n    def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False,\n                 version=None, project=None, tags=None):\n        super().__init__()\n        self._name = name\n        self._save_dir = save_dir\n        self._anonymous = \"allow\" if anonymous else None\n        self._id = version or id\n        self._tags = tags\n        self._project = project\n        self._experiment = None\n        self._offline = offline\n\n    def __getstate__(self):\n        state = self.__dict__.copy()\n        # cannot be pickled\n        state['_experiment'] = None\n        # args needed to reload correct experiment\n        state['_id'] = self.experiment.id\n        return state\n\n    @property\n    def experiment(self):\n        r\"\"\"\n\n          Actual wandb object. To use wandb features do the following.\n\n          Example::\n\n              self.logger.experiment.some_wandb_function()\n\n          \"\"\"\n        if self._experiment is None:\n            if self._offline:\n                os.environ[\"WANDB_MODE\"] = \"dryrun\"\n            self._experiment = wandb.init(\n                name=self._name, dir=self._save_dir, project=self._project, anonymous=self._anonymous,\n                id=self._id, resume=\"allow\", tags=self._tags)\n        return self._experiment\n\n    def watch(self, model, log=\"gradients\", log_freq=100):\n        wandb.watch(model, log, log_freq)\n\n    @rank_zero_only\n    def log_hyperparams(self, params):\n        self.experiment.config.update(params)\n\n    @rank_zero_only\n    def log_metrics(self, metrics, step=None):\n        metrics[\"global_step\"] = step\n        self.experiment.history.add(metrics)\n\n    def save(self):\n        pass\n\n    @rank_zero_only\n    def finalize(self, status='success'):\n        try:\n            exit_code = 0 if status == 'success' else 1\n            wandb.join(exit_code)\n        except TypeError:\n            wandb.join()\n\n    @property\n    def name(self):\n        return self.experiment.project_name()\n\n    @property\n    def version(self):\n        return self.experiment.id\n", "path": "pytorch_lightning/logging/wandb.py"}], "after_files": [{"content": "import os\n\ntry:\n    import wandb\nexcept ImportError:\n    raise ImportError('Missing wandb package.')\n\nfrom .base import LightningLoggerBase, rank_zero_only\n\n\nclass WandbLogger(LightningLoggerBase):\n    \"\"\"\n    Logger for W&B.\n\n    Args:\n        name (str): display name for the run.\n        save_dir (str): path where data is saved.\n        offline (bool): run offline (data can be streamed later to wandb servers).\n        id or version (str): sets the version, mainly used to resume a previous run.\n        anonymous (bool): enables or explicitly disables anonymous logging.\n        project (str): the name of the project to which this run will belong.\n        tags (list of str): tags associated with this run.\n\n    Example\n    --------\n    .. code-block:: python\n\n        from pytorch_lightning.logging import WandbLogger\n        from pytorch_lightning import Trainer\n\n        wandb_logger = WandbLogger()\n        trainer = Trainer(logger=wandb_logger)\n    \"\"\"\n\n    def __init__(self, name=None, save_dir=None, offline=False, id=None, anonymous=False,\n                 version=None, project=None, tags=None, experiment=None):\n        super().__init__()\n        self._name = name\n        self._save_dir = save_dir\n        self._anonymous = \"allow\" if anonymous else None\n        self._id = version or id\n        self._tags = tags\n        self._project = project\n        self._experiment = experiment\n        self._offline = offline\n\n    def __getstate__(self):\n        state = self.__dict__.copy()\n        # cannot be pickled\n        state['_experiment'] = None\n        # args needed to reload correct experiment\n        state['_id'] = self.experiment.id\n        return state\n\n    @property\n    def experiment(self):\n        r\"\"\"\n\n          Actual wandb object. To use wandb features do the following.\n\n          Example::\n\n              self.logger.experiment.some_wandb_function()\n\n          \"\"\"\n        if self._experiment is None:\n            if self._offline:\n                os.environ[\"WANDB_MODE\"] = \"dryrun\"\n            self._experiment = wandb.init(\n                name=self._name, dir=self._save_dir, project=self._project, anonymous=self._anonymous,\n                id=self._id, resume=\"allow\", tags=self._tags)\n        return self._experiment\n\n    def watch(self, model, log=\"gradients\", log_freq=100):\n        wandb.watch(model, log, log_freq)\n\n    @rank_zero_only\n    def log_hyperparams(self, params):\n        self.experiment.config.update(params)\n\n    @rank_zero_only\n    def log_metrics(self, metrics, step=None):\n        metrics[\"global_step\"] = step\n        self.experiment.log(metrics)\n\n    def save(self):\n        pass\n\n    @rank_zero_only\n    def finalize(self, status='success'):\n        try:\n            exit_code = 0 if status == 'success' else 1\n            wandb.join(exit_code)\n        except TypeError:\n            wandb.join()\n\n    @property\n    def name(self):\n        return self.experiment.project_name()\n\n    @property\n    def version(self):\n        return self.experiment.id\n", "path": "pytorch_lightning/logging/wandb.py"}]}