§
    l¬¸j‚  ã                  óŒ   — d Z ddlmZ ddlmZ ddlZddlZddlm	Z	m
Z
mZ ddlmZmZmZ 	 	 	 	 	 ddd„Zdde	j        d
dfdd„ZdS )zkFunctions for estimating the best YOLO batch size to use a fraction of the available GPU memory in PyTorch.é    )Úannotations)ÚdeepcopyN)ÚDEFAULT_CFGÚLOGGERÚcolorstr)ÚautocastÚget_torch_device_backendÚprofile_opsé€  Téÿÿÿÿé   Úmodelútorch.nn.ModuleÚimgszÚintÚampúbool | torch.dtypeÚbatchÚfloatÚmax_num_objÚdataset_sizeÚreturnc                ó4  — t          |t          |                      ¦   «         ¦  «        j        j        ¬¦  «        5  t          t          | ¦  «                             ¦   «         |d|cxk     rdk     rn n|nd||¬¦  «        cddd¦  «         S # 1 swxY w Y   dS )a   Compute optimal YOLO training batch size using the autobatch() function.

    Args:
        model (torch.nn.Module): YOLO model to check batch size for.
        imgsz (int, optional): Image size used for training.
        amp (bool | torch.dtype, optional): Whether to use mixed precision, or the autocast dtype.
        batch (int | float, optional): Fraction of GPU memory to use. If -1, use default.
        max_num_obj (int, optional): The maximum number of objects from dataset.
        dataset_size (int, optional): Total number of training images. If > 0, batch size will not exceed this value.

    Returns:
        (int): Optimal batch size computed using the autobatch() function.

    Raises:
        RuntimeError: If no candidate batch size produces a usable profile.

    Notes:
        If 0.0 < batch < 1.0, it's used as the fraction of GPU memory to use.
        Otherwise, a default fraction of 0.6 is used.
    )ÚenabledÚdeviceg        g      ð?ç333333ã?)Úfractionr   r   N)r   ÚnextÚ
parametersr   ÚtypeÚ	autobatchr   Útrain)r   r   r   r   r   r   s         úk/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/utils/autobatch.pyÚcheck_train_batch_sizer$      sí   € õ8 
˜#¥d¨5×+;Ò+;Ñ+=Ô+=Ñ&>Ô&>Ô&EÔ&JÐ	KÑ	KÔ	Kð 
ð 
ÝÝ�U‰OŒO×!Ò!Ñ#Ô#ØØ! EÐ/Ð/Ò/Ð/¨CÒ/Ð/Ð/Ð/Ð/�U�U°SØ#Ø%ð
ñ 
ô 
ð
ð 
ð 
ð 
ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
øøøð 
ð 
ð 
ð 
ð 
ð 
s   »ABÂBÂBr   r   Ú
batch_sizec                ó¢  ‡‡‡‡‡‡‡— t          d¦  «        }t          j        |› d‰› d|dz  › d�¦  «         t          |                      ¦   «         ¦  «        j        }|j        dv rt          j        |› d|› �¦  «         |S |j        dk    r1t          j	        j
        j        rt          j        |› d	|› �¦  «         |S t          |¦  «        }d
}	|j                             ¦   «         › d|j        › �}
|                     |¦  «        }|j        |	z  Š|                     |¦  «        |	z  }|                     |¦  «        |	z  }‰||z   z
  }t          j        |› |
› d|j        › d‰d›d|d›d|d›d|d›d�¦  «         ‰dk     rg d¢ng d¢Š‰dk    rˆfd„‰D ¦   «         Š| j                             dd¦  «        Š	 ˆˆfd„‰D ¦   «         }t-          || d||¬¦  «        Šˆˆfd„t/          t1          ‰‰¦  «        ¦  «        D ¦   «         }|�r;t1          |Ž \  Š}t3          j        ‰|d¬¦  «        }t7          t9          ||z  ¦  «        |d         z
  |d         z  ¦  «        }t          ˆˆfd„t/          ‰¦  «        D ¦   «         d ¦  «        }|�|‰|         k    r‰|dz
           }|dk     s|d!k    rt          j        |› d"|› d#|› d$�¦  «         |}‰dk    rt;          |‰¦  «        }t3          j        ||¦  «        |z   |z   ‰z  }t          j        |› d%|› d&|
› d'‰|z  d›d(‰d›d)|dz  d*›d+�¦  «         ||                     ¦   «          S nJ# t@          $ r=}t          j        |› d,|› d-|› d$�¦  «         |cY d }~|                     ¦   «          S d }~ww xY w	 |                     ¦   «          n# |                     ¦   «          w xY wtC          |› d.‰› d/�¦  «        ‚)0a  Automatically estimate the best YOLO batch size to use a fraction of the available GPU memory.

    Args:
        model (torch.nn.Module): YOLO model to compute batch size for.
        imgsz (int, optional): The image size used as input for the YOLO model.
        fraction (float, optional): The fraction of available CUDA memory to use.
        batch_size (int, optional): The default batch size to use if an error is detected.
        max_num_obj (int, optional): The maximum number of objects from dataset.
        dataset_size (int, optional): Total number of training images. If > 0, batch size will not exceed this value.

    Returns:
        (int): The optimal batch size.

    Raises:
        RuntimeError: If no candidate batch size produces a usable profile.
    zAutoBatch: z'Computing optimal batch size for imgsz=z at éd   z% GPU memory utilization.>   ÚcpuÚmpsz3intended for GPU devices, using default batch-size ÚcudazHRequires torch.backends.cudnn.benchmark=False, using default batch-size i   @ú:z (z) z.2fz	G total, zG reserved, zG allocated, zG freeé   )r   é   é   é   r,   )r   r-   r.   r/   r,   é    é@   r   c                ó    •— g | ]
}|‰k    ¯|‘ŒS © r3   )Ú.0Úbr   s     €r#   ú
<listcomp>zautobatch.<locals>.<listcomp>f   s#   ø€ ÐCÐCÐC˜Q°°lÒ1BÐ1B�qÐ1BÐ1BÐ1Bó    Úchannelsé   c                ó>   •— g | ]}t          j        |‰‰‰¦  «        ‘ŒS r3   )ÚtorchÚempty)r4   r5   Úchr   s     €€r#   r6   zautobatch.<locals>.<listcomp>i   s)   ø€ ÐEÐEÐE°A�uŒ{˜1˜b %¨Ñ/Ô/ÐEÐEÐEr7   r   )Únr   r   c                óü   •— g | ]x\  }\  }}|rnt          |d          t          t          f¦  «        rLd|d          cxk     r‰k     r9n n6|dk    s&‰|dz
           r|d          ‰|dz
           d          k    ¯n||d          g‘ŒyS )r-   r   r   )Ú
isinstancer   r   )r4   ÚiÚxÚyÚresultsÚts       €€r#   r6   zautobatch.<locals>.<listcomp>m   s©   ø€ ð 
ð 
ð 
á�‘6�A�qØð
õ ˜1˜Qœ4¥#¥u Ñ.Ô.ð	
ð
 �A�a”D��’�˜1’����Ø�a’�˜w q¨1¡uœ~�°°1´¸ÀÀAÁ¼ÀqÔ8IÒ1IÐ1Ið ��!”ˆIð
 2JÐ1IÐ1Ir7   )Údegc              3  óJ   •K  — | ]\  }}|°‰|         ‰d          k    ¯|V — ŒdS )r   Nr3   )r4   rA   rC   Úbatch_sizesÚfit_xs      €€r#   ú	<genexpr>zautobatch.<locals>.<genexpr>y   sB   øè è € Ð`Ð`™d˜a ÀÐ`ÀkÐRSÄnÐW\Ð]^ÔW_ÒF_ÐF_˜ÐF_ÐF_ÐF_ÐF_Ð`Ð`r7   Ni   zbatch=z. outside safe range, using default batch-size ú.zUsing batch-size z for ú zG/zG (z.0fu   %) âœ…zerror detected: z,  using default batch-size z1no usable batch size found while profiling batch=zO. See the errors above, free GPU memory, reduce imgsz, or set batch explicitly.)"r   r   Úinfor   r   r   r    Úwarningr;   ÚbackendsÚcudnnÚ	benchmarkr	   ÚupperÚindexÚget_device_propertiesÚtotal_memoryÚmemory_reservedÚmemory_allocatedÚnameÚyamlÚgetr
   Ú	enumerateÚzipÚnpÚpolyfitr   ÚroundÚminÚpolyvalÚempty_cacheÚ	ExceptionÚRuntimeError)r   r   r   r%   r   r   Úprefixr   ÚacceleratorÚgbÚdÚ
propertiesÚrÚaÚfÚimgÚxyÚfit_yÚpr5   ÚoomÚerH   r=   rI   rD   rE   s    `   `                @@@@@r#   r!   r!   5   s  øøøøøøø€ õ2 �mÑ$Ô$€FÝ
„K�6ÐvÐvÀ%ÐvÐvÈXÐX[É^ÐvÐvÐvÑwÔwÐwÝ�%×"Ò"Ñ$Ô$Ñ%Ô%Ô,€FØ„{�nÐ$Ð$ÝŒ˜&ÐaÐaÐU_ÐaÐaÑbÔbÐbØÐØ„{�fÒÐ¥¤Ô!5Ô!?ÐÝŒ˜&ÐvÐvÐjtÐvÐvÑwÔwÐwØÐõ +¨6Ñ2Ô2€KØ	€BØŒ;×ÒÑÔÐ/Ð/ ¤Ð/Ð/€AØ×2Ò2°6Ñ:Ô:€JØÔ "Ñ$€AØ×#Ò# FÑ+Ô+¨bÑ0€AØ×$Ò$ VÑ,Ô,¨rÑ1€AØ	ˆQ�‰U‰€AÝ
„K�6Ðw˜1ÐwÐw 
¤ÐwÐw°1ÐwÐwÐwÀ1ÐwÐwÐwÐVWÐwÐwÐwÐjkÐwÐwÐwÐwÑxÔxÐxð '(¨"¢f fÐ"Ð"Ð"Ð"Ð2JÐ2JÐ2J€KØ�aÒÐØCÐCÐCÐC +ÐCÑCÔCˆØ	Œ�Š˜
 AÑ	&Ô	&€Bð!"ØEÐEÐEÐEÐE¸ÐEÑEÔEˆÝ˜c 5¨A°fÈ+ÐVÑVÔVˆð
ð 
ð 
ð 
ð 
å&¥s¨;¸Ñ'@Ô'@ÑAÔAð
ñ 
ô 
ˆð ñ 	Ý ˜8‰LˆE�5Ý”
˜5 %¨QÐ/Ñ/Ô/ˆAÝ•U˜1˜x™<Ñ(Ô(¨1¨Q¬4Ñ/°1°Q´4Ñ7Ñ8Ô8ˆAÝÐ`Ð`Ð`Ð`Ð`¥i°Ñ&8Ô&8Ð`Ñ`Ô`ÐbfÑgÔgˆCØˆ 1¨°CÔ(8Ò#8Ð#8Ø  a¡Ô(�Ø�1Šuˆu˜˜Dš˜Ý” &ÐnÐn°ÐnÐnÐakÐnÐnÐnÑoÔoÐoØ�Ø˜aÒÐÝ˜˜<Ñ(Ô(�åœ
 1 aÑ(Ô(¨1Ñ,¨qÑ0°AÑ5ˆHÝŒK˜6ÐxÐx°AÐxÐx¸AÐxÐxÀÀHÁÐxÐxÐxÐTUÐxÐxÐxÐ^fÐilÑ^lÐxÐxÐxÐxÑyÔyÐyØð
 	×ÒÑ!Ô!Ð!Ð!ð)	øõ  ð ð ð ÝŒ˜&Ð^Ð^°!Ð^Ð^ÐQ[Ð^Ð^Ð^Ñ_Ô_Ð_ØÐÐÐÐÐà×ÒÑ!Ô!Ð!Ð!øøøøð	øøøð!	ð( 	×ÒÑ!Ô!Ð!Ð!øˆ×ÒÑ!Ô!Ð!Ð!øøøå
Øð 	Yð 	YÀKð 	Yð 	Yð 	Yñô ð s7   Æ7E6M ÍN# Í
N
ÍNÍ+N
Í,N# ÎN
Î
N# Î#N9)r   Tr   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r%   r   r   r   r   r   r   r   )Ú__doc__Ú
__future__r   Úcopyr   Únumpyr]   r;   Úultralytics.utilsr   r   r   Úultralytics.utils.torch_utilsr   r	   r
   r$   r   r!   r3   r7   r#   ú<module>ry      sé   ðà qÐ qà "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð à Ð Ð Ð Ø €€€à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ Yð
 Ø"ØØØð#
ð #
ð #
ð #
ð #
ðP ØØ!Ô'ØØðYð Yð Yð Yð Yð Yð Yr7   