o
    i"                     @   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	m
Z
 ddlmZ ddlmZ ddlmZ ddd	Zd
d ZedkrNeejdd  dS dS )zLanguage model training script.    N)__version__)dynamic_import_lm)dynamic_import_optimizer)dynamic_import_schedulerTc                 C   sD  | du rt jdt jt jd} | jdddd | jddd	d | jd
ddd | jddtdd | jddg ddd | jddtddgdd | jdt|dd | jddtdd | jdt|d d | jd!dtd"d | jd#d$d%d&d'd( | jd)d*d+td,d | jd-dtd&d.d/ | jd0d1td2d | jd3t|d4d | jd5t|d6d | jd7td8d9 | jd:tdd;d< | jd=d>td?d | jd@d+td&dAd/ | jdBdCtdDdEd< | jdFtddGd< | jdHdItdJdKd< | jdLdMtd&dNd/ | jdOdPtd&dQd/ | jdRddSdTdU dVdW | jdXdYtdZd[d< | jd\td]d^d< | jd_td`dad< | S )bzGet parser.Nz0Train a new language model on one CPU or one GPU)descriptionconfig_file_parser_classformatter_classz--configTzconfig file path)is_config_filehelpz	--config2zCsecond config file path that overwrites the settings in `--config`.z	--config3zRthird config file path that overwrites the settings in `--config` and `--config2`.z--ngpuz5Number of GPUs. If not given, use all visible devices)defaulttyper
   z--train-dtypefloat32)float16r   float64O0O1O2O3zData type for training (only pytorch backend). O0,O1,.. flags require apex. See https://nvidia.github.io/apex/amp.html#opt-levels)r   choicesr
   z	--backendchainerpytorchzBackend library)r   r   r   r
   z--outdirzOutput directory)r   requiredr
   z--debugmode   	Debugmodez--dict
Dictionaryz--seedzRandom seedz--resumez-r ?z!Resume the training from snapshot)r   nargsr
   z	--verbosez-Vr   zVerbose optionz--tensorboard-dirzTensorboard log dir path)r   r   r   r
   z--report-interval-itersd   zReport interval iterationsz--train-labelzFilename of train label dataz--valid-labelz!Filename of validation label dataz--test-labelzFilename of test label data)r   r
   z--dump-hdf5-pathz+Path to dump a preprocessed dataset as hdf5)r   r   r
   z--optsgd	Optimizerz--sortagradzFHow many epochs to use sortagrad for. 0 = deactivated, -1 = all epochsz--batchsizez-bi,  z%Number of examples in each mini-batchz--accum-gradz Number of gradient accumuerationz--epochz-e   z*Number of sweeps over the dataset to trainz--early-stop-criterionzvalidation/main/lossz=Value to monitor to trigger an early stopping of the trainingz
--patience   zINumber of epochs to wait without improvement before stopping the trainingz--schedulersappendc                 S   s
   |  dS )N=)split)kv r'   G/home/ubuntu/.local/lib/python3.10/site-packages/espnet/bin/lm_train.py<lambda>   s   
 zget_parser.<locals>.<lambda>zoptimizer schedulers, you can configure params like: <optimizer-param>-<scheduler-name>-<schduler-param> e.g., "--schedulers lr=noam --lr-noam-warmup 1000".)r   actionr   r
   z
--gradclipz-c   zGradient norm threshold to clipz--maxlen(   z0Batch size is reduced if the input sequence > MLz--model-moduler   zOmodel defined module (default: espnet.nets.xxx_backend.lm.default:DefaultRNNLM))	configargparseArgumentParserYAMLConfigFileParserArgumentDefaultsHelpFormatteraddadd_argumentintstrfloat)parserr   r'   r'   r(   
get_parser   s(  	r7   c              	   C   s  t  }|| \}}|jdkr|jdkrtd|j d|jdkr0|jdv r0td|j dt|j|j}|	| |j
d	urS|j
D ]\}}t|}|	|| qDt|j|j}|	| || }t|_|jdkrutjtjd
d ntjtjd
d td |jd	u rtjd}	|	d	urt|	d}
n.td ztjddgtjtjd}W n tjt fy   d}
Y nw t|j!" dd }
|
|_n|j}
t#d|
  t#dtjdd  |j$}t%$| t&j%$| t'|j(d}|) }W d	   n	1 sw   Y  dd |D }|*dd |+d dd t,|D |_-t||_.t#d |j  |jdkrFdd!l/m0} || d	S |jd"krXdd!l1m0} || d	S td#)$z	Train LM.r   r   z/chainer backend does not support --train-dtype z.Use --dtype float32.r   )r   r   r   r   r   z--train-dtype z" does not support the CPU backend.Nz>%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s)levelformatzSkip DEBUG/INFO messagesCUDA_VISIBLE_DEVICES,z CUDA_VISIBLE_DEVICES is not set.z
nvidia-smiz-L)stdoutstderr
r   zngpu: zpython path = 
PYTHONPATHz(None)rbc                 S   s    g | ]}| d dd qS )zutf-8 r   )decoder%   ).0entryr'   r'   r(   
<listcomp>  s     zmain.<locals>.<listcomp>z<blank>z<eos>c                 S   s   i | ]\}}||qS r'   r'   )rC   ixr'   r'   r(   
<dictcomp>  s    zmain.<locals>.<dictcomp>z
backend = )trainr   z'Only chainer and pytorch are supported.)2r7   parse_known_argsbackendtrain_dtypeNotImplementedErrorngpu
ValueErrorr   model_moduleadd_arguments
schedulersr   r   opt
parse_argsr   versionverboseloggingbasicConfigINFOWARNwarningosenvirongetlenr%   
subprocessrunPIPECalledProcessErrorFileNotFoundErrorr=   rB   infoseedrandomnpopendict	readlinesinsertr#   	enumeratechar_list_dictn_vocabespnet.lm.chainer_backend.lmrI   espnet.lm.pytorch_backend.lm)cmd_argsr6   args_model_classkvscheduler_class	opt_classcvdrN   pnseedf
dictionary	char_listrI   r'   r'   r(   main   s   












r   __main__r   )NT)__doc__rW   r\   rg   r`   sysr-   numpyrh   espnetr   espnet.nets.lm_interfacer   espnet.optimizer.factoryr   espnet.scheduler.schedulerr   r7   r   __name__argvr'   r'   r'   r(   <module>   s$   
 $a