§
    ezni  ã                   óf   — d 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 dd„Zdd	ej        fd
„ZdS )zlFunctions for estimating the best YOLO batch size to use a fraction of the available CUDA memory in PyTorch.é    )ÚdeepcopyN)ÚDEFAULT_CFGÚLOGGERÚcolorstr)Úprofileé€  Tc                 óÜ   — t           j        j                             |¦  «        5  t	          t          | ¦  «                             ¦   «         |¦  «        cddd¦  «         S # 1 swxY w Y   dS )as  
    Check YOLO training batch size using the autobatch() function.

    Args:
        model (torch.nn.Module): YOLO model to check batch size for.
        imgsz (int): Image size used for training.
        amp (bool): If True, use automatic mixed precision (AMP) for training.

    Returns:
        (int): Optimal batch size computed using the autobatch() function.
    N)ÚtorchÚcudaÚampÚautocastÚ	autobatchr   Útrain)ÚmodelÚimgszr   s      ú{/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/ultralytics/utils/autobatch.pyÚcheck_train_batch_sizer      sŸ   € õ 
ŒŒ×	 Ò	  Ñ	%Ô	%ð 9ð 9Ý� %™œ×.Ò.Ñ0Ô0°%Ñ8Ô8ð9ð 9ð 9ð 9ñ 9ô 9ð 9ð 9ð 9ð 9ð 9ð 9øøøð 9ð 9ð 9ð 9ð 9ð 9s   ¥/A!Á!A%Á(A%g333333ã?c                 óº  ‡— t          d¦  «        }t          j        |› d‰› �¦  «         t          |                      ¦   «         ¦  «        j        }|j        dk    rt          j        |› d|› �¦  «         |S t          j        j	        j
        rt          j        |› d|› �¦  «         |S d}t          |¦  «                             ¦   «         }t          j                             |¦  «        }|j        |z  }	t          j                             |¦  «        |z  }
t          j                             |¦  «        |z  }|	|
|z   z
  }t          j        |› |› d|j        › d|	d	›d
|
d	›d|d	›d|d	›d�¦  «         g d¢}	 ˆfd„|D ¦   «         }t'          || d|¬¦  «        }d„ |D ¦   «         }t)          j        |dt-          |¦  «        …         |d¬¦  «        }t/          ||z  |d         z
  |d         z  ¦  «        }d|v r:|                     d¦  «        }|||         k    r|t3          |dz
  d¦  «                 }|dk     s|dk    r|}t          j        |› d|› d�¦  «         t)          j        ||¦  «        |
z   |z   |	z  }t          j        |› d|› d|› d|	|z  d	›d|	d	›d|dz  d ›d!�¦  «         |S # t6          $ r)}t          j        |› d"|› d#|› d�¦  «         |cY d}~S d}~w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. Defaults to 640.
        fraction (float, optional): The fraction of available CUDA memory to use. Defaults to 0.60.
        batch_size (int, optional): The default batch size to use if an error is detected. Defaults to 16.

    Returns:
        (int): The optimal batch size.
    zAutoBatch: z'Computing optimal batch size for imgsz=Úcpuz0CUDA not detected, using default CPU batch-size uP    âš ï¸� Requires 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)é   é   é   é   é   c                 ó>   •— g | ]}t          j        |d ‰‰¦  «        ‘ŒS )é   )r
   Úempty)Ú.0Úbr   s     €r   ú
<listcomp>zautobatch.<locals>.<listcomp>D   s)   ø€ ÐDÐDÐD°1�uŒ{˜1˜a ¨Ñ.Ô.ÐDÐDÐDó    r   )ÚnÚdevicec                 ó"   — g | ]}|¯|d          ‘ŒS )r   © )r   Úxs     r   r    zautobatch.<locals>.<listcomp>H   s!   € Ð(Ð(Ð(�a aÐ(ˆQˆqŒTÐ(Ð(Ð(r!   Nr   )Údegr   i   u?   WARNING âš ï¸� CUDA anomaly detected, using default batch-size ú.zUsing batch-size z for ú zG/zG (éd   z.0fu   %) âœ…u   WARNING âš ï¸� error detected: z,  using default batch-size )r   r   ÚinfoÚnextÚ
parametersr#   Útyper
   ÚbackendsÚcudnnÚ	benchmarkÚstrÚupperr   Úget_device_propertiesÚtotal_memoryÚmemory_reservedÚmemory_allocatedÚnamer   ÚnpÚpolyfitÚlenÚintÚindexÚmaxÚpolyvalÚ	ExceptionÚwarning)r   r   ÚfractionÚ
batch_sizeÚprefixr#   ÚgbÚdÚ
propertiesÚtÚrÚaÚfÚbatch_sizesÚimgÚresultsÚyÚpr   ÚiÚes    `                   r   r   r      sw  ø€ õ �mÑ$Ô$€FÝ
„K�6ÐIÐIÀ%ÐIÐIÑJÔJÐJÝ�%×"Ò"Ñ$Ô$Ñ%Ô%Ô,€FØ„{�eÒÐÝŒ�vÐ[Ð[ÈzÐ[Ð[Ñ\Ô\Ð\ØÐÝ„~ÔÔ%ð ÝŒ�vÐ{Ð{ÐoyÐ{Ð{Ñ|Ô|Ð|ØÐð 
€BÝˆF‰Œ×ÒÑÔ€AÝ”×1Ò1°&Ñ9Ô9€JØÔ "Ñ$€AÝŒ
×"Ò" 6Ñ*Ô*¨RÑ/€AÝŒ
×#Ò# FÑ+Ô+¨bÑ0€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ð #Ð"Ð"€KðØDÐDÐDÐD¸ÐDÑDÔDˆÝ˜#˜u¨°&Ð9Ñ9Ô9ˆð )Ð(˜7Ð(Ñ(Ô(ˆÝŒJ�{ 7¥C¨¡F¤F 7Ô+¨Q°AÐ6Ñ6Ô6ˆÝ��X‘  !¤Ñ$¨¨!¬Ñ,Ñ-Ô-ˆØ�7ˆ?ˆ?Ø—’˜dÑ#Ô#ˆAØ�K ”NÒ"Ð"Ø¥ A¨¡E¨1¡¤Ô.�ØˆqŠ5ˆ5�A˜’H�HØˆAÝŒK˜6ÐoÐoÐblÐoÐoÐoÑpÔpÐpå”J˜q !Ñ$Ô$ qÑ(¨1Ñ,°Ñ1ˆÝŒ�vÐtÐt°ÐtÐt¸ÐtÐt¸AÀ¹LÐtÐtÐtÐPQÐtÐtÐtÐZbÐehÑZhÐtÐtÐtÐtÑuÔuÐuØˆøÝð ð ð ÝŒ˜&ÐmÐmÀÐmÐmÐ`jÐmÐmÐmÑnÔnÐnØÐÐÐÐÐÐøøøøðøøøs   Å5D1J' Ê'
KÊ1KËKËK)r   T)Ú__doc__Úcopyr   Únumpyr9   r
   Úultralytics.utilsr   r   r   Úultralytics.utils.torch_utilsr   r   Úbatchr   r%   r!   r   ú<module>rY      s    ðà rÐ rà Ð Ð Ð Ð Ð à Ð Ð Ð Ø €€€à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1ð9ð 9ð 9ð 9ð" ¨¸+Ô:Kð :ð :ð :ð :ð :ð :r!   