§
    ­¸j�&  ã                  ó^  — d Z ddlmZ ddlZddlmZmZmZ ddlZddlm	Z	m
Z
 ddlmZ ddlmZ  e¦   «         Zee	e	geeef         f         Zeeedf         eedf         ej        ej        ej        eeeej        dz  f	         Zdd„Zdd„Z G d„ de
j        ¦  «        Z G d„ d¦  «        ZdS )z@Per-signature CUDA graph capture for RF-DETR's training forward.é    )ÚannotationsN)ÚAnyÚCallableÚcast)ÚTensorÚnn)Ú
get_logger)ÚNestedTensor.ÚreturnÚboolc                 ór   — 	 t          j        d¦  «        S # t          $ r t          j        ¦   «         cY S w xY w)z=Return CUDA autocast state across supported PyTorch versions.Úcuda)ÚtorchÚis_autocast_enabledÚ	TypeError© ó    úo/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/training/cuda_graph_step.pyÚ_cuda_autocast_enabledr   #   sH   € ð+ÝÔ(¨Ñ0Ô0Ð0øÝð +ð +ð +ÝÔ(Ñ*Ô*Ð*Ð*Ð*ð+øøøs   ‚ –6µ6útorch.dtypec                 ón   — t          t          dd¦  «        } | � | d¦  «        nt          j        ¦   «         S )z=Return CUDA autocast dtype across supported PyTorch versions.Úget_autocast_dtypeNr   )Úgetattrr   Úget_autocast_gpu_dtype)Ú	get_dtypes    r   Ú_cuda_autocast_dtyper   +   s7   € å�Ð3°TÑ:Ô:€IØ )Ð 5ˆ9ˆ9�VÑÔÐ½5Ô;WÑ;YÔ;YÐYr   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd„Zˆ xZS )Ú_GraphableForwardzâAdapt ``LWDETR`` inputs to the Tensor-only CUDA graph API.

    ``LWDETR.forward`` does not read ``targets``; the variable-length targets remain outside capture and enter the
    unchanged eager criterion after this call.
    Úinnerú	nn.Moduler   ÚNonec                óV   •— t          ¦   «                              ¦   «          || _        d S ©N)ÚsuperÚ__init__r   )Úselfr   Ú	__class__s     €r   r%   z_GraphableForward.__init__8   s$   ø€ Ý‰Œ×ÒÑÔÐØˆŒ
ˆ
ˆ
r   Útensorsr   Úmaskúdict[str, Any]c           	     ó”   — t          t          t          t          f         |                      t          ||¦  «        d¦  «        ¦  «        S )z3Forward Tensor inputs through the wrapped detector.N)r   ÚdictÚstrr   r   r
   )r&   r(   r)   s      r   Úforwardz_GraphableForward.forward<   s2   € å•D��c˜”N D§J¢J­|¸GÀTÑ/JÔ/JÈDÑ$QÔ$QÑRÔRÐRr   )r   r    r   r!   )r(   r   r)   r   r   r*   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r.   Ú__classcell__)r'   s   @r   r   r   1   sc   ø€ € € € € ðð ðð ð ð ð ð ðSð Sð Sð Sð Sð Sð Sð Sr   r   c                  ó<   — e Zd ZdZdddœdd„Zddd„Zd d„Zd!d„ZdS )"ÚCudaGraphTrainingRunneraÎ  Capture and replay a training forward for each static execution signature.

    This deliberately is not an :class:`~torch.nn.Module`: the original model remains
    registered on the Lightning module, so checkpoint keys, optimizers, and EMA keep the
    same parameter ownership and names. Capture failures stop training because an
    invalidated capture can leave CUDA state unsafe for an eager retry.

    Args:
        inner: Detection model to execute.
        num_warmup_iters: Warmup iterations used by ``make_graphed_callables``.
        fp8_recipe: Lightning's active Transformer Engine recipe. When supplied, use
            Transformer Engine's FP8-aware capture API for one fixed execution signature.
    é   N)Ú
fp8_reciper   r    Únum_warmup_itersÚintr7   ú
Any | Noner   r!   c               ó^  — || _         || _        || _        d| _        i | _        |�Jddlm} h d£}|t          t          j	        |¦  «        j
        ¦  «        k    st          d¦  «        ‚|| _        t          |dd¦  «        }t          |dd¦  «        }t          |¦  «        r |¦   «          dS dS )zPSelect and validate the capture backend without replacing registered parameters.Nr   )Úmake_graphed_callables>   ÚrecipeÚenabledÚcache_quantized_paramsÚclone_param_grads_on_returnz©FP8 CUDA graphs require the Transformer Engine 2.19 capture API, including clone_param_grads_on_return. Install a compatible Transformer Engine or set cuda_graphs=False.ÚtransformerÚenable_cuda_graph_capture)r   r8   r7   Ú_te_captureÚ_graphed_cacheÚtransformer_engine.pytorchr<   ÚsetÚinspectÚ	signatureÚ
parametersÚRuntimeErrorr   Úcallable)r&   r   r8   r7   r<   ÚrequiredrA   Úenable_captures           r   r%   z CudaGraphTrainingRunner.__init__P   sà   € àˆŒ
Ø 0ˆÔØ$ˆŒØ6:ˆÔØEGˆÔàÐ!àIÐIÐIÐIÐIÐIàeÐeÐeˆHØ�s¥7Ô#4Ð5KÑ#LÔ#LÔ#WÑXÔXÒXÐXÝ"ðuñô ð ð  6ˆDÔå˜e ]°DÑ9Ô9ˆÝ  Ð.IÈ4ÑPÔPˆÝ�NÑ#Ô#ð 	ØˆNÑÔÐÐÐð	ð 	r   Úsamplesr
   Útargetsúlist[dict[str, Tensor]] | Noner*   c           
     óJ  — |                      ¦   «         \  }}| j        j        r|€;t          t          t
          t          f         |                      ||¦  «        ¦  «        S t          ¦   «         }t          |j	        ¦  «        t          |j	        ¦  «        |j
        |j
        |j        |j        |j        ||rt          ¦   «         ndf	}| j                             |¦  «        }|€›| j        �}| j        r7t#          t%          | j        ¦  «        ¦  «        }t'          d|› d|› d�¦  «        ‚t)          d„ | j                             ¦   «         D ¦   «         ¦  «        rt'          d¦  «        ‚|                      |||¦  «        }| j        €|                      ¦   «           |||¦  «        S )z±Capture or replay training inputs; leave evaluation inputs eager.

        Raises:
            RuntimeError: If CUDA capture fails; restart the process before retrying.
        Nz@FP8 CUDA graphs require one fixed execution signature; captured z, got zR. Keep batch size, resolution, dtype and autocast fixed, or set cuda_graphs=False.c              3  ó(   K  — | ]}|j         d uV — Œd S r#   )Úgrad)Ú.0Ú	parameters     r   ú	<genexpr>z3CudaGraphTrainingRunner.__call__.<locals>.<genexpr>‹   s)   è è € Ð[Ð[°i�y”~¨TÐ1Ð[Ð[Ð[Ð[Ð[Ð[r   z@FP8 CUDA graph capture requires no existing parameter gradients.)Ú	decomposer   Útrainingr   r,   r-   r   r   ÚtupleÚshapeÚdtypeÚdeviceÚrequires_gradr   rD   Úgetr7   ÚnextÚiterrJ   ÚanyrI   Ú_try_captureÚ_own_accumulated_gradients)	r&   rN   rO   r(   r)   Úautocast_enabledÚkeyÚgraphedÚ
cached_keys	            r   Ú__call__z CudaGraphTrainingRunner.__call__i   s¸  € ð  ×)Ò)Ñ+Ô+‰ˆ�ØŒzÔ"ð 	F d lÝ��S¥#˜Xœ¨¯
ª
°7¸GÑ(DÔ(DÑEÔEÐEå1Ñ3Ô3Ðå�'”-Ñ Ô Ý�$”*ÑÔØŒMØŒJØŒNØÔ!ØÔØØ&6Ð@Õ Ñ"Ô"Ð"¸Dð

ˆð Ô%×)Ò)¨#Ñ.Ô.ˆØˆ?ØŒÐ*ð Ô&ð å!%¥d¨4Ô+>Ñ&?Ô&?Ñ!@Ô!@�JÝ&ðkÐ[eð kð kÐmpð kð kð kñô ð õ Ð[Ð[À4Ä:×CXÒCXÑCZÔCZÐ[Ñ[Ô[Ñ[Ô[ð kÝ&Ð'iÑjÔjÐjØ×'Ò'¨°°sÑ;Ô;ˆGØŒ?Ð"Ø×+Ò+Ñ-Ô-Ð-Øˆw�w Ñ%Ô%Ð%r   c                óˆ   — | j                              ¦   «         D ]'}|j        �|j                             ¦   «         |_        Œ(dS )aa  Move live gradients out of the static buffers a previous graphed backward handed to autograd.

        ``make_graphed_callables`` returns its static gradient buffers from every backward. When ``.grad`` was ``None``,
        autograd adopts the buffer itself instead of copying it, and the next replay overwrites that buffer before
        autograd accumulates into it, turning ``g0 + g1`` into ``2 * g1``. Copying before the replay costs one gradient-
        sized copy per microbatch after the first of an accumulation window and nothing when
        ``zero_grad(set_to_none=True)`` ran in between.
        N)r   rI   rS   Úclone)r&   rU   s     r   rc   z2CudaGraphTrainingRunner._own_accumulated_gradients’   sI   € ð œ×.Ò.Ñ0Ô0ð 	8ð 	8ˆIØŒ~Ð)Ø!*¤×!5Ò!5Ñ!7Ô!7�	”øð	8ð 	8r   r(   r   r)   re   Ú_ExecutionKeyÚ_GraphedCallablec                óø  — t          | j        ¦  «        }t          j        ¦   «         }t	          ¦   «         o|}	 |rt          j        d¦  «         | j        �=t          t          |                      |||f| j	        dd| j
        dd¬¦  «        ¦  «        }n=t          t          t          j                             |||f| j	        d¬¦  «        ¦  «        }n&# t          $ r}t          d|› d�¦  «        |‚d}~ww xY w|rt          j        |¦  «         n# |rt          j        |¦  «         w w xY w|| j        |<   |�^}	}
}}}t                                dt%          | j        ¦  «        |	|
|r|nd	| j
        �d
nd¦  «         |S )zCCapture one signature without modifying live accumulated gradients.FNT)r8   Úallow_unused_inputr>   r=   r?   r@   )r8   rn   z2CUDA graph capture failed for execution signature zu. Training cannot safely continue after failed capture. Restart the process and set cuda_graphs=False to run eagerly.z{Captured CUDA graph %d for input shape %s (mask %s, autocast %s); later batches with this signature replay it. Backend: %s.ÚoffzTransformer Engine FP8ÚPyTorch)r   r   r   Úis_autocast_cache_enabledr   Úset_autocast_cache_enabledrC   r   rl   r8   r7   r   r<   Ú	ExceptionrJ   rD   ÚloggerÚinfoÚlen)r&   r(   r)   re   Ú	graphableÚautocast_cache_enabledÚdisable_autocast_cacherf   ÚexcÚtensor_shapeÚ
mask_shapeÚ_restrd   Úautocast_dtypes                 r   rb   z$CudaGraphTrainingRunner._try_captureŸ   sö  € å% d¤jÑ1Ô1ˆ	Ý!&Ô!@Ñ!BÔ!BÐÝ!7Ñ!9Ô!9Ð!TÐ>TÐð'	IØ%ð 8õ Ô0°Ñ7Ô7Ð7ØÔÐ+õ Ý$Ø×$Ò$Ø!Ø  $˜Ø)-Ô)>Ø+/Ø $Ø#œØ/4Ø48ð %ñ 	ô 	ñô ��õ Ý$Ý”J×5Ò5Ø!Ø  $˜Ø)-Ô)>Ø+/ð	 6ñ ô ñô �øøõ ð 	ð 	ð 	Ýð<ÀSð <ð <ð <ñô ð ð	øøøøð	øøøð &ð IÝÔ0Ð1GÑHÔHÐHøøð &ð IÝÔ0Ð1GÑHÔHÐHÐHðIøøøð $+ˆÔ˜CÑ ØMPÑJˆ�j 5Ð*:¸NÝ�ŠðHå�Ô#Ñ$Ô$ØØØ.Ð9ˆNˆN°EØ(,¬Ð(CÐ$Ð$Èñ	
ô 	
ð 	
ð ˆs*   ¹BC ÃD Ã
C4ÃC/Ã/C4Ã4D ÄD')r6   )r   r    r8   r9   r7   r:   r   r!   r#   )rN   r
   rO   rP   r   r*   )r   r!   )r(   r   r)   r   re   rk   r   rl   )r/   r0   r1   r2   r%   rh   rc   rb   r   r   r   r5   r5   A   s†   € € € € € ðð ðÐ`dð ð ð ð ð ð ð2'&ð '&ð '&ð '&ð '&ðR8ð 8ð 8ð 8ð9ð 9ð 9ð 9ð 9ð 9r   r5   )r   r   )r   r   )r2   Ú
__future__r   rG   Útypingr   r   r   r   r   r   Úrfdetr.utilities.loggerr	   Úrfdetr.utilities.tensorsr
   rt   r,   r-   rl   rY   r9   r[   r\   r   rk   r   r   ÚModuler   r5   r   r   r   ú<module>r„      s—  ðð GÐ Fà "Ð "Ð "Ð "Ð "Ð "à €€€Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à €€€Ø Ð Ð Ð Ð Ð Ð Ð à .Ð .Ð .Ð .Ð .Ð .Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1à	ˆ‰Œ€à˜V VÐ,¨d°3¸°8¬nÐ<Ô=Ð ØØ	ˆ#ˆsˆ(„OØ	ˆ#ˆsˆ(„OØ	„KØ	„KØ	„LØØØØ	„K�$Ñðô
€ð+ð +ð +ð +ðZð Zð Zð ZðSð Sð Sð Sð S˜œ	ñ Sô Sð Sð Wð Wð Wð Wð Wñ Wô Wð Wð Wð Wr   