a
    d$                     @   s2  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mZm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Zd d	lmZ d d
lmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ e
e ddddZ0e/e1e
e dddZ2e
e1 e1e
e1 e
e1 dee1e1f dddZ3edee e
e1 e
e ed d!d"Z4ed9e!e5ed$ d%d&d'Z6ed(ee1 ej7d)d*d+Z8e	e e	e1 e1e	ee1ef  d,d-d.Z9d:e1e1e5d0d1d2Z:edd3d4d5Z;eee%f e1dd6d7d8Z<dS );    )OrderedDict)contextmanager)	lru_cache)Any	GeneratorListOptionalSequenceTupleUnionN)Tensor)	Optimizer)
DataLoader)PossibleUserWarning)Timer)Loop)ParallelStrategy)Strategy)BaseProgress)CombinedLoader)MisconfigurationException)recursive_detach)rank_zero_warn)is_param_in_hook_signature)STEP_OUTPUT)lossreturnc                 C   s*   | dur&t |  s&td|  ddS )z`Checks for finite loss value.

    Args:
        loss: the loss value to check to be finite
    Nz(The loss returned in `training_step` is .)torchisfiniteall
ValueError)r    r"   j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/loops/utilities.pycheck_finite_loss(   s    r$   )training_step_outputtruncated_bptt_stepsr   c                 C   sL   |s"t | trd| v rtddS t | tr4d| vr<tdt| d }|S )a  Get the hidden state if present from the training step output.

    Raises:
        MisconfigurationException: If :attr:`~pytorch_lightning.core.Lightning.LightningModule.truncated_bptt_steps` is
            not enabled and hiddens are returned or vice versa.
    hiddenszUYou returned "hiddens" in your `training_step` but `truncated_bptt_steps` is disabledNzeYou enabled `truncated_bptt_steps` but did not `return {..., "hiddens": ...}` in your `training_step`)
isinstancedictr   r   )r%   r&   r'   r"   r"   r#   _extract_hiddens2   s    r*   z
pl.Trainer)	min_steps	max_steps
min_epochs
max_epochstrainerr   c                 C   sb   |du r:|dkr6t dd |jD s6tdtd d}nd}|du rN| durNd}|du rZd	}||fS )
a  This utility computes the default values for the minimum and maximum number of steps and epochs given the
    values the user has selected.

    Args:
        min_steps: Minimum number of steps.
        max_steps: Maximum number of steps.
        min_epochs: Minimum number of epochs.
        max_epochs: Maximum number of epochs.
        trainer: Trainer instance.

    Returns:
        The parsed limits, with default values being set for the ones that the user did not specify.
    Nc                 s   s   | ]}t |tV  qd S N)r(   r   ).0cbr"   r"   r#   	<genexpr>]       z%_parse_loop_limits.<locals>.<genexpr>zj`max_epochs` was not set. Setting it to 1000 epochs. To train without an epoch limit, set `max_epochs=-1`.)categoryi     r   )any	callbacksr   r   )r+   r,   r-   r.   r/   r"   r"   r#   _parse_loop_limitsH   s    r:   zpl.LightningModule)kwargslightning_module
optimizersopt_idxr'   r   c                 C   sp   t |d}t|dkrZt|d}|r<|js2td|| d< n|sZ|jrZtdt| d|jdkrl|| d< | S )	a  Builds the keyword arguments for training_step.

    Args:
        kwargs: The kwargs passed down to the hooks.
        lightning_module: the LightningModule with a `training_step` hook implementation
        optimizers: the list of optimizers from the Trainer
        opt_idx: the index of the current optimizer
        hiddens: the hidden state of the previous RNN iteration

    Returns:
        the keyword arguments for the training step
    Ztraining_stepr7   Zoptimizer_idxzYour `LightningModule.training_step` signature contains an `optimizer_idx` argument but in manual optimization optimizers must be handled by the user. Remove the optimizer_idx argument or set `self.automatic_optimization = True`.zYour LightningModule defines zH optimizers but `training_step` is missing the `optimizer_idx` argument.r   r'   )getattrlenr   Zautomatic_optimizationr!   r&   )r;   r<   r=   r>   r'   Ztraining_step_fxZhas_opt_idx_in_train_stepr"   r"   r#   _build_training_step_kwargsr   s     




rA   T)NNN)strategyblockr   c                 c   sH   t | tr>|r>|   dV  W d   qD1 s20    Y  ndV  dS )a  Blocks synchronization in :class:`~pytorch_lightning.strategies.parallel.ParallelStrategy`. This is useful
    for example when accumulating gradients to reduce communication when it is not needed.

    Args:
        strategy: the strategy instance to use.
        block: whether the context manager is enabled or not

    Returns:
        context manager with sync behaviour off
    N)r(   r   Zblock_backward_sync)rB   rC   r"   r"   r#   _block_parallel_sync_behavior   s    
&rD   r7   )frequenciesr   c                 C   s
   t | S r1   )npZcumsum)rE   r"   r"   r#   !_cumulative_optimizer_frequencies   s    rG   )r=   rE   	batch_idxr   c                 C   sJ   |st t| S tt|}|d }|| }tj||dd}|| | fgS )zReturns the currently active optimizers. When multiple optimizers are used with different frequencies, only
    one of the optimizers is active at a time.

    Returns:
        A list of tuples (opt_idx, optimizer) of currently active optimizers.
    r0   right)Zside)list	enumeraterG   tuplerF   Zsearchsorted)r=   rE   rH   Zfreq_cumsumZoptimizers_loop_lengthZcurrent_place_in_loopr>   r"   r"   r#   _get_active_optimizers   s    	rM   r0   )currentmaximumr   c                 C   s   |dko| |kS )zCheck if the limit has been reached (if enabled).

    Args:
        current: the current value
        maximum: the maximum value (or -1 to disable limit)

    Returns:
        bool: whether the limit has been reached
    r0   r"   )rN   rO   r"   r"   r#   _is_max_limit_reached   s    
rP   )loopr   c                 C   s<   t |  D ]*}t|tr$|  qt|trt| qd S r1   )varsvaluesr(   r   resetr   _reset_progress)rQ   vr"   r"   r#   rU      s
    


rU   )
dataloaderepochr   c                 C   s<   dD ]2}t | |d}|durtt |ddr|| qdS )a  Calls the ``set_epoch`` method on either the sampler or the batch sampler of the given dataloader.

    Every PyTorch dataloader has either a sampler or a batch sampler, and if it is wrapped by a
    :class:`~torch.utils.data.distributed.DistributedSampler`, ``set_epoch`` must be called at the beginning
    of every epoch to ensure shuffling applies a new ordering. This has no effect if shuffling is off.
    )samplerZbatch_samplerN	set_epoch)r?   callablerZ   )rW   rX   Zsampler_namerY   r"   r"   r#   _set_sampler_epoch   s    r\   )T)r0   )=collectionsr   
contextlibr   	functoolsr   typingr   r   r   r   r	   r
   r   numpyrF   r   r   Ztorch.optimr   Ztorch.utils.datar   Zpytorch_lightningplZ#lightning_fabric.utilities.warningsr   Z!pytorch_lightning.callbacks.timerr   Zpytorch_lightning.loopsr   Z%pytorch_lightning.strategies.parallelr   Z%pytorch_lightning.strategies.strategyr   Z"pytorch_lightning.trainer.progressr   Z$pytorch_lightning.trainer.supportersr   Z&pytorch_lightning.utilities.exceptionsr   Z"pytorch_lightning.utilities.memoryr   Z%pytorch_lightning.utilities.rank_zeror   Z+pytorch_lightning.utilities.signature_utilsr   Z!pytorch_lightning.utilities.typesr   r$   intr*   r:   rA   boolrD   ZndarrayrG   rM   rP   rU   r\   r"   r"   r"   r#   <module>   s^   $

++