def compare_init_model_opts(opt: Opt, curr_opt: Opt): """ Print loud warning when `init_model` opts differ from previous configuration. """ if opt.get('init_model') is None: return opt['init_model'] = modelzoo_path(opt['datapath'], opt['init_model']) optfile = opt['init_model'] + '.opt' if not os.path.isfile(optfile): return init_model_opt = load_opt_file(optfile) extra_opts = {} different_opts = {} exempt_opts = [ 'model_file', 'dict_file', 'override', 'starttime', 'init_model', 'batchindex', ] # search through init model opts for k, v in init_model_opt.items(): if (k not in exempt_opts and k in init_model_opt and init_model_opt[k] != curr_opt.get(k)): if isinstance(v, list): if init_model_opt[k] != list(curr_opt[k]): different_opts[k] = ','.join([str(x) for x in v]) else: different_opts[k] = v # search through opts to load for k, v in curr_opt.items(): if k not in exempt_opts and k not in init_model_opt: if isinstance(v, list): extra_opts[k] = ','.join([str(x) for x in v]) else: extra_opts[k] = v # print warnings extra_strs = ['{}: {}'.format(k, v) for k, v in extra_opts.items()] if extra_strs: print('\n' + '*' * 75) print('[ WARNING ] : your model is being loaded with opts that do not ' 'exist in the model you are initializing the weights with: ' '{}'.format(','.join(extra_strs))) different_strs = [ '--{} {}'.format(k, v).replace('_', '-') for k, v in different_opts.items() ] if different_strs: print('\n' + '*' * 75) print('[ WARNING ] : your model is being loaded with opts that differ ' 'from the model you are initializing the weights with. Add the ' 'following args to your run command to change this: \n' '\n{}'.format(' '.join(different_strs))) print('*' * 75)
def load_pretrained_bi_encoder_opt(): opt = load_opt_file(PRETRAINED_BIENC_OPT_FILE) if isinstance(opt['fixed_candidates_path'], str): opt['fixed_candidates_path'] = PARLAI_DIR + opt['fixed_candidates_path'] opt['data_path'] = PARLAI_DIR + 'data' opt['datapath'] = PARLAI_DIR + 'data' opt['model_file'] = PRETRAINED_BI_MODEL_DIR + 'model' opt['dict_file'] = PRETRAINED_BI_MODEL_DIR + 'model.dict' opt['encode_candidate_vecs'] = False return opt
def _load_opts(self, opt): optfile = opt.get('init_opt') new_opt = load_opt_file(optfile) for key, value in new_opt.items(): # existing command line parameters take priority. if key not in opt: raise RuntimeError( 'Trying to set opt from file that does not exist: ' + str(key) ) if key not in opt['override']: opt[key] = value opt['override'][key] = value
def _load_known_opts(self, optfile, parsed): """ Pull in CLI args for proper models/tasks/etc. Called before args are parsed; ``_load_opts`` is used for actually overriding opts after they are parsed. """ new_opt = load_opt_file(optfile) for key, value in new_opt.items(): # existing command line parameters take priority. if key not in parsed or parsed[key] is None: parsed[key] = value
def get_model_name(opt): """ Get the model name from either `--model` or `--model-file`. """ model = opt.get('model', None) if model is None: # try to get model name from model opt file model_file = opt.get('model_file', None) if model_file is not None: model_file = modelzoo_path(opt.get('datapath'), model_file) optfile = model_file + '.opt' if os.path.isfile(optfile): new_opt = load_opt_file(optfile) model = new_opt.get('model', None) return model
def load_agent_module(opt: Opt): """ Load agent options and module from file if opt file exists. Checks to see if file exists opt['model_file'] + ".opt"; if so, load up the options from the file and use that to create an agent, loading the model type from that file and overriding any options specified in that file when instantiating the agent. If that file does not exist, return None. """ model_file = opt['model_file'] optfile = model_file + '.opt' if os.path.isfile(optfile): new_opt = load_opt_file(optfile) # TODO we need a better way to say these options are never copied... if 'datapath' in new_opt: # never use the datapath from an opt dump del new_opt['datapath'] if 'batchindex' in new_opt: # This saved variable can cause trouble if we switch to BS=1 at test time del new_opt['batchindex'] # only override opts specified in 'override' dict if opt.get('override'): for k, v in opt['override'].items(): if str(v) != str(new_opt.get(k, None)): print("[ warning: overriding opt['{}'] to {} (" "previously: {} )]".format(k, v, new_opt.get(k, None))) new_opt[k] = v model_class = get_agent_module(new_opt['model']) # check for model version if hasattr(model_class, 'model_version'): curr_version = new_opt.get('model_version', 0) if curr_version != model_class.model_version(): model = new_opt['model'] m = ('It looks like you are trying to load an older version of' ' the selected model. Change your model argument to use ' 'the old version from parlai/agents/legacy_agents: for ' 'example: `-m legacy:{m}:{v}` or ' '`--model parlai.agents.legacy_agents.{m}.{m}_v{v}:{c}`') if '.' not in model: # give specific error message if it's easy raise RuntimeError( m.format(m=model, v=curr_version, c=model_class.__name__)) else: # otherwise generic one raise RuntimeError( m.format(m='modelname', v=curr_version, c='ModelAgent')) if hasattr(model_class, 'upgrade_opt'): new_opt = model_class.upgrade_opt(new_opt) # add model arguments to new_opt if they aren't in new_opt already for k, v in opt.items(): if k not in new_opt: new_opt[k] = v new_opt['model_file'] = model_file if not new_opt.get('dict_file'): new_opt['dict_file'] = model_file + '.dict' elif new_opt.get('dict_file') and not os.path.isfile( new_opt['dict_file']): old_dict_file = new_opt['dict_file'] new_opt['dict_file'] = model_file + '.dict' if not os.path.isfile(new_opt['dict_file']): warn_once( 'WARNING: Neither the specified dict file ({}) nor the ' '`model_file`.dict file ({}) exists, check to make sure either ' 'is correct. This may manifest as a shape mismatch later ' 'on.'.format(old_dict_file, new_opt['dict_file'])) # if we want to load weights from --init-model, compare opts with # loaded ones compare_init_model_opts(opt, new_opt) return model_class(new_opt) else: return None