U
    yâh<  ã                   @   s6   d dl Z ddlmZ dgZdd„ ZG dd„ deƒZdS )é    Né   )ÚBaseSchedulerÚCubicSLc                 C   s   t |t|| ƒƒS ©N)ÚmaxÚmin)ÚxÚloÚhi© r   ú\/var/www/html/venv/lib/python3.8/site-packages/torch/ao/pruning/scheduler/cubic_scheduler.pyÚ_clamp   s    r   c                       s8   e Zd ZdZd‡ fdd	„	Zedd
d„ƒZdd„ Z‡  ZS )r   aÛ  Sets the sparsity level of each parameter group to the final sl
    plus a given exponential function.

    .. math::

        s_i = s_f + (s_0 - s_f) \cdot \left( 1 - \frac{t - t_0}{n\Delta t} \right)^3

    where :math:`s_i` is the sparsity at epoch :math:`t`, :math;`s_f` is the final
    sparsity level, :math:`f(i)` is the function to be applied to the current epoch
    :math:`t`, initial epoch :math:`t_0`, and final epoch :math:`t_f`.
    :math:`\Delta t` is used to control how often the update of the sparsity level
    happens. By default,

    Args:
        sparsifier (BaseSparsifier): Wrapped sparsifier.
        init_sl (int, list): Initial level of sparsity
        init_t (int, list): Initial step, when pruning starts
        delta_t (int, list): Pruning frequency
        total_t (int, list): Total number of pruning steps
        initially_zero (bool, list): If True, sets the level of sparsity to 0
            before init_t (:math:`t_0`). Otherwise, the sparsity level before
            init_t (:math:`t_0`) is set to init_sl(:math:`s_0`)
        last_epoch (int): The index of last epoch. Default: -1.
        verbose (bool): If ``True``, prints a message to stdout for
            each update. Default: ``False``.
    ç        r   é
   éd   Féÿÿÿÿc	           	         sV   || _ |  |¡| _|  |¡| _|  |¡| _|  |¡| _|  |¡| _tƒ  |||¡ d S r   )	Ú
sparsifierZ_make_sure_a_listÚinit_slÚinit_tÚdelta_tÚtotal_tÚinitially_zeroÚsuperÚ__init__)	Úselfr   r   r   r   r   r   Ú
last_epochÚverbose©Ú	__class__r   r   r   '   s    
zCubicSL.__init__c                 C   sD   |r||k rdS || | d|| ||   d   }t || |ƒ}|S )a]  "Computes the current level of sparsity.

        Based on https://arxiv.org/pdf/1710.01878.pdf

        Args:
            s_0: Initial level of sparsity, :math:`s_i`
            s_f: Target level of sparsity, :math:`s_f`
            t: Current step, :math:`t`
            t_0: Initial step, :math:`t_0`
            dt: Pruning frequency, :math:`\Delta T`
            n: Pruning steps, :math:`n`
            initially_zero: Sets the level of sparsity to 0 before t_0.
                If False, sets to s_0

        Returns:
            The sparsity level :math:`s_t` at the current step :math:`t`
        r   g      ð?é   )r   )Ús_0Ús_fÚtÚt_0ÚdtÚnr   Zs_tr   r   r   Úsparsity_compute_fn<   s
    $zCubicSL.sparsity_compute_fnc                    s<   ˆ j st d¡ ‡ fdd„tˆ jˆ jˆ jˆ jˆ jˆ j	ƒD ƒS )NzUTo get the last sparsity level computed by the scheduler, please use `get_last_sl()`.c                    s2   g | ]*\}}}}}}ˆ j ||ˆ j||||d ‘qS ))r    r!   r"   r#   r$   r%   r   )r&   r   )Ú.0Zinitial_sparsityZfinal_sparsityZinitial_epochZdelta_epochZinterval_epochsr   ©r   r   r   Ú
<listcomp>Z   s   	øùz"CubicSL.get_sl.<locals>.<listcomp>)
Z_get_sl_called_within_stepÚwarningsÚwarnÚzipr   Zbase_slr   r   r   r   r(   r   r(   r   Úget_slU   s    ÿ

úözCubicSL.get_sl)r   r   r   r   Fr   F)F)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr&   r-   Ú__classcell__r   r   r   r   r      s          ø)r*   Zbase_schedulerr   Ú__all__r   r   r   r   r   r   Ú<module>   s   