o v_f:@shdZddlZddlZddlmZddlmZddlmZGdddZ dd Z Gd d d Z d d Z dS)z Copyright (c) 2022, salesforce.com, inc. All rights reserved. SPDX-License-Identifier: BSD-3-Clause For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause N)Dict) OmegaConf)registryc@seZdZddZddZddZeddZed d Zed d Z d dZ ddZ e ddZ e ddZe ddZddZddZddZdS)ConfigcCspi|_||_td|||jj}t|jj}| |}|j |fi|}| |}t |||||_dS)N configuration) configargsrregister_build_opt_listoptionsrloadcfg_pathbuild_runner_configbuild_model_configbuild_dataset_configmerge)selfrZ user_configr runner_config model_configdataset_configrA/mnt/petrelfs/wufan/project/UnimerDemo/unimernet/common/config.py__init__s    zConfig.__init__cCst}||dS)z This method validates the configuration, such that 1) all the user specified options are valid; 2) no type mismatches between the user specified options and the config. N)create_runner_config_validatorvalidate)rrZrunner_config_validatorrrr_validate_runner_config+szConfig._validate_runner_configcCs||}t|SN)_convert_to_dot_listr from_dotlist)roptsZ opts_dot_listrrrr 4s  zConfig._build_opt_listcKs|dd}|dusJdt|j}|dus!Jd|jd|dd}|s/|dd}|dus7Jd|j|d}t}t|t|d|di}|S) Nmodelz!Missing model configuration file.zModel 'z' has not been registered.zmodel.model_type model_typezMissing model_type.)r!) getrget_model_classarchdefault_config_pathrcreaterr )rkwargsr model_clsr!Zmodel_config_pathrrrrr8s      zConfig.build_model_configcCs d|jiS)Nrun)r)rrrrrSs zConfig.build_runner_configc Cs||dd}|durtdt}|D]'}t|}||dd}|j|d}t|t|d||d|ii}q|S)Ndatasetsz?Expecting 'datasets' as the root key for dataset configuration.typedefaultr,) r"KeyErrorrr&rget_builder_classr%rr )rr+r dataset_name builder_clsZdataset_config_typeZdataset_config_pathrrrrWs$  zConfig.build_dataset_configcCs^|durg}t|dkr|S|dddk}|r|Sddt|ddd|dddDS)Nr=cSsg|] \}}|d|qS)r3r).0optvaluerrr ~sz/Config._convert_to_dot_list..)lenfindzip)rrZ has_equalrrrrrs (zConfig._convert_to_dot_listcCs|jSrr*rrrr get_configszConfig.get_configcC|jjSr)rr)r>rrrrun_cfgzConfig.run_cfgcCr@r)rr+r>rrr datasets_cfgrBzConfig.datasets_cfgcCr@r)rr r>rrr model_cfgrBzConfig.model_cfgcCstdt||jjtd|jj}|D])}||jjvr:td|d|jj|}t||qtd|dqtdt||jjdS)Nz# ===== Running Parameters =====z# ====== Dataset Attributes ======z ======== z =======zNo dataset named 'z' in config. Skippingz! ====== Model Attributes ======)logginginfo_convert_node_to_jsonrr)r+warningr )rr+datasetrrrr pretty_prints     zConfig.pretty_printcCstj|dd}tj|dddS)NT)resolve)indent sort_keys)r to_containerjsondumps)rnode containerrrrrGszConfig._convert_node_to_jsoncCs t|jSr)rrOrr>rrrto_dicts zConfig.to_dictN)__name__ __module__ __qualname__rrr staticmethodrrrrr?propertyrArCrDrJrGrTrrrrrs*        rcCs t|Sr)rrO)rRrrr node_to_dicts rZc@sfeZdZdZGdddZddZddZdefd d Zd d Z dddZ ddZ ddZ ddZ d S)ConfigValidatora This is a preliminary implementation to centralize and validate the configuration. May be altered in the future. A helper class to validate configurations from yaml file. This serves the following purposes: 1. Ensure all the options in the yaml are defined, raise error if not. 2. when type mismatches are found, the validator will raise an error. 3. a central place to store and display helpful messages for supported configurations. c@seZdZdddZddZdS)zConfigValidator._ArgumentNcCs"||_d|_||_||_||_dSr)namevalchoicesr,help)rr\r^r,r_rrrrs  z"ConfigValidator._Argument.__init__cCsh|jd|j}|jdur|d|jd7}|jdur$|d|j7}|jdur2|d|jd7}|S)Nr3z, ()z , choices: )r\r]r,r^r_)rsrrr__str__s   z!ConfigValidator._Argument.__str__)NNN)rUrVrWrrbrrrr _Arguments  rccCs||_t|_d|_dSr) descriptiondict arguments parsed_args)rrdrrrrs zConfigValidator.__init__cCs|jdus Jd|j|S)NzNo arguments parsed yet.)rg)rkeyrrr __getitem__s zConfigValidator.__getitem__returncCs|Sr) format_helpr>rrrrbszConfigValidator.__str__cOs|j|i||j|d<dS)zH Assume the first argument is the name of the argument. rN)rcrf)rrr'rrr add_argumentszConfigValidator.add_argumentNc Cs|D]_\}}||jvsJ|d|d|j|jdurEz|j|||j|_WntyDt|d|j|jdw|j|jdurc||j|jvscJ|d|j|jdq|S)zP Convert yaml config (dict-like) to list, required by argparse. z0 is not a valid argument. Support arguments are .Nz is not a valid z must be one of )itemsrfformat_argumentsr,r] ValueErrorr^)rrkvrrrrs   zConfigValidator.validatecCstddt|jDS)NcSsg|]}|qSrr)r5rqrrrr8sz4ConfigValidator.format_arguments..)strsortedrfkeysr>rrrrosz ConfigValidator.format_argumentscCst|j}|d|S)Nz, available arguments: )rsrdro)rZhelp_msgrrrrks zConfigValidator.format_helpcCst|dSr)printrkr>rrr print_helpszConfigValidator.print_helpr)rUrVrW__doc__rcrrirsrbrlrrorkrwrrrrr[s   r[cCs`tdd}|jdtddgdd|jdtttfd d |jd td d |jd tdd |jdtdd t}|jdt|d|dt }|jdt|d|d|jdtdd |jdtdd |jdtdd |jdtdd |jdtdd |jdtd d |jd!td"d |jd#d$d%|jd&td'd |jd(td)d |jd*td+d |jd,d-d%|jd.t d/d |jd0t d1d |jd2t d3d |jd4td5d |jd6td7d8gd9d|jd:td;d |jdt d=|jd?t d@d |jdAtdBd |jdCtdDd |jdEtdFd |jdGtdHd |jdItdJdKgdLd|jdMtdNd |S)ONzRunner configurations)rdrunnerZ runner_baseZ runner_iterzRunner to use. The "runner_base" uses epoch-based training while iter-based runner runs based on iters. Default: runner_base)r,r^r_Ztrain_dataset_ratioszRatios of training dataset. This is used in iteration-based runner. Do not support for epoch-based runner because how to define an epoch becomes tricky. Default: None)r,r_Z max_itersz$Maximum number of iterations to run.Z max_epochz Maximum number of epochs to run.iters_per_inner_epochzRNumber of iterations per inner epoch. This is required when runner is runner_iter.Zlr_schedz'Learning rate scheduler to use, from {}taskzTask to use, from {}Zinit_lrzTInitial learning rate. This will be the learning rate after warmup and before decay.min_lrz$Minimum learning rate (after decay).Z warmup_lrz"Starting learning rate for warmup.Z lr_decay_ratezOLearning rate decay rate. Required if using a decaying learning rate scheduler. weight_decayzWeight decay rate.Zbatch_size_trainzTraining batch size.Zbatch_size_evalz8Evaluation batch size, including validation and testing. num_workersz#Number of workers for data loading.)r_ warmup_stepsz>Number of warmup steps. Required if a warmup schedule is used.seedz Random seed. output_dirz.Output directory to save checkpoints and logs.evaluatezLWhether to only evaluate the model. If true, training will not be performed.Z train_splitszSplits to use for training.Z valid_splitszHSplits to use for validation. If not provided, will skip the validation.Z test_splitszBSplits to use for testing. If not provided, will skip the testing.accum_grad_itersz0Number of iterations to accumulate gradient for.devicecpucudaz2Device to use. Support 'cuda' or 'cpu' as for now. world_sizez-Number of processes participating in the job.dist_urlr. distributedZuse_dist_eval_samplerzs    X