U
    &c                     @   s   d dl Z d dlZd dlZd dl mZmZ d dlmZmZmZm	Z	 ddl
mZmZmZ e rbd dlZzd dlm  mZ dZW n ek
r   dZY nX edd	 ZeeZeG d
d dZdS )    N)	dataclassfield)AnyDictOptionalTuple   )cached_propertyis_torch_availabletorch_requiredTFc                   C   s   t S )N)_has_tpu r   r   >/tmp/pip-unpacked-wheel-ymerj3tt/transformers/training_args.pyis_tpu_available   s    r   c                   @   sB  e Zd ZU dZeddidZeed< edddidZe	ed	< eddd
idZ
e	ed< edddidZe	ed< edddidZe	ed< edddidZe	ed< edddidZeed< edddidZeed< edddidZeed< edddidZeed< edddidZeed< ed dd!idZeed"< ed#dd$idZeed%< ed&dd'idZeed(< ed)dd*idZeed+< ed,dd-idZeed.< ed/dd0idZee ed1< eddd2idZe	ed3< ed4dd5idZeed6< ed4dd7idZeed8< ed/dd9idZee ed:< eddd;idZ e	ed<< ed=dd>idZ!eed?< eddd@idZ"e	edA< edBddCidZ#eedD< ed)ddEidZ$eedF< ed/ddGidZ%ee edH< edddIidZ&e	edJ< e'edKdLdMZ(e'edKdNdOZ)e*e+e,dPef dKdQdRZ-e'e+dPdKdSdTZ.e'e+dUdV Z/dWdX Z0e1ee2f dKdYdZZ3d/S )[TrainingArgumentsa  
    TrainingArguments is the subset of the arguments we use in our example scripts
    **which relate to the training loop itself**.

    Using `HfArgumentParser` we can turn this class
    into argparse arguments to be able to specify them on
    the command line.
    helpzQThe output directory where the model predictions and checkpoints will be written.)metadata
output_dirFz{Overwrite the content of the output directory.Use this to continue training if output_dir points to a checkpoint directory.)defaultr   overwrite_output_dirzWhether to run training.do_trainz#Whether to run eval on the dev set.do_evalz+Whether to run predictions on the test set.
do_predictz4Run evaluation during training at each logging step.evaluate_during_training   z$Batch size per GPU/CPU for training.per_gpu_train_batch_sizez&Batch size per GPU/CPU for evaluation.per_gpu_eval_batch_sizer   zONumber of updates steps to accumulate before performing a backward/update pass.gradient_accumulation_stepsg-C6
?z#The initial learning rate for Adam.learning_rateg        zWeight decay if we apply some.weight_decayg:0yE>zEpsilon for Adam optimizer.adam_epsilong      ?zMax gradient norm.max_grad_normg      @z+Total number of training epochs to perform.num_train_epochszQIf > 0: set total number of training steps to perform. Override num_train_epochs.	max_stepsr   z Linear warmup over warmup_steps.warmup_stepsNzTensorboard log dir.logging_dirz"Log and eval the first global_steplogging_first_stepi  zLog every X updates steps.logging_stepsz&Save checkpoint every X updates steps.
save_stepszwLimit the total amount of checkpoints.Deletes the older checkpoints in the output_dir. Default is unlimited checkpointssave_total_limitz)Do not use CUDA even when it is availableno_cuda*   zrandom seed for initializationseedzOWhether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bitfp16ZO1zFor fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3'].See details at https://nvidia.github.io/apex/amp.htmlfp16_opt_levelz$For distributed training: local_rank
local_rankzBTPU: Number of TPU cores (automatically passed by launcher script)tpu_num_coresz#TPU: Whether to print debug metricstpu_metrics_debug)returnc                 C   s   | j td| j S Nr   )r   maxn_gpuselfr   r   r   train_batch_sizet   s    z"TrainingArguments.train_batch_sizec                 C   s   | j td| j S r4   )r   r5   r6   r7   r   r   r   eval_batch_sizex   s    z!TrainingArguments.eval_batch_sizeztorch.devicec                 C   s   t d | jr td}d}nbt r4t }d}nN| jdkrbttj	
 rPdnd}tj	 }n tjjdd td| j}d}||fS )	NzPyTorch: setting up devicescpur   r#   cudaZnccl)backendr   )loggerinfor+   torchdevicer   xmZ
xla_devicer0   r<   Zis_availableZdevice_countZdistributedZinit_process_group)r8   rA   r6   r   r   r   _setup_devices|   s    


z TrainingArguments._setup_devicesc                 C   s
   | j d S )Nr   rC   r7   r   r   r   rA      s    zTrainingArguments.devicec                 C   s
   | j d S r4   rD   r7   r   r   r   r6      s    zTrainingArguments.n_gpuc                 C   s   t jt| ddS )z<
        Serializes this instance to a JSON string.
           )indent)jsondumpsdataclassesasdictr7   r   r   r   to_json_string   s    z TrainingArguments.to_json_stringc                    s>   t | }ttttg t r( tj	  fdd|
 D S )uM   
        Sanitized serialization to use with TensorBoard’s hparams
        c                    s*   i | ]"\}}|t | kr|nt|qS r   )typestr).0kvZvalid_typesr   r   
<dictcomp>   s      z7TrainingArguments.to_sanitized_dict.<locals>.<dictcomp>)rI   rJ   boolintfloatrM   r
   appendr@   ZTensoritems)r8   dr   rQ   r   to_sanitized_dict   s
    
z#TrainingArguments.to_sanitized_dict)4__name__
__module____qualname____doc__r   r   rM   __annotations__r   rS   r   r   r   r   r   rT   r   r   r   rU   r   r    r!   r"   r$   r%   r&   r   r'   r(   r)   r*   r+   r-   r.   r/   r0   r1   r2   propertyr9   r:   r	   r   r   rC   rA   r6   rK   r   r   rY   r   r   r   r   r      s   
	
 		 r   )rI   rG   loggingr   r   typingr   r   r   r   Z
file_utilsr	   r
   r   r@   Ztorch_xla.core.xla_modelcoreZ	xla_modelrB   r   ImportErrorr   	getLoggerrZ   r>   r   r   r   r   r   <module>   s"   


