Ë
    b›¾jÐ:  ã                   ó¤  — d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlmZmZmZmZmZ d dlmZ ddlmZ dd	lmZ dd
lm Z m!Z!m"Z" ddl#m$Z$ e
rddl%m&Z& dZ'dZ(	 d3dejR                  ejT                  dœdddee   de+de	de	dedee   f   fd„Z,dddede-de+de+de+de+d ee.   d!ee.   d"eedee.ef   gdf      fd#„Z/d$„ Z0ddd%ed&ee.e-f   deg ee-ee.e-f   f   f   fd'„Z1ddd(edgee$   f   d)eee$   gee$   f   d*e+fd+„Z2d,eee.ef   ef   ddd-ee.ef   ddfd.„Z3d/eedgdf      dedgdf   fd0„Z4d1ee   ddfd2„Z5y)4é    N)ÚPath)Údefault_timer)
ÚIOÚTYPE_CHECKINGÚAnyÚCallableÚDictÚIterableÚListÚOptionalÚTupleÚUnion)ÚConfigÚ	OptimizerÚconstantÚfix_random_seedÚset_gpu_allocator)ÚPrinteré   )ÚErrors)ÚConfigSchemaTraining)ÚloggerÚregistryÚresolve_dot_namesé   )ÚExample©ÚLanguagez
model-bestz
model-lastéÿÿÿÿ)Úuse_gpuÚstdoutÚstderrÚnlpr   Úoutput_pathr    r!   r"   Úreturnc                óv  ‡ ‡‡‡— t        d¬«      }‰ j                  j                  «       }|d   d   �t        |d   d   «       |d   d   }|dk\  r|rt	        |«       t        j                  |d   t        ¬«      }|d	   |d
   g}	t        ||	«      \  }
}|d   Š|d   }|d   }|d   }t        |d   «      Š|d   }ˆˆ ˆˆfd„}|d   }|d   }t        ‰ ‰t        ‰ |
||d   «      t        ‰ ||«      |d   |d   |d   |d   |d   |||¬«      }t        ‰«       |j                  |j                  d‰ j                   › �«      dz   «       |r&|j                  |j                  d|› �«      dz   «       |r&|j                  |j                  d|› �«      dz   «       |j                  |j                  d‰j"                  › �«      dz   «       ‰ j%                  |¬ «      5   |‰ ||«      \  }}ddd«       	 |D ][  \  }}}|�G‰ j%                  |¬ «      5  t'        |‰ |«       ddd«       ‰� ||«       t)        ‰t*        z  «      |d!<    |�|nd«       Œ] 	  «        ‰�	 |d#«       	 ‰j2                  r‰ j5                  ‰j2                  «       ‰�6|j                  |j7                  d$‰t*        z  «      dz   «       ‰ ‰t*        z  fS ‰ dfS # 1 sw Y   ŒàxY w# 1 sw Y   Œ¸xY w# t,        $ r8}‰�/|j                  |j/                  d"t1        |«      › �«      dz   «       |‚d}~ww xY w#  «        ‰�	 |d#«       w w xY w)%aU  Train a pipeline.

    nlp (Language): The initialized nlp object with the full config.
    output_path (Optional[Path]): Optional output path to save trained model to.
    use_gpu (int): Whether to train on GPU. Make sure to call require_gpu
        before calling this function.
    stdout (file): A file-like object to write output messages. To disable
        printing, set to io.StringIO.
    stderr (file): A second file-like object to write output messages. To disable
        printing, set to io.StringIO.

    RETURNS (tuple): The final nlp object and the path to the exported model.
    T)Úno_printÚtrainingÚseedNÚgpu_allocatorr   )ÚschemaÚtrain_corpusÚ
dev_corpusÚ	optimizerÚscore_weightsÚbatcherr   Úbefore_to_diskÚbefore_updatec                 óV  •— ‰j                  ‰j                  «      5   ‰‰«      j                  ‰t        z  «       d d d «       | rX‰t        z  j                  «       rt        j                  ‰t        z  «       t        j                  ‰t        z  ‰t        z  «       y y # 1 sw Y   ŒdxY w©N)	Ú
use_paramsÚaveragesÚto_diskÚDIR_MODEL_LASTÚDIR_MODEL_BESTÚexistsÚshutilÚrmtreeÚcopytree)Úis_bestr1   r#   r.   r$   s    €€€€ú]/home/spazio/kingdom_calls/work5/transenv/lib/python3.12/site-packages/spacy/training/loop.pyÚsave_checkpointztrain.<locals>.save_checkpointM   sŠ   ø€ Ø�^‰^˜I×.Ñ.Ó/ñ 	FÙ˜3Ó×'Ñ'¨´nÑ(DÔE÷	Fáð œnÑ,×4Ñ4Ô6Ü—‘˜k¬NÑ:Ô;Ü�O‰O˜K¬.Ñ8¸+ÌÑ:VÕWð ÷	Fð 	Fús   �BÂB(Úfrozen_componentsÚannotating_componentsÚ
max_epochsÚdropoutÚaccumulate_gradientÚpatienceÚ	max_stepsÚeval_frequency)rD   rE   rF   rG   rH   ÚexcluderB   r2   z
Pipeline: ú
zFrozen components: zSet annotations on update for: zInitial learn rate: )Údisabler$   zAAborting and saving the final best model. Encountered exception: Fz"Saved pipeline to output directory)r   ÚconfigÚinterpolater   r   r   Úresolver   r   Úcreate_before_to_disk_callbackÚtrain_while_improvingÚcreate_train_batchesÚcreate_evaluation_callbackÚclean_output_dirÚwriteÚinfoÚ
pipe_namesÚ
learn_rateÚselect_pipesÚupdate_metaÚstrr8   Ú	ExceptionÚwarnÚreprr6   r5   Úgood)r#   r$   r    r!   r"   ÚmsgrL   Ú	allocatorÚTÚ	dot_namesr,   r-   r/   r0   Útrain_loggerr2   r@   rA   rB   Útraining_step_iteratorÚlog_stepÚfinalize_loggerÚbatchrU   Úis_best_checkpointÚer1   r.   s   ``                        @@r?   Útrainrj   #   s½  û€ ô, ˜4Ô
 €Cà�Z‰Z×#Ñ#Ó%€FØˆjÑ˜&Ñ!Ð-Ü˜˜zÑ*¨6Ñ2Ô3Ø�zÑ" ?Ñ3€IØ�!‚|™	Ü˜)Ô$Ü×Ñ˜ 
Ñ+Ô4HÔI€AØ�>Ñ" A l¡OÐ4€IÜ0°¸ÓCÑ€L�*Ø�+‘€IØ�oÑ&€MØ�	‰l€GØ�X‘;€LÜ3°AÐ6FÑ4GÓH€NØ�oÑ&€M÷Xð Ð-Ñ.ÐàÐ5Ñ6Ðä2ØØÜ˜S ,°¸¸<¹ÓIÜ" 3¨
°MÓBØ�)‘ØÐ3Ñ4Ø�:‘Ø�K‘.ØÐ)Ñ*Ø!Ø3Ø#ôÐô �[Ô!Ø
‡L�L�—‘˜J s§~¡~Ð&6Ð7Ó8¸4Ñ?Ô@ÙØ�‰�S—X‘XÐ 3Ð4EÐ3FÐGÓHÈ4ÑOÔPÙØ�‰Ø�H‰HÐ6Ð7LÐ6MÐNÓOÐRVÑVô	
ð ‡L�L�—‘Ð0°×1EÑ1EÐ0FÐGÓHÈ4ÑOÔPØ	×	Ñ	Ð"3Ð	Ó	4ñ FÙ$0°°f¸fÓ$EÑ!ˆ�/÷Fð#Ø/Eò 	GÑ+ˆE�4Ð+Ø!Ð-Ø×%Ñ%Ð.?Ð%Ó@ñ .Ü  3¨Ô-÷.àÐ*Ù#Ð$6Ô7Ü*-¨k¼NÑ.JÓ*K�D˜Ñ'ÙÐ/Ð;‘TÀÕFñ	Gñ& 	ÔØÐ"Ù˜EÕ"à×ÒØ�‰�y×)Ñ)Ô*ØÐØ�‰Ø�H‰HÐ9¸;ÌÑ;WÓXØñô	
ð �[¤>Ñ1Ð2Ð2à�Tˆ{Ð÷GFð Fú÷
.ð .ûô ò 	ØÐ"Ø�L‰LØ—‘ð.Ü.2°1«g¨Yð8óð ñ	ôð ˆûð	ûñ 	ÔØÐ"Ù˜EÕ"ð #úsH   ÇKÇ2K  ÈKÈ5K  ËKËK	ËK  Ë 	L!Ë)3LÌL!Ì!L$ Ì$L8r.   rD   rH   rE   rF   rG   rI   rB   r2   c             #   óÄ  K  — t        |t        «      rt        |«      }n|}g }i }d}t        «       }t	        |«      D �]“  \  }\  }}|r||dœ} || |«       t        |«      }t        ||«      D ]  }| j                  |||d|	|
¬«       Œ | j                  D ]D  \  }}||	vsŒt        |d«      sŒ|j                  sŒ%|j                  dvsŒ4|j                  |«       ŒF |j                  «        ||z  sj|j                  r/| j                  |j                  «      5   |«       \  }}ddd«       n
 |«       \  }}|j!                  |f«       |t#        |«      d   k(  }nd\  }}d}|t%        d	„ |D «       «      z  }|||||t'        t        «       |z
  «      |d
œ}|||f–— |�i }t#        d„ |D «       «      }|d    }|r
||z
  |k\  r y|s�Œ�||k\  s�Œ” y y# 1 sw Y   Œ§xY w­w)a  Train until an evaluation stops improving. Works as a generator,
    with each iteration yielding a tuple `(batch, info, is_best_checkpoint)`,
    where info is a dict, and is_best_checkpoint is in [True, False, None] --
    None indicating that the iteration was not evaluated as a checkpoint.
    The evaluation is conducted by calling the evaluate callback.

    Positional arguments:
        nlp: The spaCy pipeline to evaluate.
        optimizer: The optimizer callable.
        train_data (Iterable[Batch]): A generator of batches, with the training
            data. Each batch should be a Sized[Tuple[Input, Annot]]. The training
            data iterable needs to take care of iterating over the epochs and
            shuffling.
        evaluate (Callable[[], Tuple[float, Any]]): A callback to perform evaluation.
            The callback should take no arguments and return a tuple
            `(main_score, other_scores)`. The main_score should be a float where
            higher is better. other_scores can be any object.

    Every iteration, the function yields out a tuple with:

    * batch: A list of Example objects.
    * info: A dict with various information about the last update (see below).
    * is_best_checkpoint: A value in None, False, True, indicating whether this
        was the best evaluation so far. You should use this to save the model
        checkpoints during training. If None, evaluation was not conducted on
        that iteration. False means evaluation was conducted, but a previous
        evaluation was better.

    The info dict provides the following information:

        epoch (int): How many passes over the data have been completed.
        step (int): How many steps have been completed.
        score (float): The main score from the last evaluation.
        other_scores: : The other scores from the last evaluation.
        losses: The accumulated losses throughout training.
        checkpoints: A list of previous results, where each result is a
            (score, step, epoch) tuple.
    r   )ÚstepÚepochF)ÚdropÚlossesÚsgdrI   Ú	annotatesÚis_trainable)TFNN)NNc              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr4   )Úlen)Ú.0Úegs     r?   ú	<genexpr>z(train_while_improving.<locals>.<genexpr>ù   s   è ø€ Ò2 bœ#˜bŸ'Ñ2ùs   ‚)rm   rl   ÚscoreÚother_scoresro   ÚcheckpointsÚsecondsÚwordsc              3   ó,   K  — | ]  \  }}|| f–— Œ y ­wr4   © )ru   Úr_scoreÚr_steps      r?   rw   z(train_while_improving.<locals>.<genexpr>
  s   è ø€ ÒL±°¸&˜7 V GÔ,ÑLùs   ‚r   )Ú
isinstanceÚfloatr   ÚtimerÚ	enumerateÚnextÚsubdivide_batchÚupdateÚpipelineÚhasattrrr   ÚmodelÚfinish_updateÚstep_schedulesr6   r5   ÚappendÚmaxÚsumÚint)r#   r.   Ú
train_dataÚevaluaterD   rH   rE   rF   rG   rI   rB   r2   ÚdropoutsÚresultsro   Ú
words_seenÚ
start_timerl   rm   rg   Úbefore_update_argsÚsubbatchÚnameÚprocrx   ry   rh   rU   Úbest_resultÚ	best_steps                                 r?   rP   rP   ™   s/  è ø€ ôj �'œ5Ô!Ü˜GÓ$‰àˆØ€GØ!€FØ€JÜ“€JÜ )¨*Ó 5ó :Ñˆ‰nˆu�eÙØ*.¸Ñ!?ÐÙ˜#Ð1Ô2Ü�x“.ˆÜ'¨Ð/BÓCò 	ˆHØ�J‰JØØØØØØ/ð õ ð	ð Ÿ,™,ò 	.‰JˆD�$à˜GÒ#Ü˜D .Õ1Ø×%Ó%Ø—J‘JÐ&9Ò9à×"Ñ" 9Õ-ð	.ð 	× Ñ Ô"Ø�~Ò%Ø×!Ò!Ø—^‘^ I×$6Ñ$6Ó7ñ 5Ù*2«*Ñ'�E˜<÷5ð 5ñ '/£jÑ#��|Ø�N‰N˜E 4˜=Ô)Ø!&¬#¨g«,°q©/Ñ!9Ñà".ÑˆE�<Ø!%ÐØ”cÑ2¨EÔ2Ó2Ñ2ˆ
àØØØ(ØØ"Üœ5›7 ZÑ/Ó0Øñ	
ˆð �TÐ-Ð-Ò-ØÐ)ØˆFô ÑLÀGÔLÓLˆØ  ‘^�Oˆ	Ù˜ 	Ñ)¨hÒ6Ùã˜ Ô*Ùñu:÷45ð 5üsD   ‚BG Â G Â-G Â:G Ã	AG ÄGÄ#B$G Ç	G ÇG ÇG	ÇG c              #   óØ   K  — t        | «      } | j                  d„ ¬«       t        | «      |z  }d}t        |«      D ]  }| |||z    }|r|–— |t        |«      z  }Œ  | |d  }|r|–— y y ­w)Nc                 ó,   — t        | j                  «      S r4   )rt   Ú	predicted)rv   s    r?   ú<lambda>z!subdivide_batch.<locals>.<lambda>  s   € œc "§,¡,Ó/€ ó    )Úkeyr   )ÚlistÚsortrt   Úrange)rg   rE   Úsub_lenÚstartÚir˜   s         r?   r†   r†     s‡   è ø€ Ü�‹K€EØ	‡J�JÑ/€JÔ0Ü�%‹jÐ/Ñ/€GØ€EÜÐ&Ó'ò ˆØ˜ ¨¡Ð1ˆÙØŠNØ”�X“Ñ‰ð	ð
 �U�Vˆ}€HÙØ‹ð ùs   ‚A(A*r-   Úweightsc                 ó¶   ‡ ‡‡— ‰j                  «       D ��ci c]  \  }}|€Œ	||“Œ c}}Šdt        t        t        t        t        f   f   fˆˆ ˆfd„}|S c c}}w )Nr%   c            	      ó:  •‡— 	 ‰j                   ‰‰«      «      Š‰j                  «       D ��ci c]  \  }}|€Œ	||“Œ c}}Š‰	j                  «       D ��ci c]  \  }}|‰v sŒ||“Œ c}}Š	‰j                  «       D ]S  \  }}|‰	v sŒt        |t        t        f«      rŒ"t        t        j                  j	                  |t        |«      ¬«      «      ‚ 	 t        ˆˆ	fd„‰	D «       «      }|‰fS # t        $ r9} t        t        j                  j	                  ‰j
                  ¬«      «      | ‚d } ~ ww xY wc c}}w c c}}w # t        $ rU} t        ‰j                  «       «      }t        j                   j	                  dt#        | «      |¬«      }t        |«      d ‚d } ~ ww xY w)N)rˆ   )r™   Ú
score_typec              3   óh   •K  — | ])  }‰j                  |d «      ‰j                  |d «      z  –— Œ+ y­w)ç        N)Úget)ru   ÚsÚscoresr©   s     €€r?   rw   z?create_evaluation_callback.<locals>.evaluate.<locals>.<genexpr>6  s1   øè ø€ ò !Ø=>�—
‘
˜1˜cÓ" W§[¡[°°CÓ%8Õ8ñ!ùs   ƒ/2r/   )Údictr¢   Úkeys)r’   ÚKeyErrorr   ÚE900ÚformatrV   Úitemsr�   r�   r‚   Ú
ValueErrorÚE915Útyper�   r£   r³   ÚE983rZ   )
ri   r¢   ÚvalueÚweighted_scorer³   Úerrr±   r-   r#   r©   s
         @€€€r?   r’   z,create_evaluation_callback.<locals>.evaluate'  sk  ù€ ð	OØ—\‘\¡*¨S£/Ó2ˆFð 06¯|©|«~×S¡  eÀÑAR�#�u‘*ÓSˆØ07·±³×Q¡* # uÀ3È&Â=�3˜‘:ÓQˆØ Ÿ,™,›.ò 	W‰JˆC�Ø�gŠ~¤j°¼¼e¸Õ&EÜ ¤§¡×!3Ñ!3¸ÌÈeËÐ!3Ó!UÓVÐVð	Wð	*Ü ô !ØBIô!ó ˆNð ˜vÐ%Ð%øô% ò 	OÜœ6Ÿ;™;×-Ñ-°s·~±~Ð-ÓFÓGÈQÐNûð	Oüó
 TùÛQøô ò 	*Ü˜Ÿ™›Ó&ˆDÜ—+‘+×$Ñ$¨/¼sÀ1»vÈDÐ$ÓQˆCÜ˜3“- TÐ)ûð	*úsF   „C+ ®
D0¹D0ÁD6Á"D6ÃD< Ã+	D-Ã44D(Ä(D-Ä<	FÅAFÆF)r·   r   r‚   r	   rZ   )r#   r-   r©   r¢   r¼   r’   s   ```   r?   rR   rR   "  sT   ú€ ð -4¯M©M«O×Q™j˜c 5¸uÑ?Pˆs�E‰zÓQ€Gð&”eœE¤4¬¬U¨
Ñ#3Ð3Ñ4÷ &ð0 €Oùó5 Rs
   —
A¢AÚcorpusr0   rC   c              #   ó  K  — d}|dk\  r,t         || «      «      }|st        t        j                  «      ‚|dk  s||k7  rH|dk\  rt	        j
                  «       n || «      } ||«      D ]  }||f–— Œ
 |dz  }|dk  rŒA||k7  rŒGy y ­w)Nr   r   )r£   r¸   r   ÚE986ÚrandomÚshuffle)r#   r¿   r0   rC   rm   Úexamplesrg   s          r?   rQ   rQ   B  s•   è ø€ ð €EØ�Q‚Ü™˜s›Ó$ˆÙäœVŸ[™[Ó)Ð)Ø
�qŠ.˜E ZÒ/Ø˜Š?Ü�N‰N˜8Õ$á˜c“{ˆHÙ˜XÓ&ò 	ˆEØ˜�,Óð	à�‰
ˆð �q‹.˜E ZÕ/ùs   ‚A>B	ÂB	ÂB	r(   rU   c                 óî   — i |j                   d<   | d   D ]*  }|€Œ|d   j                  |d«      |j                   d   |<   Œ, |j                  D ]%  }||d   v sŒ|d   |   |j                   d   |› d�<   Œ' y )NÚperformancer/   ry   r®   ro   Ú_loss)Úmetar¯   rV   )r(   r#   rU   ÚmetricÚ	pipe_names        r?   rY   rY   X  s™   € ð !€C‡H�Hˆ]ÑØ˜?Ñ+ò TˆØÑØ.2°>Ñ.B×.FÑ.FÀvÈsÓ.SˆC�H‰H�]Ñ# FÒ+ðTð —^‘^ò Uˆ	Ø˜˜X™Ò&Ø;?À¹>È)Ñ;TˆC�H‰H�]Ñ# y k°Ð$7Ò8ñUr¡   Úcallbackc                 ó,   ‡ ‡— ddl mŠ d‰d‰fˆˆ fd„}|S )Nr   r   r#   r%   c                 ó¢   •— ‰s| S  ‰| «      }t        |‰«      s5t        j                  j                  dt	        |«      ¬«      }t        |«      ‚|S )Nr1   )r™   r¼   )r�   r   ÚE914r¶   rº   r¸   )r#   Úmodified_nlpr¾   r   rË   s      €€r?   r1   z6create_before_to_disk_callback.<locals>.before_to_diski  sM   ø€ ÙØˆJÙ “}ˆÜ˜,¨Ô1Ü—+‘+×$Ñ$Ð*:Ä$À|ÓBTÐ$ÓUˆCÜ˜S“/Ð!ØÐr¡   )Úlanguager   )rË   r1   r   s   ` @r?   rO   rO   d  s#   ù€ õ $ð˜Hð ¨ö ð Ðr¡   Úpathc                 ó\  — | �o| j                  «       r^| t        z  | t        z  fD ]H  }|j                  «       sŒ	 t        j                  t        |«      «       t        j                  d|«       ŒJ yyy# t        $ r/}t        t        j                  j                  | ¬«      «      |‚d}~ww xY w)a  Remove an existing output directory. Typically used to ensure that that
    a directory like model-best and its contents aren't just being overwritten
    by nlp.to_disk, which could preserve existing subdirectories (e.g.
    components that don't exist anymore).
    Nz%Removed existing output directory: %s)rÑ   )r:   r9   r8   r;   r<   rZ   r   Údebugr[   ÚIOErrorr   ÚE901r¶   )rÑ   Úsubdirri   s      r?   rS   rS   u  s›   € ð Ð˜DŸK™KœMØœnÑ,¨d´^Ñ.CÐDò 	HˆFØ�}‰}�ðHÜ—M‘M¤# f£+Ô.Ü—L‘LÐ!HÈ&ÕQñ		Hð *Ðøô !ò HÜ!¤&§+¡+×"4Ñ"4¸$Ð"4Ó"?Ó@ÀaÐGûðHús   º4A3Á3	B+Á<*B&Â&B+r4   )6rÂ   r;   ÚsysÚpathlibr   Útimeitr   rƒ   Útypingr   r   r   r   r	   r
   r   r   r   r   Ú	thinc.apir   r   r   r   r   Úwasabir   Úerrorsr   Úschemasr   Úutilr   r   r   Úexampler   rÐ   r   r9   r8   r!   r"   r�   rj   r‚   rZ   rP   r†   rR   rQ   rY   rO   rS   r~   r¡   r?   ú<module>rá      sŒ  ðÛ Û Û 
Ý Ý )÷÷ ÷ ÷ VÕ UÝ å Ý *ß 6Ñ 6Ý áÝ#ð €Ø€ð
 #'ðsð Ø—‘Ø—‘òsØ	ðsà˜$‘ðsð ð	sð
 ðsð ðsð ˆ:�x ‘~Ð%Ñ&ósðlwØ	ðwàðwð ðwð ðwð ðwð ðwð ðwð �#‰Yðwð   ™9ðwð ˜H j°$°s¸C°x±.Ð%AÀ4Ð%GÑHÑIówòtðØ	ðØ!)ðØ48¸¸e¸Ñ4Dðàˆb�%˜˜t C¨ JÑ/Ð/Ñ0Ð0Ñ1óð@Ø	ðà�j�\ 8¨GÑ#4Ð4Ñ5ðð �x Ñ(Ð)¨8°GÑ+<Ð<Ñ=ðð ó	ð,	UØ�D˜˜c˜‘N FÐ*Ñ+ð	UØ2<ð	UØDHÈÈcÈÁNð	Uà	ó	UðØ�x  ¨jÐ 8Ñ9Ñ:ðàˆzˆl˜JÐ&Ñ'óð"H˜8 D™>ð H¨dô Hr¡   