
    h                        S r SSKJr  SSKrSSKJr  SSKrSSKrSSK	J
r
JrJr  SSKJrJr      S           SS	 jjrSS
\
R"                  S4           SS jjrg)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                    [        US9   [        [        U 5      R                  5       USUs=:  a  S:  a  O  OUOSUS9sSSS5        $ ! , (       d  f       g= f)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?)fractionmax_num_objN)r   	autobatchr   train)modelimgszampbatchr   s        U/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/utils/autobatch.pycheck_train_batch_sizer      sF    0 
#	UO!!#UcE>OC>OUUXfq
 
		s   5A		
Ar   c                @   [        S5      n[        R                  " U SU SUS-   S35        [        U R	                  5       5      R
                  nUR                  S;   a  [        R                  " U SU 35        U$ [        R                  R                  R                  (       a  [        R                  " U SU 35        U$ S	nS
[        R                  " SS5      R                  5       S    3n[        R                  R!                  U5      n	U	R"                  U-  n
[        R                  R%                  U5      U-  n[        R                  R'                  U5      U-  nXU-   -
  n[        R                  " U U SU	R(                   SU
S SUS SUS SUS S35        U
S:  a  / SQO/ SQn U Vs/ s H  n[        R*                  " USX5      PM     nn[-        UU SXdS9n[/        [1        UU5      5       VVVs/ s Hv  u  nu  nnU(       d  M  [3        US   [4        [6        45      (       d  M2  SUS   s=:  a  U
:  d  MD  O  MH  US:X  d!  UUS-
     (       a  US   UUS-
     S   :  d  Mo  UUS   /PMx     nnnnU(       a  [1        U6 O/ / 4u  nn[8        R:                  " UUSS9n[5        [=        X-  5      US   -
  US   -  5      nSU;   a+  UR?                  S5      nXU   :  a  U[A        US-
  S5         nUS:  d  US:  a!  [        R                  " U SU S U S!35        Un[8        RB                  " UU5      U-   U-   U
-  n[        R                  " U S"U S#U S$X-  S S%U
S S&US-  S' S(35        U[        R                  RE                  5         $ s  snf s  snnnf ! [F         aI  n[        R                  " U S)U S*U S!35        Us SnA[        R                  RE                  5         $ SnAff = f! [        R                  RE                  5         f = f)+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.

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       @      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emptyr	   	enumeratezip
isinstanceintfloatnppolyfitroundindexmaxpolyvalempty_cache	Exception)r   r   r   
batch_sizer   prefixr'   gbd
propertiestrafbatch_sizesbimgresultsixyxyfit_xfit_ypes                             r   r   r   .   s   * m$F
KK6(A%XX[^L\\vwx%""$%,,F{{n$&!UV`Uabc~~%%&!ijtiuvw 
B
		0#6<<>qABCA11&9J"$A

""6*R/A

##F+b0A	UA
KK6(1#R
01S'1S'VWX[U\\ijkloippvwx '("f"2JK!8CD1u{{1a.Dc5AfV
 's;'@A
A	6Aq  1Q4#u.  AaD1	  	 
 awq1u~1Aq8I1I Q!IA 	 
 $&sBxB8uJJue+q|$qt+qt347?d#AN"AE1.q5AHNNfXVA3.\]g\hhijkAJJq!$q(1,1vh/s%s!AL;MRPQRUwVYZbehZhilYmmstu
 	

 = E
,  &!1!4PQ[P\\]^_

 	 	

 sm   1N' 5"N'N' >N N 3N N 	#N 0N ;DN' N' '
O:1 O5O:O= 5O::O= = P)r
   Tr   )r   torch.nn.Moduler   rA   r   boolr   zint | floatr   rA   returnrA   )r   ra   r   rA   r   rB   rK   rA   r   rA   rc   rA   )__doc__
__future__r   r4   copyr   numpyrC   r0   ultralytics.utilsr   r   r   ultralytics.utils.torch_utilsr   r	   r   r   r        r   <module>rl      s    r " 	    ; ; ?
 


 

 	

 
 	
@ !''J!J!J! J! 	J!
 J! 	J!rk   