o
    h`+                    @   s"  d Z ddlZddlZddlZddlZddlZ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 ddlmZmZmZmZmZmZ ddlZddlmZ ddlmZmZmZm Z m!Z! e!"e#Z$e  rjddl%Z%ej&'d	duo{e(d
d) dkZ*e*rz%ddl+Z+e,e+dre+j-.drdZ*ne(d
d) dkre$/d dZ*W n e0e1fy   dZ*Y nw ej&'ddupej&'dduZ2ere2rzej3dZ4e$5de4 d W n, ej3j6y   zej3dZ4e$5de4 d W n ej3j6y   dZ2Y nw Y nw ddl7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z? ddlm@Z@mAZA dd ZBdd ZCdd  ZDd!d" ZEd#d$ ZFd%d& ZGd'd( ZHd)d* ZId+d, ZJd-d. ZKd/d0 ZLd1d2 ZMd3d4 ZNd5d6 ZOd7d8 ZPd9d: ZQd;d< ZRd=eSd>eTd?e<fd@dAZUd=eSd>eTd?e<fdBdCZVd=eSd>eTd?e<fdDdEZWd=eSd>eTd?e<fdFdGZXdHdI ZYdJdK ZZG dLdM dMe9Z[G dNdO dOe9Z\G dPdQ dQe9Z]G dRdS dSe9Z^G dTdU dUe9Z_G dVdW dWe_Z`G dXdY dYeaZbG dZd[ d[e9ZcG d\d] d]e9ZdG d^d_ d_e9ZeG d`da dae9ZfG dbdc dce9Zge^e]e_ece[e\edeee`efegddZhdedf ZidS )gz+
Integrations with other Python libraries.
    N)asdict)Path)TYPE_CHECKINGAnyDictLiteralOptionalUnion   )__version__)flatten_dictis_datasets_availableis_pandas_availableis_torch_availableloggingcomet_ml
COMET_MODE DISABLEDconfigzcomet.api_keyTz5comet_ml is installed but `COMET_API_KEY` is not set.Fneptunezneptune-clientzNeptune version z available.zNeptune-client version )ProgressCallbackTrainerCallback)PREFIX_CHECKPOINT_DIRBestRunIntervalStrategy)ParallelMode)ENV_VARS_TRUE_VALUESis_torch_tpu_availablec                   C   s2   t dd tv rtd dS tjdd uS )NWANDB_DISABLEDr   zUsing the `WANDB_DISABLED` environment variable is deprecated and will be removed in v5. Use the --report_to flag to control the integrations used for logging result (for instance --report_to none).Fwandb)	osgetenvupperr   loggerwarning	importlibutil	find_spec r)   r)   a/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/integrations/integration_utils.pyis_wandb_availableN   s   r+   c                   C      t jdd uS )Nclearmlr&   r'   r(   r)   r)   r)   r*   is_clearml_availableY      r/   c                   C      t S N)
_has_cometr)   r)   r)   r*   is_comet_available]      r4   c                   C   s    t jdd upt jdd uS )NtensorboardtensorboardXr.   r)   r)   r)   r*   is_tensorboard_availablea   s    r8   c                   C   r,   )Noptunar.   r)   r)   r)   r*   is_optuna_availablee   r0   r:   c                   C   r,   )Nrayr.   r)   r)   r)   r*   is_ray_availablei   r0   r<   c                   C      t  sdS tjdd uS )NFzray.tune)r<   r&   r'   r(   r)   r)   r)   r*   is_ray_tune_availablem      r>   c                   C   r,   )Nsigoptr.   r)   r)   r)   r*   is_sigopt_availables   r0   rA   c                   C   s8   t jdd u r
dS t jdd u rdS t jdd uS )NazuremlFzazureml.corezazureml.core.runr.   r)   r)   r)   r*   is_azureml_availablew   s
   rC   c                   C   s(   t dd dkrdS tjdd uS )NDISABLE_MLFLOW_INTEGRATIONFALSETRUEFmlflow)r!   r"   r#   r&   r'   r(   r)   r)   r)   r*   is_mlflow_available   s   rH   c                   C   s   d t jdt jdfvS )NdagshubrG   r.   r)   r)   r)   r*   is_dagshub_available   s   rJ   c                   C   r1   r2   )_has_neptuner)   r)   r)   r*   is_neptune_available   r5   rL   c                   C   r,   )N
codecarbonr.   r)   r)   r)   r*   is_codecarbon_available   r0   rN   c                   C   r,   )Nflytekitr.   r)   r)   r)   r*   is_flytekit_available   r0   rP   c                   C   r=   )NFzflytekitplugins.deck)rP   r&   r'   r(   r)   r)   r)   r*    is_flyte_deck_standard_available   r?   rQ   c                   C   r,   )Ndvcliver.   r)   r)   r)   r*   is_dvclive_available   r0   rS   c                 C   sl   t  rdd l}t| |jr| jS t rt| tr| S t r$t| tr$| S t r.t| tr.| S t	d| j
 )Nr   zUnknown type for trial )r:   r9   
isinstanceTrialparamsr>   dictrA   r+   RuntimeError	__class__)trialr9   r)   r)   r*   	hp_params   s   


r[   n_trials	directionreturnc                    st  dd l } jjdkr^d fdd	}|dd }|dd}t|tr$|nd }|d ur,d n|}|jd||d|}	|	j||||d |	 sT|	j	}
t
t|
j|
j|
jS |	j}d	d
 |D S t|D ]U}d  _tt j} jjtjkrztdtjj|dd tt|}t| D ]\}}|dkrt j|| q j d d t! dd d u r " } #| _qbd S )Nr   c                    s   d }|rt |D ]}|trt j||}q	d  _ jjdkrD jj	t
jkr,td |  tjjt jdd  j|d n j|| d t dd d u r]  } | _ jS )N   Conly support DDP optuna HPO for ParallelMode.DISTRIBUTED currently.r   srcresume_from_checkpointrd   rZ   	objective)r!   listdir
startswithr   pathjoinrf   args
world_sizeparallel_moder   DISTRIBUTEDrX   _hp_search_setuptorchdistributedbroadcast_object_listpickledumpstraingetattrevaluatecompute_objective)rZ   checkpoint_dir
checkpointsubdirmetricstrainerr)   r*   
_objective   s$   

z(run_hp_search_optuna.<locals>._objectivetimeoutn_jobsr_   )r]   
directions)r\   r   r   c                 S   s"   g | ]}t t|j|j|jqS r)   )r   strnumbervaluesrV   ).0bestr)   r)   r*   
<listcomp>   s   " z(run_hp_search_optuna.<locals>.<listcomp>r`   ra   
local_rankrc   rf   r2   r)   )$r9   rk   process_indexpoprT   listcreate_studyoptimize_is_multi_objective
best_trialr   r   r   valuerV   best_trialsrangerf   rs   rt   rm   r   rn   rX   rp   rq   rr   loadsbytesr   itemssetattrru   rv   rw   rx   )r~   r\   r]   kwargsr9   r   r   r   r   studyr   r   iargs_main_rankrk   keyr   r|   r)   r}   r*   run_hp_search_optuna   s>   r   c                    s  dd l  dd l dtf fdd}| jjs&ddlm} td |dd	| _| 	t
}d | _d
|vrYddi|d
< | jjdkrDd|d
 d< d| jjdkrMdnd }td| d |d
 dd}|| j_d|vrwddlm}	 |	dgd|d< d|v rddlm}
m}m}m} t|d |
|||fr| jjr| jjtjkrtdjt|d j d j!j"|| dt#$fdd}t%drƈj&|_& j!j'|f| (d |d|}|j)d|d d  | jj*d!}t+|j,|j-d |j.|}|d ur| /| |S )"Nr   rZ   c                    sD  zddl m} ||r|t W n	 ty   Y nw d |_ j }|rSd|_|	 }t
t|t d }|j|| d W d    n1 sMw   Y  n|j| d t|dd d u r| }|||_||jdd t }|j|d	  jj|} jj||d
 W d    d S 1 sw   Y  d S d S )Nr   )NotebookProgressCallbackrf   *re   )rZ   T)rf   done)ry   )rz   )transformers.utils.notebookr   pop_callbackadd_callbackr   ModuleNotFoundErrorrf   ru   get_checkpointas_directorynextr   globr   as_posixrv   rw   rx   updatetempfileTemporaryDirectory_tune_save_checkpoint
Checkpointfrom_directoryreport)rZ   local_trainerr   rz   ry   checkpoint_pathr|   temp_checkpoint_dir)r;   r)   r*   r      s8   




"z%run_hp_search_ray.<locals>._objectiver
   )TrainerMemoryTrackerzMemory tracking for your Trainer is currently enabled. Automatically disabling the memory tracker since the memory tracker is not serializable.T)skip_memory_metricsresources_per_trialcpur_   gpuz1 CPUz
 and 1 GPUr   zgNo `resources_per_trial` arg was passed into `hyperparameter_search`. Setting it to a default value of z for each trial.progress_reporter)CLIReporterrf   )metric_columns	scheduler)ASHASchedulerHyperBandForBOHBMedianStoppingRulePopulationBasedTraininga  You are using {cls} as a scheduler but you haven't enabled evaluation during training. This means your trials will not report intermediate results to Ray Tune, and can thus not be stopped early or used to exploit other trials parameters. If this is what you want, do not use {cls}. If you would like to use {cls}, make sure you pass `do_eval=True` and `evaluation_strategy='steps'` in the Trainer `args`.)cls)r   c                     sb   t  r*ddl}tj|j d}tj	d|}tj
|}|tj|j< |j|  | i |S )a"  
        Wrapper around `tune.with_parameters` to ensure datasets_modules are loaded on each Actor.

        Without this, an ImportError will be thrown. See https://github.com/huggingface/transformers/issues/11565.

        Assumes that `_objective`, defined above, is a function.
        r   Nz__init__.pydatasets_modules)r   datasets.loadr!   ri   rj   loadinit_dynamic_modulesr&   r'   spec_from_file_locationmodule_from_specsysmodulesnameloaderexec_module)rk   r   datasetsdynamic_modules_pathspecr   )	trainabler)   r*    dynamic_modules_import_trainableN  s   	z;run_hp_search_ray.<locals>.dynamic_modules_import_trainable
__mixins__)r   num_samples   )metricmodescope)0r;   	ray.trainrW   _memory_trackerr   trainer_utilsr   r$   r%   r   TensorBoardCallbackmodelrk   n_gpuinfoget_n_gpuray.tuner   ray.tune.schedulersr   r   r   r   rT   do_evalevaluation_strategyr   NOrX   formattype__name__tunewith_parameters	functoolswrapshasattrr   runhp_spaceget_best_trial	ray_scoper   trial_idlast_resultr   r   )r~   r\   r]   r   r   r   
_tb_writerresource_msggpus_per_trialr   r   r   r   r   r   analysisr   best_runr)   )r;   r   r*   run_hp_search_ray   sr   &
	

r   c              	   K   s  dd l }| jjdkrtjddkr|d |jdd| d d|dd	gd
|d}t	
d|j  | D ]b}|V d | _| jjd
krj| jjtjkrQtd| |j tjjt| jdd | jd d n| jd |jd t| dd d u r|  }| || _|d| j W d    n1 sw   Y  q8t|  d }t!|j|j"d j#|j$}	|	S ddl m%}
 |
 }|&dd }|d ur|'| |( j)d| d d|dd	gd
|dd}t	
d|j  |j*j+|j,k rs|(|j- ) }d | _| jjd
kr'| jjtjkrtd| | tjjt| jdd | jd d n| jd |d t| dd d u rA|  }| || _d| jdg}|(|j. j)|j|d}t	
d|j d|j d |(|j/ }|j*j+|j,k st|(|j0 / 1 d }t!|j|j#|j$}	|	S t2|D ]Z}d | _tt| j}| jjtjkrtdtjj|dd t3t4|}t5|6 D ]\}}|dkrt7| j|| q| jd d t| dd d u r|  }| || _qd S )Nr   r@   z8.0.0huggingfacezhuggingface-tuneofflinerf   r   )r   rf   strategyr_   )r   r   
parametersr|   parallel_bandwidthbudgetz6created experiment: https://app.sigopt.com/experiment/zConly support DDP Sigopt HPO for ParallelMode.DISTRIBUTED currently.ra   rc   re   )
Connectionproxies)r   r   r|   r   observation_budgetprojectr   r   )
suggestionr   z"[suggestion_id, observation_id]: [, ]r   )8r@   rk   r   r&   metadataversionset_projectcreate_experimentr   r$   r   idlooprf   rl   rm   r   rn   rX   ro   r   rp   rq   rr   rs   rt   ru   rv   rw   rx   
log_metricr   get_best_runsr   r   r   assignmentsr   r   set_proxiesexperimentscreateprogressobservation_countr  suggestionsobservationsfetchbest_assignmentsiterate_pagesr   r   r   r   r   r   )r~   r\   r]   r   r@   
experimentr   r|   r   r   r   connr  r  r   obsr   r   rk   r   r   r)   r)   r*   run_hp_search_sigopts  s   
	*

 
r  c                    s:  ddl m} | stddd ld}jjD ]}t|tr"d} nq|s+t  dgj	_
d d d d |d	d }|d
d }|dd }	|dd }
|ddd }|d d< |d d< |	rk|	|d<  fdd}|sj|||
dn|}td|  j|||d t d  d  d S )Nr
   )r+   z8This function needs wandb installed: `pip install wandb`r   FTr    )run_idrf   hyperparameterssweep_idr  r   entityr   z	eval/lossgoalc                     s  j rj n } | jj_| ji d j}d _jd t	|d d t
dd d u rP }|_t|}|vrPtd d|   d} d d urod	krdj d k }nd
kroj d k}|sw d d u r| j d< j d< t| d< jS )N)r  r   _itemsre   rf   zProvided metric zU not found. This might result in unexpected sweeps charts. The available metrics are Fr  minimizemaximizer   )r   initr   state
trial_namer   r   rf   ru   varsrv   rw   rx   rewrite_logsr$   r%   keysr  rW   )r   r   r|   format_metrics
best_scorer   r]   r   r~   r    r)   r*   r     s6   


z'run_hp_search_wandb.<locals>._objective)r  r"  zwandb sweep id - )functioncountr  rf   r   )integrationsr+   ImportErrorr    callback_handler	callbacksrT   WandbCallbackr   rk   	report_tor   r   sweepr$   r   agentr   )r~   r\   r]   r   r+   reporting_to_wandbcallbackr!  r  r   r"  sweep_configr   r)   r/  r*   run_hp_search_wandb  s:   


!r=  c                  C   s   g } t  rt s| d t r| d t r| d t r%| d t r-| d t r5| d t r=| d t rE| d t	 rM| d	 t
 rU| d
 | S )Nazure_mlr   rI   rR   rG   r   r6   r    rM   r-   )rC   rH   appendr4   rJ   rS   rL   r8   r+   rN   r/   )r2  r)   r)   r*   $get_available_reporting_integrations  s,   









r@  c                 C   s~   i }d}t |}d}t |}|  D ]*\}}||r&||d||d   < q||r6||d||d   < q||d| < q|S )Neval_test_eval/ztest/ztrain/)lenr   rh   )dnew_deval_prefixeval_prefix_lentest_prefixtest_prefix_lenkvr)   r)   r*   r+  3  s   

r+  c                   @   s>   e Zd ZdZdddZdddZdd Zdd	d
Zdd ZdS )r   z
    A [`TrainerCallback`] that sends the logs to [TensorBoard](https://www.tensorflow.org/tensorboard).

    Args:
        tb_writer (`SummaryWriter`, *optional*):
            The writer to use. Will instantiate one if not set.
    Nc                 C   s   t  }|s	td|r8zddlm} || _W n$ ty7   zddlm} || _W n ty4   d | _Y nw Y nw d | _|| _d S )NzuTensorBoardCallback requires tensorboard to be installed. Either update your PyTorch version or install tensorboardX.r   )SummaryWriter)r8   rX   torch.utils.tensorboardrM  _SummaryWriterr3  r7   	tb_writer)selfrP  has_tensorboardrM  r)   r)   r*   __init__L  s(   



zTensorBoardCallback.__init__c                 C   s*   |p|j }| jd ur| j|d| _d S d S )N)log_dir)logging_dirrO  rP  )rQ  rk   rT  r)   r)   r*   _init_summary_writerc  s   

z(TensorBoardCallback._init_summary_writerc           	      K   s   |j sd S d }|jr|j}|d urtj|j|}| jd u r$| || | jd urR| j	d|
  d|v rT|d }t|drV|jd urX|j
 }| j	d| d S d S d S d S d S )Nrk   r   r   model_config)is_world_process_zerois_hyper_param_searchr)  r!   ri   rj   rU  rP  rV  add_textto_json_stringr   r   )	rQ  rk   r(  controlr   rT  r)  r   model_config_jsonr)   r)   r*   on_train_beginh  s&   


z"TensorBoardCallback.on_train_beginc              
   K   s   |j sd S | jd u r| | | jd urJt|}| D ]&\}}t|ttfr1| j|||j	 qt
d| dt| d| d q| j  d S d S )N)Trainer is attempting to log a value of "
" of type 
 for key "zn" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.)rX  rP  rV  r+  r   rT   intfloat
add_scalarglobal_stepr$   r%   r   flushrQ  rk   r(  r\  logsr   rK  rL  r)   r)   r*   on_log~  s(   


zTensorBoardCallback.on_logc                 K   s   | j r| j   d | _ d S d S r2   )rP  closerQ  rk   r(  r\  r   r)   r)   r*   on_train_end  s   

z TensorBoardCallback.on_train_endr2   )	r   
__module____qualname____doc__rS  rV  r^  ri  rl  r)   r)   r)   r*   r   C  s    


r   c                   @   sF   e Zd ZdZdd Zdd ZdddZdd	d
ZdddZdd Z	dS )r6  z{
    A [`TrainerCallback`] that logs metrics, media, model checkpoints to [Weight and Biases](https://www.wandb.com/).
    c                 C   s   t  }|s	td|rdd l}|| _d| _tdd t	dhv r?t
dtd d td	td d
 d| _d S tdd | _d S )NzFWandbCallback requires wandb to be installed. Run `pip install wandb`.r   FWANDB_LOG_MODELrE   rF   zSetting `WANDB_LOG_MODEL` as zn is deprecated and will be removed in version 5 of transformers. Use one of `'end'` or `'checkpoint'` instead.zSetting `WANDB_LOG_MODEL` from z to `end` insteadendfalse)r+   rX   r    _wandb_initializedr!   r"   r#   r   unionDeprecationWarningr$   r   
_log_modellower)rQ  	has_wandbr    r)   r)   r*   rS    s   
zWandbCallback.__init__c           
      K   sT  | j du rdS d| _|jrtd i | }t|dr-|jdur-|j }i ||}|j}i }|dur@||d< |j	|d< n|j	du sP|j	|j
ksP|j	|d< | j jdu re| j jddtdd	i| | j jj|dd
 t| j ddr| j d | j jdddd tdd}	t s|	dv r| j j||	td|jd | j jjdd dS dS )u  
        Setup the optional Weights & Biases (*wandb*) integration.

        One can subclass and override this method to customize the setup if needed. Find more information
        [here](https://docs.wandb.ai/guides/integrations/huggingface). You can also override the following environment
        variables:

        Environment:
        - **WANDB_LOG_MODEL** (`str`, *optional*, defaults to `"false"`):
            Whether to log model and checkpoints during training. Can be `"end"`, `"checkpoint"` or `"false"`. If set
            to `"end"`, the model will be uploaded at the end of training. If set to `"checkpoint"`, the checkpoint
            will be uploaded every `args.save_steps` . If set to `"false"`, the model will not be uploaded. Use along
            with [`~transformers.TrainingArguments.load_best_model_at_end`] to upload best model.

            <Deprecated version="5.0">

            Setting `WANDB_LOG_MODEL` as `bool` will be deprecated in version 5 of 🤗 Transformers.

            </Deprecated>
        - **WANDB_WATCH** (`str`, *optional* defaults to `"false"`):
            Can be `"gradients"`, `"all"`, `"parameters"`, or `"false"`. Set to `"all"` to log gradients and
            parameters.
        - **WANDB_PROJECT** (`str`, *optional*, defaults to `"huggingface"`):
            Set this to a custom string to store results in a different project.
        - **WANDB_DISABLED** (`bool`, *optional*, defaults to `False`):
            Whether to disable wandb entirely. Set `WANDB_DISABLED=true` to disable.
        NTz`Automatic Weights & Biases logging enabled, to disable set os.environ["WANDB_DISABLED"] = "true"r   r   groupr  WANDB_PROJECTr   )allow_val_changedefine_metrictrain/global_stepr   )step_metric	step_syncWANDB_WATCHrr  )allr   	gradientsd   )loglog_freqtransformers_trainer)coder)   )rs  rt  rX  r$   r   to_dictr   r   r)  run_name
output_dirr   r'  r!   r"   r   rv   r}  r   watchmaxlogging_steps_label)
rQ  rk   r(  r   r   combined_dictrW  r)  	init_args_watch_modelr)   r)   r*   setup  sB   




zWandbCallback.setupNc                 K   sR   | j d u rd S |j}|r| j   d| _d |_| js'| j|||fi | d S d S NF)rs  rY  finishrt  r  r  )rQ  rk   r(  r\  r   r   	hp_searchr)   r)   r*   r^    s   

zWandbCallback.on_train_beginc              
   K   st  | j d u rd S | jdv r| jr|jrddlm} ||||d}t }	||	 |j	s9dd t
| j j D nd|j |jd|ji}
td	 |jd u sU|j|jkr]d
| j jj nd
| j jj }| j j|d|
d}t|	dD ]%}| r|j|jdd}||  W d    n1 sw   Y  qu| j j| W d    d S 1 sw   Y  d S d S d S d S )N)rq  rz   r
   Trainerrk   r   	tokenizerc                 S   ,   i | ]\}}t |tjr|d s||qS _rT   numbersNumberrh   r   rK  rL  r)   r)   r*   
<dictcomp>      
z.WandbCallback.on_train_end.<locals>.<dictcomp>rC  ztrain/total_flosszLogging model artifacts. ...zmodel-r   r   r   r  r   wb)r   )rs  rw  rt  rX  r~   r  r   r   
save_modelload_best_model_at_endrW   summaryr   metric_for_best_modelbest_metric
total_flosr$   r   r  r  r   r  r   Artifactr   r   is_filenew_filewrite
read_byteslog_artifact)rQ  rk   r(  r\  r   r  r   r  fake_trainertemp_dirr  
model_nameartifactffar)   r)   r*   rl    s<   



"zWandbCallback.on_train_endc                 K   sR   | j d u rd S | js| ||| |jr't|}| j i |d|ji d S d S )Nr~  )rs  rt  r  rX  r+  r  re  )rQ  rk   r(  r\  r   rh  r   r)   r)   r*   ri  #  s   
zWandbCallback.on_logc           
      K   s   | j dkrg| jri|jrkdd t| jj D }d|j }tj	
|j|}td| d |jd u s:|j|jkrBd| jjj nd| jjj }| jj|d|d}	|	| | jj|	d|j gd	 d S d S d S d S )
Nrz   c                 S   r  r  r  r  r)   r)   r*   r  .  r  z)WandbCallback.on_save.<locals>.<dictcomp>checkpoint- Logging checkpoint artifacts in z. ...r   r  )aliases)rw  rt  rX  rW   rs  r  r   re  r!   ri   rj   r  r$   r   r  r   r  r   r  add_dirr  )
rQ  rk   r(  r\  r   checkpoint_metadatackpt_dirartifact_pathcheckpoint_namer  r)   r)   r*   on_save,  s   
zWandbCallback.on_saver2   NN
r   rm  rn  ro  rS  r  r^  rl  ri  r  r)   r)   r)   r*   r6    s    
D

"	r6  c                   @   s<   e Zd ZdZdd Zdd ZdddZdd	d
Zdd ZdS )CometCallbackz^
    A [`TrainerCallback`] that sends the logs to [Comet ML](https://www.comet.ml/site/).
    c                 C   s   t stdd| _d| _d S )NzLCometCallback requires comet-ml to be installed. Run `pip install comet-ml`.F)r3   rX   rt  _log_assetsrQ  r)   r)   r*   rS  F  s   
zCometCallback.__init__c                 C   s  d| _ tdd }|dv rd| _|jrtdd }d}dtd	d
i}|dkr?tjdi |}|dd t	
d n|dkr^tdd|d< tjdi |}|dd t	
d |dur|j|dd |j|ddd t|dr|j|jddd dS dS dS dS )a  
        Setup the optional Comet.ml integration.

        Environment:
        - **COMET_MODE** (`str`, *optional*, defaults to `ONLINE`):
            Whether to create an online, offline experiment or disable Comet logging. Can be `OFFLINE`, `ONLINE`, or
            `DISABLED`.
        - **COMET_PROJECT_NAME** (`str`, *optional*):
            Comet project name for experiments.
        - **COMET_OFFLINE_DIRECTORY** (`str`, *optional*):
            Folder to use for saving offline experiments when `COMET_MODE` is `OFFLINE`.
        - **COMET_LOG_ASSETS** (`str`, *optional*, defaults to `TRUE`):
            Whether or not to log training assets (tf event logs, checkpoints, etc), to Comet. Can be `TRUE`, or
            `FALSE`.

        For a number of configurable items in the environment, see
        [here](https://www.comet.ml/docs/python-sdk/advanced/#comet-configuration-variables).
        TCOMET_LOG_ASSETSrE   >   1rF   r   ONLINENproject_nameCOMET_PROJECT_NAMEr   zCreated fromtransformersz)Automatic Comet.ml online logging enabledOFFLINECOMET_OFFLINE_DIRECTORYz./offline_directoryzLAutomatic Comet.ml offline logging enabled; use `comet upload` when finished)	frameworkzargs/)prefixr  r   zconfig/r)   )rt  r!   r"   r#   r  rX  r   
Experiment	log_otherr$   r   OfflineExperiment_set_model_graph_log_parametersr   r   )rQ  rk   r(  r   
log_assets
comet_moder  experiment_kwargsr)   r)   r*   r  L  s2   

zCometCallback.setupNc                 K      | j s| ||| d S d S r2   rt  r  rQ  rk   r(  r\  r   r   r)   r)   r*   r^  v     zCometCallback.on_train_beginc                 K   sN   | j s
| ||| |jr#tj }|d ur%|j||j|jdd d S d S d S )Nr  )stepepochr  )	rt  r  rX  r   r   get_global_experiment_log_metricsre  r  )rQ  rk   r(  r\  r   rh  r   r  r)   r)   r*   ri  z  s   
zCometCallback.on_logc                 K   s`   | j r*|jr,tj }|d ur.| jdu r$td |j|j	dd|j
d |  d S d S d S d S )NTz(Logging checkpoints. This may take time.)	recursivelog_file_namer  )rt  rX  r   r   r  r  r$   r   log_asset_folderr  re  rq  )rQ  rk   r(  r\  r   r  r)   r)   r*   rl    s   


zCometCallback.on_train_endr2   r  )	r   rm  rn  ro  rS  r  r^  ri  rl  r)   r)   r)   r*   r  A  s    
*
r  c                   @   s,   e Zd ZdZd	ddZdd Zd	ddZdS )
AzureMLCallbackzh
    A [`TrainerCallback`] that sends the logs to [AzureML](https://pypi.org/project/azureml-sdk/).
    Nc                 C   s   t  std|| _d S )NzPAzureMLCallback requires azureml to be installed. Run `pip install azureml-sdk`.)rC   rX   azureml_run)rQ  r  r)   r)   r*   rS    s   
zAzureMLCallback.__init__c                 K   s2   ddl m} | jd u r|jr| | _d S d S d S )Nr   Run)azureml.core.runr  r  rX  get_context)rQ  rk   r(  r\  r   r  r)   r)   r*   on_init_end  s   zAzureMLCallback.on_init_endc                 K   sJ   | j r|jr!| D ]\}}t|ttfr| j j|||d q
d S d S d S )N)description)r  rX  r   rT   rb  rc  r  rg  r)   r)   r*   ri    s   zAzureMLCallback.on_logr2   )r   rm  rn  ro  rS  r  ri  r)   r)   r)   r*   r    s
    
r  c                   @   sL   e Zd ZdZdd Zdd ZdddZdd	d
Zdd Zdd Z	dd Z
dS )MLflowCallbackz
    A [`TrainerCallback`] that sends the logs to [MLflow](https://www.mlflow.org/). Can be disabled by setting
    environment variable `DISABLE_MLFLOW_INTEGRATION = TRUE`.
    c                 C   sJ   t  stddd l}|jjj| _|jjj| _d| _	d| _
d| _|| _d S )NzIMLflowCallback requires mlflow to be installed. Run `pip install mlflow`.r   F)rH   rX   rG   utils
validationMAX_PARAM_VAL_LENGTH_MAX_PARAM_VAL_LENGTHMAX_PARAMS_TAGS_PER_BATCH_MAX_PARAMS_TAGS_PER_BATCHrt  _auto_end_run_log_artifacts_ml_flow)rQ  rG   r)   r)   r*   rS    s   
zMLflowCallback.__init__c              
   C   s  t dd tv | _t dd tv | _t dd| _t dd tv | _t dd| _t	
d| j d	|j d
| j d| j  |jr| j du sT| jsT| jrx| jr^| j| j | jj|j| jd t	
d| j jj  d| _| }t|dr|jdur|j }i ||}| jrt|n|}t| D ]\}}tt|| jkrt	d| d| d ||= qt| }tdt|| jD ]}	| j t!||	|	| j   qt dd}
|
rt"#|
}
| j$|
 d| _%dS )at  
        Setup the optional MLflow integration.

        Environment:
        - **HF_MLFLOW_LOG_ARTIFACTS** (`str`, *optional*):
            Whether to use MLflow `.log_artifact()` facility to log artifacts. This only makes sense if logging to a
            remote server, e.g. s3 or GCS. If set to `True` or *1*, will copy each saved checkpoint on each save in
            [`TrainingArguments`]'s `output_dir` to the local or remote artifact storage. Using it without a remote
            storage will just copy the files to your artifact location.
        - **MLFLOW_EXPERIMENT_NAME** (`str`, *optional*, defaults to `None`):
            Whether to use an MLflow experiment_name under which to launch the run. Default to `None` which will point
            to the `Default` experiment in MLflow. Otherwise, it is a case sensitive name of the experiment to be
            activated. If an experiment with this name does not exist, a new experiment with this name is created.
        - **MLFLOW_TAGS** (`str`, *optional*):
            A string dump of a dictionary of key/value pair to be added to the MLflow run as tags. Example:
            `os.environ['MLFLOW_TAGS']='{"release.candidate": "RC1", "release.version": "2.2.0"}'`.
        - **MLFLOW_NESTED_RUN** (`str`, *optional*):
            Whether to use MLflow nested runs. If set to `True` or *1*, will create a nested run inside the current
            run.
        - **MLFLOW_RUN_ID** (`str`, *optional*):
            Allow to reattach to an existing run which can be usefull when resuming training from a checkpoint. When
            `MLFLOW_RUN_ID` environment variable is set, `start_run` attempts to resume a run with the specified run ID
            and other parameters are ignored.
        - **MLFLOW_FLATTEN_PARAMS** (`str`, *optional*, defaults to `False`):
            Whether to flatten the parameters dictionary before logging.
        HF_MLFLOW_LOG_ARTIFACTSrE   MLFLOW_NESTED_RUNMLFLOW_EXPERIMENT_NAMENMLFLOW_FLATTEN_PARAMSMLFLOW_RUN_IDzMLflow experiment_name=z, run_name=z	, nested=z, tags=)r  nestedzMLflow run started with run_id=Tr   r_  z" for key "z" as a parameter. MLflow's log_param() only accepts values no longer than 250 characters so we dropped this attribute. You can use `MLFLOW_FLATTEN_PARAMS` environment variable to flatten the parameters and avoid this message.r   MLFLOW_TAGS)&r!   r"   r#   r   r  _nested_run_experiment_name_flatten_params_run_idr$   debugr  rX  r  
active_runset_experiment	start_runr   r  r  r  r   r   r   r   r   rD  r   r  r%   r   r  
log_paramsrW   jsonr   set_tagsrt  )rQ  rk   r(  r   r  rW  r   r   combined_dict_itemsr   mlflow_tagsr)   r)   r*   r    sJ   
 

zMLflowCallback.setupNc                 K   r  r2   r  r  r)   r)   r*   r^    r  zMLflowCallback.on_train_beginc           
   
   K   s   | j s
| ||| |jr@i }| D ]!\}}	t|	ttfr#|	||< qtd|	 dt	|	 d| d q| j
j||jd d S d S )Nr_  r`  ra  zc" as a metric. MLflow's log_metric() only accepts float and int types so we dropped this attribute.)r|   r  )rt  r  rX  r   rT   rb  rc  r$   r%   r   r  log_metricsre  )
rQ  rk   r(  r\  rh  r   r   r|   rK  rL  r)   r)   r*   ri    s   
zMLflowCallback.on_logc                 K   s:   | j r|jr| jr| j r| j  d S d S d S d S d S r2   )rt  rX  r  r  r  end_runrk  r)   r)   r*   rl    s
   zMLflowCallback.on_train_endc                 K   sp   | j r2|jr4| jr6d|j }tj|j|}t	d| d | j
jj|d|i| j
j d d S d S d S d S )Nr  r  . This may take time.
model_path)	artifactspython_model)rt  rX  r  re  r!   ri   rj   r  r$   r   r  pyfunc	log_modelPythonModelrQ  rk   r(  r\  r   r  r  r)   r)   r*   r    s   

zMLflowCallback.on_savec                 C   s@   | j rtt| jdd r| j d ur| j  d S d S d S d S )Nr  )r  callablerv   r  r  r  r  r)   r)   r*   __del__!  s   zMLflowCallback.__del__r2   )r   rm  rn  ro  rS  r  r^  ri  rl  r  r  r)   r)   r)   r*   r    s    
F
r  c                       s4   e Zd ZdZ fddZ fddZdd Z  ZS )DagsHubCallbackzh
    A [`TrainerCallback`] that logs to [DagsHub](https://dagshub.com/). Extends [`MLflowCallback`]
    c                    s.   t    t stdddlm} || _d S )NzLDagsHubCallback requires dagshub to be installed. Run `pip install dagshub`.r   )Repo)superrS  rJ   r3  dagshub.uploadr  )rQ  r  rY   r)   r*   rS  1  s
   

zDagsHubCallback.__init__c                    s   t dd tv | _t dpd| _t d| _| j| jt j	d | jt j	d dd	 t d
p6dd| _
td| _| jdu rHtdt j|i | dS )z
        Setup the DagsHub's Logging integration.

        Environment:
        - **HF_DAGSHUB_LOG_ARTIFACTS** (`str`, *optional*):
                Whether to save the data and model artifacts for the experiment. Default to `False`.
        HF_DAGSHUB_LOG_ARTIFACTSrE   HF_DAGSHUB_MODEL_NAMEmainMLFLOW_TRACKING_URI.r   BRANCH)ownerr   branchr  NzpDagsHubCallback requires the `MLFLOW_TRACKING_URI` environment variable to be set. Did you run `dagshub.init()`?)r!   r"   r#   r   log_artifactsr   remoter  splitseprepor   ri   rX   r  r  )rQ  rk   r   r  r)   r*   r  :  s   	

zDagsHubCallback.setupc                 K   sP   | j r&t| dd rt| jjtj|j	d | j
t| j|j	 d S d S )Ntrain_dataloaderz
dataset.pt)r%  rv   rp   saver*  datasetr!   ri   rj   r  r)  	directoryr   r  rk  r)   r)   r*   rl  U  s
   zDagsHubCallback.on_train_end)r   rm  rn  ro  rS  r  rl  __classcell__r)   r)   r  r*   r  ,  s
    	r  c                       s   e Zd Z fddZ  ZS )NeptuneMissingConfigurationc                    s   t  d d S )NaA  
        ------ Unsupported ---- We were not able to create new runs. You provided a custom Neptune run to
        `NeptuneCallback` with the `run` argument. For the integration to work fully, provide your `api_token` and
        `project` by saving them as environment variables or passing them to the callback.
        )r  rS  r  r  r)   r*   rS  ^  s   z$NeptuneMissingConfiguration.__init__)r   rm  rn  rS  r.  r)   r)   r  r*   r/  ]  s    r/  c                   @   s<  e Zd ZdZdZdZdZdZdZdhZ	dddd	dd
ddde
e de
e de
e dedede
e fddZdd Zdd Zdd Zdd Zdd Zedd Zed d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*ed+efd,d-Zd.d/ Zd?d0d1Zd2d3 Zd4d5 Zd6d7 Zd?d8d9Z e!d:d; Z"d?d<e
e#ee$f  fd=d>Z%dS )@NeptuneCallbacka  TrainerCallback that sends the logs to [Neptune](https://app.neptune.ai).

    Args:
        api_token (`str`, *optional*): Neptune API token obtained upon registration.
            You can leave this argument out if you have saved your token to the `NEPTUNE_API_TOKEN` environment
            variable (strongly recommended). See full setup instructions in the
            [docs](https://docs.neptune.ai/setup/installation).
        project (`str`, *optional*): Name of an existing Neptune project, in the form "workspace-name/project-name".
            You can find and copy the name in Neptune from the project settings -> Properties. If None (default), the
            value of the `NEPTUNE_PROJECT` environment variable is used.
        name (`str`, *optional*): Custom name for the run.
        base_namespace (`str`, optional, defaults to "finetuning"): In the Neptune run, the root namespace
            that will contain all of the metadata logged by the callback.
        log_parameters (`bool`, *optional*, defaults to `True`):
            If True, logs all Trainer arguments and model parameters provided by the Trainer.
        log_checkpoints (`str`, *optional*): If "same", uploads checkpoints whenever they are saved by the Trainer.
            If "last", uploads only the most recently saved checkpoint. If "best", uploads the best checkpoint (among
            the ones saved by the Trainer). If `None`, does not upload checkpoints.
        run (`Run`, *optional*): Pass a Neptune run object if you want to continue logging to an existing run.
            Read more about resuming runs in the [docs](https://docs.neptune.ai/logging/to_existing_object).
        **neptune_run_kwargs (*optional*):
            Additional keyword arguments to be passed directly to the
            [`neptune.init_run()`](https://docs.neptune.ai/api/neptune#init_run) function when a new run is created.

    For instructions and examples, see the [Transformers integration
    guide](https://docs.neptune.ai/integrations/transformers) in the Neptune documentation.
    z%source_code/integrations/transformersmodel_parametersrZ   trial_paramstrainer_parametersztrain/epochN
finetuningT)	api_tokenr  r   base_namespacer   log_parameterslog_checkpointsr5  r  r   r6  r7  r8  c                K   s^  t  stdzddlm}	 ddlm}
 W n ty*   ddlm}
 ddlm}	 Y nw |
d|t	t
d f |
d|t	t
d f |
d|t	t
d f |
d|t	 |
d	||	t
d f |
d
|t |
d|t	t
d f || _|| _|| _|| _d | _d| _d | _d| _|||d|| _d | _| jd u| _d | _| jdv rd| j | _d| _d S d| _d| _d S )NzwNeptuneCallback requires the Neptune client library to be installed. To install the library, run `pip install neptune`.r   r  )verify_typer5  r  r   r6  r   r7  r8  F)r5  r  r   >   r   lastzcheckpoints/Tcheckpoints)rL   
ValueErrorr   r  neptune.internal.utilsr9  r3  neptune.new.internal.utils#neptune.new.metadata_containers.runr   r   bool_base_namespace_pathr  _log_checkpoints_initial_run_run_is_monitoring_runr   _force_reset_monitoring_run_init_run_kwargs_volatile_checkpoints_dir_should_upload_checkpoint_recent_checkpoint_path_target_checkpoints_namespace*_should_clean_recently_uploaded_checkpoint)rQ  r5  r  r   r6  r   r7  r8  neptune_run_kwargsr  r9  r)   r)   r*   rS    sF   


zNeptuneCallback.__init__c                 C   s"   | j r| j   | ` d | _ d S d S r2   )rD  stopr  r)   r)   r*   _stop_run_if_exists  s
   

z#NeptuneCallback._stop_run_if_existsc              
   K   s   zddl m} ddlm}m} W n ty'   ddlm} ddlm}m} Y nw |   z|di | j	|| _
| j
d  | _W d S  ||fyT } zt |d }~ww )Nr   )init_run)NeptuneMissingApiTokenException"NeptuneMissingProjectNameExceptionsys/idr)   )r   rP  neptune.exceptionsrQ  rR  r3  neptune.newneptune.new.exceptionsrO  rG  rD  r  r   r/  )rQ  additional_neptune_kwargsrP  rQ  rR  er)   r)   r*   _initialize_run  s   zNeptuneCallback._initialize_runc                 C   s(   | j | _d| _| jd  | _d | _ d S )NTrS  )rC  rD  rE  r  r   r  r)   r)   r*   _use_initial_run  s   
z NeptuneCallback._use_initial_runc                 C   sb   | j d ur|   d S | js| jrd S | jr(| js(| js(| j| jd d| _d S |   d| _d S )N)with_idTF)rC  rZ  rF  rE  rD  rY  r   r  r)   r)   r*   _ensure_run_with_monitoring  s   


z+NeptuneCallback._ensure_run_with_monitoringc                 C   s@   | j d ur|   d S | js| j| jddddd d| _d S d S )NF)r[  capture_stdoutcapture_stderrcapture_hardware_metricscapture_traceback)rC  rZ  rD  rY  r   rE  r  r)   r)   r*   '_ensure_at_least_run_without_monitoring  s   

z7NeptuneCallback._ensure_at_least_run_without_monitoringc                 C   s   | j d u r	|   | j S r2   )rD  ra  r  r)   r)   r*   r     s   
zNeptuneCallback.runc                 C   s   | j | j S r2   )r   rA  r  r)   r)   r*   _metadata_namespace  s   z#NeptuneCallback._metadata_namespacec                 C   s   t | jtj< d S r2   )r	  r   r0  integration_version_keyr  r)   r)   r*   _log_integration_version  r0   z(NeptuneCallback._log_integration_versionc                 C   s   |  | jtj< d S r2   )to_sanitized_dictrb  r0  trainer_parameters_key)rQ  rk   r)   r)   r*   _log_trainer_parameters  s   z'NeptuneCallback._log_trainer_parametersc                 C   sJ   ddl m} |rt|dr!|jd ur#||j | jtj< d S d S d S d S )Nr   )stringify_unsupportedr   )neptune.utilsrh  r   r   r  rb  r0  model_parameters_key)rQ  r   rh  r)   r)   r*   _log_model_parameters  s   z%NeptuneCallback._log_model_parametersc                 C   sR   |rt |dr|j| jtj< |r#t |dr%|jd ur'|j| jtj< d S d S d S d S )Nr)  r2  )r   r)  rb  r0  trial_name_keyr2  trial_params_key)rQ  r(  r)   r)   r*   "_log_hyper_param_search_parameters  s
   z2NeptuneCallback._log_hyper_param_search_parameterssource_directoryrz   c           	   
   C   s   t j|| }}| jd urMt j| j|}z|ddt jj}t j||}t|| |}W n t	yL } zt
d| W Y d }~nd }~ww | j| j | | jrh| jd urh| j| j | j || _d S )Nz..r   zpNeptuneCallback was unable to made a copy of checkpoint due to I/O exception: '{}'. Could fail trying to upload.)r!   ri   rj   rH  replacelstripr(  shutilcopytreeIOErrorr$   r%   r   rb  rK  upload_filesrL  rJ  delete_files)	rQ  ro  rz   target_pathrelative_pathconsistent_checkpoint_path	cpkt_path	copy_pathrX  r)   r)   r*   _log_model_checkpoint  s&   

z%NeptuneCallback._log_model_checkpointc                 K   sH   d | _ | jr|js|jd urt j| _ | jdkr |js"tdd S d S )Nr   zWTo save the best model checkpoint, the load_best_model_at_end argument must be enabled.)	rH  rB  overwrite_output_dirsave_total_limitr   r   r   r  r<  rk  r)   r)   r*   r  2  s   zNeptuneCallback.on_init_endc                 K   sR   |j sd S |   d| _|   | jr| | | | |jr'| | d S d S )NT)	rX  r\  rF  rd  r  rg  rk  rY  rn  r  r)   r)   r*   r^  :  s   

zNeptuneCallback.on_train_beginc                 K   s   |    d S r2   )rO  rk  r)   r)   r*   rl  I  s   zNeptuneCallback.on_train_endc                 C   s&   | j d urtj| j dd |   d S )NT)ignore_errors)rH  rr  rmtreerO  r  r)   r)   r*   r  L  s   
zNeptuneCallback.__del__c                 K   s$   | j r| |jd|j  d S d S )Nr  )rI  r|  r  re  rk  r)   r)   r*   r  R  s   zNeptuneCallback.on_savec           	      K   s`   | j dkr.|j}|dsd| }||}|jrtjntj}|jd u p*|||j| _	d S d S )Nr   rA  )
rB  r  rh   r   greater_is_betternpgreaterlessr  rI  )	rQ  rk   r(  r\  r|   r   best_metric_namemetric_valueoperatorr)   r)   r*   on_evaluateV  s   



zNeptuneCallback.on_evaluatec                 C   s*   |j jD ]}t|| r|j  S qtd)Nz6The trainer doesn't have a NeptuneCallback configured.)r4  r5  rT   r   	Exception)r   r~   r;  r)   r)   r*   get_runb  s
   

zNeptuneCallback.get_runrh  c                 K   sj   |j sd S |d ur1t| D ]#\}}t|ttfr0|tjv r%|| j|< q| j| j	||j
d qd S d S )N)r  )rX  r+  r   rT   rb  rc  r0  flat_metricsrb  r  re  )rQ  rk   r(  r\  rh  r   r   r   r)   r)   r*   ri  j  s   
zNeptuneCallback.on_logr2   )&r   rm  rn  ro  rc  rj  rl  rm  rf  r  r   r   r@  rS  rO  rY  rZ  r\  ra  propertyr   rb  rd  rg  rk  rn  r|  r  r^  rl  r  r  r  classmethodr  r   rc  ri  r)   r)   r)   r*   r0  h  sf    	
7




 r0  c                   @   s2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )CodeCarbonCallbackzI
    A [`TrainerCallback`] that tracks the CO2 emission of training.
    c                 C   s&   t  stddd l}|| _d | _d S )NzWCodeCarbonCallback requires `codecarbon` to be installed. Run `pip install codecarbon`.r   )rN   rX   rM   _codecarbontracker)rQ  rM   r)   r)   r*   rS  |  s   
zCodeCarbonCallback.__init__c                 K   s.   | j d u r|jr| jj|jd| _ d S d S d S )N)r  )r  is_local_process_zeror  EmissionsTrackerr  rk  r)   r)   r*   r    s   zCodeCarbonCallback.on_init_endNc                 K   "   | j r|jr| j   d S d S d S r2   )r  r  startr  r)   r)   r*   r^       z!CodeCarbonCallback.on_train_beginc                 K   r  r2   )r  r  rN  rk  r)   r)   r*   rl    r  zCodeCarbonCallback.on_train_endr2   )r   rm  rn  ro  rS  r  r^  rl  r)   r)   r)   r*   r  w  s    

r  c                   @   sF   e Zd ZdZdd Zdd ZdddZdd	d
ZdddZdd Z	dS )ClearMLCallbacka  
    A [`TrainerCallback`] that sends the logs to [ClearML](https://clear.ml/).

    Environment:
    - **CLEARML_PROJECT** (`str`, *optional*, defaults to `HuggingFace Transformers`):
        ClearML project name.
    - **CLEARML_TASK** (`str`, *optional*, defaults to `Trainer`):
        ClearML task name.
    - **CLEARML_LOG_MODEL** (`bool`, *optional*, defaults to `False`):
        Whether to log models as artifacts during training.
    c                 C   sR   t  rdd l}|| _ntdd| _d| _d | _tdd	 t
dhv | _d S )Nr   zNClearMLCallback requires 'clearml' to be installed. Run `pip install clearml`.FCLEARML_LOG_MODELrE   rF   )r/   r-   _clearmlrX   rt  _initialized_externally_clearml_taskr!   r"   r#   r   ru  rw  )rQ  r-   r)   r)   r*   rS    s   "zClearMLCallback.__init__c                 K   s   | j d u rd S | jrd S |jrltd | jd u rQ| j j r2| j j | _d| _d| _td n| j jj	t
ddt
ddddd	dd
| _d| _td | j|d t|drn|jd urp| j|jd d S d S d S d S )Nz"Automatic ClearML logging enabled.Tz)External ClearML Task has been connected.CLEARML_PROJECTzHuggingFace TransformersCLEARML_TASKr  F)r6   pytorch)r  	task_nameauto_connect_frameworks
output_uriz"ClearML Task has been initialized.Argsr   zModel Configuration)r  rt  rX  r$   r   r  Taskcurrent_taskr  r'  r!   r"   connectr   r   )rQ  rk   r(  r   r  r   r)   r)   r*   r    s2   





zClearMLCallback.setupNc                 K   s@   | j d u rd S |jrd| _| js| j||||fi | d S d S r  )r  rY  rt  r  )rQ  rk   r(  r\  r   r  r   r)   r)   r*   r^    s   
zClearMLCallback.on_train_beginc           	      K   s:   | j d u rd S | jr|jr| js| j  d S d S d S d S r2   )r  r  rX  r  rj  )	rQ  rk   r(  r\  r   r  r|   rh  r   r)   r)   r*   rl    s
   
zClearMLCallback.on_train_endc              
   K   s2  | j d u rd S | js| j||||fi | |jrd}t|}	d}
t|
}g d}| D ]i\}}t|ttfr||v rG| j	
 j||d q-||r^| j	
 j||	d  d||jd q-||
ru| j	
 j||d  d||jd q-| j	
 j|d||jd q-td	| d
t| d| d q-d S d S )NrA  rB  )train_runtimetrain_samples_per_secondtrain_steps_per_second
train_lossr  r  r  eval)titleseriesr   	iterationtestru   r_  r`  ra  zn" as a scalar. This invocation of ClearML logger's  report_scalar() is incorrect so we dropped this attribute.)r  rt  r  rX  rD  r   rT   rb  rc  r  
get_loggerreport_single_valuerh   report_scalarre  r$   r%   r   )rQ  rk   r(  r\  r   r  rh  r   rG  rH  rI  rJ  single_value_scalarsrK  rL  r)   r)   r*   ri    sH   






zClearMLCallback.on_logc                 K   sd   | j r,| jr.|jr0d|j }tj|j|}t	d| d | jj
||jdd d S d S d S d S )Nr  r  r  F)r  auto_delete_file)rw  r  rX  re  r!   ri   rj   r  r$   r   update_output_modelr  r)   r)   r*   r    s   zClearMLCallback.on_saver  )NNNN)NNNr  r)   r)   r)   r*   r    s    


*r  c                       s<   e Zd ZdZddedef fddZdd Zd	d
 Z  ZS )FlyteCallbacka^  A [`TrainerCallback`] that sends the logs to [Flyte](https://flyte.org/).
    NOTE: This callback only works within a Flyte task.

    Args:
        save_log_history (`bool`, *optional*, defaults to `True`):
            When set to True, the training logs are saved as a Flyte Deck.

        sync_checkpoints (`bool`, *optional*, defaults to `True`):
            When set to True, checkpoints are synced with Flyte and can be used to resume training in the case of an
            interruption.

    Example:

    ```python
    # Note: This example skips over some setup steps for brevity.
    from flytekit import current_context, task


    @task
    def train_hf_transformer():
        cp = current_context().checkpoint
        trainer = Trainer(..., callbacks=[FlyteCallback()])
        output = trainer.train(resume_from_checkpoint=cp.restore())
    ```
    Tsave_log_historysync_checkpointsc                    sX   t    t stdt rt std d}ddlm	} | j
| _|| _|| _d S )NzLFlyteCallback requires flytekit to be installed. Run `pip install flytekit`.zSyncing log history requires both flytekitplugins-deck-standard and pandas to be installed. Run `pip install flytekitplugins-deck-standard pandas` to enable this feature.Fr   )current_context)r  rS  rP   r3  rQ   r   r$   r%   rO   r  rz   cpr  r  )rQ  r  r  r  r  r)   r*   rS  (  s   


zFlyteCallback.__init__c                 K   sR   | j r%|jr'd|j }tj|j|}td| d | j	
| d S d S d S )Nr  zSyncing checkpoint in z to Flyte. This may take time.)r  rX  re  r!   ri   rj   r  r$   r   r  r+  r  r)   r)   r*   r  :  s   zFlyteCallback.on_savec           	      K   sL   | j r$dd l}ddlm} ddlm} ||j}|d| | d S d S )Nr   )Deck)TableRendererzLog History)	r  pandasrO   r  flytekitplugins.deck.rendererr  	DataFramelog_historyto_html)	rQ  rk   r(  r\  r   pdr  r  log_history_dfr)   r)   r*   rl  B  s   zFlyteCallback.on_train_end)TT)	r   rm  rn  ro  r@  rS  r  rl  r.  r)   r)   r  r*   r    s
    r  c                   @   sh   e Zd ZdZ		ddee deeed ef  fddZ	dd	 Z
dd
dZdddZdd Zdd ZdS )DVCLiveCallbackaW  
    A [`TrainerCallback`] that sends the logs to [DVCLive](https://www.dvc.org/doc/dvclive).

    Use the environment variables below in `setup` to configure the integration. To customize this callback beyond
    those environment variables, see [here](https://dvc.org/doc/dvclive/ml-frameworks/huggingface).

    Args:
        live (`dvclive.Live`, *optional*, defaults to `None`):
            Optional Live instance. If None, a new instance will be created using **kwargs.
        log_model (Union[Literal["all"], bool], *optional*, defaults to `None`):
            Whether to use `dvclive.Live.log_artifact()` to log checkpoints created by [`Trainer`]. If set to `True`,
            the final checkpoint is logged at the end of training. If set to `"all"`, the entire
            [`TrainingArguments`]'s `output_dir` is logged at each checkpoint.
    Nliver  r  c                 K   sd   t  stdddlm} || _d| _d | _t||r#|| _d| _d S |d ur0td|j dd S )NzLDVCLiveCallback requires dvclive to be installed. Run `pip install dvclive`.r   LiveFTzFound class z  for live, expected dvclive.Live)	rS   rX   rR   r  rw  rt  r  rT   rY   )rQ  r  r  r   r  r)   r)   r*   rS  \  s   

zDVCLiveCallback.__init__c                 C   sx   ddl m} d| _| jdur&td}| tv rd| _n	| dkr&d| _|j	r:| j
s0| | _
| j
|  dS dS )a@  
        Setup the optional DVCLive integration. To customize this callback beyond the environment variables below, see
        [here](https://dvc.org/doc/dvclive/ml-frameworks/huggingface).

        Environment:
        - **HF_DVCLIVE_LOG_MODEL** (`str`, *optional*):
            Whether to use `dvclive.Live.log_artifact()` to log checkpoints created by [`Trainer`]. If set to `True` or
            *1*, the final checkpoint is logged at the end of training. If set to `all`, the entire
            [`TrainingArguments`]'s `output_dir` is logged at each checkpoint.
        r   r  TNHF_DVCLIVE_LOG_MODELr  )rR   r  _initalizedrw  r!   r"   r#   r   rx  rX  r  r  r  )rQ  rk   r(  r   r  log_model_envr)   r)   r*   r  p  s   

zDVCLiveCallback.setupc                 K   r  r2   r  r  r)   r)   r*   r^    r  zDVCLiveCallback.on_train_beginc              
   K   s   | j s
| ||| |jrJddlm} ddlm} | D ]%\}	}
||
r1| j	
||	d|
 qtd|
 dt|
 d|	 d q| j	  d S d S )	Nr   )Metric)standardize_metric_namezdvclive.huggingfacer_  r`  ra  zh" as a scalar. This invocation of DVCLive's Live.log_metric() is incorrect so we dropped this attribute.)rt  r  rX  dvclive.plotsr  dvclive.utilsr  r   	could_logr  r  r$   r%   r   	next_step)rQ  rk   r(  r\  r   rh  r   r  r  r   r   r)   r)   r*   ri    s&   
zDVCLiveCallback.on_logc                 K   s4   | j dkr| jr|jr| j|j d S d S d S d S )Nr  )rw  rt  rX  r  r  r  rk  r)   r)   r*   r    s   zDVCLiveCallback.on_savec           	      K   s   | j rC|jrEddlm} | jdu r<|||d|dd}|jr#dnd}tj	|j
|}|| | jj||ddd	 | j  d S d S d S )
Nr   r  Tr   r  r  r   r:  )r   r   copy)rt  rX  transformers.trainerr  rw  r   r  r!   ri   rj   r  r  r  r  rq  )	rQ  rk   r(  r\  r   r  r  r   r  r)   r)   r*   rl    s   

zDVCLiveCallback.on_train_endr  r2   )r   rm  rn  ro  r   r   r	   r   r@  rS  r  r^  ri  r  rl  r)   r)   r)   r*   r  L  s    


r  )r>  r   rG   r   r6   r    rM   r-   rI   flyterR   c                 C   s>   | D ]}|t vrt| ddt   dqdd | D S )Nz is not supported, only r  z are supported.c                 S   s   g | ]}t | qS r)   )INTEGRATION_TO_CALLBACK)r   integrationr)   r)   r*   r     s    z7get_reporting_integration_callbacks.<locals>.<listcomp>)r  r<  rj   r,  )r7  r  r)   r)   r*   #get_reporting_integration_callbacks  s   r  )jro  r   importlib.metadatar&   importlib.utilr  r  r!   rs   rr  r   r   dataclassesr   pathlibr   typingr   r   r   r   r   r	   numpyr  r   r   r	  r  r   r   r   r   r   r  r   r$   rp   r'   r(   r"   r#   r3   r   r   r   
get_configr%   r3  r<  rK   r  _neptune_versionr   PackageNotFoundErrortrainer_callbackr   r   r   r   r   r   training_argsr   r   r   r+   r/   r4   r8   r:   r<   r>   rA   rC   rH   rJ   rL   rN   rP   rQ   rS   r[   rb  r   r   r   r  r=  r@  r+  r   r6  r  r  r  r  r  r/  r0  r  r  r  r  r  r  r)   r)   r)   r*   <module>   s    
$
9 bEV )M 1  y?f