
    FPh                     n    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y)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  c                     t         j                  j                  j                  |      5  t	        t        |       j                         |      cddd       S # 1 sw Y   yxY w)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      fC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/utils/autobatch.pycheck_train_batch_sizer      s=     
	 	 	%%..0%8 
&	%	%s   #AA g333333?c                    t        d      }t        j                  | d|        t        | j	                               j
                  }|j                  dk(  rt        j                  | d|        |S t        j                  j                  j                  rt        j                  | d|        |S d}t        |      j                         }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       g d}	 |D cg c]  }t        j&                  |d||       }}t)        || d|      }|D cg c]
  }|s|d    }}t+        j,                  |dt/        |       |d      }t1        ||z  |d   z
  |d   z        }d|v r+|j3                  d      }|||   k\  r|t5        |dz
  d         }|dk  s|dkD  r|}t        j                  | d| d       t+        j6                  ||      |
z   |z   |	z  }t        j                  | d| d| d|	|z  d	d|	d	d|dz  dd        |S c c}w c c}w # t8        $ 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.67.
        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)                  )ndevicer   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nameemptyr   nppolyfitlenintindexmaxpolyval	Exceptionwarning)r   r   fraction
batch_sizeprefixr   gbd
propertiestrafbatch_sizesbimgresultsxypies                         r   r   r      s    m$F
KK6(A%IJ%""$%,,F{{evhNzl[\~~%%vhnoynz{| 
BFA11&9J"$A

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

##F+b0A	QUA
KK6(1#R
01S'1S'VWX[U\\ijkloippvwx #K8CD1u{{1a.D#u&9 #(7aaQqT7(JJ{7CF+QA6X!$!,-7?d#AKN"AE1.q5AHAKK6("ablammnopJJq!$q(1,1vh/s%s!AL;MRPQRUwVYZbehZhilYmmstu# E )  &!@C_`j_kklmnsC   J= 	J3(J= =J8J8C&J= 3
J= =	K0K+%K0+K0)r   T)__doc__copyr   numpyr1   r
   ultralytics.utilsr   r   r   ultralytics.utils.torch_utilsr   r   batchr        r   <module>rU      s3    r    ; ; 19" +:K:K :rT   