
    j/                        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dS )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  T   modeltorch.nn.Moduleimgszintampboolbatchint | floatmax_num_objdataset_sizereturnc                    t          |          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, 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?)fractionr   r   N)r   	autobatchr   train)r   r   r   r   r   r   s         ^/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/ultralytics/utils/autobatch.pycheck_train_batch_sizer      s    2 
#			 
 
UOO!!##!E////C/////UUS#%
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   AA##A'*A'r   r   float
batch_sizec                   t          d          }t          j        | d d|dz   d           t          |                                           j        }|j        dv rt          j        | d|            |S t          j	        j
        j        rt          j        | d|            |S d	}d
t          j        dd                                          d          }	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           dk     rg dng d}dk    rfd|D             }| j                            dd          	 fd|D             }t/          || d||          fdt1          t3          |                    D             }|r	t3          | ng g f\  }}t5          j        ||d          }t9          t;          ||z            |d         z
  |d         z            }d v r:                    d           }|||         k    r|t?          |dz
  d                   }|dk     s|d!k    rt          j        | d"| d#| d$           |}dk    rtA          |          }t5          j!        ||          |z   |z   z  }t          j        | d%| d&|	 d'|z  dd(dd)|dz  d*d+           |t          j        "                                 S # tF          $ rG}t          j        | d,| d-| d$           |cY d }~t          j        "                                 S d }~ww xY w# t          j        "                                 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(       @   c                     g | ]
}|k    |S  r/   ).0br   s     r   
<listcomp>zautobatch.<locals>.<listcomp>`   s#    CCCQl1B1Bq1B1B1B    channels   c                >    g | ]}t          j        |          S r/   )torchempty)r0   r1   chr   s     r   r2   zautobatch.<locals>.<listcomp>c   s)    EEEAu{1b%//EEEr3   r   )ndevicer   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    )r0   ixyresultsts       r   r2   zautobatch.<locals>.<listcomp>g   s     
 
 
6Aq
 1Q4#u..	

 AaD1awq1u~1Aq8I1I1I !I
 2J1I1Ir3   )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warningr7   backendscudnn	benchmarkosgetenvstripcudaget_device_propertiestotal_memorymemory_reservedmemory_allocatednameyamlgetr	   	enumeratezipnppolyfitr   roundindexmaxminpolyvalempty_cache	Exception)r   r   r   r!   r   r   prefixr;   gbd
propertiesrafbatch_sizesimgxyfit_xfit_ypr1   r>   er9   rA   rB   s    `   `                 @@@r   r   r   3   s   , m$$F
K6ww%wwXX[^wwwxxx%""$$%%,F{n$$&bbV`bbccc~% &vvjtvvwww 
BC	0#66<<>>qACCA11&99J"$A
""6**R/A
##F++b0A	QUA
K6w1ww
ww1www1wwwVWwwwjkwwwwxxx '("ff""""2J2J2JKaCCCC+CCC	
A	&	&B!!EEEEEEEEc5Af+VVV
 
 
 
 
&s;'@'@AA
 
 
 $&3sBxxB8uJue+++q8|$$qt+qt3447??d##AKN""AE1.q55AHHNfjjAjj]gjjjkkkA!A|$$AJq!$$q(1,1vttttttALtttPQtttZbehZhttttuuu
 	
    	    &^^!^^Q[^^^___
    	 	
    s1   E4M 
N*#N%N*N- %N**N- - O)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   rN   copyr   numpyr[   r7   ultralytics.utilsr   r   r   ultralytics.utils.torch_utilsr   r	   r   r   r   r/   r3   r   <module>rx      s    r r " " " " " " 				            ; ; ; ; ; ; ; ; ; ; ? ? ? ? ? ? ? ?
  
  
  
  
  
J !'P! P! P! P! P! P! P!r3   