
    RЦi                        S 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\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\	R0                  R2                  S\S\S\\\	R,                  4   S\S\S\\   S\S	\4S jjr SS\\\4   S\	R0                  R2                  S\S	\\\4   4S jjr   S S\	R0                  R2                  S\S\\	R8                  R:                     S\\   S\S	\\   4S jjrg! \ a    Sr GN"f = 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_state_dictload_checkpointremap_state_dictresume_checkpointtextprefixreturnc                 N    U R                  U5      (       a  U [        U5      S  $ U $ )N)
startswithlen)r   r   s     S/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/models/_helpers.py_remove_prefixr      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.)itemsr   )r   cleaned_state_dict	to_removekvrs         r   r   r      sJ    I   "Aq$A  !1 # r   checkpoint_pathuse_emadeviceweights_onlyc                    U (       Ga(  [         R                  R                  U 5      (       Ga  [        U 5      R	                  S5      (       a0  [
        (       d   S5       e[        R                  R                  XS9nO [        R                  " 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    [        R                  " XS9n Nf = f)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    map_locationr!   )r$    state_dict_emaN	model_emar   modelzLoaded {} from checkpoint '{}'No checkpoint found at '{}')ospathisfilestrendswith_has_safetensorssafetensorstorch	load_fileload	TypeError
isinstancedictgetr   _loggerinfoformaterrorFileNotFoundError)r   r   r    r!   
checkpointstate_dict_keyr   s          r   r	   r	   ,   sB   " 277>>/::((88##T%TT#$**44_4TJN"ZZ[gh
 j$'':>>*:DAM!1Z^^K>J!,+!-J&!(%Nj&@Xbc
5<<^]^3::?KL!!%  N"ZZM
Ns   >E F ?F 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!   )r?   )	r*   r+   splitextlowerhasattrrD   NotImplementedErrorr	   r   )
r(   r   r   r    r?   r@   rA   r!   r   incompatible_keyss
             r   r
   r
   Z   s    2 
ww(,2248HH5+,,!!/2 	 &&JKK !&dJ%j8
	z1
--j-Hr   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 )zipr   r   numelshapereshape)r   r(   rJ   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 Or   	optimizerloss_scalerlog_infoc                    Sn[         R                  R                  U5      (       Ga  [        R                  " USS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                  XcR                     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)aV  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.

Returns:
    Resume epoch number if available, else None.
NcpuFr#   r   z(Restoring model state from checkpoint...rU   z,Restoring optimizer state from checkpoint...z2Restoring AMP loss scaler state from checkpoint...epochversion   z!Loaded checkpoint '{}' (epoch {})zLoaded checkpoint '{}'r)   )r*   r+   r,   r1   r3   r5   r6   r8   r9   r   r	   r>   r:   r;   r<   )r(   r   rU   rV   rW   resume_epochr=   r   s           r   r   r      s   & L	ww~~o&&ZZeRWX
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!!r   )TrY   F)TrY   TFNF)T)NNT) __doc__loggingr*   typingr   r   r   r   r   r1   safetensors.torchr0   r/   ImportError	getLogger__name__r8   __all__r-   r   r   boolr    r	   nnModuler
   r   optim	Optimizerintr    r   r   <module>rm      s#    	 7 7  

H
%
m c c c3h DcN   +0"	+"+"+" c5<<'(+" 	+"
 
#s(^+"b +0(,"'xx'' ' c5<<'(	'
 ' ' H%' ' 	'Z #cNxx  
#s(^	@ 6:%)4"xx4"4" EKK1124" c]	4"
 4" c]4"e  s   D; ;EE