
    
3ji'                        S r SSKrSSKrSSKrSSKrSSKJrJrJrJ	r	J
r
  SSKr SSKrSr\R                   " \5      r/ SQrS\S\4S	 jr  S S\S
\
\\R,                  4   S\4S jjrS\S\S\4S jrS\\\4   S\\\4   4S jr   S!S\S\S\
\\R,                  4   S\S\\\4   4
S jjr      S"S\R8                  R:                  S\S\S\
\\R,                  4   S\S\S\	\   S\S\4S jjr S#S\\\4   S\R8                  R:                  S\S\\\4   4S jjr    S$S\R8                  R:                  S\S\	\R@                  RB                     S\	\   S\S\S\	\"   4S jjr#g! \ a    Sr GNLf = f)%zhModel creation / weight loading / state_dict helpers

Hacked together by / Copyright 2020 Ross Wightman
    N)AnyCallableDictOptionalUnionTF)clean_state_dictload_checkpointload_state_dictremap_state_dictresume_checkpointcheckpoint_pathreturnc                     [        [        R                  S5      (       d  g [        R                  R                  [	        U 5      5      nU(       a  SSR                  U5       S3$ S$ ! [
         a    / n N,f = f)N get_unsafe_globals_in_checkpoint z Unsupported globals: z, .)hasattrtorchserializationr   str	Exceptionjoin)r   unsafe_globalss     N/home/wildlama/miniconda3/lib/python3.13/site-packages/timm/models/_helpers.py_checkpoint_unsafe_globalsr      sv    5&&(JKK,,MMcRaNbc ES#DIIn$=#>a@ZXZZ  s   (A( (A76A7map_locationweights_onlyc                 N   U=(       a    [        [        R                  S5      n U(       aM  [        R                  R                  [        R
                  /5         [        R                  " XUS9sS S S 5        $ [        R                  " XUS9$ ! , (       d  f       N#= f! [         aC  nU(       d  [        R                  " XS9s S nA$ [        S[        R                   S35      UeS nAf[        R                   a&  nU(       d  e [        S[        U 5       S35      UeS nAff = f)Nsafe_globalsr   r   )r   zAweights_only=True is not supported by this PyTorch build (torch==z). No automatic unsafe pickle fallback is performed. Upgrade PyTorch, or explicitly set weights_only=False only for trusted local checkpoints.zeweights_only=True blocked loading this checkpoint because it requires non-allowlisted pickle globals.zp No automatic unsafe pickle fallback is performed. If this checkpoint is trusted, retry with weights_only=False.)r   r   r   r   argparse	Namespaceload	TypeErrorRuntimeError__version__pickleUnpicklingErrorr   )r   r   r   use_safe_globalses        r   _torch_loadr+   )   s	   
 $T0C0C^(T$$1183E3E2FGzz/[gh HGzz/S_`` HG  ::oIIOPUPaPaOb ch h
 		
 !! s)/:; <LL

 	sM   5B B/	B 9B 
BB 
D$)C'D$	C''D$>!DD$textprefixc                 N    U R                  U5      (       a  U [        U5      S  $ U $ )N)
startswithlen)r,   r-   s     r   _remove_prefixr1   H   s'    vCKL!!K    
state_dictc                 p    0 nSnU R                  5        H  u  p4U H  n[        X55      nM     XAU'   M     U$ )N)zmodule.z
_orig_mod.)itemsr1   )r3   cleaned_state_dict	to_removekvrs         r   r   r   O   sJ    I   "Aq$A  !1 # r2   use_emadevicec                    U (       Ga  [         R                  R                  U 5      (       a  [        U 5      R	                  S5      (       a0  [
        (       d   S5       e[        R                  R                  XS9nO
[        XUS9nSn[        U[        5      (       aK  U(       a  UR                  SS5      b  SnO.U(       a  UR                  SS5      b  SnOS	U;   a  S	nOS
U;   a  S
n[        U(       a  XE   OU5      n[        R                  SR!                  XP5      5        U$ [        R#                  SR!                  U 5      5        [%        5       e)a?  Load state dictionary from checkpoint file.

Args:
    checkpoint_path: Path to checkpoint file.
    use_ema: Whether to use EMA weights if available.
    device: Device to load checkpoint to.
    weights_only: Whether to load only weights (torch.load parameter).

Returns:
    State dictionary loaded from checkpoint.
z.safetensorsz-`pip install safetensors` to use .safetensors)r<   r    r   state_dict_emaN	model_emar3   modelzLoaded {} from checkpoint '{}'No checkpoint found at '{}')ospathisfiler   endswith_has_safetensorssafetensorsr   	load_filer+   
isinstancedictgetr   _loggerinfoformaterrorFileNotFoundError)r   r;   r<   r   
checkpointstate_dict_keyr3   s          r   r
   r
   ]   s   " 277>>/::((88##T%TT#$**44_4TJ$_XdeJj$'':>>*:DAM!1Z^^K>J!,+!-J&!(%Nj&@Xbc
5<<^]^3::?KL!!r2   r@   strictremap	filter_fnc                 @   [         R                  R                  U5      S   R                  5       S;   a.  [	        U S5      (       a  U R                  U5        g[        S5      e[        XX7S9nU(       a  [        X5      nOU(       a  U" X5      nU R                  XS9n	U	$ )a  Load checkpoint into model.

Args:
    model: Model to load checkpoint into.
    checkpoint_path: Path to checkpoint file.
    use_ema: Whether to use EMA weights if available.
    device: Device to load checkpoint to.
    strict: Whether to strictly enforce state_dict keys match.
    remap: Whether to remap state dict keys by order.
    filter_fn: Optional function to filter state dict.
    weights_only: Whether to load only weights (torch.load parameter).

Returns:
    Incompatible keys from model.load_state_dict().
)z.npzz.npyload_pretrainedz"Model cannot load numpy checkpointN)r<   r   )rS   )	rB   rC   splitextlowerr   rX   NotImplementedErrorr
   r   )
r@   r   r;   r<   rS   rT   rU   r   r3   incompatible_keyss
             r   r	   r	      s    2 
ww(,2248HH5+,,!!/2 	 &&JKK !&dJ%j8
	z1
--j-Hr2   allow_reshapec                    0 n[        UR                  5       R                  5       U R                  5       5       H  u  u  pEu  pgUR                  5       UR                  5       :X  d(   SU SUR                   SU SUR                   S3	5       eUR                  UR                  :w  aK  U(       a  UR                  UR                  5      nO( SU SUR                   SU SUR                   S3	5       eXsU'   M     U$ )a{  Remap checkpoint by iterating over state dicts in order (ignoring original keys).

This assumes models (and originating state dict) were created with params registered in same order.

Args:
    state_dict: State dict to remap.
    model: Model whose state dict keys to use.
    allow_reshape: Whether to allow reshaping tensors to match.

Returns:
    Remapped state dictionary.
zTensor size mismatch z: z vs z. Remap failed.zTensor shape mismatch )zipr3   r5   numelshapereshape)r3   r@   r]   out_dictkavakbvbs           r   r   r      s    " H!%"2"2"4":":"<j>N>N>PQ(2xxzRXXZ't+@BrxxjPTUWTXXZ[][c[cZdds)tt'88rxxZZ)j 6rd"RXXJd2$bQSQYQYPZZijju R Or2   	optimizerloss_scalerlog_infoc                    Sn[         R                  R                  U5      (       Gau  [        USUS9n[	        U[
        5      (       Ga  SU;   Ga  U(       a  [        R                  S5        [        US   5      nU R                  U5        Ub6  SU;   a0  U(       a  [        R                  S5        UR                  US   5        UbI  UR                  U;   a9  U(       a  [        R                  S5        UR                  XsR                     5        S	U;   aG  US	   nS
U;   a  US
   S:  a  US-  nU(       a'  [        R                  SR                  XS	   5      5        U$ U R                  U5        U(       a$  [        R                  SR                  U5      5        U$ [        R                  SR                  U5      5        [        5       e)a  Resume training from checkpoint.

Args:
    model: Model to load checkpoint into.
    checkpoint_path: Path to checkpoint file.
    optimizer: Optional optimizer to restore state.
    loss_scaler: Optional AMP loss scaler to restore state.
    log_info: Whether to log loading info.
    weights_only: Whether to load only weights via torch.load.

Returns:
    Resume epoch number if available, else None.
Ncpur    r3   z(Restoring model state from checkpoint...rh   z,Restoring optimizer state from checkpoint...z2Restoring AMP loss scaler state from checkpoint...epochversion   z!Loaded checkpoint '{}' (epoch {})zLoaded checkpoint '{}'rA   )rB   rC   rD   r+   rI   rJ   rL   rM   r   r
   rR   rN   rO   rP   )	r@   r   rh   ri   rj   r   resume_epochrQ   r3   s	            r   r   r      s   * L	ww~~o&& uS_`
j$''LJ,FGH)*\*BCJ!!*-$
)BLL!OP))*[*AB&;+E+E+SLL!UV++J7Q7Q,RS*$)'2
*z)/Dq/H A%LLL!D!K!KOho]p!qr
  !!*-5<<_MN3::?KL!!r2   )rl   T)Trl   T)Trl   TFNT)T)NNTT)$__doc__r!   loggingrB   r'   typingr   r   r   r   r   r   safetensors.torchrG   rF   ImportError	getLogger__name__rL   __all__r   r   r<   boolr+   r1   r   r
   nnModuler	   r   optim	Optimizerintr    r2   r   <module>r      s     	  7 7  

H
%[ [ [ 27!C-. > c c c3h DcN   +0!	("("(" c5<<'((" 	("
 
#s(^("\ +0(,!'xx'' ' c5<<'(	'
 ' ' H%' ' 	'Z #cNxx  
#s(^	@ 6:%)!6"xx6"6" EKK1126" c]	6"
 6" 6" c]6"}  s   E- -E98E9