
    9i                          S SK r S SKJrJrJrJr  S SKrS SKJ	r	  S SK
Jr  S SKJr  S SKJr  Sr\" \5      r\R$                  " \	R&                  S9 " S S	\5      5       rg)
    N)DictOptionalTupleUnion)Trainers)BaseTrainer)TRAINERS)
get_logger)module_namec                      ^  \ rS rSrS\4U 4S jjrS rS\S\\\\	4      4S jr
 SS\\   S\\\4   4S	 jjrS
rU =r$ )SequenceClassificationTrainer   cfg_filec                 $   > [         TU ]  U5        g)zA trainer is used for Sequence Classification

Based on Config file (*.yaml or *.json), the trainer trains or evaluates on a dataset

Args:
    cfg_file (str): the path of config file
Raises:
    ValueError: _description_
N)super__init__)selfr   argskwargs	__class__s       w/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/trainers/nlp/sequence_classification_trainer.pyr   &SequenceClassificationTrainer.__init__   s     	"    c                 .    [         R                  S5        g )NTrain)loggerinfo)r   r   r   s      r   train#SequenceClassificationTrainer.train    s    Gr   attrreturnc                    UR                  S5      nSR                  U5      n[        U R                  US   5      (       a  U R                  US      OSnUSS  H  n[        XE5      (       d  US4s  $ XE   nM     U(       a	  US:w  a  X44$ US4$ )a  get attribute from config, if the attribute does exist, return false

Example:

>>> self.__attr_is_exist("model path")
>>> out: (model-path, "/workspace/bert-base-sst2")
>>> self.__attr_is_exist("model weights")
>>> out: (model-weights, False)

Args:
    attr (str): attribute str, "model path" -> config["model"][path]

Returns:
    Tuple[Union[str, bool]]:[target attribute name, the target attribute or False]
 -r   N   F )splitjoinhasattrcfg)r   r    pathsattr_strtargetpath_s         r   __attr_is_exist-SequenceClassificationTrainer.__attr_is_exist$   s      

3'.txxq'B'B%(#12YE6))&]F 
 fl##r   checkpoint_pathc           
         SSK nSSKJn  SSKJn  SSKJn  SSKJn  SSK	J
n	  SSKJn
  S	nU R                  S
5      nU R                  S5      nU R                  S5      nXU/nU H*  nUS   (       a  M  [        UR                  US   5      5      e   U(       d&  U R                  S5      S   nU(       d  [!        S5      eUR#                  SU R                  S5      S   5      nU(       d  [!        S5      eU" US   R%                  S5      6 nUS   nU" UUU5      nU
" UUS   SUR&                  S9nUR)                  U5      nUR+                  5         SnSnSnSnSn[-        5       n[-        5       nSnUR.                  R1                  5       (       a  SOSnUR3                  U5        [5        U5       GHM  u  n n! U!R7                  5        V"V#s0 s H5  u  n"n#U"[9        U#UR:                  5      (       a  U#R3                  U5      OU#_M7     n!n"n#[>        R>                  " 5       n$URA                  5          U!RC                  S5      n%U" U!5      n&SSS5        [>        R>                  " 5       n'UU'U$-
  -  nSW&;   d   eU&S   n(URE                  W%RG                  5       5        URE                  U(RG                  5       5        UURI                  URK                  U(SS9U%:H  5      RM                  5       -  nUU%RN                  S   -  n[Q        U(RN                  5      S:X  d  U(RN                  S   S:X  a  URS                  U(U%5      n)O2[Q        U(RN                  5      S:X  a  URU                  U(U%5      n)O[<        eUU)RW                  5       RM                  5       -  nUS-  nUURX                  -  nU S-   S-  S:X  d  GM  [Q        URZ                  5      URX                  -  n*[\        R_                  S R                  U S-   U*5      5        GMP     [\        R_                  S!R                  UUS"-  U-  5      5        UU-  n+[\        R_                  S#R                  U+5      5        [`        Rb                  " U5      n[-        5       n,US    GH  n-U-Re                  S$5      (       a>  UU-  n.[\        R_                  S%R                  U.5      5        U,Rg                  S$U.45        MX  U-S&:X  Ga  URh                  Rj                  S:X  aU  U	" U[`        RJ                  " USS'95      n/[\        R_                  S(R                  U/5      5        U,Rg                  S&U/45        M  U	" U[`        RJ                  " USS'9S)S*9n/[\        R_                  S+R                  U/5      5        U,Rg                  S,U/45        U	" U[`        RJ                  " USS'9S-S*9n/[\        R_                  S.R                  U/5      5        U,Rg                  S/U/45        GMu  [m        S0U--  5      e   [o        U,5      $ s  sn#n"f ! [<         a1    U!R7                  5        V"V#s0 s H	  u  n"n#U"U#_M     Os  sn#n"f n!n"n# GNAf = f! , (       d  f       GN= f)1a  evaluate a dataset

evaluate a dataset via a specific model from the `checkpoint_path` path, if the `checkpoint_path`
does not exist, read from the config file.

Args:
    checkpoint_path (Optional[str], optional): the model path. Defaults to None.

Returns:
    Dict[str, float]: the results about the evaluation
    Example:
    {"accuracy": 0.5091743119266054, "f1": 0.673780487804878}
r   N)load_dataset)GeneralDataset)SequenceClassification)losses)f1_score)
DataLoaderz)Attribute {} is not given in config file!zevaluation metricszevaluation batch_sizezdataset valid filezevaluation model_pathz_Argument checkout_path must be passed if the evaluation-model_path is not given in config file!max_sequence_lengthzevaluation max_sequence_lengthzpArgument max_sequence_length must be passed if the evaluation-max_sequence_length does not exist in config file!/
validationF)
batch_sizeshuffle
collate_fng        zcuda:0cpu	label_idslogits)dimr%      d   zEval: {}/{} steps finishedz/Inference time = {:.2f}s, [{:.4f} ms / sample] i  zEval loss: {}accuracyzAccuracy: {}f1)axiszF1: {}macro)averagezMacro F1: {}zmacro-f1microzMicro F1: {}zmicro-f1zMetric %s not implemented)8torcheasynlp.appzoor3   easynlp.appzoo.datasetr4   ,easynlp.appzoo.sequence_classification.modelr5   easynlp.utilsr6   sklearn.metricsr7   torch.utils.datar8   -_SequenceClassificationTrainer__attr_is_existAttributeErrorformat
ValueErrorgetr'   batch_fnfrom_pretrainedevallistcudais_availableto	enumerateitems
isinstanceTensorRuntimeErrortimeno_gradpopextendtolistsumargmaxitemshapelenmse_losscross_entropymeanr=   datasetr   r   nparrayendswithappendconfig
num_labelsNotImplementedErrordict)0r   r1   r   r   rL   r3   r4   r5   r6   r7   r8   	raise_strmetricseval_batch_sizetest_dataset_pathattrsattr_r:   raw_datasetvalid_datasetpre_datasetvalid_dataloadermodel
total_losstotal_stepstotal_sampleshit_num	total_numlogits_listy_truestotal_spent_timedevice_stepbatchkeyvalinfer_start_timerA   outputsinfer_end_timerB   tmp_loss
total_step	eval_losseval_outputsmetricaccrG   s0                                                   r   evaluate&SequenceClassificationTrainer.evaluateA   s   " 	/9	#(,/?	&&';<../FG 001EF+<=E99$Y%5%5eAh%?@@  "223JKBOO" u  %jj!  !AB2FH #W  #$5b$9$?$?$DE#L1 %]O%8:%&r*"++	- '66G 	


	f&"ZZ4466E%&67LE5	A %*KKM
 %2S &0ell&C&CCFF6NM$1    $yy{!IIk2	, ! "YY[N1A AAw&&&X&FNN9++-.v}}/uyyV,	9;;?46BG++I6<< A%b)9Q)>!??69=V\\"a'!//	B""(--/..00J1K-888M	S A% $,,.1A1L1LM
8??AIz+ ,S 8X 	ELL.5EG 	H ,	O**956hh{+vbkFz**	)N11#67##Z$564<<**a/!'299[r+JKBKK 34 ''r
3!		+B7 ')B KK 5 5b 9: ''R(89!		+B7 ')B KK 5 5b 9: ''R(89)*E*NOO1 "6 L!![   A27++-@-hc3c-@@A !s<   9X<XX0YXY/Y 
?
YY
Y	 )N)__name__
__module____qualname____firstlineno__strr   r   r   r   boolrS   r   r   floatr   __static_attributes____classcell__)r   s   @r   r   r      sg    
# 
#C E%T	2B,C < 37b""*3-b" #3:.b" b"r   r   )rd   typingr   r   r   r   numpyrr   modelscope.metainfor   modelscope.trainers.baser   modelscope.trainers.builderr	   modelscope.utils.loggerr
   PATHr   register_modulebert_sentiment_analysisr   r   r   r   <module>r      s^     / /  ( 0 0 .	D	 
h&F&FGQ"K Q" HQ"r   