
    /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
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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.

    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   N)r   	autobatchr   train)r   r   r   r   r   s        `/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/autobatch.pycheck_train_batch_sizer      s    . 
#			 
 
UOO!!##UcE>O>O>O>OC>O>O>O>O>OUUUXfq
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   AA""A&)A&r   r   float
batch_sizec                J   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}| 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#           |}t5          j         ||          |
z   |z   z  }t          j        | d$| d%| d&|z  dd'dd(|dz  d)d*           |t          j        !                                 S # tD          $ rG}t          j        | d+| d,| d#           |cY d}~t          j        !                                 S d}~ww xY w# t          j        !                                 w xY w)-a9  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.

    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   c                >    g | ]}t          j        |          S  )torchempty).0bchr   s     r   
<listcomp>zautobatch.<locals>.<listcomp>Y   s)    EEEAu{1b%//EEE    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   )r3   ixyresultsts       r   r6   zautobatch.<locals>.<listcomp>]   s     
 
 
6Aq
 1Q4#u..	

 AaD1awq1u~1Aq8I1I1I !I
 2J1I1Ir7   )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
parametersr9   typewarningr1   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polyvalempty_cache	Exception)r   r   r   r    r   prefixr9   gbd
propertiesrafbatch_sizesimgxyfit_xfit_ypr4   r<   er5   r?   r@   s    `                    @@@r   r   r   -   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	&	&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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   1EL. .
M?8M:M?N :M??N  N")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   )__doc__
__future__r   rL   copyr   numpyrY   r1   ultralytics.utilsr   r   r   ultralytics.utils.torch_utilsr   r	   r   r   r   r0   r7   r   <module>ru      s    r r " " " " " " 				            ; ; ; ; ; ; ; ; ; ; ? ? ? ? ? ? ? ?
 
 
 
 
 
> !'J! J! J! J! J! J! J!r7   