
    vjh                        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m	Z	m
Z
 d dlmZmZmZm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 d d
lmZmZ d dl m!Z! d dl"m#Z# d dl$m%Z%  e#            Z& e            Z'd Z(	 	 	 	 	 d!dej        j)        de*dee         dee         dee+         de,de,ddfdZ-	 	 d"dedefdZ.	 	 d"dZ/defdZ0	 	 d"dee*ej1        f         de*defd Z2dS )#    N)OrderedDict)copytreeignore_patternsrmtree)CallableDictOptionalUnion)nn)	Optimizer)_LRScheduler)FileLocalStorage)ConfigJSONIteratorEncoder)ConfigFields	ModelFile)copytree_py37)
get_logger)	is_masterc                     t                      }|                                 D ]\  }}|                                ||<   t          | dt                                |_        |S )zCopy a model state_dict to cpu.

    Args:
        state_dict (OrderedDict): Model weights on GPU.

    Returns:
        OrderedDict: Model weights on GPU.
    	_metadata)r   itemscpugetattrr   )
state_dictstate_dict_cpukeyvals       k/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/utils/checkpoint.pyweights_to_cpur!      s_     !]]N$$&& ( (S!ggiis&z;NNN    Tmodelfilename	optimizerlr_schedulermeta	with_meta
with_modelreturnc                    i }|s|st          d          |rp|i }n4t          |t                    st          dt	          |                     ddlm} |                    |t          j	                               t          | t          j        j        j                  r| j        } t          | d          r"| j        |                    | j                   ||d	<   t          |t"                    r|                                |d
<   nQt          |t                    r<i |d
<   |                                D ]"\  }	}
|
                                |d
         |	<   #|'t          |d          r|                                |d<   |rVt          | t          j        j        j                  r| j        } t)          |                                           }|s|}n||d<   t+          j                    5 }t          j        ||           t1          j        |                                |           ddd           dS # 1 swxY w Y   dS )af  Save checkpoint to file.

    The checkpoint will have 3 fields: ``meta``, ``state_dict`` and
    ``optimizer``. By default, ``meta`` will contain version and time info.

    Args:
        model (Module): Module whose params are to be saved.
        filename (str): Checkpoint filename.
        optimizer (:obj:`Optimizer`, optional): Optimizer to be saved.
        lr_scheduler(:obj:`_LRScheduler`, optional): LRScheduler to be saved.
        meta (dict, optional): Metadata to be saved in checkpoint.
        with_meta (bool, optional): Save meta info.
        with_model(bool, optional): Save model states.
    z;Save meta by "with_meta=True" or model by "with_model=True"Nz%meta must be a dict or None, but got r   )__version__)
modelscopetimeCLASSES)r/   r'   r%   r   r&   )
ValueError
isinstancedict	TypeErrortyper-   r,   updater.   asctimetorchr   parallelDistributedDataParallelmodulehasattrr/   r   r   r   r!   ioBytesIOsaver   writegetvalue)r#   r$   r%   r&   r'   r(   r)   
checkpointr,   nameoptim_weightsfs                r    save_checkpointrF   /   s   * J KZ KIK K 	K  C<DDD$'' 	FDT

DDF F F******{@@@eUX.FGG 	!LE5)$$ 	/)BKKK...!
6 i++ 	C&/&:&:&<&<J{##	4(( 	C&(J{#(00 C Ce050@0@0B0B
;'-- #l(K(K#)5)@)@)B)BJ~& 0eUX.FGG 	!LE!%"2"2"4"455 	0!JJ'/J|$	 +
:q!!!
1::<<***+ + + + + + + + + + + + + + + + + +s   4=H>>IIc                     t           j                            |           st          d|  d          t	          j        | dd          }|d|v rt          |t                    r|                    |d                    nt          |t                    rb|d         }|
                                D ]E\  }}||v r|                    ||                    %t                              d| d|             Fnt                              d	|             |=d
|v r|                    |d
                    nt                              d|             |#d|vr|n|d         }|                    |           |                    di           S )NzCheckpoint file z does not exist!r   Tmap_locationweights_onlyr%   zThe state dict of optimizer z% cannot be found in checkpoint file: z@The state dict of optimizer cannot be found in checkpoint file: r&   zCThe state dict of lr_scheduler cannot be found in checkpoint file: r   r'   )ospathexistsr0   r7   loadr1   r   load_state_dictr2   r   loggerwarningget)	r$   r#   r%   r&   rA   optimizer_dictr   optimizer_insr   s	            r    load_checkpointrU   v   s    7>>(## HFHFFFGGGH5tLLLJ*$$)Y// 
))*[*ABBBBIt,, !+K!8*3//*;*;  &Cn,,%55nS6IJJJJo3ooemoo    NN]S[]]   Z''((N)CDDDDNN`V^``   #/z#A#AZZzH
j)))>>&"%%%r"   c                    d d 	 ddfd}	 	 	 dd	 dddfd		}t           j                            |t          j                  }t          j        |d
d          }|t          j        |            || ||dd          \  }	}
}}t          	                    d          dd          r$ || 	                    d          j
                   | |	|
||dS )a{  
    Load model checkpoint file and feed the parameters into the model.
    Args:
        model_to_load: The model to be load
        model_local_dir: The actual checkpoint dir on local disk.
        default_dtype: Set the default float type by 'torch.set_default_dtype'
        load_state_fn: An optional load_state_fn used to load state_dict into the model.

    Returns:

    c                 ~   t                      }|                                 D ]m\  }}||v r_d                    ||g          }|||<   |                    |           t	          t          |          t          |g          z
            }h|||<   ng }t          |          dk    r|                                }|||fS )N.r   )r   r   joinremovelistsetlencopy)	state_dictshead_prefix!expected_keys_without_head_prefixmissing_keysnew_state_dictrB   r:   name_with_headmissing_head_keyss	            r    _add_head_prefix_to_state_dictzBload_task_model_checkpoint.<locals>._add_head_prefix_to_state_dict   s     %'--// 	. 	.LD&888!$;*=!>!>17~.188>>>#C$5$5^<L8M8M$MNN'-t$$011A55 A F F H H0,>>r"   c                 >   g }|r|D ]}|}	|r| d| }	n2|r0d                     |                    d          dd                    }	|	|v rQ||	         j        }
| |         j        }||
k    r1|                    || |         j        ||	         j        f           | |= |S )NrX      )rY   splitshapeappend)r_   model_state_dictloaded_keysprefixadd_prefix_to_modelremove_prefix_from_modelignore_mismatched_sizesmismatched_keycheckpoint_key	model_keymodel_shapecheckpoint_shapes               r    _find_mismatched_keysz9load_task_model_checkpoint.<locals>._find_mismatched_keys   s     " 	8"- 8 8*	+ H#) < <N < <II( H #)=)=c)B)B122)F G GI 000"29"="CK'2>'B'H$';66&--+[-H-N-i8>@A A A (7r"   Nc                 2  
 g }g }                                 D ]d}d }d|v r|                    dd          }d|v r|                    dd          }|r*|                    |           |                    |           et          ||          D ]\  }	}                    |	          |<   t          dd                                           _        g 
| || ||d 
           n#ddt          j	        f
fd	 | |
           
S )Ngammaweightbetabiasr   )rn   head_prefix_keyslocal_metadata
error_msgs r:   c                     i n                     |d d         i           }	||dg g f} | j        |  | j                                        D ]\  }}| |||z   dz              d S )NTrX   )rR   _load_from_state_dict_modulesr   )
r:   rn   r~   argsrB   childr   rN   metadatar   s
         r    rN   zMload_task_model_checkpoint.<locals>._load_state_dict_into_model.<locals>.load  s    '/'7X\\3B3K>% >%"FND"b"$,,d33#)?#8#8#:#: 9 9KD%(UFTMC$78889 9r"   )rn   )r   )
keysreplacerk   zippopr   r^   r   r   Module)r#   r   start_prefixr}   load_state_fnold_keysnew_keysr   new_keyold_keyr   rN   r   s    `        @@@r    _load_state_dict_into_modelz?load_task_model_checkpoint.<locals>._load_state_dict_into_model   s    ??$$ 	) 	)CG#~~++gx88}}++ff55 )$$$((( #Hh 7 7 	: 	:GW",.."9"9Jw :{D99__&&
#+J 
$M#!1#%' ' ' ' '9 9RY 9 9 9 9 9 9 9 9 9 D|,,,,r"   c                   $%&' |                                  }t          |                                          }t          |                                          }| j        &t	          &          dk    r7t          &fd|D                       }t          &fd|D                       }	nd}d}	| o|	}
|o|	 }|
r&fd|D             $&fd|D             }n|r&fd|D             }g $t          t          |          t          |          z
            }t          t          |          t          |          z
            }| j        }g }g }g }t                      }t                      }t	          |          dk    rt	          |          dk    rt          |t                    r|g}|D ] 't          'fd|D                       |'<   !|D ]*''fd	|D             }|                    |           ||'<   +t          t          |          t          |          z
            }t          t          |          t          |          z
            }+                    d
d           }+                    dd           }||D ]%%fd|D             }||D ]%%fd|D             }|r* ,| |&||
          }|D ]}|                     |           t                      }t	          |          dk    r*|D ]'}||         s (||||         |          \  }}}|||<   (d}| }t                      }t	          | j                  dk    r!t          | | j                  s|r
| j        dz   }t	          | j                  dk    rnt          | | j                  rY|sWt          | | j                  }|D ]}t          | |          ||<   t          $fd|D                       rt!          d           )|||&||
|          } *||||          }t	          |          dk    r1|D ].}  *||          || dz   |          }!|                    |!           /t	          |          dk    r4d                    |          }"t%          d| j        j         d|"           t	          |          dk    rFt*                              d| j        j         d| d| j        j         d| j        j         d	           ntt	          |          dk    r9t*                              d| j        j         d| d| j        j         d           n(t*                              d| j        j         d           t	          |          dk    r,t*                              d| j        j         d | d!           nHt	          |          dk    r5t*                              d"| j        j         d#| j        j         d$           t	          |          dk    rJd%                    d& |D                       }#t*                              d| j        j         d'|# d!           ||||fS )(Nr   c              3   B   K   | ]}|                               V  d S N
startswith.0srn   s     r    	<genexpr>zGload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<genexpr>'  sH       /D /D)*V$$/D /D /D /D /D /Dr"   c              3   B   K   | ]}|                               V  d S r   r   r   s     r    r   zGload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<genexpr>)  s@       .= .=)*V$$.= .= .= .= .= .=r"   Fc                 >    g | ]}|                               |S  r   r   s     r    
<listcomp>zHload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<listcomp>4  s:     5 5 5V0D0D55 5 5r"   c                     g | ]I}|                               r0d                     |                    d           dd                   n|JS rX   rh   Nr   rY   ri   r   s     r    r   zHload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<listcomp>7  s^        /0ll6.B.BIabb)***  r"   c                 >    g | ]}d                      |g          S )rX   )rY   r   s     r    r   zHload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<listcomp>=  s)    JJJqSXXvqk22JJJr"   c              3   B   K   | ]}|                               V  d S r   r   r   r   prefix_heads     r    r   zGload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<genexpr>R  sH       >M >M23ALL-->M >M >M >M >M >Mr"   c                     g | ]G}|                               d                     |                    d           dd                   HS r   r   r   s     r    r   zHload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<listcomp>V  sZ     5 5 534||K005HHQWWS\\!""-..5 5 5r"   _keys_to_ignore_on_load_missing"_keys_to_ignore_on_load_unexpectedc                 >    g | ]}t          j        |          |S r   researchr   kpats     r    r   zHload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<listcomp>m  s1          rya/@/@/HA/H/H/Hr"   c                 >    g | ]}t          j        |          |S r   r   r   s     r    r   zHload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<listcomp>s  s1     # # #")C2C2C2KA2K2K2Kr"   )rn   
add_prefixremove_prefixr   rX   c              3       K   | ]}|v V  	d S r   r   )r   r   %expected_keys_not_base_model_prefixeds     r    r   zGload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<genexpr>  s=       4 4 ?? 4 4 4 4 4 4r"   zjThe state dictionary of the model you are trying to load is corrupted. Are you sure it was properly saved?z
	z#Error(s) in loading state_dict for z:
	zESome weights of the model checkpoint were not used when initializing z: z,
- This IS expected if you are initializing z from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForTokenClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing z from the checkpoint of a model that you expect to be exactly identical (initializing a BertForTokenClassification model from a BertForTokenClassification model).z0
- This IS Not expected if you are initializing z from the checkpoint of a model with a same task while the structure is different (e.g. initializing a BertForTokenClassification model from a BertForTokenClassification model).z9All model checkpoint weights were used when initializing z.
zSome weights of zK were not initialized from the model checkpoint and are newly initialized: zo
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.zAll the weights of z were initialized from the model checkpoint If your task is similar to the task the model of the checkpoint was trained on, you can already use z* for predictions without further training.
c           	      .    g | ]\  }}}d | d| d| dS )z- z: found shape z in the checkpoint and z in the model instantiatedr   )r   r   shape1shape2s       r    r   zHload_task_model_checkpoint.<locals>._load_checkpoint.<locals>.<listcomp>  sJ     , , ,'C jSiiiiviii, , ,r"   zl were not initialized from the model checkpoint and are newly initialized because the shapes did not match:
)r   r[   r   base_model_prefixr]   anyr\   r`   r2   r1   strextendr   _init_weightsr;   r   r0   rY   RuntimeError	__class____name__rP   rQ   info)-r#   r   r   rq   
_fast_initrl   expected_keyskeys_from_pretrainedpretrained_has_prefix_modulemodel_expects_prefix_modulerp   ro   rb   unexpected_keysprefix_heads&expected_head_keys_without_head_prefixre   unexpected_head_keyspretrained_has_prefix_headr}   ra   r   r   uninitialized_modulesr:   missing_head_keys_by_headr`   r   model_to_loadheads_to_loadmismatched_keysr   headlocal_error_msgs	error_msgmismatched_warningr   r   rn   r   rf   rw   r   kwargsretrieve_modules_from_namess-                                       @@@@r    _load_checkpointz4load_task_model_checkpoint.<locals>._load_checkpoint  sp	    !++---224455#JOO$5$566( v;;??+. /D /D /D /D.B/D /D /D ,D ,D(*- .= .= .= .=.;.= .= .= += +='' ,1(*/''C#C#cHc :^C^?^# 	75 5 5 5(5 5 51   &  MM ! 	7JJJJMJJJM461C..5I1J1JJKKs#7883};M;MMNN (13.!%)VV"66 |q  S%9%9A%=%=,,, . ,~  , M M:= >M >M >M >M7K>M >M >M ;M ;M*;77  , E E5 5 5 58E5 5 51 7==57 7 7 $E !! !#'O$$<==>$? $?  #O$$<==>? ?O +1**-t+5 +5'-3ZZ0$.8 .8* +66         +      .99  # # # #.# # #
  	,$?$?.6%8 %8 %8! 0 , ,##F++++ %)FF!  1$$/ O O1+> OB`B`"K1A+1N$C& C&?J 1< >O-k: u&''!++G'5) 5)+-I , !2S8Lu&''!++'1) 1)+1M , $E5+BCCM+ I I-4UK-H-Hk** 4 4 4 424 4 4 4 4 ' &' ' '
 0/ $#
 
 10
1=}N N
 }!!% 4 4#>#>!$'TCZ$P $P !!"23333z??QJ//I`eo6N``U^``   !##NNn!&!9n n=Ln n!&!9n n
 O,n n n    %&&**NN6!&!96 6=Q6 6!&!96 6 67 7 7 7 KKiEOLdiii   |q  NNn5?#; n n/;n n n   
 !!Q&&KKeo&>  8=8P    
 !##!% , ,+:, , , " " NN<5?#; < <.< < <= = = _ozIIr"   Fc                    t          d |D                       }|                    t          d |D                                 }g }|                                 D ]\  }}|rH|                    |          r0d                    |                    d          dd                    n|}n.|r,t          |          dk    rd                    ||g          n|}||v r|                    |           |S )Nc                 n    g | ]2}d                      |                    d           dd                   3S )rX   Nr   )rY   ri   r   r   s     r    r   zSload_task_model_checkpoint.<locals>.retrieve_modules_from_names.<locals>.<listcomp>  s7    JJJS388CIIcNN3B3$788JJJr"   c                     g | ]L}|d                                           d                    |                    d          dd                   MS )r   rX   N)isdigitrY   ri   r   s     r    r   zSload_task_model_checkpoint.<locals>.retrieve_modules_from_names.<locals>.<listcomp>  s[       25r7??$$3,--  r"   rX   rh   r   )r\   unionnamed_modulesr   rY   ri   r]   rk   )	r#   namesrn   r   r   module_keysretrieved_modulesrB   r:   s	            r    r   z?load_task_model_checkpoint.<locals>.retrieve_modules_from_names  s1   
 JJEJJJKK "''  9>      !//11 	1 	1LD& M,0OOF,C,CNsxxJJsOOABB') ) )IM  M36t99q==sxx///f{""!((000  r"   c                    |r1t          j        |j                                                  | _        n|j        | _        t	          | dd           [t           j                            | j        j        d| j        j	        d         | j        j	        d         z
  fdd          | j        _        t          | d          rt          |d          r|j        | _        d S d S d S )Nr|   r   constantout_featuresnum_embeddings)r   	Parameterrz   cloner   
functionalpadr|   datarj   r;   r   r   )output_embeddingsinput_embeddingstorchscripts      r    _tie_or_clone_weightsz9load_task_model_checkpoint.<locals>._tie_or_clone_weights  s     	?')| '--//(1 (1$$ (8'>$$fd33?*,-*;*;!&+%,215',2156
 	+ 	+"' $n55 	M' "2;4 ;4 	M-=-L***	M 	M 	M 	Mr"   c                     |rB| j                                         }|)| j                                        } ||           d S d S d S r   )r   get_output_embeddingsencoderget_input_embeddings)r#   tie_word_embeddingsr   r   r   s       r    tie_weightsz/load_task_model_checkpoint.<locals>.tie_weights  sg     	K %
 @ @ B B ,#(=#E#E#G#G %%&79IJJJJJ		K 	K,,r"   r   TrH   )r   rq   r   r   r   )r#   rb   r   r   r   r   )NFF)F)rK   rL   rY   r   TORCH_MODEL_BIN_FILEr7   rN   set_default_dtyper   rR   r   )r   model_local_dirdefault_dtyper   r   r   r   	ckpt_filer   rb   r   r   r   rf   rw   r   r   r   s       `        @@@@@r    load_task_model_checkpointr      s   "? ? ?$  F 4 4 4 4lOJ OJ OJ OJ OJ OJ OJ OJ OJf ,0/427	! ! ! !> +0M M M M2K K K K K K _i.LMMIIEMMMJ ...AQAQ# $B B B>L/?J vzz&!!#8%@@ KM6::f#5#5#IJJJ $**   r"   configc                    t          |t                    r|                                }t          j        |vr!d|t          j                 i|t          j        <   t          j        |dt                    }t          j
                            | t          j                  }t                              |                                |           d S )Nr4      )indentcls)r1   r   to_dictr   pipelinetaskjsondumpsr   rK   rL   rY   r   CONFIGURATIONstorager?   encode)target_folderr   cfg_strconfig_files       r    save_configurationr  >  s    &&!! "!!F**)/8I1J(K|$%j/BCCCG',,}i.EFFKMM'..""K00000r"   r  save_checkpoint_namesave_functionc                 $   |t          |t                    st          d          |t          j                            |          rt          d| d          |t          d          t          j                            ||          }|g}t          |          }|	                    t          j                   |	                    d           |	                    d           t          | d          rT| j        Mt                      r?t          j        j        d	k    rt$          }nt&          } || j        |t)          | d
           	  || |fi | dS # t          $ r-}	t          dt+          |	          j         d|	 d          d}	~	ww xY w)a  save the pretrained model, its configuration and other related files to a directory, so that it can be re-loaded

    Args:
        model (Model): Model whose params are to be saved.

        target_folder (Union[str, os.PathLike]):
        Directory to which to save. Will be created if it doesn't exist.

        save_checkpoint_name (str):
        The checkpoint name to be saved in the target_folder

        save_function (Callable):
        The function to use to save the state dictionary.
    Nz'A valid save function must be passed inzProvided path (z#) should be a directory, not a filez6At least pass in one checkpoint name for saving methodz*.safetensorsz.*	model_dir   T)ignoredirs_exist_okz)During saving checkpoints, the error of "z
 with msg z thrown)r1   r   	ExceptionrK   rL   isfiler0   rY   r\   addr   r  r;   r  r   sysversion_infominorr   r   r   r4   r   )
r#   r  r  r  r   output_ckpt_pathorigin_file_to_be_ignoredignore_file_setcopytree_funces
             r    save_pretrainedr!  H  s   ( J}h$G$GABBB} = =PmPPP
 
 	
 #DF F 	F w||M3GHH "6 6344O	/000(((u  
  % ;	 ;!Q&&$MM)MO"O4		  	  	  	 $e-8888888 $ $ $#Q8H # ## # #$ $ 	$$s   
E 
F"(F

F)NNNTT)NN)3r<   rK   r   r  r.   collectionsr   shutilr   r   r   typingr   r   r	   r
   r  r7   r   torch.optimr   torch.optim.lr_schedulerr   modelscope.fileior   r   modelscope.utils.configr   r   modelscope.utils.constantr   r   modelscope.utils.file_utilsr   modelscope.utils.loggerr   modelscope.utils.torch_utilsr   rP   r	  r!   r   r   r2   boolrF   rU   r   r  PathLiker!  r   r"   r    <module>r/     s   
			 				 				 



  # # # # # # 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2         ! ! ! ! ! ! 1 1 1 1 1 1 0 0 0 0 0 0 0 0 ? ? ? ? ? ? ? ? = = = = = = = = 5 5 5 5 5 5 . . . . . . 2 2 2 2 2 2	
,..  & 6:;?+/&*'+D+ D+58? D+!D+'	2D+ #+<"8D+ #4.	D+
  $D+ !%D+ 15D+ D+ D+ D+R ,015&& &&(&& #/&& && && &&V .2-1\ \ \ \~1d 1 1 1 1 15.2;$ ;$#(bk)9#:;$*-;$ $,;$ ;$ ;$ ;$ ;$ ;$r"   