o
    ¡ôjwk  ã                   @   sÔ  U d dl mZ d dlmZmZmZmZmZ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rCd dlmZ d dlmZ eeeeef f ZG d	d
„ d
ƒZG dd„ deƒZ	d—dejjdeg ej f de	eej gej f  defdd„Z!i dej"dej#dddej$dej%dej&di“dej"dej#dddej$dej%dej&di“dej"d ej#d!dd"ej$d#ej%d#ej&d$i“d%ej#d&dd&ej$d&ej%d&ej&d'd(d)i“d*ej#d+dd+ej$d+ej%d+ej&d,d(d-i“d.ej#d/dd0ej$d1ej%d1ej&d2d(d3i“d4ej#d5dd5ej$d6ej%d6ej&d7d(d8i“d9ej"d:ej#d;dd<ej$d=ej%d=ej&d>i“d?ej#d@ddAej$d@ej%d@ej&dBd(dCi“dDej#dEddFej$dEej%dEej&dGd(dHi“dIej#dJddKej$dLej%dLej&dMd(dNi“dOej#dPddPej$dPej%dPej&dQd(dRi“dSej#dTddTej$dTej%dTej&dUd(dVi“dWej#dXddXej$dXej%dXej&dYd(dZi“d[ej#d\dd\ej$d\ej%d\ej&d]d(d^i“d_ej#d`dd`ej$d`ej%d`ej&dad(dbi“dcej#ddej%deej&dfd(dgi“ej#dhej%diiej#dhej%djej&dkd(dliej#dmej%dnej&dod(dpiej#dqej%drej&dsd(dtiej#duej%dvej&dwd(dxiej#dyej%dzej&d{d(d|iej"d}ej#d~ej%dLiej"dej#d€ej%d�iej"d‚ej#dƒej%dfid„œ	¥Z'eeeeeej(f ef f e)d…< d†d‡dˆd‰dŠœZ*d‹ej+dŒeej(ef de	e fd�dŽ„Z,d�d�dej(fd‘d’„Z-e
d“ed”�Z.G d•d–„ d–ee. ƒZ/dS )˜é    )Údeque)	ÚTYPE_CHECKINGÚAnyÚCallableÚDequeÚDictÚListÚOptionalÚTypeVarÚUnionN)Úoverride)Ú_TORCH_GREATER_EQUAL_2_1)Úrank_zero_onlyÚrank_zero_warn)ÚFabric)Ú	Precisionc                   @   s€   e Zd ZdZ	ddee deded	ed
df
dd„Zdddœdedededee dee d
dfdd„Z	d
e
fdd„Zddd„ZdS )Ú
Throughputaƒ  Computes throughput.

    +------------------------+-------------------------------------------------------------------------------------+
    | Key                    | Value                                                                               |
    +========================+=====================================================================================+
    | batches_per_sec        | Rolling average (over ``window_size`` most recent updates) of the number of batches |
    |                        | processed per second                                                                |
    +--------------------------+-----------------------------------------------------------------------------------+
    | samples_per_sec        | Rolling average (over ``window_size`` most recent updates) of the number of samples |
    |                        | processed per second                                                                |
    +--------------------------+-----------------------------------------------------------------------------------+
    | items_per_sec          | Rolling average (over ``window_size`` most recent updates) of the number of items   |
    |                        | processed per second                                                                |
    +--------------------------+-----------------------------------------------------------------------------------+
    | flpps_per_sec          | Rolling average (over ``window_size`` most recent updates) of the number of flops   |
    |                        | processed per second                                                                |
    +--------------------------+-----------------------------------------------------------------------------------+
    | device/batches_per_sec | batches_per_sec divided by world size                                               |
    +--------------------------+-----------------------------------------------------------------------------------+
    | device/samples_per_sec | samples_per_sec divided by world size                                               |
    +--------------------------+-----------------------------------------------------------------------------------+
    | device/items_per_sec   | items_per_sec divided by world size. This may include padding depending on the data |
    +--------------------------+-----------------------------------------------------------------------------------+
    | device/flops_per_sec   | flops_per_sec divided by world size.                                                |
    +--------------------------+-----------------------------------------------------------------------------------+
    | device/mfu             | device/flops_per_sec divided by world size.                                         |
    +--------------------------+-----------------------------------------------------------------------------------+
    | time                   | Total elapsed time                                                                  |
    +--------------------------+-----------------------------------------------------------------------------------+
    | batches                | Total batches seen                                                                  |
    +--------------------------+-----------------------------------------------------------------------------------+
    | samples                | Total samples seen                                                                  |
    +--------------------------+-----------------------------------------------------------------------------------+
    | lengths                | Total items seen                                                                    |
    +--------------------------+-----------------------------------------------------------------------------------+

    Example::

        throughput = Throughput()
        t0 = time()
        for i in range(1000):
            do_work()
            if torch.cuda.is_available(): torch.cuda.synchronize()  # required or else time() won't be correct
            throughput.update(time=time() - t0, samples=i)
            if i % 10 == 0:
                print(throughput.compute())

    Notes:
        - The implementation assumes that devices FLOPs are all the same as it normalizes by the world size and only
          takes a single ``available_flops`` value.
        - items_per_sec, flops_per_sec and MFU do not account for padding if present. We suggest using
          samples_per_sec or batches_per_sec to measure throughput under this circumstance.

    Args:
        available_flops: Number of theoretical flops available for a single device.
        world_size: Number of devices available across hosts. Global metrics are not included if the world size is 1.
        window_size: Number of batches to use for a rolling average.
        separator: Key separator to use when creating per-device and global metrics.

    Né   éd   ú/Úavailable_flopsÚ
world_sizeÚwindow_sizeÚ	separatorÚreturnc                 C   sj   || _ || _|dksJ ‚|| _|dksJ ‚t|d�| _t|d�| _t|d�| _t|d�| _t|d�| _	d S )Nr   r   )Úmaxlen)
r   r   r   Ú_MonotonicWindowÚ_timeÚ_batchesÚ_samplesÚ_lengthsr   Ú_flops)Úselfr   r   r   r   © r#   úx/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/lightning/fabric/utilities/throughput.pyÚ__init___   s   zThroughput.__init__)ÚlengthsÚflopsÚtimeÚbatchesÚsamplesr&   r'   c                C   sÌ   | j  |¡ ||k rtd|› d|› d�ƒ‚| j |¡ | j |¡ |durU||k r4td|› d|› d�ƒ‚| j |¡ t| jƒt| jƒkrUtdt| jƒ› dt| jƒ› d�ƒ‚|durd| j || j	 ¡ dS dS )	ad  Update throughput metrics.

        Args:
            time: Total elapsed time in seconds. It should monotonically increase by the iteration time with each
                call.
            batches: Total batches seen per device. It should monotonically increase with each call.
            samples: Total samples seen per device. It should monotonically increase by the batch size with each call.
            lengths: Total length of the samples seen. It should monotonically increase by the lengths of a batch with
                each call.
            flops: Flops elapased per device since last ``update()`` call. You can easily compute this by using
                :func:`measure_flops` and multiplying it by the number of batches that have been processed.
                The value might be different in each device if the batch size is not the same.

        zExpected samples (z') to be greater or equal than batches (ú)NzExpected lengths (z') to be greater or equal than samples (zIf lengths are passed (z1), there needs to be the same number of samples ()
r   ÚappendÚ
ValueErrorr   r   r    ÚlenÚRuntimeErrorr!   r   )r"   r(   r)   r*   r&   r'   r#   r#   r$   Úupdater   s$   ÿÿþzThroughput.updatec                 C   sÊ  | j d | jd | jd dœ}| jr| jd |d< | jdk}t| j ƒ| j jkrŸ| j d | j d  }| jd | jd  }| jd | jd  }|| }|| }| d| j› d�|| d| j› d�|i¡ |rt|| j }| ||| j d	œ¡ t| jƒ| jjkrŸ| jd | jd  }	|	| }
|
|d| j› d
�< |rŸ|
| j }||d
< t| j	ƒ| j	jkrãt
| j	ƒ| j	d  }| j d | j d  }|| }|| j }|rË||d< ||d| j› d�< | jrã|| j |d| j› d�< |S )zCompute throughput metrics.éÿÿÿÿ)r(   r)   r*   r&   r   r   ÚdeviceÚbatches_per_secÚsamples_per_sec)r3   r4   Úitems_per_secÚflops_per_secZmfu)r   r   r   r    r   r.   r   r0   r   r!   Úsumr   )r"   ÚmetricsZadd_global_metricsÚelapsed_timeZelapsed_batchesZelapsed_samplesZdev_samples_per_secZdev_batches_per_secr4   Zelapsed_lengthsZdev_items_per_secr5   Zelapsed_flopsr6   Zdev_flops_per_secr#   r#   r$   Úcompute›   sR   ý
þ
þ

zThroughput.computec                 C   s6   | j  ¡  | j ¡  | j ¡  | j ¡  | j ¡  d S ©N)r   Úclearr   r   r    r!   ©r"   r#   r#   r$   ÚresetÏ   s
   



zThroughput.reset)Nr   r   r   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   ÚfloatÚintÚstrr%   r0   Ú_THROUGHPUT_METRICSr:   r>   r#   r#   r#   r$   r   !   s>    >ÿÿÿÿÿ
þùýüûúù
ø)4r   c                       sJ   e Zd ZdZdddeddf‡ fdd„Zdd	ee dedefd
d„Z	‡  Z
S )ÚThroughputMonitora  Computes throughput.

    This class will automatically keep a count of the number of log calls (``step``). But that can be modified as
    desired. For manual logging, using :class:`Throughput` directly might be desired.

    Example::

        logger = ...
        fabric = Fabric(logger=logger)
        throughput = ThroughputMonitor()
        t0 = time()
        for i in range(1, 100):
            do_work()
            if torch.cuda.is_available(): torch.cuda.synchronize()  # required or else time() won't be correct
            throughput.update(time=time() - t0, batches=i, samples=i)
            if i % 10 == 0:
                throughput.compute_and_log(step=i)

    Args:
        fabric: The Fabric object.
        \**kwargs: See available parameters in :class:`Throughput`

    Úfabricr   Úkwargsr   Nc                    s‚   |  ¡  t|jjƒ}t|j|ƒ}tƒ jd||jdœ|¤Ž || _	d| _
t| jƒ| _t| ji d�| _t| ji d�| _t| jƒ| _d S )N)r   r   r1   )Údefaultr#   )Ú_validate_launchedÚ_plugin_to_compute_dtypeÚstrategyÚ	precisionÚget_available_flopsr2   Úsuperr%   r   Ú_fabricÚstepr   r0   r:   Úcompute_and_logr>   )r"   rH   rI   Údtyper   ©Ú	__class__r#   r$   r%   ð   s   zThroughputMonitor.__init__rR   c                 K   s>   |du r	| j d n|| _ | jdi |¤Ž}| jj|| j d� |S )zÁSee :meth:`Throughput.compute`

        Args:
            step: Can be used to override the logging step.
            \**kwargs: See available parameters in :meth:`Throughput.compute`

        Nr   )r8   rR   r#   )rR   r:   rQ   Úlog_dict)r"   rR   rI   r8   r#   r#   r$   rS   ý   s   z!ThroughputMonitor.compute_and_logr;   )r?   r@   rA   rB   r   r%   r	   rD   rF   rS   Ú__classcell__r#   r#   rU   r$   rG   ×   s    $rG   ÚmodelÚ
forward_fnÚloss_fnr   c                 C   sˆ   t stdƒ‚ddlm} || dd�}|�$ |du r|ƒ  n||ƒ ƒ ¡  W d  ƒ | ¡ S W d  ƒ | ¡ S 1 s;w   Y  | ¡ S )a-  Utility to compute the total number of FLOPs used by a module during training or during inference.

    It's recommended to create a meta-device model for this:

    Example::

        with torch.device("meta"):
            model = MyModel()
            x = torch.randn(2, 32)

        model_fwd = lambda: model(x)
        fwd_flops = measure_flops(model, model_fwd)

        model_loss = lambda y: y.sum()
        fwd_and_bwd_flops = measure_flops(model, model_fwd, model_loss)

    Args:
        model: The model whose FLOPs should be measured.
        forward_fn: A function that runs ``forward`` on the model and returns the result.
        loss_fn: A function that computes the loss given the ``forward_fn`` output. If provided, the loss and `backward`
            FLOPs will be included in the result.

    z(`measure_flops` requires PyTorch >= 2.1.r   )ÚFlopCounterModeF)ÚdisplayN)r   ÚImportErrorZtorch.utils.flop_counterr\   ÚbackwardZget_total_flops)rY   rZ   r[   r\   Zflop_counterr#   r#   r$   Úmeasure_flops  s   
üý
þûr`   úh100 nvlg  ˜™ÓwÎBg  $^/lÞBÚtfloat32g €SÕCg €SÕCg  />2,Cúh100 sxmg  ˜™Ów¾Bg  $^/lÎBg €SÕüBg  />2Cú	h100 pcieg   èvH·Bg   èvHÇBg  š»Ÿ|õBg  š»Ÿ|Cg @.C€Czrtx 4090g  $êóÇÒBg €µbÆCÚint4g €µbÆCzrtx 4080g  ¬m%ÆBg €÷¯Ê)öBg €÷¯Ê)CÚl4g  ˜¼ÄŽ»Bg  àWëHËBg  $� ƒÛBg  $� ƒëBg  $� ƒûBÚl40g  ŠË“ÔBg  ŠË“äBg  ŠË“ôBg  ŠË“CÚa100g  Ðê¤¡Bg  ¸’2¼±Bg  ¸’2¼áBg  ¸’2¼ñBg  ¸’2¼CÚa6000g  \àE™ÁBg  \àE™ÑBg €ÊgºšñBg @$ šCÚa40g  xÛîÁBg  xÛîÑBg €æbcñBg À�¦CÚa10gg  ÀêP`¼Bg  4&õkÌBg  4&õkÜBg  4&õkìBg  4&õküBzrtx 3090 tig  @åœ0ÂBg  @åœ0òBg  @åœ0Czrtx 3090g  P­b0ÀBg  Üq¾$ðBg  Üq¾$ Czrtx 3080 tig  c†¿Bg  c†ïBg  Y­ÿBzrtx 3080g  jZ»Bg  œ.¶ëBg  œ.¶ûBzrtx 3070g  øIvv²Bg  ²gH|âBg  ²gH|òBÚt4g   }¶w�Bg  ˆôþŽÍBg  ˆôþŽÝBg  ˆôþŽíBg   h_¤Bg  ”HÔBg  ”H´Bg  ”HäBg  ”HôBg  `écÔ©Bg  H`¬ë¹Bg  ±3ñâéBg  ±3ñâùBg   xH£Bg   xH³Bg   xHãBg €±ð�FóBg  ðˆ„� Bg  <v°Bg  Â/|àBg  Â/|ðBg  pkG¦­Bg  pkG¦½Bg  *‰¬íBg  *‰¬ýBg  ÀêP`œBg  �ØáŽ¬Bg  ÀBw™Bg  ÀBw©Bg  ÀBwÙBg  @YØÔ�Bg  @YØÔ­B)	úquadro rtx 5000zrtx 2080 superzrtx 2080 tizrtx 2080zrtx 2070 superú	titan rtxúv100 sxmú	v100 pcieú
v100s pcieÚ_CUDA_FLOPSg  è�°vÄBg  ¬ÓŠ÷ÛBg  ª�CïBg  ª`teæB)Úv2Úv3Úv4Z	v5litepodr2   rT   c                 C   sx  | j dkrétj | ¡}| ¡ }d|v r,d|v rd}n�d|v r!d}n‰d|v s)d|v r+d	}n~d
|v r9d|v r6dnd
}nqd|v r[| d¡d }d}d|v rMd}nd|v rSd}d|› |› �}nOd|v rbd}nHd|v rid}nAd|v rpd}n:d|v rwd}n3d|v r~d}n,d|v r…d}n%d|v rŒd}nd|v r“d}nd|v ršd }nd!|v r¡d"}n	td#|›�ƒ d$S |tvrºtd#|›d%|›�ƒ d$S t| }|tju rÔd&d'l	m
} |ƒ rÔt ¡ d(krÔd)}||vrãt|›d*|› �ƒ d$S t|| ƒS | j d+k�r:d&d,lm} |rþd&d-lm}	 nd&d-lm}	 |	 ¡ }
|
 d.¡�p|
d/  d0¡d& }| ¡ }t|tƒ�s#J ‚|tv�r4td1|›d2|› �ƒ d$S tt| ƒS d$S )3z²Returns the available theoretical FLOPs.

    This is an optimistic upper limit that could only be achievable if only thick matmuls were run in a benchmark
    environment.

    ÚcudaÚh100Zhbm3rc   Znvlra   ZpcieZhbm2erd   rf   Zteslarg   zgeforce rtxú é   Ú rP   z superÚtiz tizrtx ri   rh   rj   rk   rl   rm   rn   zv100-sxmro   z	v100-pcierp   z
v100s-pcierq   zFLOPs not found for Nz
, chip is r   )Ú_is_ampere_or_laterZhighestrb   z does not support Úxla)Ú_XLA_GREATER_EQUAL_2_1)ÚtpuÚTYPEZACCELERATOR_TYPEú-zFLOPs not found for TPU z with )ÚtypeÚtorchrv   Úget_device_nameÚlowerÚsplitr   rr   Úfloat32Ú"lightning.fabric.accelerators.cudar|   Úget_float32_matmul_precisionrD   Ú!lightning.fabric.accelerators.xlar~   Ztorch_xla._internalr   Ztorch_xla.experimentalZget_tpu_envÚgetÚ
isinstancerE   Ú
_TPU_FLOPS)r2   rT   Údevice_nameZchipÚnumberÚextraZdtype_to_flopsr|   r~   r   Ztpu_envr#   r#   r$   rO     sŠ   
€

ðrO   Úpluginr   c           
      C   sÌ   ddl m}m}m}m}m}m}m}m}m	}	 t
| |ƒs"td| › �ƒ‚t
| |ƒr*| jS t
| ||fƒr4| jS t
| |ƒr<tjS t
| |	|fƒrF| jS t
| |ƒrNtjS t
| |ƒrZ| jjpYtjS t
| |ƒrbtjS t| ƒ‚)Nr   )	ÚBitsandbytesPrecisionÚDeepSpeedPrecisionÚDoublePrecisionÚFSDPPrecisionÚHalfPrecisionÚMixedPrecisionr   ÚTransformerEnginePrecisionÚXLAPrecisionz!Expected a precision plugin, got )Úlightning.fabric.pluginsr’   r“   r”   r•   r–   r—   r   r˜   r™   rŒ   r/   rT   Z_desired_input_dtyperƒ   ÚdoubleZ_desired_dtypeÚint8Úmixed_precision_configÚreduce_dtyper‡   ÚNotImplementedError)
r‘   r’   r“   r”   r•   r–   r—   r   r˜   r™   r#   r#   r$   rL   b  s$   ,





rL   ÚT)Úboundc                       sp   e Zd ZdZdeddf‡ fdd„Zedee fdd„ƒZ	e
d	eddfd
d„ƒZe
dededdfdd„ƒZ‡  ZS )r   zjCustom fixed size list that only supports right-append and ensures that all values increase monotonically.r   r   Nc                    s   t ƒ  ¡  || _d S r;   )rP   r%   r   )r"   r   rU   r#   r$   r%   ‰  s   

z_MonotonicWindow.__init__c                 C   s   t | ƒdkr
| d S d S )Nr   r1   )r.   r=   r#   r#   r$   Úlast�  s   z_MonotonicWindow.lastÚxc                 C   sR   | j }|d ur||krtd|› d|› �ƒ‚t | |¡ t| ƒ| jkr'| d= d S d S )Nz&Expected the value to increase, last: z, current: r   )r¢   r-   Úlistr,   r.   r   )r"   r£   r¢   r#   r#   r$   r,   “  s   
ÿz_MonotonicWindow.appendÚkeyÚvaluec                 C   s   t dƒ‚)Nz__setitem__ is not supported)rŸ   )r"   r¥   r¦   r#   r#   r$   Ú__setitem__�  s   z_MonotonicWindow.__setitem__)r?   r@   rA   rB   rD   r%   Úpropertyr	   r    r¢   r   r,   r   r§   rX   r#   r#   rU   r$   r   †  s    	 r   r;   )0Úcollectionsr   Útypingr   r   r   r   r   r   r	   r
   r   rƒ   Útyping_extensionsr   Ú"lightning.fabric.utilities.importsr   Ú$lightning.fabric.utilities.rank_zeror   r   Zlightning.fabricr   rš   r   rE   rD   rC   rF   r   rG   ÚnnÚModuleÚTensorr`   Úfloat64r‡   Úbfloat16Úfloat16rœ   rr   rT   Ú__annotations__r�   r2   rO   rL   r    r   r#   r#   r#   r$   Ú<module>   sÐ  , 77ýÿþý
ü)úýúõúíúã%úÛ-úÓ5úË@úÀHú¸Pú°Yú§aúŸiú—qú�yú‡ ú �þ ü �ó þüüüüü	ýýý �*½ Oõ&V!