/usr/local/lib/python3.6/site-packages/transformers/__pycache__
NameSizeModeActions
activations.cpython-36.pyc77500644editdlrm
activations_tf.cpython-36.pyc41360644editdlrm
configuration_utils.cpython-36.pyc393970644editdlrm
convert_graph_to_onnx.cpython-36.pyc160360644editdlrm
convert_pytorch_checkpoint_to_tf2.cpython-36.pyc91760644editdlrm
convert_slow_tokenizer.cpython-36.pyc304510644editdlrm
convert_slow_tokenizers_checkpoints_to_fast.cpython-36.pyc29500644editdlrm
convert_tf_hub_seq_to_seq_bert_to_pytorch.cpython-36.pyc18310644editdlrm
debug_utils.cpython-36.pyc110980644editdlrm
deepspeed.cpython-36.pyc128870644editdlrm
dependency_versions_check.cpython-36.pyc9610644editdlrm
dependency_versions_table.cpython-36.pyc20390644editdlrm
dynamic_module_utils.cpython-36.pyc139500644editdlrm
feature_extraction_sequence_utils.cpython-36.pyc128480644editdlrm
feature_extraction_utils.cpython-36.pyc225680644editdlrm
file_utils.cpython-36.pyc38030644editdlrm
generation_beam_constraints.cpython-36.pyc165560644editdlrm
generation_beam_search.cpython-36.pyc254400644editdlrm
generation_flax_logits_process.cpython-36.pyc110800644editdlrm
generation_flax_utils.cpython-36.pyc223200644editdlrm
generation_logits_process.cpython-36.pyc283870644editdlrm
generation_stopping_criteria.cpython-36.pyc63520644editdlrm
generation_tf_logits_process.cpython-36.pyc165240644editdlrm
generation_tf_utils.cpython-36.pyc863740644editdlrm
generation_utils.cpython-36.pyc1135760644editdlrm
hf_argparser.cpython-36.pyc89970644editdlrm
image_utils.cpython-36.pyc97300644editdlrm
integrations.cpython-36.pyc314040644editdlrm
keras_callbacks.cpython-36.pyc145290644editdlrm
modelcard.cpython-36.pyc261650644editdlrm
modeling_flax_outputs.cpython-36.pyc348780644editdlrm
modeling_flax_pytorch_utils.cpython-36.pyc83510644editdlrm
modeling_flax_utils.cpython-36.pyc286020644editdlrm
modeling_outputs.cpython-36.pyc590830644editdlrm
modeling_tf_outputs.cpython-36.pyc457330644editdlrm
modeling_tf_pytorch_utils.cpython-36.pyc125920644editdlrm
modeling_tf_utils.cpython-36.pyc709060644editdlrm
modeling_utils.cpython-36.pyc983820644editdlrm
optimization.cpython-36.pyc226530644editdlrm
optimization_tf.cpython-36.pyc134080644editdlrm
processing_utils.cpython-36.pyc85680644editdlrm
pytorch_utils.cpython-36.pyc11180644editdlrm
testing_utils.cpython-36.pyc432770644editdlrm
tf_utils.cpython-36.pyc12510644editdlrm
tokenization_utils.cpython-36.pyc258450644editdlrm
tokenization_utils_base.cpython-36.pyc1292790644editdlrm
tokenization_utils_fast.cpython-36.pyc219790644editdlrm
trainer.cpython-36.pyc818400644editdlrm
trainer_callback.cpython-36.pyc229060644editdlrm
trainer_pt_utils.cpython-36.pyc398290644editdlrm
trainer_seq2seq.cpython-36.pyc80280644editdlrm
trainer_tf.cpython-36.pyc255360644editdlrm
trainer_utils.cpython-36.pyc155710644editdlrm
training_args.cpython-36.pyc546000644editdlrm
training_args_seq2seq.cpython-36.pyc23170644editdlrm
training_args_tf.cpython-36.pyc128690644editdlrm
__init__.cpython-36.pyc1094310644editdlrm
Edit: /usr/local/lib/python3.6/site-packages/transformers/__pycache__/optimization.cpython-36.pyc (22653B)
3 Egll @sdZddlZddlZddlmZmZmZmZmZddl Z ddl m Z ddl m Z ddl mZddlmZdd lmZdd lmZejeZd*e ed d d Zd,e eedddZd.ddZd0e eeeedddZd2e eeeedddZd4ddZej eej!eej"eej#eej$eej%eiZ&d5ee'efe eeeedddZ(Gd d!d!e Z)Gd"d#d#e Z*Gd$d%d%eZ+d6d'd(Z,dS)7z$PyTorch optimization for BERT model.N)CallableIterableOptionalTupleUnion)nn) Optimizer)LambdaLR) SchedulerType)logging)require_version) optimizer last_epochcCst|dd|dS)a Create a schedule with a constant learning rate, using the learning rate set in optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. cSsdS)Nr )_rrC/usr/local/lib/python3.6/site-packages/transformers/optimization.py/sz'get_constant_schedule..)r)r )rrrrrget_constant_schedule"s r)rnum_warmup_stepsrcs tdfdd }t|||dS)ad Create a schedule with a constant learning rate preceded by a warmup period during which the learning rate increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. ) current_stepcs"|krt|ttdSdS)Ng?)floatmax)r)rrr lr_lambdaCsz4get_constant_schedule_with_warmup..lr_lambda)r)intr )rrrrr)rr!get_constant_schedule_with_warmup2srcs tdfdd }t|||S)a Create a schedule with a learning rate that decreases linearly from the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. )rcsB|krt|ttdStdt|ttdS)Nr g)rr)r)num_training_stepsrrrr^sz2get_linear_schedule_with_warmup..lr_lambda)rr )rrrrrr)rrrget_linear_schedule_with_warmupKsr?)rrr num_cyclesrcsfdd}t|||S)a Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`float`, *optional*, defaults to 0.5): The number of waves in the cosine schedule (the defaults is to just decrease from the max value to 0 following a half-cosine). last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. csf|krt|ttdSt|ttd}tdddtjtjtd|S)Nr gg?g?g@)rrmathcospi)rprogress)rrrrrrsz2get_cosine_schedule_with_warmup..lr_lambda)r )rrrrrrr)rrrrget_cosine_schedule_with_warmuphsr$csfdd}t|||S)a Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, with several hard restarts, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`int`, *optional*, defaults to 1): The number of hard restarts to use. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. csr|krt|ttdSt|ttd}|dkrHdStdddtjtjt|dS)Nr g?gg?)rrr r!r")rr#)rrrrrrs zEget_cosine_with_hard_restarts_schedule_with_warmup..lr_lambda)r )rrrrrrr)rrrr2get_cosine_with_hard_restarts_schedule_with_warmupsr%Hz>?csN|jdks(tdddtdfdd }t|||S)a Create a schedule with a learning rate that decreases as a polynomial decay from the initial lr set in the optimizer to end lr defined by *lr_end*, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. lr_end (`float`, *optional*, defaults to 1e-7): The end LR. power (`float`, *optional*, defaults to 1.0): Power factor. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Note: *power* defaults to 1.0 as in the fairseq implementation, which in turn is based on the original BERT implementation at https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/optimization.py#L37 Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. lrzlr_end (z&) must be be smaller than initial lr ())rcsj|krt|ttdS|kr.S}}d||}||}|SdS)Nr )rr)rZlr_rangeZ decay_stepsZ pct_remainingZdecay)lr_endlr_initrrpowerrrrsz.lr_lambda)defaults ValueErrorrr )rrrr*r,rrr)r*r+rrr,r)get_polynomial_decay_schedule_with_warmups   r/)namerrrcCsrt|}t|}|tjkr"||S|dkr8t|d|tjkrN|||dS|dkrdt|d||||dS)a Unified API to get any scheduler from its name. Args: name (`str` or `SchedulerType`): The name of the scheduler to use. optimizer (`torch.optim.Optimizer`): The optimizer that will be used during training. num_warmup_steps (`int`, *optional*): The number of warmup steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it. num_training_steps (`int``, *optional*): The number of training steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it. Nz; requires `num_warmup_steps`, please provide that argument.)rz= requires `num_training_steps`, please provide that argument.)rr)r TYPE_TO_SCHEDULER_FUNCTIONCONSTANTr.CONSTANT_WITH_WARMUP)r0rrrZ schedule_funcrrr get_schedulers   r4c sTeZdZdZdeejjee eefeee e d fd d Z de d ddZ ZS)AdamWa5 Implements Adam algorithm with weight decay fix as introduced in [Decoupled Weight Decay Regularization](https://arxiv.org/abs/1711.05101). Parameters: params (`Iterable[nn.parameter.Parameter]`): Iterable of parameters to optimize or dictionaries defining parameter groups. lr (`float`, *optional*, defaults to 1e-3): The learning rate to use. betas (`Tuple[float,float]`, *optional*, defaults to (0.9, 0.999)): Adam's betas parameters (b1, b2). eps (`float`, *optional*, defaults to 1e-6): Adam's epsilon for numerical stability. weight_decay (`float`, *optional*, defaults to 0): Decoupled weight decay to apply. correct_bias (`bool`, *optional*, defaults to `True`): Whether or not to correct bias in Adam (for instance, in Bert TF repository they use `False`). no_deprecation_warning (`bool`, *optional*, defaults to `False`): A flag used to disable the deprecation warning (set to `True` to disable the warning). MbP??+?ư>TF)paramsr(betaseps weight_decay correct_biasno_deprecation_warningc s|stjdttd|dkr0td|dd|dkoFdkns`td|dd d|d kovdknstd|d d d|kstd |dt|||||d }tj||dS) NzThis implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warningz torch>=1.5.0gzInvalid learning rate: z - should be >= 0.0rg?zInvalid beta parameter: z - should be in [0.0, 1.0)r zInvalid epsilon value: )r(r<r=r>r?)warningswarn FutureWarningr r.dictsuper__init__) selfr;r(r<r=r>r?r@r-) __class__rrrF's zAdamW.__init__N)closurecCsd}|dk r|}x||jD]p}xh|dD]Z}|jdkr@q.|jj}|jrVtd|j|}t|dkrd|d<tj|j|d<tj|j|d<|d|d}}|d\} } |dd 7<|j | j |d | d |j | j ||d | d |j j |d } |d} |drJd | |d} d | |d}| t j || } |jj|| | d |ddkr.|jj |j|d |dd q.WqW|S)z Performs a single optimization step. Arguments: closure (`Callable`, *optional*): A closure that reevaluates the model and returns the loss. Nr;zJAdam does not support sparse gradients, please consider SparseAdam insteadrstepexp_avg exp_avg_sqr<r g?)alpha)valuer=r(r?r>g) param_groupsgraddata is_sparse RuntimeErrorstatelentorch zeros_likemul_add_Zaddcmul_sqrtr Zaddcdiv_)rGrIlossgroupprPrTrKrLbeta1Zbeta2ZdenomZ step_sizeZbias_correction1Zbias_correction2rrrrJCs<      (z AdamW.stepr7r8)r6r_r9r:TF)N)__name__ __module__ __qualname____doc__rr parameter ParameterrrboolrFrrJ __classcell__rr)rHrr5s0r5c s\eZdZdZdfd d Zed d ZeddZeddZeddZ dddZ Z S) Adafactora! AdaFactor pytorch implementation can be used as a drop in replacement for Adam original fairseq code: https://github.com/pytorch/fairseq/blob/master/fairseq/optim/adafactor.py Paper: *Adafactor: Adaptive Learning Rates with Sublinear Memory Cost* https://arxiv.org/abs/1804.04235 Note that this optimizer internally adjusts the learning rate depending on the `scale_parameter`, `relative_step` and `warmup_init` options. To use a manual (external) learning rate schedule you should set `scale_parameter=False` and `relative_step=False`. Arguments: params (`Iterable[nn.parameter.Parameter]`): Iterable of parameters to optimize or dictionaries defining parameter groups. lr (`float`, *optional*): The external learning rate. eps (`Tuple[float, float]`, *optional*, defaults to (1e-30, 1e-3)): Regularization constants for square gradient and parameter scale respectively clip_threshold (`float`, *optional*, defaults 1.0): Threshold of root mean square of final gradient update decay_rate (`float`, *optional*, defaults to -0.8): Coefficient used to compute running averages of square beta1 (`float`, *optional*): Coefficient used for computing running averages of gradient weight_decay (`float`, *optional*, defaults to 0): Weight decay (L2 penalty) scale_parameter (`bool`, *optional*, defaults to `True`): If True, learning rate is scaled by root mean square relative_step (`bool`, *optional*, defaults to `True`): If True, time-dependent learning rate is computed instead of external learning rate warmup_init (`bool`, *optional*, defaults to `False`): Time-dependent learning rate computation depends on whether warm-up initialization is being used This implementation handles low-precision (FP16, bfloat) values, but we have not thoroughly tested. Recommended T5 finetuning settings (https://discuss.huggingface.co/t/t5-finetuning-tips/684/3): - Training without LR warmup or clip_threshold is not recommended. - use scheduled LR warm-up to fixed LR - use clip_threshold=1.0 (https://arxiv.org/abs/1804.04235) - Disable relative updates - Use scale_parameter=False - Additional optimizer operations like gradient clipping should not be used alongside Adafactor Example: ```python Adafactor(model.parameters(), scale_parameter=False, relative_step=False, warmup_init=False, lr=1e-3) ``` Others reported the following combination to work well: ```python Adafactor(model.parameters(), scale_parameter=True, relative_step=True, warmup_init=True, lr=None) ``` When using `lr=None` with [`Trainer`] you will most likely need to use [`~optimization.AdafactorSchedule`] scheduler as following: ```python from transformers.optimization import Adafactor, AdafactorSchedule optimizer = Adafactor(model.parameters(), scale_parameter=True, relative_step=True, warmup_init=True, lr=None) lr_scheduler = AdafactorSchedule(optimizer) trainer = Trainer(..., optimizers=(optimizer, lr_scheduler)) ``` Usage: ```python # replace AdamW with Adafactor optimizer = Adafactor( model.parameters(), lr=1e-3, eps=(1e-30, 1e-3), clip_threshold=1.0, decay_rate=-0.8, beta1=None, weight_decay=0.0, relative_step=False, scale_parameter=False, warmup_init=False, ) ```NKH9MbP??g?TFc sZtd|dk r| rtd| r.| r.tdt|||||||| | d } tj|| dS)Nz torch>=1.5.0z;Cannot combine manual `lr` and `relative_step=True` optionsz0`warmup_init=True` requires `relative_step=True`) r(r=clip_threshold decay_rater^r>scale_parameter relative_step warmup_init)r r.rDrErF) rGr;r(r=rmrnr^r>rorprqr-)rHrrrFs   zAdafactor.__init__cCsj|d}|dr@|dr$d|dnd}t|dtj|d}d}|drbt|d d |d }||S) Nr(rprqgư>rJg{Gz?g?ror=r RMS)minr rZr) param_groupZ param_stateZ rel_step_szZmin_stepZ param_scalerrr_get_lrszAdafactor._get_lrcCs t|dk}|ddk }||fS)Nr^)rU)rtZ param_shapefactoreduse_first_momentrrr _get_optionss  zAdafactor._get_optionscCs|jd|jdS)Nrvg?)ZnormZnumel)Ztensorrrr_rmsszAdafactor._rmscCs6||jdddjjd}|jdj}tj||S)Nr T)dimZkeepdimrvr|)meanZrsqrt_Z unsqueezersqrtrVmul)exp_avg_sq_rowexp_avg_sq_colZr_factorZc_factorrrr_approx_sq_grad szAdafactor._approx_sq_gradcCsBd}|dk r|}x(|jD]}x|dD]}|jdkr@q.|jj}|jtjtjhkrb|j}|jrpt d|j |}|j }|j ||\}} t |dkrd|d<| rtj||d<|rtj|ddj||d<tj|dd|ddj||d <ntj||d <d|d <nV| r4|dj||d<|r`|dj||d<|d j||d <n|d j||d <|j} |jjtjtjhkr| j} |dd7<|j| |d <|j||} d tj|d|d} |d |dd} |rZ|d}|d }|j| j| jddd | d|j| j| jddd | d|j||} | j|n.|d }|j| j| d | d|jj|} | j|j| |djd d| j| | r|d}|j|dj| d|dd|} |ddkr | j| |d | d| j| |jjtjtjhkr.|jj| q.WqW|S)z Performs a single optimization step Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. Nr;z,Adafactor does not support sparse gradients.rrJrKr rrvrrLrrg?rnr=)r{)rMrm)rsr^r>r|r}r|r|r})rOrPrQZdtyperVZfloat16Zbfloat16rrRrSrTshaperyrUrWZzerosZtorzrur powrXrYr~rrZdiv_Zclamp_Zcopy_)rGrIr[r\r]rPrTZ grad_shaperwrxZ p_data_fp32r(Zbeta2tupdaterrrLrKrrrrJsx  *          zAdafactor.steprirj皙) NrrkrNrlTTF)N) r`rarbrcrF staticmethodruryrzrrJrgrr)rHrrhsS   rhcs*eZdZdZdfdd ZddZZS)AdafactorSchedulea8 Since [`~optimization.Adafactor`] performs its own scheduling, if the training loop relies on a scheduler (e.g., for logging), this class creates a proxy object that retrieves the current lr values from the optimizer. It returns `initial_lr` during startup and the actual `lr` during stepping. csLfdd}x|jD] }|d<qWtj||x|jD] }|d=q:WdS)NcsS)Nr)r) initial_lrrrrzsz-AdafactorSchedule.__init__..lr_lambdar)rOrErF)rGrrrr\)rH)rrrFys     zAdafactorSchedule.__init__cs0|jfddjD}t|dkr,|j}|S)Ncs8g|]0}|ddjdk rj|j|ddqS)r;rN)rPrurT).0r\)optrr sz,AdafactorSchedule.get_lr..r)rrOrUZbase_lrs)rGZlrsr)rrget_lrs    zAdafactorSchedule.get_lr)r)r`rarbrcrFrrgrr)rHrrqs rcCs t||S)aX Get a proxy schedule for [`~optimization.Adafactor`] Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. initial_lr (`float`, *optional*, defaults to 0.0): Initial lr Return: [`~optimization.Adafactor`] proxy schedule object. )r)rrrrrget_adafactor_schedulesrr|)r|r|)r|r|)r|r|)rr|r|)r r|r|)r&r'r|)NN)r)-rcr rAtypingrrrrrrVrZ torch.optimrZtorch.optim.lr_schedulerr Z trainer_utilsr utilsr Zutils.versionsr get_loggerr`loggerrrrrrr$r%r/ZLINEARZCOSINEZCOSINE_WITH_RESTARTSZ POLYNOMIALr2r3r1strr4r5rhrrrrrrs<        "# 2 &$pq