U
    4Af                     @   s   d dl Z d dlmZmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ e
 sde sded	d
Zd
ZedddZG dd deZdS )    N)ArgumentParser	Namespace   )%SingleSentenceClassificationProcessor)TextClassificationPipeline)is_tf_availableis_torch_availablelogging   )BaseTransformersCLICommandzRAt least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI trainingFargsc                 C   s   t | S )z
    Factory function used to instantiate training command from provided command line arguments.

    Returns: TrainCommand
    )TrainCommandr    r   ?/tmp/pip-unpacked-wheel-zw5xktn0/transformers/commands/train.pytrain_command_factory    s    r   c                   @   sD   e Zd ZeedddZedddZdd Zd	d
 Z	dd Z
dS )r   )parserc                 C   s  | j ddd}|jdtddd |jdtd	d
d |jdtddd |jdtddd |jdddd |jdtddd |jdtddd |jdtddd |jdtd d!d |jd"td#d$d |jd%td&d'd |jd(td)d*d |jd+td,d-d |jd.td/d0d |jtd1 d2S )3z
        Register this command to argparse so it's available for the transformer-cli

        Args:
            parser: Root parser to register command-specific arguments
        Ztrainz$CLI tool to train a model on a task.)helpz--train_dataTzcpath to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences.)typerequiredr   z--column_labelr   z3Column of the dataset csv file with example labels.)r   defaultr   z--column_textr
   z2Column of the dataset csv file with example texts.z--column_idr   z0Column of the dataset csv file with example ids.z--skip_first_row
store_truez-Skip the first row of the csv file (headers).)actionr   z--validation_data zpath to validation dataset.z--validation_splitg?z^if validation dataset is not provided, fraction of train dataset to use as validation dataset.z--outputz./z path to saved the trained model.z--tasktext_classificationzTask to train the model on.z--modelzgoogle-bert/bert-base-uncasedz%Model's name or path to stored model.z--train_batch_size    zBatch size for training.z--valid_batch_size@   zBatch size for validation.z--learning_rategiUMu>zLearning rate.z--adam_epsilong:0yE>zEpsilon for Adam optimizer.)funcN)
add_parseradd_argumentstrintfloatset_defaultsr   )r   Ztrain_parserr   r   r   register_subcommand*   sn                     z TrainCommand.register_subcommandr   c                 C   sB  t d| _t rdnd| _tj|jdd |j| _|j| _|j	| _	|j
| _
| jd|j d|j  |jdkrt|j| _n|jd	krtn|jd
krt| jd|j  tj|j|j|j	|j
|jd| _d | _|jr| jd|j  tj|j|j|j	|j
|jd| _|j| _|j| _|j| _|j| _|j| _d S )Nztransformers-cli/trainingtfZtorchT)exist_okzLoading z pipeline for r   Ztoken_classificationZquestion_answeringzLoading dataset from )column_labelcolumn_text	column_idskip_first_rowz Loading validation dataset from )r	   Z
get_loggerloggerr   	frameworkosmakedirsoutputr'   r(   r)   infoZtaskmodelr   Zfrom_pretrainedpipelineNotImplementedErrorZ
train_data	ProcessorZcreate_from_csvr*   train_datasetvalid_datasetvalidation_datavalidation_splittrain_batch_sizevalid_batch_sizelearning_rateadam_epsilon)selfr   r   r   r   __init__]   sJ    


zTrainCommand.__init__c                 C   s   | j dkr|  S |  S )Nr%   )r,   run_tf	run_torchr=   r   r   r   run   s    
zTrainCommand.runc                 C   s   t d S )N)r3   rA   r   r   r   r@      s    zTrainCommand.run_torchc              	   C   s:   | j j| j| j| j| j| j| j| jd | j 	| j
 d S )N)r7   r8   r;   r<   r9   r:   )r2   Zfitr5   r6   r8   r;   r<   r9   r:   Zsave_pretrainedr/   rA   r   r   r   r?      s    zTrainCommand.run_tfN)__name__
__module____qualname__staticmethodr   r$   r   r>   rB   r@   r?   r   r   r   r   r   )   s   2-r   )r-   argparser   r   datar   r4   Z	pipelinesr   utilsr   r   r	   r   r   RuntimeErrorZUSE_XLAZUSE_AMPr   r   r   r   r   r   <module>   s   	