Ë
    Fêñi/  ã                  óÌ   — d Z ddlmZ ddl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 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd	„Zdd
e
j"                  ddf	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zy)zlFunctions for estimating the best YOLO batch size to use a fraction of the available CUDA memory in PyTorch.é    )ÚannotationsN)Údeepcopy)ÚDEFAULT_CFGÚLOGGERÚcolorstr)ÚautocastÚprofile_opsé€  é   c                ó´   — t        |¬«      5  t        t        | «      j                  «       |d|cxk  rdk  rn n|nd||¬«      cddd«       S # 1 sw Y   yxY w)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, optional): Use automatic mixed precision if True.
        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.

    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.
    )Úenabledg        g      ð?ç333333ã?)ÚfractionÚmax_num_objÚdataset_sizeN)r   Ú	autobatchr   Útrain)ÚmodelÚimgszÚampÚbatchr   r   s         ú]/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/utils/autobatch.pyÚcheck_train_batch_sizer      sS   € ô2 
˜#Ô	ñ 
ÜÜ�U‹O×!Ñ!Ó#ØØ! EÔ/¨CÕ/‘U°SØ#Ø%ô
÷
÷ 
ò 
ús   �7AÁAr   c                óp  — t        d«      }t        j                  |› d|› d|dz  › d�«       t        | j	                  «       «      j
                  }|j                  dv rt        j                  |› d|› �«       |S t        j                  j                  j                  rt        j                  |› d|› �«       |S d	}d
t        j                  dd«      j                  «       d   › �}	t        j                  j!                  |«      }
|
j"                  |z  }t        j                  j%                  |«      |z  }t        j                  j'                  |«      |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kD  r|D �cg c]
  }||k  sŒ	|‘Œ }}| j*                  j-                  dd«      }	 |D �cg c]  }t        j.                  ||||«      ‘Œ }}t1        || d||¬«      }t3        t5        ||«      «      D ���cg c]Y  \  }\  }}|rOt7        |d   t8        t:        f«      r6d|d   cxk  r|k  r(n n%|dk(  s||dz
     r|d   ||dz
     d   kD  r||d   g‘Œ[ }}}}|rt5        |Ž ng g f\  }}t=        j>                  ||d¬«      }t9        tA        ||z  «      |d   z
  |d   z  «      }d|v r+|jC                  d«      }|||   k\  r|tE        |dz
  d«         }|dk  s|dkD  r t        j                  |› d |› d!|› d"�«       |}|dkD  rtG        ||«      }t=        jH                  ||«      |z   |z   |z  }t        j                  |› d#|› d$|	› d%||z  d›d&|d›d'|dz  d(›d)�«       |t        j                  jK                  «        S c c}w c c}w c c}}}w # tL        $ rH}t        j                  |› d*|› d+|› d"�«       |cY d}~t        j                  jK                  «        S d}~ww xY w# t        j                  jK                  «        w xY w),a¯  Automatically estimate the best YOLO batch size to use a fraction of the available CUDA 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.
    zAutoBatch: z'Computing optimal batch size for imgsz=z at éd   z% CUDA memory utilization.>   ÚcpuÚmpsz4intended for CUDA devices, using default batch-size zHRequires torch.backends.cudnn.benchmark=False, using default batch-size i   @zCUDA:ÚCUDA_VISIBLE_DEVICESÚ0r   z (z) z.2fz	G total, zG reserved, zG allocated, zG freeé   )r   é   é   é   r    )r   r!   r"   r#   r    é    é@   Úchannelsé   r   )ÚnÚdevicer   r!   )ÚdegNi   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 )'r   r   ÚinfoÚnextÚ
parametersr)   ÚtypeÚwarningÚtorchÚbackendsÚcudnnÚ	benchmarkÚosÚgetenvÚstripÚcudaÚget_device_propertiesÚtotal_memoryÚmemory_reservedÚmemory_allocatedÚnameÚyamlÚgetÚemptyr	   Ú	enumerateÚzipÚ
isinstanceÚintÚfloatÚnpÚpolyfitÚroundÚindexÚmaxÚminÚpolyvalÚempty_cacheÚ	Exception)r   r   r   Ú
batch_sizer   r   Úprefixr)   ÚgbÚdÚ
propertiesÚtÚrÚaÚfÚbatch_sizesÚbÚchÚimgÚresultsÚiÚxÚyÚxyÚfit_xÚfit_yÚpÚes                               r   r   r   3   sP  € ô, �mÓ$€FÜ
‡K�K�6�(ÐAÀ%ÀÈÈXÐX[É^ÐL\Ð\vÐwÔxÜ�%×"Ñ"Ó$Ó%×,Ñ,€FØ‡{�{�nÑ$Ü�‰˜&˜Ð!UÐV`ÐUaÐbÔcØÐÜ‡~�~×Ñ×%Ò%Ü�‰˜&˜Ð!iÐjtÐiuÐvÔwØÐð 
€BØ
”—	‘	Ð0°#Ó6×<Ñ<Ó>¸qÑAÐBÐC€AÜ—‘×1Ñ1°&Ó9€JØ×Ñ "Ñ$€AÜ�
‰
×"Ñ" 6Ó*¨RÑ/€AÜ�
‰
×#Ñ# FÓ+¨bÑ0€AØ	ˆQ�‰U‰€AÜ
‡K�K�6�(˜1˜#˜R 
§¡Ð0°°1°S°'¸À1ÀSÀ'ÈÐVWÐX[ÐU\Ð\iÐjkÐloÐipÐpvÐwÔxð '(¨"¢fÓ"Ò2J€KØ�aÒØ"-ÖC˜Q°°lÓ1B’qÐCˆÐCØ	�‰�‰˜
 AÓ	&€Bð!!Ø9DÖE°AŒu�{‰{˜1˜b %¨Õ/ÐEˆÐEÜ˜c 5¨A°fÈ+ÔVˆô
 '¤s¨;¸Ó'@ÓA÷
ð 
á�‘6�A�qÙÜ˜1˜Q™4¤#¤u Ô.Ø�A�a‘D”˜1•Ø�a’˜w q¨1¡uš~°°1±¸ÀÀAÁ¹ÀqÑ8IÒ1Ið ��!‘ŠIð
ˆò 
ñ $&”s˜B‘x¨B°¨8‰ˆˆuÜ�J‰J�u˜e¨Ô+ˆÜ”�q˜8‘|Ó$ q¨¡tÑ+¨q°©tÑ3Ó4ˆØ�7‰?Ø—‘˜dÓ#ˆAØ�K ‘NÒ"Ø¤ A¨¡E¨1£Ñ.�ØˆqŠ5�A˜’HÜ�N‰N˜f˜X V¨A¨3Ð.\Ð]gÐ\hÐhiÐjÔkØˆAØ˜!ÒÜ�A�|Ó$ˆAä—J‘J˜q !Ó$ qÑ(¨1Ñ,°Ñ1ˆÜ�‰�v�hÐ/°¨s°%¸°s¸!¸AÀ¹LÈÐ;MÈRÐPQÐRUÈwÐVYÐZbÐehÑZhÐilÐYmÐmsÐtÔuØô
 	�
‰
×ÑÕ ùòG Dùò Fùô
øô0 ò Ü�‰˜&˜Ð!1°!°Ð4PÐQ[ÐP\Ð\]Ð^Ô_ØÔä�
‰
×ÑÕ ûð	ûô 	�
‰
×ÑÕ ús\   Æ-
N0Æ8N0ÇO ÇN5Ç>+O È)AN:ÊD
O Î5O Ï	PÏ
PÏ)PÏ*P ÐPÐP Ð P5)r
   Téÿÿÿÿr   r   )r   útorch.nn.Moduler   rE   r   Úboolr   zint | floatr   rE   r   rE   ÚreturnrE   )r   rg   r   rE   r   rF   rP   rE   r   rE   r   rE   ri   rE   )Ú__doc__Ú
__future__r   r6   Úcopyr   ÚnumpyrG   r2   Úultralytics.utilsr   r   r   Úultralytics.utils.torch_utilsr   r	   r   r   r   © ó    r   ú<module>rr      sß   ðá rå "ã 	Ý ã Û ç ;Ñ ;ß ?ð
 ØØØØð 
Øð 
àð 
ð 
ð 
ð ð	 
ð
 ð 
ð ð 
ð 	ó 
ðJ ØØ!×'Ñ'ØØðP!ØðP!àðP!ð ðP!ð ð	P!ð
 ðP!ð ðP!ð 	ôP!rq   