
      i[Z                     N   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZmZ ddlmZ ddlmZ dd	lmZ ddlZdd
lmZmZmZ ddlmZ ddlmZm Z  ddl!m"Z" ddl#m$Z$ erddl%m&Z& ddl'm(Z(  ejR                  e*      Z+ ed      Z,dZ-dedefdZ. G d de      Z/y)z
Neptune Logger
--------------
    N)	Namespace)	Generator)wraps)TYPE_CHECKINGAnyCallableOptionalUnion)RequirementCache)Tensor)override)_add_prefix_convert_params_sanitize_callable_params)
Checkpoint)Loggerrank_zero_experiment)ModelSummary)rank_zero_onlyRunHandlerzneptune>=1.0z*source_code/integrations/pytorch-lightningfuncreturnc                 T     t               dt        dt        dt        f fd       }|S )Nargskwargsr   c                  v    ddl m} t        j                  |      5   | i |cd d d        S # 1 sw Y   y xY w)Nr   )InactiveRunException)neptune.exceptionsr    
contextlibsuppress)r   r   r    r   s      v/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pytorch_lightning/loggers/neptune.pywrapperz _catch_inactive.<locals>.wrapper8   s.    ;  !56(( 766s   /8)r   r   )r   r%   s   ` r$   _catch_inactiver&   7   s5    
4[)s )c )c ) ) N    c                   f    e Zd ZdZdZdZdZdddddddd	ee   d
ee   dee   dee	d      dee
   dedef fdZd5dZedefd       ZdedefdZed	ee   d
ee   dee   dee	d      deddfd       Zdeeef   fdZdeeef   ddfdZeed6d              Zeed6d              Zeeede	eeef   ef   ddfd                     Zeeed7deee	eef   f   d ee    ddfd!                     Z!eeed"eddf fd#                     Z"eedee   fd$              Z#eed8d%d&d'e ddfd(              Z$eeed)e%ddfd*                     Z&ed+ed)e%defd,       Z'e(d-eeef   d.ede)e   fd/       Z*e(d7d0eeef   d1ee   de+fd2       Z,eedee   fd3              Z-eedee   fd4              Z. xZ/S )9NeptuneLoggera  Log using `Neptune <https://docs.neptune.ai/integrations/lightning/>`_.

    Install it with pip:

    .. code-block:: bash

        pip install neptune

    or conda:

    .. code-block:: bash

        conda install -c conda-forge neptune-client

    **Quickstart**

    Pass a NeptuneLogger instance to the Trainer to log metadata with Neptune:

    .. code-block:: python


        from pytorch_lightning import Trainer
        from pytorch_lightning.loggers import NeptuneLogger
        import neptune

        neptune_logger = NeptuneLogger(
            api_key=neptune.ANONYMOUS_API_TOKEN,  # replace with your own
            project="common/pytorch-lightning-integration",  # format "workspace-name/project-name"
            tags=["training", "resnet"],  # optional
        )
        trainer = Trainer(max_epochs=10, logger=neptune_logger)

    **How to use NeptuneLogger?**

    Use the logger anywhere in your :class:`~pytorch_lightning.core.LightningModule` as follows:

    .. code-block:: python

        from neptune.types import File
        from pytorch_lightning import LightningModule


        class LitModel(LightningModule):
            def training_step(self, batch, batch_idx):
                # log metrics
                acc = ...
                self.append("train/loss", loss)

            def any_lightning_module_function_or_hook(self):
                # log images
                img = ...
                self.logger.experiment["train/misclassified_images"].append(File.as_image(img))

                # generic recipe
                metadata = ...
                self.logger.experiment["your/metadata/structure"] = metadata

    Note that the syntax ``self.logger.experiment["your/metadata/structure"].append(metadata)`` is specific to
    Neptune and extends the logger capabilities. It lets you log various types of metadata, such as
    scores, files, images, interactive visuals, and CSVs.
    Refer to the `Neptune docs <https://docs.neptune.ai/logging/methods>`_
    for details.
    You can also use the regular logger methods ``log_metrics()``, and ``log_hyperparams()`` with NeptuneLogger.

    **Log after fitting or testing is finished**

    You can log objects after the fitting or testing methods are finished:

    .. code-block:: python

        neptune_logger = NeptuneLogger(project="common/pytorch-lightning-integration")

        trainer = pl.Trainer(logger=neptune_logger)
        model = ...
        datamodule = ...
        trainer.fit(model, datamodule=datamodule)
        trainer.test(model, datamodule=datamodule)

        # Log objects after `fit` or `test` methods
        # model summary
        neptune_logger.log_model_summary(model=model, max_depth=-1)

        # generic recipe
        metadata = ...
        neptune_logger.experiment["your/metadata/structure"] = metadata

    **Log model checkpoints**

    If you have :class:`~pytorch_lightning.callbacks.ModelCheckpoint` configured,
    the Neptune logger automatically logs model checkpoints.
    Model weights will be uploaded to the "model/checkpoints" namespace in the Neptune run.
    You can disable this option with:

    .. code-block:: python

        neptune_logger = NeptuneLogger(log_model_checkpoints=False)

    **Pass additional parameters to the Neptune run**

    You can also pass ``neptune_run_kwargs`` to add details to the run, like ``tags`` or ``description``:

    .. testcode::
        :skipif: not _NEPTUNE_AVAILABLE

        from pytorch_lightning import Trainer
        from pytorch_lightning.loggers import NeptuneLogger

        neptune_logger = NeptuneLogger(
            project="common/pytorch-lightning-integration",
            name="lightning-run",
            description="mlp quick run with pytorch-lightning",
            tags=["mlp", "quick-run"],
        )
        trainer = Trainer(max_epochs=3, logger=neptune_logger)

    Check `run documentation <https://docs.neptune.ai/api/neptune/#init_run>`_
    for more info about additional run parameters.

    **Details about Neptune run structure**

    Runs can be viewed as nested dictionary-like structures that you can define in your code.
    Thanks to this you can easily organize your metadata in a way that is most convenient for you.

    The hierarchical structure that you apply to your metadata is reflected in the Neptune web app.

    See also:
        - Read about
          `what objects you can log to Neptune <https://docs.neptune.ai/logging/what_you_can_log/>`_.
        - Check out an `example run <https://app.neptune.ai/o/common/org/pytorch-lightning-integration/e/PTL-1/all>`_
          with multiple types of metadata logged.
        - For more detailed examples, see the
          `user guide <https://docs.neptune.ai/integrations/lightning/>`_.

    Args:
        api_key: Optional.
            Neptune API token, found on https://www.neptune.ai upon registration.
            You should save your token to the `NEPTUNE_API_TOKEN`
            environment variable and leave the api_key argument out of your code.
            Instructions: `Setting your API token <https://docs.neptune.ai/setup/setting_api_token/>`_.
        project: Optional.
            Name of a project in the form "workspace-name/project-name", for example "tom/mask-rcnn".
            If ``None``, the value of `NEPTUNE_PROJECT` environment variable is used.
            You need to create the project on https://www.neptune.ai first.
        name: Optional. Editable name of the run.
            The run name is displayed in the Neptune web app.
        run: Optional. Default is ``None``. A Neptune ``Run`` object.
            If specified, this existing run will be used for logging, instead of a new run being created.
            You can also pass a namespace handler object; for example, ``run["test"]``, in which case all
            metadata is logged under the "test" namespace inside the run.
        log_model_checkpoints: Optional. Default is ``True``. Log model checkpoint to Neptune.
            Works only if ``ModelCheckpoint`` is passed to the ``Trainer``.
        prefix: Optional. Default is ``"training"``. Root namespace for all metadata logging.
        \**neptune_run_kwargs: Additional arguments like ``tags``, ``description``, ``capture_stdout``, etc.
            used when a run is created.

    Raises:
        ModuleNotFoundError:
            If the required Neptune package is not installed.
        ValueError:
            If an argument passed to the logger's constructor is incorrect.

    /hyperparams	artifactsNTtraining)api_keyprojectnamerunlog_model_checkpointsprefixr.   r/   r0   r1   )r   r   r2   r3   neptune_run_kwargsc                   t         st        t        t                     | j                  |||||       t        
|           || _        || _        || _        || _	        || _
        || _        || _        d | _        | j                  V| j                          ddlm} | j                  }	t#        |	|      r|	j%                         }	t&        j(                  |	t*        <   y y )Nr   r   )_NEPTUNE_AVAILABLEModuleNotFoundErrorstr_verify_input_argumentssuper__init___log_model_checkpoints_prefix	_run_name_project_name_api_key_run_instance_neptune_run_kwargs_run_short_id_retrieve_run_dataneptune.handlerr   
isinstanceget_root_objectpl__version___INTEGRATION_VERSION_KEY)selfr.   r/   r0   r1   r2   r3   r4   r   root_obj	__class__s             r$   r;   zNeptuneLogger.__init__   s     "%c*<&=>> 	$$WgtSBTU&;#$ #5 ,0)##%/ ))H(G,#33513H-. *r'   r   c                 <   ddl m} | j                  J | j                  }t        ||      r|j	                         }|j                          |j                  d      r1|d   j                         | _        |d   j                         | _	        y d| _        d| _	        y )Nr   r   zsys/idzsys/nameOFFLINEzoffline-name)
rE   r   rA   rF   rG   waitexistsfetchrC   r>   )rK   r   rL   s      r$   rD   z NeptuneLogger._retrieve_run_data  s    +!!---%%h(//1H??8$!)(!3!9!9!;D%j1779DN!*D+DNr'   c                    i }t        j                  t              5  | j                  }d d d        | j                  | j                  |d<   | j
                  | j
                  |d<   | j                  | j                  |d<   t        j                  t              5  | j                  | j                  |d<   d d d        |S # 1 sw Y   xY w# 1 sw Y   |S xY w)Nr/   	api_tokenr1   r0   )r"   r#   AttributeErrorrB   r?   r@   rC   r>   )rK   r   s     r$   _neptune_init_argsz NeptuneLogger._neptune_init_args!  s      0++D 1 )"00DO==$ $D),,DK   0~~)#~~V 1 # 10 1 s   CCC
Ckeysc                     | j                   r(| j                  j                  | j                   g|      S | j                  j                  |      S )zXReturn sequence of keys joined by `LOGGER_JOIN_CHAR`, started with `_prefix` if defined.)r=   LOGGER_JOIN_CHARjoin)rK   rW   s     r$   _construct_path_with_prefixz)NeptuneLogger._construct_path_with_prefix8  sC    <<((--t||.Cd.CDD$$))$//r'   c                     ddl m} ddlm} |t	        |||f      st        d      t        d | ||fD              xs |}||rt        d      y y )Nr   r   r   zQRun parameter expected to be of type `neptune.Run`, or `neptune.handler.Handler`.c              3   $   K   | ]  }|d u 
 y wN ).0args     r$   	<genexpr>z8NeptuneLogger._verify_input_arguments.<locals>.<genexpr>N  s     )^E]c#T/E]s   zlWhen an already initialized run object is provided, you can't provide other `neptune.init_run()` parameters.)neptuner   rE   r   rF   
ValueErrorany)r.   r/   r0   r1   r4   r   r   any_neptune_init_arg_passeds           r$   r9   z%NeptuneLogger._verify_input_arguments>  sl     	 + ?:cC>#Bpqq '*)^gwX\E])^&^&tbt#?:   ;?r'   c                 D    | j                   j                         }d |d<   |S )NrA   )__dict__copy)rK   states     r$   __getstate__zNeptuneLogger.__getstate__U  s#    ""$!%or'   rj   c                 \    dd l }|| _         |j                  di | j                  | _        y Nr   r_   )rc   rh   init_runrV   rA   )rK   rj   rc   s      r$   __setstate__zNeptuneLogger.__setstate__[  s*    -W--H0G0GHr'   c                     | j                   S )aK  Actual Neptune run object. Allows you to use neptune logging features in your
        :class:`~pytorch_lightning.core.LightningModule`.

        Example::

            class LitModel(LightningModule):
                def training_step(self, batch, batch_idx):
                    # log metrics
                    acc = ...
                    self.logger.experiment["train/acc"].append(acc)

                    # log images
                    img = ...
                    self.logger.experiment["train/misclassified_images"].append(File.as_image(img))

        Note that the syntax ``self.logger.experiment["your/metadata/structure"].append(metadata)``
        is specific to Neptune and extends the logger capabilities.
        It lets you log various types of metadata, such as scores, files,
        images, interactive visuals, and CSVs. Refer to the
        `Neptune docs <https://docs.neptune.ai/logging/methods>`_
        for more detailed explanations.
        You can also use the regular logger methods ``log_metrics()``, and ``log_hyperparams()``
        with NeptuneLogger.

        )r1   rK   s    r$   
experimentzNeptuneLogger.experimenta  s    8 xxr'   c                     dd l }| j                  sR |j                  di | j                  | _        | j	                          t
        j                  | j                  t        <   | j                  S rm   )rc   rA   rn   rV   rD   rH   rI   rJ   )rK   rc   s     r$   r1   zNeptuneLogger.run  s[     	!!!1!1!1!LD4K4K!LD##%;=>>D78!!!r'   paramsc                     ddl m} t        |      }t        |      }| j                  }| j                  |      } ||      | j                  |<   y)a  Log hyperparameters to the run.

        Hyperparameters will be logged under the "<prefix>/hyperparams" namespace.

        Note:

            You can also log parameters by directly using the logger instance:
            ``neptune_logger.experiment["model/hyper-parameters"] = params_dict``.

            In this way you can keep hierarchical structure of the parameters.

        Args:
            params: `dict`.
                Python dictionary structure with parameters.

        Example::

            from pytorch_lightning.loggers import NeptuneLogger
            import neptune

            PARAMS = {
                "batch_size": 64,
                "lr": 0.07,
                "decay_factor": 0.97,
            }

            neptune_logger = NeptuneLogger(
                api_key=neptune.ANONYMOUS_API_TOKEN,
                project="common/pytorch-lightning-integration"
            )

            neptune_logger.log_hyperparams(PARAMS)

        r   )stringify_unsupportedN)neptune.utilsrv   r   r   PARAMETERS_KEYr[   r1   )rK   rt   rv   parameters_keys       r$   log_hyperparamszNeptuneLogger.log_hyperparams  sJ    L 	8 (*62,,99.I#8#@ r'   metricsstepc                     t         j                  dk7  rt        d      t        || j                  | j
                        }|j                         D ]%  \  }}| j                  |   j                  ||       ' y)zLog metrics (numeric values) in Neptune runs.

        Args:
            metrics: Dictionary with metric names as keys and measured quantities as values.
            step: Step number at which the metrics should be recorded

        r   z&run tried to log from global_rank != 0)r|   N)	r   rankrd   r   r=   rY   itemsr1   append)rK   r{   r|   keyvals        r$   log_metricszNeptuneLogger.log_metrics  se     !#EFFgt||T5J5JKHCHHSM  4 0 (r'   statusc                 ~    | j                   sy |r|| j                  | j                  d      <   t        |   |       y )Nr   )rA   r1   r[   r:   finalize)rK   r   rM   s     r$   r   zNeptuneLogger.finalize  s<     !! CIDHHT55h?@ r'   c                 f    t         j                  j                  t        j                         d      S )zGets the save directory of the experiment which in this case is ``None`` because Neptune does not save
        locally.

        Returns:
            the root directory where experiment logs get saved

        z.neptune)ospathrZ   getcwdrq   s    r$   save_dirzNeptuneLogger.save_dir  s     ww||BIIK44r'   modelzpl.LightningModule	max_depthc                     ddl m} t        t        ||            }|j	                  |d      | j
                  | j                  d      <   y )Nr   )File)r   r   txt)content	extensionzmodel/summary)neptune.typesr   r8   r   from_contentr1   r[   )rK   r   r   r   	model_strs        r$   log_model_summaryzNeptuneLogger.log_model_summary  sI     	'5IFG	FJFWFW GX G
11/BCr'   checkpoint_callbackc                    | j                   syt               }| j                  d      }t        |d      rf|j                  rZ| j                  |j                  |      }|j                  |       | j                  | d|    j                  |j                         t        |d      rW|j                  D ]H  }| j                  ||      }|j                  |       | j                  | d|    j                  |       J t        |d      r|j                  r|j                  | j                  | j                  d      <   | j                  |j                  |      }|j                  |       | j                  | d|    j                  |j                         | j                  j                  |      rQ| j                  j                         }| j                  ||      }t        ||z
        D ]  }	| j                  | d|	 =  t        |d      r`|j                  rS|j                  j!                         j#                         j%                         | j                  | j                  d	      <   yyy)
zAutomatically log checkpointed model. Called after model checkpoint callback saves a new checkpoint.

        Args:
            checkpoint_callback: the model checkpoint callback instance

        Nzmodel/checkpointslast_model_pathr*   best_k_modelsbest_model_pathzmodel/best_model_pathbest_model_scorezmodel/best_model_score)r<   setr[   hasattrr   _get_full_model_nameaddr1   uploadr   r   rQ   get_structure(_get_full_model_names_from_exp_structurelistr   cpudetachnumpy)
rK   r   
file_namescheckpoints_namespacemodel_last_namer   
model_nameexp_structureuploaded_model_namesfile_to_drops
             r$   after_save_checkpointz#NeptuneLogger.after_save_checkpoint  sB    **U
 $ @ @AT U &(9:?R?b?b"778K8[8[]pqONN?+HH-.a/@ABIIJ]JmJmn &8*88!66s<OP
z*12!J<@AHHM 9 &(9:?R?b?bReRuRuDHHT556MNO223F3V3VXklJNN:&HH-.a
|<=DDEXEhEhi 88??01 HH224M#'#P#PQ^`u#v  $%9J%F GHH 56a~FG !H &(:;@S@d@d#4488:AACIIK HHT556NOP Ae;r'   
model_pathc                    t        |d      rt        j                  j                  |       } t        j                  j                  |j                        }| j                  |      st        |  d| d      t        j                  j                  | t        |      dz   d       \  }}|j                  t        j                  d      S | j                  t        j                  d      S )zZReturns model name which is string `model_path` appended to `checkpoint_callback.dirpath`.dirpathz was expected to start with .   Nr*   )r   r   r   normpathr   
startswithrd   splitextlenreplacesep)r   r   expected_model_pathfilepath_s        r$   r   z"NeptuneLogger._get_full_model_name%  s     &	2))*5J"$''"2"23F3N3N"O(()<= J</KL_K``a!bcc''**:c:M6NQR6R6T+UVKHa##BFFC00!!"&&#..r'   r   	namespacec                     |j                  | j                        }|D ]  }||   }	 |}t        | j                  |            S )zHReturns all paths to properties which were already logged in `namespace`)splitrY   r   _dict_paths)clsr   r   structure_keysr   uploaded_models_dicts         r$   r   z6NeptuneLogger._get_full_model_names_from_exp_structure2  sG     %.OOC4H4H$I!C)#.M ",3??#7899r'   dpath_in_buildc              #      K   |j                         D ]?  \  }}|| d| n|}t        |t              s| &| j                  ||      E d {    A y 7 w)Nr*   )r   rF   dictr   )r   r   r   kvr   s         r$   r   zNeptuneLogger._dict_paths;  sY     GGIDAq-:-Fm_AaS)ADa&
??1d333 
 4s   AAAAc                     | j                   S )zMReturn the experiment name or 'offline-name' when exp is run in offline mode.)r>   rq   s    r$   r0   zNeptuneLogger.nameD  s     ~~r'   c                     | j                   S )zMReturn the experiment version.

        It's Neptune Run's short_id

        )rC   rq   s    r$   versionzNeptuneLogger.versionJ  s     !!!r'   )r   N)r   r   r^   ))0__name__
__module____qualname____doc__rY   rx   ARTIFACTS_KEYr	   r8   r
   boolr   r;   rD   propertyr   rV   r[   staticmethodr9   rk   ro   r   rr   r1   r   r   r&   r   rz   r   floatintr   r   r   r   r   r   r   classmethodr   r   r   r   r0   r   __classcell__)rM   s   @r$   r)   r)   B   s   aF "NM
 "&!%"1504 $@ #$@ #	$@
 sm$@ e,-.$@  (~$@ $@ "$@L," D  ,0 0 0 ## sm e,-.	
 ! 
 ,d38n I$sCx. IT I   8 	"  	" +AeDcNI,E&F +A4 +A   +AZ 14U65=-A(A#B 1(SV- 1cg 1   1  !s !t !   ! 5(3- 5  5 
'; 
 
UY 
  
 . . .   .` 
/ 
/: 
/RU 
/ 
/ :T#s(^ :`c :hklohp : : 4DcN 48C= 4T] 4 4 hsm    "# "  "r'   r)   )0r   r"   loggingr   argparser   collections.abcr   	functoolsr   typingr   r   r   r	   r
    lightning_utilities.core.importsr   torchr   typing_extensionsr   pytorch_lightningrH   !lightning_fabric.utilities.loggerr   r   r   pytorch_lightning.callbacksr    pytorch_lightning.loggers.loggerr   r   )pytorch_lightning.utilities.model_summaryr   %pytorch_lightning.utilities.rank_zeror   rc   r   rE   r   	getLoggerr   logr6   rJ   r&   r)   r_   r'   r$   <module>r      s   
   	  %  @ @ =  &  e e 2 I B @'g! &n5 G 
( x P"F P"r'   