
    juM                        d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZmZmZmZmZmZmZmZmZ  ed          Z e j        dd	                              d
          Ze dZd Z	 erJ  ej        dd          du s" e j        d          s ej        d          sJ  e j        d          p ej        d          ZesJ d dlZd dl m!Z!m"Z" d dl#m$Z$  ed          Z%n# e&e'f$ r dZY nw xY wd)dZ(d*dZ)d Z*d+dZ+d,dZ,d Z-d-dZ.d,d Z/d! Z0d" Z1d# Z2d$ Z3d% Z4d& Z5d' Z6ere2e3e4e5e6d(ni Z7dS ).    N)ThreadPoolExecutor)isfinite)Path)sleeptime)	ENVIRONMENTGITLOGGERPYTHON_VERSIONRANKSETTINGSTESTS_RUNNINGRetrycolorstrz
Platform: ULTRALYTICS_PLATFORM_URLz https://platform.ultralytics.com/z/api/webhooksc                     | s| S t          j        ddt          j        ddt          |                                                                         dd                                        d          dd         S )zGConvert text to URL-safe slug (e.g., 'My Project 1' -> 'my-project-1').z-+-z[^a-z0-9\s-]  N   )resubstrlowerreplacestrip)texts    i/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/callbacks/platform.pyslugifyr       sn     6%bf_b#d))//:K:KLLTTUXZ]^^__eefijjkolokopp    platformFTULTRALYTICS_API_KEYapi_key)ConsoleLoggerSystemLogger)model_info_for_loggers
   )max_workersc           
      D   ddl }| dd         }|                    d          }t          j        d          pt	          j        d          }|st          d|  dt           d	          t          }d
d| i}t          |          dk    r|d         dk    r|\  }}	}
| d| d|
 d}n;t          |          dk    r|\  }}}| d| d| d| d}nt          d|  d          d|v rdnd}	 t          d          D ]p}	  |j        ||d|          } nX# |j        j        $ rF}t          j        d|dz    d|  d|            |dk    r t!          dd|z  z             Y d}~id}~ww xY wnJ# t"          $ r=}|rt          d|  d|           |t          j        d|  d|            Y d}~dS d}~ww xY wd|j        cxk    rdk     rn nd |j        v r|j        d          S |j        d!k    rt          d"|  d#          |j        d$k    rt)          d%|  d&          |j        d'k    r-|rt+          d(|            t          j        d(|             dS |j        d)k    rt-          d*|  d+          |                                 t-          d,|  d-|j                   ).a,  Resolve ul:// URIs to signed URLs by authenticating with Ultralytics Platform.

    Formats:
        ul://username/datasets/slug  -> Returns signed URL to NDJSON file
        ul://username/project/model  -> Returns signed URL to .pt file

    Args:
        uri (str): Platform URI starting with "ul://".
        hard (bool): Whether to raise an error if resolution fails.

    Returns:
        (str | None): Signed URL on success, None if not found and hard=False.

    Raises:
        ValueError: If API key is missing/invalid or URI format is wrong.
        PermissionError: If access is denied.
        RuntimeError: If resource is not ready (e.g., dataset still processing).
        FileNotFoundError: If resource not found and hard=True.
        ConnectionError: If network request fails and hard=True.
    r   N   r   r#   r$   z"ULTRALYTICS_API_KEY required for 'z'. Get key at z	/settingsAuthorizationBearer       datasetsz
/datasets/z/exportz/models/z	/downloadzInvalid platform URI: z8. Use ul://user/datasets/name or ul://user/project/model)r(   i  )r(   Z   F)headersallow_redirectstimeoutzRetry z/3 failed for :    zFailed to resolve i,    locationi  z!Invalid ULTRALYTICS_API_KEY for ''i  zAccess denied for 'z+'. Check dataset/model visibility settings.i  zNot found on platform: i  zResource not ready: z". Dataset may still be processing.z'Unexpected response from platform for 'z': )requestssplitosgetenvr   get
ValueErrorPLATFORM_URLPLATFORM_API_URLlenrangehead
exceptionsConnectionErrorr
   warningr   	Exceptionstatus_coder2   PermissionErrorFileNotFoundErrorRuntimeErrorraise_for_status)urihardr:   pathpartsr$   baser2   username_slugurlprojectmodelr4   attemptres                     r   resolve_platform_urir\   .   s   * OOOqrr7DJJsOOEi-..I(,y2I2IG jhchhQ]hhhiiiD 3' 3 34G 5zzQ58z11!!T9999D999 
Uq#( '5DDxDD'DDEDDD o#oooppp )C//jjXGQxx 	( 	(G(!HM#wW^___&6 ( ( (M!MM3MM!MMNNNa<<a1g:&''''''''	(
     	J!"As"A"Aa"A"ABBI6C66166777ttttt	 am!!!!c!!!!!jAI&=&=y$$ 	}CSCCCDDD}dCdddeee} 	E#$Cc$C$CDDD666777t}Y#YYYZZZ 
XXXXX
Y
YYsB   ,E- >DE- E)#<E$E- $E))E- -
F472F//F4e   c                 N   ddl |                     d          r|                     d          s| S                     | d                                       | d                   c}t                    |k    r| S                     d         d         |          |j        dk    r                    |          }n#                    fd|D                       }i |                                 |                                d}d	| v r| d	         |d	<   |S )
z?Interpolate plot curve data to n points to reduce storage size.r   Nxyr/   c                 >    g | ]}                     |          S  )interp).0yinpr_   x_news     r   
<listcomp>z _interp_plot.<locals>.<listcomp>   s)    >>>b"))E1b11>>>r!   )r_   r`   ap)numpyr>   arrayrB   linspacendimrd   tolist)plotnr`   y_newresultrg   r_   rh   s        @@@r   _interp_plotrt      s,   88C==  88DIc 3 3DAq
1vv{{ KK!aeQ''E 	v{{		%A&&>>>>>>A>>>?? @?5<<>>???Ft||DztMr!   c                    t          | t                    rd |                                 D             S t          | t          t          f          rd | D             S t          | t
                    rt          |           r| ndS | S )zSReplace non-finite floats in payloads with None so requests JSON encoding succeeds.c                 4    i | ]\  }}|t          |          S rc   _sanitize_json_valuere   kvs      r   
<dictcomp>z(_sanitize_json_value.<locals>.<dictcomp>   s'    EEEtq!'**EEEr!   c                 ,    g | ]}t          |          S rc   rw   )re   r{   s     r   ri   z(_sanitize_json_value.<locals>.<listcomp>   s!    777A$Q''777r!   N)
isinstancedictitemslisttuplefloatr   )values    r   rx   rx      s    % FEEu{{}}EEEE%$'' 8777777% 2 1uuT1Lr!   r6   c                     | ||t          |          d|r|d<   t          |d          fd            }	  |            S # t          $ r,}t          j        t
           d|  d|            Y d}~dS d}~ww xY w)	z1Send event to Platform endpoint with retry logic.)eventrW   namedatamodelIdr/   timesdelayc                     t          j        t           dddt           id          } d| j        cxk    rdk     rqn nn| j        dvre	 |                                                     d	| j                  }n# t          $ r
 | j        }Y nw xY wt          j
        t           |            d S |                                  |                                 S )
Nz/training/metricsr,   r-      jsonr2   r4   r7   i  >       error)r:   postrA   _api_keyrI   r   r>   reasonrH   r
   rG   PREFIXrM   )rZ   msgpayloads     r   r   z_send.<locals>.post   s    M222$&:&:&:;	
 
 
 !-%%%%#%%%%%!-z*I*Iffhhll7AH55   hNf+c++,,,4	vvxxs   -A9 9BBzFailed to send r5   N)rx   r   rH   r
   debugr   )	r   r   rW   r   model_idretryr   r[   r   s	           @r   _sendr      s    '4I]^bIcIcddG &%	
a       ! "tvv   ;;u;;;;<<<ttttts   	A 
A:!A55A:c                 N    t                               t          | ||||           dS )z4Send event asynchronously using bounded thread pool.N)	_executorsubmitr   )r   r   rW   r   r   s        r   _send_asyncr      s&    UE4$AAAAAr!   c                     |r?|                     d          r,d|d<   d| _        t          j        t           d           dS dS dS )aS  Apply centralized stop signals returned by Platform webhook responses.

    Notes:
        ``ctx["cancelled"]`` is the durable cancellation signal. During startup, trainer setup later resets
        ``trainer.stop``, so early stop requests still rely on ``on_pretrain_routine_end()`` to reapply the flag after
        setup completes.
    	cancelledTu'   Training cancelled from Platform ⚠️N)r>   stopr
   infor   )trainerctxresponses      r   _handle_control_responser      si      HHLL-- HKvFFFGGGGGH H H Hr!   r/   c                     ddl m} t                                                       s t	          j        t           d             dS t          dd           fd            }	  |            }n6# t          $ r)}	t	          j        t           d	|	            Y d}	~	dS d}	~	ww xY w | |d
         ||          r|	                    d          S dS )z3Upload model checkpoint to Platform via signed URL.r   )safe_uploadzModel file not found: Nr.   r6   r   c                      j         d} r| d<   t          j        t           d| ddt           id          }|                                 |                                S )N)rW   r   filenamer   z/models/uploadr,   r-   r   r   )r   r:   r   rA   r   rM   r   )r   rZ   r   
model_pathr   rW   s     r   get_signed_urlz%_upload_model.<locals>.get_signed_url   s    %tQQ 	*!)GIM///$&:&:&:;	
 
 
 	
vvxxr!   zFailed to get upload URL: 	uploadUrl)filerV   r   progressgcsPath)
ultralytics.utils.uploadsr   r   existsr
   rG   r   r   rH   r>   )
r   rW   r   r   r   r   r   r   r   r[   s
   ```  `    r   _upload_modelr      s6   555555j!!J &DD
DDEEEt !       ~   &??A??@@@ttttt
 {
[(9QYZZZ #xx	"""4s   .
A9 9
B,B''B,c                 N    t                               t          | |||           dS )z6Upload model asynchronously using bounded thread pool.r   N)r   r   r   )r   rW   r   r   s       r   _upload_model_asyncr     s'    ]JQQQQQr!   c                     ddl } ddl}ddl}ddlm} ddlm}m} |                                }| 	                    d          }|t          j                    t          j                    t          t          t          j        t#          j                    pd |            d                    t          j                  t+          |j        dz  d          t+          |j        dz  d          d	}	 t.          j        rYt.          j        rt.          j        |d
<   t.          j        rt.          j        |d<   t.          j        rt.          j        dd         |d<   n# t8          $ r Y nw xY w	 |j                                        rI|j                                        |d<   |j                                        dk    r |d          nd|d<   n# t8          $ r Y nw xY w|S )zLCollect comprehensive environment info using existing ultralytics utilities.r   N)__version__)get_cpu_infoget_gpu_infor   r   i   @r/   )ultralyticsVersionhostnamer<   environmentpythonVersionpythonExecutablecpuCountcpucommand
totalRamGbtotalDiskGbgitRepository	gitBranch   	gitCommitgpuCountgpuType) shutilpsutiltorchultralyticsr   ultralytics.utils.torch_utilsr   r   virtual_memory
disk_usagesocketgethostnamer"   r   r   sys
executabler<   	cpu_countjoinargvroundtotalr	   is_repooriginbranchcommitrH   cudais_availabledevice_count)	r   r   r   r   r   r   memoryr   envs	            r   _get_environment_infor     s   MMMMMMLLL''''''HHHHHHHH ""$$F""3''J *&((!!"'NLNN'a|~~88CH%%FLG4a88Z-91== C	; 	3z 2'*zO$z .#&:K z 3#&:crc?K    :""$$ 	X#j5577C
O05
0G0G0I0IA0M0M\\!___SWC	N    Js&   -A%E 
E E $A"G 
GGc                 8   t          | j        j                  }|                    dd          }t	          |          dk    r |d          dt          |d                    nt          |          }|t          t          | j        j        pd                    fS )z1Get slugified project and name from trainer args.r   r/   r6   r   train)r   argsrW   r;   rB   r    r   )r   rawrQ   rW   s       r   _get_project_namer   :  s    
gl"
#
#CIIc1E36u::??q//GE!H--///PSGGC 1 <W==>>>>r!   c                 j   t           dvs| j        j        sdS t          |           \  t	          j        t           d           dt                      dddd| _        fd}t          dd|	          d
<   d
         
                                 t                      }d t          | j                                                  D             }t          d|| j        t!          | j                  |dd          }|r|                    d          rq|d         d<   |                    d          r>|d         d<   t&           d dd          }t	          j        t           d|            t)          | |           dS t	          j        t           d           d| _        dS )z.Initialize Platform logging at training start.   r   ra   Nz&Streaming training metrics to PlatformF)r   last_uploadr   console_loggersystem_loggerc                 B    t          d|| |dd                    dS )z0Send batched console output to Platform webhook.console_output)chunkIdcontent	lineCountr   N)r   )r   
line_countchunk_idr   r   rW   s      r   send_console_outputz6on_pretrain_routine_start.<locals>.send_console_outputO  s<     W:NN
O	
 	
 	
 	
 	
r!   r+   g      @)
batch_sizeflush_intervalon_flushr   c                 4    i | ]\  }}|t          |          S rc   )r   ry   s      r   r|   z-on_pretrain_routine_start.<locals>.<dictcomp>b  s$    CCC1!SVVCCCr!   training_started)	trainArgsepochsdevicer      r   r   r   	modelSlug
model_slugr   zView model at z(Training will not be tracked on Platform)r   r   rW   r   r
   r   r   r   r"   r%   start_capturer   varsr   r   r   r   r  r>   r@   r   rG   )	r   r   r   
train_argsr   rV   r   r   rW   s	         @@@r   on_pretrain_routine_startr	  B  s   7',"6%g..MGT
K6AAABBB DFFZ^qu
v
vCG
 
 
 
 
 
 
 *QsUhiiiC''))) ())K DCW\(:(:(@(@(B(BCCCJ #n'.))&		
 	
 	  H   HLL++  "9-J<<$$ 	8 ( 5C!AAGAAc,.?AACK66666777 #x88888&JJJKKKr!   c                     t          | dd          }|r-|d         r't          j        t           d           d| _        dS dS dS )zDApply pre-start cancellation after _setup_train resets trainer.stop.r"   Nr   u4   Training cancelled from Platform before starting ✅T)getattrr
   r   r   r   )r   r   s     r   on_pretrain_routine_endr    s_    
':t
,
,C
 s; vSSSTTT   r!   c                 "   	 t           dd          rt          dvs j        j        sdS t	                     \  	i                       j        d           j        } j        r' j        j	        r j        j	        d         d         |d<   d} j
        dk    rc	 t                     }|                    dd          |                    d	d          |                    d
d          d}n# t          $ r Y nw xY wi }	 d         st                      d<   d                             d          }n# t          $ r Y nw xY w j
        || j         j        d|r|d<   	 fd}t$                              |           dS )z-Log training and system metrics at epoch end.r"   Nr   r   )prefixr   lrzmodel/parameterszmodel/GFLOPszmodel/speed_PyTorch(ms))
parametersgflopsspeedMsr   T)rates)epochmetricssystemfitnessbest_fitness	modelInfoc                  `    t          dd         d          } t          |            dS )zOSend epoch_end and check response for cancellation (runs in background thread).	epoch_endr   r/   r  N)r   r   )r   r   r   r   rW   r   s    r   _send_and_check_cancelz0on_fit_epoch_end.<locals>._send_and_check_cancel  s:    gwc*oUVWWW #x88888r!   )r  r   r   rW   r   label_loss_itemstlossr  	optimizerparam_groupsr  r'   r>   rH   r&   get_metricsr  r  r   r   )
r   r  
model_infor   r  r  r   r   r   rW   s
   `     @@@@r   on_fit_epoch_endr#    s   
':t
,
,C $g%%W\-A%%g..MGT\))'-)HH\GO\G @W.; @)6q9$? J}	)'22D"hh'91==((>15588$=qAA JJ
  	 	 	D	 F?# 	2#/>>C _%111==    ?, G  *)9 9 9 9 9 9 9 9 9
 +,,,,,s%   #AC5 5
DD5D> >
E
Ec                    t          | dd          }|rt          dvs| j        j        sdS t	                      |d         z
  dk     rdS | j        r-t          | j                                                  r| j        n| j        }|sdS t          |           \  }}t          ||||d                    t	                      |d<   dS )z7Upload model checkpoint (rate limited to every 15 min).r"   Nr   r   i  r   r   )r  r   r   rW   r   bestr   r   lastr   r   )r   r   r   rW   r   s        r   on_model_saver'    s    
':t
,
,C $g%%W\-A% vvM""S((!(_$w|2D2D2K2K2M2M_SZS_J %g..MGT
GTC
OLLLLCr!   c           	         t          | dd          }|rt          dvs| j        j        sdS t	          |           \  }}|d         rt          j        t           d           |d         r|d                                          d|d<   d}d}| j	        rt          | j	                                                  rit          | j	                                                  j        }t          | j	        ||dd|d	         
          }|st          j        t           d           i }t          | di                                           D ]I}|                    d          r2|d                             d          r|d         ||d         d         <   Jt          t          | dd          di                                           D ]Z}|                    d          rC|d                             d          r(|                    |d         d         |d                    [d |                                D             }t          t          | dd          dd          p| j        pi                     d          }	t)          |	t*                    r!t-          |	                                          n|	rt-          |	          nd}
t/          di | j        d| j        it          | d| j                  | j        ||d|
|d|||d	         d           t8           d| d|                    d|           }t          j        t           d|            dS )zDLog final results, upload best model, and send validation plot data.r"   Nr   r   z0Uploading partial results for cancelled trainingr   Tr.   r   )r   r   r   zDModel will not be available for download on Platform (upload failed)plotsr   type	validatorc                 ,    g | ]}t          |          S rc   )rt   )re   ps     r   ri   z on_train_end.<locals>.<listcomp>  s    ===\!__===r!   namestraining_completer  
best_epoch)r  	bestEpochbestFitness	modelPath	modelSize)results
classNamesr)  r  r  r   r  zView results at )r  r   r   rW   r   r
   r   r   stop_capturer%  r   r   statst_sizer   rG   valuesr>   
setdefaultr   r~   r   r   r   r  r  r  r  r@   )r   r   rW   r   gcs_path
model_sizeplots_by_typer   r)  r.  class_namesrV   s               r   on_train_endr@    s   
':t
,
,C $g%%W\-A%%g..MGT
; QvOOOPPP  %**,,, $ HJ| lW\**1133 l','',,..6
 wtST_bcm_nooo 	lNfjjjkkk M"--4466 ? ?88F 	?V 0 0 8 8 	?26v,M$v,v./d;;WbIIPPRR I I88F 	IV 0 0 8 8 	I$$T&\&%94<HHH==m&:&:&<&<===E GG[$77$GGlGLL^\^KcKcdkKlKlE*4UD*A*Ae$u||~~&&&V[GetE{{{aeK	 KgoJy'/JJ$WlGMJJ&3%'  &
	
 
	
 	J!   $ 
C
CG
C
CcgglD&A&A
C
CC
K60030011111r!   )r	  r  r#  r'  r@  )T)r]   )Nr6   )N)Fr/   N)8r<   r"   r   r   r   concurrent.futuresr   mathr   pathlibr   r   r   ultralytics.utilsr   r	   r
   r   r   r   r   r   r   r   r=   rstripr@   rA   r    r>   r   r:   ultralytics.utils.loggerr%   r&   r   r'   r   AssertionErrorImportErrorr\   rt   rx   r   r   r   r   r   r   r   r	  r  r#  r'  r@  	callbacksrc   r!   r   <module>rJ     s\   
			  				  



 1 1 1 1 1 1                     v v v v v v v v v v v v v v v v v v v v v v	,		 ry35WXX__`cdd"111 q q q8<
E**d22ibi@U6V6V2ZfZbZfgpZqZq22qry.//J<8<	3J3JHOO8OOODDDDDDDDDDDDDD""r222II$   HHHTZ TZ TZ TZn   8     >B B B B
H H H! ! ! !HR R R R
0 0 0f? ? ?:  :  : z  1- 1- 1-h     &:2 :2 :2J %>#:,&$   
 
		s   =A;C9 9	DD