
    i                         S SK r S SKrS SKrS SKJr  S SKJrJrJr  S SK	r	S\
S\4S jrS\\
\4   S\\	R                     4S jrS	\
S\4S
 jrS\
S\\
\
4   S\
4S jrg)    N)Path)AnyDictOptionalmodel_file_pathreturnc                     SSK Jn  SSKJn  [	        U 5      R
                  R                  nUR                  SU 35      nUR                  U5       nUS-  nSSS5        WR                  5       (       d   e U$ ! , (       d  f       N(= f! [         a*    [	        U 5      R                  5       R
                  S-  n U$ f = f)a  
Get the default path to resouce files (which contain files such as the
checkpoint and param files), either:
1. Uses the path from importlib.resources, only works with buck2
2. Uses default path located in examples/models/llama/params

Expected to be called from with a `model.py` file located in a
`executorch/examples/models/<model_name>` directory.

Args:
    model_file_path: The file path to the eager model definition.
        For example, `executorch/examples/models/llama/model.py`,
        where `executorch/examples/models/llama` contains all
        the llama2-related files.

Returns:
    The path to the resource directory containing checkpoint, params, etc.
r   N)paramszexecutorch.examples.models.r
   )importlib.resources	resources executorch.examples.models.llamar
   r   parentnamefilesas_fileexists	Exceptionabsolute)r   
_resourcesr
   
model_name	model_dir
model_pathresource_dirs          d/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/examples/models/checkpoint.pyget_default_model_resource_dirr      s    (J0 	< /*1166
$$'B:,%OP		*j%0L +""$$$$  +*  JO,557>>I	Js*   AB A:B :
BB 0B?>B?
checkpointc                 ~   Sn[        U 5      S:  a  [        [        U 5      5      nX   nUR                  nU R	                  5        VVs/ s H7  u  pE[        US5      (       d  M  UR                  U:w  d  M*  XER                  4PM9     nnn[        U5      S:  a  [        SU SUR                   SU 35        U$ s  snnf )zO
Get the dtype of the checkpoint, returning "None" if the checkpoint is empty.
Nr   dtypezMixed dtype model. Dtype of z: z . Mismatches in the checkpoint: )lennextiterr   itemshasattrprint)r   r   	first_keyfirstkeyvaluemismatched_dtypess          r   get_checkpoint_dtyper*   =   s     E
:j)*	% )..0
0
ug& +0;;%+? S++0 	 

  !A%.ykEKK=Hhizh{| L
s   	B9$B96B9	input_dirc                 F   [         R                  R                  U S5      n[         R                  R                  U5      (       a  [	        US5       n[
        R                  " U5      nS S S 5        WS   n[        [        UR                  5       5      5      n0 nU HL  n[        R                  " [         R                  R                  X5      S[        R                  " S5      S9Xg'   MN     0 nUR                  5        H  u  pXg   U	   n
XU	'   M     U$ [         R                  R                  U S5      n[         R                  R                  U5      (       a,  [        R                  " US[        R                  " S5      S9nU$ [        SU  35      e! , (       d  f       GN0= f)	Nzpytorch_model.bin.index.jsonr
weight_mapTcpu)weights_onlymap_locationzpytorch_model.binz+Could not find pytorch_model checkpoint in )ospathjoinr   openjsonloadsortedsetvaluestorchdevicer"   FileNotFoundError)r+   
index_pathfindexr.   checkpoint_shardsshard_to_weightsshardmerged_state_dictweight_nametensorr   
state_dicts                r   "load_checkpoint_from_pytorch_modelrH   R   sU   i)GHJ	ww~~j!!*c"aIIaLE #<(
"3z'8'8':#;< &E&+jjY.!"\\%0'# ' ","2"2"4K%,[9F-3k* #5 !  i)<=J	ww~~j!!ZZTU8K

 
I)U
VV; #"s   F
F r'   mapping_dictc                 t    [        S U R                  S5       5       5      (       ai  [        R                  " SSU 5      n[        R                  " SU 5      nUc  [        SU  S35      eUR                  S	5      nX   nUR                  U5      nU$ X   n U$ ! [         a  n[        S
U  S35      UeSnAff = f)zTMap a state dict key using a mapping dictionary with "{}" layer number placeholders.c              3   @   #    U  H  oR                  5       v   M     g 7f)N)isdigit).0ks     r   	<genexpr>!get_mapped_key.<locals>.<genexpr>z   s     3Nqyy{{Ns   .z(\.\d+)z.{}z\.(\d+)NzFError converting the state dict. Could not find layer number in key: "zG". Please make sure you're loading a checkpoint with the right format.    z8Error converting the state dict. Found unexpected key: ")	anysplitresubsearchr   groupformatKeyError)r'   rI   abstract_keymatch	layer_numnew_keyes          r   get_mapped_keyr`   v   s    3CIIcN33366*eS9LIIj#.E}\]`\a b[ [  AI"0GnnY/G N #'G N  Fse LS S
 	s   BB B 
B7"B22B7)r6   r2   rU   pathlibr   typingr   r   r   r;   strr   r   r*   rH   r`        r   <module>rf      s     	 	  & & 'C 'D 'TT#s(^ 8M *!W# !W$ !WH 4S> c re   