§
    “©ºjq/  ã                   óî   — d dl mZmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZmZ erd dlmZ  G d„ d¦  «        Z	 	 	 	 dd
eeedf                  dee         dee         ded         ddf
d„Zdd„Zdd„ZdS )é    )ÚTYPE_CHECKINGÚLiteralÚOptionalÚUnionN)ÚCallback)ÚMisconfigurationException)ÚEVAL_DATALOADERSÚTRAIN_DATALOADERS)Ú	_LRFinderc                   óŠ  — e Zd ZdZd/d„Z	 	 	 	 	 	 	 	 	 	 	 	 d0dddeeedf                  dee         dee         ded         de	d         de
dededede
dededee         fd„Z	 	 	 	 	 	 	 	 	 	 	 	 d1dddeeedf                  dee         dee         ded         de	d         d'ed(ed)ede
d*ee         d+ed,e
ded-         fd.„ZdS )2ÚTunerzTuner class to tune your model.Útrainerú
pl.TrainerÚreturnNc                 ó   — || _         d S )N)Ú_trainer)Úselfr   s     ún/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/tuner/tuning.pyÚ__init__zTuner.__init__   s   € ØˆŒˆˆó    ÚfitÚpoweré   é   é   Ú
batch_sizeçš™™™™™©?é    Úmodelzpl.LightningModuleÚtrain_dataloadersúpl.LightningDataModuleÚval_dataloadersÚdataloadersÚ
datamoduleÚmethod©r   ÚvalidateÚtestÚpredictÚmodeÚsteps_per_trialÚinit_valÚ
max_trialsÚbatch_arg_nameÚmarginÚmax_valc           	      ój  ‡— t          ||||¦  «         t          | j        ¦  «         d|cxk    rdk     sn J d|›�¦   «         ‚ddlm}  ||||	|
|||¬¦  «        Šd‰_        ‰g| j        j        z   | j        _        |dk    r| j                             ||||¦  «         nk|d	k    r| j                             |||¬
¦  «         nG|dk    r| j         	                    |||¬
¦  «         n#|dk    r| j         
                    |||¬
¦  «         ˆfd„| j        j        D ¦   «         | j        _        ‰j        S )ad
  Iteratively try to find the largest batch size for a given model that does not give an out of memory (OOM)
        error.

        Args:
            model: Model to tune.
            train_dataloaders: A collection of :class:`torch.utils.data.DataLoader` or a
                :class:`~pytorch_lightning.core.datamodule.LightningDataModule` specifying training samples.
                In the case of multiple dataloaders, please see this :ref:`section <multiple-dataloaders>`.
            val_dataloaders: A :class:`torch.utils.data.DataLoader` or a sequence of them specifying validation samples.
            dataloaders: A :class:`torch.utils.data.DataLoader` or a sequence of them specifying val/test/predict
                samples used for running tuner on validation/testing/prediction.
            datamodule: An instance of :class:`~pytorch_lightning.core.datamodule.LightningDataModule`.
            method: Method to run tuner on. It can be any of ``("fit", "validate", "test", "predict")``.
            mode: Search strategy to update the batch size:

                - ``'power'``: Keep multiplying the batch size by 2, until we get an OOM error.
                - ``'binsearch'``: Initially keep multiplying by 2 and after encountering an OOM error
                    do a binary search between the last successful batch size and the batch size that failed.

            steps_per_trial: number of steps to run with a given batch size.
                Ideally 1 should be enough to test if an OOM error occurs,
                however in practise a few are needed
            init_val: initial batch size to start the search with
            max_trials: max number of increases in batch size done before
               algorithm is terminated
            batch_arg_name: name of the attribute that stores the batch size.
                It is expected that the user has provided a model or datamodule that has a hyperparameter
                with that name. We will look for this attribute name in the following places

                - ``model``
                - ``model.hparams``
                - ``trainer.datamodule`` (the datamodule passed to the tune method)

            margin: Margin to reduce the found batch size by to provide a safety buffer. Only applied when using
                'binsearch' mode. Should be a float between 0 and 1. Defaults to 0.05 (5% reduction).
            max_val: Maximum batch size limit, defaults to 8192.
                Helps prevent testing unrealistically large or inefficient batch sizes (e.g., 2**25)
                when running on CPU or when automatic OOM detection is not available.

        g        g      ð?z1`margin` should be between 0 and 1. Found margin=r   ©ÚBatchSizeFinder)r*   r+   r,   r-   r.   r/   r0   Tr   r'   )r$   r(   r)   c                 ó   •— g | ]}|‰u¯|‘Œ	S © r5   )Ú.0ÚcbÚbatch_size_finders     €r   ú
<listcomp>z*Tuner.scale_batch_size.<locals>.<listcomp>t   s$   ø€ Ð"gÐ"gÐ"g¨"È2ÐUfÐKfÐKf 2ÐKfÐKfÐKfr   )Ú_check_tuner_configurationÚ%_check_scale_batch_size_configurationr   Ú-pytorch_lightning.callbacks.batch_size_finderr3   Ú_early_exitÚ	callbacksr   r'   r(   r)   Úoptimal_batch_size)r   r   r    r"   r#   r$   r%   r*   r+   r,   r-   r.   r/   r0   r3   r8   s                  @r   Úscale_batch_sizezTuner.scale_batch_size   s™  ø€ õp 	#Ð#4°oÀ{ÐTZÑ[Ô[Ð[Ý-¨d¬mÑ<Ô<Ð<Ø�fÐ"Ð"Ò"Ð"˜sÒ"Ð"Ð"Ð"Ð"Ð$ZÐQWÐ$ZÐ$ZÑ"Ô"Ð"ð 	RÐQÐQÐQÐQÐQà&5 oØØ+ØØ!Ø)ØØð'
ñ '
ô '
Ðð )-ÐÔ%Ø#4Ð"5¸¼Ô8OÑ"OˆŒÔà�UŠ?ˆ?ØŒM×Ò˜eÐ%6¸ÈÑTÔTÐTÐTØ�zÒ!Ð!ØŒM×"Ò" 5¨+À*Ð"ÑMÔMÐMÐMØ�vÒÐØŒM×Ò˜u k¸jÐÑIÔIÐIÐIØ�yÒ Ð ØŒM×!Ò! %¨ÀÐ!ÑLÔLÐLà"gÐ"gÐ"gÐ"g°´Ô0GÐ"gÑ"gÔ"gˆŒÔØ Ô3Ð3r   ç:Œ0âŽyE>é   éd   Úexponentialç      @TÚ Úmin_lrÚmax_lrÚnum_trainingÚearly_stop_thresholdÚupdate_attrÚ	attr_namer   c           	      óx  ‡— |dk    rt          d¦  «        ‚t          ||||¦  «         t          | j        ¦  «         ddlm}  ||||	|
|||¬¦  «        Šd‰_        ‰g| j        j        z   | j        _        | j                             ||||¦  «         ˆfd„| j        j        D ¦   «         | j        _        ‰j	        S )aX  Enables the user to do a range test of good initial learning rates, to reduce the amount of guesswork in
        picking a good starting learning rate.

        Args:
            model: Model to tune.
            train_dataloaders: A collection of :class:`torch.utils.data.DataLoader` or a
                :class:`~pytorch_lightning.core.datamodule.LightningDataModule` specifying training samples.
                In the case of multiple dataloaders, please see this :ref:`section <multiple-dataloaders>`.
            val_dataloaders: A :class:`torch.utils.data.DataLoader` or a sequence of them specifying validation samples.
            dataloaders: A :class:`torch.utils.data.DataLoader` or a sequence of them specifying val/test/predict
                samples used for running tuner on validation/testing/prediction.
            datamodule: An instance of :class:`~pytorch_lightning.core.datamodule.LightningDataModule`.
            method: Method to run tuner on. It can be any of ``("fit", "validate", "test", "predict")``.
            min_lr: minimum learning rate to investigate
            max_lr: maximum learning rate to investigate
            num_training: number of learning rates to test
            mode: Search strategy to update learning rate after each batch:

                - ``'exponential'``: Increases the learning rate exponentially.
                - ``'linear'``: Increases the learning rate linearly.

            early_stop_threshold: Threshold for stopping the search. If the
                loss at any point is larger than early_stop_threshold*best_loss
                then the search is stopped. To disable, set to None.
            update_attr: Whether to update the learning rate attribute or not.
            attr_name: Name of the attribute which stores the learning rate. The names 'learning_rate' or 'lr' get
                automatically detected. Otherwise, set the name here.

        Raises:
            MisconfigurationException:
                If learning rate/lr in ``model`` or ``model.hparams`` isn't overridden,
                or if you are using more than one optimizer.

        r   z>method='fit' is the only valid configuration to run lr finder.r   ©ÚLearningRateFinder)rG   rH   Únum_training_stepsr*   rJ   rK   rL   Tc                 ó   •— g | ]}|‰u¯|‘Œ	S r5   r5   )r6   r7   Úlr_finder_callbacks     €r   r9   z!Tuner.lr_find.<locals>.<listcomp>Á   s$   ø€ Ð"hÐ"hÐ"h¨"È2ÐUgÐKgÐKg 2ÐKgÐKgÐKgr   )
r   r:   Ú_check_lr_find_configurationr   Ú%pytorch_lightning.callbacks.lr_finderrO   r=   r>   r   Ú
optimal_lr)r   r   r    r"   r#   r$   r%   rG   rH   rI   r*   rJ   rK   rL   rO   rR   s                  @r   Úlr_findzTuner.lr_findw   sñ   ø€ ðd �UŠ?ˆ?Ý+Ð,lÑmÔmÐmå"Ð#4°oÀ{ÐTZÑ[Ô[Ð[Ý$ T¤]Ñ3Ô3Ð3ð 	MÐLÐLÐLÐLÐLà'9Ð'9ØØØ+ØØ!5Ø#Øð(
ñ (
ô (
Ðð *.ÐÔ&Ø#5Ð"6¸¼Ô9PÑ"PˆŒÔàŒ×Ò˜%Ð!2°OÀZÑPÔPÐPà"hÐ"hÐ"hÐ"h°´Ô0GÐ"hÑ"hÔ"hˆŒÔà!Ô,Ð,r   ©r   r   r   N)NNNNr   r   r   r   r   r   r   r   )NNNNr   rA   rB   rC   rD   rE   TrF   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r
   r	   r   ÚstrÚintÚfloatr@   ÚboolrV   r5   r   r   r   r      sL  € € € € € Ø)Ð)ð ð  ð  ð  ð [_Ø6:Ø26Ø9=Ø@EØØ ØØØ*ØØðV4ð V4à#ðV4ð $ EÐ*;Ð=UÐ*UÔ$VÔWðV4ð "Ð"2Ô3ð	V4ð
 Ð.Ô/ðV4ð Ð5Ô6ðV4ð Ð<Ô=ðV4ð ðV4ð ðV4ð ðV4ð ðV4ð ðV4ð ðV4ð ðV4ð 
�#ŒðV4ð V4ð V4ð V4ðv [_Ø6:Ø26Ø9=Ø@EØØØØ!Ø03Ø ØðL-ð L-à#ðL-ð $ EÐ*;Ð=UÐ*UÔ$VÔWðL-ð "Ð"2Ô3ð	L-ð
 Ð.Ô/ðL-ð Ð5Ô6ðL-ð Ð<Ô=ðL-ð ðL-ð ðL-ð ðL-ð ðL-ð ' uœoðL-ð ðL-ð ðL-ð 
�+Ô	ðL-ð L-ð L-ð L-ð L-ð L-r   r   r   r    r!   r"   r#   r%   r&   r   c                 ó¦   — d}||vrt          d|›d|› d�¦  «        ‚|dk    r|�t          d|›d�¦  «        ‚d S | €|�t          d|›d	�¦  «        ‚d S )
Nr&   zmethod z is invalid. Should be one of ú.r   zIn tuner with method=zs, `dataloaders` argument should be None, please consider setting `train_dataloaders` and `val_dataloaders` instead.zIn tuner with `method`=zt, `train_dataloaders` and `val_dataloaders` arguments should be None, please consider setting `dataloaders` instead.)Ú
ValueErrorr   )r    r"   r#   r%   Úsupported_methodss        r   r:   r:   Æ   s¾   € ð ?ÐØÐ&Ð&Ð&ÝÐ_ 6Ð_Ð_ÐK\Ð_Ð_Ð_Ñ`Ô`Ð`à�‚€ØÐ"Ý+ð^¨ð ^ð ^ð ^ñô ð ð #Ð"ð Ð(¨OÐ,GÝ+ð\¨&ð \ð \ð \ñô ð ð -HÐ,Gr   r   r   c                 ó\   ‡— ddl mŠ ˆfd„| j        D ¦   «         }|rt          d¦  «        ‚d S )Nr   rN   c                 ó4   •— g | ]}t          |‰¦  «        ¯|‘ŒS r5   ©Ú
isinstance)r6   r7   rO   s     €r   r9   z0_check_lr_find_configuration.<locals>.<listcomp>â   s)   ø€ ÐaÐaÐa 2½jÈÐM_Ñ>`Ô>`Ða˜BÐaÐaÐar   zqTrainer is already configured with a `LearningRateFinder` callback.Please remove it if you want to use the Tuner.)rT   rO   r>   rb   )r   Úconfigured_callbacksrO   s     @r   rS   rS   Þ   s[   ø€ àHÐHÐHÐHÐHÐHàaÐaÐaÐa¨Ô):ÐaÑaÔaÐØð 
Ýð=ñ
ô 
ð 	
ð
ð 
r   c                 ó’   ‡— | j         j        rt          d¦  «        ‚ddlmŠ ˆfd„| j        D ¦   «         }|rt          d¦  «        ‚d S )NzMTuning the batch size is currently not supported with distributed strategies.r   r2   c                 ó4   •— g | ]}t          |‰¦  «        ¯|‘ŒS r5   rf   )r6   r7   r3   s     €r   r9   z9_check_scale_batch_size_configuration.<locals>.<listcomp>ñ   s(   ø€ Ð^Ð^Ð^ 2½jÈÈ_Ñ>]Ô>]Ð^˜BÐ^Ð^Ð^r   znTrainer is already configured with a `BatchSizeFinder` callback.Please remove it if you want to use the Tuner.)Ú_accelerator_connectorÚis_distributedrb   r<   r3   r>   )r   rh   r3   s     @r   r;   r;   ê   s|   ø€ ØÔ%Ô4ð jÝÐhÑiÔiÐið NÐMÐMÐMÐMÐMà^Ð^Ð^Ð^¨Ô):Ð^Ñ^Ô^ÐØð 
Ýð=ñ
ô 
ð 	
ð
ð 
r   )NNNr   rW   )Útypingr   r   r   r   Úpytorch_lightningÚplÚ$pytorch_lightning.callbacks.callbackr   Ú&pytorch_lightning.utilities.exceptionsr   Ú!pytorch_lightning.utilities.typesr	   r
   Ú!pytorch_lightning.tuner.lr_finderr   r   r:   rS   r;   r5   r   r   ú<module>rt      sj  ðð ;Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :à Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø LÐ LÐ LÐ LÐ LÐ LØ QÐ QÐ QÐ QÐ QÐ QÐ QÐ Qàð <Ø;Ð;Ð;Ð;Ð;Ð;ðj-ð j-ð j-ð j-ð j-ñ j-ô j-ð j-ð\ W[Ø26Ø.2Ø<Að	ð Ø Ð&7Ð9QÐ&QÔ RÔSðàÐ.Ô/ðð Ð*Ô+ðð Ð8Ô9ð	ð
 
ðð ð ð ð0	
ð 	
ð 	
ð 	
ð
ð 
ð 
ð 
ð 
ð 
r   