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
mZmZmZmZ ddlZddlmZmZmZmZmZmZmZmZmZmZmZmZmZ e r[ddlZdd Z dUd	e!d
e"fddZ#dUd	e!de"fddZ$dd Z%G dd dZ&G dd deZ'G dd deZ(G dd deZ)dZ*e+de* d Z,dd Z-G dd  d eZ.G d!d" d"eZ/G d#d$ d$eZ0G d%d& d&eZ1G d'd( d(eZ2d)e3e4e5f d*e5fd+d,Z6d*e3e4e5f fd-d.Z7d*e3e4e5f fd/d0Z8d1d2 Z9d*e3e4e5f fd3d4Z:G d5d6 d6eZ;d7d8 Z<d9d: Z=dVd;d<Z>G d=d> d>eZ?G d?d@ d@Z@dAdB ZAdCdD ZBdEdF ZC	dWdHeDdIe!dJe"fdKdLZEG dMdN dNeZFG dOdP dPZGdUdQe4dRe"fdSdTZHdS )Xz6
PyTorch-independent utilities for the Trainer class.
    N)Any
NamedTupleOptionalUnion   )ExplicitEnumis_psutil_availableis_tf_availableis_torch_availableis_torch_cuda_availableis_torch_hpu_availableis_torch_mlu_availableis_torch_mps_availableis_torch_musa_availableis_torch_npu_availableis_torch_xla_availableis_torch_xpu_availablerequires_backendsc                 C   s   t  d }t| dS )zN
    Helper function to set worker seed during Dataloader initialization.
    l        N)torchinitial_seedset_seed)_worker_seed r   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/trainer_utils.pyseed_worker4   s   r   Fseed	warn_onlyc                 C   s   t |  t r1dtjd< dtjd< dtjd< dtjd< dtjd< tjd|d	 dtjj_d
tjj_	t
 r@ddl}|jj  dS dS )a	  
    Helper function for reproducible behavior during distributed training. See
    - https://pytorch.org/docs/stable/notes/randomness.html for pytorch
    - https://www.tensorflow.org/api_docs/python/tf/config/experimental/enable_op_determinism for tensorflow
    1CUDA_LAUNCH_BLOCKINGz:16:8CUBLAS_WORKSPACE_CONFIGASCEND_LAUNCH_BLOCKINGHCCL_DETERMINISTICFLASH_ATTENTION_DETERMINISTICT)r   Fr   N)r   r
   osenvironr   use_deterministic_algorithmsbackendscudnndeterministic	benchmarkr	   
tensorflowconfigexperimentalenable_op_determinism)r   r   tfr   r   r   enable_full_determinism<   s   






r0   r)   c                 C   s   t |  tj |  t r t|  tj|  |r td t	 r)tj
|  t r2tj|  t r;tj|  t rDtj|  t rMtj|  t rdddl}|j |  |rf|jj  dS dS dS )aY  
    Helper function for reproducible behavior to set the seed in `random`, `numpy`, `torch` and/or `tf` (if installed).

    Args:
        seed (`int`):
            The seed to set.
        deterministic (`bool`, *optional*, defaults to `False`):
            Whether to use deterministic algorithms where available. Can slow down training.
    Tr   N)randomr   npr
   r   manual_seedcudamanual_seed_allr&   r   mlur   musar   npur   hpur   xpur	   r+   r   r,   r-   r.   )r   r)   r/   r   r   r   r   \   s0   



r   c                 C   sL   | j r$t|d|d }| jt| }|t|| | }|S )aL  
    Implements the NEFTune forward pass for the model using forward hooks. Note this works only for torch.nn.Embedding
    layers. This method is slightly adapted from the original source code that can be found here:
    https://github.com/neelsjain/NEFTune Simply add it to your model as follows:
    ```python
    model = ...
    model.embed_tokens.neftune_noise_alpha = 0.1
    model.embed_tokens.register_forward_hook(neftune_post_forward_hook)
    ```
    Args:
        module (`torch.nn.Module`):
            The embedding module where the hook is attached. Note that you need to set `module.neftune_noise_alpha` to
            the desired noise alpha value.
        input (`torch.Tensor`):
            The input tensor to the model.
        output (`torch.Tensor`):
            The output tensor of the model (i.e. the embeddings).
    r      )trainingr   tensorsizeneftune_noise_alphasqrt
zeros_likeuniform_)moduleinputoutputdimsmag_normr   r   r   neftune_post_forward_hook   s
   rH   c                   @   s   e Zd ZdZ		ddeejeej f deejeej f deeejeej f  deeejeej f  fddZ	d	d
 Z
dd ZdS )EvalPredictionaz  
    Evaluation output (always contains labels), to be used to compute metrics.

    Parameters:
        predictions (`np.ndarray`): Predictions of the model.
        label_ids (`np.ndarray`): Targets to be matched.
        inputs (`np.ndarray`, *optional*): Input data passed to the model.
        losses (`np.ndarray`, *optional*): Loss values computed during evaluation.
    Npredictions	label_idsinputslossesc                 C   sf   || _ || _|| _|| _| j | jf| _| jd ur!|  j| jf7  _| jd ur1|  j| jf7  _d S d S N)rJ   rK   rL   rM   elements)selfrJ   rK   rL   rM   r   r   r   __init__   s   

zEvalPrediction.__init__c                 C   s
   t | jS rN   )iterrO   rP   r   r   r   __iter__   s   
zEvalPrediction.__iter__c                 C   s(   |dk s|t | jkrtd| j| S )Nr   ztuple index out of range)lenrO   
IndexError)rP   idxr   r   r   __getitem__   s   
zEvalPrediction.__getitem__)NN)__name__
__module____qualname____doc__r   r2   ndarraytupler   rQ   rT   rX   r   r   r   r   rI      s    
rI   c                   @   sb   e Zd ZU eejeej f ed< eeejeej f  ed< ee	e
ef  ed< ee ed< dS )EvalLoopOutputrJ   rK   metricsnum_samplesN)rY   rZ   r[   r   r2   r]   r^   __annotations__r   dictstrfloatintr   r   r   r   r_      s
   
 r_   c                   @   sV   e Zd ZU eejeej f ed< eeejeej f  ed< ee	e
ef  ed< dS )PredictionOutputrJ   rK   r`   N)rY   rZ   r[   r   r2   r]   r^   rb   r   rc   rd   re   r   r   r   r   rg      s   
 rg   c                   @   s.   e Zd ZU eed< eed< eeef ed< dS )TrainOutputglobal_steptraining_lossr`   N)rY   rZ   r[   rf   rb   re   rc   rd   r   r   r   r   rh      s   
 rh   
checkpoint^z\-(\d+)$c                    sF   t  } fdd|D }t|dkrd S t j t|dd dS )Nc                    s4   g | ]}t |d urtjtj |r|qS rN   )_re_checkpointsearchr$   pathisdirjoin).0ro   folderr   r   
<listcomp>   s
    $z'get_last_checkpoint.<locals>.<listcomp>r   c                 S   s   t t|  d S )Nr   )rf   rm   rn   groups)xr   r   r   <lambda>       z%get_last_checkpoint.<locals>.<lambda>key)r$   listdirrU   ro   rq   max)rt   contentcheckpointsr   rs   r   get_last_checkpoint   s   

r   c                   @      e Zd ZdZdZdZdS )IntervalStrategynostepsepochNrY   rZ   r[   NOSTEPSEPOCHr   r   r   r   r          r   c                   @      e Zd ZdZdZdZdZdS )SaveStrategyr   r   r   bestN)rY   rZ   r[   r   r   r   BESTr   r   r   r   r      
    r   c                   @   r   )EvaluationStrategyr   r   r   Nr   r   r   r   r   r      r   r   c                   @   r   )HubStrategyend
every_saverk   all_checkpointsN)rY   rZ   r[   END
EVERY_SAVE
CHECKPOINTALL_CHECKPOINTSr   r   r   r   r      r   r   c                   @   sN   e Zd ZU dZeed< eeee f ed< e	ee
f ed< dZee
 ed< dS )BestRunac  
    The best run found by a hyperparameter search (see [`~Trainer.hyperparameter_search`]).

    Parameters:
        run_id (`str`):
            The id of the best run (if models were saved, the corresponding checkpoint will be in the folder ending
            with run-{run_id}).
        objective (`float`):
            The objective that was obtained for this run.
        hyperparameters (`Dict[str, Any]`):
            The hyperparameters picked to get this run.
        run_summary (`Optional[Any]`):
            A summary of tuning experiments. `ray.tune.ExperimentAnalysis` object for Ray backend.
    run_id	objectivehyperparametersNrun_summary)rY   rZ   r[   r\   rd   rb   r   re   listrc   r   r   r   r   r   r   r   r      s   
 r   r`   returnc                 C   sf   t | } | dd}| dd}dd |  D }|D ]}| |d}qt| dkr-|S t|  S )aj  
    The default objective to maximize/minimize when doing an hyperparameter search. It is the evaluation loss if no
    metrics are provided to the [`Trainer`], the sum of all metrics otherwise.

    Args:
        metrics (`Dict[str, float]`): The metrics returned by the evaluate method.

    Return:
        `float`: The objective to minimize or maximize
    	eval_lossNr   c                 S   s.   g | ]}| d s| ds| dr|qS )_runtime_per_second_compilation_time)endswithrr   mr   r   r   ru   !  s    z-default_compute_objective.<locals>.<listcomp>r   )copydeepcopypopkeysrU   sumvalues)r`   lossr   speed_metricssmr   r   r   default_compute_objective  s   
r   c                 C   sV   ddl m} | sJ d| jddddd| d	dd
| ddd| dg ddS )Nr   )is_optuna_availablez:This function needs Optuna installed: `pip install optuna`learning_rateư>-C6?T)lognum_train_epochs   r   (   per_device_train_batch_size             @   r   r   r   r   )integrationsr   suggest_floatsuggest_intsuggest_categorical)trialr   r   r   r   default_hp_space_optuna+  s   r   c                 C   s^   ddl m} | sJ dddlm} |dd|ttdd|dd	|g d
dS )Nr   )is_ray_tune_availablez:This function needs ray installed: `pip install ray[tune]`r   )tuner   r      r   r   r   )	r   r   rayr   
loguniformchoicer   rangeuniform)r   r   r   r   r   r   default_hp_space_ray7  s   

r   c                 C   sD   ddddddddd	dd
ddddddddg ddddgS )Nr   r   )minr}   r   doubler   )boundsnametypetransformationr   r   r   rf   )r   r   r   r   r   )48163264r   categorical)categorical_valuesr   r   r   )r   r   r   r   default_hp_space_sigoptE  s   r   c                 C   sV   ddl m} | stddddddd	d
ddddddddddg diddS )Nr   )is_wandb_availablez8This function needs wandb installed: `pip install wandb`r1   r   minimize)r   goalr   r   r   )distributionr   r}   int_uniformr   r   r   r   r   )methodmetric
parameters)r   r   ImportError)r   r   r   r   r   default_hp_space_wandbR  s   



r   c                   @   r   )HPSearchBackendoptunar   sigoptwandbN)rY   rZ   r[   OPTUNARAYSIGOPTWANDBr   r   r   r   r   d  r   r   c                 C   s,   t  rddlm  m} | dkS | dv S )z
    Whether or not the current process is the local process, based on `xm.get_ordinal()` (for TPUs) first, then on
    `local_rank`.
    r   N)r   )r   torch_xla.core.xla_modelcore	xla_modelget_ordinal)
local_rankxmr   r   r   is_main_processk  s   r   c                 C   sD   t  rddlm  m} | S | dkr t r ddl}|j S dS )zg
    Return the number of processes launched in parallel. Works with `torch.distributed` and TPUs.
    r   Nr   r   )	r   r   r   r   xrt_world_sizer
   r   distributedget_world_size)r   r   r   r   r   r   total_processes_numberw  s   
r   c           
      C   s   t   | }|  dt|di}|dkr|S |dur(|| }t|d||  d< |dur:|| }t|d||  d< |durL|| }	t|	d||  d< |S )	a  
    Measure and return speed performance metrics.

    This function requires a time snapshot `start_time` before the operation to be measured starts and this function
    should be run immediately after the operation to be measured has completed.

    Args:
    - split: name to prefix metric (like train, eval, test...)
    - start_time: operation start time
    - num_samples: number of samples processed
    - num_steps: number of steps processed
    - num_tokens: number of tokens processed
    r   r   r   N   _samples_per_second_steps_per_second_tokens_per_second)timeround)
split
start_timera   	num_steps
num_tokensruntimeresultsamples_per_secondsteps_per_secondtokens_per_secondr   r   r   r     s   r   c                   @   s8   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdS )SchedulerTypeaL  
    Scheduler names for the parameter `lr_scheduler_type` in [`TrainingArguments`].
    By default, it uses "linear". Internally, this retrieves `get_linear_schedule_with_warmup` scheduler from [`Trainer`].
    Scheduler types:
       - "linear" = get_linear_schedule_with_warmup
       - "cosine" = get_cosine_schedule_with_warmup
       - "cosine_with_restarts" = get_cosine_with_hard_restarts_schedule_with_warmup
       - "polynomial" = get_polynomial_decay_schedule_with_warmup
       - "constant" =  get_constant_schedule
       - "constant_with_warmup" = get_constant_schedule_with_warmup
       - "inverse_sqrt" = get_inverse_sqrt_schedule
       - "reduce_lr_on_plateau" = get_reduce_on_plateau_schedule
       - "cosine_with_min_lr" = get_cosine_with_min_lr_schedule_with_warmup
       - "warmup_stable_decay" = get_wsd_schedule
    linearcosinecosine_with_restarts
polynomialconstantconstant_with_warmupinverse_sqrtreduce_lr_on_plateaucosine_with_min_lrwarmup_stable_decayN)rY   rZ   r[   r\   LINEARCOSINECOSINE_WITH_RESTARTS
POLYNOMIALCONSTANTCONSTANT_WITH_WARMUPINVERSE_SQRTREDUCE_ON_PLATEAUCOSINE_WITH_MIN_LRWARMUP_STABLE_DECAYr   r   r   r   r    s    r  c                   @   sd   e Zd ZdZddddddZddd	Zd
d Zdd Zdd Zdd Z	dd Z
dd ZdddZdS )TrainerMemoryTrackera  
    A helper class that tracks cpu and gpu memory.

    This class will silently skip unless `psutil` is available. Install with `pip install psutil`.

    When a stage completes, it can pass metrics dict to update with the memory metrics gathered during this stage.

    Example :

    ```python
    self._memory_tracker = TrainerMemoryTracker(self.args.skip_memory_metrics)
    self._memory_tracker.start()
    # code ...
    metrics = {"train_runtime": 10.5}
    self._memory_tracker.stop_and_update_metrics(metrics)
    ```

    At the moment GPU tracking is only for `pytorch`, but can be extended to support `tensorflow`.

    To understand this class' intricacies please read the documentation of [`~Trainer.log_metrics`].
    inittrainevaltest)rQ   r"  _inner_training_loopevaluatepredictFc                 C   s   || _ t s	d| _ | j rd S dd l}t st st r&dd l}|| _i | _n;t r4dd l}|| _i | _n-t	 rBdd l}|| _i | _nt
 rPdd l}|| _i | _nt r^dd l}|| _i | _nd | _| | _d | _i | _d| _d S )NTr   F)skip_memory_metricsr   psutilr   r   r   r   gpur   r   r   r   Processprocess	cur_stagecpuinit_reported)rP   r(  r)  r   r   r   r   rQ     s>   

zTrainerMemoryTracker.__init__c                 C   s>   t  jjjj}|| jv r| j| S td| d| j  )z+derives the stage/caller name automaticallyzwas called from z+, but only expect to be called from one of )inspectcurrentframef_backf_codeco_namestages
ValueErrorr   )rP   callerr   r   r   derive_stage  s   

z!TrainerMemoryTracker.derive_stagec                 C   s   | j  jS )z4get resident set size memory for the current process)r,  memory_inforssrS   r   r   r   cpu_mem_used  s   z!TrainerMemoryTracker.cpu_mem_usedc                 C   s&   d| _ 	 t|  | j | _ | jsd S q)Nr   )cpu_mem_used_peakr}   r;  peak_monitoringrS   r   r   r   peak_monitor_func  s   z&TrainerMemoryTracker.peak_monitor_funcc                 C   s  | j rdS |  }| jdur| j|krdS || _t  | jdurtj r3| jj  | jj	  nSt
 rC| jj  | jj	  nCt rS| jj  | jj	  n3t rc| jj  | jj	  n#t rs| jj  | jj	  nt r}| jj  n	t r| jj	  | jdurtj r| jj | _nAt
 r| jj | _n6t r| jj | _n+t r| jj | _n t r| jj | _nt r| jj | _n
t r| jj | _|  | _d| _tj| jd}d|_|   dS )z%start tracking for the caller's stageNT)target)!r(  r8  r-  gccollectr   r4   is_availablereset_peak_memory_statsempty_cacher   r6   r   r7   r   r:   r   r8   r   r9   r   mpsmemory_allocatedgpu_mem_used_at_startcurrent_allocated_memoryr;  cpu_mem_used_at_startr=  	threadingThreadr>  daemonstart)rP   stagepeak_monitor_threadr   r   r   rM  )  s^   




zTrainerMemoryTracker.startc                 C   s^  | j dur| j |krdS d| _t  | jdurYtj r$| jj  n5t r.| jj	  n+t
 r8| jj  n!t rB| jj  nt rL| jj  nt rPn	t rY| jj  | jdurtj rs| jj | _| jj | _nlt r| jj	 | _| jj	 | _nZt
 r| jj | _| jj | _nHt r| jj | _| jj | _n6t r| jj | _| jj | _n$t r| jj | _| jj | _nt r| jj | _d| _ntd| j| j| j| j d| j| j < | jdurtd| j| j | j| j  d< nd| j| j  d< |  | _| j| j| j| j td| j | j d| j!| j < d| _ dS )	z"stop tracking for the passed stageNFzNo available GPU device found!)beginr   allocr   peakedzNot available)rP  r   rQ  rR  )"r-  r=  r@  rA  r   r4   rB  rD  r   r6   r   r7   r   r:   r   r8   r   r   rE  rF  gpu_mem_used_nowmax_memory_allocatedgpu_mem_used_peakr9   rH  r6  rG  r*  r}   r;  cpu_mem_used_nowrI  r<  r.  )rP   rN  r   r   r   stopg  sp   



 


zTrainerMemoryTracker.stopc                 C   s  | j rdS | jdur| j|krdS |g}| js |dd d| _|D ]B}dD ]=}|| jv rC|| j| v rC| j| | || d| d< | jdurc|| jv rc|| j| v rc| j| | || d| d< q&q"|d dkr| jd d	 |d
< | jdur| jd d	 |d< dS dS dS )zupdates the metricsNr   r!  T)rQ  rR  	_mem_cpu__delta	_mem_gpu_rP  before_init_mem_cpubefore_init_mem_gpu)r(  r-  r/  insertr.  r   r*  )rP   rN  r`   r5  tr   r   r   update_metrics  s,   "
z#TrainerMemoryTracker.update_metricsNc                 C   s8   | j rdS |  }| | |dur| || dS dS )z<combine stop and metrics update in one call for simpler codeN)r(  r8  rW  r_  )rP   r`   rN  r   r   r   stop_and_update_metrics  s   
z,TrainerMemoryTracker.stop_and_update_metricsFrN   )rY   rZ   r[   r\   r5  rQ   r8  r;  r>  rM  rW  r_  r`  r   r   r   r   r     s     
.
>V)r   c                 C   s6   zt | duW S  ty   Y dS  ty   Y dS w )zR
    Checks if the dataset implements __len__() and it doesn't raise an error
    NF)rU   	TypeErrorAttributeError)datasetr   r   r   
has_length  s   re  c                 C   s   t | ttfrt| dd | D S t | tr$t| dd |  D S t | tjr.|  S t	 rAt | t
jrA|  dkrA|  S | S )zM
    Recursively calls `.item()` on the element of the dictionary passed
    c                 s   s    | ]}t |V  qd S rN   denumpify_detensorizer   r   r   r   	<genexpr>      z(denumpify_detensorize.<locals>.<genexpr>c                 S   s   i | ]	\}}|t |qS r   rf  rr   kvr   r   r   
<dictcomp>  s    z)denumpify_detensorize.<locals>.<dictcomp>r   )
isinstancer   r^   r   rc   itemsr2   genericitemr
   r   Tensornumel)r`   r   r   r   rg    s   
rg  c                 C   sF   t | tjrtt| jj}|t| j t| j	 S tt| jS )za
    Return the number of arguments of the passed function, even if it's a partial function.
    )
rn  	functoolspartialrU   r0  	signaturefuncr   argskeywords)rw  
total_argsr   r   r   number_of_arguments  s   r{     functionstarting_batch_sizeauto_find_batch_sizec                 C   sL   | du rt jt||dS |rttd ddlm} || |dS t j| |dS )a%  
    Args:
    A basic decorator that will try to execute `function`. If it fails from exceptions related to out-of-memory or
    CUDNN, the batch size is cut in half and passed to `function`. `function` must take in a `batch_size` parameter as
    its first argument.
        function (`callable`, *optional*)
            A function to wrap
        starting_batch_size (`int`, *optional*)
            The batch size to try and fit into memory
        auto_find_batch_size (`bool`, *optional*)
            If False, will just execute `function`
    N)r~  r  
accelerater   )find_executable_batch_size)r}  r~  )
batch_size)rt  ru  r  r   accelerate.utils)r}  r~  r  %accelerate_find_executable_batch_sizer   r   r   r    s   
r  c                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	
FSDPOption
full_shardshard_grad_opno_shardhybrid_shardhybrid_shard_zero2offload	auto_wrapN)
rY   rZ   r[   
FULL_SHARDSHARD_GRAD_OPNO_SHARDHYBRID_SHARDHYBRID_SHARD_ZERO2OFFLOAD	AUTO_WRAPr   r   r   r   r  9  s    r  c                   @   sV   e Zd ZdZ			ddee dee fddZdedefd	d
Zde	e fddZ
dS )RemoveColumnsCollatorzWWrap the data collator to remove unused columns before they are passed to the collator.N
model_namedescriptionc                 C   s(   || _ || _|| _|| _|| _d| _d S )NF)data_collatorsignature_columnsloggerr  r  message_logged)rP   r  r  r  r  r  r   r   r   rQ   F  s   
zRemoveColumnsCollator.__init__featurer   c                    s   t |ts|S  jsR jrR jrRtt| t j }t	|dkrR j
d u r*dnd j
 d} jd| d j dd| d	d| d
 j d d _ fdd| D S )Nr    zin the z setzThe following columns z) don't have a corresponding argument in `z!.forward` and have been ignored: z, z. If z are not expected by `z/.forward`,  you can safely ignore this message.Tc                    s    i | ]\}}| j v r||qS r   )r  rj  rS   r   r   rm  c  s     z9RemoveColumnsCollator._remove_columns.<locals>.<dictcomp>)rn  rc   r  r  r  r   setr   r  rU   r  inforq   ro  )rP   r  ignored_columnsdset_descriptionr   rS   r   _remove_columnsU  s&   
z%RemoveColumnsCollator._remove_columnsfeaturesc                    s    fdd|D }  |S )Nc                    s   g | ]}  |qS r   )r  )rr   r  rS   r   r   ru   f  ry   z2RemoveColumnsCollator.__call__.<locals>.<listcomp>)r  )rP   r  r   rS   r   __call__e  s   
zRemoveColumnsCollator.__call__NNN)rY   rZ   r[   r\   r   rd   rQ   rc   r  r   r  r   r   r   r   r  C  s    
r  r{   return_is_regexc                    s   d}d}t | trtt|  }|  ksdnd}n$ | v r!d}nt fdd| D r/d}nt fdd| D r>d}d}|rD||fS |S )aK  A helper method to check if the passed module's key name matches any of the target modules in the optim_target_modules.

    Args:
        optim_target_modules (`Union[str, List[str]]`):
            A list of strings to try to match. Can be also a full string.
        key (`str`):
            A key to search any matches in optim_target_modules
        return_is_regex (`bool`):
            If set to `True`, the method will return whether the passed `optim_target_modules`
            is a regex or not.

    Returns:
        `bool` : True of match object if key matches any target modules from config, False or
        None if no match found
        `bool` : If the matched target module is a regex to silence out the warnings in Trainer
        for extra modules being found (only if `target_module_found=True` for an array of regex).
    FTc                 3   s    | ]}| v V  qd S rN   r   )rr   
target_keyrz   r   r   rh    ri  z-check_target_module_exists.<locals>.<genexpr>c                 3   s     | ]}t t| V  qd S rN   )boolre	fullmatch)rr   optim_target_modulerz   r   r   rh    s    )rn  rd   r  r  r  any)optim_target_modulesr{   r  target_module_foundis_regexr   rz   r   check_target_module_existsj  s   
r  ra  r  )Nr|  F)Ir\   r   rt  r@  r0  r$   r1   r  rJ  r   typingr   r   r   r   numpyr2   utilsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   rf   r  r0   r   rH   rI   r_   rg   rh   PREFIX_CHECKPOINT_DIRcompilerm   r   r   r   r   r   r   rc   rd   re   r   r   r   r   r   r   r   r   r   r  r   re  rg  r{  callabler  r  r  r  r   r   r   r   <module>   st   < $%
  4

'